blob: a2f6e5639d9d48a6859bf994e35e8acbd01c0827 [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...
Sean Silva519323d2016-07-25 05:57:59 +000069 BasicBlock *EntryBlock = &F->front();
70 BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator());
Owen Andersonf0081db2009-09-08 19:53:15 +000071 if (!BR || BR->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +000072 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000073
Sean Silva519323d2016-07-25 05:57:59 +000074 BasicBlock *ReturnBlock = nullptr;
75 BasicBlock *NonReturnBlock = nullptr;
76 unsigned ReturnCount = 0;
77 for (BasicBlock *BB : successors(EntryBlock)) {
Reid Klecknerc26a17a2015-02-04 19:14:57 +000078 if (isa<ReturnInst>(BB->getTerminator())) {
Sean Silva519323d2016-07-25 05:57:59 +000079 ReturnBlock = BB;
80 ReturnCount++;
Owen Anderson2f82e272009-06-14 08:26:32 +000081 } else
Sean Silva519323d2016-07-25 05:57:59 +000082 NonReturnBlock = BB;
Reid Klecknerc26a17a2015-02-04 19:14:57 +000083 }
Sean Silvafe5abd52016-07-25 05:00:00 +000084
Sean Silva519323d2016-07-25 05:57:59 +000085 if (ReturnCount != 1)
Craig Topperf40110f2014-04-25 05:29:35 +000086 return nullptr;
Sean Silvafe5abd52016-07-25 05:00:00 +000087
Owen Anderson2f82e272009-06-14 08:26:32 +000088 // Clone the function, so that we can hack away on it.
Rafael Espindola229e38f2010-10-13 01:36:30 +000089 ValueToValueMapTy VMap;
Sean Silva519323d2016-07-25 05:57:59 +000090 Function *DuplicateFunction = CloneFunction(F, VMap);
91 DuplicateFunction->setLinkage(GlobalValue::InternalLinkage);
92 BasicBlock *NewEntryBlock = cast<BasicBlock>(VMap[EntryBlock]);
93 BasicBlock *NewReturnBlock = cast<BasicBlock>(VMap[ReturnBlock]);
94 BasicBlock *NewNonReturnBlock = cast<BasicBlock>(VMap[NonReturnBlock]);
Sean Silvafe5abd52016-07-25 05:00:00 +000095
Owen Anderson2f82e272009-06-14 08:26:32 +000096 // Go ahead and update all uses to the duplicate, so that we can just
97 // use the inliner functionality when we're done hacking.
Sean Silva519323d2016-07-25 05:57:59 +000098 F->replaceAllUsesWith(DuplicateFunction);
Sean Silvafe5abd52016-07-25 05:00:00 +000099
Owen Anderson2f82e272009-06-14 08:26:32 +0000100 // Special hackery is needed with PHI nodes that have inputs from more than
101 // one extracted block. For simplicity, just split the PHIs into a two-level
102 // sequence of PHIs, some of which will go in the extracted region, and some
103 // of which will go outside.
Sean Silva519323d2016-07-25 05:57:59 +0000104 BasicBlock *PreReturn = NewReturnBlock;
105 NewReturnBlock = NewReturnBlock->splitBasicBlock(
106 NewReturnBlock->getFirstNonPHI()->getIterator());
107 BasicBlock::iterator I = PreReturn->begin();
108 Instruction *Ins = &NewReturnBlock->front();
109 while (I != PreReturn->end()) {
110 PHINode *OldPhi = dyn_cast<PHINode>(I);
111 if (!OldPhi)
112 break;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000113
Sean Silva519323d2016-07-25 05:57:59 +0000114 PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
115 OldPhi->replaceAllUsesWith(RetPhi);
116 Ins = NewReturnBlock->getFirstNonPHI();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000117
Sean Silva519323d2016-07-25 05:57:59 +0000118 RetPhi->addIncoming(&*I, PreReturn);
119 RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock),
120 NewEntryBlock);
121 OldPhi->removeIncomingValue(NewEntryBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000122
Owen Anderson2f82e272009-06-14 08:26:32 +0000123 ++I;
124 }
Sean Silva519323d2016-07-25 05:57:59 +0000125 NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock);
Sean Silvafe5abd52016-07-25 05:00:00 +0000126
Owen Anderson2f82e272009-06-14 08:26:32 +0000127 // Gather up the blocks that we're going to extract.
Sean Silva519323d2016-07-25 05:57:59 +0000128 std::vector<BasicBlock *> ToExtract;
129 ToExtract.push_back(NewNonReturnBlock);
130 for (BasicBlock &BB : *DuplicateFunction)
131 if (&BB != NewEntryBlock && &BB != NewReturnBlock &&
132 &BB != NewNonReturnBlock)
133 ToExtract.push_back(&BB);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000134
Owen Anderson2f82e272009-06-14 08:26:32 +0000135 // The CodeExtractor needs a dominator tree.
136 DominatorTree DT;
Sean Silva519323d2016-07-25 05:57:59 +0000137 DT.recalculate(*DuplicateFunction);
Chandler Carruth73523022014-01-13 13:07:17 +0000138
Sean Silvaf8015752016-08-02 02:15:45 +0000139 // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo.
140 LoopInfo LI(DT);
141 BranchProbabilityInfo BPI(*DuplicateFunction, LI);
142 BlockFrequencyInfo BFI(*DuplicateFunction, BPI, LI);
143
Dan Gohman4a618822010-02-10 16:03:48 +0000144 // Extract the body of the if.
Sean Silva519323d2016-07-25 05:57:59 +0000145 Function *ExtractedFunction =
Sean Silvaf8015752016-08-02 02:15:45 +0000146 CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, &BFI, &BPI)
147 .extractCodeRegion();
Sean Silvafe5abd52016-07-25 05:00:00 +0000148
Owen Anderson2f82e272009-06-14 08:26:32 +0000149 // Inline the top-level if test into all callers.
Sean Silva519323d2016-07-25 05:57:59 +0000150 std::vector<User *> Users(DuplicateFunction->user_begin(),
151 DuplicateFunction->user_end());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000152 for (User *User : Users)
153 if (CallInst *CI = dyn_cast<CallInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000154 InlineFunction(CI, IFI);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000155 else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000156 InlineFunction(II, IFI);
Sean Silvafe5abd52016-07-25 05:00:00 +0000157
Owen Anderson2f82e272009-06-14 08:26:32 +0000158 // Ditch the duplicate, since we're done with it, and rewrite all remaining
159 // users (function pointers, etc.) back to the original function.
Sean Silva519323d2016-07-25 05:57:59 +0000160 DuplicateFunction->replaceAllUsesWith(F);
161 DuplicateFunction->eraseFromParent();
Sean Silvafe5abd52016-07-25 05:00:00 +0000162
Owen Andersonbd6a2132009-06-15 20:50:26 +0000163 ++NumPartialInlined;
Sean Silvafe5abd52016-07-25 05:00:00 +0000164
Sean Silva519323d2016-07-25 05:57:59 +0000165 return ExtractedFunction;
Owen Anderson2f82e272009-06-14 08:26:32 +0000166}
167
Sean Silvafe5abd52016-07-25 05:00:00 +0000168bool PartialInlinerImpl::run(Module &M) {
Sean Silva519323d2016-07-25 05:57:59 +0000169 std::vector<Function *> Worklist;
170 Worklist.reserve(M.size());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000171 for (Function &F : M)
172 if (!F.use_empty() && !F.isDeclaration())
Sean Silva519323d2016-07-25 05:57:59 +0000173 Worklist.push_back(&F);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000174
Sean Silva519323d2016-07-25 05:57:59 +0000175 bool Changed = false;
176 while (!Worklist.empty()) {
177 Function *CurrFunc = Worklist.back();
178 Worklist.pop_back();
Sean Silvafe5abd52016-07-25 05:00:00 +0000179
Sean Silva519323d2016-07-25 05:57:59 +0000180 if (CurrFunc->use_empty())
181 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000182
Sean Silva519323d2016-07-25 05:57:59 +0000183 bool Recursive = false;
184 for (User *U : CurrFunc->users())
185 if (Instruction *I = dyn_cast<Instruction>(U))
186 if (I->getParent()->getParent() == CurrFunc) {
187 Recursive = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000188 break;
189 }
Sean Silva519323d2016-07-25 05:57:59 +0000190 if (Recursive)
191 continue;
Sean Silvafe5abd52016-07-25 05:00:00 +0000192
Sean Silvaf8015752016-08-02 02:15:45 +0000193 if (Function *NewFunc = unswitchFunction(CurrFunc)) {
194 Worklist.push_back(NewFunc);
Sean Silva519323d2016-07-25 05:57:59 +0000195 Changed = true;
Owen Anderson2f82e272009-06-14 08:26:32 +0000196 }
Owen Anderson2f82e272009-06-14 08:26:32 +0000197 }
Easwaran Raman1832bf62016-06-27 16:50:18 +0000198
Sean Silva519323d2016-07-25 05:57:59 +0000199 return Changed;
Sean Silvafe5abd52016-07-25 05:00:00 +0000200}
201
202char PartialInlinerLegacyPass::ID = 0;
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000203INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner",
204 "Partial Inliner", false, false)
205INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
206INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner",
207 "Partial Inliner", false, false)
Sean Silvafe5abd52016-07-25 05:00:00 +0000208
209ModulePass *llvm::createPartialInliningPass() {
210 return new PartialInlinerLegacyPass();
211}
212
213PreservedAnalyses PartialInlinerPass::run(Module &M,
214 ModuleAnalysisManager &AM) {
Daniel Jasperaec2fa32016-12-19 08:22:17 +0000215 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
216 std::function<AssumptionCache &(Function &)> GetAssumptionCache =
217 [&FAM](Function &F) -> AssumptionCache & {
218 return FAM.getResult<AssumptionAnalysis>(F);
219 };
220 InlineFunctionInfo IFI(nullptr, &GetAssumptionCache);
Sean Silva423c7142016-08-01 04:16:09 +0000221 if (PartialInlinerImpl(IFI).run(M))
Easwaran Raman1832bf62016-06-27 16:50:18 +0000222 return PreservedAnalyses::none();
223 return PreservedAnalyses::all();
Duncan Sands29c8efc2009-07-03 15:30:58 +0000224}