Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1 | //===- 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 Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/IPO/PartialInlining.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 18 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 19 | #include "llvm/Analysis/LoopInfo.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 20 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 21 | #include "llvm/IR/CFG.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 22 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Instructions.h" |
| 25 | #include "llvm/IR/Module.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 26 | #include "llvm/Pass.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/IPO.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 32 | #define DEBUG_TYPE "partial-inlining" |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 33 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 34 | STATISTIC(NumPartialInlined, "Number of functions partially inlined"); |
| 35 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 36 | namespace { |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 37 | struct PartialInlinerImpl { |
Benjamin Kramer | 061f4a5 | 2017-01-13 14:39:03 +0000 | [diff] [blame] | 38 | PartialInlinerImpl(InlineFunctionInfo IFI) : IFI(std::move(IFI)) {} |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 39 | bool run(Module &M); |
| 40 | Function *unswitchFunction(Function *F); |
| 41 | |
| 42 | private: |
| 43 | InlineFunctionInfo IFI; |
| 44 | }; |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 45 | struct PartialInlinerLegacyPass : public ModulePass { |
| 46 | static char ID; // Pass identification, replacement for typeid |
| 47 | PartialInlinerLegacyPass() : ModulePass(ID) { |
| 48 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 49 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 50 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 51 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 52 | AU.addRequired<AssumptionCacheTracker>(); |
| 53 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 54 | bool runOnModule(Module &M) override { |
| 55 | if (skipModule(M)) |
| 56 | return false; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 57 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 58 | AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>(); |
| 59 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 60 | [&ACT](Function &F) -> AssumptionCache & { |
| 61 | return ACT->getAssumptionCache(F); |
| 62 | }; |
| 63 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
Sean Silva | 423c714 | 2016-08-01 04:16:09 +0000 | [diff] [blame] | 64 | return PartialInlinerImpl(IFI).run(M); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 65 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 66 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 67 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 68 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 69 | Function *PartialInlinerImpl::unswitchFunction(Function *F) { |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 70 | // First, verify that this function is an unswitching candidate... |
Xinliang David Li | 016a82b | 2017-04-22 19:24:19 +0000 | [diff] [blame] | 71 | if (F->hasAddressTaken()) |
| 72 | return nullptr; |
| 73 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 74 | BasicBlock *EntryBlock = &F->front(); |
| 75 | BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator()); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 76 | if (!BR || BR->isUnconditional()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 77 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 78 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 79 | BasicBlock *ReturnBlock = nullptr; |
| 80 | BasicBlock *NonReturnBlock = nullptr; |
| 81 | unsigned ReturnCount = 0; |
| 82 | for (BasicBlock *BB : successors(EntryBlock)) { |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 83 | if (isa<ReturnInst>(BB->getTerminator())) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 84 | ReturnBlock = BB; |
| 85 | ReturnCount++; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 86 | } else |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 87 | NonReturnBlock = BB; |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 88 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 89 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 90 | if (ReturnCount != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 91 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 92 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 93 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 94 | ValueToValueMapTy VMap; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 95 | Function *DuplicateFunction = CloneFunction(F, VMap); |
| 96 | DuplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
| 97 | BasicBlock *NewEntryBlock = cast<BasicBlock>(VMap[EntryBlock]); |
| 98 | BasicBlock *NewReturnBlock = cast<BasicBlock>(VMap[ReturnBlock]); |
| 99 | BasicBlock *NewNonReturnBlock = cast<BasicBlock>(VMap[NonReturnBlock]); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 100 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 101 | // Go ahead and update all uses to the duplicate, so that we can just |
| 102 | // use the inliner functionality when we're done hacking. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 103 | F->replaceAllUsesWith(DuplicateFunction); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 104 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 105 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 106 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 107 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 108 | // of which will go outside. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 109 | BasicBlock *PreReturn = NewReturnBlock; |
| 110 | NewReturnBlock = NewReturnBlock->splitBasicBlock( |
| 111 | NewReturnBlock->getFirstNonPHI()->getIterator()); |
| 112 | BasicBlock::iterator I = PreReturn->begin(); |
| 113 | Instruction *Ins = &NewReturnBlock->front(); |
| 114 | while (I != PreReturn->end()) { |
| 115 | PHINode *OldPhi = dyn_cast<PHINode>(I); |
| 116 | if (!OldPhi) |
| 117 | break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 118 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 119 | PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); |
| 120 | OldPhi->replaceAllUsesWith(RetPhi); |
| 121 | Ins = NewReturnBlock->getFirstNonPHI(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 122 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 123 | RetPhi->addIncoming(&*I, PreReturn); |
| 124 | RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock), |
| 125 | NewEntryBlock); |
| 126 | OldPhi->removeIncomingValue(NewEntryBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 127 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 128 | ++I; |
| 129 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 130 | NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 131 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 132 | // Gather up the blocks that we're going to extract. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 133 | std::vector<BasicBlock *> ToExtract; |
| 134 | ToExtract.push_back(NewNonReturnBlock); |
| 135 | for (BasicBlock &BB : *DuplicateFunction) |
| 136 | if (&BB != NewEntryBlock && &BB != NewReturnBlock && |
| 137 | &BB != NewNonReturnBlock) |
| 138 | ToExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 139 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 140 | // The CodeExtractor needs a dominator tree. |
| 141 | DominatorTree DT; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 142 | DT.recalculate(*DuplicateFunction); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 143 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 144 | // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo. |
| 145 | LoopInfo LI(DT); |
| 146 | BranchProbabilityInfo BPI(*DuplicateFunction, LI); |
| 147 | BlockFrequencyInfo BFI(*DuplicateFunction, BPI, LI); |
| 148 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 149 | // Extract the body of the if. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 150 | Function *ExtractedFunction = |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 151 | CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, &BFI, &BPI) |
| 152 | .extractCodeRegion(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 153 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 154 | // Inline the top-level if test into all callers. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 155 | std::vector<User *> Users(DuplicateFunction->user_begin(), |
| 156 | DuplicateFunction->user_end()); |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 157 | |
| 158 | for (User *User : Users) { |
| 159 | CallSite CS; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 160 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 161 | CS = CallSite(CI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 162 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame^] | 163 | CS = CallSite(II); |
| 164 | else |
| 165 | llvm_unreachable("All uses must be calls"); |
| 166 | |
| 167 | OptimizationRemarkEmitter ORE(CS.getCaller()); |
| 168 | DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); |
| 169 | BasicBlock *Block = CS.getParent(); |
| 170 | ORE.emit(OptimizationRemark(DEBUG_TYPE, "PartiallyInlined", DLoc, Block) |
| 171 | << ore::NV("Callee", F) << " partially inlined into " |
| 172 | << ore::NV("Caller", CS.getCaller())); |
| 173 | |
| 174 | InlineFunction(CS, IFI); |
| 175 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 176 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 177 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 178 | // users (function pointers, etc.) back to the original function. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 179 | DuplicateFunction->replaceAllUsesWith(F); |
| 180 | DuplicateFunction->eraseFromParent(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 181 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 182 | ++NumPartialInlined; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 183 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 184 | return ExtractedFunction; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 187 | bool PartialInlinerImpl::run(Module &M) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 188 | std::vector<Function *> Worklist; |
| 189 | Worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 190 | for (Function &F : M) |
| 191 | if (!F.use_empty() && !F.isDeclaration()) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 192 | Worklist.push_back(&F); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 193 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 194 | bool Changed = false; |
| 195 | while (!Worklist.empty()) { |
| 196 | Function *CurrFunc = Worklist.back(); |
| 197 | Worklist.pop_back(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 198 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 199 | if (CurrFunc->use_empty()) |
| 200 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 201 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 202 | bool Recursive = false; |
| 203 | for (User *U : CurrFunc->users()) |
| 204 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 205 | if (I->getParent()->getParent() == CurrFunc) { |
| 206 | Recursive = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 207 | break; |
| 208 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 209 | if (Recursive) |
| 210 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 211 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 212 | if (Function *NewFunc = unswitchFunction(CurrFunc)) { |
| 213 | Worklist.push_back(NewFunc); |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 214 | Changed = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 215 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 216 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 217 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 218 | return Changed; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | char PartialInlinerLegacyPass::ID = 0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 222 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", |
| 223 | "Partial Inliner", false, false) |
| 224 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 225 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", |
| 226 | "Partial Inliner", false, false) |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 227 | |
| 228 | ModulePass *llvm::createPartialInliningPass() { |
| 229 | return new PartialInlinerLegacyPass(); |
| 230 | } |
| 231 | |
| 232 | PreservedAnalyses PartialInlinerPass::run(Module &M, |
| 233 | ModuleAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 234 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 235 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 236 | [&FAM](Function &F) -> AssumptionCache & { |
| 237 | return FAM.getResult<AssumptionAnalysis>(F); |
| 238 | }; |
| 239 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
Sean Silva | 423c714 | 2016-08-01 04:16:09 +0000 | [diff] [blame] | 240 | if (PartialInlinerImpl(IFI).run(M)) |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 241 | return PreservedAnalyses::none(); |
| 242 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 243 | } |