1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===// 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 // A pass wrapper around the ExtractLoop() scalar transformation to extract each 11 // top-level loop into its own new function. If the loop is the ONLY loop in a 12 // given function, it is not touched. This is a pass most useful for debugging 13 // via bugpoint. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/Analysis/LoopPass.h" 19 #include "llvm/IR/Dominators.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/IR/Module.h" 22 #include "llvm/Pass.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Transforms/IPO.h" 25 #include "llvm/Transforms/Scalar.h" 26 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 27 #include "llvm/Transforms/Utils/CodeExtractor.h" 28 #include <fstream> 29 #include <set> 30 using namespace llvm; 31 32 #define DEBUG_TYPE "loop-extract" 33 34 STATISTIC(NumExtracted, "Number of loops extracted"); 35 36 namespace { 37 struct LoopExtractor : public LoopPass { 38 static char ID; // Pass identification, replacement for typeid 39 unsigned NumLoops; 40 41 explicit LoopExtractor(unsigned numLoops = ~0) 42 : LoopPass(ID), NumLoops(numLoops) { 43 initializeLoopExtractorPass(*PassRegistry::getPassRegistry()); 44 } 45 46 bool runOnLoop(Loop *L, LPPassManager &) override; 47 48 void getAnalysisUsage(AnalysisUsage &AU) const override { 49 AU.addRequiredID(BreakCriticalEdgesID); 50 AU.addRequiredID(LoopSimplifyID); 51 AU.addRequired<DominatorTreeWrapperPass>(); 52 AU.addRequired<LoopInfoWrapperPass>(); 53 } 54 }; 55 } 56 57 char LoopExtractor::ID = 0; 58 INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract", 59 "Extract loops into new functions", false, false) 60 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges) 61 INITIALIZE_PASS_DEPENDENCY(LoopSimplify) 62 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 63 INITIALIZE_PASS_END(LoopExtractor, "loop-extract", 64 "Extract loops into new functions", false, false) 65 66 namespace { 67 /// SingleLoopExtractor - For bugpoint. 68 struct SingleLoopExtractor : public LoopExtractor { 69 static char ID; // Pass identification, replacement for typeid 70 SingleLoopExtractor() : LoopExtractor(1) {} 71 }; 72 } // End anonymous namespace 73 74 char SingleLoopExtractor::ID = 0; 75 INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single", 76 "Extract at most one loop into a new function", false, false) 77 78 // createLoopExtractorPass - This pass extracts all natural loops from the 79 // program into a function if it can. 80 // 81 Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); } 82 83 bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) { 84 if (skipLoop(L)) 85 return false; 86 87 // Only visit top-level loops. 88 if (L->getParentLoop()) 89 return false; 90 91 // If LoopSimplify form is not available, stay out of trouble. 92 if (!L->isLoopSimplifyForm()) 93 return false; 94 95 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 96 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 97 bool Changed = false; 98 99 // If there is more than one top-level loop in this function, extract all of 100 // the loops. Otherwise there is exactly one top-level loop; in this case if 101 // this function is more than a minimal wrapper around the loop, extract 102 // the loop. 103 bool ShouldExtractLoop = false; 104 105 // Extract the loop if the entry block doesn't branch to the loop header. 106 TerminatorInst *EntryTI = 107 L->getHeader()->getParent()->getEntryBlock().getTerminator(); 108 if (!isa<BranchInst>(EntryTI) || 109 !cast<BranchInst>(EntryTI)->isUnconditional() || 110 EntryTI->getSuccessor(0) != L->getHeader()) { 111 ShouldExtractLoop = true; 112 } else { 113 // Check to see if any exits from the loop are more than just return 114 // blocks. 115 SmallVector<BasicBlock*, 8> ExitBlocks; 116 L->getExitBlocks(ExitBlocks); 117 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) 118 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) { 119 ShouldExtractLoop = true; 120 break; 121 } 122 } 123 124 if (ShouldExtractLoop) { 125 // We must omit EH pads. EH pads must accompany the invoke 126 // instruction. But this would result in a loop in the extracted 127 // function. An infinite cycle occurs when it tries to extract that loop as 128 // well. 129 SmallVector<BasicBlock*, 8> ExitBlocks; 130 L->getExitBlocks(ExitBlocks); 131 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) 132 if (ExitBlocks[i]->isEHPad()) { 133 ShouldExtractLoop = false; 134 break; 135 } 136 } 137 138 if (ShouldExtractLoop) { 139 if (NumLoops == 0) return Changed; 140 --NumLoops; 141 CodeExtractor Extractor(DT, *L); 142 if (Extractor.extractCodeRegion() != nullptr) { 143 Changed = true; 144 // After extraction, the loop is replaced by a function call, so 145 // we shouldn't try to run any more loop passes on it. 146 LPM.markLoopAsDeleted(*L); 147 LI.erase(L); 148 } 149 ++NumExtracted; 150 } 151 152 return Changed; 153 } 154 155 // createSingleLoopExtractorPass - This pass extracts one natural loop from the 156 // program into a function if it can. This is used by bugpoint. 157 // 158 Pass *llvm::createSingleLoopExtractorPass() { 159 return new SingleLoopExtractor(); 160 } 161