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 #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"
27 STATISTIC(NumPartialInlined
, "Number of functions partially inlined");
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 initializePartialInlinerPass(*PassRegistry::getPassRegistry());
37 bool runOnModule(Module
& M
);
40 Function
* unswitchFunction(Function
* F
);
44 char PartialInliner::ID
= 0;
45 INITIALIZE_PASS(PartialInliner
, "partial-inliner",
46 "Partial Inliner", false, false)
48 ModulePass
* llvm::createPartialInliningPass() { return new PartialInliner(); }
50 Function
* PartialInliner::unswitchFunction(Function
* F
) {
51 // First, verify that this function is an unswitching candidate...
52 BasicBlock
* entryBlock
= F
->begin();
53 BranchInst
*BR
= dyn_cast
<BranchInst
>(entryBlock
->getTerminator());
54 if (!BR
|| BR
->isUnconditional())
57 BasicBlock
* returnBlock
= 0;
58 BasicBlock
* nonReturnBlock
= 0;
59 unsigned returnCount
= 0;
60 for (succ_iterator SI
= succ_begin(entryBlock
), SE
= succ_end(entryBlock
);
62 if (isa
<ReturnInst
>((*SI
)->getTerminator())) {
71 // Clone the function, so that we can hack away on it.
72 ValueToValueMapTy VMap
;
73 Function
* duplicateFunction
= CloneFunction(F
, VMap
,
74 /*ModuleLevelChanges=*/false);
75 duplicateFunction
->setLinkage(GlobalValue::InternalLinkage
);
76 F
->getParent()->getFunctionList().push_back(duplicateFunction
);
77 BasicBlock
* newEntryBlock
= cast
<BasicBlock
>(VMap
[entryBlock
]);
78 BasicBlock
* newReturnBlock
= cast
<BasicBlock
>(VMap
[returnBlock
]);
79 BasicBlock
* newNonReturnBlock
= cast
<BasicBlock
>(VMap
[nonReturnBlock
]);
81 // Go ahead and update all uses to the duplicate, so that we can just
82 // use the inliner functionality when we're done hacking.
83 F
->replaceAllUsesWith(duplicateFunction
);
85 // Special hackery is needed with PHI nodes that have inputs from more than
86 // one extracted block. For simplicity, just split the PHIs into a two-level
87 // sequence of PHIs, some of which will go in the extracted region, and some
88 // of which will go outside.
89 BasicBlock
* preReturn
= newReturnBlock
;
90 newReturnBlock
= newReturnBlock
->splitBasicBlock(
91 newReturnBlock
->getFirstNonPHI());
92 BasicBlock::iterator I
= preReturn
->begin();
93 BasicBlock::iterator Ins
= newReturnBlock
->begin();
94 while (I
!= preReturn
->end()) {
95 PHINode
* OldPhi
= dyn_cast
<PHINode
>(I
);
98 PHINode
* retPhi
= PHINode::Create(OldPhi
->getType(), 2, "", Ins
);
99 OldPhi
->replaceAllUsesWith(retPhi
);
100 Ins
= newReturnBlock
->getFirstNonPHI();
102 retPhi
->addIncoming(I
, preReturn
);
103 retPhi
->addIncoming(OldPhi
->getIncomingValueForBlock(newEntryBlock
),
105 OldPhi
->removeIncomingValue(newEntryBlock
);
109 newEntryBlock
->getTerminator()->replaceUsesOfWith(preReturn
, newReturnBlock
);
111 // Gather up the blocks that we're going to extract.
112 std::vector
<BasicBlock
*> toExtract
;
113 toExtract
.push_back(newNonReturnBlock
);
114 for (Function::iterator FI
= duplicateFunction
->begin(),
115 FE
= duplicateFunction
->end(); FI
!= FE
; ++FI
)
116 if (&*FI
!= newEntryBlock
&& &*FI
!= newReturnBlock
&&
117 &*FI
!= newNonReturnBlock
)
118 toExtract
.push_back(FI
);
120 // The CodeExtractor needs a dominator tree.
122 DT
.runOnFunction(*duplicateFunction
);
124 // Extract the body of the if.
125 Function
* extractedFunction
= ExtractCodeRegion(DT
, toExtract
);
127 InlineFunctionInfo IFI
;
129 // Inline the top-level if test into all callers.
130 std::vector
<User
*> Users(duplicateFunction
->use_begin(),
131 duplicateFunction
->use_end());
132 for (std::vector
<User
*>::iterator UI
= Users
.begin(), UE
= Users
.end();
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
);
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();
146 return extractedFunction
;
149 bool PartialInliner::runOnModule(Module
& M
) {
150 std::vector
<Function
*> worklist
;
151 worklist
.reserve(M
.size());
152 for (Module::iterator FI
= M
.begin(), FE
= M
.end(); FI
!= FE
; ++FI
)
153 if (!FI
->use_empty() && !FI
->isDeclaration())
154 worklist
.push_back(&*FI
);
156 bool changed
= false;
157 while (!worklist
.empty()) {
158 Function
* currFunc
= worklist
.back();
161 if (currFunc
->use_empty()) continue;
163 bool recursive
= false;
164 for (Function::use_iterator UI
= currFunc
->use_begin(),
165 UE
= currFunc
->use_end(); UI
!= UE
; ++UI
)
166 if (Instruction
* I
= dyn_cast
<Instruction
>(*UI
))
167 if (I
->getParent()->getParent() == currFunc
) {
171 if (recursive
) continue;
174 if (Function
* newFunc
= unswitchFunction(currFunc
)) {
175 worklist
.push_back(newFunc
);