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 #define DEBUG_TYPE "loop-instsimplify"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Analysis/InstructionSimplify.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/LoopPass.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Dominators.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Target/TargetLibraryInfo.h"
26 #include "llvm/Transforms/Utils/Local.h"
29 STATISTIC(NumSimplified, "Number of redundant instructions simplified");
32 class LoopInstSimplify : public LoopPass {
34 static char ID; // Pass ID, replacement for typeid
35 LoopInstSimplify() : LoopPass(ID) {
36 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry());
39 bool runOnLoop(Loop*, LPPassManager&) override;
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.addRequired<LoopInfo>();
44 AU.addRequiredID(LoopSimplifyID);
45 AU.addPreservedID(LoopSimplifyID);
46 AU.addPreservedID(LCSSAID);
47 AU.addPreserved("scalar-evolution");
48 AU.addRequired<TargetLibraryInfo>();
53 char LoopInstSimplify::ID = 0;
54 INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify",
55 "Simplify instructions in loops", false, false)
56 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
57 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
58 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
59 INITIALIZE_PASS_DEPENDENCY(LCSSA)
60 INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify",
61 "Simplify instructions in loops", false, false)
63 Pass *llvm::createLoopInstSimplifyPass() {
64 return new LoopInstSimplify();
67 bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
68 if (skipOptnoneFunction(L))
71 DominatorTreeWrapperPass *DTWP =
72 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
73 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : 0;
74 LoopInfo *LI = &getAnalysis<LoopInfo>();
75 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
76 const DataLayout *DL = DLP ? &DLP->getDataLayout() : 0;
77 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>();
79 SmallVector<BasicBlock*, 8> ExitBlocks;
80 L->getUniqueExitBlocks(ExitBlocks);
81 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end());
83 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
85 // The bit we are stealing from the pointer represents whether this basic
86 // block is the header of a subloop, in which case we only process its phis.
87 typedef PointerIntPair<BasicBlock*, 1> WorklistItem;
88 SmallVector<WorklistItem, 16> VisitStack;
89 SmallPtrSet<BasicBlock*, 32> Visited;
99 VisitStack.push_back(WorklistItem(L->getHeader(), false));
101 while (!VisitStack.empty()) {
102 WorklistItem Item = VisitStack.pop_back_val();
103 BasicBlock *BB = Item.getPointer();
104 bool IsSubloopHeader = Item.getInt();
106 // Simplify instructions in the current basic block.
107 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) {
108 Instruction *I = BI++;
110 // The first time through the loop ToSimplify is empty and we try to
111 // simplify all instructions. On later iterations ToSimplify is not
112 // empty and we only bother simplifying instructions that are in it.
113 if (!ToSimplify->empty() && !ToSimplify->count(I))
116 // Don't bother simplifying unused instructions.
117 if (!I->use_empty()) {
118 Value *V = SimplifyInstruction(I, DL, TLI, DT);
119 if (V && LI->replacementPreservesLCSSAForm(I, V)) {
120 // Mark all uses for resimplification next time round the loop.
121 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
123 Next->insert(cast<Instruction>(*UI));
125 I->replaceAllUsesWith(V);
130 LocalChanged |= RecursivelyDeleteTriviallyDeadInstructions(I, TLI);
132 if (IsSubloopHeader && !isa<PHINode>(I))
136 // Add all successors to the worklist, except for loop exit blocks and the
137 // bodies of subloops. We visit the headers of loops so that we can process
138 // their phis, but we contract the rest of the subloop body and only follow
139 // edges leading back to the original loop.
140 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE;
142 BasicBlock *SuccBB = *SI;
143 if (!Visited.insert(SuccBB))
146 const Loop *SuccLoop = LI->getLoopFor(SuccBB);
147 if (SuccLoop && SuccLoop->getHeader() == SuccBB
148 && L->contains(SuccLoop)) {
149 VisitStack.push_back(WorklistItem(SuccBB, true));
151 SmallVector<BasicBlock*, 8> SubLoopExitBlocks;
152 SuccLoop->getExitBlocks(SubLoopExitBlocks);
154 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) {
155 BasicBlock *ExitBB = SubLoopExitBlocks[i];
156 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB))
157 VisitStack.push_back(WorklistItem(ExitBB, false));
163 bool IsExitBlock = std::binary_search(ExitBlocks.begin(),
164 ExitBlocks.end(), SuccBB);
168 VisitStack.push_back(WorklistItem(SuccBB, false));
172 // Place the list of instructions to simplify on the next loop iteration
174 std::swap(ToSimplify, Next);
177 Changed |= LocalChanged;
178 } while (LocalChanged);