blob: eba7b3ae4d135638c99023f537c9f5537a7a5565 [file] [log] [blame]
Owen Andersonca399022009-06-14 08:26:32 +00001//===- 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 Anderson63676842009-06-15 20:50:26 +000023#include "llvm/ADT/Statistic.h"
Owen Andersonca399022009-06-14 08:26:32 +000024#include "llvm/Support/CFG.h"
25using namespace llvm;
26
Owen Anderson63676842009-06-15 20:50:26 +000027STATISTIC(NumPartialInlined, "Number of functions partially inlined");
28
Owen Andersonca399022009-06-14 08:26:32 +000029namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000030 struct PartialInliner : public ModulePass {
Owen Andersonca399022009-06-14 08:26:32 +000031 virtual void getAnalysisUsage(AnalysisUsage &AU) const { }
32 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000033 PartialInliner() : ModulePass(ID) {}
Owen Andersonca399022009-06-14 08:26:32 +000034
35 bool runOnModule(Module& M);
36
37 private:
38 Function* unswitchFunction(Function* F);
39 };
40}
41
42char PartialInliner::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000043INITIALIZE_PASS(PartialInliner, "partial-inliner",
44 "Partial Inliner", false, false);
Owen Andersonca399022009-06-14 08:26:32 +000045
46ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
47
48Function* PartialInliner::unswitchFunction(Function* F) {
49 // First, verify that this function is an unswitching candidate...
50 BasicBlock* entryBlock = F->begin();
Owen Andersona0ecbe72009-09-08 19:53:15 +000051 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
52 if (!BR || BR->isUnconditional())
Owen Andersonca399022009-06-14 08:26:32 +000053 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 Patele9916a32010-06-24 00:33:28 +000070 ValueMap<const Value*, Value*> VMap;
71 Function* duplicateFunction = CloneFunction(F, VMap);
Owen Andersonca399022009-06-14 08:26:32 +000072 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
73 F->getParent()->getFunctionList().push_back(duplicateFunction);
Devang Patele9916a32010-06-24 00:33:28 +000074 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
75 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
76 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
Owen Andersonca399022009-06-14 08:26:32 +000077
78 // Go ahead and update all uses to the duplicate, so that we can just
79 // use the inliner functionality when we're done hacking.
80 F->replaceAllUsesWith(duplicateFunction);
81
82 // Special hackery is needed with PHI nodes that have inputs from more than
83 // one extracted block. For simplicity, just split the PHIs into a two-level
84 // sequence of PHIs, some of which will go in the extracted region, and some
85 // of which will go outside.
86 BasicBlock* preReturn = newReturnBlock;
87 newReturnBlock = newReturnBlock->splitBasicBlock(
88 newReturnBlock->getFirstNonPHI());
89 BasicBlock::iterator I = preReturn->begin();
90 BasicBlock::iterator Ins = newReturnBlock->begin();
91 while (I != preReturn->end()) {
92 PHINode* OldPhi = dyn_cast<PHINode>(I);
93 if (!OldPhi) break;
94
95 PHINode* retPhi = PHINode::Create(OldPhi->getType(), "", Ins);
96 OldPhi->replaceAllUsesWith(retPhi);
97 Ins = newReturnBlock->getFirstNonPHI();
98
99 retPhi->addIncoming(I, preReturn);
100 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
101 newEntryBlock);
102 OldPhi->removeIncomingValue(newEntryBlock);
103
104 ++I;
105 }
106 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
107
108 // Gather up the blocks that we're going to extract.
109 std::vector<BasicBlock*> toExtract;
110 toExtract.push_back(newNonReturnBlock);
111 for (Function::iterator FI = duplicateFunction->begin(),
112 FE = duplicateFunction->end(); FI != FE; ++FI)
113 if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
114 &*FI != newNonReturnBlock)
115 toExtract.push_back(FI);
116
117 // The CodeExtractor needs a dominator tree.
118 DominatorTree DT;
119 DT.runOnFunction(*duplicateFunction);
120
Dan Gohmanf451cb82010-02-10 16:03:48 +0000121 // Extract the body of the if.
Owen Andersonca399022009-06-14 08:26:32 +0000122 Function* extractedFunction = ExtractCodeRegion(DT, toExtract);
123
Chris Lattner60915142010-04-22 23:07:58 +0000124 InlineFunctionInfo IFI;
125
Owen Andersonca399022009-06-14 08:26:32 +0000126 // Inline the top-level if test into all callers.
127 std::vector<User*> Users(duplicateFunction->use_begin(),
128 duplicateFunction->use_end());
129 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
130 UI != UE; ++UI)
Chris Lattner60915142010-04-22 23:07:58 +0000131 if (CallInst *CI = dyn_cast<CallInst>(*UI))
132 InlineFunction(CI, IFI);
133 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
134 InlineFunction(II, IFI);
Owen Andersonca399022009-06-14 08:26:32 +0000135
136 // Ditch the duplicate, since we're done with it, and rewrite all remaining
137 // users (function pointers, etc.) back to the original function.
138 duplicateFunction->replaceAllUsesWith(F);
139 duplicateFunction->eraseFromParent();
140
Owen Anderson63676842009-06-15 20:50:26 +0000141 ++NumPartialInlined;
142
Owen Andersonca399022009-06-14 08:26:32 +0000143 return extractedFunction;
144}
145
146bool PartialInliner::runOnModule(Module& M) {
147 std::vector<Function*> worklist;
148 worklist.reserve(M.size());
149 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
150 if (!FI->use_empty() && !FI->isDeclaration())
Dan Gohmanb7a9f2b2010-01-05 16:20:55 +0000151 worklist.push_back(&*FI);
Owen Andersonca399022009-06-14 08:26:32 +0000152
153 bool changed = false;
154 while (!worklist.empty()) {
155 Function* currFunc = worklist.back();
156 worklist.pop_back();
157
158 if (currFunc->use_empty()) continue;
159
160 bool recursive = false;
161 for (Function::use_iterator UI = currFunc->use_begin(),
162 UE = currFunc->use_end(); UI != UE; ++UI)
Gabor Greif85e01df2010-07-22 11:48:35 +0000163 if (Instruction* I = dyn_cast<Instruction>(*UI))
Owen Andersonca399022009-06-14 08:26:32 +0000164 if (I->getParent()->getParent() == currFunc) {
165 recursive = true;
166 break;
167 }
168 if (recursive) continue;
169
170
171 if (Function* newFunc = unswitchFunction(currFunc)) {
172 worklist.push_back(newFunc);
173 changed = true;
174 }
175
176 }
177
178 return changed;
Duncan Sandsd5f50da2009-07-03 15:30:58 +0000179}