Make use of @llvm.assume in ValueTracking (computeKnownBits, etc.)
[oota-llvm.git] / lib / Transforms / Utils / SimplifyInstructions.cpp
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"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AssumptionTracker.h"
22 #include "llvm/Analysis/InstructionSimplify.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"
28 #include "llvm/Target/TargetLibraryInfo.h"
29 #include "llvm/Transforms/Utils/Local.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) {
40       initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
41     }
42
43     void getAnalysisUsage(AnalysisUsage &AU) const override {
44       AU.setPreservesCFG();
45       AU.addRequired<AssumptionTracker>();
46       AU.addRequired<TargetLibraryInfo>();
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       DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
55       const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr;
56       const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
57       AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();
58       SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
59       bool Changed = false;
60
61       do {
62         for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
63           // Here be subtlety: the iterator must be incremented before the loop
64           // body (not sure why), so a range-for loop won't work here.
65           for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
66             Instruction *I = BI++;
67             // The first time through the loop ToSimplify is empty and we try to
68             // simplify all instructions.  On later iterations ToSimplify is not
69             // empty and we only bother simplifying instructions that are in it.
70             if (!ToSimplify->empty() && !ToSimplify->count(I))
71               continue;
72             // Don't waste time simplifying unused instructions.
73             if (!I->use_empty())
74               if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AT)) {
75                 // Mark all uses for resimplification next time round the loop.
76                 for (User *U : I->users())
77                   Next->insert(cast<Instruction>(U));
78                 I->replaceAllUsesWith(V);
79                 ++NumSimplified;
80                 Changed = true;
81               }
82             bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
83             if (res)  {
84               // RecursivelyDeleteTriviallyDeadInstruction can remove
85               // more than one instruction, so simply incrementing the
86               // iterator does not work. When instructions get deleted
87               // re-iterate instead.
88               BI = BB->begin(); BE = BB->end();
89               Changed |= res;
90             }
91           }
92
93         // Place the list of instructions to simplify on the next loop iteration
94         // into ToSimplify.
95         std::swap(ToSimplify, Next);
96         Next->clear();
97       } while (!ToSimplify->empty());
98
99       return Changed;
100     }
101   };
102 }
103
104 char InstSimplifier::ID = 0;
105 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
106                       "Remove redundant instructions", false, false)
107 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker)
108 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
109 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
110                     "Remove redundant instructions", false, false)
111 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
112
113 // Public interface to the simplify instructions pass.
114 FunctionPass *llvm::createInstructionSimplifierPass() {
115   return new InstSimplifier();
116 }