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 #include "llvm/Transforms/IPO.h" 16 #include "llvm/ADT/Statistic.h" 17 #include "llvm/IR/CFG.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/IR/Instructions.h" 20 #include "llvm/IR/Module.h" 21 #include "llvm/Pass.h" 22 #include "llvm/Transforms/Utils/Cloning.h" 23 #include "llvm/Transforms/Utils/CodeExtractor.h" 24 using namespace llvm; 25 26 #define DEBUG_TYPE "partialinlining" 27 28 STATISTIC(NumPartialInlined, "Number of functions partially inlined"); 29 30 namespace { 31 struct PartialInliner : public ModulePass { 32 void getAnalysisUsage(AnalysisUsage &AU) const override { } 33 static char ID; // Pass identification, replacement for typeid 34 PartialInliner() : ModulePass(ID) { 35 initializePartialInlinerPass(*PassRegistry::getPassRegistry()); 36 } 37 38 bool runOnModule(Module& M) override; 39 40 private: 41 Function* unswitchFunction(Function* F); 42 }; 43 } 44 45 char PartialInliner::ID = 0; 46 INITIALIZE_PASS(PartialInliner, "partial-inliner", 47 "Partial Inliner", false, false) 48 49 ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); } 50 51 Function* PartialInliner::unswitchFunction(Function* F) { 52 // First, verify that this function is an unswitching candidate... 53 BasicBlock *entryBlock = &F->front(); 54 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); 55 if (!BR || BR->isUnconditional()) 56 return nullptr; 57 58 BasicBlock* returnBlock = nullptr; 59 BasicBlock* nonReturnBlock = nullptr; 60 unsigned returnCount = 0; 61 for (BasicBlock *BB : successors(entryBlock)) { 62 if (isa<ReturnInst>(BB->getTerminator())) { 63 returnBlock = BB; 64 returnCount++; 65 } else 66 nonReturnBlock = BB; 67 } 68 69 if (returnCount != 1) 70 return nullptr; 71 72 // Clone the function, so that we can hack away on it. 73 ValueToValueMapTy VMap; 74 Function* duplicateFunction = CloneFunction(F, VMap); 75 duplicateFunction->setLinkage(GlobalValue::InternalLinkage); 76 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]); 77 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]); 78 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]); 79 80 // Go ahead and update all uses to the duplicate, so that we can just 81 // use the inliner functionality when we're done hacking. 82 F->replaceAllUsesWith(duplicateFunction); 83 84 // Special hackery is needed with PHI nodes that have inputs from more than 85 // one extracted block. For simplicity, just split the PHIs into a two-level 86 // sequence of PHIs, some of which will go in the extracted region, and some 87 // of which will go outside. 88 BasicBlock* preReturn = newReturnBlock; 89 newReturnBlock = newReturnBlock->splitBasicBlock( 90 newReturnBlock->getFirstNonPHI()->getIterator()); 91 BasicBlock::iterator I = preReturn->begin(); 92 Instruction *Ins = &newReturnBlock->front(); 93 while (I != preReturn->end()) { 94 PHINode* OldPhi = dyn_cast<PHINode>(I); 95 if (!OldPhi) break; 96 97 PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); 98 OldPhi->replaceAllUsesWith(retPhi); 99 Ins = newReturnBlock->getFirstNonPHI(); 100 101 retPhi->addIncoming(&*I, preReturn); 102 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock), 103 newEntryBlock); 104 OldPhi->removeIncomingValue(newEntryBlock); 105 106 ++I; 107 } 108 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock); 109 110 // Gather up the blocks that we're going to extract. 111 std::vector<BasicBlock*> toExtract; 112 toExtract.push_back(newNonReturnBlock); 113 for (Function::iterator FI = duplicateFunction->begin(), 114 FE = duplicateFunction->end(); FI != FE; ++FI) 115 if (&*FI != newEntryBlock && &*FI != newReturnBlock && 116 &*FI != newNonReturnBlock) 117 toExtract.push_back(&*FI); 118 119 // The CodeExtractor needs a dominator tree. 120 DominatorTree DT; 121 DT.recalculate(*duplicateFunction); 122 123 // Extract the body of the if. 124 Function* extractedFunction 125 = CodeExtractor(toExtract, &DT).extractCodeRegion(); 126 127 InlineFunctionInfo IFI; 128 129 // Inline the top-level if test into all callers. 130 std::vector<User *> Users(duplicateFunction->user_begin(), 131 duplicateFunction->user_end()); 132 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end(); 133 UI != UE; ++UI) 134 if (CallInst *CI = dyn_cast<CallInst>(*UI)) 135 InlineFunction(CI, IFI); 136 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI)) 137 InlineFunction(II, IFI); 138 139 // Ditch the duplicate, since we're done with it, and rewrite all remaining 140 // users (function pointers, etc.) back to the original function. 141 duplicateFunction->replaceAllUsesWith(F); 142 duplicateFunction->eraseFromParent(); 143 144 ++NumPartialInlined; 145 146 return extractedFunction; 147 } 148 149 bool PartialInliner::runOnModule(Module& M) { 150 if (skipModule(M)) 151 return false; 152 153 std::vector<Function*> worklist; 154 worklist.reserve(M.size()); 155 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI) 156 if (!FI->use_empty() && !FI->isDeclaration()) 157 worklist.push_back(&*FI); 158 159 bool changed = false; 160 while (!worklist.empty()) { 161 Function* currFunc = worklist.back(); 162 worklist.pop_back(); 163 164 if (currFunc->use_empty()) continue; 165 166 bool recursive = false; 167 for (User *U : currFunc->users()) 168 if (Instruction* I = dyn_cast<Instruction>(U)) 169 if (I->getParent()->getParent() == currFunc) { 170 recursive = true; 171 break; 172 } 173 if (recursive) continue; 174 175 176 if (Function* newFunc = unswitchFunction(currFunc)) { 177 worklist.push_back(newFunc); 178 changed = true; 179 } 180 181 } 182 183 return changed; 184 } 185