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" |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 17 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 18 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/InlineCost.h" |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/LoopInfo.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 24 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CFG.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 26 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 27 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Instructions.h" |
| 29 | #include "llvm/IR/Module.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/IPO.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 36 | #define DEBUG_TYPE "partial-inlining" |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 37 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 38 | STATISTIC(NumPartialInlined, |
| 39 | "Number of callsites functions partially inlined into."); |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 40 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 41 | // Command line option to disable partial-inlining. The default is false: |
| 42 | static cl::opt<bool> |
| 43 | DisablePartialInlining("disable-partial-inlining", cl::init(false), |
| 44 | cl::Hidden, cl::desc("Disable partial ininling")); |
| 45 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 46 | static cl::opt<unsigned> MaxNumInlineBlocks( |
| 47 | "max-num-inline-blocks", cl::init(5), cl::Hidden, |
| 48 | cl::desc("Max Number of Blocks To be Partially Inlined")); |
| 49 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 50 | // Command line option to set the maximum number of partial inlining allowed |
| 51 | // for the module. The default value of -1 means no limit. |
| 52 | static cl::opt<int> MaxNumPartialInlining( |
| 53 | "max-partial-inlining", cl::init(-1), cl::Hidden, cl::ZeroOrMore, |
| 54 | cl::desc("Max number of partial inlining. The default is unlimited")); |
| 55 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 56 | namespace { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 57 | |
| 58 | struct FunctionOutliningInfo { |
| 59 | FunctionOutliningInfo() |
| 60 | : Entries(), ReturnBlock(nullptr), NonReturnBlock(nullptr), |
| 61 | ReturnBlockPreds() {} |
| 62 | // Returns the number of blocks to be inlined including all blocks |
| 63 | // in Entries and one return block. |
| 64 | unsigned GetNumInlinedBlocks() const { return Entries.size() + 1; } |
| 65 | |
| 66 | // A set of blocks including the function entry that guard |
| 67 | // the region to be outlined. |
| 68 | SmallVector<BasicBlock *, 4> Entries; |
| 69 | // The return block that is not included in the outlined region. |
| 70 | BasicBlock *ReturnBlock; |
| 71 | // The dominating block of the region ot be outlined. |
| 72 | BasicBlock *NonReturnBlock; |
| 73 | // The set of blocks in Entries that that are predecessors to ReturnBlock |
| 74 | SmallVector<BasicBlock *, 4> ReturnBlockPreds; |
| 75 | }; |
| 76 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 77 | struct PartialInlinerImpl { |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 78 | PartialInlinerImpl( |
| 79 | std::function<AssumptionCache &(Function &)> *GetAC, |
| 80 | std::function<TargetTransformInfo &(Function &)> *GTTI, |
| 81 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GBFI, |
| 82 | ProfileSummaryInfo *ProfSI) |
| 83 | : GetAssumptionCache(GetAC), GetTTI(GTTI), GetBFI(GBFI), PSI(ProfSI) {} |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 84 | bool run(Module &M); |
| 85 | Function *unswitchFunction(Function *F); |
| 86 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 87 | std::unique_ptr<FunctionOutliningInfo> computeOutliningInfo(Function *F); |
| 88 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 89 | private: |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 90 | int NumPartialInlining = 0; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 91 | std::function<AssumptionCache &(Function &)> *GetAssumptionCache; |
| 92 | std::function<TargetTransformInfo &(Function &)> *GetTTI; |
| 93 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI; |
| 94 | ProfileSummaryInfo *PSI; |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 95 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 96 | bool shouldPartialInline(CallSite CS, OptimizationRemarkEmitter &ORE); |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 97 | bool IsLimitReached() { |
| 98 | return (MaxNumPartialInlining != -1 && |
| 99 | NumPartialInlining >= MaxNumPartialInlining); |
| 100 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 101 | }; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 102 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 103 | struct PartialInlinerLegacyPass : public ModulePass { |
| 104 | static char ID; // Pass identification, replacement for typeid |
| 105 | PartialInlinerLegacyPass() : ModulePass(ID) { |
| 106 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 107 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 108 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 109 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 110 | AU.addRequired<AssumptionCacheTracker>(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 111 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 112 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 113 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 114 | bool runOnModule(Module &M) override { |
| 115 | if (skipModule(M)) |
| 116 | return false; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 117 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 118 | AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 119 | TargetTransformInfoWrapperPass *TTIWP = |
| 120 | &getAnalysis<TargetTransformInfoWrapperPass>(); |
| 121 | ProfileSummaryInfo *PSI = |
| 122 | getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 123 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 124 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 125 | [&ACT](Function &F) -> AssumptionCache & { |
| 126 | return ACT->getAssumptionCache(F); |
| 127 | }; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 128 | |
| 129 | std::function<TargetTransformInfo &(Function &)> GetTTI = |
| 130 | [&TTIWP](Function &F) -> TargetTransformInfo & { |
| 131 | return TTIWP->getTTI(F); |
| 132 | }; |
| 133 | |
| 134 | return PartialInlinerImpl(&GetAssumptionCache, &GetTTI, None, PSI).run(M); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 135 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 136 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 137 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 138 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 139 | std::unique_ptr<FunctionOutliningInfo> |
| 140 | PartialInlinerImpl::computeOutliningInfo(Function *F) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 141 | BasicBlock *EntryBlock = &F->front(); |
| 142 | BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator()); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 143 | if (!BR || BR->isUnconditional()) |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 144 | return std::unique_ptr<FunctionOutliningInfo>(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 145 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 146 | // Returns true if Succ is BB's successor |
| 147 | auto IsSuccessor = [](BasicBlock *Succ, BasicBlock *BB) { |
| 148 | return is_contained(successors(BB), Succ); |
| 149 | }; |
| 150 | |
| 151 | auto SuccSize = [](BasicBlock *BB) { |
| 152 | return std::distance(succ_begin(BB), succ_end(BB)); |
| 153 | }; |
| 154 | |
| 155 | auto IsReturnBlock = [](BasicBlock *BB) { |
| 156 | TerminatorInst *TI = BB->getTerminator(); |
| 157 | return isa<ReturnInst>(TI); |
| 158 | }; |
| 159 | |
| 160 | auto GetReturnBlock = [=](BasicBlock *Succ1, BasicBlock *Succ2) { |
| 161 | if (IsReturnBlock(Succ1)) |
| 162 | return std::make_tuple(Succ1, Succ2); |
| 163 | if (IsReturnBlock(Succ2)) |
| 164 | return std::make_tuple(Succ2, Succ1); |
| 165 | |
| 166 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); |
| 167 | }; |
| 168 | |
| 169 | // Detect a triangular shape: |
| 170 | auto GetCommonSucc = [=](BasicBlock *Succ1, BasicBlock *Succ2) { |
| 171 | if (IsSuccessor(Succ1, Succ2)) |
| 172 | return std::make_tuple(Succ1, Succ2); |
| 173 | if (IsSuccessor(Succ2, Succ1)) |
| 174 | return std::make_tuple(Succ2, Succ1); |
| 175 | |
| 176 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); |
| 177 | }; |
| 178 | |
| 179 | std::unique_ptr<FunctionOutliningInfo> OutliningInfo = |
| 180 | llvm::make_unique<FunctionOutliningInfo>(); |
| 181 | |
| 182 | BasicBlock *CurrEntry = EntryBlock; |
| 183 | bool CandidateFound = false; |
| 184 | do { |
| 185 | // The number of blocks to be inlined has already reached |
| 186 | // the limit. When MaxNumInlineBlocks is set to 0 or 1, this |
| 187 | // disables partial inlining for the function. |
| 188 | if (OutliningInfo->GetNumInlinedBlocks() >= MaxNumInlineBlocks) |
| 189 | break; |
| 190 | |
| 191 | if (SuccSize(CurrEntry) != 2) |
| 192 | break; |
| 193 | |
| 194 | BasicBlock *Succ1 = *succ_begin(CurrEntry); |
| 195 | BasicBlock *Succ2 = *(succ_begin(CurrEntry) + 1); |
| 196 | |
| 197 | BasicBlock *ReturnBlock, *NonReturnBlock; |
| 198 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); |
| 199 | |
| 200 | if (ReturnBlock) { |
| 201 | OutliningInfo->Entries.push_back(CurrEntry); |
| 202 | OutliningInfo->ReturnBlock = ReturnBlock; |
| 203 | OutliningInfo->NonReturnBlock = NonReturnBlock; |
| 204 | CandidateFound = true; |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | BasicBlock *CommSucc; |
| 209 | BasicBlock *OtherSucc; |
| 210 | std::tie(CommSucc, OtherSucc) = GetCommonSucc(Succ1, Succ2); |
| 211 | |
| 212 | if (!CommSucc) |
| 213 | break; |
| 214 | |
| 215 | OutliningInfo->Entries.push_back(CurrEntry); |
| 216 | CurrEntry = OtherSucc; |
| 217 | |
| 218 | } while (true); |
| 219 | |
| 220 | if (!CandidateFound) |
| 221 | return std::unique_ptr<FunctionOutliningInfo>(); |
| 222 | |
| 223 | // Do sanity check of the entries: threre should not |
| 224 | // be any successors (not in the entry set) other than |
| 225 | // {ReturnBlock, NonReturnBlock} |
| 226 | assert(OutliningInfo->Entries[0] == &F->front()); |
| 227 | DenseSet<BasicBlock *> Entries; |
| 228 | for (BasicBlock *E : OutliningInfo->Entries) |
| 229 | Entries.insert(E); |
| 230 | |
| 231 | // Returns true of BB has Predecessor which is not |
| 232 | // in Entries set. |
| 233 | auto HasNonEntryPred = [Entries](BasicBlock *BB) { |
| 234 | for (auto Pred : predecessors(BB)) { |
| 235 | if (!Entries.count(Pred)) |
| 236 | return true; |
| 237 | } |
| 238 | return false; |
| 239 | }; |
| 240 | auto CheckAndNormalizeCandidate = |
| 241 | [Entries, HasNonEntryPred](FunctionOutliningInfo *OutliningInfo) { |
| 242 | for (BasicBlock *E : OutliningInfo->Entries) { |
| 243 | for (auto Succ : successors(E)) { |
| 244 | if (Entries.count(Succ)) |
| 245 | continue; |
| 246 | if (Succ == OutliningInfo->ReturnBlock) |
| 247 | OutliningInfo->ReturnBlockPreds.push_back(E); |
| 248 | else if (Succ != OutliningInfo->NonReturnBlock) |
| 249 | return false; |
| 250 | } |
| 251 | // There should not be any outside incoming edges either: |
| 252 | if (HasNonEntryPred(E)) |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | }; |
| 257 | |
| 258 | if (!CheckAndNormalizeCandidate(OutliningInfo.get())) |
| 259 | return std::unique_ptr<FunctionOutliningInfo>(); |
| 260 | |
| 261 | // Now further growing the candidate's inlining region by |
| 262 | // peeling off dominating blocks from the outlining region: |
| 263 | while (OutliningInfo->GetNumInlinedBlocks() < MaxNumInlineBlocks) { |
| 264 | BasicBlock *Cand = OutliningInfo->NonReturnBlock; |
| 265 | if (SuccSize(Cand) != 2) |
| 266 | break; |
| 267 | |
| 268 | if (HasNonEntryPred(Cand)) |
| 269 | break; |
| 270 | |
| 271 | BasicBlock *Succ1 = *succ_begin(Cand); |
| 272 | BasicBlock *Succ2 = *(succ_begin(Cand) + 1); |
| 273 | |
| 274 | BasicBlock *ReturnBlock, *NonReturnBlock; |
| 275 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); |
| 276 | if (!ReturnBlock || ReturnBlock != OutliningInfo->ReturnBlock) |
| 277 | break; |
| 278 | |
| 279 | if (NonReturnBlock->getSinglePredecessor() != Cand) |
| 280 | break; |
| 281 | |
| 282 | // Now grow and update OutlininigInfo: |
| 283 | OutliningInfo->Entries.push_back(Cand); |
| 284 | OutliningInfo->NonReturnBlock = NonReturnBlock; |
| 285 | OutliningInfo->ReturnBlockPreds.push_back(Cand); |
| 286 | Entries.insert(Cand); |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 287 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 288 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 289 | return OutliningInfo; |
| 290 | } |
| 291 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 292 | bool PartialInlinerImpl::shouldPartialInline(CallSite CS, |
| 293 | OptimizationRemarkEmitter &ORE) { |
| 294 | // TODO : more sharing with shouldInline in Inliner.cpp |
| 295 | using namespace ore; |
| 296 | Instruction *Call = CS.getInstruction(); |
| 297 | Function *Callee = CS.getCalledFunction(); |
| 298 | Function *Caller = CS.getCaller(); |
| 299 | auto &CalleeTTI = (*GetTTI)(*Callee); |
| 300 | InlineCost IC = getInlineCost(CS, getInlineParams(), CalleeTTI, |
| 301 | *GetAssumptionCache, GetBFI, PSI); |
| 302 | |
| 303 | if (IC.isAlways()) { |
| 304 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AlwaysInline", Call) |
| 305 | << NV("Callee", Callee) |
| 306 | << " should always be fully inlined, not partially"); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | if (IC.isNever()) { |
| 311 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call) |
| 312 | << NV("Callee", Callee) << " not partially inlined into " |
| 313 | << NV("Caller", Caller) |
| 314 | << " because it should never be inlined (cost=never)"); |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (!IC) { |
| 319 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "TooCostly", Call) |
| 320 | << NV("Callee", Callee) << " not partially inlined into " |
| 321 | << NV("Caller", Caller) << " because too costly to inline (cost=" |
| 322 | << NV("Cost", IC.getCost()) << ", threshold=" |
| 323 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "CanBePartiallyInlined", Call) |
| 328 | << NV("Callee", Callee) << " can be partially inlined into " |
| 329 | << NV("Caller", Caller) << " with cost=" << NV("Cost", IC.getCost()) |
| 330 | << " (threshold=" |
| 331 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
| 332 | return true; |
| 333 | } |
| 334 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 335 | Function *PartialInlinerImpl::unswitchFunction(Function *F) { |
| 336 | |
| 337 | if (F->hasAddressTaken()) |
| 338 | return nullptr; |
| 339 | |
Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame^] | 340 | // Let inliner handle it |
| 341 | if (F->hasFnAttribute(Attribute::AlwaysInline)) |
| 342 | return nullptr; |
| 343 | |
| 344 | if (F->hasFnAttribute(Attribute::NoInline)) |
| 345 | return nullptr; |
| 346 | |
| 347 | if (PSI->isFunctionEntryCold(F)) |
| 348 | return nullptr; |
| 349 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 350 | std::unique_ptr<FunctionOutliningInfo> OutliningInfo = |
| 351 | computeOutliningInfo(F); |
| 352 | |
| 353 | if (!OutliningInfo) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 354 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 355 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 356 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 357 | ValueToValueMapTy VMap; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 358 | Function *DuplicateFunction = CloneFunction(F, VMap); |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 359 | BasicBlock *NewReturnBlock = |
| 360 | cast<BasicBlock>(VMap[OutliningInfo->ReturnBlock]); |
| 361 | BasicBlock *NewNonReturnBlock = |
| 362 | cast<BasicBlock>(VMap[OutliningInfo->NonReturnBlock]); |
| 363 | DenseSet<BasicBlock *> NewEntries; |
| 364 | for (BasicBlock *BB : OutliningInfo->Entries) { |
| 365 | NewEntries.insert(cast<BasicBlock>(VMap[BB])); |
| 366 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 367 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 368 | // Go ahead and update all uses to the duplicate, so that we can just |
| 369 | // use the inliner functionality when we're done hacking. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 370 | F->replaceAllUsesWith(DuplicateFunction); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 371 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 372 | auto getFirstPHI = [](BasicBlock *BB) { |
| 373 | BasicBlock::iterator I = BB->begin(); |
| 374 | PHINode *FirstPhi = nullptr; |
| 375 | while (I != BB->end()) { |
| 376 | PHINode *Phi = dyn_cast<PHINode>(I); |
| 377 | if (!Phi) |
| 378 | break; |
| 379 | if (!FirstPhi) { |
| 380 | FirstPhi = Phi; |
| 381 | break; |
| 382 | } |
| 383 | } |
| 384 | return FirstPhi; |
| 385 | }; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 386 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 387 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 388 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 389 | // of which will go outside. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 390 | BasicBlock *PreReturn = NewReturnBlock; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 391 | // only split block when necessary: |
| 392 | PHINode *FirstPhi = getFirstPHI(PreReturn); |
| 393 | unsigned NumPredsFromEntries = OutliningInfo->ReturnBlockPreds.size(); |
| 394 | if (FirstPhi && FirstPhi->getNumIncomingValues() > NumPredsFromEntries + 1) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 395 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 396 | NewReturnBlock = NewReturnBlock->splitBasicBlock( |
| 397 | NewReturnBlock->getFirstNonPHI()->getIterator()); |
| 398 | BasicBlock::iterator I = PreReturn->begin(); |
| 399 | Instruction *Ins = &NewReturnBlock->front(); |
| 400 | while (I != PreReturn->end()) { |
| 401 | PHINode *OldPhi = dyn_cast<PHINode>(I); |
| 402 | if (!OldPhi) |
| 403 | break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 404 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 405 | PHINode *RetPhi = |
| 406 | PHINode::Create(OldPhi->getType(), NumPredsFromEntries + 1, "", Ins); |
| 407 | OldPhi->replaceAllUsesWith(RetPhi); |
| 408 | Ins = NewReturnBlock->getFirstNonPHI(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 409 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 410 | RetPhi->addIncoming(&*I, PreReturn); |
| 411 | for (BasicBlock *E : OutliningInfo->ReturnBlockPreds) { |
| 412 | BasicBlock *NewE = cast<BasicBlock>(VMap[E]); |
| 413 | RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewE), NewE); |
| 414 | OldPhi->removeIncomingValue(NewE); |
| 415 | } |
| 416 | ++I; |
| 417 | } |
| 418 | for (auto E : OutliningInfo->ReturnBlockPreds) { |
| 419 | BasicBlock *NewE = cast<BasicBlock>(VMap[E]); |
| 420 | NewE->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock); |
| 421 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 422 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 423 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 424 | // Returns true if the block is to be partial inlined into the caller |
| 425 | // (i.e. not to be extracted to the out of line function) |
| 426 | auto ToBeInlined = [=](BasicBlock *BB) { |
| 427 | return BB == NewReturnBlock || NewEntries.count(BB); |
| 428 | }; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 429 | // Gather up the blocks that we're going to extract. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 430 | std::vector<BasicBlock *> ToExtract; |
| 431 | ToExtract.push_back(NewNonReturnBlock); |
| 432 | for (BasicBlock &BB : *DuplicateFunction) |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 433 | if (!ToBeInlined(&BB) && &BB != NewNonReturnBlock) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 434 | ToExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 435 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 436 | // The CodeExtractor needs a dominator tree. |
| 437 | DominatorTree DT; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 438 | DT.recalculate(*DuplicateFunction); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 439 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 440 | // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo. |
| 441 | LoopInfo LI(DT); |
| 442 | BranchProbabilityInfo BPI(*DuplicateFunction, LI); |
| 443 | BlockFrequencyInfo BFI(*DuplicateFunction, BPI, LI); |
| 444 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 445 | // Extract the body of the if. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 446 | Function *ExtractedFunction = |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 447 | CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, &BFI, &BPI) |
| 448 | .extractCodeRegion(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 449 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 450 | // Inline the top-level if test into all callers. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 451 | std::vector<User *> Users(DuplicateFunction->user_begin(), |
| 452 | DuplicateFunction->user_end()); |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 453 | |
| 454 | for (User *User : Users) { |
| 455 | CallSite CS; |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 456 | if (CallInst *CI = dyn_cast<CallInst>(User)) |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 457 | CS = CallSite(CI); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 458 | else if (InvokeInst *II = dyn_cast<InvokeInst>(User)) |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 459 | CS = CallSite(II); |
| 460 | else |
| 461 | llvm_unreachable("All uses must be calls"); |
| 462 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 463 | if (IsLimitReached()) |
| 464 | continue; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 465 | |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 466 | OptimizationRemarkEmitter ORE(CS.getCaller()); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 467 | if (!shouldPartialInline(CS, ORE)) |
| 468 | continue; |
| 469 | |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 470 | DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); |
| 471 | BasicBlock *Block = CS.getParent(); |
| 472 | ORE.emit(OptimizationRemark(DEBUG_TYPE, "PartiallyInlined", DLoc, Block) |
| 473 | << ore::NV("Callee", F) << " partially inlined into " |
| 474 | << ore::NV("Caller", CS.getCaller())); |
| 475 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 476 | InlineFunctionInfo IFI(nullptr, GetAssumptionCache); |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 477 | InlineFunction(CS, IFI); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 478 | NumPartialInlining++; |
| 479 | // update stats |
| 480 | NumPartialInlined++; |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 481 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 482 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 483 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 484 | // users (function pointers, etc.) back to the original function. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 485 | DuplicateFunction->replaceAllUsesWith(F); |
| 486 | DuplicateFunction->eraseFromParent(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 487 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 488 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 489 | return ExtractedFunction; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 492 | bool PartialInlinerImpl::run(Module &M) { |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 493 | if (DisablePartialInlining) |
| 494 | return false; |
| 495 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 496 | std::vector<Function *> Worklist; |
| 497 | Worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 498 | for (Function &F : M) |
| 499 | if (!F.use_empty() && !F.isDeclaration()) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 500 | Worklist.push_back(&F); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 501 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 502 | bool Changed = false; |
| 503 | while (!Worklist.empty()) { |
| 504 | Function *CurrFunc = Worklist.back(); |
| 505 | Worklist.pop_back(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 506 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 507 | if (CurrFunc->use_empty()) |
| 508 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 509 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 510 | bool Recursive = false; |
| 511 | for (User *U : CurrFunc->users()) |
| 512 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 513 | if (I->getParent()->getParent() == CurrFunc) { |
| 514 | Recursive = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 515 | break; |
| 516 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 517 | if (Recursive) |
| 518 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 519 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 520 | if (Function *NewFunc = unswitchFunction(CurrFunc)) { |
| 521 | Worklist.push_back(NewFunc); |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 522 | Changed = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 523 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 524 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 525 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 526 | return Changed; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | char PartialInlinerLegacyPass::ID = 0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 530 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", |
| 531 | "Partial Inliner", false, false) |
| 532 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 533 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| 534 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 535 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", |
| 536 | "Partial Inliner", false, false) |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 537 | |
| 538 | ModulePass *llvm::createPartialInliningPass() { |
| 539 | return new PartialInlinerLegacyPass(); |
| 540 | } |
| 541 | |
| 542 | PreservedAnalyses PartialInlinerPass::run(Module &M, |
| 543 | ModuleAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 544 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 545 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 546 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 547 | [&FAM](Function &F) -> AssumptionCache & { |
| 548 | return FAM.getResult<AssumptionAnalysis>(F); |
| 549 | }; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 550 | |
| 551 | std::function<BlockFrequencyInfo &(Function &)> GetBFI = |
| 552 | [&FAM](Function &F) -> BlockFrequencyInfo & { |
| 553 | return FAM.getResult<BlockFrequencyAnalysis>(F); |
| 554 | }; |
| 555 | |
| 556 | std::function<TargetTransformInfo &(Function &)> GetTTI = |
| 557 | [&FAM](Function &F) -> TargetTransformInfo & { |
| 558 | return FAM.getResult<TargetIRAnalysis>(F); |
| 559 | }; |
| 560 | |
| 561 | ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M); |
| 562 | |
| 563 | if (PartialInlinerImpl(&GetAssumptionCache, &GetTTI, {GetBFI}, PSI).run(M)) |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 564 | return PreservedAnalyses::none(); |
| 565 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 566 | } |