Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1 | //===- PartialInlining.cpp - Inline parts of functions --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass performs partial inlining, typically by inlining an if statement |
| 11 | // that surrounds the body of the function. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "partialinlining" |
| 16 | #include "llvm/Transforms/IPO.h" |
| 17 | #include "llvm/Instructions.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Analysis/Dominators.h" |
| 21 | #include "llvm/Transforms/Utils/Cloning.h" |
| 22 | #include "llvm/Transforms/Utils/FunctionUtils.h" |
Owen Anderson | 6367684 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/CFG.h" |
| 26 | using namespace llvm; |
| 27 | |
Owen Anderson | 6367684 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 28 | STATISTIC(NumPartialInlined, "Number of functions partially inlined"); |
| 29 | |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 30 | namespace { |
| 31 | struct VISIBILITY_HIDDEN PartialInliner : public ModulePass { |
| 32 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { } |
| 33 | static char ID; // Pass identification, replacement for typeid |
| 34 | PartialInliner() : ModulePass(&ID) {} |
| 35 | |
| 36 | bool runOnModule(Module& M); |
| 37 | |
| 38 | private: |
| 39 | Function* unswitchFunction(Function* F); |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | char PartialInliner::ID = 0; |
| 44 | static RegisterPass<PartialInliner> X("partial-inliner", "Partial Inliner"); |
| 45 | |
| 46 | ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); } |
| 47 | |
| 48 | Function* PartialInliner::unswitchFunction(Function* F) { |
| 49 | // First, verify that this function is an unswitching candidate... |
| 50 | BasicBlock* entryBlock = F->begin(); |
| 51 | if (!isa<BranchInst>(entryBlock->getTerminator())) |
| 52 | return 0; |
| 53 | |
| 54 | BasicBlock* returnBlock = 0; |
| 55 | BasicBlock* nonReturnBlock = 0; |
| 56 | unsigned returnCount = 0; |
| 57 | for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock); |
| 58 | SI != SE; ++SI) |
| 59 | if (isa<ReturnInst>((*SI)->getTerminator())) { |
| 60 | returnBlock = *SI; |
| 61 | returnCount++; |
| 62 | } else |
| 63 | nonReturnBlock = *SI; |
| 64 | |
| 65 | if (returnCount != 1) |
| 66 | return 0; |
| 67 | |
| 68 | // Clone the function, so that we can hack away on it. |
| 69 | DenseMap<const Value*, Value*> ValueMap; |
| 70 | Function* duplicateFunction = CloneFunction(F, ValueMap); |
| 71 | duplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
| 72 | F->getParent()->getFunctionList().push_back(duplicateFunction); |
| 73 | BasicBlock* newEntryBlock = cast<BasicBlock>(ValueMap[entryBlock]); |
| 74 | BasicBlock* newReturnBlock = cast<BasicBlock>(ValueMap[returnBlock]); |
| 75 | BasicBlock* newNonReturnBlock = cast<BasicBlock>(ValueMap[nonReturnBlock]); |
| 76 | |
| 77 | // Go ahead and update all uses to the duplicate, so that we can just |
| 78 | // use the inliner functionality when we're done hacking. |
| 79 | F->replaceAllUsesWith(duplicateFunction); |
| 80 | |
| 81 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 82 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 83 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 84 | // of which will go outside. |
| 85 | BasicBlock* preReturn = newReturnBlock; |
| 86 | newReturnBlock = newReturnBlock->splitBasicBlock( |
| 87 | newReturnBlock->getFirstNonPHI()); |
| 88 | BasicBlock::iterator I = preReturn->begin(); |
| 89 | BasicBlock::iterator Ins = newReturnBlock->begin(); |
| 90 | while (I != preReturn->end()) { |
| 91 | PHINode* OldPhi = dyn_cast<PHINode>(I); |
| 92 | if (!OldPhi) break; |
| 93 | |
| 94 | PHINode* retPhi = PHINode::Create(OldPhi->getType(), "", Ins); |
| 95 | OldPhi->replaceAllUsesWith(retPhi); |
| 96 | Ins = newReturnBlock->getFirstNonPHI(); |
| 97 | |
| 98 | retPhi->addIncoming(I, preReturn); |
| 99 | retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock), |
| 100 | newEntryBlock); |
| 101 | OldPhi->removeIncomingValue(newEntryBlock); |
| 102 | |
| 103 | ++I; |
| 104 | } |
| 105 | newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock); |
| 106 | |
| 107 | // Gather up the blocks that we're going to extract. |
| 108 | std::vector<BasicBlock*> toExtract; |
| 109 | toExtract.push_back(newNonReturnBlock); |
| 110 | for (Function::iterator FI = duplicateFunction->begin(), |
| 111 | FE = duplicateFunction->end(); FI != FE; ++FI) |
| 112 | if (&*FI != newEntryBlock && &*FI != newReturnBlock && |
| 113 | &*FI != newNonReturnBlock) |
| 114 | toExtract.push_back(FI); |
| 115 | |
| 116 | // The CodeExtractor needs a dominator tree. |
| 117 | DominatorTree DT; |
| 118 | DT.runOnFunction(*duplicateFunction); |
| 119 | |
| 120 | // Extract the body of the the if. |
| 121 | Function* extractedFunction = ExtractCodeRegion(DT, toExtract); |
| 122 | |
| 123 | // Inline the top-level if test into all callers. |
| 124 | std::vector<User*> Users(duplicateFunction->use_begin(), |
| 125 | duplicateFunction->use_end()); |
| 126 | for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end(); |
| 127 | UI != UE; ++UI) |
| 128 | if (CallInst* CI = dyn_cast<CallInst>(*UI)) |
| 129 | InlineFunction(CI); |
| 130 | else if (InvokeInst* II = dyn_cast<InvokeInst>(*UI)) |
| 131 | InlineFunction(II); |
| 132 | |
| 133 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 134 | // users (function pointers, etc.) back to the original function. |
| 135 | duplicateFunction->replaceAllUsesWith(F); |
| 136 | duplicateFunction->eraseFromParent(); |
| 137 | |
Owen Anderson | 6367684 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 138 | ++NumPartialInlined; |
| 139 | |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 140 | return extractedFunction; |
| 141 | } |
| 142 | |
| 143 | bool PartialInliner::runOnModule(Module& M) { |
| 144 | std::vector<Function*> worklist; |
| 145 | worklist.reserve(M.size()); |
| 146 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 147 | if (!FI->use_empty() && !FI->isDeclaration()) |
| 148 | worklist.push_back(&*FI); |
| 149 | |
| 150 | bool changed = false; |
| 151 | while (!worklist.empty()) { |
| 152 | Function* currFunc = worklist.back(); |
| 153 | worklist.pop_back(); |
| 154 | |
| 155 | if (currFunc->use_empty()) continue; |
| 156 | |
| 157 | bool recursive = false; |
| 158 | for (Function::use_iterator UI = currFunc->use_begin(), |
| 159 | UE = currFunc->use_end(); UI != UE; ++UI) |
| 160 | if (Instruction* I = dyn_cast<Instruction>(UI)) |
| 161 | if (I->getParent()->getParent() == currFunc) { |
| 162 | recursive = true; |
| 163 | break; |
| 164 | } |
| 165 | if (recursive) continue; |
| 166 | |
| 167 | |
| 168 | if (Function* newFunc = unswitchFunction(currFunc)) { |
| 169 | worklist.push_back(newFunc); |
| 170 | changed = true; |
| 171 | } |
| 172 | |
| 173 | } |
| 174 | |
| 175 | return changed; |
Duncan Sands | d5f50da | 2009-07-03 15:30:58 +0000 | [diff] [blame^] | 176 | } |