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