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/Compiler.h"
25 #include "llvm/Support/CFG.h"
28 STATISTIC(NumPartialInlined
, "Number of functions partially inlined");
31 struct VISIBILITY_HIDDEN PartialInliner
: public ModulePass
{
32 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const { }
33 static char ID
; // Pass identification, replacement for typeid
34 PartialInliner() : ModulePass(&ID
) {}
36 bool runOnModule(Module
& M
);
39 Function
* unswitchFunction(Function
* F
);
43 char PartialInliner::ID
= 0;
44 static RegisterPass
<PartialInliner
> X("partial-inliner", "Partial Inliner");
46 ModulePass
* llvm::createPartialInliningPass() { return new PartialInliner(); }
48 Function
* PartialInliner::unswitchFunction(Function
* F
) {
49 // First, verify that this function is an unswitching candidate...
50 BasicBlock
* entryBlock
= F
->begin();
51 if (!isa
<BranchInst
>(entryBlock
->getTerminator()))
54 BasicBlock
* returnBlock
= 0;
55 BasicBlock
* nonReturnBlock
= 0;
56 unsigned returnCount
= 0;
57 for (succ_iterator SI
= succ_begin(entryBlock
), SE
= succ_end(entryBlock
);
59 if (isa
<ReturnInst
>((*SI
)->getTerminator())) {
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
]);
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
);
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
);
94 PHINode
* retPhi
= PHINode::Create(OldPhi
->getType(), "", Ins
);
95 OldPhi
->replaceAllUsesWith(retPhi
);
96 Ins
= newReturnBlock
->getFirstNonPHI();
98 retPhi
->addIncoming(I
, preReturn
);
99 retPhi
->addIncoming(OldPhi
->getIncomingValueForBlock(newEntryBlock
),
101 OldPhi
->removeIncomingValue(newEntryBlock
);
105 newEntryBlock
->getTerminator()->replaceUsesOfWith(preReturn
, newReturnBlock
);
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
);
116 // The CodeExtractor needs a dominator tree.
118 DT
.runOnFunction(*duplicateFunction
);
120 // Extract the body of the the if.
121 Function
* extractedFunction
= ExtractCodeRegion(DT
, toExtract
);
123 // Inline the top-level if test into all callers.
124 std::vector
<User
*> Users(duplicateFunction
->use_begin(),
125 duplicateFunction
->use_end());
126 for (std::vector
<User
*>::iterator UI
= Users
.begin(), UE
= Users
.end();
128 if (CallInst
* CI
= dyn_cast
<CallInst
>(*UI
))
130 else if (InvokeInst
* II
= dyn_cast
<InvokeInst
>(*UI
))
133 // Ditch the duplicate, since we're done with it, and rewrite all remaining
134 // users (function pointers, etc.) back to the original function.
135 duplicateFunction
->replaceAllUsesWith(F
);
136 duplicateFunction
->eraseFromParent();
140 return extractedFunction
;
143 bool PartialInliner::runOnModule(Module
& M
) {
144 std::vector
<Function
*> worklist
;
145 worklist
.reserve(M
.size());
146 for (Module::iterator FI
= M
.begin(), FE
= M
.end(); FI
!= FE
; ++FI
)
147 if (!FI
->use_empty() && !FI
->isDeclaration())
148 worklist
.push_back(&*FI
);
150 bool changed
= false;
151 while (!worklist
.empty()) {
152 Function
* currFunc
= worklist
.back();
155 if (currFunc
->use_empty()) continue;
157 bool recursive
= false;
158 for (Function::use_iterator UI
= currFunc
->use_begin(),
159 UE
= currFunc
->use_end(); UI
!= UE
; ++UI
)
160 if (Instruction
* I
= dyn_cast
<Instruction
>(UI
))
161 if (I
->getParent()->getParent() == currFunc
) {
165 if (recursive
) continue;
168 if (Function
* newFunc
= unswitchFunction(currFunc
)) {
169 worklist
.push_back(newFunc
);