blob: ac88aee45fc41050dbfa66caeeb086c749e606d6 [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"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
Stephen Hines36b56882014-04-23 16:57:46 -070018#include "llvm/IR/CFG.h"
19#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
Owen Andersonca399022009-06-14 08:26:32 +000022#include "llvm/Pass.h"
Owen Andersonca399022009-06-14 08:26:32 +000023#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth99650c92012-05-04 10:18:49 +000024#include "llvm/Transforms/Utils/CodeExtractor.h"
Owen Andersonca399022009-06-14 08:26:32 +000025using 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 {
Stephen Hines36b56882014-04-23 16:57:46 -070031 void getAnalysisUsage(AnalysisUsage &AU) const override { }
Owen Andersonca399022009-06-14 08:26:32 +000032 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000033 PartialInliner() : ModulePass(ID) {
34 initializePartialInlinerPass(*PassRegistry::getPassRegistry());
35 }
Stephen Hines36b56882014-04-23 16:57:46 -070036
37 bool runOnModule(Module& M) override;
38
Owen Andersonca399022009-06-14 08:26:32 +000039 private:
40 Function* unswitchFunction(Function* F);
41 };
42}
43
44char PartialInliner::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000045INITIALIZE_PASS(PartialInliner, "partial-inliner",
Owen Andersonce665bd2010-10-07 22:25:06 +000046 "Partial Inliner", false, false)
Owen Andersonca399022009-06-14 08:26:32 +000047
48ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
49
50Function* PartialInliner::unswitchFunction(Function* F) {
51 // First, verify that this function is an unswitching candidate...
52 BasicBlock* entryBlock = F->begin();
Owen Andersona0ecbe72009-09-08 19:53:15 +000053 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
54 if (!BR || BR->isUnconditional())
Owen Andersonca399022009-06-14 08:26:32 +000055 return 0;
56
57 BasicBlock* returnBlock = 0;
58 BasicBlock* nonReturnBlock = 0;
59 unsigned returnCount = 0;
60 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
61 SI != SE; ++SI)
62 if (isa<ReturnInst>((*SI)->getTerminator())) {
63 returnBlock = *SI;
64 returnCount++;
65 } else
66 nonReturnBlock = *SI;
67
68 if (returnCount != 1)
69 return 0;
70
71 // Clone the function, so that we can hack away on it.
Rafael Espindola1ed219a2010-10-13 01:36:30 +000072 ValueToValueMapTy VMap;
Dan Gohman6cb8c232010-08-26 15:41:53 +000073 Function* duplicateFunction = CloneFunction(F, VMap,
74 /*ModuleLevelChanges=*/false);
Owen Andersonca399022009-06-14 08:26:32 +000075 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
76 F->getParent()->getFunctionList().push_back(duplicateFunction);
Devang Patele9916a32010-06-24 00:33:28 +000077 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
78 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
79 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
Owen Andersonca399022009-06-14 08:26:32 +000080
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);
84
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);
96 if (!OldPhi) break;
97
Jay Foad3ecfc862011-03-30 11:28:46 +000098 PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
Owen Andersonca399022009-06-14 08:26:32 +000099 OldPhi->replaceAllUsesWith(retPhi);
100 Ins = newReturnBlock->getFirstNonPHI();
101
102 retPhi->addIncoming(I, preReturn);
103 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
104 newEntryBlock);
105 OldPhi->removeIncomingValue(newEntryBlock);
106
107 ++I;
108 }
109 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
110
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);
119
120 // The CodeExtractor needs a dominator tree.
121 DominatorTree DT;
Stephen Hines36b56882014-04-23 16:57:46 -0700122 DT.recalculate(*duplicateFunction);
123
Dan Gohmanf451cb82010-02-10 16:03:48 +0000124 // Extract the body of the if.
Chandler Carruth99650c92012-05-04 10:18:49 +0000125 Function* extractedFunction
126 = CodeExtractor(toExtract, &DT).extractCodeRegion();
Owen Andersonca399022009-06-14 08:26:32 +0000127
Chris Lattner60915142010-04-22 23:07:58 +0000128 InlineFunctionInfo IFI;
129
Owen Andersonca399022009-06-14 08:26:32 +0000130 // Inline the top-level if test into all callers.
Stephen Hines36b56882014-04-23 16:57:46 -0700131 std::vector<User *> Users(duplicateFunction->user_begin(),
132 duplicateFunction->user_end());
Owen Andersonca399022009-06-14 08:26:32 +0000133 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
134 UI != UE; ++UI)
Chris Lattner60915142010-04-22 23:07:58 +0000135 if (CallInst *CI = dyn_cast<CallInst>(*UI))
136 InlineFunction(CI, IFI);
137 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
138 InlineFunction(II, IFI);
Owen Andersonca399022009-06-14 08:26:32 +0000139
140 // Ditch the duplicate, since we're done with it, and rewrite all remaining
141 // users (function pointers, etc.) back to the original function.
142 duplicateFunction->replaceAllUsesWith(F);
143 duplicateFunction->eraseFromParent();
144
Owen Anderson63676842009-06-15 20:50:26 +0000145 ++NumPartialInlined;
146
Owen Andersonca399022009-06-14 08:26:32 +0000147 return extractedFunction;
148}
149
150bool PartialInliner::runOnModule(Module& M) {
151 std::vector<Function*> worklist;
152 worklist.reserve(M.size());
153 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
154 if (!FI->use_empty() && !FI->isDeclaration())
Dan Gohmanb7a9f2b2010-01-05 16:20:55 +0000155 worklist.push_back(&*FI);
Owen Andersonca399022009-06-14 08:26:32 +0000156
157 bool changed = false;
158 while (!worklist.empty()) {
159 Function* currFunc = worklist.back();
160 worklist.pop_back();
161
162 if (currFunc->use_empty()) continue;
163
164 bool recursive = false;
Stephen Hines36b56882014-04-23 16:57:46 -0700165 for (User *U : currFunc->users())
166 if (Instruction* I = dyn_cast<Instruction>(U))
Owen Andersonca399022009-06-14 08:26:32 +0000167 if (I->getParent()->getParent() == currFunc) {
168 recursive = true;
169 break;
170 }
171 if (recursive) continue;
172
173
174 if (Function* newFunc = unswitchFunction(currFunc)) {
175 worklist.push_back(newFunc);
176 changed = true;
177 }
178
179 }
180
181 return changed;
Duncan Sandsd5f50da2009-07-03 15:30:58 +0000182}