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" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 17 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Instructions.h" |
| 20 | #include "llvm/IR/Module.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/IPO.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 24 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "partialinlining" |
| 28 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 29 | STATISTIC(NumPartialInlined, "Number of functions partially inlined"); |
| 30 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 31 | namespace { |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 32 | struct PartialInlinerImpl { |
| 33 | PartialInlinerImpl(InlineFunctionInfo IFI) : IFI(IFI) {} |
| 34 | bool run(Module &M); |
| 35 | Function *unswitchFunction(Function *F); |
| 36 | |
| 37 | private: |
| 38 | InlineFunctionInfo IFI; |
| 39 | }; |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 40 | struct PartialInlinerLegacyPass : public ModulePass { |
| 41 | static char ID; // Pass identification, replacement for typeid |
| 42 | PartialInlinerLegacyPass() : ModulePass(ID) { |
| 43 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 44 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 45 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 46 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 47 | AU.addRequired<AssumptionCacheTracker>(); |
| 48 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 49 | bool runOnModule(Module &M) override { |
| 50 | if (skipModule(M)) |
| 51 | return false; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 52 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 53 | AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>(); |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 54 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 55 | [&ACT](Function &F) -> AssumptionCache & { |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 56 | return ACT->getAssumptionCache(F); |
| 57 | }; |
| 58 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
| 59 | return PartialInlinerImpl(IFI).run(M); |
| 60 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 61 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 62 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 63 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 64 | Function *PartialInlinerImpl::unswitchFunction(Function *F) { |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 65 | // First, verify that this function is an unswitching candidate... |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 66 | BasicBlock *EntryBlock = &F->front(); |
| 67 | BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator()); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 68 | if (!BR || BR->isUnconditional()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 69 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 70 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 71 | BasicBlock *ReturnBlock = nullptr; |
| 72 | BasicBlock *NonReturnBlock = nullptr; |
| 73 | unsigned ReturnCount = 0; |
| 74 | for (BasicBlock *BB : successors(EntryBlock)) { |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 75 | if (isa<ReturnInst>(BB->getTerminator())) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 76 | ReturnBlock = BB; |
| 77 | ReturnCount++; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 78 | } else |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 79 | NonReturnBlock = BB; |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 80 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 81 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 82 | if (ReturnCount != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 83 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 84 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 85 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 86 | ValueToValueMapTy VMap; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 87 | 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 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 | // 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 Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 95 | F->replaceAllUsesWith(DuplicateFunction); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 96 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 97 | // 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 Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 101 | 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 Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 110 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 111 | PHINode *RetPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); |
| 112 | OldPhi->replaceAllUsesWith(RetPhi); |
| 113 | Ins = NewReturnBlock->getFirstNonPHI(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 114 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 115 | RetPhi->addIncoming(&*I, PreReturn); |
| 116 | RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewEntryBlock), |
| 117 | NewEntryBlock); |
| 118 | OldPhi->removeIncomingValue(NewEntryBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 119 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 120 | ++I; |
| 121 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 122 | NewEntryBlock->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 123 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 124 | // Gather up the blocks that we're going to extract. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 125 | 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 Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 131 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 132 | // The CodeExtractor needs a dominator tree. |
| 133 | DominatorTree DT; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 134 | DT.recalculate(*DuplicateFunction); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 135 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 136 | // Extract the body of the if. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 137 | Function *ExtractedFunction = |
| 138 | CodeExtractor(ToExtract, &DT).extractCodeRegion(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 139 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 140 | // Inline the top-level if test into all callers. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 141 | std::vector<User *> Users(DuplicateFunction->user_begin(), |
| 142 | DuplicateFunction->user_end()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 143 | for (User *User : Users) |
| 144 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 145 | InlineFunction(CI, IFI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 146 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 147 | InlineFunction(II, IFI); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 148 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 149 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 150 | // users (function pointers, etc.) back to the original function. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 151 | DuplicateFunction->replaceAllUsesWith(F); |
| 152 | DuplicateFunction->eraseFromParent(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 153 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 154 | ++NumPartialInlined; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 155 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 156 | return ExtractedFunction; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 159 | bool PartialInlinerImpl::run(Module &M) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 160 | std::vector<Function *> Worklist; |
| 161 | Worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 162 | for (Function &F : M) |
| 163 | if (!F.use_empty() && !F.isDeclaration()) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 164 | Worklist.push_back(&F); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 165 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 166 | bool Changed = false; |
| 167 | while (!Worklist.empty()) { |
| 168 | Function *CurrFunc = Worklist.back(); |
| 169 | Worklist.pop_back(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 170 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 171 | if (CurrFunc->use_empty()) |
| 172 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 173 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 174 | 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 Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 179 | break; |
| 180 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 181 | if (Recursive) |
| 182 | continue; |
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 | if (Function *newFunc = unswitchFunction(CurrFunc)) { |
| 185 | Worklist.push_back(newFunc); |
| 186 | Changed = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 187 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 188 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 189 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 190 | return Changed; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | char PartialInlinerLegacyPass::ID = 0; |
| 194 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", |
| 195 | "Partial Inliner", false, false) |
| 196 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 197 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", |
| 198 | "Partial Inliner", false, false) |
| 199 | |
| 200 | ModulePass *llvm::createPartialInliningPass() { |
| 201 | return new PartialInlinerLegacyPass(); |
| 202 | } |
| 203 | |
| 204 | PreservedAnalyses PartialInlinerPass::run(Module &M, |
| 205 | ModuleAnalysisManager &AM) { |
| 206 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame^] | 207 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 208 | [&FAM](Function &F) -> AssumptionCache & { |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 209 | return FAM.getResult<AssumptionAnalysis>(F); |
| 210 | }; |
| 211 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
| 212 | if (PartialInlinerImpl(IFI).run(M)) |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 213 | return PreservedAnalyses::none(); |
| 214 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 215 | } |