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>(); |
| 54 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&ACT]( |
| 55 | Function &F) -> AssumptionCache & { |
| 56 | return ACT->getAssumptionCache(F); |
| 57 | }; |
| 58 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
| 59 | return PartialInlinerImpl(IFI).run(M); |
| 60 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +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... |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 66 | BasicBlock *entryBlock = &F->front(); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 67 | BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); |
| 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 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 71 | BasicBlock* returnBlock = nullptr; |
| 72 | BasicBlock* nonReturnBlock = nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 73 | unsigned returnCount = 0; |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 74 | for (BasicBlock *BB : successors(entryBlock)) { |
| 75 | if (isa<ReturnInst>(BB->getTerminator())) { |
| 76 | returnBlock = BB; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 77 | returnCount++; |
| 78 | } else |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 79 | nonReturnBlock = BB; |
| 80 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 81 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +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; |
Peter Collingbourne | dba9956 | 2016-05-10 20:23:24 +0000 | [diff] [blame] | 87 | Function* duplicateFunction = CloneFunction(F, VMap); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 88 | duplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 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. |
| 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. |
| 101 | BasicBlock* preReturn = newReturnBlock; |
| 102 | newReturnBlock = newReturnBlock->splitBasicBlock( |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 103 | newReturnBlock->getFirstNonPHI()->getIterator()); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 104 | BasicBlock::iterator I = preReturn->begin(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 105 | Instruction *Ins = &newReturnBlock->front(); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 106 | while (I != preReturn->end()) { |
| 107 | PHINode* OldPhi = dyn_cast<PHINode>(I); |
| 108 | if (!OldPhi) break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 109 | |
| 110 | PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 111 | OldPhi->replaceAllUsesWith(retPhi); |
| 112 | Ins = newReturnBlock->getFirstNonPHI(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 113 | |
| 114 | retPhi->addIncoming(&*I, preReturn); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 115 | retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock), |
| 116 | newEntryBlock); |
| 117 | OldPhi->removeIncomingValue(newEntryBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 118 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 119 | ++I; |
| 120 | } |
| 121 | newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 122 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 123 | // Gather up the blocks that we're going to extract. |
| 124 | std::vector<BasicBlock*> toExtract; |
| 125 | toExtract.push_back(newNonReturnBlock); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 126 | for (BasicBlock &BB : *duplicateFunction) |
| 127 | if (&BB != newEntryBlock && &BB != newReturnBlock && |
| 128 | &BB != newNonReturnBlock) |
| 129 | toExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 130 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 131 | // The CodeExtractor needs a dominator tree. |
| 132 | DominatorTree DT; |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 133 | DT.recalculate(*duplicateFunction); |
| 134 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 135 | // Extract the body of the if. |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 136 | Function* extractedFunction |
| 137 | = CodeExtractor(toExtract, &DT).extractCodeRegion(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 138 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 139 | // Inline the top-level if test into all callers. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 140 | std::vector<User *> Users(duplicateFunction->user_begin(), |
| 141 | duplicateFunction->user_end()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 142 | for (User *User : Users) |
| 143 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 144 | InlineFunction(CI, IFI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 145 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 146 | InlineFunction(II, IFI); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 147 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 148 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 149 | // users (function pointers, etc.) back to the original function. |
| 150 | duplicateFunction->replaceAllUsesWith(F); |
| 151 | duplicateFunction->eraseFromParent(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 152 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 153 | ++NumPartialInlined; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 154 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 155 | return extractedFunction; |
| 156 | } |
| 157 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 158 | bool PartialInlinerImpl::run(Module &M) { |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 159 | std::vector<Function*> worklist; |
| 160 | worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 161 | for (Function &F : M) |
| 162 | if (!F.use_empty() && !F.isDeclaration()) |
| 163 | worklist.push_back(&F); |
| 164 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 165 | bool changed = false; |
| 166 | while (!worklist.empty()) { |
| 167 | Function* currFunc = worklist.back(); |
| 168 | worklist.pop_back(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 169 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 170 | if (currFunc->use_empty()) continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 171 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 172 | bool recursive = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 173 | for (User *U : currFunc->users()) |
| 174 | if (Instruction* I = dyn_cast<Instruction>(U)) |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 175 | if (I->getParent()->getParent() == currFunc) { |
| 176 | recursive = true; |
| 177 | break; |
| 178 | } |
| 179 | if (recursive) continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 180 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 181 | if (Function* newFunc = unswitchFunction(currFunc)) { |
| 182 | worklist.push_back(newFunc); |
| 183 | changed = true; |
| 184 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 185 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 186 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame^] | 187 | return changed; |
| 188 | } |
| 189 | |
| 190 | char PartialInlinerLegacyPass::ID = 0; |
| 191 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", |
| 192 | "Partial Inliner", false, false) |
| 193 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
| 194 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", |
| 195 | "Partial Inliner", false, false) |
| 196 | |
| 197 | ModulePass *llvm::createPartialInliningPass() { |
| 198 | return new PartialInlinerLegacyPass(); |
| 199 | } |
| 200 | |
| 201 | PreservedAnalyses PartialInlinerPass::run(Module &M, |
| 202 | ModuleAnalysisManager &AM) { |
| 203 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 204 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&FAM]( |
| 205 | Function &F) -> AssumptionCache & { |
| 206 | return FAM.getResult<AssumptionAnalysis>(F); |
| 207 | }; |
| 208 | InlineFunctionInfo IFI(nullptr, &GetAssumptionCache); |
| 209 | if (PartialInlinerImpl(IFI).run(M)) |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 210 | return PreservedAnalyses::none(); |
| 211 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 212 | } |