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