LLVM 24.0.0git
HotColdSplitting.cpp
Go to the documentation of this file.
1//===- HotColdSplitting.cpp -- Outline Cold Regions -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// The goal of hot/cold splitting is to improve the memory locality of code.
11/// The splitting pass does this by identifying cold blocks and moving them into
12/// separate functions.
13///
14/// When the splitting pass finds a cold block (referred to as "the sink"), it
15/// grows a maximal cold region around that block. The maximal region contains
16/// all blocks (post-)dominated by the sink [*]. In theory, these blocks are as
17/// cold as the sink. Once a region is found, it's split out of the original
18/// function provided it's profitable to do so.
19///
20/// [*] In practice, there is some added complexity because some blocks are not
21/// safe to extract.
22///
23/// TODO: Use the PM to get domtrees, and preserve BFI/BPI.
24/// TODO: Reorder outlined functions.
25///
26//===----------------------------------------------------------------------===//
27
29#include "IPOOptions.h"
32#include "llvm/ADT/Statistic.h"
39#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/CFG.h"
42#include "llvm/IR/Dominators.h"
44#include "llvm/IR/Function.h"
45#include "llvm/IR/Instruction.h"
47#include "llvm/IR/Module.h"
48#include "llvm/IR/PassManager.h"
50#include "llvm/IR/User.h"
51#include "llvm/IR/Value.h"
52#include "llvm/Support/Debug.h"
54#include "llvm/Transforms/IPO.h"
56#include <cassert>
57#include <limits>
58#include <string>
59
60#define DEBUG_TYPE "hotcoldsplit"
61
62STATISTIC(NumColdRegionsFound, "Number of cold regions found.");
63STATISTIC(NumColdRegionsOutlined, "Number of cold regions outlined.");
64
65using namespace llvm;
66
67namespace {
68// Same as blockEndsInUnreachable in CodeGen/BranchFolding.cpp. Do not modify
69// this function unless you modify the MBB version as well.
70//
71/// A no successor, non-return block probably ends in unreachable and is cold.
72/// Also consider a block that ends in an indirect branch to be a return block,
73/// since many targets use plain indirect branches to return.
74bool blockEndsInUnreachable(const BasicBlock &BB) {
75 if (!succ_empty(&BB))
76 return false;
77 if (BB.empty())
78 return true;
79 const Instruction *I = BB.getTerminator();
81}
82
83void analyzeProfMetadata(BasicBlock *BB,
84 BranchProbability ColdProbThresh,
85 SmallPtrSetImpl<BasicBlock *> &AnnotatedColdBlocks) {
86 // TODO: Handle branches with > 2 successors.
88 if (!CondBr)
89 return;
90
91 uint64_t TrueWt, FalseWt;
92 if (!extractBranchWeights(*CondBr, TrueWt, FalseWt))
93 return;
94
95 auto SumWt = TrueWt + FalseWt;
96 if (SumWt == 0)
97 return;
98
99 auto TrueProb = BranchProbability::getBranchProbability(TrueWt, SumWt);
100 auto FalseProb = BranchProbability::getBranchProbability(FalseWt, SumWt);
101
102 if (TrueProb <= ColdProbThresh)
103 AnnotatedColdBlocks.insert(CondBr->getSuccessor(0));
104
105 if (FalseProb <= ColdProbThresh)
106 AnnotatedColdBlocks.insert(CondBr->getSuccessor(1));
107}
108
109bool unlikelyExecuted(BasicBlock &BB) {
110 // Exception handling blocks are unlikely executed.
111 if (BB.isEHPad() || isa<ResumeInst>(BB.getTerminator()))
112 return true;
113
114 // The block is cold if it calls/invokes a cold function. However, do not
115 // mark sanitizer traps as cold.
116 for (Instruction &I : BB)
117 if (auto *CB = dyn_cast<CallBase>(&I))
118 if (CB->hasFnAttr(Attribute::Cold) &&
119 !CB->getMetadata(LLVMContext::MD_nosanitize))
120 return true;
121
122 // The block is cold if it has an unreachable terminator, unless it's
123 // preceded by a call to a (possibly warm) noreturn call (e.g. longjmp).
124 if (blockEndsInUnreachable(BB)) {
125 if (auto *CI =
126 dyn_cast_or_null<CallInst>(BB.getTerminator()->getPrevNode()))
127 if (CI->hasFnAttr(Attribute::NoReturn))
128 return false;
129 return true;
130 }
131
132 return false;
133}
134
135/// Check whether it's safe to outline \p BB.
136static bool mayExtractBlock(const BasicBlock &BB) {
137 // EH pads are unsafe to outline because doing so breaks EH type tables. It
138 // follows that invoke instructions cannot be extracted, because CodeExtractor
139 // requires unwind destinations to be within the extraction region.
140 //
141 // Resumes that are not reachable from a cleanup landing pad are considered to
142 // be unreachable. It’s not safe to split them out either.
143
144 if (BB.hasAddressTaken() || BB.isEHPad())
145 return false;
146 auto Term = BB.getTerminator();
147 if (isa<InvokeInst>(Term) || isa<ResumeInst>(Term))
148 return false;
149
150 // Do not outline basic blocks that have token type instructions. e.g.,
151 // exception:
152 // %0 = cleanuppad within none []
153 // call void @"?terminate@@YAXXZ"() [ "funclet"(token %0) ]
154 // br label %continue-exception
155 if (llvm::any_of(
156 BB, [](const Instruction &I) { return I.getType()->isTokenTy(); })) {
157 return false;
158 }
159
160 return true;
161}
162
163/// Mark \p F cold. Based on this assumption, also optimize it for minimum size.
164/// If \p UpdateEntryCount is true (set when this is a new split function and
165/// module has profile data), set entry count to 0 to ensure treated as cold.
166/// Return true if the function is changed.
167static bool markFunctionCold(Function &F, bool UpdateEntryCount = false) {
168 assert(!F.hasOptNone() && "Can't mark this cold");
169 bool Changed = false;
170 if (!F.hasFnAttribute(Attribute::Cold)) {
171 F.addFnAttr(Attribute::Cold);
172 Changed = true;
173 }
174 if (!F.hasMinSize()) {
175 F.addFnAttr(Attribute::MinSize);
176 Changed = true;
177 }
178 if (UpdateEntryCount) {
179 // Set the entry count to 0 to ensure it is placed in the unlikely text
180 // section when function sections are enabled.
181 F.setEntryCount(0);
182 Changed = true;
183 }
184
185 return Changed;
186}
187
188} // end anonymous namespace
189
190/// Check whether \p F is inherently cold.
191bool HotColdSplitting::isFunctionCold(const Function &F) const {
192 if (F.hasFnAttribute(Attribute::Cold))
193 return true;
194
195 if (F.getCallingConv() == CallingConv::Cold)
196 return true;
197
198 if (PSI->isFunctionEntryCold(&F))
199 return true;
200
201 return false;
202}
203
204bool HotColdSplitting::isBasicBlockCold(
205 BasicBlock *BB, BranchProbability ColdProbThresh,
206 SmallPtrSetImpl<BasicBlock *> &AnnotatedColdBlocks,
207 BlockFrequencyInfo *BFI) const {
208 if (BFI) {
209 if (PSI->isColdBlock(BB, BFI))
210 return true;
211 } else {
212 // Find cold blocks of successors of BB during a reverse postorder traversal.
213 analyzeProfMetadata(BB, ColdProbThresh, AnnotatedColdBlocks);
214
215 // A statically cold BB would be known before it is visited
216 // because the prof-data of incoming edges are 'analyzed' as part of RPOT.
217 if (AnnotatedColdBlocks.count(BB))
218 return true;
219 }
220
221 if (Opts.hot_cold_static_analysis && unlikelyExecuted(*BB))
222 return true;
223
224 return false;
225}
226
227// Returns false if the function should not be considered for hot-cold split
228// optimization.
229bool HotColdSplitting::shouldOutlineFrom(const Function &F) const {
230 if (F.hasFnAttribute(Attribute::AlwaysInline))
231 return false;
232
233 if (F.isNoInline())
234 return false;
235
236 // A function marked `noreturn` may contain unreachable terminators: these
237 // should not be considered cold, as the function may be a trampoline.
238 if (F.hasFnAttribute(Attribute::NoReturn))
239 return false;
240
241 if (F.hasFnAttribute(Attribute::SanitizeAddress) ||
242 F.hasFnAttribute(Attribute::SanitizeHWAddress) ||
243 F.hasFnAttribute(Attribute::SanitizeThread) ||
244 F.hasFnAttribute(Attribute::SanitizeConcurrency) ||
245 F.hasFnAttribute(Attribute::SanitizeMemory))
246 return false;
247
248 // Do not outline scoped EH personality functions.
249 if (F.hasPersonalityFn())
250 if (isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
251 return false;
252
253 return true;
254}
255
256/// Get the benefit score of outlining \p Region.
259 // Sum up the code size costs of non-terminator instructions. Tight coupling
260 // with \ref getOutliningPenalty is needed to model the costs of terminators.
261 InstructionCost Benefit = 0;
262 for (BasicBlock *BB : Region)
263 for (Instruction &I : *BB)
264 if (&I != BB->getTerminator())
265 Benefit +=
266 TTI.getInstructionCost(&I, TargetTransformInfo::TCK_CodeSize);
267
268 return Benefit;
269}
270
271/// Get the penalty score for outlining \p Region.
272static int getOutliningPenalty(const IPOOptions &Opts,
274 unsigned NumInputs, unsigned NumOutputs) {
275 int Penalty = Opts.hotcoldsplit_threshold;
276 LLVM_DEBUG(dbgs() << "Applying penalty for splitting: " << Penalty << "\n");
277
278 // If the splitting threshold is set at or below zero, skip the usual
279 // profitability check.
280 if (Opts.hotcoldsplit_threshold <= 0)
281 return Penalty;
282
283 // Find the number of distinct exit blocks for the region. Use a conservative
284 // check to determine whether control returns from the region.
285 bool NoBlocksReturn = true;
286 SmallPtrSet<BasicBlock *, 2> SuccsOutsideRegion;
287 for (BasicBlock *BB : Region) {
288 // If a block has no successors, only assume it does not return if it's
289 // unreachable.
290 if (succ_empty(BB)) {
291 NoBlocksReturn &= isa<UnreachableInst>(BB->getTerminator());
292 continue;
293 }
294
295 for (BasicBlock *SuccBB : successors(BB)) {
296 if (!is_contained(Region, SuccBB)) {
297 NoBlocksReturn = false;
298 SuccsOutsideRegion.insert(SuccBB);
299 }
300 }
301 }
302
303 // Count the number of phis in exit blocks with >= 2 incoming values from the
304 // outlining region. These phis are split (\ref severSplitPHINodesOfExits),
305 // and new outputs are created to supply the split phis. CodeExtractor can't
306 // report these new outputs until extraction begins, but it's important to
307 // factor the cost of the outputs into the cost calculation.
308 unsigned NumSplitExitPhis = 0;
309 for (BasicBlock *ExitBB : SuccsOutsideRegion) {
310 for (PHINode &PN : ExitBB->phis()) {
311 // Find all incoming values from the outlining region.
312 int NumIncomingVals = 0;
313 for (unsigned i = 0; i < PN.getNumIncomingValues(); ++i)
314 if (llvm::is_contained(Region, PN.getIncomingBlock(i))) {
315 ++NumIncomingVals;
316 if (NumIncomingVals > 1) {
317 ++NumSplitExitPhis;
318 break;
319 }
320 }
321 }
322 }
323
324 // Apply a penalty for calling the split function. Factor in the cost of
325 // materializing all of the parameters.
326 int NumOutputsAndSplitPhis = NumOutputs + NumSplitExitPhis;
327 int NumParams = NumInputs + NumOutputsAndSplitPhis;
328 if (NumParams > Opts.hotcoldsplit_max_params) {
329 LLVM_DEBUG(dbgs() << NumInputs << " inputs and " << NumOutputsAndSplitPhis
330 << " outputs exceeds parameter limit ("
331 << Opts.hotcoldsplit_max_params << ")\n");
332 return std::numeric_limits<int>::max();
333 }
334 const int CostForArgMaterialization = 2 * TargetTransformInfo::TCC_Basic;
335 LLVM_DEBUG(dbgs() << "Applying penalty for: " << NumParams << " params\n");
336 Penalty += CostForArgMaterialization * NumParams;
337
338 // Apply the typical code size cost for an output alloca and its associated
339 // reload in the caller. Also penalize the associated store in the callee.
340 LLVM_DEBUG(dbgs() << "Applying penalty for: " << NumOutputsAndSplitPhis
341 << " outputs/split phis\n");
342 const int CostForRegionOutput = 3 * TargetTransformInfo::TCC_Basic;
343 Penalty += CostForRegionOutput * NumOutputsAndSplitPhis;
344
345 // Apply a `noreturn` bonus.
346 if (NoBlocksReturn) {
347 LLVM_DEBUG(dbgs() << "Applying bonus for: " << Region.size()
348 << " non-returning terminators\n");
349 Penalty -= Region.size();
350 }
351
352 // Apply a penalty for having more than one successor outside of the region.
353 // This penalty accounts for the switch needed in the caller.
354 if (SuccsOutsideRegion.size() > 1) {
355 LLVM_DEBUG(dbgs() << "Applying penalty for: " << SuccsOutsideRegion.size()
356 << " non-region successors\n");
357 Penalty += (SuccsOutsideRegion.size() - 1) * TargetTransformInfo::TCC_Basic;
358 }
359
360 return Penalty;
361}
362
363// Determine if it is beneficial to split the \p Region.
364bool HotColdSplitting::isSplittingBeneficial(CodeExtractor &CE,
365 const BlockSequence &Region,
367 assert(!Region.empty());
368
369 // Perform a simple cost/benefit analysis to decide whether or not to permit
370 // splitting.
371 SetVector<Value *> Inputs, Outputs, Sinks;
372 CE.findInputsOutputs(Inputs, Outputs, Sinks);
373 InstructionCost OutliningBenefit = getOutliningBenefit(Region, TTI);
374 int OutliningPenalty =
375 getOutliningPenalty(Opts, Region, Inputs.size(), Outputs.size());
376 LLVM_DEBUG(dbgs() << "Split profitability: benefit = " << OutliningBenefit
377 << ", penalty = " << OutliningPenalty << "\n");
378 if (!OutliningBenefit.isValid() || OutliningBenefit <= OutliningPenalty)
379 return false;
380
381 return true;
382}
383
384// Split the single \p EntryPoint cold region. \p CE is the region code
385// extractor.
386Function *HotColdSplitting::extractColdRegion(
387 BasicBlock &EntryPoint, CodeExtractor &CE,
390 Function *OrigF = EntryPoint.getParent();
391 if (Function *OutF = CE.extractCodeRegion(CEAC)) {
392 User *U = *OutF->user_begin();
393 CallInst *CI = cast<CallInst>(U);
394 NumColdRegionsOutlined++;
395 if (TTI.useColdCCForColdCall(*OutF)) {
396 OutF->setCallingConv(CallingConv::Cold);
398 }
399 CI->setIsNoInline();
400
401 if (Opts.enable_cold_section)
402 OutF->setSection(Opts.hotcoldsplit_cold_section_name);
403 else {
404 if (OrigF->hasSection())
405 OutF->setSection(OrigF->getSection());
406 }
407
408 markFunctionCold(*OutF, true);
409
410 LLVM_DEBUG(llvm::dbgs() << "Outlined Region: " << *OutF);
411 ORE.emit([&]() {
412 return OptimizationRemark(DEBUG_TYPE, "HotColdSplit",
413 &*EntryPoint.begin())
414 << ore::NV("Original", OrigF) << " split cold code into "
415 << ore::NV("Split", OutF);
416 });
417 return OutF;
418 }
419
420 ORE.emit([&]() {
421 return OptimizationRemarkMissed(DEBUG_TYPE, "ExtractFailed",
422 &*EntryPoint.begin())
423 << "Failed to extract region at block "
424 << ore::NV("Block", &EntryPoint);
425 });
426 return nullptr;
427}
428
429/// A pair of (basic block, score).
430using BlockTy = std::pair<BasicBlock *, unsigned>;
431
432namespace {
433/// A maximal outlining region. This contains all blocks post-dominated by a
434/// sink block, the sink block itself, and all blocks dominated by the sink.
435/// If sink-predecessors and sink-successors cannot be extracted in one region,
436/// the static constructor returns a list of suitable extraction regions.
437class OutliningRegion {
438 /// A list of (block, score) pairs. A block's score is non-zero iff it's a
439 /// viable sub-region entry point. Blocks with higher scores are better entry
440 /// points (i.e. they are more distant ancestors of the sink block).
441 SmallVector<BlockTy, 0> Blocks = {};
442
443 /// The suggested entry point into the region. If the region has multiple
444 /// entry points, all blocks within the region may not be reachable from this
445 /// entry point.
446 BasicBlock *SuggestedEntryPoint = nullptr;
447
448 /// Whether the entire function is cold.
449 bool EntireFunctionCold = false;
450
451 /// If \p BB is a viable entry point, return \p Score. Return 0 otherwise.
452 static unsigned getEntryPointScore(BasicBlock &BB, unsigned Score) {
453 return mayExtractBlock(BB) ? Score : 0;
454 }
455
456 /// These scores should be lower than the score for predecessor blocks,
457 /// because regions starting at predecessor blocks are typically larger.
458 static constexpr unsigned ScoreForSuccBlock = 1;
459 static constexpr unsigned ScoreForSinkBlock = 1;
460
461 OutliningRegion(const OutliningRegion &) = delete;
462 OutliningRegion &operator=(const OutliningRegion &) = delete;
463
464public:
465 OutliningRegion() = default;
466 OutliningRegion(OutliningRegion &&) = default;
467 OutliningRegion &operator=(OutliningRegion &&) = default;
468
469 static std::vector<OutliningRegion> create(BasicBlock &SinkBB,
470 const DominatorTree &DT,
471 const PostDominatorTree &PDT) {
472 std::vector<OutliningRegion> Regions;
473 SmallPtrSet<BasicBlock *, 4> RegionBlocks;
474
475 Regions.emplace_back();
476 OutliningRegion *ColdRegion = &Regions.back();
477
478 auto addBlockToRegion = [&](BasicBlock *BB, unsigned Score) {
479 RegionBlocks.insert(BB);
480 ColdRegion->Blocks.emplace_back(BB, Score);
481 };
482
483 // The ancestor farthest-away from SinkBB, and also post-dominated by it.
484 unsigned SinkScore = getEntryPointScore(SinkBB, ScoreForSinkBlock);
485 ColdRegion->SuggestedEntryPoint = (SinkScore > 0) ? &SinkBB : nullptr;
486 unsigned BestScore = SinkScore;
487
488 // Visit SinkBB's ancestors using inverse DFS.
489 auto PredIt = ++idf_begin(&SinkBB);
490 auto PredEnd = idf_end(&SinkBB);
491 while (PredIt != PredEnd) {
492 BasicBlock &PredBB = **PredIt;
493 bool SinkPostDom = PDT.dominates(&SinkBB, &PredBB);
494
495 // If the predecessor is cold and has no predecessors, the entire
496 // function must be cold.
497 if (SinkPostDom && pred_empty(&PredBB)) {
498 ColdRegion->EntireFunctionCold = true;
499 return Regions;
500 }
501
502 // If SinkBB does not post-dominate a predecessor, do not mark the
503 // predecessor (or any of its predecessors) cold.
504 if (!SinkPostDom || !mayExtractBlock(PredBB)) {
505 PredIt.skipChildren();
506 continue;
507 }
508
509 // Keep track of the post-dominated ancestor farthest away from the sink.
510 // The path length is always >= 2, ensuring that predecessor blocks are
511 // considered as entry points before the sink block.
512 unsigned PredScore = getEntryPointScore(PredBB, PredIt.getPathLength());
513 if (PredScore > BestScore) {
514 ColdRegion->SuggestedEntryPoint = &PredBB;
515 BestScore = PredScore;
516 }
517
518 addBlockToRegion(&PredBB, PredScore);
519 ++PredIt;
520 }
521
522 // If the sink can be added to the cold region, do so. It's considered as
523 // an entry point before any sink-successor blocks.
524 //
525 // Otherwise, split cold sink-successor blocks using a separate region.
526 // This satisfies the requirement that all extraction blocks other than the
527 // first have predecessors within the extraction region.
528 if (mayExtractBlock(SinkBB)) {
529 addBlockToRegion(&SinkBB, SinkScore);
530 if (pred_empty(&SinkBB)) {
531 ColdRegion->EntireFunctionCold = true;
532 return Regions;
533 }
534 } else {
535 Regions.emplace_back();
536 ColdRegion = &Regions.back();
537 BestScore = 0;
538 }
539
540 // Find all successors of SinkBB dominated by SinkBB using DFS.
541 auto SuccIt = ++df_begin(&SinkBB);
542 auto SuccEnd = df_end(&SinkBB);
543 while (SuccIt != SuccEnd) {
544 BasicBlock &SuccBB = **SuccIt;
545 bool SinkDom = DT.dominates(&SinkBB, &SuccBB);
546
547 // Don't allow the backwards & forwards DFSes to mark the same block.
548 bool DuplicateBlock = RegionBlocks.count(&SuccBB);
549
550 // If SinkBB does not dominate a successor, do not mark the successor (or
551 // any of its successors) cold.
552 if (DuplicateBlock || !SinkDom || !mayExtractBlock(SuccBB)) {
553 SuccIt.skipChildren();
554 continue;
555 }
556
557 unsigned SuccScore = getEntryPointScore(SuccBB, ScoreForSuccBlock);
558 if (SuccScore > BestScore) {
559 ColdRegion->SuggestedEntryPoint = &SuccBB;
560 BestScore = SuccScore;
561 }
562
563 addBlockToRegion(&SuccBB, SuccScore);
564 ++SuccIt;
565 }
566
567 return Regions;
568 }
569
570 /// Whether this region has nothing to extract.
571 bool empty() const { return !SuggestedEntryPoint; }
572
573 /// The blocks in this region.
574 ArrayRef<std::pair<BasicBlock *, unsigned>> blocks() const { return Blocks; }
575
576 /// Whether the entire function containing this region is cold.
577 bool isEntireFunctionCold() const { return EntireFunctionCold; }
578
579 /// Remove a sub-region from this region and return it as a block sequence.
580 BlockSequence takeSingleEntrySubRegion(DominatorTree &DT) {
581 assert(!empty() && !isEntireFunctionCold() && "Nothing to extract");
582
583 // Remove blocks dominated by the suggested entry point from this region.
584 // During the removal, identify the next best entry point into the region.
585 // Ensure that the first extracted block is the suggested entry point.
586 BlockSequence SubRegion = {SuggestedEntryPoint};
587 BasicBlock *NextEntryPoint = nullptr;
588 unsigned NextScore = 0;
589 auto RegionEndIt = Blocks.end();
590 auto RegionStartIt = remove_if(Blocks, [&](const BlockTy &Block) {
591 BasicBlock *BB = Block.first;
592 unsigned Score = Block.second;
593 bool InSubRegion =
594 BB == SuggestedEntryPoint || DT.dominates(SuggestedEntryPoint, BB);
595 if (!InSubRegion && Score > NextScore) {
596 NextEntryPoint = BB;
597 NextScore = Score;
598 }
599 if (InSubRegion && BB != SuggestedEntryPoint)
600 SubRegion.push_back(BB);
601 return InSubRegion;
602 });
603 Blocks.erase(RegionStartIt, RegionEndIt);
604
605 // Update the suggested entry point.
606 SuggestedEntryPoint = NextEntryPoint;
607
608 return SubRegion;
609 }
610};
611} // namespace
612
613bool HotColdSplitting::outlineColdRegions(Function &F, bool HasProfileSummary) {
614 // The set of cold blocks outlined.
615 SmallPtrSet<BasicBlock *, 4> ColdBlocks;
616
617 // The set of cold blocks cannot be outlined.
618 SmallPtrSet<BasicBlock *, 4> CannotBeOutlinedColdBlocks;
619
620 // Set of cold blocks obtained with RPOT.
621 SmallPtrSet<BasicBlock *, 4> AnnotatedColdBlocks;
622
623 // The worklist of non-intersecting regions left to outline. The first member
624 // of the pair is the entry point into the region to be outlined.
626
627 // Set up an RPO traversal. Experimentally, this performs better (outlines
628 // more) than a PO traversal, because we prevent region overlap by keeping
629 // the first region to contain a block.
630 ReversePostOrderTraversal<Function *> RPOT(&F);
631
632 // Calculate domtrees lazily. This reduces compile-time significantly.
633 std::unique_ptr<DominatorTree> DT;
634 std::unique_ptr<PostDominatorTree> PDT;
635
636 // Calculate BFI lazily (it's only used to query ProfileSummaryInfo). This
637 // reduces compile-time significantly. TODO: When we *do* use BFI, we should
638 // be able to salvage its domtrees instead of recomputing them.
639 BlockFrequencyInfo *BFI = nullptr;
640 if (HasProfileSummary)
641 BFI = GetBFI(F);
642
643 TargetTransformInfo &TTI = GetTTI(F);
644 OptimizationRemarkEmitter &ORE = (*GetORE)(F);
645 AssumptionCache *AC = LookupAC(F);
646 auto ColdProbThresh = TTI.getPredictableBranchThreshold().getCompl();
647
648 if (Opts.hotcoldsplit_cold_probability_denom)
649 ColdProbThresh =
650 BranchProbability(1, *Opts.hotcoldsplit_cold_probability_denom);
651
652 unsigned OutlinedFunctionID = 1;
653 // Find all cold regions.
654 for (BasicBlock *BB : RPOT) {
655 // This block is already part of some outlining region.
656 if (ColdBlocks.count(BB))
657 continue;
658
659 // This block is already part of some region cannot be outlined.
660 if (CannotBeOutlinedColdBlocks.count(BB))
661 continue;
662
663 if (!isBasicBlockCold(BB, ColdProbThresh, AnnotatedColdBlocks, BFI))
664 continue;
665
666 LLVM_DEBUG({
667 dbgs() << "Found a cold block:\n";
668 BB->dump();
669 });
670
671 if (!DT)
672 DT = std::make_unique<DominatorTree>(F);
673 if (!PDT)
674 PDT = std::make_unique<PostDominatorTree>(F);
675
676 auto Regions = OutliningRegion::create(*BB, *DT, *PDT);
677 for (OutliningRegion &Region : Regions) {
678 if (Region.empty())
679 continue;
680
681 if (Region.isEntireFunctionCold()) {
682 LLVM_DEBUG(dbgs() << "Entire function is cold\n");
683 return false;
684 }
685
686 do {
687 BlockSequence SubRegion = Region.takeSingleEntrySubRegion(*DT);
688 LLVM_DEBUG({
689 dbgs() << "Hot/cold splitting attempting to outline these blocks:\n";
690 for (BasicBlock *BB : SubRegion)
691 BB->dump();
692 });
693
694 // TODO: Pass BFI and BPI to update profile information.
695 CodeExtractor CE(
696 SubRegion, &*DT, /* AggregateArgs */ false, /* BFI */ nullptr,
697 /* BPI */ nullptr, AC, /* AllowVarArgs */ false,
698 /* AllowAlloca */ false, /* AllocaBlock */ nullptr,
699 /* DeallocationBlocks */ {},
700 /* Suffix */ "cold." + std::to_string(OutlinedFunctionID),
701 /* ArgsInZeroAddressSpace */ false,
702 /* VoidReturnWithSingleOutput */ false);
703
704 if (CE.isEligible() && isSplittingBeneficial(CE, SubRegion, TTI) &&
705 // If this outlining region intersects with another, drop the new
706 // region.
707 //
708 // TODO: It's theoretically possible to outline more by only keeping
709 // the largest region which contains a block, but the extra
710 // bookkeeping to do this is tricky/expensive.
711 none_of(SubRegion, [&](BasicBlock *Block) {
712 return ColdBlocks.contains(Block);
713 })) {
714 ColdBlocks.insert_range(SubRegion);
715
716 LLVM_DEBUG({
717 for (auto *Block : SubRegion)
718 dbgs() << " contains cold block:" << Block->getName() << "\n";
719 });
720
721 OutliningWorklist.emplace_back(
722 std::make_pair(SubRegion[0], std::move(CE)));
723 ++OutlinedFunctionID;
724 } else {
725 // The cold block region cannot be outlined.
726 for (auto *Block : SubRegion)
727 if ((DT->dominates(BB, Block) && PDT->dominates(Block, BB)) ||
728 (PDT->dominates(BB, Block) && DT->dominates(Block, BB)))
729 // Will skip this cold block in the loop to save the compile time
730 CannotBeOutlinedColdBlocks.insert(Block);
731 }
732 } while (!Region.empty());
733
734 ++NumColdRegionsFound;
735 }
736 }
737
738 if (OutliningWorklist.empty())
739 return false;
740
741 // Outline single-entry cold regions, splitting up larger regions as needed.
742 // Cache and recycle the CodeExtractor analysis to avoid O(n^2) compile-time.
743 CodeExtractorAnalysisCache CEAC(F);
744 for (auto &BCE : OutliningWorklist) {
745 Function *Outlined =
746 extractColdRegion(*BCE.first, BCE.second, CEAC, BFI, TTI, ORE);
747 assert(Outlined && "Should be outlined");
748 (void)Outlined;
749 }
750
751 return true;
752}
753
755 bool Changed = false;
756 bool HasProfileSummary = (M.getProfileSummary(/* IsCS */ false) != nullptr);
757 for (Function &F : M) {
758 // Do not touch declarations.
759 if (F.isDeclaration())
760 continue;
761
762 // Do not modify `optnone` functions.
763 if (F.hasOptNone())
764 continue;
765
766 if (isFunctionCold(F))
767 continue;
768
769 if (!shouldOutlineFrom(F)) {
770 LLVM_DEBUG(llvm::dbgs() << "Skipping " << F.getName() << "\n");
771 continue;
772 }
773
774 LLVM_DEBUG(llvm::dbgs() << "Outlining in " << F.getName() << "\n");
775 Changed |= outlineColdRegions(F, HasProfileSummary);
776 }
777 return Changed;
778}
779
782 auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
783
784 auto LookupAC = [&FAM](Function &F) -> AssumptionCache * {
785 return FAM.getCachedResult<AssumptionAnalysis>(F);
786 };
787
788 auto GBFI = [&FAM](Function &F) {
789 return &FAM.getResult<BlockFrequencyAnalysis>(F);
790 };
791
792 std::function<TargetTransformInfo &(Function &)> GTTI =
794 return FAM.getResult<TargetIRAnalysis>(F);
795 };
796
797 std::unique_ptr<OptimizationRemarkEmitter> ORE;
798 std::function<OptimizationRemarkEmitter &(Function &)> GetORE =
799 [&ORE](Function &F) -> OptimizationRemarkEmitter & {
800 ORE.reset(new OptimizationRemarkEmitter(&F));
801 return *ORE;
802 };
803
805
806 if (HotColdSplitting(getIPOOptions(M), PSI, GBFI, GTTI, &GetORE, LookupAC)
807 .run(M))
809 return PreservedAnalyses::all();
810}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
bbsections Prepares for basic block by splitting functions into clusters of basic blocks
static bool blockEndsInUnreachable(const MachineBasicBlock *MBB)
A no successor, non-return block probably ends in unreachable and is cold.
#define DEBUG_TYPE
std::pair< BasicBlock *, unsigned > BlockTy
A pair of (basic block, score).
static InstructionCost getOutliningBenefit(ArrayRef< BasicBlock * > Region, TargetTransformInfo &TTI)
Get the benefit score of outlining Region.
static int getOutliningPenalty(const IPOOptions &Opts, ArrayRef< BasicBlock * > Region, unsigned NumInputs, unsigned NumOutputs)
Get the penalty score for outlining Region.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains the declarations for profiling metadata utility functions.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:440
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
bool empty() const
Definition BasicBlock.h:462
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
Definition BasicBlock.h:666
bool isEHPad() const
Return true if this basic block is an exception handling block.
Definition BasicBlock.h:683
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
BranchProbability getCompl() const
void setCallingConv(CallingConv::ID CC)
void setIsNoInline()
A cache for the CodeExtractor analysis.
Utility class for extracting code into a new function.
Conditional Branch instruction.
BasicBlock * getSuccessor(unsigned i) const
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
StringRef getSection() const
Get the custom section of this global if it has one.
bool hasSection() const
Check if this global has a custom object file section.
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI bool run(Module &M)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
An analysis pass based on the new PM to deliver ProfileSummaryInfo.
Analysis providing profile information.
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
size_type size() const
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
@ TCK_CodeSize
Instruction code size.
LLVM_ABI BranchProbability getPredictableBranchThreshold() const
If a branch or a select condition is skewed in one direction by more than this factor,...
@ TCC_Basic
The cost of a typical 'add' instruction.
LLVM_ABI bool useColdCCForColdCall(Function &F) const
Return true if the input function which is cold at all call sites, should use coldcc calling conventi...
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition CallingConv.h:47
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
@ User
could "use" a pointer
DiagnosticInfoOptimizationBase::Argument NV
This is an optimization pass for GlobalISel generic memory operations.
bool succ_empty(const Instruction *I)
Definition CFG.h:141
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
df_iterator< T > df_begin(const T &G)
InnerAnalysisManagerProxy< FunctionAnalysisManager, Module > FunctionAnalysisManagerModuleProxy
Provide the FunctionAnalysisManager to Module proxy.
SmallVector< BasicBlock *, 0 > BlockSequence
A sequence of basic blocks.
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
const IPOOptions & getIPOOptions(const LLVMContext &C)
Definition IPOOptions.h:36
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1769
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
idf_iterator< T > idf_end(const T &G)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
auto remove_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1800
TargetTransformInfo TTI
idf_iterator< T > idf_begin(const T &G)
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
df_iterator< T > df_end(const T &G)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39