blob: f40902ffa2fd58a1e626706f3defb4bacd70830c [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
33 PartialInliner() : ModulePass(&ID) {}
34
35 bool runOnModule(Module& M);
36
37 private:
38 Function* unswitchFunction(Function* F);
39 };
40}
41
42char PartialInliner::ID = 0;
43static RegisterPass<PartialInliner> X("partial-inliner", "Partial Inliner");
44
45ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
46
47Function* PartialInliner::unswitchFunction(Function* F) {
48 // First, verify that this function is an unswitching candidate...
49 BasicBlock* entryBlock = F->begin();
Owen Andersona0ecbe72009-09-08 19:53:15 +000050 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
51 if (!BR || BR->isUnconditional())
Owen Andersonca399022009-06-14 08:26:32 +000052 return 0;
53
54 BasicBlock* returnBlock = 0;
55 BasicBlock* nonReturnBlock = 0;
56 unsigned returnCount = 0;
57 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
58 SI != SE; ++SI)
59 if (isa<ReturnInst>((*SI)->getTerminator())) {
60 returnBlock = *SI;
61 returnCount++;
62 } else
63 nonReturnBlock = *SI;
64
65 if (returnCount != 1)
66 return 0;
67
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]);
76
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);
80
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);
92 if (!OldPhi) break;
93
94 PHINode* retPhi = PHINode::Create(OldPhi->getType(), "", Ins);
95 OldPhi->replaceAllUsesWith(retPhi);
96 Ins = newReturnBlock->getFirstNonPHI();
97
98 retPhi->addIncoming(I, preReturn);
99 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
100 newEntryBlock);
101 OldPhi->removeIncomingValue(newEntryBlock);
102
103 ++I;
104 }
105 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
106
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);
115
116 // The CodeExtractor needs a dominator tree.
117 DominatorTree DT;
118 DT.runOnFunction(*duplicateFunction);
119
120 // Extract the body of the the if.
121 Function* extractedFunction = ExtractCodeRegion(DT, toExtract);
122
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();
127 UI != UE; ++UI)
128 if (CallInst* CI = dyn_cast<CallInst>(*UI))
129 InlineFunction(CI);
130 else if (InvokeInst* II = dyn_cast<InvokeInst>(*UI))
131 InlineFunction(II);
132
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();
137
Owen Anderson63676842009-06-15 20:50:26 +0000138 ++NumPartialInlined;
139
Owen Andersonca399022009-06-14 08:26:32 +0000140 return extractedFunction;
141}
142
143bool 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())
Dan Gohmanb7a9f2b2010-01-05 16:20:55 +0000148 worklist.push_back(&*FI);
Owen Andersonca399022009-06-14 08:26:32 +0000149
150 bool changed = false;
151 while (!worklist.empty()) {
152 Function* currFunc = worklist.back();
153 worklist.pop_back();
154
155 if (currFunc->use_empty()) continue;
156
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) {
162 recursive = true;
163 break;
164 }
165 if (recursive) continue;
166
167
168 if (Function* newFunc = unswitchFunction(currFunc)) {
169 worklist.push_back(newFunc);
170 changed = true;
171 }
172
173 }
174
175 return changed;
Duncan Sandsd5f50da2009-07-03 15:30:58 +0000176}