LLVM API Documentation
00001 //===- LoopInstSimplify.cpp - Loop Instruction Simplification Pass --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass performs lightweight instruction simplification on loop bodies. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Transforms/Scalar.h" 00015 #include "llvm/ADT/STLExtras.h" 00016 #include "llvm/ADT/Statistic.h" 00017 #include "llvm/Analysis/AssumptionTracker.h" 00018 #include "llvm/Analysis/InstructionSimplify.h" 00019 #include "llvm/Analysis/LoopInfo.h" 00020 #include "llvm/Analysis/LoopPass.h" 00021 #include "llvm/IR/DataLayout.h" 00022 #include "llvm/IR/Dominators.h" 00023 #include "llvm/IR/Instructions.h" 00024 #include "llvm/Support/Debug.h" 00025 #include "llvm/Target/TargetLibraryInfo.h" 00026 #include "llvm/Transforms/Utils/Local.h" 00027 using namespace llvm; 00028 00029 #define DEBUG_TYPE "loop-instsimplify" 00030 00031 STATISTIC(NumSimplified, "Number of redundant instructions simplified"); 00032 00033 namespace { 00034 class LoopInstSimplify : public LoopPass { 00035 public: 00036 static char ID; // Pass ID, replacement for typeid 00037 LoopInstSimplify() : LoopPass(ID) { 00038 initializeLoopInstSimplifyPass(*PassRegistry::getPassRegistry()); 00039 } 00040 00041 bool runOnLoop(Loop*, LPPassManager&) override; 00042 00043 void getAnalysisUsage(AnalysisUsage &AU) const override { 00044 AU.setPreservesCFG(); 00045 AU.addRequired<AssumptionTracker>(); 00046 AU.addRequired<LoopInfo>(); 00047 AU.addRequiredID(LoopSimplifyID); 00048 AU.addPreservedID(LoopSimplifyID); 00049 AU.addPreservedID(LCSSAID); 00050 AU.addPreserved("scalar-evolution"); 00051 AU.addRequired<TargetLibraryInfo>(); 00052 } 00053 }; 00054 } 00055 00056 char LoopInstSimplify::ID = 0; 00057 INITIALIZE_PASS_BEGIN(LoopInstSimplify, "loop-instsimplify", 00058 "Simplify instructions in loops", false, false) 00059 INITIALIZE_PASS_DEPENDENCY(AssumptionTracker) 00060 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo) 00061 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 00062 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 00063 INITIALIZE_PASS_DEPENDENCY(LCSSA) 00064 INITIALIZE_PASS_END(LoopInstSimplify, "loop-instsimplify", 00065 "Simplify instructions in loops", false, false) 00066 00067 Pass *llvm::createLoopInstSimplifyPass() { 00068 return new LoopInstSimplify(); 00069 } 00070 00071 bool LoopInstSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { 00072 if (skipOptnoneFunction(L)) 00073 return false; 00074 00075 DominatorTreeWrapperPass *DTWP = 00076 getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 00077 DominatorTree *DT = DTWP ? &DTWP->getDomTree() : nullptr; 00078 LoopInfo *LI = &getAnalysis<LoopInfo>(); 00079 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 00080 const DataLayout *DL = DLP ? &DLP->getDataLayout() : nullptr; 00081 const TargetLibraryInfo *TLI = &getAnalysis<TargetLibraryInfo>(); 00082 AssumptionTracker *AT = &getAnalysis<AssumptionTracker>(); 00083 00084 SmallVector<BasicBlock*, 8> ExitBlocks; 00085 L->getUniqueExitBlocks(ExitBlocks); 00086 array_pod_sort(ExitBlocks.begin(), ExitBlocks.end()); 00087 00088 SmallPtrSet<const Instruction*, 8> S1, S2, *ToSimplify = &S1, *Next = &S2; 00089 00090 // The bit we are stealing from the pointer represents whether this basic 00091 // block is the header of a subloop, in which case we only process its phis. 00092 typedef PointerIntPair<BasicBlock*, 1> WorklistItem; 00093 SmallVector<WorklistItem, 16> VisitStack; 00094 SmallPtrSet<BasicBlock*, 32> Visited; 00095 00096 bool Changed = false; 00097 bool LocalChanged; 00098 do { 00099 LocalChanged = false; 00100 00101 VisitStack.clear(); 00102 Visited.clear(); 00103 00104 VisitStack.push_back(WorklistItem(L->getHeader(), false)); 00105 00106 while (!VisitStack.empty()) { 00107 WorklistItem Item = VisitStack.pop_back_val(); 00108 BasicBlock *BB = Item.getPointer(); 00109 bool IsSubloopHeader = Item.getInt(); 00110 00111 // Simplify instructions in the current basic block. 00112 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { 00113 Instruction *I = BI++; 00114 00115 // The first time through the loop ToSimplify is empty and we try to 00116 // simplify all instructions. On later iterations ToSimplify is not 00117 // empty and we only bother simplifying instructions that are in it. 00118 if (!ToSimplify->empty() && !ToSimplify->count(I)) 00119 continue; 00120 00121 // Don't bother simplifying unused instructions. 00122 if (!I->use_empty()) { 00123 Value *V = SimplifyInstruction(I, DL, TLI, DT, AT); 00124 if (V && LI->replacementPreservesLCSSAForm(I, V)) { 00125 // Mark all uses for resimplification next time round the loop. 00126 for (User *U : I->users()) 00127 Next->insert(cast<Instruction>(U)); 00128 00129 I->replaceAllUsesWith(V); 00130 LocalChanged = true; 00131 ++NumSimplified; 00132 } 00133 } 00134 bool res = RecursivelyDeleteTriviallyDeadInstructions(I, TLI); 00135 if (res) { 00136 // RecursivelyDeleteTriviallyDeadInstruction can remove 00137 // more than one instruction, so simply incrementing the 00138 // iterator does not work. When instructions get deleted 00139 // re-iterate instead. 00140 BI = BB->begin(); BE = BB->end(); 00141 LocalChanged |= res; 00142 } 00143 00144 if (IsSubloopHeader && !isa<PHINode>(I)) 00145 break; 00146 } 00147 00148 // Add all successors to the worklist, except for loop exit blocks and the 00149 // bodies of subloops. We visit the headers of loops so that we can process 00150 // their phis, but we contract the rest of the subloop body and only follow 00151 // edges leading back to the original loop. 00152 for (succ_iterator SI = succ_begin(BB), SE = succ_end(BB); SI != SE; 00153 ++SI) { 00154 BasicBlock *SuccBB = *SI; 00155 if (!Visited.insert(SuccBB)) 00156 continue; 00157 00158 const Loop *SuccLoop = LI->getLoopFor(SuccBB); 00159 if (SuccLoop && SuccLoop->getHeader() == SuccBB 00160 && L->contains(SuccLoop)) { 00161 VisitStack.push_back(WorklistItem(SuccBB, true)); 00162 00163 SmallVector<BasicBlock*, 8> SubLoopExitBlocks; 00164 SuccLoop->getExitBlocks(SubLoopExitBlocks); 00165 00166 for (unsigned i = 0; i < SubLoopExitBlocks.size(); ++i) { 00167 BasicBlock *ExitBB = SubLoopExitBlocks[i]; 00168 if (LI->getLoopFor(ExitBB) == L && Visited.insert(ExitBB)) 00169 VisitStack.push_back(WorklistItem(ExitBB, false)); 00170 } 00171 00172 continue; 00173 } 00174 00175 bool IsExitBlock = std::binary_search(ExitBlocks.begin(), 00176 ExitBlocks.end(), SuccBB); 00177 if (IsExitBlock) 00178 continue; 00179 00180 VisitStack.push_back(WorklistItem(SuccBB, false)); 00181 } 00182 } 00183 00184 // Place the list of instructions to simplify on the next loop iteration 00185 // into ToSimplify. 00186 std::swap(ToSimplify, Next); 00187 Next->clear(); 00188 00189 Changed |= LocalChanged; 00190 } while (LocalChanged); 00191 00192 return Changed; 00193 }