PartialInlining.cpp revision 207618
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" 23#include "llvm/ADT/Statistic.h" 24#include "llvm/Support/CFG.h" 25using namespace llvm; 26 27STATISTIC(NumPartialInlined, "Number of functions partially inlined"); 28 29namespace { 30 struct PartialInliner : public ModulePass { 31 virtual void getAnalysisUsage(AnalysisUsage &AU) const { } 32 static char ID; // Pass identification, replacement for typeid 33 PartialInliner() : ModulePass(&ID) {} 34 35 bool runOnModule(Module& M); 36 37 private: 38 Function* unswitchFunction(Function* F); 39 }; 40} 41 42char PartialInliner::ID = 0; 43static RegisterPass<PartialInliner> X("partial-inliner", "Partial Inliner"); 44 45ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); } 46 47Function* PartialInliner::unswitchFunction(Function* F) { 48 // First, verify that this function is an unswitching candidate... 49 BasicBlock* entryBlock = F->begin(); 50 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); 51 if (!BR || BR->isUnconditional()) 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 if. 121 Function* extractedFunction = ExtractCodeRegion(DT, toExtract); 122 123 InlineFunctionInfo IFI; 124 125 // Inline the top-level if test into all callers. 126 std::vector<User*> Users(duplicateFunction->use_begin(), 127 duplicateFunction->use_end()); 128 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end(); 129 UI != UE; ++UI) 130 if (CallInst *CI = dyn_cast<CallInst>(*UI)) 131 InlineFunction(CI, IFI); 132 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) 133 InlineFunction(II, IFI); 134 135 // Ditch the duplicate, since we're done with it, and rewrite all remaining 136 // users (function pointers, etc.) back to the original function. 137 duplicateFunction->replaceAllUsesWith(F); 138 duplicateFunction->eraseFromParent(); 139 140 ++NumPartialInlined; 141 142 return extractedFunction; 143} 144 145bool PartialInliner::runOnModule(Module& M) { 146 std::vector<Function*> worklist; 147 worklist.reserve(M.size()); 148 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) 149 if (!FI->use_empty() && !FI->isDeclaration()) 150 worklist.push_back(&*FI); 151 152 bool changed = false; 153 while (!worklist.empty()) { 154 Function* currFunc = worklist.back(); 155 worklist.pop_back(); 156 157 if (currFunc->use_empty()) continue; 158 159 bool recursive = false; 160 for (Function::use_iterator UI = currFunc->use_begin(), 161 UE = currFunc->use_end(); UI != UE; ++UI) 162 if (Instruction* I = dyn_cast<Instruction>(UI)) 163 if (I->getParent()->getParent() == currFunc) { 164 recursive = true; 165 break; 166 } 167 if (recursive) continue; 168 169 170 if (Function* newFunc = unswitchFunction(currFunc)) { 171 worklist.push_back(newFunc); 172 changed = true; 173 } 174 175 } 176 177 return changed; 178} 179