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 { |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 32 | struct PartialInlinerLegacyPass : public ModulePass { |
| 33 | static char ID; // Pass identification, replacement for typeid |
| 34 | PartialInlinerLegacyPass() : ModulePass(ID) { |
| 35 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 36 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 37 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 38 | 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 Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 45 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 46 | private: |
| 47 | PartialInlinerPass Impl; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 48 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 49 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 50 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 51 | char PartialInlinerLegacyPass::ID = 0; |
| 52 | INITIALIZE_PASS(PartialInlinerLegacyPass, "partial-inliner", "Partial Inliner", |
| 53 | false, false) |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 54 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 55 | ModulePass *llvm::createPartialInliningPass() { |
| 56 | return new PartialInlinerLegacyPass(); |
| 57 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 58 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 59 | Function *PartialInlinerPass::unswitchFunction(Function *F) { |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 60 | // First, verify that this function is an unswitching candidate... |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 61 | BasicBlock *entryBlock = &F->front(); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 62 | BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); |
| 63 | if (!BR || BR->isUnconditional()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 64 | return nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 65 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 66 | BasicBlock* returnBlock = nullptr; |
| 67 | BasicBlock* nonReturnBlock = nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 68 | unsigned returnCount = 0; |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 69 | for (BasicBlock *BB : successors(entryBlock)) { |
| 70 | if (isa<ReturnInst>(BB->getTerminator())) { |
| 71 | returnBlock = BB; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 72 | returnCount++; |
| 73 | } else |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 74 | nonReturnBlock = BB; |
| 75 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 76 | |
| 77 | if (returnCount != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 78 | return nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 79 | |
| 80 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 81 | ValueToValueMapTy VMap; |
Peter Collingbourne | dba9956 | 2016-05-10 20:23:24 +0000 | [diff] [blame] | 82 | Function* duplicateFunction = CloneFunction(F, VMap); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 83 | duplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 84 | BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]); |
| 85 | BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]); |
| 86 | BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 87 | |
| 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 Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 98 | newReturnBlock->getFirstNonPHI()->getIterator()); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 99 | BasicBlock::iterator I = preReturn->begin(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 100 | Instruction *Ins = &newReturnBlock->front(); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 101 | while (I != preReturn->end()) { |
| 102 | PHINode* OldPhi = dyn_cast<PHINode>(I); |
| 103 | if (!OldPhi) break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 104 | |
| 105 | PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 106 | OldPhi->replaceAllUsesWith(retPhi); |
| 107 | Ins = newReturnBlock->getFirstNonPHI(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 108 | |
| 109 | retPhi->addIncoming(&*I, preReturn); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 110 | 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 Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 121 | for (BasicBlock &BB : *duplicateFunction) |
| 122 | if (&BB != newEntryBlock && &BB != newReturnBlock && |
| 123 | &BB != newNonReturnBlock) |
| 124 | toExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 125 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 126 | // The CodeExtractor needs a dominator tree. |
| 127 | DominatorTree DT; |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 128 | DT.recalculate(*duplicateFunction); |
| 129 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 130 | // Extract the body of the if. |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 131 | Function* extractedFunction |
| 132 | = CodeExtractor(toExtract, &DT).extractCodeRegion(); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 134 | InlineFunctionInfo IFI; |
| 135 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 136 | // Inline the top-level if test into all callers. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 137 | std::vector<User *> Users(duplicateFunction->user_begin(), |
| 138 | duplicateFunction->user_end()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 139 | for (User *User : Users) |
| 140 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 141 | InlineFunction(CI, IFI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 142 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 143 | InlineFunction(II, IFI); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 144 | |
| 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 Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 150 | ++NumPartialInlined; |
| 151 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 152 | return extractedFunction; |
| 153 | } |
| 154 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 155 | PreservedAnalyses PartialInlinerPass::run(Module &M, ModuleAnalysisManager &) { |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 156 | std::vector<Function*> worklist; |
| 157 | worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 158 | for (Function &F : M) |
| 159 | if (!F.use_empty() && !F.isDeclaration()) |
| 160 | worklist.push_back(&F); |
| 161 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 162 | 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 Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 170 | for (User *U : currFunc->users()) |
| 171 | if (Instruction* I = dyn_cast<Instruction>(U)) |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 172 | 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 Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame^] | 185 | |
| 186 | if (changed) |
| 187 | return PreservedAnalyses::none(); |
| 188 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 189 | } |