1 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs lightweight instruction simplification on loop bodies.
12 //===----------------------------------------------------------------------===//
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/LoopIterator.h"
24 #include "llvm/Analysis/LoopPass.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Transforms/Utils/Local.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/CFG.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/Dominators.h"
31 #include "llvm/IR/Instruction.h"
32 #include "llvm/IR/Instructions.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/PassManager.h"
35 #include "llvm/IR/User.h"
36 #include "llvm/Pass.h"
37 #include "llvm/Support/Casting.h"
38 #include "llvm/Transforms/Scalar.h"
39 #include "llvm/Transforms/Utils/LoopUtils.h"
45 #define DEBUG_TYPE "loop-instsimplify"
47 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
49 static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
51 const TargetLibraryInfo &TLI) {
52 const DataLayout &DL = L.getHeader()->getModule()->getDataLayout();
53 SimplifyQuery SQ(DL, &TLI, &DT, &AC);
55 // On the first pass over the loop body we try to simplify every instruction.
56 // On subsequent passes, we can restrict this to only simplifying instructions
57 // where the inputs have been updated. We end up needing two sets: one
58 // containing the instructions we are simplifying in *this* pass, and one for
59 // the instructions we will want to simplify in the *next* pass. We use
60 // pointers so we can swap between two stably allocated sets.
61 SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
63 // Track the PHI nodes that have already been visited during each iteration so
64 // that we can identify when it is necessary to iterate.
65 SmallPtrSet<PHINode *, 4> VisitedPHIs;
67 // While simplifying we may discover dead code or cause code to become dead.
68 // Keep track of all such instructions and we will delete them at the end.
69 SmallVector<Instruction *, 8> DeadInsts;
71 // First we want to create an RPO traversal of the loop body. By processing in
72 // RPO we can ensure that definitions are processed prior to uses (for non PHI
73 // uses) in all cases. This ensures we maximize the simplifications in each
74 // iteration over the loop and minimizes the possible causes for continuing to
76 LoopBlocksRPO RPOT(&L);
81 for (BasicBlock *BB : RPOT) {
82 for (Instruction &I : *BB) {
83 if (auto *PI = dyn_cast<PHINode>(&I))
84 VisitedPHIs.insert(PI);
87 if (isInstructionTriviallyDead(&I, &TLI))
88 DeadInsts.push_back(&I);
92 // We special case the first iteration which we can detect due to the
93 // empty `ToSimplify` set.
94 bool IsFirstIteration = ToSimplify->empty();
96 if (!IsFirstIteration && !ToSimplify->count(&I))
99 Value *V = SimplifyInstruction(&I, SQ.getWithInstruction(&I));
100 if (!V || !LI.replacementPreservesLCSSAForm(&I, V))
103 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
106 auto *UserI = cast<Instruction>(U.getUser());
109 // If the instruction is used by a PHI node we have already processed
110 // we'll need to iterate on the loop body to converge, so add it to
112 if (auto *UserPI = dyn_cast<PHINode>(UserI))
113 if (VisitedPHIs.count(UserPI)) {
114 Next->insert(UserPI);
118 // If we are only simplifying targeted instructions and the user is an
119 // instruction in the loop body, add it to our set of targeted
120 // instructions. Because we process defs before uses (outside of PHIs)
121 // we won't have visited it yet.
123 // We also skip any uses outside of the loop being simplified. Those
124 // should always be PHI nodes due to LCSSA form, and we don't want to
125 // try to simplify those away.
126 assert((L.contains(UserI) || isa<PHINode>(UserI)) &&
127 "Uses outside the loop should be PHI nodes due to LCSSA!");
128 if (!IsFirstIteration && L.contains(UserI))
129 ToSimplify->insert(UserI);
132 assert(I.use_empty() && "Should always have replaced all uses!");
133 if (isInstructionTriviallyDead(&I, &TLI))
134 DeadInsts.push_back(&I);
140 // Delete any dead instructions found thus far now that we've finished an
141 // iteration over all instructions in all the loop blocks.
142 if (!DeadInsts.empty()) {
144 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, &TLI);
147 // If we never found a PHI that needs to be simplified in the next
148 // iteration, we're done.
152 // Otherwise, put the next set in place for the next iteration and reset it
153 // and the visited PHIs for that iteration.
154 std::swap(Next, ToSimplify);
165 class LoopInstSimplifyLegacyPass : public LoopPass {
167 static char ID; // Pass ID, replacement for typeid
169 LoopInstSimplifyLegacyPass() : LoopPass(ID) {
170 initializeLoopInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
173 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
176 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
177 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
178 AssumptionCache &AC =
179 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(
180 *L->getHeader()->getParent());
181 const TargetLibraryInfo &TLI =
182 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
184 return simplifyLoopInst(*L, DT, LI, AC, TLI);
187 void getAnalysisUsage(AnalysisUsage &AU) const override {
188 AU.addRequired<AssumptionCacheTracker>();
189 AU.addRequired<DominatorTreeWrapperPass>();
190 AU.addRequired<TargetLibraryInfoWrapperPass>();
191 AU.setPreservesCFG();
192 getLoopAnalysisUsage(AU);
196 } // end anonymous namespace
198 PreservedAnalyses LoopInstSimplifyPass::run(Loop &L, LoopAnalysisManager &AM,
199 LoopStandardAnalysisResults &AR,
201 if (!simplifyLoopInst(L, AR.DT, AR.LI, AR.AC, AR.TLI))
202 return PreservedAnalyses::all();
204 auto PA = getLoopPassPreservedAnalyses();
205 PA.preserveSet<CFGAnalyses>();
209 char LoopInstSimplifyLegacyPass::ID = 0;
211 INITIALIZE_PASS_BEGIN(LoopInstSimplifyLegacyPass, "loop-instsimplify",
212 "Simplify instructions in loops", false, false)
213 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
214 INITIALIZE_PASS_DEPENDENCY(LoopPass)
215 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
216 INITIALIZE_PASS_END(LoopInstSimplifyLegacyPass, "loop-instsimplify",
217 "Simplify instructions in loops", false, false)
219 Pass *llvm::createLoopInstSimplifyPass() {
220 return new LoopInstSimplifyLegacyPass();