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