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 | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CodeMetrics.h" |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/InlineCost.h" |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/LoopInfo.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
| 24 | #include "llvm/Analysis/TargetLibraryInfo.h" |
| 25 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 26 | #include "llvm/IR/CFG.h" |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 27 | #include "llvm/IR/DiagnosticInfo.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 29 | #include "llvm/IR/Instructions.h" |
| 30 | #include "llvm/IR/Module.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 31 | #include "llvm/Pass.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Utils/Cloning.h" |
Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 37 | #define DEBUG_TYPE "partial-inlining" |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 38 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 39 | STATISTIC(NumPartialInlined, |
| 40 | "Number of callsites functions partially inlined into."); |
Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 41 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 42 | // Command line option to disable partial-inlining. The default is false: |
| 43 | static cl::opt<bool> |
| 44 | DisablePartialInlining("disable-partial-inlining", cl::init(false), |
| 45 | cl::Hidden, cl::desc("Disable partial ininling")); |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 46 | // This is an option used by testing: |
| 47 | static cl::opt<bool> SkipCostAnalysis("skip-partial-inlining-cost-analysis", |
| 48 | cl::init(false), cl::ZeroOrMore, |
| 49 | cl::ReallyHidden, |
| 50 | cl::desc("Skip Cost Analysis")); |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 51 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 52 | static cl::opt<unsigned> MaxNumInlineBlocks( |
| 53 | "max-num-inline-blocks", cl::init(5), cl::Hidden, |
| 54 | cl::desc("Max Number of Blocks To be Partially Inlined")); |
| 55 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 56 | // Command line option to set the maximum number of partial inlining allowed |
| 57 | // for the module. The default value of -1 means no limit. |
| 58 | static cl::opt<int> MaxNumPartialInlining( |
| 59 | "max-partial-inlining", cl::init(-1), cl::Hidden, cl::ZeroOrMore, |
| 60 | cl::desc("Max number of partial inlining. The default is unlimited")); |
| 61 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 62 | // Used only when PGO or user annotated branch data is absent. It is |
| 63 | // the least value that is used to weigh the outline region. If BFI |
| 64 | // produces larger value, the BFI value will be used. |
| 65 | static cl::opt<int> |
| 66 | OutlineRegionFreqPercent("outline-region-freq-percent", cl::init(75), |
| 67 | cl::Hidden, cl::ZeroOrMore, |
| 68 | cl::desc("Relative frequency of outline region to " |
| 69 | "the entry block")); |
| 70 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 71 | namespace { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 72 | |
| 73 | struct FunctionOutliningInfo { |
| 74 | FunctionOutliningInfo() |
| 75 | : Entries(), ReturnBlock(nullptr), NonReturnBlock(nullptr), |
| 76 | ReturnBlockPreds() {} |
| 77 | // Returns the number of blocks to be inlined including all blocks |
| 78 | // in Entries and one return block. |
| 79 | unsigned GetNumInlinedBlocks() const { return Entries.size() + 1; } |
| 80 | |
| 81 | // A set of blocks including the function entry that guard |
| 82 | // the region to be outlined. |
| 83 | SmallVector<BasicBlock *, 4> Entries; |
| 84 | // The return block that is not included in the outlined region. |
| 85 | BasicBlock *ReturnBlock; |
| 86 | // The dominating block of the region ot be outlined. |
| 87 | BasicBlock *NonReturnBlock; |
| 88 | // The set of blocks in Entries that that are predecessors to ReturnBlock |
| 89 | SmallVector<BasicBlock *, 4> ReturnBlockPreds; |
| 90 | }; |
| 91 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 92 | struct PartialInlinerImpl { |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 93 | PartialInlinerImpl( |
| 94 | std::function<AssumptionCache &(Function &)> *GetAC, |
| 95 | std::function<TargetTransformInfo &(Function &)> *GTTI, |
| 96 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GBFI, |
| 97 | ProfileSummaryInfo *ProfSI) |
| 98 | : GetAssumptionCache(GetAC), GetTTI(GTTI), GetBFI(GBFI), PSI(ProfSI) {} |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 99 | bool run(Module &M); |
| 100 | Function *unswitchFunction(Function *F); |
| 101 | |
| 102 | private: |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 103 | int NumPartialInlining = 0; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 104 | std::function<AssumptionCache &(Function &)> *GetAssumptionCache; |
| 105 | std::function<TargetTransformInfo &(Function &)> *GetTTI; |
| 106 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI; |
| 107 | ProfileSummaryInfo *PSI; |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 108 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 109 | // Return the frequency of the OutlininingBB relative to F's entry point. |
| 110 | // The result is no larger than 1 and is represented using BP. |
| 111 | // (Note that the outlined region's 'head' block can only have incoming |
| 112 | // edges from the guarding entry blocks). |
| 113 | BranchProbability getOutliningCallBBRelativeFreq(Function *F, |
| 114 | FunctionOutliningInfo *OI, |
| 115 | Function *DuplicateFunction, |
| 116 | BlockFrequencyInfo *BFI, |
| 117 | BasicBlock *OutliningCallBB); |
| 118 | |
| 119 | // Return true if the callee of CS should be partially inlined with |
| 120 | // profit. |
| 121 | bool shouldPartialInline(CallSite CS, Function *F, FunctionOutliningInfo *OI, |
| 122 | BlockFrequencyInfo *CalleeBFI, |
| 123 | BasicBlock *OutliningCallBB, |
| 124 | int OutliningCallOverhead, |
| 125 | OptimizationRemarkEmitter &ORE); |
| 126 | |
| 127 | // Try to inline DuplicateFunction (cloned from F with call to |
| 128 | // the OutlinedFunction into its callers. Return true |
| 129 | // if there is any successful inlining. |
| 130 | bool tryPartialInline(Function *DuplicateFunction, |
| 131 | Function *F, /*orignal function */ |
| 132 | FunctionOutliningInfo *OI, Function *OutlinedFunction, |
| 133 | BlockFrequencyInfo *CalleeBFI); |
| 134 | |
| 135 | // Compute the mapping from use site of DuplicationFunction to the enclosing |
| 136 | // BB's profile count. |
| 137 | void computeCallsiteToProfCountMap(Function *DuplicateFunction, |
| 138 | DenseMap<User *, uint64_t> &SiteCountMap); |
| 139 | |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 140 | bool IsLimitReached() { |
| 141 | return (MaxNumPartialInlining != -1 && |
| 142 | NumPartialInlining >= MaxNumPartialInlining); |
| 143 | } |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 144 | |
| 145 | CallSite getCallSite(User *U) { |
| 146 | CallSite CS; |
| 147 | if (CallInst *CI = dyn_cast<CallInst>(U)) |
| 148 | CS = CallSite(CI); |
| 149 | else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) |
| 150 | CS = CallSite(II); |
| 151 | else |
| 152 | llvm_unreachable("All uses must be calls"); |
| 153 | return CS; |
| 154 | } |
| 155 | |
| 156 | CallSite getOneCallSiteTo(Function *F) { |
| 157 | User *User = *F->user_begin(); |
| 158 | return getCallSite(User); |
| 159 | } |
| 160 | |
| 161 | std::tuple<DebugLoc, BasicBlock *> getOneDebugLoc(Function *F) { |
| 162 | CallSite CS = getOneCallSiteTo(F); |
| 163 | DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); |
| 164 | BasicBlock *Block = CS.getParent(); |
| 165 | return std::make_tuple(DLoc, Block); |
| 166 | } |
| 167 | |
| 168 | // Returns the costs associated with function outlining: |
| 169 | // - The first value is the non-weighted runtime cost for making the call |
| 170 | // to the outlined function 'OutlinedFunction', including the addtional |
| 171 | // setup cost in the outlined function itself; |
| 172 | // - The second value is the estimated size of the new call sequence in |
| 173 | // basic block 'OutliningCallBB'; |
| 174 | // - The third value is the estimated size of the original code from |
| 175 | // function 'F' that is extracted into the outlined function. |
| 176 | std::tuple<int, int, int> |
| 177 | computeOutliningCosts(Function *F, const FunctionOutliningInfo *OutliningInfo, |
| 178 | Function *OutlinedFunction, |
| 179 | BasicBlock *OutliningCallBB); |
| 180 | // Compute the 'InlineCost' of block BB. InlineCost is a proxy used to |
| 181 | // approximate both the size and runtime cost (Note that in the current |
| 182 | // inline cost analysis, there is no clear distinction there either). |
| 183 | int computeBBInlineCost(BasicBlock *BB); |
| 184 | |
| 185 | std::unique_ptr<FunctionOutliningInfo> computeOutliningInfo(Function *F); |
| 186 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 187 | }; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 188 | |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 189 | struct PartialInlinerLegacyPass : public ModulePass { |
| 190 | static char ID; // Pass identification, replacement for typeid |
| 191 | PartialInlinerLegacyPass() : ModulePass(ID) { |
| 192 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 193 | } |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 194 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 195 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 196 | AU.addRequired<AssumptionCacheTracker>(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 197 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
| 198 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 199 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 200 | bool runOnModule(Module &M) override { |
| 201 | if (skipModule(M)) |
| 202 | return false; |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 203 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 204 | AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 205 | TargetTransformInfoWrapperPass *TTIWP = |
| 206 | &getAnalysis<TargetTransformInfoWrapperPass>(); |
| 207 | ProfileSummaryInfo *PSI = |
| 208 | getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
| 209 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 210 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 211 | [&ACT](Function &F) -> AssumptionCache & { |
| 212 | return ACT->getAssumptionCache(F); |
| 213 | }; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 214 | |
| 215 | std::function<TargetTransformInfo &(Function &)> GetTTI = |
| 216 | [&TTIWP](Function &F) -> TargetTransformInfo & { |
| 217 | return TTIWP->getTTI(F); |
| 218 | }; |
| 219 | |
| 220 | return PartialInlinerImpl(&GetAssumptionCache, &GetTTI, None, PSI).run(M); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 221 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 222 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 223 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 224 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 225 | std::unique_ptr<FunctionOutliningInfo> |
| 226 | PartialInlinerImpl::computeOutliningInfo(Function *F) { |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 227 | BasicBlock *EntryBlock = &F->front(); |
| 228 | BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator()); |
Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 229 | if (!BR || BR->isUnconditional()) |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 230 | return std::unique_ptr<FunctionOutliningInfo>(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 231 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 232 | // Returns true if Succ is BB's successor |
| 233 | auto IsSuccessor = [](BasicBlock *Succ, BasicBlock *BB) { |
| 234 | return is_contained(successors(BB), Succ); |
| 235 | }; |
| 236 | |
| 237 | auto SuccSize = [](BasicBlock *BB) { |
| 238 | return std::distance(succ_begin(BB), succ_end(BB)); |
| 239 | }; |
| 240 | |
| 241 | auto IsReturnBlock = [](BasicBlock *BB) { |
| 242 | TerminatorInst *TI = BB->getTerminator(); |
| 243 | return isa<ReturnInst>(TI); |
| 244 | }; |
| 245 | |
Davide Italiano | aa42a10 | 2017-05-08 20:44:01 +0000 | [diff] [blame] | 246 | auto GetReturnBlock = [&](BasicBlock *Succ1, BasicBlock *Succ2) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 247 | if (IsReturnBlock(Succ1)) |
| 248 | return std::make_tuple(Succ1, Succ2); |
| 249 | if (IsReturnBlock(Succ2)) |
| 250 | return std::make_tuple(Succ2, Succ1); |
| 251 | |
| 252 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); |
| 253 | }; |
| 254 | |
| 255 | // Detect a triangular shape: |
Davide Italiano | aa42a10 | 2017-05-08 20:44:01 +0000 | [diff] [blame] | 256 | auto GetCommonSucc = [&](BasicBlock *Succ1, BasicBlock *Succ2) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 257 | if (IsSuccessor(Succ1, Succ2)) |
| 258 | return std::make_tuple(Succ1, Succ2); |
| 259 | if (IsSuccessor(Succ2, Succ1)) |
| 260 | return std::make_tuple(Succ2, Succ1); |
| 261 | |
| 262 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); |
| 263 | }; |
| 264 | |
| 265 | std::unique_ptr<FunctionOutliningInfo> OutliningInfo = |
| 266 | llvm::make_unique<FunctionOutliningInfo>(); |
| 267 | |
| 268 | BasicBlock *CurrEntry = EntryBlock; |
| 269 | bool CandidateFound = false; |
| 270 | do { |
| 271 | // The number of blocks to be inlined has already reached |
| 272 | // the limit. When MaxNumInlineBlocks is set to 0 or 1, this |
| 273 | // disables partial inlining for the function. |
| 274 | if (OutliningInfo->GetNumInlinedBlocks() >= MaxNumInlineBlocks) |
| 275 | break; |
| 276 | |
| 277 | if (SuccSize(CurrEntry) != 2) |
| 278 | break; |
| 279 | |
| 280 | BasicBlock *Succ1 = *succ_begin(CurrEntry); |
| 281 | BasicBlock *Succ2 = *(succ_begin(CurrEntry) + 1); |
| 282 | |
| 283 | BasicBlock *ReturnBlock, *NonReturnBlock; |
| 284 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); |
| 285 | |
| 286 | if (ReturnBlock) { |
| 287 | OutliningInfo->Entries.push_back(CurrEntry); |
| 288 | OutliningInfo->ReturnBlock = ReturnBlock; |
| 289 | OutliningInfo->NonReturnBlock = NonReturnBlock; |
| 290 | CandidateFound = true; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | BasicBlock *CommSucc; |
| 295 | BasicBlock *OtherSucc; |
| 296 | std::tie(CommSucc, OtherSucc) = GetCommonSucc(Succ1, Succ2); |
| 297 | |
| 298 | if (!CommSucc) |
| 299 | break; |
| 300 | |
| 301 | OutliningInfo->Entries.push_back(CurrEntry); |
| 302 | CurrEntry = OtherSucc; |
| 303 | |
| 304 | } while (true); |
| 305 | |
| 306 | if (!CandidateFound) |
| 307 | return std::unique_ptr<FunctionOutliningInfo>(); |
| 308 | |
| 309 | // Do sanity check of the entries: threre should not |
| 310 | // be any successors (not in the entry set) other than |
| 311 | // {ReturnBlock, NonReturnBlock} |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 312 | assert(OutliningInfo->Entries[0] == &F->front() && |
| 313 | "Function Entry must be the first in Entries vector"); |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 314 | DenseSet<BasicBlock *> Entries; |
| 315 | for (BasicBlock *E : OutliningInfo->Entries) |
| 316 | Entries.insert(E); |
| 317 | |
| 318 | // Returns true of BB has Predecessor which is not |
| 319 | // in Entries set. |
| 320 | auto HasNonEntryPred = [Entries](BasicBlock *BB) { |
| 321 | for (auto Pred : predecessors(BB)) { |
| 322 | if (!Entries.count(Pred)) |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | }; |
| 327 | auto CheckAndNormalizeCandidate = |
| 328 | [Entries, HasNonEntryPred](FunctionOutliningInfo *OutliningInfo) { |
| 329 | for (BasicBlock *E : OutliningInfo->Entries) { |
| 330 | for (auto Succ : successors(E)) { |
| 331 | if (Entries.count(Succ)) |
| 332 | continue; |
| 333 | if (Succ == OutliningInfo->ReturnBlock) |
| 334 | OutliningInfo->ReturnBlockPreds.push_back(E); |
| 335 | else if (Succ != OutliningInfo->NonReturnBlock) |
| 336 | return false; |
| 337 | } |
| 338 | // There should not be any outside incoming edges either: |
| 339 | if (HasNonEntryPred(E)) |
| 340 | return false; |
| 341 | } |
| 342 | return true; |
| 343 | }; |
| 344 | |
| 345 | if (!CheckAndNormalizeCandidate(OutliningInfo.get())) |
| 346 | return std::unique_ptr<FunctionOutliningInfo>(); |
| 347 | |
| 348 | // Now further growing the candidate's inlining region by |
| 349 | // peeling off dominating blocks from the outlining region: |
| 350 | while (OutliningInfo->GetNumInlinedBlocks() < MaxNumInlineBlocks) { |
| 351 | BasicBlock *Cand = OutliningInfo->NonReturnBlock; |
| 352 | if (SuccSize(Cand) != 2) |
| 353 | break; |
| 354 | |
| 355 | if (HasNonEntryPred(Cand)) |
| 356 | break; |
| 357 | |
| 358 | BasicBlock *Succ1 = *succ_begin(Cand); |
| 359 | BasicBlock *Succ2 = *(succ_begin(Cand) + 1); |
| 360 | |
| 361 | BasicBlock *ReturnBlock, *NonReturnBlock; |
| 362 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); |
| 363 | if (!ReturnBlock || ReturnBlock != OutliningInfo->ReturnBlock) |
| 364 | break; |
| 365 | |
| 366 | if (NonReturnBlock->getSinglePredecessor() != Cand) |
| 367 | break; |
| 368 | |
| 369 | // Now grow and update OutlininigInfo: |
| 370 | OutliningInfo->Entries.push_back(Cand); |
| 371 | OutliningInfo->NonReturnBlock = NonReturnBlock; |
| 372 | OutliningInfo->ReturnBlockPreds.push_back(Cand); |
| 373 | Entries.insert(Cand); |
Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 374 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 375 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 376 | return OutliningInfo; |
| 377 | } |
| 378 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 379 | // Check if there is PGO data or user annoated branch data: |
| 380 | static bool hasProfileData(Function *F, FunctionOutliningInfo *OI) { |
| 381 | if (F->getEntryCount()) |
| 382 | return true; |
| 383 | // Now check if any of the entry block has MD_prof data: |
| 384 | for (auto *E : OI->Entries) { |
| 385 | BranchInst *BR = dyn_cast<BranchInst>(E->getTerminator()); |
| 386 | if (!BR || BR->isUnconditional()) |
| 387 | continue; |
| 388 | uint64_t T, F; |
| 389 | if (BR->extractProfMetadata(T, F)) |
| 390 | return true; |
| 391 | } |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | BranchProbability PartialInlinerImpl::getOutliningCallBBRelativeFreq( |
| 396 | Function *F, FunctionOutliningInfo *OI, Function *DuplicateFunction, |
| 397 | BlockFrequencyInfo *BFI, BasicBlock *OutliningCallBB) { |
| 398 | |
| 399 | auto EntryFreq = |
| 400 | BFI->getBlockFreq(&DuplicateFunction->getEntryBlock()); |
| 401 | auto OutliningCallFreq = BFI->getBlockFreq(OutliningCallBB); |
| 402 | |
| 403 | auto OutlineRegionRelFreq = |
| 404 | BranchProbability::getBranchProbability(OutliningCallFreq.getFrequency(), |
| 405 | EntryFreq.getFrequency()); |
| 406 | |
| 407 | if (hasProfileData(F, OI)) |
| 408 | return OutlineRegionRelFreq; |
| 409 | |
| 410 | // When profile data is not available, we need to be very |
| 411 | // conservative in estimating the overall savings. We need to make sure |
| 412 | // the outline region relative frequency is not below the threshold |
| 413 | // specified by the option. |
| 414 | OutlineRegionRelFreq = std::max(OutlineRegionRelFreq, BranchProbability(OutlineRegionFreqPercent, 100)); |
| 415 | |
| 416 | return OutlineRegionRelFreq; |
| 417 | } |
| 418 | |
| 419 | bool PartialInlinerImpl::shouldPartialInline( |
| 420 | CallSite CS, Function *F /* Original Callee */, FunctionOutliningInfo *OI, |
| 421 | BlockFrequencyInfo *CalleeBFI, BasicBlock *OutliningCallBB, |
| 422 | int NonWeightedOutliningRcost, OptimizationRemarkEmitter &ORE) { |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 423 | using namespace ore; |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 424 | if (SkipCostAnalysis) |
| 425 | return true; |
| 426 | |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 427 | Instruction *Call = CS.getInstruction(); |
| 428 | Function *Callee = CS.getCalledFunction(); |
| 429 | Function *Caller = CS.getCaller(); |
| 430 | auto &CalleeTTI = (*GetTTI)(*Callee); |
| 431 | InlineCost IC = getInlineCost(CS, getInlineParams(), CalleeTTI, |
| 432 | *GetAssumptionCache, GetBFI, PSI); |
| 433 | |
| 434 | if (IC.isAlways()) { |
| 435 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "AlwaysInline", Call) |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 436 | << NV("Callee", F) |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 437 | << " should always be fully inlined, not partially"); |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | if (IC.isNever()) { |
| 442 | ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call) |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 443 | << NV("Callee", F) << " not partially inlined into " |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 444 | << NV("Caller", Caller) |
| 445 | << " because it should never be inlined (cost=never)"); |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | if (!IC) { |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 450 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "TooCostly", Call) |
| 451 | << NV("Callee", F) << " not partially inlined into " |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 452 | << NV("Caller", Caller) << " because too costly to inline (cost=" |
| 453 | << NV("Cost", IC.getCost()) << ", threshold=" |
| 454 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
| 455 | return false; |
| 456 | } |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 457 | const DataLayout &DL = Caller->getParent()->getDataLayout(); |
| 458 | // The savings of eliminating the call: |
| 459 | int NonWeightedSavings = getCallsiteCost(CS, DL); |
| 460 | BlockFrequency NormWeightedSavings(NonWeightedSavings); |
| 461 | |
| 462 | auto RelativeFreq = |
| 463 | getOutliningCallBBRelativeFreq(F, OI, Callee, CalleeBFI, OutliningCallBB); |
| 464 | auto NormWeightedRcost = |
| 465 | BlockFrequency(NonWeightedOutliningRcost) * RelativeFreq; |
| 466 | |
| 467 | // Weighted saving is smaller than weighted cost, return false |
| 468 | if (NormWeightedSavings < NormWeightedRcost) { |
| 469 | ORE.emit( |
| 470 | OptimizationRemarkAnalysis(DEBUG_TYPE, "OutliningCallcostTooHigh", Call) |
| 471 | << NV("Callee", F) << " not partially inlined into " |
| 472 | << NV("Caller", Caller) << " runtime overhead (overhead=" |
| 473 | << NV("Overhead", (unsigned)NormWeightedRcost.getFrequency()) |
| 474 | << ", savings=" |
| 475 | << NV("Savings", (unsigned)NormWeightedSavings.getFrequency()) << ")" |
| 476 | << " of making the outlined call is too high"); |
| 477 | |
| 478 | return false; |
| 479 | } |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 480 | |
| 481 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "CanBePartiallyInlined", Call) |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 482 | << NV("Callee", F) << " can be partially inlined into " |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 483 | << NV("Caller", Caller) << " with cost=" << NV("Cost", IC.getCost()) |
| 484 | << " (threshold=" |
| 485 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"); |
| 486 | return true; |
| 487 | } |
| 488 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 489 | // TODO: Ideally we should share Inliner's InlineCost Analysis code. |
| 490 | // For now use a simplified version. The returned 'InlineCost' will be used |
| 491 | // to esimate the size cost as well as runtime cost of the BB. |
| 492 | int PartialInlinerImpl::computeBBInlineCost(BasicBlock *BB) { |
| 493 | int InlineCost = 0; |
| 494 | const DataLayout &DL = BB->getParent()->getParent()->getDataLayout(); |
| 495 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 496 | if (isa<DbgInfoIntrinsic>(I)) |
| 497 | continue; |
| 498 | |
| 499 | if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 500 | InlineCost += getCallsiteCost(CallSite(CI), DL); |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) { |
| 505 | InlineCost += getCallsiteCost(CallSite(II), DL); |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { |
| 510 | InlineCost += (SI->getNumCases() + 1) * InlineConstants::InstrCost; |
| 511 | continue; |
| 512 | } |
| 513 | InlineCost += InlineConstants::InstrCost; |
| 514 | } |
| 515 | return InlineCost; |
| 516 | } |
| 517 | |
| 518 | std::tuple<int, int, int> PartialInlinerImpl::computeOutliningCosts( |
| 519 | Function *F, const FunctionOutliningInfo *OI, Function *OutlinedFunction, |
| 520 | BasicBlock *OutliningCallBB) { |
| 521 | // First compute the cost of the outlined region 'OI' in the original |
| 522 | // function 'F': |
| 523 | int OutlinedRegionCost = 0; |
| 524 | for (BasicBlock &BB : *F) { |
| 525 | if (&BB != OI->ReturnBlock && |
| 526 | // Assuming Entry set is small -- do a linear search here: |
| 527 | std::find(OI->Entries.begin(), OI->Entries.end(), &BB) == |
| 528 | OI->Entries.end()) { |
| 529 | OutlinedRegionCost += computeBBInlineCost(&BB); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Now compute the cost of the call sequence to the outlined function |
| 534 | // 'OutlinedFunction' in BB 'OutliningCallBB': |
| 535 | int OutliningFuncCallCost = computeBBInlineCost(OutliningCallBB); |
| 536 | |
| 537 | // Now compute the cost of the extracted/outlined function itself: |
| 538 | int OutlinedFunctionCost = 0; |
| 539 | for (BasicBlock &BB : *OutlinedFunction) { |
| 540 | OutlinedFunctionCost += computeBBInlineCost(&BB); |
| 541 | } |
| 542 | |
| 543 | assert(OutlinedFunctionCost >= OutlinedRegionCost && |
| 544 | "Outlined function cost should be no less than the outlined region"); |
| 545 | int OutliningRuntimeOverhead = |
| 546 | OutliningFuncCallCost + (OutlinedFunctionCost - OutlinedRegionCost); |
| 547 | |
| 548 | return std::make_tuple(OutliningFuncCallCost, OutliningRuntimeOverhead, |
| 549 | OutlinedRegionCost); |
| 550 | } |
| 551 | |
| 552 | // Create the callsite to profile count map which is |
| 553 | // used to update the original function's entry count, |
| 554 | // after the function is partially inlined into the callsite. |
| 555 | void PartialInlinerImpl::computeCallsiteToProfCountMap( |
| 556 | Function *DuplicateFunction, |
| 557 | DenseMap<User *, uint64_t> &CallSiteToProfCountMap) { |
| 558 | std::vector<User *> Users(DuplicateFunction->user_begin(), |
| 559 | DuplicateFunction->user_end()); |
| 560 | Function *CurrentCaller = nullptr; |
| 561 | BlockFrequencyInfo *CurrentCallerBFI = nullptr; |
| 562 | |
| 563 | auto ComputeCurrBFI = [&,this](Function *Caller) { |
| 564 | // For the old pass manager: |
| 565 | if (!GetBFI) { |
| 566 | if (CurrentCallerBFI) |
| 567 | delete CurrentCallerBFI; |
| 568 | DominatorTree DT(*Caller); |
| 569 | LoopInfo LI(DT); |
| 570 | BranchProbabilityInfo BPI(*Caller, LI); |
| 571 | CurrentCallerBFI = new BlockFrequencyInfo(*Caller, BPI, LI); |
| 572 | } else { |
| 573 | // New pass manager: |
| 574 | CurrentCallerBFI = &(*GetBFI)(*Caller); |
| 575 | } |
| 576 | }; |
| 577 | |
| 578 | for (User *User : Users) { |
| 579 | CallSite CS = getCallSite(User); |
| 580 | Function *Caller = CS.getCaller(); |
| 581 | if (CurrentCaller != Caller) { |
| 582 | CurrentCaller = Caller; |
| 583 | ComputeCurrBFI(Caller); |
| 584 | } else { |
| 585 | assert(CurrentCallerBFI && "CallerBFI is not set"); |
| 586 | } |
| 587 | BasicBlock *CallBB = CS.getInstruction()->getParent(); |
| 588 | auto Count = CurrentCallerBFI->getBlockProfileCount(CallBB); |
| 589 | if (Count) |
| 590 | CallSiteToProfCountMap[User] = *Count; |
| 591 | else |
| 592 | CallSiteToProfCountMap[User] = 0; |
| 593 | } |
| 594 | } |
| 595 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 596 | Function *PartialInlinerImpl::unswitchFunction(Function *F) { |
| 597 | |
| 598 | if (F->hasAddressTaken()) |
| 599 | return nullptr; |
| 600 | |
Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame] | 601 | // Let inliner handle it |
| 602 | if (F->hasFnAttribute(Attribute::AlwaysInline)) |
| 603 | return nullptr; |
| 604 | |
| 605 | if (F->hasFnAttribute(Attribute::NoInline)) |
| 606 | return nullptr; |
| 607 | |
| 608 | if (PSI->isFunctionEntryCold(F)) |
| 609 | return nullptr; |
| 610 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 611 | if (F->user_begin() == F->user_end()) |
| 612 | return nullptr; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 613 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 614 | std::unique_ptr<FunctionOutliningInfo> OI = computeOutliningInfo(F); |
| 615 | |
| 616 | if (!OI) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 617 | return nullptr; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 618 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 619 | // Clone the function, so that we can hack away on it. |
Rafael Espindola | 229e38f | 2010-10-13 01:36:30 +0000 | [diff] [blame] | 620 | ValueToValueMapTy VMap; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 621 | Function *DuplicateFunction = CloneFunction(F, VMap); |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 622 | BasicBlock *NewReturnBlock = cast<BasicBlock>(VMap[OI->ReturnBlock]); |
| 623 | BasicBlock *NewNonReturnBlock = cast<BasicBlock>(VMap[OI->NonReturnBlock]); |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 624 | DenseSet<BasicBlock *> NewEntries; |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 625 | for (BasicBlock *BB : OI->Entries) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 626 | NewEntries.insert(cast<BasicBlock>(VMap[BB])); |
| 627 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 628 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 629 | // Go ahead and update all uses to the duplicate, so that we can just |
| 630 | // use the inliner functionality when we're done hacking. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 631 | F->replaceAllUsesWith(DuplicateFunction); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 632 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 633 | auto getFirstPHI = [](BasicBlock *BB) { |
| 634 | BasicBlock::iterator I = BB->begin(); |
| 635 | PHINode *FirstPhi = nullptr; |
| 636 | while (I != BB->end()) { |
| 637 | PHINode *Phi = dyn_cast<PHINode>(I); |
| 638 | if (!Phi) |
| 639 | break; |
| 640 | if (!FirstPhi) { |
| 641 | FirstPhi = Phi; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | return FirstPhi; |
| 646 | }; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 647 | // Special hackery is needed with PHI nodes that have inputs from more than |
| 648 | // one extracted block. For simplicity, just split the PHIs into a two-level |
| 649 | // sequence of PHIs, some of which will go in the extracted region, and some |
| 650 | // of which will go outside. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 651 | BasicBlock *PreReturn = NewReturnBlock; |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 652 | // only split block when necessary: |
| 653 | PHINode *FirstPhi = getFirstPHI(PreReturn); |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 654 | unsigned NumPredsFromEntries = OI->ReturnBlockPreds.size(); |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 655 | if (FirstPhi && FirstPhi->getNumIncomingValues() > NumPredsFromEntries + 1) { |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 656 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 657 | NewReturnBlock = NewReturnBlock->splitBasicBlock( |
| 658 | NewReturnBlock->getFirstNonPHI()->getIterator()); |
| 659 | BasicBlock::iterator I = PreReturn->begin(); |
| 660 | Instruction *Ins = &NewReturnBlock->front(); |
| 661 | while (I != PreReturn->end()) { |
| 662 | PHINode *OldPhi = dyn_cast<PHINode>(I); |
| 663 | if (!OldPhi) |
| 664 | break; |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 665 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 666 | PHINode *RetPhi = |
| 667 | PHINode::Create(OldPhi->getType(), NumPredsFromEntries + 1, "", Ins); |
| 668 | OldPhi->replaceAllUsesWith(RetPhi); |
| 669 | Ins = NewReturnBlock->getFirstNonPHI(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 670 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 671 | RetPhi->addIncoming(&*I, PreReturn); |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 672 | for (BasicBlock *E : OI->ReturnBlockPreds) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 673 | BasicBlock *NewE = cast<BasicBlock>(VMap[E]); |
| 674 | RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(NewE), NewE); |
| 675 | OldPhi->removeIncomingValue(NewE); |
| 676 | } |
| 677 | ++I; |
| 678 | } |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 679 | for (auto E : OI->ReturnBlockPreds) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 680 | BasicBlock *NewE = cast<BasicBlock>(VMap[E]); |
| 681 | NewE->getTerminator()->replaceUsesOfWith(PreReturn, NewReturnBlock); |
| 682 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 683 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 684 | |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 685 | // Returns true if the block is to be partial inlined into the caller |
| 686 | // (i.e. not to be extracted to the out of line function) |
Davide Italiano | aa42a10 | 2017-05-08 20:44:01 +0000 | [diff] [blame] | 687 | auto ToBeInlined = [&](BasicBlock *BB) { |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 688 | return BB == NewReturnBlock || NewEntries.count(BB); |
| 689 | }; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 690 | // Gather up the blocks that we're going to extract. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 691 | std::vector<BasicBlock *> ToExtract; |
| 692 | ToExtract.push_back(NewNonReturnBlock); |
| 693 | for (BasicBlock &BB : *DuplicateFunction) |
Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 694 | if (!ToBeInlined(&BB) && &BB != NewNonReturnBlock) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 695 | ToExtract.push_back(&BB); |
Duncan P. N. Exon Smith | 1732340 | 2015-10-13 17:51:03 +0000 | [diff] [blame] | 696 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 697 | // The CodeExtractor needs a dominator tree. |
| 698 | DominatorTree DT; |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 699 | DT.recalculate(*DuplicateFunction); |
Chandler Carruth | 7352302 | 2014-01-13 13:07:17 +0000 | [diff] [blame] | 700 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 701 | // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo. |
| 702 | LoopInfo LI(DT); |
| 703 | BranchProbabilityInfo BPI(*DuplicateFunction, LI); |
| 704 | BlockFrequencyInfo BFI(*DuplicateFunction, BPI, LI); |
| 705 | |
Dan Gohman | 4a61882 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 706 | // Extract the body of the if. |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 707 | Function *OutlinedFunction = |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 708 | CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, &BFI, &BPI) |
| 709 | .extractCodeRegion(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 710 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 711 | bool AnyInline = |
| 712 | tryPartialInline(DuplicateFunction, F, OI.get(), OutlinedFunction, &BFI); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 713 | |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 714 | // Ditch the duplicate, since we're done with it, and rewrite all remaining |
| 715 | // users (function pointers, etc.) back to the original function. |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 716 | DuplicateFunction->replaceAllUsesWith(F); |
| 717 | DuplicateFunction->eraseFromParent(); |
Xinliang David Li | 392e975 | 2017-05-14 02:54:02 +0000 | [diff] [blame^] | 718 | |
| 719 | if (AnyInline) |
| 720 | return OutlinedFunction; |
| 721 | |
| 722 | // Remove the function that is speculatively created: |
| 723 | if (OutlinedFunction) |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 724 | OutlinedFunction->eraseFromParent(); |
Xinliang David Li | 392e975 | 2017-05-14 02:54:02 +0000 | [diff] [blame^] | 725 | |
| 726 | return nullptr; |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 727 | } |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 728 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 729 | bool PartialInlinerImpl::tryPartialInline(Function *DuplicateFunction, |
| 730 | Function *F, |
| 731 | FunctionOutliningInfo *OI, |
| 732 | Function *OutlinedFunction, |
| 733 | BlockFrequencyInfo *CalleeBFI) { |
| 734 | if (OutlinedFunction == nullptr) |
| 735 | return false; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 736 | |
Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 737 | int NonWeightedRcost; |
| 738 | int SizeCost; |
| 739 | int OutlinedRegionSizeCost; |
| 740 | |
| 741 | auto OutliningCallBB = |
| 742 | getOneCallSiteTo(OutlinedFunction).getInstruction()->getParent(); |
| 743 | |
| 744 | std::tie(SizeCost, NonWeightedRcost, OutlinedRegionSizeCost) = |
| 745 | computeOutliningCosts(F, OI, OutlinedFunction, OutliningCallBB); |
| 746 | |
| 747 | // The call sequence to the outlined function is larger than the original |
| 748 | // outlined region size, it does not increase the chances of inlining |
| 749 | // 'F' with outlining (The inliner usies the size increase to model the |
| 750 | // the cost of inlining a callee). |
| 751 | if (!SkipCostAnalysis && OutlinedRegionSizeCost < SizeCost) { |
| 752 | OptimizationRemarkEmitter ORE(F); |
| 753 | DebugLoc DLoc; |
| 754 | BasicBlock *Block; |
| 755 | std::tie(DLoc, Block) = getOneDebugLoc(DuplicateFunction); |
| 756 | ORE.emit(OptimizationRemarkAnalysis(DEBUG_TYPE, "OutlineRegionTooSmall", |
| 757 | DLoc, Block) |
| 758 | << ore::NV("Function", F) |
| 759 | << " not partially inlined into callers (Original Size = " |
| 760 | << ore::NV("OutlinedRegionOriginalSize", OutlinedRegionSizeCost) |
| 761 | << ", Size of call sequence to outlined function = " |
| 762 | << ore::NV("NewSize", SizeCost) << ")"); |
| 763 | return false; |
| 764 | } |
| 765 | |
| 766 | assert(F->user_begin() == F->user_end() && |
| 767 | "F's users should all be replaced!"); |
| 768 | std::vector<User *> Users(DuplicateFunction->user_begin(), |
| 769 | DuplicateFunction->user_end()); |
| 770 | |
| 771 | DenseMap<User *, uint64_t> CallSiteToProfCountMap; |
| 772 | if (F->getEntryCount()) |
| 773 | computeCallsiteToProfCountMap(DuplicateFunction, CallSiteToProfCountMap); |
| 774 | |
| 775 | auto CalleeEntryCount = F->getEntryCount(); |
| 776 | uint64_t CalleeEntryCountV = (CalleeEntryCount ? *CalleeEntryCount : 0); |
| 777 | bool AnyInline = false; |
| 778 | for (User *User : Users) { |
| 779 | CallSite CS = getCallSite(User); |
| 780 | |
| 781 | if (IsLimitReached()) |
| 782 | continue; |
| 783 | |
| 784 | OptimizationRemarkEmitter ORE(CS.getCaller()); |
| 785 | |
| 786 | if (!shouldPartialInline(CS, F, OI, CalleeBFI, OutliningCallBB, |
| 787 | NonWeightedRcost, ORE)) |
| 788 | continue; |
| 789 | |
| 790 | ORE.emit( |
| 791 | OptimizationRemark(DEBUG_TYPE, "PartiallyInlined", CS.getInstruction()) |
| 792 | << ore::NV("Callee", F) << " partially inlined into " |
| 793 | << ore::NV("Caller", CS.getCaller())); |
| 794 | |
| 795 | InlineFunctionInfo IFI(nullptr, GetAssumptionCache, PSI); |
| 796 | InlineFunction(CS, IFI); |
| 797 | |
| 798 | // Now update the entry count: |
| 799 | if (CalleeEntryCountV && CallSiteToProfCountMap.count(User)) { |
| 800 | uint64_t CallSiteCount = CallSiteToProfCountMap[User]; |
| 801 | CalleeEntryCountV -= std::min(CalleeEntryCountV, CallSiteCount); |
| 802 | } |
| 803 | |
| 804 | AnyInline = true; |
| 805 | NumPartialInlining++; |
| 806 | // Update the stats |
| 807 | NumPartialInlined++; |
| 808 | } |
| 809 | |
| 810 | if (AnyInline && CalleeEntryCount) |
| 811 | F->setEntryCount(CalleeEntryCountV); |
| 812 | |
| 813 | return AnyInline; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 814 | } |
| 815 | |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 816 | bool PartialInlinerImpl::run(Module &M) { |
Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 817 | if (DisablePartialInlining) |
| 818 | return false; |
| 819 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 820 | std::vector<Function *> Worklist; |
| 821 | Worklist.reserve(M.size()); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 822 | for (Function &F : M) |
| 823 | if (!F.use_empty() && !F.isDeclaration()) |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 824 | Worklist.push_back(&F); |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 825 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 826 | bool Changed = false; |
| 827 | while (!Worklist.empty()) { |
| 828 | Function *CurrFunc = Worklist.back(); |
| 829 | Worklist.pop_back(); |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 830 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 831 | if (CurrFunc->use_empty()) |
| 832 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 833 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 834 | bool Recursive = false; |
| 835 | for (User *U : CurrFunc->users()) |
| 836 | if (Instruction *I = dyn_cast<Instruction>(U)) |
| 837 | if (I->getParent()->getParent() == CurrFunc) { |
| 838 | Recursive = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 839 | break; |
| 840 | } |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 841 | if (Recursive) |
| 842 | continue; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 843 | |
Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 844 | if (Function *NewFunc = unswitchFunction(CurrFunc)) { |
| 845 | Worklist.push_back(NewFunc); |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 846 | Changed = true; |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 847 | } |
Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 848 | } |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 849 | |
Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 850 | return Changed; |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | char PartialInlinerLegacyPass::ID = 0; |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 854 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", |
| 855 | "Partial Inliner", false, false) |
| 856 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 857 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| 858 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 859 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", |
| 860 | "Partial Inliner", false, false) |
Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 861 | |
| 862 | ModulePass *llvm::createPartialInliningPass() { |
| 863 | return new PartialInlinerLegacyPass(); |
| 864 | } |
| 865 | |
| 866 | PreservedAnalyses PartialInlinerPass::run(Module &M, |
| 867 | ModuleAnalysisManager &AM) { |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 868 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 869 | |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 870 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 871 | [&FAM](Function &F) -> AssumptionCache & { |
| 872 | return FAM.getResult<AssumptionAnalysis>(F); |
| 873 | }; |
Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 874 | |
| 875 | std::function<BlockFrequencyInfo &(Function &)> GetBFI = |
| 876 | [&FAM](Function &F) -> BlockFrequencyInfo & { |
| 877 | return FAM.getResult<BlockFrequencyAnalysis>(F); |
| 878 | }; |
| 879 | |
| 880 | std::function<TargetTransformInfo &(Function &)> GetTTI = |
| 881 | [&FAM](Function &F) -> TargetTransformInfo & { |
| 882 | return FAM.getResult<TargetIRAnalysis>(F); |
| 883 | }; |
| 884 | |
| 885 | ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M); |
| 886 | |
| 887 | if (PartialInlinerImpl(&GetAssumptionCache, &GetTTI, {GetBFI}, PSI).run(M)) |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 888 | return PreservedAnalyses::none(); |
| 889 | return PreservedAnalyses::all(); |
Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 890 | } |