blob: f5e5775da5506d218b8841bd348eea5cc53a5b87 [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"
Sean Silvaf8015752016-08-02 02:15:45 +000017#include "llvm/Analysis/BlockFrequencyInfo.h"
18#include "llvm/Analysis/BranchProbabilityInfo.h"
19#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000020#include "llvm/IR/CFG.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Dominators.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/Instructions.h"
23#include "llvm/IR/Module.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000024#include "llvm/Pass.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000025#include "llvm/Transforms/IPO.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000026#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth0fde0012012-05-04 10:18:49 +000027#include "llvm/Transforms/Utils/CodeExtractor.h"
Owen Anderson2f82e272009-06-14 08:26:32 +000028using namespace llvm;
29
Chandler Carruth964daaa2014-04-22 02:55:47 +000030#define DEBUG_TYPE "partialinlining"
31
Owen Andersonbd6a2132009-06-15 20:50:26 +000032STATISTIC(NumPartialInlined, "Number of functions partially inlined");
33
Owen Anderson2f82e272009-06-14 08:26:32 +000034namespace {
Sean Silvafe5abd52016-07-25 05:00:00 +000035struct PartialInlinerImpl {
Benjamin Kramer061f4a52017-01-13 14:39:03 +000036 PartialInlinerImpl(InlineFunctionInfo IFI) : IFI(std::move(IFI)) {}
Sean Silvafe5abd52016-07-25 05:00:00 +000037 bool run(Module &M);
38 Function *unswitchFunction(Function *F);
39
40private:
41 InlineFunctionInfo IFI;
42};
Easwaran Raman1832bf62016-06-27 16:50:18 +000043struct PartialInlinerLegacyPass : public ModulePass {
44 static char ID; // Pass identification, replacement for typeid
45 PartialInlinerLegacyPass() : ModulePass(ID) {
46 initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
47 }
Craig Topper3e4c6972014-03-05 09:10:37 +000048
Daniel Jasperaec2fa32016-12-19 08:22:17 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.addRequired<AssumptionCacheTracker>();
51 }
Easwaran Raman1832bf62016-06-27 16:50:18 +000052 bool runOnModule(Module &M) override {
53 if (skipModule(M))
54 return false;
Craig Topper3e4c6972014-03-05 09:10:37 +000055
Daniel Jasperaec2fa32016-12-19 08:22:17 +000056 AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>();
57 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
58 [&ACT](Function &F) -> AssumptionCache & {
59 return ACT->getAssumptionCache(F);
60 };
61 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
Sean Silva423c7142016-08-01 04:16:09 +000062 return PartialInlinerImpl(IFI).run(M);
Sean Silvafe5abd52016-07-25 05:00:00 +000063 }
Sean Silva519323d2016-07-25 05:57:59 +000064};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000065}
Owen Anderson2f82e272009-06-14 08:26:32 +000066
Sean Silvafe5abd52016-07-25 05:00:00 +000067Function *PartialInlinerImpl::unswitchFunction(Function *F) {
Owen Anderson2f82e272009-06-14 08:26:32 +000068 // First, verify that this function is an unswitching candidate...
Xinliang David Li016a82b2017-04-22 19:24:19 +000069 if (F->hasAddressTaken())
70 return nullptr;
71
Sean Silva519323d2016-07-25 05:57:59 +000072 BasicBlock *EntryBlock = &F->front();
73 BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator());
Owen Andersonf0081db2009-09-08 19:53:15 +000074 if (!BR || BR->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +000075 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000076
Sean Silva519323d2016-07-25 05:57:59 +000077 BasicBlock *ReturnBlock = nullptr;
78 BasicBlock *NonReturnBlock = nullptr;
79 unsigned ReturnCount = 0;
80 for (BasicBlock *BB : successors(EntryBlock)) {
Reid Klecknerc26a17a2015-02-04 19:14:57 +000081 if (isa<ReturnInst>(BB->getTerminator())) {
Sean Silva519323d2016-07-25 05:57:59 +000082 ReturnBlock = BB;
83 ReturnCount++;
Owen Anderson2f82e272009-06-14 08:26:32 +000084 } else
Sean Silva519323d2016-07-25 05:57:59 +000085 NonReturnBlock = BB;
Reid Klecknerc26a17a2015-02-04 19:14:57 +000086 }
Sean Silvafe5abd52016-07-25 05:00:00 +000087
Sean Silva519323d2016-07-25 05:57:59 +000088 if (ReturnCount != 1)
Craig Topperf40110f2014-04-25 05:29:35 +000089 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000090
Owen Anderson2f82e272009-06-14 08:26:32 +000091 // Clone the function, so that we can hack away on it.
Rafael Espindola229e38f2010-10-13 01:36:30 +000092 ValueToValueMapTy VMap;
Sean Silva519323d2016-07-25 05:57:59 +000093 Function *DuplicateFunction = CloneFunction(F, VMap);
94 DuplicateFunction->setLinkage(GlobalValue::InternalLinkage);
95 BasicBlock *NewEntryBlock = cast<BasicBlock>(VMap[EntryBlock]);
96 BasicBlock *NewReturnBlock = cast<BasicBlock>(VMap[ReturnBlock]);
97 BasicBlock *NewNonReturnBlock = cast<BasicBlock>(VMap[NonReturnBlock]);
Sean Silvafe5abd52016-07-25 05:00:00 +000098
Owen Anderson2f82e272009-06-14 08:26:32 +000099 // Go ahead and update all uses to the duplicate, so that we can just
100 // use the inliner functionality when we're done hacking.
Sean Silva519323d2016-07-25 05:57:59 +0000101 F->replaceAllUsesWith(DuplicateFunction);
Sean Silvafe5abd52016-07-25 05:00:00 +0000102
Owen Anderson2f82e272009-06-14 08:26:32 +0000103 // Special hackery is needed with PHI nodes that have inputs from more than
104 // one extracted block. For simplicity, just split the PHIs into a two-level
105 // sequence of PHIs, some of which will go in the extracted region, and some
106 // of which will go outside.
Sean Silva519323d2016-07-25 05:57:59 +0000107 BasicBlock *PreReturn = NewReturnBlock;
108 NewReturnBlock = NewReturnBlock->splitBasicBlock(
109 NewReturnBlock->getFirstNonPHI()->getIterator());
110 BasicBlock::iterator I = PreReturn->begin();
111 Instruction *Ins = &NewReturnBlock->front();
112 while (I != PreReturn->end()) {
113 PHINode *OldPhi = dyn_cast<PHINode>(I);
114 if (!OldPhi)
115 break;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000116
Sean Silva519323d2016-07-25 05:57:59 +0000117 PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
118 OldPhi->replaceAllUsesWith(RetPhi);
119 Ins = NewReturnBlock->getFirstNonPHI();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000120
Sean Silva519323d2016-07-25 05:57:59 +0000121 RetPhi->addIncoming(&*I, PreReturn);
122 RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock),
123 NewEntryBlock);
124 OldPhi->removeIncomingValue(NewEntryBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000125
Owen Anderson2f82e272009-06-14 08:26:32 +0000126 ++I;
127 }
Sean Silva519323d2016-07-25 05:57:59 +0000128 NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000129
Owen Anderson2f82e272009-06-14 08:26:32 +0000130 // Gather up the blocks that we're going to extract.
Sean Silva519323d2016-07-25 05:57:59 +0000131 std::vector<BasicBlock *> ToExtract;
132 ToExtract.push_back(NewNonReturnBlock);
133 for (BasicBlock &BB : *DuplicateFunction)
134 if (&BB != NewEntryBlock && &BB != NewReturnBlock &&
135 &BB != NewNonReturnBlock)
136 ToExtract.push_back(&BB);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000137
Owen Anderson2f82e272009-06-14 08:26:32 +0000138 // The CodeExtractor needs a dominator tree.
139 DominatorTree DT;
Sean Silva519323d2016-07-25 05:57:59 +0000140 DT.recalculate(*DuplicateFunction);
Chandler Carruth73523022014-01-13 13:07:17 +0000141
Sean Silvaf8015752016-08-02 02:15:45 +0000142 // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo.
143 LoopInfo LI(DT);
144 BranchProbabilityInfo BPI(*DuplicateFunction, LI);
145 BlockFrequencyInfo BFI(*DuplicateFunction, BPI, LI);
146
Dan Gohman4a618822010-02-10 16:03:48 +0000147 // Extract the body of the if.
Sean Silva519323d2016-07-25 05:57:59 +0000148 Function *ExtractedFunction =
Sean Silvaf8015752016-08-02 02:15:45 +0000149 CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, &BFI, &BPI)
150 .extractCodeRegion();
Sean Silvafe5abd52016-07-25 05:00:00 +0000151
Owen Anderson2f82e272009-06-14 08:26:32 +0000152 // Inline the top-level if test into all callers.
Sean Silva519323d2016-07-25 05:57:59 +0000153 std::vector<User *> Users(DuplicateFunction->user_begin(),
154 DuplicateFunction->user_end());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000155 for (User *User : Users)
156 if (CallInst *CI = dyn_cast<CallInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000157 InlineFunction(CI, IFI);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000158 else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000159 InlineFunction(II, IFI);
Sean Silvafe5abd52016-07-25 05:00:00 +0000160
Owen Anderson2f82e272009-06-14 08:26:32 +0000161 // Ditch the duplicate, since we're done with it, and rewrite all remaining
162 // users (function pointers, etc.) back to the original function.
Sean Silva519323d2016-07-25 05:57:59 +0000163 DuplicateFunction->replaceAllUsesWith(F);
164 DuplicateFunction->eraseFromParent();
Sean Silvafe5abd52016-07-25 05:00:00 +0000165
Owen Andersonbd6a2132009-06-15 20:50:26 +0000166 ++NumPartialInlined;
Sean Silvafe5abd52016-07-25 05:00:00 +0000167
Sean Silva519323d2016-07-25 05:57:59 +0000168 return ExtractedFunction;
Owen Anderson2f82e272009-06-14 08:26:32 +0000169}
170
Sean Silvafe5abd52016-07-25 05:00:00 +0000171bool PartialInlinerImpl::run(Module &M) {
Sean Silva519323d2016-07-25 05:57:59 +0000172 std::vector<Function *> Worklist;
173 Worklist.reserve(M.size());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000174 for (Function &F : M)
175 if (!F.use_empty() && !F.isDeclaration())
Sean Silva519323d2016-07-25 05:57:59 +0000176 Worklist.push_back(&F);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000177
Sean Silva519323d2016-07-25 05:57:59 +0000178 bool Changed = false;
179 while (!Worklist.empty()) {
180 Function *CurrFunc = Worklist.back();
181 Worklist.pop_back();
Sean Silvafe5abd52016-07-25 05:00:00 +0000182
Sean Silva519323d2016-07-25 05:57:59 +0000183 if (CurrFunc->use_empty())
184 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000185
Sean Silva519323d2016-07-25 05:57:59 +0000186 bool Recursive = false;
187 for (User *U : CurrFunc->users())
188 if (Instruction *I = dyn_cast<Instruction>(U))
189 if (I->getParent()->getParent() == CurrFunc) {
190 Recursive = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000191 break;
192 }
Sean Silva519323d2016-07-25 05:57:59 +0000193 if (Recursive)
194 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000195
Sean Silvaf8015752016-08-02 02:15:45 +0000196 if (Function *NewFunc = unswitchFunction(CurrFunc)) {
197 Worklist.push_back(NewFunc);
Sean Silva519323d2016-07-25 05:57:59 +0000198 Changed = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000199 }
Owen Anderson2f82e272009-06-14 08:26:32 +0000200 }
Easwaran Raman1832bf62016-06-27 16:50:18 +0000201
Sean Silva519323d2016-07-25 05:57:59 +0000202 return Changed;
Sean Silvafe5abd52016-07-25 05:00:00 +0000203}
204
205char PartialInlinerLegacyPass::ID = 0;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000206INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner",
207 "Partial Inliner", false, false)
208INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
209INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner",
210 "Partial Inliner", false, false)
Sean Silvafe5abd52016-07-25 05:00:00 +0000211
212ModulePass *llvm::createPartialInliningPass() {
213 return new PartialInlinerLegacyPass();
214}
215
216PreservedAnalyses PartialInlinerPass::run(Module &M,
217 ModuleAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000218 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
219 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
220 [&FAM](Function &F) -> AssumptionCache & {
221 return FAM.getResult<AssumptionAnalysis>(F);
222 };
223 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
Sean Silva423c7142016-08-01 04:16:09 +0000224 if (PartialInlinerImpl(IFI).run(M))
Easwaran Raman1832bf62016-06-27 16:50:18 +0000225 return PreservedAnalyses::none();
226 return PreservedAnalyses::all();
Duncan Sands29c8efc2009-07-03 15:30:58 +0000227}