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/CFG.h" |
| 25 | using namespace llvm; |
| 26 | |
Owen Anderson | 6367684 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 27 | STATISTIC(NumPartialInlined, "Number of functions partially inlined"); |
| 28 | |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 29 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 30 | struct PartialInliner : public ModulePass { |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 31 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { } |
| 32 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 33 | PartialInliner() : ModulePass(ID) {} |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 34 | |
| 35 | bool runOnModule(Module& M); |
| 36 | |
| 37 | private: |
| 38 | Function* unswitchFunction(Function* F); |
| 39 | }; |
| 40 | } |
| 41 | |
| 42 | char PartialInliner::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 43 | INITIALIZE_PASS(PartialInliner, "partial-inliner", |
| 44 | "Partial Inliner", false, false); |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 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(); |
Owen Anderson | a0ecbe7 | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 51 | BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); |
| 52 | if (!BR || BR->isUnconditional()) |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 53 | return 0; |
| 54 | |
| 55 | BasicBlock* returnBlock = 0; |
| 56 | BasicBlock* nonReturnBlock = 0; |
| 57 | unsigned returnCount = 0; |
| 58 | for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock); |
| 59 | SI != SE; ++SI) |
| 60 | if (isa<ReturnInst>((*SI)->getTerminator())) { |
| 61 | returnBlock = *SI; |
| 62 | returnCount++; |
| 63 | } else |
| 64 | nonReturnBlock = *SI; |
| 65 | |
| 66 | if (returnCount != 1) |
| 67 | return 0; |
| 68 | |
| 69 | // Clone the function, so that we can hack away on it. |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 70 | ValueMap<const Value*, Value*> VMap; |
Dan Gohman | 6cb8c23 | 2010-08-26 15:41:53 +0000 | [diff] [blame] | 71 | Function* duplicateFunction = CloneFunction(F, VMap, |
| 72 | /*ModuleLevelChanges=*/false); |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 73 | duplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
| 74 | F->getParent()->getFunctionList().push_back(duplicateFunction); |
Devang Patel | e9916a3 | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 75 | BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]); |
| 76 | BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]); |
| 77 | BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]); |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 78 | |
| 79 | // Go ahead and update all uses to the duplicate, so that we can just |
| 80 | // use the inliner functionality when we're done hacking. |
| 81 | F->replaceAllUsesWith(duplicateFunction); |
| 82 | |
| 83 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 84 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 85 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 86 | // of which will go outside. |
| 87 | BasicBlock* preReturn = newReturnBlock; |
| 88 | newReturnBlock = newReturnBlock->splitBasicBlock( |
| 89 | newReturnBlock->getFirstNonPHI()); |
| 90 | BasicBlock::iterator I = preReturn->begin(); |
| 91 | BasicBlock::iterator Ins = newReturnBlock->begin(); |
| 92 | while (I != preReturn->end()) { |
| 93 | PHINode* OldPhi = dyn_cast<PHINode>(I); |
| 94 | if (!OldPhi) break; |
| 95 | |
| 96 | PHINode* retPhi = PHINode::Create(OldPhi->getType(), "", Ins); |
| 97 | OldPhi->replaceAllUsesWith(retPhi); |
| 98 | Ins = newReturnBlock->getFirstNonPHI(); |
| 99 | |
| 100 | retPhi->addIncoming(I, preReturn); |
| 101 | retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock), |
| 102 | newEntryBlock); |
| 103 | OldPhi->removeIncomingValue(newEntryBlock); |
| 104 | |
| 105 | ++I; |
| 106 | } |
| 107 | newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock); |
| 108 | |
| 109 | // Gather up the blocks that we're going to extract. |
| 110 | std::vector<BasicBlock*> toExtract; |
| 111 | toExtract.push_back(newNonReturnBlock); |
| 112 | for (Function::iterator FI = duplicateFunction->begin(), |
| 113 | FE = duplicateFunction->end(); FI != FE; ++FI) |
| 114 | if (&*FI != newEntryBlock && &*FI != newReturnBlock && |
| 115 | &*FI != newNonReturnBlock) |
| 116 | toExtract.push_back(FI); |
| 117 | |
| 118 | // The CodeExtractor needs a dominator tree. |
| 119 | DominatorTree DT; |
| 120 | DT.runOnFunction(*duplicateFunction); |
| 121 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 122 | // Extract the body of the if. |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 123 | Function* extractedFunction = ExtractCodeRegion(DT, toExtract); |
| 124 | |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 125 | InlineFunctionInfo IFI; |
| 126 | |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 127 | // Inline the top-level if test into all callers. |
| 128 | std::vector<User*> Users(duplicateFunction->use_begin(), |
| 129 | duplicateFunction->use_end()); |
| 130 | for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end(); |
| 131 | UI != UE; ++UI) |
Chris Lattner | 6091514 | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 132 | if (CallInst *CI = dyn_cast<CallInst>(*UI)) |
| 133 | InlineFunction(CI, IFI); |
| 134 | else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) |
| 135 | InlineFunction(II, IFI); |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 136 | |
| 137 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 138 | // users (function pointers, etc.) back to the original function. |
| 139 | duplicateFunction->replaceAllUsesWith(F); |
| 140 | duplicateFunction->eraseFromParent(); |
| 141 | |
Owen Anderson | 6367684 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 142 | ++NumPartialInlined; |
| 143 | |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 144 | return extractedFunction; |
| 145 | } |
| 146 | |
| 147 | bool PartialInliner::runOnModule(Module& M) { |
| 148 | std::vector<Function*> worklist; |
| 149 | worklist.reserve(M.size()); |
| 150 | for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) |
| 151 | if (!FI->use_empty() && !FI->isDeclaration()) |
Dan Gohman | b7a9f2b | 2010-01-05 16:20:55 +0000 | [diff] [blame] | 152 | worklist.push_back(&*FI); |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 153 | |
| 154 | bool changed = false; |
| 155 | while (!worklist.empty()) { |
| 156 | Function* currFunc = worklist.back(); |
| 157 | worklist.pop_back(); |
| 158 | |
| 159 | if (currFunc->use_empty()) continue; |
| 160 | |
| 161 | bool recursive = false; |
| 162 | for (Function::use_iterator UI = currFunc->use_begin(), |
| 163 | UE = currFunc->use_end(); UI != UE; ++UI) |
Gabor Greif | 85e01df | 2010-07-22 11:48:35 +0000 | [diff] [blame] | 164 | if (Instruction* I = dyn_cast<Instruction>(*UI)) |
Owen Anderson | ca39902 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 165 | if (I->getParent()->getParent() == currFunc) { |
| 166 | recursive = true; |
| 167 | break; |
| 168 | } |
| 169 | if (recursive) continue; |
| 170 | |
| 171 | |
| 172 | if (Function* newFunc = unswitchFunction(currFunc)) { |
| 173 | worklist.push_back(newFunc); |
| 174 | changed = true; |
| 175 | } |
| 176 | |
| 177 | } |
| 178 | |
| 179 | return changed; |
Duncan Sands | d5f50da | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 180 | } |