LLVM  3.7.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 
17 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Dominators.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/Type.h"
27 #include "llvm/Pass.h"
30 using namespace llvm;
31 
32 #define DEBUG_TYPE "instsimplify"
33 
34 STATISTIC(NumSimplified, "Number of redundant instructions removed");
35 
36 namespace {
37  struct InstSimplifier : public FunctionPass {
38  static char ID; // Pass identification, replacement for typeid
39  InstSimplifier() : FunctionPass(ID) {
41  }
42 
43  void getAnalysisUsage(AnalysisUsage &AU) const override {
44  AU.setPreservesCFG();
47  }
48 
49  /// runOnFunction - Remove instructions that simplify.
50  bool runOnFunction(Function &F) override {
51  const DominatorTreeWrapperPass *DTWP =
52  getAnalysisIfAvailable<DominatorTreeWrapperPass>();
53  const DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
54  const DataLayout &DL = F.getParent()->getDataLayout();
55  const TargetLibraryInfo *TLI =
56  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
57  AssumptionCache *AC =
58  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
59  SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
60  bool Changed = false;
61 
62  do {
63  for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
64  // Here be subtlety: the iterator must be incremented before the loop
65  // body (not sure why), so a range-for loop won't work here.
66  for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
67  Instruction *I = BI++;
68  // The first time through the loop ToSimplify is empty and we try to
69  // simplify all instructions. On later iterations ToSimplify is not
70  // empty and we only bother simplifying instructions that are in it.
71  if (!ToSimplify->empty() && !ToSimplify->count(I))
72  continue;
73  // Don't waste time simplifying unused instructions.
74  if (!I->use_empty())
75  if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
76  // Mark all uses for resimplification next time round the loop.
77  for (User *U : I->users())
78  Next->insert(cast<Instruction>(U));
79  I->replaceAllUsesWith(V);
80  ++NumSimplified;
81  Changed = true;
82  }
84  if (res) {
85  // RecursivelyDeleteTriviallyDeadInstruction can remove
86  // more than one instruction, so simply incrementing the
87  // iterator does not work. When instructions get deleted
88  // re-iterate instead.
89  BI = BB->begin(); BE = BB->end();
90  Changed |= res;
91  }
92  }
93 
94  // Place the list of instructions to simplify on the next loop iteration
95  // into ToSimplify.
96  std::swap(ToSimplify, Next);
97  Next->clear();
98  } while (!ToSimplify->empty());
99 
100  return Changed;
101  }
102  };
103 }
104 
105 char InstSimplifier::ID = 0;
106 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
107  "Remove redundant instructions", false, false)
111  "Remove redundant instructions", false, false)
112 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
113 
114 // Public interface to the simplify instructions pass.
116  return new InstSimplifier();
117 }
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallPtrSet.h:78
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:276
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of .assume calls within a function.
F(f)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
DominatorTree & getDomTree()
Definition: Dominators.h:213
char & InstructionSimplifierID
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:351
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
void initializeInstSimplifierPass(PassRegistry &)
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr)
RecursivelyDeleteTriviallyDeadInstructions - If the specified value is a trivially dead instruction...
Definition: Local.cpp:340
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
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:263
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:576
iterator_range< user_iterator > users()
Definition: Value.h:300
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
#define I(x, y, z)
Definition: MD5.cpp:54
iterator_range< df_iterator< T > > depth_first(const T &G)
bool use_empty() const
Definition: Value.h:275
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
FunctionPass * createInstructionSimplifierPass()
Remove redundant instructions
Remove redundant false