LLVM  8.0.1
InstSimplifyPass.cpp
Go to the documentation of this file.
1 //===- InstSimplifyPass.cpp -----------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
12 #include "llvm/ADT/SmallPtrSet.h"
13 #include "llvm/ADT/Statistic.h"
18 #include "llvm/IR/DataLayout.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Type.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Transforms/Utils.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "instsimplify"
28 
29 STATISTIC(NumSimplified, "Number of redundant instructions removed");
30 
31 static bool runImpl(Function &F, const SimplifyQuery &SQ,
33  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
34  bool Changed = false;
35 
36  do {
37  for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
38  // Here be subtlety: the iterator must be incremented before the loop
39  // body (not sure why), so a range-for loop won't work here.
40  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
41  Instruction *I = &*BI++;
42  // The first time through the loop ToSimplify is empty and we try to
43  // simplify all instructions. On later iterations ToSimplify is not
44  // empty and we only bother simplifying instructions that are in it.
45  if (!ToSimplify->empty() && !ToSimplify->count(I))
46  continue;
47 
48  // Don't waste time simplifying unused instructions.
49  if (!I->use_empty()) {
50  if (Value *V = SimplifyInstruction(I, SQ, ORE)) {
51  // Mark all uses for resimplification next time round the loop.
52  for (User *U : I->users())
53  Next->insert(cast<Instruction>(U));
54  I->replaceAllUsesWith(V);
55  ++NumSimplified;
56  Changed = true;
57  }
58  }
60  // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
61  // instruction, so simply incrementing the iterator does not work.
62  // When instructions get deleted re-iterate instead.
63  BI = BB->begin();
64  BE = BB->end();
65  Changed = true;
66  }
67  }
68  }
69 
70  // Place the list of instructions to simplify on the next loop iteration
71  // into ToSimplify.
72  std::swap(ToSimplify, Next);
73  Next->clear();
74  } while (!ToSimplify->empty());
75 
76  return Changed;
77 }
78 
79 namespace {
80 struct InstSimplifyLegacyPass : public FunctionPass {
81  static char ID; // Pass identification, replacement for typeid
82  InstSimplifyLegacyPass() : FunctionPass(ID) {
84  }
85 
86  void getAnalysisUsage(AnalysisUsage &AU) const override {
87  AU.setPreservesCFG();
92  }
93 
94  /// runOnFunction - Remove instructions that simplify.
95  bool runOnFunction(Function &F) override {
96  if (skipFunction(F))
97  return false;
98 
99  const DominatorTree *DT =
100  &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
101  const TargetLibraryInfo *TLI =
102  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
103  AssumptionCache *AC =
104  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
106  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
107  const DataLayout &DL = F.getParent()->getDataLayout();
108  const SimplifyQuery SQ(DL, TLI, DT, AC);
109  return runImpl(F, SQ, ORE);
110  }
111 };
112 } // namespace
113 
115 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
116  "Remove redundant instructions", false, false)
121 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
122  "Remove redundant instructions", false, false)
123 
124 // Public interface to the simplify instructions pass.
126  return new InstSimplifyLegacyPass();
127 }
128 
131  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
132  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
133  auto &AC = AM.getResult<AssumptionAnalysis>(F);
135  const DataLayout &DL = F.getParent()->getDataLayout();
136  const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
137  bool Changed = runImpl(F, SQ, &ORE);
138  if (!Changed)
139  return PreservedAnalyses::all();
140 
142  PA.preserveSet<CFGAnalyses>();
143  return PA;
144 }
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:111
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:770
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:231
F(f)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const TargetLibraryInfo * TLI
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:371
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:145
const BasicBlock & getEntryBlock() const
Definition: Function.h:640
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:92
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:382
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:430
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
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:418
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:286
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
iterator_range< user_iterator > users()
Definition: Value.h:400
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:190
#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:566
LLVM Value Representation.
Definition: Value.h:73
OptimizationRemarkEmitter legacy analysis pass.
inst_range instructions(Function *F)
Definition: InstIterator.h:134
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:260
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:323
INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify", "Remove redundant instructions", false, false) INITIALIZE_PASS_END(InstSimplifyLegacyPass