blob: 76d6dfa8e881b205d66ea32e02b9e94c0fbc0499 [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
Owen Andersonca399022009-06-14 08:26:32 +000015#include "llvm/Transforms/IPO.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Stephen Hines36b56882014-04-23 16:57:46 -070017#include "llvm/IR/CFG.h"
18#include "llvm/IR/Dominators.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000019#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Module.h"
Owen Andersonca399022009-06-14 08:26:32 +000021#include "llvm/Pass.h"
Owen Andersonca399022009-06-14 08:26:32 +000022#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth99650c92012-05-04 10:18:49 +000023#include "llvm/Transforms/Utils/CodeExtractor.h"
Owen Andersonca399022009-06-14 08:26:32 +000024using namespace llvm;
25
Stephen Hinesdce4a402014-05-29 02:49:00 -070026#define DEBUG_TYPE "partialinlining"
27
Owen Anderson63676842009-06-15 20:50:26 +000028STATISTIC(NumPartialInlined, "Number of functions partially inlined");
29
Owen Andersonca399022009-06-14 08:26:32 +000030namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000031 struct PartialInliner : public ModulePass {
Stephen Hines36b56882014-04-23 16:57:46 -070032 void getAnalysisUsage(AnalysisUsage &AU) const override { }
Owen Andersonca399022009-06-14 08:26:32 +000033 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000034 PartialInliner() : ModulePass(ID) {
35 initializePartialInlinerPass(*PassRegistry::getPassRegistry());
36 }
Stephen Hines36b56882014-04-23 16:57:46 -070037
38 bool runOnModule(Module& M) override;
39
Owen Andersonca399022009-06-14 08:26:32 +000040 private:
41 Function* unswitchFunction(Function* F);
42 };
43}
44
45char PartialInliner::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000046INITIALIZE_PASS(PartialInliner, "partial-inliner",
Owen Andersonce665bd2010-10-07 22:25:06 +000047 "Partial Inliner", false, false)
Owen Andersonca399022009-06-14 08:26:32 +000048
49ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
50
51Function* PartialInliner::unswitchFunction(Function* F) {
52 // First, verify that this function is an unswitching candidate...
53 BasicBlock* entryBlock = F->begin();
Owen Andersona0ecbe72009-09-08 19:53:15 +000054 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
55 if (!BR || BR->isUnconditional())
Stephen Hinesdce4a402014-05-29 02:49:00 -070056 return nullptr;
Owen Andersonca399022009-06-14 08:26:32 +000057
Stephen Hinesdce4a402014-05-29 02:49:00 -070058 BasicBlock* returnBlock = nullptr;
59 BasicBlock* nonReturnBlock = nullptr;
Owen Andersonca399022009-06-14 08:26:32 +000060 unsigned returnCount = 0;
61 for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
62 SI != SE; ++SI)
63 if (isa<ReturnInst>((*SI)->getTerminator())) {
64 returnBlock = *SI;
65 returnCount++;
66 } else
67 nonReturnBlock = *SI;
68
69 if (returnCount != 1)
Stephen Hinesdce4a402014-05-29 02:49:00 -070070 return nullptr;
Owen Andersonca399022009-06-14 08:26:32 +000071
72 // Clone the function, so that we can hack away on it.
Rafael Espindola1ed219a2010-10-13 01:36:30 +000073 ValueToValueMapTy VMap;
Dan Gohman6cb8c232010-08-26 15:41:53 +000074 Function* duplicateFunction = CloneFunction(F, VMap,
75 /*ModuleLevelChanges=*/false);
Owen Andersonca399022009-06-14 08:26:32 +000076 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
77 F->getParent()->getFunctionList().push_back(duplicateFunction);
Devang Patele9916a32010-06-24 00:33:28 +000078 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
79 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
80 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
Owen Andersonca399022009-06-14 08:26:32 +000081
82 // Go ahead and update all uses to the duplicate, so that we can just
83 // use the inliner functionality when we're done hacking.
84 F->replaceAllUsesWith(duplicateFunction);
85
86 // Special hackery is needed with PHI nodes that have inputs from more than
87 // one extracted block. For simplicity, just split the PHIs into a two-level
88 // sequence of PHIs, some of which will go in the extracted region, and some
89 // of which will go outside.
90 BasicBlock* preReturn = newReturnBlock;
91 newReturnBlock = newReturnBlock->splitBasicBlock(
92 newReturnBlock->getFirstNonPHI());
93 BasicBlock::iterator I = preReturn->begin();
94 BasicBlock::iterator Ins = newReturnBlock->begin();
95 while (I != preReturn->end()) {
96 PHINode* OldPhi = dyn_cast<PHINode>(I);
97 if (!OldPhi) break;
98
Jay Foad3ecfc862011-03-30 11:28:46 +000099 PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
Owen Andersonca399022009-06-14 08:26:32 +0000100 OldPhi->replaceAllUsesWith(retPhi);
101 Ins = newReturnBlock->getFirstNonPHI();
102
103 retPhi->addIncoming(I, preReturn);
104 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
105 newEntryBlock);
106 OldPhi->removeIncomingValue(newEntryBlock);
107
108 ++I;
109 }
110 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
111
112 // Gather up the blocks that we're going to extract.
113 std::vector<BasicBlock*> toExtract;
114 toExtract.push_back(newNonReturnBlock);
115 for (Function::iterator FI = duplicateFunction->begin(),
116 FE = duplicateFunction->end(); FI != FE; ++FI)
117 if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
118 &*FI != newNonReturnBlock)
119 toExtract.push_back(FI);
120
121 // The CodeExtractor needs a dominator tree.
122 DominatorTree DT;
Stephen Hines36b56882014-04-23 16:57:46 -0700123 DT.recalculate(*duplicateFunction);
124
Dan Gohmanf451cb82010-02-10 16:03:48 +0000125 // Extract the body of the if.
Chandler Carruth99650c92012-05-04 10:18:49 +0000126 Function* extractedFunction
127 = CodeExtractor(toExtract, &DT).extractCodeRegion();
Owen Andersonca399022009-06-14 08:26:32 +0000128
Chris Lattner60915142010-04-22 23:07:58 +0000129 InlineFunctionInfo IFI;
130
Owen Andersonca399022009-06-14 08:26:32 +0000131 // Inline the top-level if test into all callers.
Stephen Hines36b56882014-04-23 16:57:46 -0700132 std::vector<User *> Users(duplicateFunction->user_begin(),
133 duplicateFunction->user_end());
Owen Andersonca399022009-06-14 08:26:32 +0000134 for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
135 UI != UE; ++UI)
Chris Lattner60915142010-04-22 23:07:58 +0000136 if (CallInst *CI = dyn_cast<CallInst>(*UI))
137 InlineFunction(CI, IFI);
138 else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
139 InlineFunction(II, IFI);
Owen Andersonca399022009-06-14 08:26:32 +0000140
141 // Ditch the duplicate, since we're done with it, and rewrite all remaining
142 // users (function pointers, etc.) back to the original function.
143 duplicateFunction->replaceAllUsesWith(F);
144 duplicateFunction->eraseFromParent();
145
Owen Anderson63676842009-06-15 20:50:26 +0000146 ++NumPartialInlined;
147
Owen Andersonca399022009-06-14 08:26:32 +0000148 return extractedFunction;
149}
150
151bool PartialInliner::runOnModule(Module& M) {
152 std::vector<Function*> worklist;
153 worklist.reserve(M.size());
154 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
155 if (!FI->use_empty() && !FI->isDeclaration())
Dan Gohmanb7a9f2b2010-01-05 16:20:55 +0000156 worklist.push_back(&*FI);
Owen Andersonca399022009-06-14 08:26:32 +0000157
158 bool changed = false;
159 while (!worklist.empty()) {
160 Function* currFunc = worklist.back();
161 worklist.pop_back();
162
163 if (currFunc->use_empty()) continue;
164
165 bool recursive = false;
Stephen Hines36b56882014-04-23 16:57:46 -0700166 for (User *U : currFunc->users())
167 if (Instruction* I = dyn_cast<Instruction>(U))
Owen Andersonca399022009-06-14 08:26:32 +0000168 if (I->getParent()->getParent() == currFunc) {
169 recursive = true;
170 break;
171 }
172 if (recursive) continue;
173
174
175 if (Function* newFunc = unswitchFunction(currFunc)) {
176 worklist.push_back(newFunc);
177 changed = true;
178 }
179
180 }
181
182 return changed;
Duncan Sandsd5f50da2009-07-03 15:30:58 +0000183}