blob: 2aca64a593a7142780edc52de3bbd35b304a99b1 [file] [log] [blame]
Owen Anderson2f82e272009-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
Easwaran Raman1832bf62016-06-27 16:50:18 +000015#include "llvm/Transforms/IPO/PartialInlining.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000017#include "llvm/IR/CFG.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000018#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Module.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000021#include "llvm/Pass.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000022#include "llvm/Transforms/IPO.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000023#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth0fde0012012-05-04 10:18:49 +000024#include "llvm/Transforms/Utils/CodeExtractor.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000025using namespace llvm;
26
Chandler Carruth964daaa2014-04-22 02:55:47 +000027#define DEBUG_TYPE "partialinlining"
28
Owen Andersonbd6a2132009-06-15 20:50:26 +000029STATISTIC(NumPartialInlined, "Number of functions partially inlined");
30
Owen Anderson2f82e272009-06-14 08:26:32 +000031namespace {
Sean Silvafe5abd52016-07-25 05:00:00 +000032struct PartialInlinerImpl {
33 PartialInlinerImpl(InlineFunctionInfo IFI) : IFI(IFI) {}
34 bool run(Module &M);
35 Function *unswitchFunction(Function *F);
36
37private:
38 InlineFunctionInfo IFI;
39};
Easwaran Raman1832bf62016-06-27 16:50:18 +000040struct PartialInlinerLegacyPass : public ModulePass {
41 static char ID; // Pass identification, replacement for typeid
42 PartialInlinerLegacyPass() : ModulePass(ID) {
43 initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
44 }
Craig Topper3e4c6972014-03-05 09:10:37 +000045
Sean Silvafe5abd52016-07-25 05:00:00 +000046 void getAnalysisUsage(AnalysisUsage &AU) const override {
47 AU.addRequired<AssumptionCacheTracker>();
48 }
Easwaran Raman1832bf62016-06-27 16:50:18 +000049 bool runOnModule(Module &M) override {
50 if (skipModule(M))
51 return false;
Craig Topper3e4c6972014-03-05 09:10:37 +000052
Sean Silvafe5abd52016-07-25 05:00:00 +000053 AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>();
54 std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&ACT](
55 Function &F) -> AssumptionCache & {
56 return ACT->getAssumptionCache(F);
57 };
58 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
59 return PartialInlinerImpl(IFI).run(M);
60 }
Owen Anderson2f82e272009-06-14 08:26:32 +000061 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000062}
Owen Anderson2f82e272009-06-14 08:26:32 +000063
Sean Silvafe5abd52016-07-25 05:00:00 +000064Function *PartialInlinerImpl::unswitchFunction(Function *F) {
Owen Anderson2f82e272009-06-14 08:26:32 +000065 // First, verify that this function is an unswitching candidate...
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000066 BasicBlock *entryBlock = &F->front();
Owen Andersonf0081db2009-09-08 19:53:15 +000067 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
68 if (!BR || BR->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +000069 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000070
Craig Topperf40110f2014-04-25 05:29:35 +000071 BasicBlock* returnBlock = nullptr;
72 BasicBlock* nonReturnBlock = nullptr;
Owen Anderson2f82e272009-06-14 08:26:32 +000073 unsigned returnCount = 0;
Reid Klecknerc26a17a2015-02-04 19:14:57 +000074 for (BasicBlock *BB : successors(entryBlock)) {
75 if (isa<ReturnInst>(BB->getTerminator())) {
76 returnBlock = BB;
Owen Anderson2f82e272009-06-14 08:26:32 +000077 returnCount++;
78 } else
Reid Klecknerc26a17a2015-02-04 19:14:57 +000079 nonReturnBlock = BB;
80 }
Sean Silvafe5abd52016-07-25 05:00:00 +000081
Owen Anderson2f82e272009-06-14 08:26:32 +000082 if (returnCount != 1)
Craig Topperf40110f2014-04-25 05:29:35 +000083 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000084
Owen Anderson2f82e272009-06-14 08:26:32 +000085 // Clone the function, so that we can hack away on it.
Rafael Espindola229e38f2010-10-13 01:36:30 +000086 ValueToValueMapTy VMap;
Peter Collingbournedba99562016-05-10 20:23:24 +000087 Function* duplicateFunction = CloneFunction(F, VMap);
Owen Anderson2f82e272009-06-14 08:26:32 +000088 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
Devang Patel0dc3c2d2010-06-24 00:33:28 +000089 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
90 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
91 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
Sean Silvafe5abd52016-07-25 05:00:00 +000092
Owen Anderson2f82e272009-06-14 08:26:32 +000093 // Go ahead and update all uses to the duplicate, so that we can just
94 // use the inliner functionality when we're done hacking.
95 F->replaceAllUsesWith(duplicateFunction);
Sean Silvafe5abd52016-07-25 05:00:00 +000096
Owen Anderson2f82e272009-06-14 08:26:32 +000097 // Special hackery is needed with PHI nodes that have inputs from more than
98 // one extracted block. For simplicity, just split the PHIs into a two-level
99 // sequence of PHIs, some of which will go in the extracted region, and some
100 // of which will go outside.
101 BasicBlock* preReturn = newReturnBlock;
102 newReturnBlock = newReturnBlock->splitBasicBlock(
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000103 newReturnBlock->getFirstNonPHI()->getIterator());
Owen Anderson2f82e272009-06-14 08:26:32 +0000104 BasicBlock::iterator I = preReturn->begin();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000105 Instruction *Ins = &newReturnBlock->front();
Owen Anderson2f82e272009-06-14 08:26:32 +0000106 while (I != preReturn->end()) {
107 PHINode* OldPhi = dyn_cast<PHINode>(I);
108 if (!OldPhi) break;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000109
110 PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
Owen Anderson2f82e272009-06-14 08:26:32 +0000111 OldPhi->replaceAllUsesWith(retPhi);
112 Ins = newReturnBlock->getFirstNonPHI();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000113
114 retPhi->addIncoming(&*I, preReturn);
Owen Anderson2f82e272009-06-14 08:26:32 +0000115 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
116 newEntryBlock);
117 OldPhi->removeIncomingValue(newEntryBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000118
Owen Anderson2f82e272009-06-14 08:26:32 +0000119 ++I;
120 }
121 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000122
Owen Anderson2f82e272009-06-14 08:26:32 +0000123 // Gather up the blocks that we're going to extract.
124 std::vector<BasicBlock*> toExtract;
125 toExtract.push_back(newNonReturnBlock);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000126 for (BasicBlock &BB : *duplicateFunction)
127 if (&BB != newEntryBlock && &BB != newReturnBlock &&
128 &BB != newNonReturnBlock)
129 toExtract.push_back(&BB);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000130
Owen Anderson2f82e272009-06-14 08:26:32 +0000131 // The CodeExtractor needs a dominator tree.
132 DominatorTree DT;
Chandler Carruth73523022014-01-13 13:07:17 +0000133 DT.recalculate(*duplicateFunction);
134
Dan Gohman4a618822010-02-10 16:03:48 +0000135 // Extract the body of the if.
Chandler Carruth0fde0012012-05-04 10:18:49 +0000136 Function* extractedFunction
137 = CodeExtractor(toExtract, &DT).extractCodeRegion();
Sean Silvafe5abd52016-07-25 05:00:00 +0000138
Owen Anderson2f82e272009-06-14 08:26:32 +0000139 // Inline the top-level if test into all callers.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000140 std::vector<User *> Users(duplicateFunction->user_begin(),
141 duplicateFunction->user_end());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000142 for (User *User : Users)
143 if (CallInst *CI = dyn_cast<CallInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000144 InlineFunction(CI, IFI);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000145 else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000146 InlineFunction(II, IFI);
Sean Silvafe5abd52016-07-25 05:00:00 +0000147
Owen Anderson2f82e272009-06-14 08:26:32 +0000148 // Ditch the duplicate, since we're done with it, and rewrite all remaining
149 // users (function pointers, etc.) back to the original function.
150 duplicateFunction->replaceAllUsesWith(F);
151 duplicateFunction->eraseFromParent();
Sean Silvafe5abd52016-07-25 05:00:00 +0000152
Owen Andersonbd6a2132009-06-15 20:50:26 +0000153 ++NumPartialInlined;
Sean Silvafe5abd52016-07-25 05:00:00 +0000154
Owen Anderson2f82e272009-06-14 08:26:32 +0000155 return extractedFunction;
156}
157
Sean Silvafe5abd52016-07-25 05:00:00 +0000158bool PartialInlinerImpl::run(Module &M) {
Owen Anderson2f82e272009-06-14 08:26:32 +0000159 std::vector<Function*> worklist;
160 worklist.reserve(M.size());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000161 for (Function &F : M)
162 if (!F.use_empty() && !F.isDeclaration())
163 worklist.push_back(&F);
164
Owen Anderson2f82e272009-06-14 08:26:32 +0000165 bool changed = false;
166 while (!worklist.empty()) {
167 Function* currFunc = worklist.back();
168 worklist.pop_back();
Sean Silvafe5abd52016-07-25 05:00:00 +0000169
Owen Anderson2f82e272009-06-14 08:26:32 +0000170 if (currFunc->use_empty()) continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000171
Owen Anderson2f82e272009-06-14 08:26:32 +0000172 bool recursive = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000173 for (User *U : currFunc->users())
174 if (Instruction* I = dyn_cast<Instruction>(U))
Owen Anderson2f82e272009-06-14 08:26:32 +0000175 if (I->getParent()->getParent() == currFunc) {
176 recursive = true;
177 break;
178 }
179 if (recursive) continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000180
Owen Anderson2f82e272009-06-14 08:26:32 +0000181 if (Function* newFunc = unswitchFunction(currFunc)) {
182 worklist.push_back(newFunc);
183 changed = true;
184 }
Owen Anderson2f82e272009-06-14 08:26:32 +0000185 }
Easwaran Raman1832bf62016-06-27 16:50:18 +0000186
Sean Silvafe5abd52016-07-25 05:00:00 +0000187 return changed;
188}
189
190char PartialInlinerLegacyPass::ID = 0;
191INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner",
192 "Partial Inliner", false, false)
193INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
194INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner",
195 "Partial Inliner", false, false)
196
197ModulePass *llvm::createPartialInliningPass() {
198 return new PartialInlinerLegacyPass();
199}
200
201PreservedAnalyses PartialInlinerPass::run(Module &M,
202 ModuleAnalysisManager &AM) {
203 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
204 std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&FAM](
205 Function &F) -> AssumptionCache & {
206 return FAM.getResult<AssumptionAnalysis>(F);
207 };
208 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
209 if (PartialInlinerImpl(IFI).run(M))
Easwaran Raman1832bf62016-06-27 16:50:18 +0000210 return PreservedAnalyses::none();
211 return PreservedAnalyses::all();
Duncan Sands29c8efc2009-07-03 15:30:58 +0000212}