]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Utils/SimplifyInstructions.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r301441, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/Utils/SimplifyInstructions.h"
18 #include "llvm/ADT/DepthFirstIterator.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Statistic.h"
21 #include "llvm/Analysis/AssumptionCache.h"
22 #include "llvm/Analysis/InstructionSimplify.h"
23 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Function.h"
28 #include "llvm/IR/Type.h"
29 #include "llvm/Pass.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include "llvm/Transforms/Scalar.h"
32 using namespace llvm;
33
34 #define DEBUG_TYPE "instsimplify"
35
36 STATISTIC(NumSimplified, "Number of redundant instructions removed");
37
38 static bool runImpl(Function &F, const SimplifyQuery &SQ,
39                     OptimizationRemarkEmitter *ORE) {
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 =
58                   SimplifyInstruction(I, SQ.getWithInstruction(I), ORE)) {
59             // Mark all uses for resimplification next time round the loop.
60             for (User *U : I->users())
61               Next->insert(cast<Instruction>(U));
62             I->replaceAllUsesWith(V);
63             ++NumSimplified;
64             Changed = true;
65           }
66         }
67         if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
68           // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
69           // instruction, so simply incrementing the iterator does not work.
70           // When instructions get deleted re-iterate instead.
71           BI = BB->begin();
72           BE = BB->end();
73           Changed = true;
74         }
75       }
76     }
77
78     // Place the list of instructions to simplify on the next loop iteration
79     // into ToSimplify.
80     std::swap(ToSimplify, Next);
81     Next->clear();
82   } while (!ToSimplify->empty());
83
84   return Changed;
85 }
86
87 namespace {
88   struct InstSimplifier : public FunctionPass {
89     static char ID; // Pass identification, replacement for typeid
90     InstSimplifier() : FunctionPass(ID) {
91       initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
92     }
93
94     void getAnalysisUsage(AnalysisUsage &AU) const override {
95       AU.setPreservesCFG();
96       AU.addRequired<DominatorTreeWrapperPass>();
97       AU.addRequired<AssumptionCacheTracker>();
98       AU.addRequired<TargetLibraryInfoWrapperPass>();
99       AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
100     }
101
102     /// runOnFunction - Remove instructions that simplify.
103     bool runOnFunction(Function &F) override {
104       if (skipFunction(F))
105         return false;
106
107       const DominatorTree *DT =
108           &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
109       const TargetLibraryInfo *TLI =
110           &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
111       AssumptionCache *AC =
112           &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
113       OptimizationRemarkEmitter *ORE =
114           &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
115       const DataLayout &DL = F.getParent()->getDataLayout();
116       const SimplifyQuery SQ(DL, TLI, DT, AC);
117       return runImpl(F, SQ, ORE);
118     }
119   };
120 }
121
122 char InstSimplifier::ID = 0;
123 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
124                       "Remove redundant instructions", false, false)
125 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
126 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
127 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
128 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
129 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
130                     "Remove redundant instructions", false, false)
131 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
132
133 // Public interface to the simplify instructions pass.
134 FunctionPass *llvm::createInstructionSimplifierPass() {
135   return new InstSimplifier();
136 }
137
138 PreservedAnalyses InstSimplifierPass::run(Function &F,
139                                       FunctionAnalysisManager &AM) {
140   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
141   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
142   auto &AC = AM.getResult<AssumptionAnalysis>(F);
143   auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
144   const DataLayout &DL = F.getParent()->getDataLayout();
145   const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
146   bool Changed = runImpl(F, SQ, &ORE);
147   if (!Changed)
148     return PreservedAnalyses::all();
149
150   PreservedAnalyses PA;
151   PA.preserveSet<CFGAnalyses>();
152   return PA;
153 }