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 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/IPO.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" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 23 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 26 | #define DEBUG_TYPE "partialinlining" |
| 27 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 28 | STATISTIC(NumPartialInlined, "Number of functions partially inlined"); |
| 29 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 30 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 31 | struct PartialInliner : public ModulePass { |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 32 | void getAnalysisUsage(AnalysisUsage &AU) const override { } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 33 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 34 | PartialInliner() : ModulePass(ID) { |
| 35 | initializePartialInlinerPass(*PassRegistry::getPassRegistry()); |
| 36 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 37 | |
| 38 | bool runOnModule(Module& M) override; |
| 39 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 40 | private: |
| 41 | Function* unswitchFunction(Function* F); |
| 42 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 43 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 44 | |
| 45 | char PartialInliner::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS(PartialInliner, "partial-inliner", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 47 | "Partial Inliner", false, false) |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 48 | |
| 49 | ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); } |
| 50 | |
| 51 | Function* PartialInliner::unswitchFunction(Function* F) { |
| 52 | // First, verify that this function is an unswitching candidate... |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 53 | BasicBlock *entryBlock = &F->front(); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 54 | BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator()); |
| 55 | if (!BR || BR->isUnconditional()) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 56 | return nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 57 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 58 | BasicBlock* returnBlock = nullptr; |
| 59 | BasicBlock* nonReturnBlock = nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 60 | unsigned returnCount = 0; |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 61 | for (BasicBlock *BB : successors(entryBlock)) { |
| 62 | if (isa<ReturnInst>(BB->getTerminator())) { |
| 63 | returnBlock = BB; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 64 | returnCount++; |
| 65 | } else |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 66 | nonReturnBlock = BB; |
| 67 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 68 | |
| 69 | if (returnCount != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 70 | return nullptr; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 71 | |
| 72 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 73 | ValueToValueMapTy VMap; |
Peter Collingbourne | dba9956 | 2016-05-10 20:23:24 +0000 | [diff] [blame] | 74 | Function* duplicateFunction = CloneFunction(F, VMap); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 75 | duplicateFunction->setLinkage(GlobalValue::InternalLinkage); |
Devang Patel | 0dc3c2d | 2010-06-24 00:33:28 +0000 | [diff] [blame] | 76 | BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]); |
| 77 | BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]); |
| 78 | BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 79 | |
| 80 | // Go ahead and update all uses to the duplicate, so that we can just |
| 81 | // use the inliner functionality when we're done hacking. |
| 82 | F->replaceAllUsesWith(duplicateFunction); |
| 83 | |
| 84 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 85 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 86 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 87 | // of which will go outside. |
| 88 | BasicBlock* preReturn = newReturnBlock; |
| 89 | newReturnBlock = newReturnBlock->splitBasicBlock( |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 90 | newReturnBlock->getFirstNonPHI()->getIterator()); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 91 | BasicBlock::iterator I = preReturn->begin(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 92 | Instruction *Ins = &newReturnBlock->front(); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 93 | while (I != preReturn->end()) { |
| 94 | PHINode* OldPhi = dyn_cast<PHINode>(I); |
| 95 | if (!OldPhi) break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 96 | |
| 97 | PHINode *retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 98 | OldPhi->replaceAllUsesWith(retPhi); |
| 99 | Ins = newReturnBlock->getFirstNonPHI(); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 100 | |
| 101 | retPhi->addIncoming(&*I, preReturn); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 102 | retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock), |
| 103 | newEntryBlock); |
| 104 | OldPhi->removeIncomingValue(newEntryBlock); |
| 105 | |
| 106 | ++I; |
| 107 | } |
| 108 | newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock); |
| 109 | |
| 110 | // Gather up the blocks that we're going to extract. |
| 111 | std::vector<BasicBlock*> toExtract; |
| 112 | toExtract.push_back(newNonReturnBlock); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame^] | 113 | for (BasicBlock &BB : *duplicateFunction) |
| 114 | if (&BB != newEntryBlock && &BB != newReturnBlock && |
| 115 | &BB != newNonReturnBlock) |
| 116 | toExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 117 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 118 | // The CodeExtractor needs a dominator tree. |
| 119 | DominatorTree DT; |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 120 | DT.recalculate(*duplicateFunction); |
| 121 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 122 | // Extract the body of the if. |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 123 | Function* extractedFunction |
| 124 | = CodeExtractor(toExtract, &DT).extractCodeRegion(); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 126 | InlineFunctionInfo IFI; |
| 127 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 128 | // Inline the top-level if test into all callers. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 129 | std::vector<User *> Users(duplicateFunction->user_begin(), |
| 130 | duplicateFunction->user_end()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame^] | 131 | for (User *User : Users) |
| 132 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 133 | InlineFunction(CI, IFI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame^] | 134 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Chris Lattner | 4ba01ec | 2010-04-22 23:07:58 +0000 | [diff] [blame] | 135 | InlineFunction(II, IFI); |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 136 | |
| 137 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 138 | // users (function pointers, etc.) back to the original function. |
| 139 | duplicateFunction->replaceAllUsesWith(F); |
| 140 | duplicateFunction->eraseFromParent(); |
| 141 | |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 142 | ++NumPartialInlined; |
| 143 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 144 | return extractedFunction; |
| 145 | } |
| 146 | |
| 147 | bool PartialInliner::runOnModule(Module& M) { |
Andrew Kaylor | aa641a5 | 2016-04-22 22:06:11 +0000 | [diff] [blame] | 148 | if (skipModule(M)) |
| 149 | return false; |
| 150 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 151 | std::vector<Function*> worklist; |
| 152 | worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame^] | 153 | for (Function &F : M) |
| 154 | if (!F.use_empty() && !F.isDeclaration()) |
| 155 | worklist.push_back(&F); |
| 156 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 157 | bool changed = false; |
| 158 | while (!worklist.empty()) { |
| 159 | Function* currFunc = worklist.back(); |
| 160 | worklist.pop_back(); |
| 161 | |
| 162 | if (currFunc->use_empty()) continue; |
| 163 | |
| 164 | bool recursive = false; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 165 | for (User *U : currFunc->users()) |
| 166 | if (Instruction* I = dyn_cast<Instruction>(U)) |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 167 | if (I->getParent()->getParent() == currFunc) { |
| 168 | recursive = true; |
| 169 | break; |
| 170 | } |
| 171 | if (recursive) continue; |
| 172 | |
| 173 | |
| 174 | if (Function* newFunc = unswitchFunction(currFunc)) { |
| 175 | worklist.push_back(newFunc); |
| 176 | changed = true; |
| 177 | } |
| 178 | |
| 179 | } |
| 180 | |
| 181 | return changed; |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 182 | } |