1 //===- PartialInlining.cpp - Inline parts of functions --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs partial inlining, typically by inlining an if statement
11 // that surrounds the body of the function.
13 //===----------------------------------------------------------------------===//
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"
26 #define DEBUG_TYPE "partialinlining"
28 STATISTIC(NumPartialInlined, "Number of functions partially inlined");
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());
38 bool runOnModule(Module& M) override;
41 Function* unswitchFunction(Function* F);
45 char PartialInliner::ID = 0;
46 INITIALIZE_PASS(PartialInliner, "partial-inliner",
47 "Partial Inliner", false, false)
49 ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
51 Function* PartialInliner::unswitchFunction(Function* F) {
52 // First, verify that this function is an unswitching candidate...
53 BasicBlock* entryBlock = F->begin();
54 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
55 if (!BR || BR->isUnconditional())
58 BasicBlock* returnBlock = nullptr;
59 BasicBlock* nonReturnBlock = nullptr;
60 unsigned returnCount = 0;
61 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
63 if (isa<ReturnInst>((*SI)->getTerminator())) {
72 // Clone the function, so that we can hack away on it.
73 ValueToValueMapTy VMap;
74 Function* duplicateFunction = CloneFunction(F, VMap,
75 /*ModuleLevelChanges=*/false);
76 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
77 F->getParent()->getFunctionList().push_back(duplicateFunction);
78 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
79 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
80 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
82 // Go ahead and update all uses to the duplicate, so that we can just
83 // use the inliner functionality when we're done hacking.
84 F->replaceAllUsesWith(duplicateFunction);
86 // Special hackery is needed with PHI nodes that have inputs from more than
87 // one extracted block. For simplicity, just split the PHIs into a two-level
88 // sequence of PHIs, some of which will go in the extracted region, and some
89 // of which will go outside.
90 BasicBlock* preReturn = newReturnBlock;
91 newReturnBlock = newReturnBlock->splitBasicBlock(
92 newReturnBlock->getFirstNonPHI());
93 BasicBlock::iterator I = preReturn->begin();
94 BasicBlock::iterator Ins = newReturnBlock->begin();
95 while (I != preReturn->end()) {
96 PHINode* OldPhi = dyn_cast<PHINode>(I);
99 PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
100 OldPhi->replaceAllUsesWith(retPhi);
101 Ins = newReturnBlock->getFirstNonPHI();
103 retPhi->addIncoming(I, preReturn);
104 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
106 OldPhi->removeIncomingValue(newEntryBlock);
110 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
112 // Gather up the blocks that we're going to extract.
113 std::vector<BasicBlock*> toExtract;
114 toExtract.push_back(newNonReturnBlock);
115 for (Function::iterator FI = duplicateFunction->begin(),
116 FE = duplicateFunction->end(); FI != FE; ++FI)
117 if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
118 &*FI != newNonReturnBlock)
119 toExtract.push_back(FI);
121 // The CodeExtractor needs a dominator tree.
123 DT.recalculate(*duplicateFunction);
125 // Extract the body of the if.
126 Function* extractedFunction
127 = CodeExtractor(toExtract, &DT).extractCodeRegion();
129 InlineFunctionInfo IFI;
131 // Inline the top-level if test into all callers.
132 std::vector<User *> Users(duplicateFunction->user_begin(),
133 duplicateFunction->user_end());
134 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
136 if (CallInst *CI = dyn_cast<CallInst>(*UI))
137 InlineFunction(CI, IFI);
138 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
139 InlineFunction(II, IFI);
141 // Ditch the duplicate, since we're done with it, and rewrite all remaining
142 // users (function pointers, etc.) back to the original function.
143 duplicateFunction->replaceAllUsesWith(F);
144 duplicateFunction->eraseFromParent();
148 return extractedFunction;
151 bool PartialInliner::runOnModule(Module& M) {
152 std::vector<Function*> worklist;
153 worklist.reserve(M.size());
154 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
155 if (!FI->use_empty() && !FI->isDeclaration())
156 worklist.push_back(&*FI);
158 bool changed = false;
159 while (!worklist.empty()) {
160 Function* currFunc = worklist.back();
163 if (currFunc->use_empty()) continue;
165 bool recursive = false;
166 for (User *U : currFunc->users())
167 if (Instruction* I = dyn_cast<Instruction>(U))
168 if (I->getParent()->getParent() == currFunc) {
172 if (recursive) continue;
175 if (Function* newFunc = unswitchFunction(currFunc)) {
176 worklist.push_back(newFunc);