]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Transforms/Scalar/LoopInstSimplify.cpp
Merge ^/head r327165 through r327168.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Transforms / Scalar / LoopInstSimplify.cpp
1 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
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 pass performs lightweight instruction simplification on loop bodies.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Transforms/Scalar/LoopInstSimplify.h"
15 #include "llvm/ADT/PointerIntPair.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/AssumptionCache.h"
21 #include "llvm/Analysis/InstructionSimplify.h"
22 #include "llvm/Analysis/LoopInfo.h"
23 #include "llvm/Analysis/LoopPass.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/IR/BasicBlock.h"
26 #include "llvm/IR/CFG.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/Dominators.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/PassManager.h"
33 #include "llvm/IR/User.h"
34 #include "llvm/Pass.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Transforms/Scalar.h"
37 #include "llvm/Transforms/Utils/Local.h"
38 #include "llvm/Transforms/Utils/LoopUtils.h"
39 #include <algorithm>
40 #include <utility>
41
42 using namespace llvm;
43
44 #define DEBUG_TYPE "loop-instsimplify"
45
46 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
47
48 static bool SimplifyLoopInst(Loop *L, DominatorTree *DT, LoopInfo *LI,
49                              AssumptionCache *AC,
50                              const TargetLibraryInfo *TLI) {
51   SmallVector<BasicBlock *, 8> ExitBlocks;
52   L->getUniqueExitBlocks(ExitBlocks);
53   array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
54
55   SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
56
57   // The bit we are stealing from the pointer represents whether this basic
58   // block is the header of a subloop, in which case we only process its phis.
59   using WorklistItem = PointerIntPair<BasicBlock *, 1>;
60   SmallVector<WorklistItem, 16> VisitStack;
61   SmallPtrSet<BasicBlock *, 32> Visited;
62
63   bool Changed = false;
64   bool LocalChanged;
65   do {
66     LocalChanged = false;
67
68     VisitStack.clear();
69     Visited.clear();
70
71     VisitStack.push_back(WorklistItem(L->getHeader(), false));
72
73     while (!VisitStack.empty()) {
74       WorklistItem Item = VisitStack.pop_back_val();
75       BasicBlock *BB = Item.getPointer();
76       bool IsSubloopHeader = Item.getInt();
77       const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
78
79       // Simplify instructions in the current basic block.
80       for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
81         Instruction *I = &*BI++;
82
83         // The first time through the loop ToSimplify is empty and we try to
84         // simplify all instructions. On later iterations ToSimplify is not
85         // empty and we only bother simplifying instructions that are in it.
86         if (!ToSimplify->empty() && !ToSimplify->count(I))
87           continue;
88
89         // Don't bother simplifying unused instructions.
90         if (!I->use_empty()) {
91           Value *V = SimplifyInstruction(I, {DL, TLI, DT, AC});
92           if (V && LI->replacementPreservesLCSSAForm(I, V)) {
93             // Mark all uses for resimplification next time round the loop.
94             for (User *U : I->users())
95               Next->insert(cast<Instruction>(U));
96
97             I->replaceAllUsesWith(V);
98             LocalChanged = true;
99             ++NumSimplified;
100           }
101         }
102         if (RecursivelyDeleteTriviallyDeadInstructions(I, TLI)) {
103           // RecursivelyDeleteTriviallyDeadInstruction can remove more than one
104           // instruction, so simply incrementing the iterator does not work.
105           // When instructions get deleted re-iterate instead.
106           BI = BB->begin();
107           BE = BB->end();
108           LocalChanged = true;
109         }
110
111         if (IsSubloopHeader && !isa<PHINode>(I))
112           break;
113       }
114
115       // Add all successors to the worklist, except for loop exit blocks and the
116       // bodies of subloops. We visit the headers of loops so that we can
117       // process
118       // their phis, but we contract the rest of the subloop body and only
119       // follow
120       // edges leading back to the original loop.
121       for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
122            ++SI) {
123         BasicBlock *SuccBB = *SI;
124         if (!Visited.insert(SuccBB).second)
125           continue;
126
127         const Loop *SuccLoop = LI->getLoopFor(SuccBB);
128         if (SuccLoop && SuccLoop->getHeader() == SuccBB &&
129             L->contains(SuccLoop)) {
130           VisitStack.push_back(WorklistItem(SuccBB, true));
131
132           SmallVector<BasicBlock *, 8> SubLoopExitBlocks;
133           SuccLoop->getExitBlocks(SubLoopExitBlocks);
134
135           for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
136             BasicBlock *ExitBB = SubLoopExitBlocks[i];
137             if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB).second)
138               VisitStack.push_back(WorklistItem(ExitBB, false));
139           }
140
141           continue;
142         }
143
144         bool IsExitBlock =
145             std::binary_search(ExitBlocks.begin(), ExitBlocks.end(), SuccBB);
146         if (IsExitBlock)
147           continue;
148
149         VisitStack.push_back(WorklistItem(SuccBB, false));
150       }
151     }
152
153     // Place the list of instructions to simplify on the next loop iteration
154     // into ToSimplify.
155     std::swap(ToSimplify, Next);
156     Next->clear();
157
158     Changed |= LocalChanged;
159   } while (LocalChanged);
160
161   return Changed;
162 }
163
164 namespace {
165
166 class LoopInstSimplifyLegacyPass : public LoopPass {
167 public:
168   static char ID; // Pass ID, replacement for typeid
169
170   LoopInstSimplifyLegacyPass() : LoopPass(ID) {
171     initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
172   }
173
174   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
175     if (skipLoop(L))
176       return false;
177     DominatorTreeWrapperPass *DTWP =
178         getAnalysisIfAvailable<DominatorTreeWrapperPass>();
179     DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr;
180     LoopInfo *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
181     AssumptionCache *AC =
182         &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
183             *L->getHeader()->getParent());
184     const TargetLibraryInfo *TLI =
185         &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
186
187     return SimplifyLoopInst(L, DT, LI, AC, TLI);
188   }
189
190   void getAnalysisUsage(AnalysisUsage &AU) const override {
191     AU.addRequired<AssumptionCacheTracker>();
192     AU.addRequired<TargetLibraryInfoWrapperPass>();
193     AU.setPreservesCFG();
194     getLoopAnalysisUsage(AU);
195   }
196 };
197
198 } // end anonymous namespace
199
200 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
201                                             LoopStandardAnalysisResults &AR,
202                                             LPMUpdater &) {
203   if (!SimplifyLoopInst(&L, &AR.DT, &AR.LI, &AR.AC, &AR.TLI))
204     return PreservedAnalyses::all();
205
206   auto PA = getLoopPassPreservedAnalyses();
207   PA.preserveSet<CFGAnalyses>();
208   return PA;
209 }
210
211 char LoopInstSimplifyLegacyPass::ID = 0;
212
213 INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
214                       "Simplify instructions in loops", false, false)
215 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
216 INITIALIZE_PASS_DEPENDENCY(LoopPass)
217 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
218 INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
219                     "Simplify instructions in loops", false, false)
220
221 Pass *llvm::createLoopInstSimplifyPass() {
222   return new LoopInstSimplifyLegacyPass();
223 }