blob: 49c44173491ec30f474be1a2a54b454739227e4a [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 {
Easwaran Raman1832bf62016-06-27 16:50:18 +000032struct PartialInlinerLegacyPass : public ModulePass {
33 static char ID; // Pass identification, replacement for typeid
34 PartialInlinerLegacyPass() : ModulePass(ID) {
35 initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry());
36 }
Craig Topper3e4c6972014-03-05 09:10:37 +000037
Easwaran Raman1832bf62016-06-27 16:50:18 +000038 bool runOnModule(Module &M) override {
39 if (skipModule(M))
40 return false;
41 ModuleAnalysisManager DummyMAM;
42 auto PA = Impl.run(M, DummyMAM);
43 return !PA.areAllPreserved();
44 }
Craig Topper3e4c6972014-03-05 09:10:37 +000045
Easwaran Raman1832bf62016-06-27 16:50:18 +000046private:
47 PartialInlinerPass Impl;
Owen Anderson2f82e272009-06-14 08:26:32 +000048 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000049}
Owen Anderson2f82e272009-06-14 08:26:32 +000050
Easwaran Raman1832bf62016-06-27 16:50:18 +000051char PartialInlinerLegacyPass::ID = 0;
52INITIALIZE_PASS(PartialInlinerLegacyPass, "partial-inliner", "Partial Inliner",
53 false, false)
Owen Anderson2f82e272009-06-14 08:26:32 +000054
Easwaran Raman1832bf62016-06-27 16:50:18 +000055ModulePass *llvm::createPartialInliningPass() {
56 return new PartialInlinerLegacyPass();
57}
Owen Anderson2f82e272009-06-14 08:26:32 +000058
Easwaran Raman1832bf62016-06-27 16:50:18 +000059Function *PartialInlinerPass::unswitchFunction(Function *F) {
Owen Anderson2f82e272009-06-14 08:26:32 +000060 // First, verify that this function is an unswitching candidate...
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000061 BasicBlock *entryBlock = &F->front();
Owen Andersonf0081db2009-09-08 19:53:15 +000062 BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
63 if (!BR || BR->isUnconditional())
Craig Topperf40110f2014-04-25 05:29:35 +000064 return nullptr;
Owen Anderson2f82e272009-06-14 08:26:32 +000065
Craig Topperf40110f2014-04-25 05:29:35 +000066 BasicBlock* returnBlock = nullptr;
67 BasicBlock* nonReturnBlock = nullptr;
Owen Anderson2f82e272009-06-14 08:26:32 +000068 unsigned returnCount = 0;
Reid Klecknerc26a17a2015-02-04 19:14:57 +000069 for (BasicBlock *BB : successors(entryBlock)) {
70 if (isa<ReturnInst>(BB->getTerminator())) {
71 returnBlock = BB;
Owen Anderson2f82e272009-06-14 08:26:32 +000072 returnCount++;
73 } else
Reid Klecknerc26a17a2015-02-04 19:14:57 +000074 nonReturnBlock = BB;
75 }
Owen Anderson2f82e272009-06-14 08:26:32 +000076
77 if (returnCount != 1)
Craig Topperf40110f2014-04-25 05:29:35 +000078 return nullptr;
Owen Anderson2f82e272009-06-14 08:26:32 +000079
80 // Clone the function, so that we can hack away on it.
Rafael Espindola229e38f2010-10-13 01:36:30 +000081 ValueToValueMapTy VMap;
Peter Collingbournedba99562016-05-10 20:23:24 +000082 Function* duplicateFunction = CloneFunction(F, VMap);
Owen Anderson2f82e272009-06-14 08:26:32 +000083 duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
Devang Patel0dc3c2d2010-06-24 00:33:28 +000084 BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
85 BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
86 BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
Owen Anderson2f82e272009-06-14 08:26:32 +000087
88 // Go ahead and update all uses to the duplicate, so that we can just
89 // use the inliner functionality when we're done hacking.
90 F->replaceAllUsesWith(duplicateFunction);
91
92 // Special hackery is needed with PHI nodes that have inputs from more than
93 // one extracted block. For simplicity, just split the PHIs into a two-level
94 // sequence of PHIs, some of which will go in the extracted region, and some
95 // of which will go outside.
96 BasicBlock* preReturn = newReturnBlock;
97 newReturnBlock = newReturnBlock->splitBasicBlock(
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +000098 newReturnBlock->getFirstNonPHI()->getIterator());
Owen Anderson2f82e272009-06-14 08:26:32 +000099 BasicBlock::iterator I = preReturn->begin();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000100 Instruction *Ins = &newReturnBlock->front();
Owen Anderson2f82e272009-06-14 08:26:32 +0000101 while (I != preReturn->end()) {
102 PHINode* OldPhi = dyn_cast<PHINode>(I);
103 if (!OldPhi) break;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000104
105 PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
Owen Anderson2f82e272009-06-14 08:26:32 +0000106 OldPhi->replaceAllUsesWith(retPhi);
107 Ins = newReturnBlock->getFirstNonPHI();
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000108
109 retPhi->addIncoming(&*I, preReturn);
Owen Anderson2f82e272009-06-14 08:26:32 +0000110 retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
111 newEntryBlock);
112 OldPhi->removeIncomingValue(newEntryBlock);
113
114 ++I;
115 }
116 newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
117
118 // Gather up the blocks that we're going to extract.
119 std::vector<BasicBlock*> toExtract;
120 toExtract.push_back(newNonReturnBlock);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000121 for (BasicBlock &BB : *duplicateFunction)
122 if (&BB != newEntryBlock && &BB != newReturnBlock &&
123 &BB != newNonReturnBlock)
124 toExtract.push_back(&BB);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000125
Owen Anderson2f82e272009-06-14 08:26:32 +0000126 // The CodeExtractor needs a dominator tree.
127 DominatorTree DT;
Chandler Carruth73523022014-01-13 13:07:17 +0000128 DT.recalculate(*duplicateFunction);
129
Dan Gohman4a618822010-02-10 16:03:48 +0000130 // Extract the body of the if.
Chandler Carruth0fde0012012-05-04 10:18:49 +0000131 Function* extractedFunction
132 = CodeExtractor(toExtract, &DT).extractCodeRegion();
Owen Anderson2f82e272009-06-14 08:26:32 +0000133
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000134 InlineFunctionInfo IFI;
135
Owen Anderson2f82e272009-06-14 08:26:32 +0000136 // Inline the top-level if test into all callers.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000137 std::vector<User *> Users(duplicateFunction->user_begin(),
138 duplicateFunction->user_end());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000139 for (User *User : Users)
140 if (CallInst *CI = dyn_cast<CallInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000141 InlineFunction(CI, IFI);
Benjamin Kramer135f7352016-06-26 12:28:59 +0000142 else if (InvokeInst *II = dyn_cast<InvokeInst>(User))
Chris Lattner4ba01ec2010-04-22 23:07:58 +0000143 InlineFunction(II, IFI);
Owen Anderson2f82e272009-06-14 08:26:32 +0000144
145 // Ditch the duplicate, since we're done with it, and rewrite all remaining
146 // users (function pointers, etc.) back to the original function.
147 duplicateFunction->replaceAllUsesWith(F);
148 duplicateFunction->eraseFromParent();
149
Owen Andersonbd6a2132009-06-15 20:50:26 +0000150 ++NumPartialInlined;
151
Owen Anderson2f82e272009-06-14 08:26:32 +0000152 return extractedFunction;
153}
154
Easwaran Raman1832bf62016-06-27 16:50:18 +0000155PreservedAnalyses PartialInlinerPass::run(Module &M, ModuleAnalysisManager &) {
Owen Anderson2f82e272009-06-14 08:26:32 +0000156 std::vector<Function*> worklist;
157 worklist.reserve(M.size());
Benjamin Kramer135f7352016-06-26 12:28:59 +0000158 for (Function &F : M)
159 if (!F.use_empty() && !F.isDeclaration())
160 worklist.push_back(&F);
161
Owen Anderson2f82e272009-06-14 08:26:32 +0000162 bool changed = false;
163 while (!worklist.empty()) {
164 Function* currFunc = worklist.back();
165 worklist.pop_back();
166
167 if (currFunc->use_empty()) continue;
168
169 bool recursive = false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000170 for (User *U : currFunc->users())
171 if (Instruction* I = dyn_cast<Instruction>(U))
Owen Anderson2f82e272009-06-14 08:26:32 +0000172 if (I->getParent()->getParent() == currFunc) {
173 recursive = true;
174 break;
175 }
176 if (recursive) continue;
177
178
179 if (Function* newFunc = unswitchFunction(currFunc)) {
180 worklist.push_back(newFunc);
181 changed = true;
182 }
183
184 }
Easwaran Raman1832bf62016-06-27 16:50:18 +0000185
186 if (changed)
187 return PreservedAnalyses::none();
188 return PreservedAnalyses::all();
Duncan Sands29c8efc2009-07-03 15:30:58 +0000189}