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