1 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
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 // 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
15 //===----------------------------------------------------------------------===//
17 #define DEBUG_TYPE "loop-extract"
18 #include "llvm/Transforms/IPO.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/Analysis/Dominators.h"
21 #include "llvm/Analysis/LoopPass.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Transforms/Scalar.h"
27 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
28 #include "llvm/Transforms/Utils/CodeExtractor.h"
33 STATISTIC(NumExtracted, "Number of loops extracted");
36 struct LoopExtractor : public LoopPass {
37 static char ID; // Pass identification, replacement for typeid
40 explicit LoopExtractor(unsigned numLoops = ~0)
41 : LoopPass(ID), NumLoops(numLoops) {
42 initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
45 virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequiredID(BreakCriticalEdgesID);
49 AU.addRequiredID(LoopSimplifyID);
50 AU.addRequired<DominatorTree>();
55 char LoopExtractor::ID = 0;
56 INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
57 "Extract loops into new functions", false, false)
58 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
59 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
60 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
61 INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
62 "Extract loops into new functions", false, false)
65 /// SingleLoopExtractor - For bugpoint.
66 struct SingleLoopExtractor : public LoopExtractor {
67 static char ID; // Pass identification, replacement for typeid
68 SingleLoopExtractor() : LoopExtractor(1) {}
70 } // End anonymous namespace
72 char SingleLoopExtractor::ID = 0;
73 INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
74 "Extract at most one loop into a new function", false, false)
76 // createLoopExtractorPass - This pass extracts all natural loops from the
77 // program into a function if it can.
79 Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
81 bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
82 // Only visit top-level loops.
83 if (L->getParentLoop())
86 // If LoopSimplify form is not available, stay out of trouble.
87 if (!L->isLoopSimplifyForm())
90 DominatorTree &DT = getAnalysis<DominatorTree>();
93 // If there is more than one top-level loop in this function, extract all of
94 // the loops. Otherwise there is exactly one top-level loop; in this case if
95 // this function is more than a minimal wrapper around the loop, extract
97 bool ShouldExtractLoop = false;
99 // Extract the loop if the entry block doesn't branch to the loop header.
100 TerminatorInst *EntryTI =
101 L->getHeader()->getParent()->getEntryBlock().getTerminator();
102 if (!isa<BranchInst>(EntryTI) ||
103 !cast<BranchInst>(EntryTI)->isUnconditional() ||
104 EntryTI->getSuccessor(0) != L->getHeader()) {
105 ShouldExtractLoop = true;
107 // Check to see if any exits from the loop are more than just return
109 SmallVector<BasicBlock*, 8> ExitBlocks;
110 L->getExitBlocks(ExitBlocks);
111 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
112 if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
113 ShouldExtractLoop = true;
118 if (ShouldExtractLoop) {
119 // We must omit landing pads. Landing pads must accompany the invoke
120 // instruction. But this would result in a loop in the extracted
121 // function. An infinite cycle occurs when it tries to extract that loop as
123 SmallVector<BasicBlock*, 8> ExitBlocks;
124 L->getExitBlocks(ExitBlocks);
125 for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
126 if (ExitBlocks[i]->isLandingPad()) {
127 ShouldExtractLoop = false;
132 if (ShouldExtractLoop) {
133 if (NumLoops == 0) return Changed;
135 CodeExtractor Extractor(DT, *L);
136 if (Extractor.extractCodeRegion() != 0) {
138 // After extraction, the loop is replaced by a function call, so
139 // we shouldn't try to run any more loop passes on it.
140 LPM.deleteLoopFromQueue(L);
148 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
149 // program into a function if it can. This is used by bugpoint.
151 Pass *llvm::createSingleLoopExtractorPass() {
152 return new SingleLoopExtractor();
156 // BlockFile - A file which contains a list of blocks that should not be
158 static cl::opt<std::string>
159 BlockFile("extract-blocks-file", cl::value_desc("filename"),
160 cl::desc("A file containing list of basic blocks to not extract"),
164 /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
165 /// from the module into their own functions except for those specified by the
166 /// BlocksToNotExtract list.
167 class BlockExtractorPass : public ModulePass {
168 void LoadFile(const char *Filename);
169 void SplitLandingPadPreds(Function *F);
171 std::vector<BasicBlock*> BlocksToNotExtract;
172 std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
174 static char ID; // Pass identification, replacement for typeid
175 BlockExtractorPass() : ModulePass(ID) {
176 if (!BlockFile.empty())
177 LoadFile(BlockFile.c_str());
180 bool runOnModule(Module &M);
184 char BlockExtractorPass::ID = 0;
185 INITIALIZE_PASS(BlockExtractorPass, "extract-blocks",
186 "Extract Basic Blocks From Module (for bugpoint use)",
189 // createBlockExtractorPass - This pass extracts all blocks (except those
190 // specified in the argument list) from the functions in the module.
192 ModulePass *llvm::createBlockExtractorPass() {
193 return new BlockExtractorPass();
196 void BlockExtractorPass::LoadFile(const char *Filename) {
197 // Load the BlockFile...
198 std::ifstream In(Filename);
200 errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
205 std::string FunctionName, BlockName;
208 if (!BlockName.empty())
209 BlocksToNotExtractByName.push_back(
210 std::make_pair(FunctionName, BlockName));
214 /// SplitLandingPadPreds - The landing pad needs to be extracted with the invoke
215 /// instruction. The critical edge breaker will refuse to break critical edges
216 /// to a landing pad. So do them here. After this method runs, all landing pads
217 /// should have only one predecessor.
218 void BlockExtractorPass::SplitLandingPadPreds(Function *F) {
219 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
220 InvokeInst *II = dyn_cast<InvokeInst>(I);
222 BasicBlock *Parent = II->getParent();
223 BasicBlock *LPad = II->getUnwindDest();
225 // Look through the landing pad's predecessors. If one of them ends in an
226 // 'invoke', then we want to split the landing pad.
229 PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
230 BasicBlock *BB = *PI;
231 if (BB->isLandingPad() && BB != Parent &&
232 isa<InvokeInst>(Parent->getTerminator())) {
238 if (!Split) continue;
240 SmallVector<BasicBlock*, 2> NewBBs;
241 SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", 0, NewBBs);
245 bool BlockExtractorPass::runOnModule(Module &M) {
246 std::set<BasicBlock*> TranslatedBlocksToNotExtract;
247 for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
248 BasicBlock *BB = BlocksToNotExtract[i];
249 Function *F = BB->getParent();
251 // Map the corresponding function in this module.
252 Function *MF = M.getFunction(F->getName());
253 assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
255 // Figure out which index the basic block is in its function.
256 Function::iterator BBI = MF->begin();
257 std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
258 TranslatedBlocksToNotExtract.insert(BBI);
261 while (!BlocksToNotExtractByName.empty()) {
262 // There's no way to find BBs by name without looking at every BB inside
263 // every Function. Fortunately, this is always empty except when used by
264 // bugpoint in which case correctness is more important than performance.
266 std::string &FuncName = BlocksToNotExtractByName.back().first;
267 std::string &BlockName = BlocksToNotExtractByName.back().second;
269 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
271 if (F.getName() != FuncName) continue;
273 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
274 BasicBlock &BB = *BI;
275 if (BB.getName() != BlockName) continue;
277 TranslatedBlocksToNotExtract.insert(BI);
281 BlocksToNotExtractByName.pop_back();
284 // Now that we know which blocks to not extract, figure out which ones we WANT
286 std::vector<BasicBlock*> BlocksToExtract;
287 for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
288 SplitLandingPadPreds(&*F);
289 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
290 if (!TranslatedBlocksToNotExtract.count(BB))
291 BlocksToExtract.push_back(BB);
294 for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) {
295 SmallVector<BasicBlock*, 2> BlocksToExtractVec;
296 BlocksToExtractVec.push_back(BlocksToExtract[i]);
297 if (const InvokeInst *II =
298 dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator()))
299 BlocksToExtractVec.push_back(II->getUnwindDest());
300 CodeExtractor(BlocksToExtractVec).extractCodeRegion();
303 return !BlocksToExtract.empty();