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