blob: 6c762e47f46bf113f09cf74bece0184805b559ae [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>();
Sean Silva519323d2016-07-25 05:57:59 +000054 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
55 [&ACT](Function &F) -> AssumptionCache & {
Sean Silvafe5abd52016-07-25 05:00:00 +000056 return ACT->getAssumptionCache(F);
57 };
58 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
59 return PartialInlinerImpl(IFI).run(M);
60 }
Sean Silva519323d2016-07-25 05:57:59 +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...
Sean Silva519323d2016-07-25 05:57:59 +000066 BasicBlock *EntryBlock = &F->front();
67 BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator());
Owen Andersonf0081db2009-09-08 19:53:15 +000068 if (!BR || BR->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +000069 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000070
Sean Silva519323d2016-07-25 05:57:59 +000071 BasicBlock *ReturnBlock = nullptr;
72 BasicBlock *NonReturnBlock = nullptr;
73 unsigned ReturnCount = 0;
74 for (BasicBlock *BB : successors(EntryBlock)) {
Reid Klecknerc26a17a2015-02-04 19:14:57 +000075 if (isa<ReturnInst>(BB->getTerminator())) {
Sean Silva519323d2016-07-25 05:57:59 +000076 ReturnBlock = BB;
77 ReturnCount++;
Owen Anderson2f82e272009-06-14 08:26:32 +000078 } else
Sean Silva519323d2016-07-25 05:57:59 +000079 NonReturnBlock = BB;
Reid Klecknerc26a17a2015-02-04 19:14:57 +000080 }
Sean Silvafe5abd52016-07-25 05:00:00 +000081
Sean Silva519323d2016-07-25 05:57:59 +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;
Sean Silva519323d2016-07-25 05:57:59 +000087 Function *DuplicateFunction = CloneFunction(F, VMap);
88 DuplicateFunction->setLinkage(GlobalValue::InternalLinkage);
89 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.
Sean Silva519323d2016-07-25 05:57:59 +000095 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.
Sean Silva519323d2016-07-25 05:57:59 +0000101 BasicBlock *PreReturn = NewReturnBlock;
102 NewReturnBlock = NewReturnBlock->splitBasicBlock(
103 NewReturnBlock->getFirstNonPHI()->getIterator());
104 BasicBlock::iterator I = PreReturn->begin();
105 Instruction *Ins = &NewReturnBlock->front();
106 while (I != PreReturn->end()) {
107 PHINode *OldPhi = dyn_cast<PHINode>(I);
108 if (!OldPhi)
109 break;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000110
Sean Silva519323d2016-07-25 05:57:59 +0000111 PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
112 OldPhi->replaceAllUsesWith(RetPhi);
113 Ins = NewReturnBlock->getFirstNonPHI();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000114
Sean Silva519323d2016-07-25 05:57:59 +0000115 RetPhi->addIncoming(&*I, PreReturn);
116 RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock),
117 NewEntryBlock);
118 OldPhi->removeIncomingValue(NewEntryBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000119
Owen Anderson2f82e272009-06-14 08:26:32 +0000120 ++I;
121 }
Sean Silva519323d2016-07-25 05:57:59 +0000122 NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000123
Owen Anderson2f82e272009-06-14 08:26:32 +0000124 // Gather up the blocks that we're going to extract.
Sean Silva519323d2016-07-25 05:57:59 +0000125 std::vector<BasicBlock *> ToExtract;
126 ToExtract.push_back(NewNonReturnBlock);
127 for (BasicBlock &BB : *DuplicateFunction)
128 if (&BB != NewEntryBlock && &BB != NewReturnBlock &&
129 &BB != NewNonReturnBlock)
130 ToExtract.push_back(&BB);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000131
Owen Anderson2f82e272009-06-14 08:26:32 +0000132 // The CodeExtractor needs a dominator tree.
133 DominatorTree DT;
Sean Silva519323d2016-07-25 05:57:59 +0000134 DT.recalculate(*DuplicateFunction);
Chandler Carruth73523022014-01-13 13:07:17 +0000135
Dan Gohman4a618822010-02-10 16:03:48 +0000136 // Extract the body of the if.
Sean Silva519323d2016-07-25 05:57:59 +0000137 Function *ExtractedFunction =
138 CodeExtractor(ToExtract, &DT).extractCodeRegion();
Sean Silvafe5abd52016-07-25 05:00:00 +0000139
Owen Anderson2f82e272009-06-14 08:26:32 +0000140 // Inline the top-level if test into all callers.
Sean Silva519323d2016-07-25 05:57:59 +0000141 std::vector<User *> Users(DuplicateFunction->user_begin(),
142 DuplicateFunction->user_end());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000143 for (User *User : Users)
144 if (CallInst *CI = dyn_cast<CallInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000145 InlineFunction(CI, IFI);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000146 else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000147 InlineFunction(II, IFI);
Sean Silvafe5abd52016-07-25 05:00:00 +0000148
Owen Anderson2f82e272009-06-14 08:26:32 +0000149 // Ditch the duplicate, since we're done with it, and rewrite all remaining
150 // users (function pointers, etc.) back to the original function.
Sean Silva519323d2016-07-25 05:57:59 +0000151 DuplicateFunction->replaceAllUsesWith(F);
152 DuplicateFunction->eraseFromParent();
Sean Silvafe5abd52016-07-25 05:00:00 +0000153
Owen Andersonbd6a2132009-06-15 20:50:26 +0000154 ++NumPartialInlined;
Sean Silvafe5abd52016-07-25 05:00:00 +0000155
Sean Silva519323d2016-07-25 05:57:59 +0000156 return ExtractedFunction;
Owen Anderson2f82e272009-06-14 08:26:32 +0000157}
158
Sean Silvafe5abd52016-07-25 05:00:00 +0000159bool PartialInlinerImpl::run(Module &M) {
Sean Silva519323d2016-07-25 05:57:59 +0000160 std::vector<Function *> Worklist;
161 Worklist.reserve(M.size());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000162 for (Function &F : M)
163 if (!F.use_empty() && !F.isDeclaration())
Sean Silva519323d2016-07-25 05:57:59 +0000164 Worklist.push_back(&F);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000165
Sean Silva519323d2016-07-25 05:57:59 +0000166 bool Changed = false;
167 while (!Worklist.empty()) {
168 Function *CurrFunc = Worklist.back();
169 Worklist.pop_back();
Sean Silvafe5abd52016-07-25 05:00:00 +0000170
Sean Silva519323d2016-07-25 05:57:59 +0000171 if (CurrFunc->use_empty())
172 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000173
Sean Silva519323d2016-07-25 05:57:59 +0000174 bool Recursive = false;
175 for (User *U : CurrFunc->users())
176 if (Instruction *I = dyn_cast<Instruction>(U))
177 if (I->getParent()->getParent() == CurrFunc) {
178 Recursive = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000179 break;
180 }
Sean Silva519323d2016-07-25 05:57:59 +0000181 if (Recursive)
182 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000183
Sean Silva519323d2016-07-25 05:57:59 +0000184 if (Function *newFunc = unswitchFunction(CurrFunc)) {
185 Worklist.push_back(newFunc);
186 Changed = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000187 }
Owen Anderson2f82e272009-06-14 08:26:32 +0000188 }
Easwaran Raman1832bf62016-06-27 16:50:18 +0000189
Sean Silva519323d2016-07-25 05:57:59 +0000190 return Changed;
Sean Silvafe5abd52016-07-25 05:00:00 +0000191}
192
193char PartialInlinerLegacyPass::ID = 0;
194INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner",
195 "Partial Inliner", false, false)
196INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
197INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner",
198 "Partial Inliner", false, false)
199
200ModulePass *llvm::createPartialInliningPass() {
201 return new PartialInlinerLegacyPass();
202}
203
204PreservedAnalyses PartialInlinerPass::run(Module &M,
205 ModuleAnalysisManager &AM) {
206 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
Sean Silva519323d2016-07-25 05:57:59 +0000207 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
208 [&FAM](Function &F) -> AssumptionCache & {
Sean Silvafe5abd52016-07-25 05:00:00 +0000209 return FAM.getResult<AssumptionAnalysis>(F);
210 };
211 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
212 if (PartialInlinerImpl(IFI).run(M))
Easwaran Raman1832bf62016-06-27 16:50:18 +0000213 return PreservedAnalyses::none();
214 return PreservedAnalyses::all();
Duncan Sands29c8efc2009-07-03 15:30:58 +0000215}