Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 1 | //===- HotColdSplitting.cpp -- Outline Cold Regions -------------*- C++ -*-===// |
| 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 | // Outline cold regions to a separate function. |
| 11 | // TODO: Update BFI and BPI |
| 12 | // TODO: Add all the outlined functions to a separate section. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/PostOrderIterator.h" |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/Statistic.h" |
| 19 | #include "llvm/Analysis/AliasAnalysis.h" |
| 20 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
| 21 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
| 22 | #include "llvm/Analysis/CFG.h" |
| 23 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
| 24 | #include "llvm/Analysis/PostDominators.h" |
| 25 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/TargetTransformInfo.h" |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 27 | #include "llvm/IR/BasicBlock.h" |
| 28 | #include "llvm/IR/CFG.h" |
Vedant Kumar | 03f9f15 | 2018-12-07 20:24:04 +0000 | [diff] [blame] | 29 | #include "llvm/IR/CallSite.h" |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 30 | #include "llvm/IR/DataLayout.h" |
| 31 | #include "llvm/IR/DiagnosticInfo.h" |
| 32 | #include "llvm/IR/Dominators.h" |
| 33 | #include "llvm/IR/Function.h" |
| 34 | #include "llvm/IR/Instruction.h" |
| 35 | #include "llvm/IR/Instructions.h" |
Vedant Kumar | dd4be53 | 2018-10-29 19:15:39 +0000 | [diff] [blame] | 36 | #include "llvm/IR/IntrinsicInst.h" |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 37 | #include "llvm/IR/Metadata.h" |
| 38 | #include "llvm/IR/Module.h" |
| 39 | #include "llvm/IR/PassManager.h" |
| 40 | #include "llvm/IR/Type.h" |
| 41 | #include "llvm/IR/Use.h" |
| 42 | #include "llvm/IR/User.h" |
| 43 | #include "llvm/IR/Value.h" |
| 44 | #include "llvm/Pass.h" |
| 45 | #include "llvm/Support/BlockFrequency.h" |
| 46 | #include "llvm/Support/BranchProbability.h" |
| 47 | #include "llvm/Support/Debug.h" |
| 48 | #include "llvm/Support/raw_ostream.h" |
| 49 | #include "llvm/Transforms/IPO.h" |
Aditya Kumar | 9e20ade | 2018-10-03 05:55:20 +0000 | [diff] [blame] | 50 | #include "llvm/Transforms/IPO/HotColdSplitting.h" |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Scalar.h" |
| 52 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 53 | #include "llvm/Transforms/Utils/Cloning.h" |
| 54 | #include "llvm/Transforms/Utils/CodeExtractor.h" |
| 55 | #include "llvm/Transforms/Utils/Local.h" |
| 56 | #include "llvm/Transforms/Utils/SSAUpdater.h" |
| 57 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 58 | #include <algorithm> |
| 59 | #include <cassert> |
| 60 | |
| 61 | #define DEBUG_TYPE "hotcoldsplit" |
| 62 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 63 | STATISTIC(NumColdRegionsFound, "Number of cold regions found."); |
| 64 | STATISTIC(NumColdRegionsOutlined, "Number of cold regions outlined."); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 65 | |
| 66 | using namespace llvm; |
| 67 | |
| 68 | static cl::opt<bool> EnableStaticAnalyis("hot-cold-static-analysis", |
| 69 | cl::init(true), cl::Hidden); |
| 70 | |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 71 | static cl::opt<int> |
| 72 | MinOutliningThreshold("min-outlining-thresh", cl::init(3), cl::Hidden, |
| 73 | cl::desc("Code size threshold for outlining within a " |
| 74 | "single BB (as a multiple of TCC_Basic)")); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 75 | |
| 76 | namespace { |
| 77 | |
| 78 | struct PostDomTree : PostDomTreeBase<BasicBlock> { |
| 79 | PostDomTree(Function &F) { recalculate(F); } |
| 80 | }; |
| 81 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 82 | /// A sequence of basic blocks. |
| 83 | /// |
| 84 | /// A 0-sized SmallVector is slightly cheaper to move than a std::vector. |
| 85 | using BlockSequence = SmallVector<BasicBlock *, 0>; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 86 | |
Sebastian Pop | 542e522 | 2018-10-15 21:43:11 +0000 | [diff] [blame] | 87 | // Same as blockEndsInUnreachable in CodeGen/BranchFolding.cpp. Do not modify |
| 88 | // this function unless you modify the MBB version as well. |
| 89 | // |
| 90 | /// A no successor, non-return block probably ends in unreachable and is cold. |
| 91 | /// Also consider a block that ends in an indirect branch to be a return block, |
| 92 | /// since many targets use plain indirect branches to return. |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 93 | bool blockEndsInUnreachable(const BasicBlock &BB) { |
Sebastian Pop | 542e522 | 2018-10-15 21:43:11 +0000 | [diff] [blame] | 94 | if (!succ_empty(&BB)) |
| 95 | return false; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 96 | if (BB.empty()) |
| 97 | return true; |
Chandler Carruth | edb12a8 | 2018-10-15 10:04:59 +0000 | [diff] [blame] | 98 | const Instruction *I = BB.getTerminator(); |
Sebastian Pop | 542e522 | 2018-10-15 21:43:11 +0000 | [diff] [blame] | 99 | return !(isa<ReturnInst>(I) || isa<IndirectBrInst>(I)); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Vedant Kumar | 03f9f15 | 2018-12-07 20:24:04 +0000 | [diff] [blame] | 102 | bool unlikelyExecuted(BasicBlock &BB) { |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 103 | // Exception handling blocks are unlikely executed. |
| 104 | if (BB.isEHPad()) |
| 105 | return true; |
Vedant Kumar | 03f9f15 | 2018-12-07 20:24:04 +0000 | [diff] [blame] | 106 | |
| 107 | // The block is cold if it calls/invokes a cold function. |
| 108 | for (Instruction &I : BB) |
| 109 | if (auto CS = CallSite(&I)) |
| 110 | if (CS.hasFnAttr(Attribute::Cold)) |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 111 | return true; |
| 112 | |
Vedant Kumar | 03f9f15 | 2018-12-07 20:24:04 +0000 | [diff] [blame] | 113 | // The block is cold if it has an unreachable terminator, unless it's |
| 114 | // preceded by a call to a (possibly warm) noreturn call (e.g. longjmp). |
| 115 | if (blockEndsInUnreachable(BB)) { |
| 116 | if (auto *CI = |
| 117 | dyn_cast_or_null<CallInst>(BB.getTerminator()->getPrevNode())) |
| 118 | if (CI->hasFnAttr(Attribute::NoReturn)) |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 119 | return false; |
Vedant Kumar | 03f9f15 | 2018-12-07 20:24:04 +0000 | [diff] [blame] | 120 | return true; |
| 121 | } |
| 122 | |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 126 | /// Check whether it's safe to outline \p BB. |
| 127 | static bool mayExtractBlock(const BasicBlock &BB) { |
Vedant Kumar | b3a7cae | 2018-12-11 18:05:31 +0000 | [diff] [blame] | 128 | return !BB.hasAddressTaken() && !BB.isEHPad(); |
Sebastian Pop | 542e522 | 2018-10-15 21:43:11 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 131 | /// Check whether \p Region is profitable to outline. |
| 132 | static bool isProfitableToOutline(const BlockSequence &Region, |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 133 | TargetTransformInfo &TTI) { |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 134 | if (Region.size() > 1) |
| 135 | return true; |
| 136 | |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 137 | int Cost = 0; |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 138 | const BasicBlock &BB = *Region[0]; |
Vedant Kumar | dd4be53 | 2018-10-29 19:15:39 +0000 | [diff] [blame] | 139 | for (const Instruction &I : BB) { |
| 140 | if (isa<DbgInfoIntrinsic>(&I) || &I == BB.getTerminator()) |
| 141 | continue; |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 142 | |
| 143 | Cost += TTI.getInstructionCost(&I, TargetTransformInfo::TCK_CodeSize); |
| 144 | |
| 145 | if (Cost >= (MinOutliningThreshold * TargetTransformInfo::TCC_Basic)) |
Vedant Kumar | dd4be53 | 2018-10-29 19:15:39 +0000 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 151 | /// Mark \p F cold. Return true if it's changed. |
| 152 | static bool markEntireFunctionCold(Function &F) { |
| 153 | assert(!F.hasFnAttribute(Attribute::OptimizeNone) && "Can't mark this cold"); |
| 154 | bool Changed = false; |
| 155 | if (!F.hasFnAttribute(Attribute::MinSize)) { |
| 156 | F.addFnAttr(Attribute::MinSize); |
| 157 | Changed = true; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 158 | } |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 159 | // TODO: Move this function into a cold section. |
| 160 | return Changed; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | class HotColdSplitting { |
| 164 | public: |
| 165 | HotColdSplitting(ProfileSummaryInfo *ProfSI, |
| 166 | function_ref<BlockFrequencyInfo *(Function &)> GBFI, |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 167 | function_ref<TargetTransformInfo &(Function &)> GTTI, |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 168 | std::function<OptimizationRemarkEmitter &(Function &)> *GORE) |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 169 | : PSI(ProfSI), GetBFI(GBFI), GetTTI(GTTI), GetORE(GORE) {} |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 170 | bool run(Module &M); |
| 171 | |
| 172 | private: |
| 173 | bool shouldOutlineFrom(const Function &F) const; |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 174 | bool outlineColdRegions(Function &F, ProfileSummaryInfo &PSI, |
| 175 | BlockFrequencyInfo *BFI, TargetTransformInfo &TTI, |
| 176 | DominatorTree &DT, PostDomTree &PDT, |
| 177 | OptimizationRemarkEmitter &ORE); |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 178 | Function *extractColdRegion(const BlockSequence &Region, DominatorTree &DT, |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 179 | BlockFrequencyInfo *BFI, TargetTransformInfo &TTI, |
Teresa Johnson | c8dba68 | 2018-10-24 18:53:47 +0000 | [diff] [blame] | 180 | OptimizationRemarkEmitter &ORE, unsigned Count); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 181 | SmallPtrSet<const Function *, 2> OutlinedFunctions; |
| 182 | ProfileSummaryInfo *PSI; |
| 183 | function_ref<BlockFrequencyInfo *(Function &)> GetBFI; |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 184 | function_ref<TargetTransformInfo &(Function &)> GetTTI; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 185 | std::function<OptimizationRemarkEmitter &(Function &)> *GetORE; |
| 186 | }; |
| 187 | |
| 188 | class HotColdSplittingLegacyPass : public ModulePass { |
| 189 | public: |
| 190 | static char ID; |
| 191 | HotColdSplittingLegacyPass() : ModulePass(ID) { |
| 192 | initializeHotColdSplittingLegacyPassPass(*PassRegistry::getPassRegistry()); |
| 193 | } |
| 194 | |
| 195 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 196 | AU.addRequired<AssumptionCacheTracker>(); |
| 197 | AU.addRequired<BlockFrequencyInfoWrapperPass>(); |
| 198 | AU.addRequired<ProfileSummaryInfoWrapperPass>(); |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 199 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | bool runOnModule(Module &M) override; |
| 203 | }; |
| 204 | |
| 205 | } // end anonymous namespace |
| 206 | |
| 207 | // Returns false if the function should not be considered for hot-cold split |
Sebastian Pop | 0f30f08 | 2018-09-14 20:36:19 +0000 | [diff] [blame] | 208 | // optimization. |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 209 | bool HotColdSplitting::shouldOutlineFrom(const Function &F) const { |
Sebastian Pop | 0f30f08 | 2018-09-14 20:36:19 +0000 | [diff] [blame] | 210 | // Do not try to outline again from an already outlined cold function. |
| 211 | if (OutlinedFunctions.count(&F)) |
| 212 | return false; |
| 213 | |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 214 | if (F.size() <= 2) |
| 215 | return false; |
| 216 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 217 | // TODO: Consider only skipping functions marked `optnone` or `cold`. |
| 218 | |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 219 | if (F.hasAddressTaken()) |
| 220 | return false; |
| 221 | |
| 222 | if (F.hasFnAttribute(Attribute::AlwaysInline)) |
| 223 | return false; |
| 224 | |
| 225 | if (F.hasFnAttribute(Attribute::NoInline)) |
| 226 | return false; |
| 227 | |
| 228 | if (F.getCallingConv() == CallingConv::Cold) |
| 229 | return false; |
| 230 | |
| 231 | if (PSI->isFunctionEntryCold(&F)) |
| 232 | return false; |
| 233 | return true; |
| 234 | } |
| 235 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 236 | Function *HotColdSplitting::extractColdRegion(const BlockSequence &Region, |
| 237 | DominatorTree &DT, |
| 238 | BlockFrequencyInfo *BFI, |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 239 | TargetTransformInfo &TTI, |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 240 | OptimizationRemarkEmitter &ORE, |
| 241 | unsigned Count) { |
Teresa Johnson | f431a2f | 2018-10-22 19:06:42 +0000 | [diff] [blame] | 242 | assert(!Region.empty()); |
Sebastian Pop | 1217160 | 2018-09-14 20:36:10 +0000 | [diff] [blame] | 243 | |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 244 | // TODO: Pass BFI and BPI to update profile information. |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 245 | CodeExtractor CE(Region, &DT, /* AggregateArgs */ false, /* BFI */ nullptr, |
Teresa Johnson | c8dba68 | 2018-10-24 18:53:47 +0000 | [diff] [blame] | 246 | /* BPI */ nullptr, /* AllowVarArgs */ false, |
| 247 | /* AllowAlloca */ false, |
| 248 | /* Suffix */ "cold." + std::to_string(Count)); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 249 | |
| 250 | SetVector<Value *> Inputs, Outputs, Sinks; |
| 251 | CE.findInputsOutputs(Inputs, Outputs, Sinks); |
| 252 | |
| 253 | // Do not extract regions that have live exit variables. |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 254 | if (Outputs.size() > 0) { |
| 255 | LLVM_DEBUG(llvm::dbgs() << "Not outlining; live outputs\n"); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 256 | return nullptr; |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 257 | } |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 258 | |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 259 | // TODO: Run MergeBasicBlockIntoOnlyPred on the outlined function. |
Teresa Johnson | f431a2f | 2018-10-22 19:06:42 +0000 | [diff] [blame] | 260 | Function *OrigF = Region[0]->getParent(); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 261 | if (Function *OutF = CE.extractCodeRegion()) { |
| 262 | User *U = *OutF->user_begin(); |
| 263 | CallInst *CI = cast<CallInst>(U); |
| 264 | CallSite CS(CI); |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 265 | NumColdRegionsOutlined++; |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 266 | if (TTI.useColdCCForColdCall(*OutF)) { |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 267 | OutF->setCallingConv(CallingConv::Cold); |
| 268 | CS.setCallingConv(CallingConv::Cold); |
| 269 | } |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 270 | CI->setIsNoInline(); |
Vedant Kumar | 5031546 | 2018-10-23 19:41:12 +0000 | [diff] [blame] | 271 | |
| 272 | // Try to make the outlined code as small as possible on the assumption |
| 273 | // that it's cold. |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 274 | markEntireFunctionCold(*OutF); |
Vedant Kumar | 5031546 | 2018-10-23 19:41:12 +0000 | [diff] [blame] | 275 | |
Sebastian Pop | 0f30f08 | 2018-09-14 20:36:19 +0000 | [diff] [blame] | 276 | LLVM_DEBUG(llvm::dbgs() << "Outlined Region: " << *OutF); |
Teresa Johnson | f431a2f | 2018-10-22 19:06:42 +0000 | [diff] [blame] | 277 | ORE.emit([&]() { |
| 278 | return OptimizationRemark(DEBUG_TYPE, "HotColdSplit", |
| 279 | &*Region[0]->begin()) |
| 280 | << ore::NV("Original", OrigF) << " split cold code into " |
| 281 | << ore::NV("Split", OutF); |
| 282 | }); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 283 | return OutF; |
| 284 | } |
| 285 | |
| 286 | ORE.emit([&]() { |
| 287 | return OptimizationRemarkMissed(DEBUG_TYPE, "ExtractFailed", |
| 288 | &*Region[0]->begin()) |
| 289 | << "Failed to extract region at block " |
| 290 | << ore::NV("Block", Region.front()); |
| 291 | }); |
| 292 | return nullptr; |
| 293 | } |
| 294 | |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 295 | /// A pair of (basic block, score). |
| 296 | using BlockTy = std::pair<BasicBlock *, unsigned>; |
| 297 | |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame^] | 298 | namespace { |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 299 | /// A maximal outlining region. This contains all blocks post-dominated by a |
| 300 | /// sink block, the sink block itself, and all blocks dominated by the sink. |
| 301 | class OutliningRegion { |
| 302 | /// A list of (block, score) pairs. A block's score is non-zero iff it's a |
| 303 | /// viable sub-region entry point. Blocks with higher scores are better entry |
| 304 | /// points (i.e. they are more distant ancestors of the sink block). |
| 305 | SmallVector<BlockTy, 0> Blocks = {}; |
| 306 | |
| 307 | /// The suggested entry point into the region. If the region has multiple |
| 308 | /// entry points, all blocks within the region may not be reachable from this |
| 309 | /// entry point. |
| 310 | BasicBlock *SuggestedEntryPoint = nullptr; |
| 311 | |
| 312 | /// Whether the entire function is cold. |
| 313 | bool EntireFunctionCold = false; |
| 314 | |
| 315 | /// Whether or not \p BB could be the entry point of an extracted region. |
| 316 | static bool isViableEntryPoint(BasicBlock &BB) { return !BB.isEHPad(); } |
| 317 | |
| 318 | /// If \p BB is a viable entry point, return \p Score. Return 0 otherwise. |
| 319 | static unsigned getEntryPointScore(BasicBlock &BB, unsigned Score) { |
| 320 | return isViableEntryPoint(BB) ? Score : 0; |
| 321 | } |
| 322 | |
| 323 | /// These scores should be lower than the score for predecessor blocks, |
| 324 | /// because regions starting at predecessor blocks are typically larger. |
| 325 | static constexpr unsigned ScoreForSuccBlock = 1; |
| 326 | static constexpr unsigned ScoreForSinkBlock = 1; |
| 327 | |
| 328 | OutliningRegion(const OutliningRegion &) = delete; |
| 329 | OutliningRegion &operator=(const OutliningRegion &) = delete; |
| 330 | |
| 331 | public: |
| 332 | OutliningRegion() = default; |
| 333 | OutliningRegion(OutliningRegion &&) = default; |
| 334 | OutliningRegion &operator=(OutliningRegion &&) = default; |
| 335 | |
| 336 | static OutliningRegion create(BasicBlock &SinkBB, const DominatorTree &DT, |
| 337 | const PostDomTree &PDT) { |
| 338 | OutliningRegion ColdRegion; |
| 339 | |
| 340 | SmallPtrSet<BasicBlock *, 4> RegionBlocks; |
| 341 | |
| 342 | auto addBlockToRegion = [&](BasicBlock *BB, unsigned Score) { |
| 343 | RegionBlocks.insert(BB); |
| 344 | ColdRegion.Blocks.emplace_back(BB, Score); |
| 345 | assert(RegionBlocks.size() == ColdRegion.Blocks.size() && "Duplicate BB"); |
| 346 | }; |
| 347 | |
| 348 | // The ancestor farthest-away from SinkBB, and also post-dominated by it. |
| 349 | unsigned SinkScore = getEntryPointScore(SinkBB, ScoreForSinkBlock); |
| 350 | ColdRegion.SuggestedEntryPoint = (SinkScore > 0) ? &SinkBB : nullptr; |
| 351 | unsigned BestScore = SinkScore; |
| 352 | |
| 353 | // Visit SinkBB's ancestors using inverse DFS. |
| 354 | auto PredIt = ++idf_begin(&SinkBB); |
| 355 | auto PredEnd = idf_end(&SinkBB); |
| 356 | while (PredIt != PredEnd) { |
| 357 | BasicBlock &PredBB = **PredIt; |
| 358 | bool SinkPostDom = PDT.dominates(&SinkBB, &PredBB); |
| 359 | |
| 360 | // If the predecessor is cold and has no predecessors, the entire |
| 361 | // function must be cold. |
| 362 | if (SinkPostDom && pred_empty(&PredBB)) { |
| 363 | ColdRegion.EntireFunctionCold = true; |
| 364 | return ColdRegion; |
| 365 | } |
| 366 | |
| 367 | // If SinkBB does not post-dominate a predecessor, do not mark the |
| 368 | // predecessor (or any of its predecessors) cold. |
| 369 | if (!SinkPostDom || !mayExtractBlock(PredBB)) { |
| 370 | PredIt.skipChildren(); |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | // Keep track of the post-dominated ancestor farthest away from the sink. |
| 375 | // The path length is always >= 2, ensuring that predecessor blocks are |
| 376 | // considered as entry points before the sink block. |
| 377 | unsigned PredScore = getEntryPointScore(PredBB, PredIt.getPathLength()); |
| 378 | if (PredScore > BestScore) { |
| 379 | ColdRegion.SuggestedEntryPoint = &PredBB; |
| 380 | BestScore = PredScore; |
| 381 | } |
| 382 | |
| 383 | addBlockToRegion(&PredBB, PredScore); |
| 384 | ++PredIt; |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 387 | // Add SinkBB to the cold region. It's considered as an entry point before |
| 388 | // any sink-successor blocks. |
| 389 | addBlockToRegion(&SinkBB, SinkScore); |
| 390 | |
| 391 | // Find all successors of SinkBB dominated by SinkBB using DFS. |
| 392 | auto SuccIt = ++df_begin(&SinkBB); |
| 393 | auto SuccEnd = df_end(&SinkBB); |
| 394 | while (SuccIt != SuccEnd) { |
| 395 | BasicBlock &SuccBB = **SuccIt; |
| 396 | bool SinkDom = DT.dominates(&SinkBB, &SuccBB); |
| 397 | |
| 398 | // Don't allow the backwards & forwards DFSes to mark the same block. |
| 399 | bool DuplicateBlock = RegionBlocks.count(&SuccBB); |
| 400 | |
| 401 | // If SinkBB does not dominate a successor, do not mark the successor (or |
| 402 | // any of its successors) cold. |
| 403 | if (DuplicateBlock || !SinkDom || !mayExtractBlock(SuccBB)) { |
| 404 | SuccIt.skipChildren(); |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | unsigned SuccScore = getEntryPointScore(SuccBB, ScoreForSuccBlock); |
| 409 | if (SuccScore > BestScore) { |
| 410 | ColdRegion.SuggestedEntryPoint = &SuccBB; |
| 411 | BestScore = SuccScore; |
| 412 | } |
| 413 | |
| 414 | addBlockToRegion(&SuccBB, SuccScore); |
| 415 | ++SuccIt; |
| 416 | } |
| 417 | |
| 418 | return ColdRegion; |
| 419 | } |
| 420 | |
| 421 | /// Whether this region has nothing to extract. |
| 422 | bool empty() const { return !SuggestedEntryPoint; } |
| 423 | |
| 424 | /// The blocks in this region. |
| 425 | ArrayRef<std::pair<BasicBlock *, unsigned>> blocks() const { return Blocks; } |
| 426 | |
| 427 | /// Whether the entire function containing this region is cold. |
| 428 | bool isEntireFunctionCold() const { return EntireFunctionCold; } |
| 429 | |
| 430 | /// Remove a sub-region from this region and return it as a block sequence. |
| 431 | BlockSequence takeSingleEntrySubRegion(DominatorTree &DT) { |
| 432 | assert(!empty() && !isEntireFunctionCold() && "Nothing to extract"); |
| 433 | |
| 434 | // Remove blocks dominated by the suggested entry point from this region. |
| 435 | // During the removal, identify the next best entry point into the region. |
| 436 | // Ensure that the first extracted block is the suggested entry point. |
| 437 | BlockSequence SubRegion = {SuggestedEntryPoint}; |
| 438 | BasicBlock *NextEntryPoint = nullptr; |
| 439 | unsigned NextScore = 0; |
| 440 | auto RegionEndIt = Blocks.end(); |
| 441 | auto RegionStartIt = remove_if(Blocks, [&](const BlockTy &Block) { |
| 442 | BasicBlock *BB = Block.first; |
| 443 | unsigned Score = Block.second; |
| 444 | bool InSubRegion = |
| 445 | BB == SuggestedEntryPoint || DT.dominates(SuggestedEntryPoint, BB); |
| 446 | if (!InSubRegion && Score > NextScore) { |
| 447 | NextEntryPoint = BB; |
| 448 | NextScore = Score; |
| 449 | } |
| 450 | if (InSubRegion && BB != SuggestedEntryPoint) |
| 451 | SubRegion.push_back(BB); |
| 452 | return InSubRegion; |
| 453 | }); |
| 454 | Blocks.erase(RegionStartIt, RegionEndIt); |
| 455 | |
| 456 | // Update the suggested entry point. |
| 457 | SuggestedEntryPoint = NextEntryPoint; |
| 458 | |
| 459 | return SubRegion; |
| 460 | } |
| 461 | }; |
Benjamin Kramer | b17d213 | 2019-01-12 18:36:22 +0000 | [diff] [blame^] | 462 | } // namespace |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 463 | |
| 464 | bool HotColdSplitting::outlineColdRegions(Function &F, ProfileSummaryInfo &PSI, |
| 465 | BlockFrequencyInfo *BFI, |
| 466 | TargetTransformInfo &TTI, |
| 467 | DominatorTree &DT, PostDomTree &PDT, |
| 468 | OptimizationRemarkEmitter &ORE) { |
| 469 | bool Changed = false; |
| 470 | |
| 471 | // The set of cold blocks. |
| 472 | SmallPtrSet<BasicBlock *, 4> ColdBlocks; |
| 473 | |
| 474 | // The worklist of non-intersecting regions left to outline. |
| 475 | SmallVector<OutliningRegion, 2> OutliningWorklist; |
| 476 | |
| 477 | // Set up an RPO traversal. Experimentally, this performs better (outlines |
| 478 | // more) than a PO traversal, because we prevent region overlap by keeping |
| 479 | // the first region to contain a block. |
| 480 | ReversePostOrderTraversal<Function *> RPOT(&F); |
| 481 | |
| 482 | // Find all cold regions. |
| 483 | for (BasicBlock *BB : RPOT) { |
| 484 | // Skip blocks which can't be outlined. |
| 485 | if (!mayExtractBlock(*BB)) |
| 486 | continue; |
| 487 | |
| 488 | // This block is already part of some outlining region. |
| 489 | if (ColdBlocks.count(BB)) |
| 490 | continue; |
| 491 | |
| 492 | bool Cold = PSI.isColdBlock(BB, BFI) || |
| 493 | (EnableStaticAnalyis && unlikelyExecuted(*BB)); |
| 494 | if (!Cold) |
| 495 | continue; |
| 496 | |
| 497 | LLVM_DEBUG({ |
| 498 | dbgs() << "Found a cold block:\n"; |
| 499 | BB->dump(); |
| 500 | }); |
| 501 | |
| 502 | auto Region = OutliningRegion::create(*BB, DT, PDT); |
| 503 | if (Region.empty()) |
| 504 | continue; |
| 505 | |
| 506 | if (Region.isEntireFunctionCold()) { |
| 507 | LLVM_DEBUG(dbgs() << "Entire function is cold\n"); |
| 508 | return markEntireFunctionCold(F); |
| 509 | } |
| 510 | |
| 511 | // If this outlining region intersects with another, drop the new region. |
| 512 | // |
| 513 | // TODO: It's theoretically possible to outline more by only keeping the |
| 514 | // largest region which contains a block, but the extra bookkeeping to do |
| 515 | // this is tricky/expensive. |
| 516 | bool RegionsOverlap = any_of(Region.blocks(), [&](const BlockTy &Block) { |
| 517 | return !ColdBlocks.insert(Block.first).second; |
| 518 | }); |
| 519 | if (RegionsOverlap) |
| 520 | continue; |
| 521 | |
| 522 | OutliningWorklist.emplace_back(std::move(Region)); |
| 523 | ++NumColdRegionsFound; |
| 524 | } |
| 525 | |
| 526 | // Outline single-entry cold regions, splitting up larger regions as needed. |
| 527 | unsigned OutlinedFunctionID = 1; |
| 528 | while (!OutliningWorklist.empty()) { |
| 529 | OutliningRegion Region = OutliningWorklist.pop_back_val(); |
| 530 | assert(!Region.empty() && "Empty outlining region in worklist"); |
| 531 | do { |
| 532 | BlockSequence SubRegion = Region.takeSingleEntrySubRegion(DT); |
| 533 | if (!isProfitableToOutline(SubRegion, TTI)) { |
| 534 | LLVM_DEBUG({ |
| 535 | dbgs() << "Skipping outlining; not profitable to outline\n"; |
| 536 | SubRegion[0]->dump(); |
| 537 | }); |
| 538 | continue; |
| 539 | } |
| 540 | |
| 541 | LLVM_DEBUG({ |
| 542 | dbgs() << "Hot/cold splitting attempting to outline these blocks:\n"; |
| 543 | for (BasicBlock *BB : SubRegion) |
| 544 | BB->dump(); |
| 545 | }); |
| 546 | |
| 547 | Function *Outlined = |
| 548 | extractColdRegion(SubRegion, DT, BFI, TTI, ORE, OutlinedFunctionID); |
| 549 | if (Outlined) { |
| 550 | ++OutlinedFunctionID; |
| 551 | OutlinedFunctions.insert(Outlined); |
| 552 | Changed = true; |
| 553 | } |
| 554 | } while (!Region.empty()); |
| 555 | } |
| 556 | |
| 557 | return Changed; |
| 558 | } |
| 559 | |
| 560 | bool HotColdSplitting::run(Module &M) { |
| 561 | bool Changed = false; |
| 562 | OutlinedFunctions.clear(); |
| 563 | for (auto &F : M) { |
| 564 | if (!shouldOutlineFrom(F)) { |
| 565 | LLVM_DEBUG(llvm::dbgs() << "Skipping " << F.getName() << "\n"); |
| 566 | continue; |
| 567 | } |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 568 | LLVM_DEBUG(llvm::dbgs() << "Outlining in " << F.getName() << "\n"); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 569 | DominatorTree DT(F); |
| 570 | PostDomTree PDT(F); |
| 571 | PDT.recalculate(F); |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 572 | BlockFrequencyInfo *BFI = GetBFI(F); |
Vedant Kumar | d2a895a | 2018-11-04 23:11:57 +0000 | [diff] [blame] | 573 | TargetTransformInfo &TTI = GetTTI(F); |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 574 | OptimizationRemarkEmitter &ORE = (*GetORE)(F); |
Vedant Kumar | 03aaa3e | 2018-12-07 20:23:52 +0000 | [diff] [blame] | 575 | Changed |= outlineColdRegions(F, *PSI, BFI, TTI, DT, PDT, ORE); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 576 | } |
Vedant Kumar | c299006 | 2018-10-24 22:15:41 +0000 | [diff] [blame] | 577 | return Changed; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | bool HotColdSplittingLegacyPass::runOnModule(Module &M) { |
| 581 | if (skipModule(M)) |
| 582 | return false; |
| 583 | ProfileSummaryInfo *PSI = |
Vedant Kumar | e7b789b | 2018-11-19 05:23:16 +0000 | [diff] [blame] | 584 | &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI(); |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 585 | auto GTTI = [this](Function &F) -> TargetTransformInfo & { |
| 586 | return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
| 587 | }; |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 588 | auto GBFI = [this](Function &F) { |
| 589 | return &this->getAnalysis<BlockFrequencyInfoWrapperPass>(F).getBFI(); |
| 590 | }; |
| 591 | std::unique_ptr<OptimizationRemarkEmitter> ORE; |
| 592 | std::function<OptimizationRemarkEmitter &(Function &)> GetORE = |
| 593 | [&ORE](Function &F) -> OptimizationRemarkEmitter & { |
| 594 | ORE.reset(new OptimizationRemarkEmitter(&F)); |
| 595 | return *ORE.get(); |
| 596 | }; |
| 597 | |
Sebastian Pop | a1f20fc | 2018-09-10 15:08:02 +0000 | [diff] [blame] | 598 | return HotColdSplitting(PSI, GBFI, GTTI, &GetORE).run(M); |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Aditya Kumar | 9e20ade | 2018-10-03 05:55:20 +0000 | [diff] [blame] | 601 | PreservedAnalyses |
| 602 | HotColdSplittingPass::run(Module &M, ModuleAnalysisManager &AM) { |
| 603 | auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager(); |
| 604 | |
| 605 | std::function<AssumptionCache &(Function &)> GetAssumptionCache = |
| 606 | [&FAM](Function &F) -> AssumptionCache & { |
| 607 | return FAM.getResult<AssumptionAnalysis>(F); |
| 608 | }; |
| 609 | |
| 610 | auto GBFI = [&FAM](Function &F) { |
| 611 | return &FAM.getResult<BlockFrequencyAnalysis>(F); |
| 612 | }; |
| 613 | |
| 614 | std::function<TargetTransformInfo &(Function &)> GTTI = |
| 615 | [&FAM](Function &F) -> TargetTransformInfo & { |
| 616 | return FAM.getResult<TargetIRAnalysis>(F); |
| 617 | }; |
| 618 | |
| 619 | std::unique_ptr<OptimizationRemarkEmitter> ORE; |
| 620 | std::function<OptimizationRemarkEmitter &(Function &)> GetORE = |
| 621 | [&ORE](Function &F) -> OptimizationRemarkEmitter & { |
| 622 | ORE.reset(new OptimizationRemarkEmitter(&F)); |
| 623 | return *ORE.get(); |
| 624 | }; |
| 625 | |
| 626 | ProfileSummaryInfo *PSI = &AM.getResult<ProfileSummaryAnalysis>(M); |
| 627 | |
| 628 | if (HotColdSplitting(PSI, GBFI, GTTI, &GetORE).run(M)) |
| 629 | return PreservedAnalyses::none(); |
| 630 | return PreservedAnalyses::all(); |
| 631 | } |
| 632 | |
Aditya Kumar | 801394a | 2018-09-07 15:03:49 +0000 | [diff] [blame] | 633 | char HotColdSplittingLegacyPass::ID = 0; |
| 634 | INITIALIZE_PASS_BEGIN(HotColdSplittingLegacyPass, "hotcoldsplit", |
| 635 | "Hot Cold Splitting", false, false) |
| 636 | INITIALIZE_PASS_DEPENDENCY(ProfileSummaryInfoWrapperPass) |
| 637 | INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass) |
| 638 | INITIALIZE_PASS_END(HotColdSplittingLegacyPass, "hotcoldsplit", |
| 639 | "Hot Cold Splitting", false, false) |
| 640 | |
| 641 | ModulePass *llvm::createHotColdSplittingPass() { |
| 642 | return new HotColdSplittingLegacyPass(); |
| 643 | } |