]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Utils/SimplifyInstructions.cpp
dts: Update our device tree sources file fomr Linux 4.13
[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/Scalar.h"
31 #include "llvm/Transforms/Utils/Local.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 = SimplifyInstruction(I, SQ, ORE)) {
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         }
66         if (RecursivelyDeleteTriviallyDeadInstructions(I, SQ.TLI)) {
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) {
90       initializeInstSimplifierPass(*PassRegistry::getPassRegistry());
91     }
92
93     void getAnalysisUsage(AnalysisUsage &AU) const override {
94       AU.setPreservesCFG();
95       AU.addRequired<DominatorTreeWrapperPass>();
96       AU.addRequired<AssumptionCacheTracker>();
97       AU.addRequired<TargetLibraryInfoWrapperPass>();
98       AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
99     }
100
101     /// runOnFunction - Remove instructions that simplify.
102     bool runOnFunction(Function &F) override {
103       if (skipFunction(F))
104         return false;
105
106       const DominatorTree *DT =
107           &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
108       const TargetLibraryInfo *TLI =
109           &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
110       AssumptionCache *AC =
111           &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
112       OptimizationRemarkEmitter *ORE =
113           &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
114       const DataLayout &DL = F.getParent()->getDataLayout();
115       const SimplifyQuery SQ(DL, TLI, DT, AC);
116       return runImpl(F, SQ, ORE);
117     }
118   };
119 }
120
121 char InstSimplifier::ID = 0;
122 INITIALIZE_PASS_BEGIN(InstSimplifier, "instsimplify",
123                       "Remove redundant instructions", false, false)
124 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
125 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
126 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
127 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
128 INITIALIZE_PASS_END(InstSimplifier, "instsimplify",
129                     "Remove redundant instructions", false, false)
130 char &llvm::InstructionSimplifierID = InstSimplifier::ID;
131
132 // Public interface to the simplify instructions pass.
133 FunctionPass *llvm::createInstructionSimplifierPass() {
134   return new InstSimplifier();
135 }
136
137 PreservedAnalyses InstSimplifierPass::run(Function &F,
138                                       FunctionAnalysisManager &AM) {
139   auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
140   auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
141   auto &AC = AM.getResult<AssumptionAnalysis>(F);
142   auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
143   const DataLayout &DL = F.getParent()->getDataLayout();
144   const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
145   bool Changed = runImpl(F, SQ, &ORE);
146   if (!Changed)
147     return PreservedAnalyses::all();
148
149   PreservedAnalyses PA;
150   PA.preserveSet<CFGAnalyses>();
151   return PA;
152 }