LLVM  4.0.0
SimplifyInstructions.cpp
Go to the documentation of this file.
1 //===------ SimplifyInstructions.cpp - Remove redundant instructions ------===//
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 //
10 // This is a utility pass used for testing the InstructionSimplify analysis.
11 // The analysis is applied to every instruction, and if it simplifies then the
12 // instruction is replaced by the simplification. If you are looking for a pass
13 // that performs serious instruction folding, use the instcombine pass instead.
14 //
15 //===----------------------------------------------------------------------===//
16 
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
24 #include "llvm/IR/DataLayout.h"
25 #include "llvm/IR/Dominators.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/Type.h"
28 #include "llvm/Pass.h"
30 #include "llvm/Transforms/Scalar.h"
31 using namespace llvm;
32 
33 #define DEBUG_TYPE "instsimplify"
34 
35 STATISTIC(NumSimplified, "Number of redundant instructions removed");
36 
37 static bool runImpl(Function &F, const DominatorTree *DT,
38  const TargetLibraryInfo *TLI, AssumptionCache *AC) {
39  const DataLayout &DL = F.getParent()->getDataLayout();
40  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
41  bool Changed = false;
42 
43  do {
44  for (BasicBlock *BB : depth_first(&F.getEntryBlock())) {
45  // Here be subtlety: the iterator must be incremented before the loop
46  // body (not sure why), so a range-for loop won't work here.
47  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
48  Instruction *I = &*BI++;
49  // The first time through the loop ToSimplify is empty and we try to
50  // simplify all instructions. On later iterations ToSimplify is not
51  // empty and we only bother simplifying instructions that are in it.
52  if (!ToSimplify->empty() && !ToSimplify->count(I))
53  continue;
54 
55  // Don't waste time simplifying unused instructions.
56  if (!I->use_empty()) {
57  if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
58  // Mark all uses for resimplification next time round the loop.
59  for (User *U : I->users())
60  Next->insert(cast<Instruction>(U));
61  I->replaceAllUsesWith(V);
62  ++NumSimplified;
63  Changed = true;
64  }
65  }
67  // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
68  // instruction, so simply incrementing the iterator does not work.
69  // When instructions get deleted re-iterate instead.
70  BI = BB->begin();
71  BE = BB->end();
72  Changed = true;
73  }
74  }
75  }
76 
77  // Place the list of instructions to simplify on the next loop iteration
78  // into ToSimplify.
79  std::swap(ToSimplify, Next);
80  Next->clear();
81  } while (!ToSimplify->empty());
82 
83  return Changed;
84 }
85 
86 namespace {
87  struct InstSimplifier : public FunctionPass {
88  static char ID; // Pass identification, replacement for typeid
89  InstSimplifier() : FunctionPass(ID) {
91  }
92 
93  void getAnalysisUsage(AnalysisUsage &AU) const override {
94  AU.setPreservesCFG();
98  }
99 
100  /// runOnFunction - Remove instructions that simplify.
101  bool runOnFunction(Function &F) override {
102  if (skipFunction(F))
103  return false;
104 
105  const DominatorTree *DT =
106  &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
107  const TargetLibraryInfo *TLI =
108  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
109  AssumptionCache *AC =
110  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
111  return runImpl(F, DT, TLI, AC);
112  }
113  };
114 }
115 
116 char InstSimplifier::ID = 0;
117 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
118  "Remove redundant instructions", false, false)
123  "Remove redundant instructions", false, false)
124 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
125 
126 // Public interface to the simplify instructions pass.
128  return new InstSimplifier();
129 }
130 
133  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
134  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
135  auto &AC = AM.getResult<AssumptionAnalysis>(F);
136  bool Changed = runImpl(F, &DT, &TLI, &AC);
137  if (!Changed)
138  return PreservedAnalyses::all();
139  // FIXME: This should also 'preserve the CFG'.
140  return PreservedAnalyses::none();
141 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:102
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:380
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of .assume calls within a function.
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:189
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
char & InstructionSimplifierID
#define F(x, y, z)
Definition: MD5.cpp:51
INITIALIZE_PASS_BEGIN(InstSimplifier,"instsimplify","Remove redundant instructions", false, false) INITIALIZE_PASS_END(InstSimplifier
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:401
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:96
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:110
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:653
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
static bool runImpl(Function &F, const DominatorTree *DT, const TargetLibraryInfo *TLI, AssumptionCache *AC)
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
void initializeInstSimplifierPass(PassRegistry &)
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:98
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:355
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:113
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:425
Provides information about what library functions are available for the current target.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:586
iterator_range< user_iterator > users()
Definition: Value.h:370
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:384
#define I(x, y, z)
Definition: MD5.cpp:54
Analysis pass providing the TargetLibraryInfo.
iterator_range< df_iterator< T > > depth_first(const T &G)
bool use_empty() const
Definition: Value.h:299
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:537
LLVM Value Representation.
Definition: Value.h:71
inst_range instructions(Function *F)
Definition: InstIterator.h:132
A container for analyses that lazily runs them and caches their results.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:217
FunctionPass * createInstructionSimplifierPass()
Remove redundant false