| 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" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" | 
|  | 17 | #include "llvm/ADT/DenseSet.h" | 
|  | 18 | #include "llvm/ADT/None.h" | 
|  | 19 | #include "llvm/ADT/Optional.h" | 
|  | 20 | #include "llvm/ADT/STLExtras.h" | 
|  | 21 | #include "llvm/ADT/SmallVector.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" | 
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/BlockFrequencyInfo.h" | 
|  | 24 | #include "llvm/Analysis/BranchProbabilityInfo.h" | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/InlineCost.h" | 
| Sean Silva | f801575 | 2016-08-02 02:15:45 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/LoopInfo.h" | 
| Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/ProfileSummaryInfo.h" | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetLibraryInfo.h" | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/TargetTransformInfo.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Attributes.h" | 
|  | 32 | #include "llvm/IR/BasicBlock.h" | 
| Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 33 | #include "llvm/IR/CFG.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 34 | #include "llvm/IR/CallSite.h" | 
|  | 35 | #include "llvm/IR/DebugLoc.h" | 
| Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 36 | #include "llvm/IR/DiagnosticInfo.h" | 
| Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Dominators.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 38 | #include "llvm/IR/Function.h" | 
|  | 39 | #include "llvm/IR/InstrTypes.h" | 
|  | 40 | #include "llvm/IR/Instruction.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 41 | #include "llvm/IR/Instructions.h" | 
| Reid Kleckner | 0e8c4bb | 2017-09-07 23:27:44 +0000 | [diff] [blame] | 42 | #include "llvm/IR/IntrinsicInst.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Intrinsics.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Module.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 45 | #include "llvm/IR/User.h" | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 46 | #include "llvm/Pass.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 47 | #include "llvm/Support/BlockFrequency.h" | 
|  | 48 | #include "llvm/Support/BranchProbability.h" | 
|  | 49 | #include "llvm/Support/Casting.h" | 
|  | 50 | #include "llvm/Support/CommandLine.h" | 
|  | 51 | #include "llvm/Support/ErrorHandling.h" | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 52 | #include "llvm/Transforms/IPO.h" | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 53 | #include "llvm/Transforms/Utils/Cloning.h" | 
| Chandler Carruth | 0fde001 | 2012-05-04 10:18:49 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Utils/CodeExtractor.h" | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 55 | #include "llvm/Transforms/Utils/ValueMapper.h" | 
|  | 56 | #include <algorithm> | 
|  | 57 | #include <cassert> | 
|  | 58 | #include <cstdint> | 
|  | 59 | #include <functional> | 
|  | 60 | #include <iterator> | 
|  | 61 | #include <memory> | 
|  | 62 | #include <tuple> | 
|  | 63 | #include <vector> | 
|  | 64 |  | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 65 | using namespace llvm; | 
|  | 66 |  | 
| Xinliang David Li | 15744ad | 2017-04-23 21:40:58 +0000 | [diff] [blame] | 67 | #define DEBUG_TYPE "partial-inlining" | 
| Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 68 |  | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 69 | STATISTIC(NumPartialInlined, | 
|  | 70 | "Number of callsites functions partially inlined into."); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 71 | STATISTIC(NumColdOutlinePartialInlined, "Number of times functions with " | 
|  | 72 | "cold outlined regions were partially " | 
|  | 73 | "inlined into its caller(s)."); | 
|  | 74 | STATISTIC(NumColdRegionsFound, | 
|  | 75 | "Number of cold single entry/exit regions found."); | 
|  | 76 | STATISTIC(NumColdRegionsOutlined, | 
|  | 77 | "Number of cold single entry/exit regions outlined."); | 
| Owen Anderson | bd6a213 | 2009-06-15 20:50:26 +0000 | [diff] [blame] | 78 |  | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 79 | // Command line option to disable partial-inlining. The default is false: | 
|  | 80 | static cl::opt<bool> | 
|  | 81 | DisablePartialInlining("disable-partial-inlining", cl::init(false), | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 82 | cl::Hidden, cl::desc("Disable partial inlining")); | 
|  | 83 | // Command line option to disable multi-region partial-inlining. The default is | 
|  | 84 | // false: | 
|  | 85 | static cl::opt<bool> DisableMultiRegionPartialInline( | 
|  | 86 | "disable-mr-partial-inlining", cl::init(false), cl::Hidden, | 
|  | 87 | cl::desc("Disable multi-region partial inlining")); | 
|  | 88 |  | 
|  | 89 | // Command line option to force outlining in regions with live exit variables. | 
|  | 90 | // The default is false: | 
|  | 91 | static cl::opt<bool> | 
|  | 92 | ForceLiveExit("pi-force-live-exit-outline", cl::init(false), cl::Hidden, | 
|  | 93 | cl::desc("Force outline regions with live exits")); | 
|  | 94 |  | 
|  | 95 | // Command line option to enable marking outline functions with Cold Calling | 
|  | 96 | // Convention. The default is false: | 
|  | 97 | static cl::opt<bool> | 
|  | 98 | MarkOutlinedColdCC("pi-mark-coldcc", cl::init(false), cl::Hidden, | 
|  | 99 | cl::desc("Mark outline function calls with ColdCC")); | 
|  | 100 |  | 
|  | 101 | #ifndef NDEBUG | 
|  | 102 | // Command line option to debug partial-inlining. The default is none: | 
|  | 103 | static cl::opt<bool> TracePartialInlining("trace-partial-inlining", | 
|  | 104 | cl::init(false), cl::Hidden, | 
|  | 105 | cl::desc("Trace partial inlining.")); | 
|  | 106 | #endif | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 107 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 108 | // This is an option used by testing: | 
|  | 109 | static cl::opt<bool> SkipCostAnalysis("skip-partial-inlining-cost-analysis", | 
|  | 110 | cl::init(false), cl::ZeroOrMore, | 
|  | 111 | cl::ReallyHidden, | 
|  | 112 | cl::desc("Skip Cost Analysis")); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 113 | // Used to determine if a cold region is worth outlining based on | 
|  | 114 | // its inlining cost compared to the original function.  Default is set at 10%. | 
|  | 115 | // ie. if the cold region reduces the inlining cost of the original function by | 
|  | 116 | // at least 10%. | 
|  | 117 | static cl::opt<float> MinRegionSizeRatio( | 
|  | 118 | "min-region-size-ratio", cl::init(0.1), cl::Hidden, | 
|  | 119 | cl::desc("Minimum ratio comparing relative sizes of each " | 
|  | 120 | "outline candidate and original function")); | 
|  | 121 | // Used to tune the minimum number of execution counts needed in the predecessor | 
|  | 122 | // block to the cold edge. ie. confidence interval. | 
|  | 123 | static cl::opt<unsigned> | 
|  | 124 | MinBlockCounterExecution("min-block-execution", cl::init(100), cl::Hidden, | 
|  | 125 | cl::desc("Minimum block executions to consider " | 
|  | 126 | "its BranchProbabilityInfo valid")); | 
|  | 127 | // Used to determine when an edge is considered cold. Default is set to 10%. ie. | 
|  | 128 | // if the branch probability is 10% or less, then it is deemed as 'cold'. | 
|  | 129 | static cl::opt<float> ColdBranchRatio( | 
|  | 130 | "cold-branch-ratio", cl::init(0.1), cl::Hidden, | 
|  | 131 | cl::desc("Minimum BranchProbability to consider a region cold.")); | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 132 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 133 | static cl::opt<unsigned> MaxNumInlineBlocks( | 
|  | 134 | "max-num-inline-blocks", cl::init(5), cl::Hidden, | 
| Chad Rosier | f98335e | 2017-08-24 21:21:09 +0000 | [diff] [blame] | 135 | cl::desc("Max number of blocks to be partially inlined")); | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 136 |  | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 137 | // Command line option to set the maximum number of partial inlining allowed | 
|  | 138 | // for the module. The default value of -1 means no limit. | 
|  | 139 | static cl::opt<int> MaxNumPartialInlining( | 
|  | 140 | "max-partial-inlining", cl::init(-1), cl::Hidden, cl::ZeroOrMore, | 
|  | 141 | cl::desc("Max number of partial inlining. The default is unlimited")); | 
|  | 142 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 143 | // Used only when PGO or user annotated branch data is absent. It is | 
|  | 144 | // the least value that is used to weigh the outline region. If BFI | 
|  | 145 | // produces larger value, the BFI value will be used. | 
|  | 146 | static cl::opt<int> | 
|  | 147 | OutlineRegionFreqPercent("outline-region-freq-percent", cl::init(75), | 
|  | 148 | cl::Hidden, cl::ZeroOrMore, | 
|  | 149 | cl::desc("Relative frequency of outline region to " | 
|  | 150 | "the entry block")); | 
|  | 151 |  | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 152 | static cl::opt<unsigned> ExtraOutliningPenalty( | 
|  | 153 | "partial-inlining-extra-penalty", cl::init(0), cl::Hidden, | 
|  | 154 | cl::desc("A debug option to add additional penalty to the computed one.")); | 
|  | 155 |  | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 156 | namespace { | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 157 |  | 
|  | 158 | struct FunctionOutliningInfo { | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 159 | FunctionOutliningInfo() = default; | 
|  | 160 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 161 | // Returns the number of blocks to be inlined including all blocks | 
|  | 162 | // in Entries and one return block. | 
|  | 163 | unsigned GetNumInlinedBlocks() const { return Entries.size() + 1; } | 
|  | 164 |  | 
|  | 165 | // A set of blocks including the function entry that guard | 
|  | 166 | // the region to be outlined. | 
|  | 167 | SmallVector<BasicBlock *, 4> Entries; | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 168 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 169 | // The return block that is not included in the outlined region. | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 170 | BasicBlock *ReturnBlock = nullptr; | 
|  | 171 |  | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 172 | // The dominating block of the region to be outlined. | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 173 | BasicBlock *NonReturnBlock = nullptr; | 
|  | 174 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 175 | // The set of blocks in Entries that that are predecessors to ReturnBlock | 
|  | 176 | SmallVector<BasicBlock *, 4> ReturnBlockPreds; | 
|  | 177 | }; | 
|  | 178 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 179 | struct FunctionOutliningMultiRegionInfo { | 
|  | 180 | FunctionOutliningMultiRegionInfo() | 
|  | 181 | : ORI() {} | 
|  | 182 |  | 
|  | 183 | // Container for outline regions | 
|  | 184 | struct OutlineRegionInfo { | 
|  | 185 | OutlineRegionInfo(SmallVector<BasicBlock *, 8> Region, | 
|  | 186 | BasicBlock *EntryBlock, BasicBlock *ExitBlock, | 
|  | 187 | BasicBlock *ReturnBlock) | 
|  | 188 | : Region(Region), EntryBlock(EntryBlock), ExitBlock(ExitBlock), | 
|  | 189 | ReturnBlock(ReturnBlock) {} | 
|  | 190 | SmallVector<BasicBlock *, 8> Region; | 
|  | 191 | BasicBlock *EntryBlock; | 
|  | 192 | BasicBlock *ExitBlock; | 
|  | 193 | BasicBlock *ReturnBlock; | 
|  | 194 | }; | 
|  | 195 |  | 
|  | 196 | SmallVector<OutlineRegionInfo, 4> ORI; | 
|  | 197 | }; | 
|  | 198 |  | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 199 | struct PartialInlinerImpl { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 200 |  | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 201 | PartialInlinerImpl( | 
|  | 202 | std::function<AssumptionCache &(Function &)> *GetAC, | 
|  | 203 | std::function<TargetTransformInfo &(Function &)> *GTTI, | 
|  | 204 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GBFI, | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 205 | ProfileSummaryInfo *ProfSI) | 
|  | 206 | : GetAssumptionCache(GetAC), GetTTI(GTTI), GetBFI(GBFI), PSI(ProfSI) {} | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 207 |  | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 208 | bool run(Module &M); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 209 | // Main part of the transformation that calls helper functions to find | 
|  | 210 | // outlining candidates, clone & outline the function, and attempt to | 
|  | 211 | // partially inline the resulting function. Returns true if | 
|  | 212 | // inlining was successful, false otherwise.  Also returns the outline | 
|  | 213 | // function (only if we partially inlined early returns) as there is a | 
|  | 214 | // possibility to further "peel" early return statements that were left in the | 
|  | 215 | // outline function due to code size. | 
|  | 216 | std::pair<bool, Function *> unswitchFunction(Function *F); | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 217 |  | 
| Hiroshi Inoue | d24ddcd | 2018-01-19 10:55:29 +0000 | [diff] [blame] | 218 | // This class speculatively clones the function to be partial inlined. | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 219 | // At the end of partial inlining, the remaining callsites to the cloned | 
|  | 220 | // function that are not partially inlined will be fixed up to reference | 
|  | 221 | // the original function, and the cloned function will be erased. | 
|  | 222 | struct FunctionCloner { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 223 | // Two constructors, one for single region outlining, the other for | 
|  | 224 | // multi-region outlining. | 
|  | 225 | FunctionCloner(Function *F, FunctionOutliningInfo *OI, | 
|  | 226 | OptimizationRemarkEmitter &ORE); | 
|  | 227 | FunctionCloner(Function *F, FunctionOutliningMultiRegionInfo *OMRI, | 
|  | 228 | OptimizationRemarkEmitter &ORE); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 229 | ~FunctionCloner(); | 
|  | 230 |  | 
|  | 231 | // Prepare for function outlining: making sure there is only | 
|  | 232 | // one incoming edge from the extracted/outlined region to | 
|  | 233 | // the return block. | 
|  | 234 | void NormalizeReturnBlock(); | 
|  | 235 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 236 | // Do function outlining for cold regions. | 
|  | 237 | bool doMultiRegionFunctionOutlining(); | 
|  | 238 | // Do function outlining for region after early return block(s). | 
| Florian Hahn | 0e9dec6 | 2017-11-13 10:35:52 +0000 | [diff] [blame] | 239 | // NOTE: For vararg functions that do the vararg handling in the outlined | 
|  | 240 | //       function, we temporarily generate IR that does not properly | 
|  | 241 | //       forward varargs to the outlined function. Calling InlineFunction | 
|  | 242 | //       will update calls to the outlined functions to properly forward | 
|  | 243 | //       the varargs. | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 244 | Function *doSingleRegionFunctionOutlining(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 245 |  | 
|  | 246 | Function *OrigFunc = nullptr; | 
|  | 247 | Function *ClonedFunc = nullptr; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 248 |  | 
|  | 249 | typedef std::pair<Function *, BasicBlock *> FuncBodyCallerPair; | 
|  | 250 | // Keep track of Outlined Functions and the basic block they're called from. | 
|  | 251 | SmallVector<FuncBodyCallerPair, 4> OutlinedFunctions; | 
|  | 252 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 253 | // ClonedFunc is inlined in one of its callers after function | 
|  | 254 | // outlining. | 
|  | 255 | bool IsFunctionInlined = false; | 
|  | 256 | // The cost of the region to be outlined. | 
|  | 257 | int OutlinedRegionCost = 0; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 258 | // ClonedOI is specific to outlining non-early return blocks. | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 259 | std::unique_ptr<FunctionOutliningInfo> ClonedOI = nullptr; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 260 | // ClonedOMRI is specific to outlining cold regions. | 
|  | 261 | std::unique_ptr<FunctionOutliningMultiRegionInfo> ClonedOMRI = nullptr; | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 262 | std::unique_ptr<BlockFrequencyInfo> ClonedFuncBFI = nullptr; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 263 | OptimizationRemarkEmitter &ORE; | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 264 | }; | 
|  | 265 |  | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 266 | private: | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 267 | int NumPartialInlining = 0; | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 268 | std::function<AssumptionCache &(Function &)> *GetAssumptionCache; | 
|  | 269 | std::function<TargetTransformInfo &(Function &)> *GetTTI; | 
|  | 270 | Optional<function_ref<BlockFrequencyInfo &(Function &)>> GetBFI; | 
|  | 271 | ProfileSummaryInfo *PSI; | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 272 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 273 | // Return the frequency of the OutlininingBB relative to F's entry point. | 
|  | 274 | // The result is no larger than 1 and is represented using BP. | 
|  | 275 | // (Note that the outlined region's 'head' block can only have incoming | 
|  | 276 | // edges from the guarding entry blocks). | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 277 | BranchProbability getOutliningCallBBRelativeFreq(FunctionCloner &Cloner); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 278 |  | 
|  | 279 | // Return true if the callee of CS should be partially inlined with | 
|  | 280 | // profit. | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 281 | bool shouldPartialInline(CallSite CS, FunctionCloner &Cloner, | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 282 | BlockFrequency WeightedOutliningRcost, | 
|  | 283 | OptimizationRemarkEmitter &ORE); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 284 |  | 
|  | 285 | // Try to inline DuplicateFunction (cloned from F with call to | 
|  | 286 | // the OutlinedFunction into its callers. Return true | 
|  | 287 | // if there is any successful inlining. | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 288 | bool tryPartialInline(FunctionCloner &Cloner); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 289 |  | 
|  | 290 | // Compute the mapping from use site of DuplicationFunction to the enclosing | 
|  | 291 | // BB's profile count. | 
|  | 292 | void computeCallsiteToProfCountMap(Function *DuplicateFunction, | 
|  | 293 | DenseMap<User *, uint64_t> &SiteCountMap); | 
|  | 294 |  | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 295 | bool IsLimitReached() { | 
|  | 296 | return (MaxNumPartialInlining != -1 && | 
|  | 297 | NumPartialInlining >= MaxNumPartialInlining); | 
|  | 298 | } | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 299 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 300 | static CallSite getCallSite(User *U) { | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 301 | CallSite CS; | 
|  | 302 | if (CallInst *CI = dyn_cast<CallInst>(U)) | 
|  | 303 | CS = CallSite(CI); | 
|  | 304 | else if (InvokeInst *II = dyn_cast<InvokeInst>(U)) | 
|  | 305 | CS = CallSite(II); | 
|  | 306 | else | 
|  | 307 | llvm_unreachable("All uses must be calls"); | 
|  | 308 | return CS; | 
|  | 309 | } | 
|  | 310 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 311 | static CallSite getOneCallSiteTo(Function *F) { | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 312 | User *User = *F->user_begin(); | 
|  | 313 | return getCallSite(User); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | std::tuple<DebugLoc, BasicBlock *> getOneDebugLoc(Function *F) { | 
|  | 317 | CallSite CS = getOneCallSiteTo(F); | 
|  | 318 | DebugLoc DLoc = CS.getInstruction()->getDebugLoc(); | 
|  | 319 | BasicBlock *Block = CS.getParent(); | 
|  | 320 | return std::make_tuple(DLoc, Block); | 
|  | 321 | } | 
|  | 322 |  | 
|  | 323 | // Returns the costs associated with function outlining: | 
|  | 324 | // - The first value is the non-weighted runtime cost for making the call | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 325 | //   to the outlined function, including the addtional  setup cost in the | 
|  | 326 | //    outlined function itself; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 327 | // - The second value is the estimated size of the new call sequence in | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 328 | //   basic block Cloner.OutliningCallBB; | 
|  | 329 | std::tuple<int, int> computeOutliningCosts(FunctionCloner &Cloner); | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 330 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 331 | // Compute the 'InlineCost' of block BB. InlineCost is a proxy used to | 
|  | 332 | // approximate both the size and runtime cost (Note that in the current | 
|  | 333 | // inline cost analysis, there is no clear distinction there either). | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 334 | static int computeBBInlineCost(BasicBlock *BB); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 335 |  | 
|  | 336 | std::unique_ptr<FunctionOutliningInfo> computeOutliningInfo(Function *F); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 337 | std::unique_ptr<FunctionOutliningMultiRegionInfo> | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 338 | computeOutliningColdRegionsInfo(Function *F, OptimizationRemarkEmitter &ORE); | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 339 | }; | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 340 |  | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 341 | struct PartialInlinerLegacyPass : public ModulePass { | 
|  | 342 | static char ID; // Pass identification, replacement for typeid | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 343 |  | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 344 | PartialInlinerLegacyPass() : ModulePass(ID) { | 
|  | 345 | initializePartialInlinerLegacyPassPass(*PassRegistry::getPassRegistry()); | 
|  | 346 | } | 
| Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 347 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 348 | void getAnalysisUsage(AnalysisUsage &AU) const override { | 
|  | 349 | AU.addRequired<AssumptionCacheTracker>(); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 350 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); | 
|  | 351 | AU.addRequired<TargetTransformInfoWrapperPass>(); | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 352 | } | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 353 |  | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 354 | bool runOnModule(Module &M) override { | 
|  | 355 | if (skipModule(M)) | 
|  | 356 | return false; | 
| Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 357 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 358 | AssumptionCacheTracker *ACT = &getAnalysis<AssumptionCacheTracker>(); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 359 | TargetTransformInfoWrapperPass *TTIWP = | 
|  | 360 | &getAnalysis<TargetTransformInfoWrapperPass>(); | 
|  | 361 | ProfileSummaryInfo *PSI = | 
|  | 362 | getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); | 
|  | 363 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 364 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = | 
|  | 365 | [&ACT](Function &F) -> AssumptionCache & { | 
|  | 366 | return ACT->getAssumptionCache(F); | 
|  | 367 | }; | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 368 |  | 
|  | 369 | std::function<TargetTransformInfo &(Function &)> GetTTI = | 
|  | 370 | [&TTIWP](Function &F) -> TargetTransformInfo & { | 
|  | 371 | return TTIWP->getTTI(F); | 
|  | 372 | }; | 
|  | 373 |  | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 374 | return PartialInlinerImpl(&GetAssumptionCache, &GetTTI, NoneType::None, PSI) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 375 | .run(M); | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 376 | } | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 377 | }; | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 378 |  | 
|  | 379 | } // end anonymous namespace | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 380 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 381 | std::unique_ptr<FunctionOutliningMultiRegionInfo> | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 382 | PartialInlinerImpl::computeOutliningColdRegionsInfo(Function *F, | 
|  | 383 | OptimizationRemarkEmitter &ORE) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 384 | BasicBlock *EntryBlock = &F->front(); | 
|  | 385 |  | 
|  | 386 | DominatorTree DT(*F); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 387 | LoopInfo LI(DT); | 
|  | 388 | BranchProbabilityInfo BPI(*F, LI); | 
|  | 389 | std::unique_ptr<BlockFrequencyInfo> ScopedBFI; | 
|  | 390 | BlockFrequencyInfo *BFI; | 
|  | 391 | if (!GetBFI) { | 
|  | 392 | ScopedBFI.reset(new BlockFrequencyInfo(*F, BPI, LI)); | 
|  | 393 | BFI = ScopedBFI.get(); | 
|  | 394 | } else | 
|  | 395 | BFI = &(*GetBFI)(*F); | 
|  | 396 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 397 | // Return if we don't have profiling information. | 
|  | 398 | if (!PSI->hasInstrumentationProfile()) | 
|  | 399 | return std::unique_ptr<FunctionOutliningMultiRegionInfo>(); | 
|  | 400 |  | 
|  | 401 | std::unique_ptr<FunctionOutliningMultiRegionInfo> OutliningInfo = | 
|  | 402 | llvm::make_unique<FunctionOutliningMultiRegionInfo>(); | 
|  | 403 |  | 
|  | 404 | auto IsSingleEntry = [](SmallVectorImpl<BasicBlock *> &BlockList) { | 
|  | 405 | BasicBlock *Dom = BlockList.front(); | 
| Vedant Kumar | e0b5f86 | 2018-05-10 23:01:54 +0000 | [diff] [blame] | 406 | return BlockList.size() > 1 && pred_size(Dom) == 1; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 407 | }; | 
|  | 408 |  | 
|  | 409 | auto IsSingleExit = | 
| Graham Yiu | 70293fa | 2017-11-30 03:36:57 +0000 | [diff] [blame] | 410 | [&ORE](SmallVectorImpl<BasicBlock *> &BlockList) -> BasicBlock * { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 411 | BasicBlock *ExitBlock = nullptr; | 
|  | 412 | for (auto *Block : BlockList) { | 
|  | 413 | for (auto SI = succ_begin(Block); SI != succ_end(Block); ++SI) { | 
|  | 414 | if (!is_contained(BlockList, *SI)) { | 
|  | 415 | if (ExitBlock) { | 
|  | 416 | ORE.emit([&]() { | 
|  | 417 | return OptimizationRemarkMissed(DEBUG_TYPE, "MultiExitRegion", | 
|  | 418 | &SI->front()) | 
|  | 419 | << "Region dominated by " | 
|  | 420 | << ore::NV("Block", BlockList.front()->getName()) | 
|  | 421 | << " has more than one region exit edge."; | 
|  | 422 | }); | 
|  | 423 | return nullptr; | 
|  | 424 | } else | 
|  | 425 | ExitBlock = Block; | 
|  | 426 | } | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 | return ExitBlock; | 
|  | 430 | }; | 
|  | 431 |  | 
|  | 432 | auto BBProfileCount = [BFI](BasicBlock *BB) { | 
|  | 433 | return BFI->getBlockProfileCount(BB) | 
|  | 434 | ? BFI->getBlockProfileCount(BB).getValue() | 
|  | 435 | : 0; | 
|  | 436 | }; | 
|  | 437 |  | 
|  | 438 | // Use the same computeBBInlineCost function to compute the cost savings of | 
|  | 439 | // the outlining the candidate region. | 
|  | 440 | int OverallFunctionCost = 0; | 
|  | 441 | for (auto &BB : *F) | 
|  | 442 | OverallFunctionCost += computeBBInlineCost(&BB); | 
|  | 443 |  | 
|  | 444 | #ifndef NDEBUG | 
|  | 445 | if (TracePartialInlining) | 
|  | 446 | dbgs() << "OverallFunctionCost = " << OverallFunctionCost << "\n"; | 
|  | 447 | #endif | 
|  | 448 | int MinOutlineRegionCost = | 
|  | 449 | static_cast<int>(OverallFunctionCost * MinRegionSizeRatio); | 
|  | 450 | BranchProbability MinBranchProbability( | 
|  | 451 | static_cast<int>(ColdBranchRatio * MinBlockCounterExecution), | 
|  | 452 | MinBlockCounterExecution); | 
|  | 453 | bool ColdCandidateFound = false; | 
|  | 454 | BasicBlock *CurrEntry = EntryBlock; | 
|  | 455 | std::vector<BasicBlock *> DFS; | 
|  | 456 | DenseMap<BasicBlock *, bool> VisitedMap; | 
|  | 457 | DFS.push_back(CurrEntry); | 
|  | 458 | VisitedMap[CurrEntry] = true; | 
|  | 459 | // Use Depth First Search on the basic blocks to find CFG edges that are | 
|  | 460 | // considered cold. | 
|  | 461 | // Cold regions considered must also have its inline cost compared to the | 
|  | 462 | // overall inline cost of the original function.  The region is outlined only | 
|  | 463 | // if it reduced the inline cost of the function by 'MinOutlineRegionCost' or | 
|  | 464 | // more. | 
|  | 465 | while (!DFS.empty()) { | 
|  | 466 | auto *thisBB = DFS.back(); | 
|  | 467 | DFS.pop_back(); | 
|  | 468 | // Only consider regions with predecessor blocks that are considered | 
|  | 469 | // not-cold (default: part of the top 99.99% of all block counters) | 
|  | 470 | // AND greater than our minimum block execution count (default: 100). | 
|  | 471 | if (PSI->isColdBB(thisBB, BFI) || | 
|  | 472 | BBProfileCount(thisBB) < MinBlockCounterExecution) | 
|  | 473 | continue; | 
|  | 474 | for (auto SI = succ_begin(thisBB); SI != succ_end(thisBB); ++SI) { | 
|  | 475 | if (VisitedMap[*SI]) | 
|  | 476 | continue; | 
|  | 477 | VisitedMap[*SI] = true; | 
|  | 478 | DFS.push_back(*SI); | 
|  | 479 | // If branch isn't cold, we skip to the next one. | 
|  | 480 | BranchProbability SuccProb = BPI.getEdgeProbability(thisBB, *SI); | 
|  | 481 | if (SuccProb > MinBranchProbability) | 
|  | 482 | continue; | 
|  | 483 | #ifndef NDEBUG | 
|  | 484 | if (TracePartialInlining) { | 
|  | 485 | dbgs() << "Found cold edge: " << thisBB->getName() << "->" | 
|  | 486 | << (*SI)->getName() << "\nBranch Probability = " << SuccProb | 
|  | 487 | << "\n"; | 
|  | 488 | } | 
|  | 489 | #endif | 
|  | 490 | SmallVector<BasicBlock *, 8> DominateVector; | 
|  | 491 | DT.getDescendants(*SI, DominateVector); | 
|  | 492 | // We can only outline single entry regions (for now). | 
|  | 493 | if (!IsSingleEntry(DominateVector)) | 
|  | 494 | continue; | 
|  | 495 | BasicBlock *ExitBlock = nullptr; | 
|  | 496 | // We can only outline single exit regions (for now). | 
|  | 497 | if (!(ExitBlock = IsSingleExit(DominateVector))) | 
|  | 498 | continue; | 
|  | 499 | int OutlineRegionCost = 0; | 
|  | 500 | for (auto *BB : DominateVector) | 
|  | 501 | OutlineRegionCost += computeBBInlineCost(BB); | 
|  | 502 |  | 
|  | 503 | #ifndef NDEBUG | 
|  | 504 | if (TracePartialInlining) | 
|  | 505 | dbgs() << "OutlineRegionCost = " << OutlineRegionCost << "\n"; | 
|  | 506 | #endif | 
|  | 507 |  | 
|  | 508 | if (OutlineRegionCost < MinOutlineRegionCost) { | 
|  | 509 | ORE.emit([&]() { | 
|  | 510 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "TooCostly", | 
|  | 511 | &SI->front()) | 
|  | 512 | << ore::NV("Callee", F) << " inline cost-savings smaller than " | 
|  | 513 | << ore::NV("Cost", MinOutlineRegionCost); | 
|  | 514 | }); | 
|  | 515 | continue; | 
|  | 516 | } | 
|  | 517 | // For now, ignore blocks that belong to a SISE region that is a | 
|  | 518 | // candidate for outlining.  In the future, we may want to look | 
|  | 519 | // at inner regions because the outer region may have live-exit | 
|  | 520 | // variables. | 
|  | 521 | for (auto *BB : DominateVector) | 
|  | 522 | VisitedMap[BB] = true; | 
|  | 523 | // ReturnBlock here means the block after the outline call | 
|  | 524 | BasicBlock *ReturnBlock = ExitBlock->getSingleSuccessor(); | 
|  | 525 | // assert(ReturnBlock && "ReturnBlock is NULL somehow!"); | 
|  | 526 | FunctionOutliningMultiRegionInfo::OutlineRegionInfo RegInfo( | 
|  | 527 | DominateVector, DominateVector.front(), ExitBlock, ReturnBlock); | 
|  | 528 | RegInfo.Region = DominateVector; | 
|  | 529 | OutliningInfo->ORI.push_back(RegInfo); | 
|  | 530 | #ifndef NDEBUG | 
|  | 531 | if (TracePartialInlining) { | 
|  | 532 | dbgs() << "Found Cold Candidate starting at block: " | 
|  | 533 | << DominateVector.front()->getName() << "\n"; | 
|  | 534 | } | 
|  | 535 | #endif | 
|  | 536 | ColdCandidateFound = true; | 
|  | 537 | NumColdRegionsFound++; | 
|  | 538 | } | 
|  | 539 | } | 
|  | 540 | if (ColdCandidateFound) | 
|  | 541 | return OutliningInfo; | 
|  | 542 | else | 
|  | 543 | return std::unique_ptr<FunctionOutliningMultiRegionInfo>(); | 
|  | 544 | } | 
|  | 545 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 546 | std::unique_ptr<FunctionOutliningInfo> | 
|  | 547 | PartialInlinerImpl::computeOutliningInfo(Function *F) { | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 548 | BasicBlock *EntryBlock = &F->front(); | 
|  | 549 | BranchInst *BR = dyn_cast<BranchInst>(EntryBlock->getTerminator()); | 
| Owen Anderson | f0081db | 2009-09-08 19:53:15 +0000 | [diff] [blame] | 550 | if (!BR || BR->isUnconditional()) | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 551 | return std::unique_ptr<FunctionOutliningInfo>(); | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 552 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 553 | // Returns true if Succ is BB's successor | 
|  | 554 | auto IsSuccessor = [](BasicBlock *Succ, BasicBlock *BB) { | 
|  | 555 | return is_contained(successors(BB), Succ); | 
|  | 556 | }; | 
|  | 557 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 558 | auto IsReturnBlock = [](BasicBlock *BB) { | 
|  | 559 | TerminatorInst *TI = BB->getTerminator(); | 
|  | 560 | return isa<ReturnInst>(TI); | 
|  | 561 | }; | 
|  | 562 |  | 
| Davide Italiano | aa42a10 | 2017-05-08 20:44:01 +0000 | [diff] [blame] | 563 | auto GetReturnBlock = [&](BasicBlock *Succ1, BasicBlock *Succ2) { | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 564 | if (IsReturnBlock(Succ1)) | 
|  | 565 | return std::make_tuple(Succ1, Succ2); | 
|  | 566 | if (IsReturnBlock(Succ2)) | 
|  | 567 | return std::make_tuple(Succ2, Succ1); | 
|  | 568 |  | 
|  | 569 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); | 
|  | 570 | }; | 
|  | 571 |  | 
|  | 572 | // Detect a triangular shape: | 
| Davide Italiano | aa42a10 | 2017-05-08 20:44:01 +0000 | [diff] [blame] | 573 | auto GetCommonSucc = [&](BasicBlock *Succ1, BasicBlock *Succ2) { | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 574 | if (IsSuccessor(Succ1, Succ2)) | 
|  | 575 | return std::make_tuple(Succ1, Succ2); | 
|  | 576 | if (IsSuccessor(Succ2, Succ1)) | 
|  | 577 | return std::make_tuple(Succ2, Succ1); | 
|  | 578 |  | 
|  | 579 | return std::make_tuple<BasicBlock *, BasicBlock *>(nullptr, nullptr); | 
|  | 580 | }; | 
|  | 581 |  | 
|  | 582 | std::unique_ptr<FunctionOutliningInfo> OutliningInfo = | 
|  | 583 | llvm::make_unique<FunctionOutliningInfo>(); | 
|  | 584 |  | 
|  | 585 | BasicBlock *CurrEntry = EntryBlock; | 
|  | 586 | bool CandidateFound = false; | 
|  | 587 | do { | 
|  | 588 | // The number of blocks to be inlined has already reached | 
|  | 589 | // the limit. When MaxNumInlineBlocks is set to 0 or 1, this | 
|  | 590 | // disables partial inlining for the function. | 
|  | 591 | if (OutliningInfo->GetNumInlinedBlocks() >= MaxNumInlineBlocks) | 
|  | 592 | break; | 
|  | 593 |  | 
| Vedant Kumar | e0b5f86 | 2018-05-10 23:01:54 +0000 | [diff] [blame] | 594 | if (succ_size(CurrEntry) != 2) | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 595 | break; | 
|  | 596 |  | 
|  | 597 | BasicBlock *Succ1 = *succ_begin(CurrEntry); | 
|  | 598 | BasicBlock *Succ2 = *(succ_begin(CurrEntry) + 1); | 
|  | 599 |  | 
|  | 600 | BasicBlock *ReturnBlock, *NonReturnBlock; | 
|  | 601 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); | 
|  | 602 |  | 
|  | 603 | if (ReturnBlock) { | 
|  | 604 | OutliningInfo->Entries.push_back(CurrEntry); | 
|  | 605 | OutliningInfo->ReturnBlock = ReturnBlock; | 
|  | 606 | OutliningInfo->NonReturnBlock = NonReturnBlock; | 
|  | 607 | CandidateFound = true; | 
|  | 608 | break; | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | BasicBlock *CommSucc; | 
|  | 612 | BasicBlock *OtherSucc; | 
|  | 613 | std::tie(CommSucc, OtherSucc) = GetCommonSucc(Succ1, Succ2); | 
|  | 614 |  | 
|  | 615 | if (!CommSucc) | 
|  | 616 | break; | 
|  | 617 |  | 
|  | 618 | OutliningInfo->Entries.push_back(CurrEntry); | 
|  | 619 | CurrEntry = OtherSucc; | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 620 | } while (true); | 
|  | 621 |  | 
|  | 622 | if (!CandidateFound) | 
|  | 623 | return std::unique_ptr<FunctionOutliningInfo>(); | 
|  | 624 |  | 
|  | 625 | // Do sanity check of the entries: threre should not | 
|  | 626 | // be any successors (not in the entry set) other than | 
|  | 627 | // {ReturnBlock, NonReturnBlock} | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 628 | assert(OutliningInfo->Entries[0] == &F->front() && | 
|  | 629 | "Function Entry must be the first in Entries vector"); | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 630 | DenseSet<BasicBlock *> Entries; | 
|  | 631 | for (BasicBlock *E : OutliningInfo->Entries) | 
|  | 632 | Entries.insert(E); | 
|  | 633 |  | 
|  | 634 | // Returns true of BB has Predecessor which is not | 
|  | 635 | // in Entries set. | 
|  | 636 | auto HasNonEntryPred = [Entries](BasicBlock *BB) { | 
|  | 637 | for (auto Pred : predecessors(BB)) { | 
|  | 638 | if (!Entries.count(Pred)) | 
|  | 639 | return true; | 
|  | 640 | } | 
|  | 641 | return false; | 
|  | 642 | }; | 
|  | 643 | auto CheckAndNormalizeCandidate = | 
|  | 644 | [Entries, HasNonEntryPred](FunctionOutliningInfo *OutliningInfo) { | 
|  | 645 | for (BasicBlock *E : OutliningInfo->Entries) { | 
|  | 646 | for (auto Succ : successors(E)) { | 
|  | 647 | if (Entries.count(Succ)) | 
|  | 648 | continue; | 
|  | 649 | if (Succ == OutliningInfo->ReturnBlock) | 
|  | 650 | OutliningInfo->ReturnBlockPreds.push_back(E); | 
|  | 651 | else if (Succ != OutliningInfo->NonReturnBlock) | 
|  | 652 | return false; | 
|  | 653 | } | 
|  | 654 | // There should not be any outside incoming edges either: | 
|  | 655 | if (HasNonEntryPred(E)) | 
|  | 656 | return false; | 
|  | 657 | } | 
|  | 658 | return true; | 
|  | 659 | }; | 
|  | 660 |  | 
|  | 661 | if (!CheckAndNormalizeCandidate(OutliningInfo.get())) | 
|  | 662 | return std::unique_ptr<FunctionOutliningInfo>(); | 
|  | 663 |  | 
|  | 664 | // Now further growing the candidate's inlining region by | 
|  | 665 | // peeling off dominating blocks from the outlining region: | 
|  | 666 | while (OutliningInfo->GetNumInlinedBlocks() < MaxNumInlineBlocks) { | 
|  | 667 | BasicBlock *Cand = OutliningInfo->NonReturnBlock; | 
| Vedant Kumar | e0b5f86 | 2018-05-10 23:01:54 +0000 | [diff] [blame] | 668 | if (succ_size(Cand) != 2) | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 669 | break; | 
|  | 670 |  | 
|  | 671 | if (HasNonEntryPred(Cand)) | 
|  | 672 | break; | 
|  | 673 |  | 
|  | 674 | BasicBlock *Succ1 = *succ_begin(Cand); | 
|  | 675 | BasicBlock *Succ2 = *(succ_begin(Cand) + 1); | 
|  | 676 |  | 
|  | 677 | BasicBlock *ReturnBlock, *NonReturnBlock; | 
|  | 678 | std::tie(ReturnBlock, NonReturnBlock) = GetReturnBlock(Succ1, Succ2); | 
|  | 679 | if (!ReturnBlock || ReturnBlock != OutliningInfo->ReturnBlock) | 
|  | 680 | break; | 
|  | 681 |  | 
|  | 682 | if (NonReturnBlock->getSinglePredecessor() != Cand) | 
|  | 683 | break; | 
|  | 684 |  | 
|  | 685 | // Now grow and update OutlininigInfo: | 
|  | 686 | OutliningInfo->Entries.push_back(Cand); | 
|  | 687 | OutliningInfo->NonReturnBlock = NonReturnBlock; | 
|  | 688 | OutliningInfo->ReturnBlockPreds.push_back(Cand); | 
|  | 689 | Entries.insert(Cand); | 
| Reid Kleckner | c26a17a | 2015-02-04 19:14:57 +0000 | [diff] [blame] | 690 | } | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 691 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 692 | return OutliningInfo; | 
|  | 693 | } | 
|  | 694 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 695 | // Check if there is PGO data or user annoated branch data: | 
|  | 696 | static bool hasProfileData(Function *F, FunctionOutliningInfo *OI) { | 
| Easwaran Raman | a17f220 | 2017-12-22 01:33:52 +0000 | [diff] [blame] | 697 | if (F->hasProfileData()) | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 698 | return true; | 
|  | 699 | // Now check if any of the entry block has MD_prof data: | 
|  | 700 | for (auto *E : OI->Entries) { | 
|  | 701 | BranchInst *BR = dyn_cast<BranchInst>(E->getTerminator()); | 
|  | 702 | if (!BR || BR->isUnconditional()) | 
|  | 703 | continue; | 
|  | 704 | uint64_t T, F; | 
|  | 705 | if (BR->extractProfMetadata(T, F)) | 
|  | 706 | return true; | 
|  | 707 | } | 
|  | 708 | return false; | 
|  | 709 | } | 
|  | 710 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 711 | BranchProbability | 
|  | 712 | PartialInlinerImpl::getOutliningCallBBRelativeFreq(FunctionCloner &Cloner) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 713 | BasicBlock *OutliningCallBB = Cloner.OutlinedFunctions.back().second; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 714 | auto EntryFreq = | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 715 | Cloner.ClonedFuncBFI->getBlockFreq(&Cloner.ClonedFunc->getEntryBlock()); | 
|  | 716 | auto OutliningCallFreq = | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 717 | Cloner.ClonedFuncBFI->getBlockFreq(OutliningCallBB); | 
|  | 718 | // FIXME Hackery needed because ClonedFuncBFI is based on the function BEFORE | 
|  | 719 | // we outlined any regions, so we may encounter situations where the | 
|  | 720 | // OutliningCallFreq is *slightly* bigger than the EntryFreq. | 
|  | 721 | if (OutliningCallFreq.getFrequency() > EntryFreq.getFrequency()) { | 
|  | 722 | OutliningCallFreq = EntryFreq; | 
|  | 723 | } | 
|  | 724 | auto OutlineRegionRelFreq = BranchProbability::getBranchProbability( | 
|  | 725 | OutliningCallFreq.getFrequency(), EntryFreq.getFrequency()); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 726 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 727 | if (hasProfileData(Cloner.OrigFunc, Cloner.ClonedOI.get())) | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 728 | return OutlineRegionRelFreq; | 
|  | 729 |  | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 730 | // When profile data is not available, we need to be conservative in | 
|  | 731 | // estimating the overall savings. Static branch prediction can usually | 
|  | 732 | // guess the branch direction right (taken/non-taken), but the guessed | 
|  | 733 | // branch probability is usually not biased enough. In case when the | 
|  | 734 | // outlined region is predicted to be likely, its probability needs | 
|  | 735 | // to be made higher (more biased) to not under-estimate the cost of | 
|  | 736 | // function outlining. On the other hand, if the outlined region | 
|  | 737 | // is predicted to be less likely, the predicted probablity is usually | 
|  | 738 | // higher than the actual. For instance, the actual probability of the | 
|  | 739 | // less likely target is only 5%, but the guessed probablity can be | 
|  | 740 | // 40%. In the latter case, there is no need for further adjustement. | 
|  | 741 | // FIXME: add an option for this. | 
|  | 742 | if (OutlineRegionRelFreq < BranchProbability(45, 100)) | 
|  | 743 | return OutlineRegionRelFreq; | 
|  | 744 |  | 
|  | 745 | OutlineRegionRelFreq = std::max( | 
|  | 746 | OutlineRegionRelFreq, BranchProbability(OutlineRegionFreqPercent, 100)); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 747 |  | 
|  | 748 | return OutlineRegionRelFreq; | 
|  | 749 | } | 
|  | 750 |  | 
|  | 751 | bool PartialInlinerImpl::shouldPartialInline( | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 752 | CallSite CS, FunctionCloner &Cloner, | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 753 | BlockFrequency WeightedOutliningRcost, | 
|  | 754 | OptimizationRemarkEmitter &ORE) { | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 755 | using namespace ore; | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 756 |  | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 757 | Instruction *Call = CS.getInstruction(); | 
|  | 758 | Function *Callee = CS.getCalledFunction(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 759 | assert(Callee == Cloner.ClonedFunc); | 
|  | 760 |  | 
| Florian Hahn | a7dcfa7 | 2018-03-10 14:53:44 +0000 | [diff] [blame] | 761 | if (SkipCostAnalysis) | 
|  | 762 | return isInlineViable(*Callee); | 
|  | 763 |  | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 764 | Function *Caller = CS.getCaller(); | 
|  | 765 | auto &CalleeTTI = (*GetTTI)(*Callee); | 
|  | 766 | InlineCost IC = getInlineCost(CS, getInlineParams(), CalleeTTI, | 
| Haicheng Wu | 0812c5b | 2017-08-21 20:00:09 +0000 | [diff] [blame] | 767 | *GetAssumptionCache, GetBFI, PSI, &ORE); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 768 |  | 
|  | 769 | if (IC.isAlways()) { | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 770 | ORE.emit([&]() { | 
|  | 771 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "AlwaysInline", Call) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 772 | << NV("Callee", Cloner.OrigFunc) | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 773 | << " should always be fully inlined, not partially"; | 
|  | 774 | }); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 775 | return false; | 
|  | 776 | } | 
|  | 777 |  | 
|  | 778 | if (IC.isNever()) { | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 779 | ORE.emit([&]() { | 
|  | 780 | return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline", Call) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 781 | << NV("Callee", Cloner.OrigFunc) << " not partially inlined into " | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 782 | << NV("Caller", Caller) | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 783 | << " because it should never be inlined (cost=never)"; | 
|  | 784 | }); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 785 | return false; | 
|  | 786 | } | 
|  | 787 |  | 
|  | 788 | if (!IC) { | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 789 | ORE.emit([&]() { | 
|  | 790 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "TooCostly", Call) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 791 | << NV("Callee", Cloner.OrigFunc) << " not partially inlined into " | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 792 | << NV("Caller", Caller) << " because too costly to inline (cost=" | 
|  | 793 | << NV("Cost", IC.getCost()) << ", threshold=" | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 794 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"; | 
|  | 795 | }); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 796 | return false; | 
|  | 797 | } | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 798 | const DataLayout &DL = Caller->getParent()->getDataLayout(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 799 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 800 | // The savings of eliminating the call: | 
|  | 801 | int NonWeightedSavings = getCallsiteCost(CS, DL); | 
|  | 802 | BlockFrequency NormWeightedSavings(NonWeightedSavings); | 
|  | 803 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 804 | // Weighted saving is smaller than weighted cost, return false | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 805 | if (NormWeightedSavings < WeightedOutliningRcost) { | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 806 | ORE.emit([&]() { | 
|  | 807 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "OutliningCallcostTooHigh", | 
|  | 808 | Call) | 
|  | 809 | << NV("Callee", Cloner.OrigFunc) << " not partially inlined into " | 
|  | 810 | << NV("Caller", Caller) << " runtime overhead (overhead=" | 
|  | 811 | << NV("Overhead", (unsigned)WeightedOutliningRcost.getFrequency()) | 
|  | 812 | << ", savings=" | 
|  | 813 | << NV("Savings", (unsigned)NormWeightedSavings.getFrequency()) | 
|  | 814 | << ")" | 
|  | 815 | << " of making the outlined call is too high"; | 
|  | 816 | }); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 817 |  | 
|  | 818 | return false; | 
|  | 819 | } | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 820 |  | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 821 | ORE.emit([&]() { | 
|  | 822 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "CanBePartiallyInlined", Call) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 823 | << NV("Callee", Cloner.OrigFunc) << " can be partially inlined into " | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 824 | << NV("Caller", Caller) << " with cost=" << NV("Cost", IC.getCost()) | 
|  | 825 | << " (threshold=" | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 826 | << NV("Threshold", IC.getCostDelta() + IC.getCost()) << ")"; | 
|  | 827 | }); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 828 | return true; | 
|  | 829 | } | 
|  | 830 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 831 | // TODO: Ideally  we should share Inliner's InlineCost Analysis code. | 
|  | 832 | // For now use a simplified version. The returned 'InlineCost' will be used | 
|  | 833 | // to esimate the size cost as well as runtime cost of the BB. | 
|  | 834 | int PartialInlinerImpl::computeBBInlineCost(BasicBlock *BB) { | 
|  | 835 | int InlineCost = 0; | 
|  | 836 | const DataLayout &DL = BB->getParent()->getParent()->getDataLayout(); | 
|  | 837 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { | 
|  | 838 | if (isa<DbgInfoIntrinsic>(I)) | 
|  | 839 | continue; | 
|  | 840 |  | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 841 | switch (I->getOpcode()) { | 
|  | 842 | case Instruction::BitCast: | 
|  | 843 | case Instruction::PtrToInt: | 
|  | 844 | case Instruction::IntToPtr: | 
|  | 845 | case Instruction::Alloca: | 
|  | 846 | continue; | 
|  | 847 | case Instruction::GetElementPtr: | 
|  | 848 | if (cast<GetElementPtrInst>(I)->hasAllZeroIndices()) | 
|  | 849 | continue; | 
| Adrian Prantl | 0e6694d | 2017-12-19 22:05:25 +0000 | [diff] [blame] | 850 | break; | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 851 | default: | 
|  | 852 | break; | 
|  | 853 | } | 
|  | 854 |  | 
|  | 855 | IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(I); | 
|  | 856 | if (IntrInst) { | 
|  | 857 | if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start || | 
|  | 858 | IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) | 
|  | 859 | continue; | 
|  | 860 | } | 
|  | 861 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 862 | if (CallInst *CI = dyn_cast<CallInst>(I)) { | 
|  | 863 | InlineCost += getCallsiteCost(CallSite(CI), DL); | 
|  | 864 | continue; | 
|  | 865 | } | 
|  | 866 |  | 
|  | 867 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) { | 
|  | 868 | InlineCost += getCallsiteCost(CallSite(II), DL); | 
|  | 869 | continue; | 
|  | 870 | } | 
|  | 871 |  | 
|  | 872 | if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) { | 
|  | 873 | InlineCost += (SI->getNumCases() + 1) * InlineConstants::InstrCost; | 
|  | 874 | continue; | 
|  | 875 | } | 
|  | 876 | InlineCost += InlineConstants::InstrCost; | 
|  | 877 | } | 
|  | 878 | return InlineCost; | 
|  | 879 | } | 
|  | 880 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 881 | std::tuple<int, int> | 
|  | 882 | PartialInlinerImpl::computeOutliningCosts(FunctionCloner &Cloner) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 883 | int OutliningFuncCallCost = 0, OutlinedFunctionCost = 0; | 
|  | 884 | for (auto FuncBBPair : Cloner.OutlinedFunctions) { | 
|  | 885 | Function *OutlinedFunc = FuncBBPair.first; | 
|  | 886 | BasicBlock* OutliningCallBB = FuncBBPair.second; | 
|  | 887 | // Now compute the cost of the call sequence to the outlined function | 
|  | 888 | // 'OutlinedFunction' in BB 'OutliningCallBB': | 
|  | 889 | OutliningFuncCallCost += computeBBInlineCost(OutliningCallBB); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 890 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 891 | // Now compute the cost of the extracted/outlined function itself: | 
|  | 892 | for (BasicBlock &BB : *OutlinedFunc) | 
|  | 893 | OutlinedFunctionCost += computeBBInlineCost(&BB); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 894 | } | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 895 | assert(OutlinedFunctionCost >= Cloner.OutlinedRegionCost && | 
| Xinliang David Li | 5fdc75a | 2017-06-02 22:38:48 +0000 | [diff] [blame] | 896 | "Outlined function cost should be no less than the outlined region"); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 897 |  | 
| Xinliang David Li | 0b7d858 | 2017-06-02 22:08:04 +0000 | [diff] [blame] | 898 | // The code extractor introduces a new root and exit stub blocks with | 
|  | 899 | // additional unconditional branches. Those branches will be eliminated | 
|  | 900 | // later with bb layout. The cost should be adjusted accordingly: | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 901 | OutlinedFunctionCost -= | 
|  | 902 | 2 * InlineConstants::InstrCost * Cloner.OutlinedFunctions.size(); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 903 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 904 | int OutliningRuntimeOverhead = | 
|  | 905 | OutliningFuncCallCost + | 
|  | 906 | (OutlinedFunctionCost - Cloner.OutlinedRegionCost) + | 
|  | 907 | ExtraOutliningPenalty; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 908 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 909 | return std::make_tuple(OutliningFuncCallCost, OutliningRuntimeOverhead); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 910 | } | 
|  | 911 |  | 
|  | 912 | // Create the callsite to profile count map which is | 
|  | 913 | // used to update the original function's entry count, | 
|  | 914 | // after the function is partially inlined into the callsite. | 
|  | 915 | void PartialInlinerImpl::computeCallsiteToProfCountMap( | 
|  | 916 | Function *DuplicateFunction, | 
|  | 917 | DenseMap<User *, uint64_t> &CallSiteToProfCountMap) { | 
|  | 918 | std::vector<User *> Users(DuplicateFunction->user_begin(), | 
|  | 919 | DuplicateFunction->user_end()); | 
|  | 920 | Function *CurrentCaller = nullptr; | 
| Vitaly Buka | a637489 | 2017-05-27 05:32:09 +0000 | [diff] [blame] | 921 | std::unique_ptr<BlockFrequencyInfo> TempBFI; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 922 | BlockFrequencyInfo *CurrentCallerBFI = nullptr; | 
|  | 923 |  | 
|  | 924 | auto ComputeCurrBFI = [&,this](Function *Caller) { | 
|  | 925 | // For the old pass manager: | 
|  | 926 | if (!GetBFI) { | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 927 | DominatorTree DT(*Caller); | 
|  | 928 | LoopInfo LI(DT); | 
|  | 929 | BranchProbabilityInfo BPI(*Caller, LI); | 
| Vitaly Buka | a637489 | 2017-05-27 05:32:09 +0000 | [diff] [blame] | 930 | TempBFI.reset(new BlockFrequencyInfo(*Caller, BPI, LI)); | 
|  | 931 | CurrentCallerBFI = TempBFI.get(); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 932 | } else { | 
|  | 933 | // New pass manager: | 
|  | 934 | CurrentCallerBFI = &(*GetBFI)(*Caller); | 
|  | 935 | } | 
|  | 936 | }; | 
|  | 937 |  | 
|  | 938 | for (User *User : Users) { | 
|  | 939 | CallSite CS = getCallSite(User); | 
|  | 940 | Function *Caller = CS.getCaller(); | 
|  | 941 | if (CurrentCaller != Caller) { | 
|  | 942 | CurrentCaller = Caller; | 
|  | 943 | ComputeCurrBFI(Caller); | 
|  | 944 | } else { | 
|  | 945 | assert(CurrentCallerBFI && "CallerBFI is not set"); | 
|  | 946 | } | 
|  | 947 | BasicBlock *CallBB = CS.getInstruction()->getParent(); | 
|  | 948 | auto Count = CurrentCallerBFI->getBlockProfileCount(CallBB); | 
|  | 949 | if (Count) | 
|  | 950 | CallSiteToProfCountMap[User] = *Count; | 
|  | 951 | else | 
|  | 952 | CallSiteToProfCountMap[User] = 0; | 
|  | 953 | } | 
|  | 954 | } | 
|  | 955 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 956 | PartialInlinerImpl::FunctionCloner::FunctionCloner( | 
|  | 957 | Function *F, FunctionOutliningInfo *OI, OptimizationRemarkEmitter &ORE) | 
|  | 958 | : OrigFunc(F), ORE(ORE) { | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 959 | ClonedOI = llvm::make_unique<FunctionOutliningInfo>(); | 
|  | 960 |  | 
|  | 961 | // Clone the function, so that we can hack away on it. | 
|  | 962 | ValueToValueMapTy VMap; | 
|  | 963 | ClonedFunc = CloneFunction(F, VMap); | 
|  | 964 |  | 
|  | 965 | ClonedOI->ReturnBlock = cast<BasicBlock>(VMap[OI->ReturnBlock]); | 
|  | 966 | ClonedOI->NonReturnBlock = cast<BasicBlock>(VMap[OI->NonReturnBlock]); | 
|  | 967 | for (BasicBlock *BB : OI->Entries) { | 
|  | 968 | ClonedOI->Entries.push_back(cast<BasicBlock>(VMap[BB])); | 
|  | 969 | } | 
|  | 970 | for (BasicBlock *E : OI->ReturnBlockPreds) { | 
|  | 971 | BasicBlock *NewE = cast<BasicBlock>(VMap[E]); | 
|  | 972 | ClonedOI->ReturnBlockPreds.push_back(NewE); | 
|  | 973 | } | 
|  | 974 | // Go ahead and update all uses to the duplicate, so that we can just | 
|  | 975 | // use the inliner functionality when we're done hacking. | 
|  | 976 | F->replaceAllUsesWith(ClonedFunc); | 
|  | 977 | } | 
|  | 978 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 979 | PartialInlinerImpl::FunctionCloner::FunctionCloner( | 
|  | 980 | Function *F, FunctionOutliningMultiRegionInfo *OI, | 
|  | 981 | OptimizationRemarkEmitter &ORE) | 
|  | 982 | : OrigFunc(F), ORE(ORE) { | 
|  | 983 | ClonedOMRI = llvm::make_unique<FunctionOutliningMultiRegionInfo>(); | 
|  | 984 |  | 
|  | 985 | // Clone the function, so that we can hack away on it. | 
|  | 986 | ValueToValueMapTy VMap; | 
|  | 987 | ClonedFunc = CloneFunction(F, VMap); | 
|  | 988 |  | 
|  | 989 | // Go through all Outline Candidate Regions and update all BasicBlock | 
|  | 990 | // information. | 
|  | 991 | for (FunctionOutliningMultiRegionInfo::OutlineRegionInfo RegionInfo : | 
|  | 992 | OI->ORI) { | 
|  | 993 | SmallVector<BasicBlock *, 8> Region; | 
|  | 994 | for (BasicBlock *BB : RegionInfo.Region) { | 
|  | 995 | Region.push_back(cast<BasicBlock>(VMap[BB])); | 
|  | 996 | } | 
|  | 997 | BasicBlock *NewEntryBlock = cast<BasicBlock>(VMap[RegionInfo.EntryBlock]); | 
|  | 998 | BasicBlock *NewExitBlock = cast<BasicBlock>(VMap[RegionInfo.ExitBlock]); | 
|  | 999 | BasicBlock *NewReturnBlock = nullptr; | 
|  | 1000 | if (RegionInfo.ReturnBlock) | 
|  | 1001 | NewReturnBlock = cast<BasicBlock>(VMap[RegionInfo.ReturnBlock]); | 
|  | 1002 | FunctionOutliningMultiRegionInfo::OutlineRegionInfo MappedRegionInfo( | 
|  | 1003 | Region, NewEntryBlock, NewExitBlock, NewReturnBlock); | 
|  | 1004 | ClonedOMRI->ORI.push_back(MappedRegionInfo); | 
|  | 1005 | } | 
|  | 1006 | // Go ahead and update all uses to the duplicate, so that we can just | 
|  | 1007 | // use the inliner functionality when we're done hacking. | 
|  | 1008 | F->replaceAllUsesWith(ClonedFunc); | 
|  | 1009 | } | 
|  | 1010 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1011 | void PartialInlinerImpl::FunctionCloner::NormalizeReturnBlock() { | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1012 | auto getFirstPHI = [](BasicBlock *BB) { | 
|  | 1013 | BasicBlock::iterator I = BB->begin(); | 
|  | 1014 | PHINode *FirstPhi = nullptr; | 
|  | 1015 | while (I != BB->end()) { | 
|  | 1016 | PHINode *Phi = dyn_cast<PHINode>(I); | 
|  | 1017 | if (!Phi) | 
|  | 1018 | break; | 
|  | 1019 | if (!FirstPhi) { | 
|  | 1020 | FirstPhi = Phi; | 
|  | 1021 | break; | 
|  | 1022 | } | 
|  | 1023 | } | 
|  | 1024 | return FirstPhi; | 
|  | 1025 | }; | 
|  | 1026 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1027 | // Shouldn't need to normalize PHIs if we're not outlining non-early return | 
|  | 1028 | // blocks. | 
|  | 1029 | if (!ClonedOI) | 
|  | 1030 | return; | 
|  | 1031 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1032 | // Special hackery is needed with PHI nodes that have inputs from more than | 
|  | 1033 | // one extracted block.  For simplicity, just split the PHIs into a two-level | 
|  | 1034 | // sequence of PHIs, some of which will go in the extracted region, and some | 
|  | 1035 | // of which will go outside. | 
|  | 1036 | BasicBlock *PreReturn = ClonedOI->ReturnBlock; | 
|  | 1037 | // only split block when necessary: | 
|  | 1038 | PHINode *FirstPhi = getFirstPHI(PreReturn); | 
|  | 1039 | unsigned NumPredsFromEntries = ClonedOI->ReturnBlockPreds.size(); | 
|  | 1040 |  | 
|  | 1041 | if (!FirstPhi || FirstPhi->getNumIncomingValues() <= NumPredsFromEntries + 1) | 
|  | 1042 | return; | 
|  | 1043 |  | 
|  | 1044 | auto IsTrivialPhi = [](PHINode *PN) -> Value * { | 
|  | 1045 | Value *CommonValue = PN->getIncomingValue(0); | 
|  | 1046 | if (all_of(PN->incoming_values(), | 
|  | 1047 | [&](Value *V) { return V == CommonValue; })) | 
|  | 1048 | return CommonValue; | 
|  | 1049 | return nullptr; | 
|  | 1050 | }; | 
|  | 1051 |  | 
|  | 1052 | ClonedOI->ReturnBlock = ClonedOI->ReturnBlock->splitBasicBlock( | 
|  | 1053 | ClonedOI->ReturnBlock->getFirstNonPHI()->getIterator()); | 
|  | 1054 | BasicBlock::iterator I = PreReturn->begin(); | 
|  | 1055 | Instruction *Ins = &ClonedOI->ReturnBlock->front(); | 
|  | 1056 | SmallVector<Instruction *, 4> DeadPhis; | 
|  | 1057 | while (I != PreReturn->end()) { | 
|  | 1058 | PHINode *OldPhi = dyn_cast<PHINode>(I); | 
|  | 1059 | if (!OldPhi) | 
|  | 1060 | break; | 
|  | 1061 |  | 
|  | 1062 | PHINode *RetPhi = | 
|  | 1063 | PHINode::Create(OldPhi->getType(), NumPredsFromEntries + 1, "", Ins); | 
|  | 1064 | OldPhi->replaceAllUsesWith(RetPhi); | 
|  | 1065 | Ins = ClonedOI->ReturnBlock->getFirstNonPHI(); | 
|  | 1066 |  | 
|  | 1067 | RetPhi->addIncoming(&*I, PreReturn); | 
|  | 1068 | for (BasicBlock *E : ClonedOI->ReturnBlockPreds) { | 
|  | 1069 | RetPhi->addIncoming(OldPhi->getIncomingValueForBlock(E), E); | 
|  | 1070 | OldPhi->removeIncomingValue(E); | 
|  | 1071 | } | 
|  | 1072 |  | 
|  | 1073 | // After incoming values splitting, the old phi may become trivial. | 
|  | 1074 | // Keeping the trivial phi can introduce definition inside the outline | 
|  | 1075 | // region which is live-out, causing necessary overhead (load, store | 
|  | 1076 | // arg passing etc). | 
|  | 1077 | if (auto *OldPhiVal = IsTrivialPhi(OldPhi)) { | 
|  | 1078 | OldPhi->replaceAllUsesWith(OldPhiVal); | 
|  | 1079 | DeadPhis.push_back(OldPhi); | 
|  | 1080 | } | 
|  | 1081 | ++I; | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1082 | } | 
|  | 1083 | for (auto *DP : DeadPhis) | 
|  | 1084 | DP->eraseFromParent(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1085 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1086 | for (auto E : ClonedOI->ReturnBlockPreds) { | 
|  | 1087 | E->getTerminator()->replaceUsesOfWith(PreReturn, ClonedOI->ReturnBlock); | 
|  | 1088 | } | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1089 | } | 
|  | 1090 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1091 | bool PartialInlinerImpl::FunctionCloner::doMultiRegionFunctionOutlining() { | 
|  | 1092 |  | 
|  | 1093 | auto ComputeRegionCost = [](SmallVectorImpl<BasicBlock *> &Region) { | 
|  | 1094 | int Cost = 0; | 
|  | 1095 | for (BasicBlock* BB : Region) | 
|  | 1096 | Cost += computeBBInlineCost(BB); | 
|  | 1097 | return Cost; | 
|  | 1098 | }; | 
|  | 1099 |  | 
|  | 1100 | assert(ClonedOMRI && "Expecting OutlineInfo for multi region outline"); | 
|  | 1101 |  | 
|  | 1102 | if (ClonedOMRI->ORI.empty()) | 
|  | 1103 | return false; | 
|  | 1104 |  | 
|  | 1105 | // The CodeExtractor needs a dominator tree. | 
|  | 1106 | DominatorTree DT; | 
|  | 1107 | DT.recalculate(*ClonedFunc); | 
|  | 1108 |  | 
|  | 1109 | // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo. | 
|  | 1110 | LoopInfo LI(DT); | 
|  | 1111 | BranchProbabilityInfo BPI(*ClonedFunc, LI); | 
|  | 1112 | ClonedFuncBFI.reset(new BlockFrequencyInfo(*ClonedFunc, BPI, LI)); | 
|  | 1113 |  | 
|  | 1114 | SetVector<Value *> Inputs, Outputs, Sinks; | 
|  | 1115 | for (FunctionOutliningMultiRegionInfo::OutlineRegionInfo RegionInfo : | 
|  | 1116 | ClonedOMRI->ORI) { | 
|  | 1117 | int CurrentOutlinedRegionCost = ComputeRegionCost(RegionInfo.Region); | 
|  | 1118 |  | 
|  | 1119 | CodeExtractor CE(RegionInfo.Region, &DT, /*AggregateArgs*/ false, | 
|  | 1120 | ClonedFuncBFI.get(), &BPI, /* AllowVarargs */ false); | 
|  | 1121 |  | 
|  | 1122 | CE.findInputsOutputs(Inputs, Outputs, Sinks); | 
|  | 1123 |  | 
|  | 1124 | #ifndef NDEBUG | 
|  | 1125 | if (TracePartialInlining) { | 
|  | 1126 | dbgs() << "inputs: " << Inputs.size() << "\n"; | 
|  | 1127 | dbgs() << "outputs: " << Outputs.size() << "\n"; | 
|  | 1128 | for (Value *value : Inputs) | 
|  | 1129 | dbgs() << "value used in func: " << *value << "\n"; | 
|  | 1130 | for (Value *output : Outputs) | 
|  | 1131 | dbgs() << "instr used in func: " << *output << "\n"; | 
|  | 1132 | } | 
|  | 1133 | #endif | 
|  | 1134 | // Do not extract regions that have live exit variables. | 
|  | 1135 | if (Outputs.size() > 0 && !ForceLiveExit) | 
|  | 1136 | continue; | 
|  | 1137 |  | 
|  | 1138 | Function *OutlinedFunc = CE.extractCodeRegion(); | 
|  | 1139 |  | 
|  | 1140 | if (OutlinedFunc) { | 
|  | 1141 | CallSite OCS = PartialInlinerImpl::getOneCallSiteTo(OutlinedFunc); | 
|  | 1142 | BasicBlock *OutliningCallBB = OCS.getInstruction()->getParent(); | 
|  | 1143 | assert(OutliningCallBB->getParent() == ClonedFunc); | 
|  | 1144 | OutlinedFunctions.push_back(std::make_pair(OutlinedFunc,OutliningCallBB)); | 
|  | 1145 | NumColdRegionsOutlined++; | 
|  | 1146 | OutlinedRegionCost += CurrentOutlinedRegionCost; | 
|  | 1147 |  | 
|  | 1148 | if (MarkOutlinedColdCC) { | 
|  | 1149 | OutlinedFunc->setCallingConv(CallingConv::Cold); | 
|  | 1150 | OCS.setCallingConv(CallingConv::Cold); | 
|  | 1151 | } | 
|  | 1152 | } else | 
|  | 1153 | ORE.emit([&]() { | 
|  | 1154 | return OptimizationRemarkMissed(DEBUG_TYPE, "ExtractFailed", | 
|  | 1155 | &RegionInfo.Region.front()->front()) | 
|  | 1156 | << "Failed to extract region at block " | 
|  | 1157 | << ore::NV("Block", RegionInfo.Region.front()); | 
|  | 1158 | }); | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | return !OutlinedFunctions.empty(); | 
|  | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | Function * | 
|  | 1165 | PartialInlinerImpl::FunctionCloner::doSingleRegionFunctionOutlining() { | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1166 | // Returns true if the block is to be partial inlined into the caller | 
|  | 1167 | // (i.e. not to be extracted to the out of line function) | 
|  | 1168 | auto ToBeInlined = [&, this](BasicBlock *BB) { | 
|  | 1169 | return BB == ClonedOI->ReturnBlock || | 
|  | 1170 | (std::find(ClonedOI->Entries.begin(), ClonedOI->Entries.end(), BB) != | 
|  | 1171 | ClonedOI->Entries.end()); | 
|  | 1172 | }; | 
|  | 1173 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1174 | assert(ClonedOI && "Expecting OutlineInfo for single region outline"); | 
|  | 1175 | // The CodeExtractor needs a dominator tree. | 
|  | 1176 | DominatorTree DT; | 
|  | 1177 | DT.recalculate(*ClonedFunc); | 
|  | 1178 |  | 
|  | 1179 | // Manually calculate a BlockFrequencyInfo and BranchProbabilityInfo. | 
|  | 1180 | LoopInfo LI(DT); | 
|  | 1181 | BranchProbabilityInfo BPI(*ClonedFunc, LI); | 
|  | 1182 | ClonedFuncBFI.reset(new BlockFrequencyInfo(*ClonedFunc, BPI, LI)); | 
|  | 1183 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1184 | // Gather up the blocks that we're going to extract. | 
|  | 1185 | std::vector<BasicBlock *> ToExtract; | 
|  | 1186 | ToExtract.push_back(ClonedOI->NonReturnBlock); | 
|  | 1187 | OutlinedRegionCost += | 
|  | 1188 | PartialInlinerImpl::computeBBInlineCost(ClonedOI->NonReturnBlock); | 
|  | 1189 | for (BasicBlock &BB : *ClonedFunc) | 
|  | 1190 | if (!ToBeInlined(&BB) && &BB != ClonedOI->NonReturnBlock) { | 
|  | 1191 | ToExtract.push_back(&BB); | 
|  | 1192 | // FIXME: the code extractor may hoist/sink more code | 
|  | 1193 | // into the outlined function which may make the outlining | 
|  | 1194 | // overhead (the difference of the outlined function cost | 
|  | 1195 | // and OutliningRegionCost) look larger. | 
|  | 1196 | OutlinedRegionCost += computeBBInlineCost(&BB); | 
|  | 1197 | } | 
|  | 1198 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1199 | // Extract the body of the if. | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1200 | Function *OutlinedFunc = | 
|  | 1201 | CodeExtractor(ToExtract, &DT, /*AggregateArgs*/ false, | 
|  | 1202 | ClonedFuncBFI.get(), &BPI, | 
|  | 1203 | /* AllowVarargs */ true) | 
|  | 1204 | .extractCodeRegion(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1205 |  | 
|  | 1206 | if (OutlinedFunc) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1207 | BasicBlock *OutliningCallBB = | 
|  | 1208 | PartialInlinerImpl::getOneCallSiteTo(OutlinedFunc) | 
|  | 1209 | .getInstruction() | 
|  | 1210 | ->getParent(); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1211 | assert(OutliningCallBB->getParent() == ClonedFunc); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1212 | OutlinedFunctions.push_back(std::make_pair(OutlinedFunc, OutliningCallBB)); | 
|  | 1213 | } else | 
|  | 1214 | ORE.emit([&]() { | 
|  | 1215 | return OptimizationRemarkMissed(DEBUG_TYPE, "ExtractFailed", | 
|  | 1216 | &ToExtract.front()->front()) | 
|  | 1217 | << "Failed to extract region at block " | 
|  | 1218 | << ore::NV("Block", ToExtract.front()); | 
|  | 1219 | }); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1220 |  | 
|  | 1221 | return OutlinedFunc; | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | PartialInlinerImpl::FunctionCloner::~FunctionCloner() { | 
|  | 1225 | // Ditch the duplicate, since we're done with it, and rewrite all remaining | 
|  | 1226 | // users (function pointers, etc.) back to the original function. | 
|  | 1227 | ClonedFunc->replaceAllUsesWith(OrigFunc); | 
|  | 1228 | ClonedFunc->eraseFromParent(); | 
|  | 1229 | if (!IsFunctionInlined) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1230 | // Remove each function that was speculatively created if there is no | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1231 | // reference. | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1232 | for (auto FuncBBPair : OutlinedFunctions) { | 
|  | 1233 | Function *Func = FuncBBPair.first; | 
|  | 1234 | Func->eraseFromParent(); | 
|  | 1235 | } | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1236 | } | 
|  | 1237 | } | 
|  | 1238 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1239 | std::pair<bool, Function *> PartialInlinerImpl::unswitchFunction(Function *F) { | 
|  | 1240 |  | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 1241 | if (F->hasAddressTaken()) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1242 | return {false, nullptr}; | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 1243 |  | 
| Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame] | 1244 | // Let inliner handle it | 
|  | 1245 | if (F->hasFnAttribute(Attribute::AlwaysInline)) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1246 | return {false, nullptr}; | 
| Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame] | 1247 |  | 
|  | 1248 | if (F->hasFnAttribute(Attribute::NoInline)) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1249 | return {false, nullptr}; | 
| Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame] | 1250 |  | 
|  | 1251 | if (PSI->isFunctionEntryCold(F)) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1252 | return {false, nullptr}; | 
| Xinliang David Li | ab8722f | 2017-05-02 18:43:21 +0000 | [diff] [blame] | 1253 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1254 | if (F->user_begin() == F->user_end()) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1255 | return {false, nullptr}; | 
| Xinliang David Li | d21601a | 2017-04-27 16:34:00 +0000 | [diff] [blame] | 1256 |  | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1257 | OptimizationRemarkEmitter ORE(F); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1258 |  | 
|  | 1259 | // Only try to outline cold regions if we have a profile summary, which | 
|  | 1260 | // implies we have profiling information. | 
| Easwaran Raman | a17f220 | 2017-12-22 01:33:52 +0000 | [diff] [blame] | 1261 | if (PSI->hasProfileSummary() && F->hasProfileData() && | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1262 | !DisableMultiRegionPartialInline) { | 
|  | 1263 | std::unique_ptr<FunctionOutliningMultiRegionInfo> OMRI = | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1264 | computeOutliningColdRegionsInfo(F, ORE); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1265 | if (OMRI) { | 
|  | 1266 | FunctionCloner Cloner(F, OMRI.get(), ORE); | 
|  | 1267 |  | 
|  | 1268 | #ifndef NDEBUG | 
|  | 1269 | if (TracePartialInlining) { | 
|  | 1270 | dbgs() << "HotCountThreshold = " << PSI->getHotCountThreshold() << "\n"; | 
|  | 1271 | dbgs() << "ColdCountThreshold = " << PSI->getColdCountThreshold() | 
|  | 1272 | << "\n"; | 
|  | 1273 | } | 
|  | 1274 | #endif | 
|  | 1275 | bool DidOutline = Cloner.doMultiRegionFunctionOutlining(); | 
|  | 1276 |  | 
|  | 1277 | if (DidOutline) { | 
|  | 1278 | #ifndef NDEBUG | 
|  | 1279 | if (TracePartialInlining) { | 
|  | 1280 | dbgs() << ">>>>>> Outlined (Cloned) Function >>>>>>\n"; | 
|  | 1281 | Cloner.ClonedFunc->print(dbgs()); | 
|  | 1282 | dbgs() << "<<<<<< Outlined (Cloned) Function <<<<<<\n"; | 
|  | 1283 | } | 
|  | 1284 | #endif | 
|  | 1285 |  | 
|  | 1286 | if (tryPartialInline(Cloner)) | 
|  | 1287 | return {true, nullptr}; | 
|  | 1288 | } | 
|  | 1289 | } | 
|  | 1290 | } | 
|  | 1291 |  | 
|  | 1292 | // Fall-thru to regular partial inlining if we: | 
|  | 1293 | //    i) can't find any cold regions to outline, or | 
|  | 1294 | //   ii) can't inline the outlined function anywhere. | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1295 | std::unique_ptr<FunctionOutliningInfo> OI = computeOutliningInfo(F); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1296 | if (!OI) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1297 | return {false, nullptr}; | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1298 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1299 | FunctionCloner Cloner(F, OI.get(), ORE); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1300 | Cloner.NormalizeReturnBlock(); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1301 |  | 
|  | 1302 | Function *OutlinedFunction = Cloner.doSingleRegionFunctionOutlining(); | 
|  | 1303 |  | 
|  | 1304 | if (!OutlinedFunction) | 
|  | 1305 | return {false, nullptr}; | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1306 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1307 | bool AnyInline = tryPartialInline(Cloner); | 
| Xinliang David Li | 392e975 | 2017-05-14 02:54:02 +0000 | [diff] [blame] | 1308 |  | 
|  | 1309 | if (AnyInline) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1310 | return {true, OutlinedFunction}; | 
| Xinliang David Li | 392e975 | 2017-05-14 02:54:02 +0000 | [diff] [blame] | 1311 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1312 | return {false, nullptr}; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1313 | } | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1314 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1315 | bool PartialInlinerImpl::tryPartialInline(FunctionCloner &Cloner) { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1316 | if (Cloner.OutlinedFunctions.empty()) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1317 | return false; | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1318 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1319 | int SizeCost = 0; | 
|  | 1320 | BlockFrequency WeightedRcost; | 
|  | 1321 | int NonWeightedRcost; | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1322 | std::tie(SizeCost, NonWeightedRcost) = computeOutliningCosts(Cloner); | 
|  | 1323 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1324 | // Only calculate RelativeToEntryFreq when we are doing single region | 
|  | 1325 | // outlining. | 
|  | 1326 | BranchProbability RelativeToEntryFreq; | 
|  | 1327 | if (Cloner.ClonedOI) { | 
|  | 1328 | RelativeToEntryFreq = getOutliningCallBBRelativeFreq(Cloner); | 
|  | 1329 | } else | 
|  | 1330 | // RelativeToEntryFreq doesn't make sense when we have more than one | 
|  | 1331 | // outlined call because each call will have a different relative frequency | 
|  | 1332 | // to the entry block.  We can consider using the average, but the | 
|  | 1333 | // usefulness of that information is questionable. For now, assume we never | 
|  | 1334 | // execute the calls to outlined functions. | 
|  | 1335 | RelativeToEntryFreq = BranchProbability(0, 1); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1336 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1337 | WeightedRcost = BlockFrequency(NonWeightedRcost) * RelativeToEntryFreq; | 
|  | 1338 |  | 
|  | 1339 | // The call sequence(s) to the outlined function(s) are larger than the sum of | 
|  | 1340 | // the original outlined region size(s), it does not increase the chances of | 
|  | 1341 | // inlining the function with outlining (The inliner uses the size increase to | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1342 | // model the cost of inlining a callee). | 
|  | 1343 | if (!SkipCostAnalysis && Cloner.OutlinedRegionCost < SizeCost) { | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1344 | OptimizationRemarkEmitter OrigFuncORE(Cloner.OrigFunc); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1345 | DebugLoc DLoc; | 
|  | 1346 | BasicBlock *Block; | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1347 | std::tie(DLoc, Block) = getOneDebugLoc(Cloner.ClonedFunc); | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1348 | OrigFuncORE.emit([&]() { | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 1349 | return OptimizationRemarkAnalysis(DEBUG_TYPE, "OutlineRegionTooSmall", | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1350 | DLoc, Block) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1351 | << ore::NV("Function", Cloner.OrigFunc) | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1352 | << " not partially inlined into callers (Original Size = " | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1353 | << ore::NV("OutlinedRegionOriginalSize", Cloner.OutlinedRegionCost) | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1354 | << ", Size of call sequence to outlined function = " | 
| Vivek Pandya | 9590658 | 2017-10-11 17:12:59 +0000 | [diff] [blame] | 1355 | << ore::NV("NewSize", SizeCost) << ")"; | 
|  | 1356 | }); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1357 | return false; | 
|  | 1358 | } | 
|  | 1359 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1360 | assert(Cloner.OrigFunc->user_begin() == Cloner.OrigFunc->user_end() && | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1361 | "F's users should all be replaced!"); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1362 |  | 
|  | 1363 | std::vector<User *> Users(Cloner.ClonedFunc->user_begin(), | 
|  | 1364 | Cloner.ClonedFunc->user_end()); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1365 |  | 
|  | 1366 | DenseMap<User *, uint64_t> CallSiteToProfCountMap; | 
| Easwaran Raman | a17f220 | 2017-12-22 01:33:52 +0000 | [diff] [blame] | 1367 | auto CalleeEntryCount = Cloner.OrigFunc->getEntryCount(); | 
|  | 1368 | if (CalleeEntryCount) | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1369 | computeCallsiteToProfCountMap(Cloner.ClonedFunc, CallSiteToProfCountMap); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1370 |  | 
| Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 1371 | uint64_t CalleeEntryCountV = | 
|  | 1372 | (CalleeEntryCount ? CalleeEntryCount.getCount() : 0); | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1373 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1374 | bool AnyInline = false; | 
|  | 1375 | for (User *User : Users) { | 
|  | 1376 | CallSite CS = getCallSite(User); | 
|  | 1377 |  | 
|  | 1378 | if (IsLimitReached()) | 
|  | 1379 | continue; | 
|  | 1380 |  | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1381 | OptimizationRemarkEmitter CallerORE(CS.getCaller()); | 
|  | 1382 | if (!shouldPartialInline(CS, Cloner, WeightedRcost, CallerORE)) | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1383 | continue; | 
|  | 1384 |  | 
| Florian Hahn | 41e32bf | 2017-11-03 11:29:00 +0000 | [diff] [blame] | 1385 | // Construct remark before doing the inlining, as after successful inlining | 
|  | 1386 | // the callsite is removed. | 
|  | 1387 | OptimizationRemark OR(DEBUG_TYPE, "PartiallyInlined", CS.getInstruction()); | 
|  | 1388 | OR << ore::NV("Callee", Cloner.OrigFunc) << " partially inlined into " | 
|  | 1389 | << ore::NV("Caller", CS.getCaller()); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1390 |  | 
|  | 1391 | InlineFunctionInfo IFI(nullptr, GetAssumptionCache, PSI); | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1392 | // We can only forward varargs when we outlined a single region, else we | 
|  | 1393 | // bail on vararg functions. | 
|  | 1394 | if (!InlineFunction(CS, IFI, nullptr, true, | 
|  | 1395 | (Cloner.ClonedOI ? Cloner.OutlinedFunctions.back().first | 
|  | 1396 | : nullptr))) | 
| Florian Hahn | 41e32bf | 2017-11-03 11:29:00 +0000 | [diff] [blame] | 1397 | continue; | 
|  | 1398 |  | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1399 | CallerORE.emit(OR); | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1400 |  | 
|  | 1401 | // Now update the entry count: | 
|  | 1402 | if (CalleeEntryCountV && CallSiteToProfCountMap.count(User)) { | 
|  | 1403 | uint64_t CallSiteCount = CallSiteToProfCountMap[User]; | 
|  | 1404 | CalleeEntryCountV -= std::min(CalleeEntryCountV, CallSiteCount); | 
|  | 1405 | } | 
|  | 1406 |  | 
|  | 1407 | AnyInline = true; | 
|  | 1408 | NumPartialInlining++; | 
|  | 1409 | // Update the stats | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1410 | if (Cloner.ClonedOI) | 
|  | 1411 | NumPartialInlined++; | 
|  | 1412 | else | 
|  | 1413 | NumColdOutlinePartialInlined++; | 
|  | 1414 |  | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1415 | } | 
|  | 1416 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1417 | if (AnyInline) { | 
|  | 1418 | Cloner.IsFunctionInlined = true; | 
|  | 1419 | if (CalleeEntryCount) | 
| Easwaran Raman | e5b8de2 | 2018-01-17 22:24:23 +0000 | [diff] [blame] | 1420 | Cloner.OrigFunc->setEntryCount( | 
|  | 1421 | CalleeEntryCount.setCount(CalleeEntryCountV)); | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1422 | OptimizationRemarkEmitter OrigFuncORE(Cloner.OrigFunc); | 
|  | 1423 | OrigFuncORE.emit([&]() { | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1424 | return OptimizationRemark(DEBUG_TYPE, "PartiallyInlined", Cloner.OrigFunc) | 
|  | 1425 | << "Partially inlined into at least one caller"; | 
|  | 1426 | }); | 
|  | 1427 |  | 
| Xinliang David Li | eea0ade | 2017-06-15 23:56:59 +0000 | [diff] [blame] | 1428 | } | 
| Xinliang David Li | 66bdfca | 2017-05-12 23:41:43 +0000 | [diff] [blame] | 1429 |  | 
|  | 1430 | return AnyInline; | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1431 | } | 
|  | 1432 |  | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1433 | bool PartialInlinerImpl::run(Module &M) { | 
| Xinliang David Li | db8d09b | 2017-04-23 23:39:04 +0000 | [diff] [blame] | 1434 | if (DisablePartialInlining) | 
|  | 1435 | return false; | 
|  | 1436 |  | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1437 | std::vector<Function *> Worklist; | 
|  | 1438 | Worklist.reserve(M.size()); | 
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1439 | for (Function &F : M) | 
|  | 1440 | if (!F.use_empty() && !F.isDeclaration()) | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1441 | Worklist.push_back(&F); | 
| Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 1442 |  | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1443 | bool Changed = false; | 
|  | 1444 | while (!Worklist.empty()) { | 
|  | 1445 | Function *CurrFunc = Worklist.back(); | 
|  | 1446 | Worklist.pop_back(); | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1447 |  | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1448 | if (CurrFunc->use_empty()) | 
|  | 1449 | continue; | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1450 |  | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1451 | bool Recursive = false; | 
|  | 1452 | for (User *U : CurrFunc->users()) | 
|  | 1453 | if (Instruction *I = dyn_cast<Instruction>(U)) | 
|  | 1454 | if (I->getParent()->getParent() == CurrFunc) { | 
|  | 1455 | Recursive = true; | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1456 | break; | 
|  | 1457 | } | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1458 | if (Recursive) | 
|  | 1459 | continue; | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1460 |  | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1461 | std::pair<bool, Function * > Result = unswitchFunction(CurrFunc); | 
|  | 1462 | if (Result.second) | 
|  | 1463 | Worklist.push_back(Result.second); | 
|  | 1464 | if (Result.first) { | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1465 | Changed = true; | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1466 | } | 
| Owen Anderson | 2f82e27 | 2009-06-14 08:26:32 +0000 | [diff] [blame] | 1467 | } | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 1468 |  | 
| Sean Silva | 519323d | 2016-07-25 05:57:59 +0000 | [diff] [blame] | 1469 | return Changed; | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1470 | } | 
|  | 1471 |  | 
|  | 1472 | char PartialInlinerLegacyPass::ID = 0; | 
| Eugene Zelenko | e9ea08a | 2017-10-10 22:49:55 +0000 | [diff] [blame] | 1473 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1474 | INITIALIZE_PASS_BEGIN(PartialInlinerLegacyPass, "partial-inliner", | 
|  | 1475 | "Partial Inliner", false, false) | 
|  | 1476 | INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 1477 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) | 
|  | 1478 | INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1479 | INITIALIZE_PASS_END(PartialInlinerLegacyPass, "partial-inliner", | 
|  | 1480 | "Partial Inliner", false, false) | 
| Sean Silva | fe5abd5 | 2016-07-25 05:00:00 +0000 | [diff] [blame] | 1481 |  | 
|  | 1482 | ModulePass *llvm::createPartialInliningPass() { | 
|  | 1483 | return new PartialInlinerLegacyPass(); | 
|  | 1484 | } | 
|  | 1485 |  | 
|  | 1486 | PreservedAnalyses PartialInlinerPass::run(Module &M, | 
|  | 1487 | ModuleAnalysisManager &AM) { | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1488 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 1489 |  | 
| Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 1490 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = | 
|  | 1491 | [&FAM](Function &F) -> AssumptionCache & { | 
|  | 1492 | return FAM.getResult<AssumptionAnalysis>(F); | 
|  | 1493 | }; | 
| Xinliang David Li | 6133846 | 2017-05-02 02:44:14 +0000 | [diff] [blame] | 1494 |  | 
|  | 1495 | std::function<BlockFrequencyInfo &(Function &)> GetBFI = | 
|  | 1496 | [&FAM](Function &F) -> BlockFrequencyInfo & { | 
|  | 1497 | return FAM.getResult<BlockFrequencyAnalysis>(F); | 
|  | 1498 | }; | 
|  | 1499 |  | 
|  | 1500 | std::function<TargetTransformInfo &(Function &)> GetTTI = | 
|  | 1501 | [&FAM](Function &F) -> TargetTransformInfo & { | 
|  | 1502 | return FAM.getResult<TargetIRAnalysis>(F); | 
|  | 1503 | }; | 
|  | 1504 |  | 
|  | 1505 | ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M); | 
|  | 1506 |  | 
| Sean Fertile | 18f1733 | 2018-04-20 19:56:26 +0000 | [diff] [blame] | 1507 | if (PartialInlinerImpl(&GetAssumptionCache, &GetTTI, {GetBFI}, PSI) | 
| Graham Yiu | 8b1882c | 2017-11-30 02:41:36 +0000 | [diff] [blame] | 1508 | .run(M)) | 
| Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 1509 | return PreservedAnalyses::none(); | 
|  | 1510 | return PreservedAnalyses::all(); | 
| Duncan Sands | 29c8efc | 2009-07-03 15:30:58 +0000 | [diff] [blame] | 1511 | } |