LLVM  9.0.0svn
InstSimplifyPass.cpp
Go to the documentation of this file.
1 //===- InstSimplifyPass.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "llvm/ADT/SmallPtrSet.h"
12 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Utils.h"
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "instsimplify"
27 
28 STATISTIC(NumSimplified, "Number of redundant instructions removed");
29 
30 static bool runImpl(Function &F, const SimplifyQuery &SQ,
32  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
33  bool Changed = false;
34 
35  do {
36  for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
37  // Here be subtlety: the iterator must be incremented before the loop
38  // body (not sure why), so a range-for loop won't work here.
39  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
40  Instruction *I = &*BI++;
41  // The first time through the loop ToSimplify is empty and we try to
42  // simplify all instructions. On later iterations ToSimplify is not
43  // empty and we only bother simplifying instructions that are in it.
44  if (!ToSimplify->empty() && !ToSimplify->count(I))
45  continue;
46 
47  // Don't waste time simplifying unused instructions.
48  if (!I->use_empty()) {
49  if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
50  // Mark all uses for resimplification next time round the loop.
51  for (User *U : I->users())
52  Next->insert(cast<Instruction>(U));
53  I->replaceAllUsesWith(V);
54  ++NumSimplified;
55  Changed = true;
56  }
57  }
59  // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
60  // instruction, so simply incrementing the iterator does not work.
61  // When instructions get deleted re-iterate instead.
62  BI = BB->begin();
63  BE = BB->end();
64  Changed = true;
65  }
66  }
67  }
68 
69  // Place the list of instructions to simplify on the next loop iteration
70  // into ToSimplify.
71  std::swap(ToSimplify, Next);
72  Next->clear();
73  } while (!ToSimplify->empty());
74 
75  return Changed;
76 }
77 
78 namespace {
79 struct InstSimplifyLegacyPass : public FunctionPass {
80  static char ID; // Pass identification, replacement for typeid
81  InstSimplifyLegacyPass() : FunctionPass(ID) {
83  }
84 
85  void getAnalysisUsage(AnalysisUsage &AU) const override {
86  AU.setPreservesCFG();
91  }
92 
93  /// runOnFunction - Remove instructions that simplify.
94  bool runOnFunction(Function &F) override {
95  if (skipFunction(F))
96  return false;
97 
98  const DominatorTree *DT =
99  &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
100  const TargetLibraryInfo *TLI =
101  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
102  AssumptionCache *AC =
103  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
105  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
106  const DataLayout &DL = F.getParent()->getDataLayout();
107  const SimplifyQuery SQ(DL, TLI, DT, AC);
108  return runImpl(F, SQ, ORE);
109  }
110 };
111 } // namespace
112 
114 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
115  "Remove redundant instructions", false, false)
120 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
121  "Remove redundant instructions", false, false)
122 
123 // Public interface to the simplify instructions pass.
125  return new InstSimplifyLegacyPass();
126 }
127 
130  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
131  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
132  auto &AC = AM.getResult<AssumptionAnalysis>(F);
134  const DataLayout &DL = F.getParent()->getDataLayout();
135  const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
136  bool Changed = runImpl(F, SQ, &ORE);
137  if (!Changed)
138  return PreservedAnalyses::all();
139 
141  PA.preserveSet<CFGAnalyses>();
142  return PA;
143 }
Defines passes for running instruction simplification across chunks of IR.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:776
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
F(f)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const TargetLibraryInfo * TLI
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:50
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
static bool runImpl(Function &F, const SimplifyQuery &SQ, OptimizationRemarkEmitter *ORE)
FunctionPass * createInstSimplifyLegacyPass()
Create a legacy pass that does instruction simplification on each instruction in a function...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:429
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
const BasicBlock & getEntryBlock() const
Definition: Function.h:645
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:434
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:159
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
A function analysis which provides an AssumptionCache.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
Provides information about what library functions are available for the current target.
instsimplify
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
iterator_range< user_iterator > users()
Definition: Value.h:399
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:114
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:189
#define I(x, y, z)
Definition: MD5.cpp:58
Analysis pass providing the TargetLibraryInfo.
iterator_range< df_iterator< T > > depth_first(const T &G)
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:565
LLVM Value Representation.
Definition: Value.h:72
OptimizationRemarkEmitter legacy analysis pass.
inst_range instructions(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
The optimization diagnostic interface.
bool use_empty() const
Definition: Value.h:322
INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify", "Remove redundant instructions", false, false) INITIALIZE_PASS_END(InstSimplifyLegacyPass