LLVM API Documentation

LoopExtractor.cpp
Go to the documentation of this file.
00001 //===- LoopExtractor.cpp - Extract each loop into a new function ----------===//
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 // A pass wrapper around the ExtractLoop() scalar transformation to extract each
00011 // top-level loop into its own new function. If the loop is the ONLY loop in a
00012 // given function, it is not touched. This is a pass most useful for debugging
00013 // via bugpoint.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #define DEBUG_TYPE "loop-extract"
00018 #include "llvm/Transforms/IPO.h"
00019 #include "llvm/ADT/Statistic.h"
00020 #include "llvm/Analysis/Dominators.h"
00021 #include "llvm/Analysis/LoopPass.h"
00022 #include "llvm/IR/Instructions.h"
00023 #include "llvm/IR/Module.h"
00024 #include "llvm/Pass.h"
00025 #include "llvm/Support/CommandLine.h"
00026 #include "llvm/Transforms/Scalar.h"
00027 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00028 #include "llvm/Transforms/Utils/CodeExtractor.h"
00029 #include <fstream>
00030 #include <set>
00031 using namespace llvm;
00032 
00033 STATISTIC(NumExtracted, "Number of loops extracted");
00034 
00035 namespace {
00036   struct LoopExtractor : public LoopPass {
00037     static char ID; // Pass identification, replacement for typeid
00038     unsigned NumLoops;
00039 
00040     explicit LoopExtractor(unsigned numLoops = ~0) 
00041       : LoopPass(ID), NumLoops(numLoops) {
00042         initializeLoopExtractorPass(*PassRegistry::getPassRegistry());
00043       }
00044 
00045     virtual bool runOnLoop(Loop *L, LPPassManager &LPM);
00046 
00047     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00048       AU.addRequiredID(BreakCriticalEdgesID);
00049       AU.addRequiredID(LoopSimplifyID);
00050       AU.addRequired<DominatorTree>();
00051     }
00052   };
00053 }
00054 
00055 char LoopExtractor::ID = 0;
00056 INITIALIZE_PASS_BEGIN(LoopExtractor, "loop-extract",
00057                       "Extract loops into new functions", false, false)
00058 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
00059 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
00060 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
00061 INITIALIZE_PASS_END(LoopExtractor, "loop-extract",
00062                     "Extract loops into new functions", false, false)
00063 
00064 namespace {
00065   /// SingleLoopExtractor - For bugpoint.
00066   struct SingleLoopExtractor : public LoopExtractor {
00067     static char ID; // Pass identification, replacement for typeid
00068     SingleLoopExtractor() : LoopExtractor(1) {}
00069   };
00070 } // End anonymous namespace
00071 
00072 char SingleLoopExtractor::ID = 0;
00073 INITIALIZE_PASS(SingleLoopExtractor, "loop-extract-single",
00074                 "Extract at most one loop into a new function", false, false)
00075 
00076 // createLoopExtractorPass - This pass extracts all natural loops from the
00077 // program into a function if it can.
00078 //
00079 Pass *llvm::createLoopExtractorPass() { return new LoopExtractor(); }
00080 
00081 bool LoopExtractor::runOnLoop(Loop *L, LPPassManager &LPM) {
00082   // Only visit top-level loops.
00083   if (L->getParentLoop())
00084     return false;
00085 
00086   // If LoopSimplify form is not available, stay out of trouble.
00087   if (!L->isLoopSimplifyForm())
00088     return false;
00089 
00090   DominatorTree &DT = getAnalysis<DominatorTree>();
00091   bool Changed = false;
00092 
00093   // If there is more than one top-level loop in this function, extract all of
00094   // the loops. Otherwise there is exactly one top-level loop; in this case if
00095   // this function is more than a minimal wrapper around the loop, extract
00096   // the loop.
00097   bool ShouldExtractLoop = false;
00098 
00099   // Extract the loop if the entry block doesn't branch to the loop header.
00100   TerminatorInst *EntryTI =
00101     L->getHeader()->getParent()->getEntryBlock().getTerminator();
00102   if (!isa<BranchInst>(EntryTI) ||
00103       !cast<BranchInst>(EntryTI)->isUnconditional() ||
00104       EntryTI->getSuccessor(0) != L->getHeader()) {
00105     ShouldExtractLoop = true;
00106   } else {
00107     // Check to see if any exits from the loop are more than just return
00108     // blocks.
00109     SmallVector<BasicBlock*, 8> ExitBlocks;
00110     L->getExitBlocks(ExitBlocks);
00111     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00112       if (!isa<ReturnInst>(ExitBlocks[i]->getTerminator())) {
00113         ShouldExtractLoop = true;
00114         break;
00115       }
00116   }
00117 
00118   if (ShouldExtractLoop) {
00119     // We must omit landing pads. Landing pads must accompany the invoke
00120     // instruction. But this would result in a loop in the extracted
00121     // function. An infinite cycle occurs when it tries to extract that loop as
00122     // well.
00123     SmallVector<BasicBlock*, 8> ExitBlocks;
00124     L->getExitBlocks(ExitBlocks);
00125     for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
00126       if (ExitBlocks[i]->isLandingPad()) {
00127         ShouldExtractLoop = false;
00128         break;
00129       }
00130   }
00131 
00132   if (ShouldExtractLoop) {
00133     if (NumLoops == 0) return Changed;
00134     --NumLoops;
00135     CodeExtractor Extractor(DT, *L);
00136     if (Extractor.extractCodeRegion() != 0) {
00137       Changed = true;
00138       // After extraction, the loop is replaced by a function call, so
00139       // we shouldn't try to run any more loop passes on it.
00140       LPM.deleteLoopFromQueue(L);
00141     }
00142     ++NumExtracted;
00143   }
00144 
00145   return Changed;
00146 }
00147 
00148 // createSingleLoopExtractorPass - This pass extracts one natural loop from the
00149 // program into a function if it can.  This is used by bugpoint.
00150 //
00151 Pass *llvm::createSingleLoopExtractorPass() {
00152   return new SingleLoopExtractor();
00153 }
00154 
00155 
00156 // BlockFile - A file which contains a list of blocks that should not be
00157 // extracted.
00158 static cl::opt<std::string>
00159 BlockFile("extract-blocks-file", cl::value_desc("filename"),
00160           cl::desc("A file containing list of basic blocks to not extract"),
00161           cl::Hidden);
00162 
00163 namespace {
00164   /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
00165   /// from the module into their own functions except for those specified by the
00166   /// BlocksToNotExtract list.
00167   class BlockExtractorPass : public ModulePass {
00168     void LoadFile(const char *Filename);
00169     void SplitLandingPadPreds(Function *F);
00170 
00171     std::vector<BasicBlock*> BlocksToNotExtract;
00172     std::vector<std::pair<std::string, std::string> > BlocksToNotExtractByName;
00173   public:
00174     static char ID; // Pass identification, replacement for typeid
00175     BlockExtractorPass() : ModulePass(ID) {
00176       if (!BlockFile.empty())
00177         LoadFile(BlockFile.c_str());
00178     }
00179 
00180     bool runOnModule(Module &M);
00181   };
00182 }
00183 
00184 char BlockExtractorPass::ID = 0;
00185 INITIALIZE_PASS(BlockExtractorPass, "extract-blocks",
00186                 "Extract Basic Blocks From Module (for bugpoint use)",
00187                 false, false)
00188 
00189 // createBlockExtractorPass - This pass extracts all blocks (except those
00190 // specified in the argument list) from the functions in the module.
00191 //
00192 ModulePass *llvm::createBlockExtractorPass() {
00193   return new BlockExtractorPass();
00194 }
00195 
00196 void BlockExtractorPass::LoadFile(const char *Filename) {
00197   // Load the BlockFile...
00198   std::ifstream In(Filename);
00199   if (!In.good()) {
00200     errs() << "WARNING: BlockExtractor couldn't load file '" << Filename
00201            << "'!\n";
00202     return;
00203   }
00204   while (In) {
00205     std::string FunctionName, BlockName;
00206     In >> FunctionName;
00207     In >> BlockName;
00208     if (!BlockName.empty())
00209       BlocksToNotExtractByName.push_back(
00210           std::make_pair(FunctionName, BlockName));
00211   }
00212 }
00213 
00214 /// SplitLandingPadPreds - The landing pad needs to be extracted with the invoke
00215 /// instruction. The critical edge breaker will refuse to break critical edges
00216 /// to a landing pad. So do them here. After this method runs, all landing pads
00217 /// should have only one predecessor.
00218 void BlockExtractorPass::SplitLandingPadPreds(Function *F) {
00219   for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
00220     InvokeInst *II = dyn_cast<InvokeInst>(I);
00221     if (!II) continue;
00222     BasicBlock *Parent = II->getParent();
00223     BasicBlock *LPad = II->getUnwindDest();
00224 
00225     // Look through the landing pad's predecessors. If one of them ends in an
00226     // 'invoke', then we want to split the landing pad.
00227     bool Split = false;
00228     for (pred_iterator
00229            PI = pred_begin(LPad), PE = pred_end(LPad); PI != PE; ++PI) {
00230       BasicBlock *BB = *PI;
00231       if (BB->isLandingPad() && BB != Parent &&
00232           isa<InvokeInst>(Parent->getTerminator())) {
00233         Split = true;
00234         break;
00235       }
00236     }
00237 
00238     if (!Split) continue;
00239 
00240     SmallVector<BasicBlock*, 2> NewBBs;
00241     SplitLandingPadPredecessors(LPad, Parent, ".1", ".2", 0, NewBBs);
00242   }
00243 }
00244 
00245 bool BlockExtractorPass::runOnModule(Module &M) {
00246   std::set<BasicBlock*> TranslatedBlocksToNotExtract;
00247   for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
00248     BasicBlock *BB = BlocksToNotExtract[i];
00249     Function *F = BB->getParent();
00250 
00251     // Map the corresponding function in this module.
00252     Function *MF = M.getFunction(F->getName());
00253     assert(MF->getFunctionType() == F->getFunctionType() && "Wrong function?");
00254 
00255     // Figure out which index the basic block is in its function.
00256     Function::iterator BBI = MF->begin();
00257     std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
00258     TranslatedBlocksToNotExtract.insert(BBI);
00259   }
00260 
00261   while (!BlocksToNotExtractByName.empty()) {
00262     // There's no way to find BBs by name without looking at every BB inside
00263     // every Function. Fortunately, this is always empty except when used by
00264     // bugpoint in which case correctness is more important than performance.
00265 
00266     std::string &FuncName  = BlocksToNotExtractByName.back().first;
00267     std::string &BlockName = BlocksToNotExtractByName.back().second;
00268 
00269     for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) {
00270       Function &F = *FI;
00271       if (F.getName() != FuncName) continue;
00272 
00273       for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
00274         BasicBlock &BB = *BI;
00275         if (BB.getName() != BlockName) continue;
00276 
00277         TranslatedBlocksToNotExtract.insert(BI);
00278       }
00279     }
00280 
00281     BlocksToNotExtractByName.pop_back();
00282   }
00283 
00284   // Now that we know which blocks to not extract, figure out which ones we WANT
00285   // to extract.
00286   std::vector<BasicBlock*> BlocksToExtract;
00287   for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
00288     SplitLandingPadPreds(&*F);
00289     for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
00290       if (!TranslatedBlocksToNotExtract.count(BB))
00291         BlocksToExtract.push_back(BB);
00292   }
00293 
00294   for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i) {
00295     SmallVector<BasicBlock*, 2> BlocksToExtractVec;
00296     BlocksToExtractVec.push_back(BlocksToExtract[i]);
00297     if (const InvokeInst *II =
00298         dyn_cast<InvokeInst>(BlocksToExtract[i]->getTerminator()))
00299       BlocksToExtractVec.push_back(II->getUnwindDest());
00300     CodeExtractor(BlocksToExtractVec).extractCodeRegion();
00301   }
00302 
00303   return !BlocksToExtract.empty();
00304 }