LLVM 23.0.0git
RegAllocGreedy.cpp
Go to the documentation of this file.
1//===- RegAllocGreedy.cpp - greedy register allocator ---------------------===//
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// This file defines the RAGreedy function pass for register allocation in
10// optimized builds.
11//
12//===----------------------------------------------------------------------===//
13
14#include "RegAllocGreedy.h"
15#include "AllocationOrder.h"
16#include "InterferenceCache.h"
17#include "RegAllocBase.h"
18#include "SplitKit.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/BitVector.h"
21#include "llvm/ADT/IndexedMap.h"
22#include "llvm/ADT/SmallSet.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/ADT/StringRef.h"
60#include "llvm/IR/Analysis.h"
62#include "llvm/IR/Function.h"
63#include "llvm/IR/LLVMContext.h"
65#include "llvm/Pass.h"
69#include "llvm/Support/Debug.h"
71#include "llvm/Support/Timer.h"
73#include <algorithm>
74#include <cassert>
75#include <cstdint>
76#include <utility>
77
78using namespace llvm;
79
80#define DEBUG_TYPE "regalloc"
81
82STATISTIC(NumGlobalSplits, "Number of split global live ranges");
83STATISTIC(NumLocalSplits, "Number of split local live ranges");
84STATISTIC(NumEvicted, "Number of interferences evicted");
85
87 "split-spill-mode", cl::Hidden,
88 cl::desc("Spill mode for splitting live ranges"),
89 cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"),
90 clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"),
91 clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed")),
93
96 cl::desc("Last chance recoloring max depth"),
97 cl::init(5));
98
100 "lcr-max-interf", cl::Hidden,
101 cl::desc("Last chance recoloring maximum number of considered"
102 " interference at a time"),
103 cl::init(8));
104
106 "exhaustive-register-search", cl::NotHidden,
107 cl::desc("Exhaustive Search for registers bypassing the depth "
108 "and interference cutoffs of last chance recoloring"),
109 cl::Hidden);
110
111// This option should be deprecated!
112// FIXME: Find a good default for this flag and remove the flag.
114CSRFirstTimeCost("regalloc-csr-first-time-cost",
115 cl::desc("Cost for first time use of callee-saved register."),
116 cl::init(0), cl::Hidden);
117
119 "regalloc-csr-cost-scale",
120 cl::desc("Scale for the callee-saved register cost, in percentage."),
121 cl::init(80), cl::Hidden);
122
124 "grow-region-complexity-budget",
125 cl::desc("growRegion() does not scale with the number of BB edges, so "
126 "limit its budget and bail out once we reach the limit."),
127 cl::init(10000), cl::Hidden);
128
130 "greedy-regclass-priority-trumps-globalness",
131 cl::desc("Change the greedy register allocator's live range priority "
132 "calculation to make the AllocationPriority of the register class "
133 "more important then whether the range is global"),
134 cl::Hidden);
135
137 "greedy-reverse-local-assignment",
138 cl::desc("Reverse allocation order of local live ranges, such that "
139 "shorter local live ranges will tend to be allocated first"),
140 cl::Hidden);
141
143 "split-threshold-for-reg-with-hint",
144 cl::desc("The threshold for splitting a virtual register with a hint, in "
145 "percentage"),
146 cl::init(75), cl::Hidden);
147
148static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator",
150
151namespace {
152class RAGreedyLegacy : public MachineFunctionPass {
154
155public:
156 RAGreedyLegacy(const RegAllocFilterFunc F = nullptr);
157
158 static char ID;
159 /// Return the pass name.
160 StringRef getPassName() const override { return "Greedy Register Allocator"; }
161
162 /// RAGreedy analysis usage.
163 void getAnalysisUsage(AnalysisUsage &AU) const override;
164 /// Perform register allocation.
165 bool runOnMachineFunction(MachineFunction &mf) override;
166
167 MachineFunctionProperties getRequiredProperties() const override {
168 return MachineFunctionProperties().setNoPHIs();
169 }
170
171 MachineFunctionProperties getClearedProperties() const override {
172 return MachineFunctionProperties().setIsSSA();
173 }
174};
175
176} // end anonymous namespace
177
178RAGreedyLegacy::RAGreedyLegacy(const RegAllocFilterFunc F)
179 : MachineFunctionPass(ID), F(std::move(F)) {}
180
204
206 : RegAllocBase(F) {
207 VRM = Analyses.VRM;
208 LIS = Analyses.LIS;
209 Matrix = Analyses.LRM;
210 Indexes = Analyses.Indexes;
211 MBFI = Analyses.MBFI;
212 DomTree = Analyses.DomTree;
213 Loops = Analyses.Loops;
214 ORE = Analyses.ORE;
215 Bundles = Analyses.Bundles;
216 SpillPlacer = Analyses.SpillPlacer;
217 DebugVars = Analyses.DebugVars;
218 LSS = Analyses.LSS;
219 EvictProvider = Analyses.EvictProvider;
220 PriorityProvider = Analyses.PriorityProvider;
221}
222
224 raw_ostream &OS,
225 function_ref<StringRef(StringRef)> MapClassName2PassName) const {
226 StringRef FilterName = Opts.FilterName.empty() ? "all" : Opts.FilterName;
227 OS << "greedy<" << FilterName << '>';
228}
229
248
251 MFPropsModifier _(*this, MF);
252
253 RAGreedy::RequiredAnalyses Analyses(MF, MFAM);
254 RAGreedy Impl(Analyses, Opts.Filter);
255
256 bool Changed = Impl.run(MF);
257 if (!Changed)
258 return PreservedAnalyses::all();
260 PA.preserveSet<CFGAnalyses>();
261 PA.preserve<MachineBlockFrequencyAnalysis>();
262 PA.preserve<LiveIntervalsAnalysis>();
263 PA.preserve<SlotIndexesAnalysis>();
264 PA.preserve<LiveDebugVariablesAnalysis>();
265 PA.preserve<LiveStacksAnalysis>();
266 PA.preserve<VirtRegMapAnalysis>();
267 PA.preserve<LiveRegMatrixAnalysis>();
268 return PA;
269}
270
272 VRM = &P.getAnalysis<VirtRegMapWrapperLegacy>().getVRM();
273 LIS = &P.getAnalysis<LiveIntervalsWrapperPass>().getLIS();
274 LSS = &P.getAnalysis<LiveStacksWrapperLegacy>().getLS();
275 LRM = &P.getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM();
276 Indexes = &P.getAnalysis<SlotIndexesWrapperPass>().getSI();
277 MBFI = &P.getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI();
279 ORE = &P.getAnalysis<MachineOptimizationRemarkEmitterPass>().getORE();
280 Loops = &P.getAnalysis<MachineLoopInfoWrapperPass>().getLI();
281 Bundles = &P.getAnalysis<EdgeBundlesWrapperLegacy>().getEdgeBundles();
282 SpillPlacer = &P.getAnalysis<SpillPlacementWrapperLegacy>().getResult();
283 DebugVars = &P.getAnalysis<LiveDebugVariablesWrapperLegacy>().getLDV();
285 &P.getAnalysis<RegAllocEvictionAdvisorAnalysisLegacy>().getProvider();
287 &P.getAnalysis<RegAllocPriorityAdvisorAnalysisLegacy>().getProvider();
288}
289
290bool RAGreedyLegacy::runOnMachineFunction(MachineFunction &MF) {
291 RAGreedy::RequiredAnalyses Analyses(*this);
292 RAGreedy Impl(Analyses, F);
293 return Impl.run(MF);
294}
295
296char RAGreedyLegacy::ID = 0;
297char &llvm::RAGreedyLegacyID = RAGreedyLegacy::ID;
298
299INITIALIZE_PASS_BEGIN(RAGreedyLegacy, "greedy", "Greedy Register Allocator",
300 false, false)
304INITIALIZE_PASS_DEPENDENCY(RegisterCoalescerLegacy)
305INITIALIZE_PASS_DEPENDENCY(MachineSchedulerLegacy)
316INITIALIZE_PASS_END(RAGreedyLegacy, "greedy", "Greedy Register Allocator",
318
319#ifndef NDEBUG
320const char *const RAGreedy::StageName[] = {
321 "RS_New",
322 "RS_Assign",
323 "RS_Split",
324 "RS_Split2",
325 "RS_Spill",
326 "RS_Done"
327};
328#endif
329
330// Hysteresis to use when comparing floats.
331// This helps stabilize decisions based on float comparisons.
332const float Hysteresis = (2007 / 2048.0f); // 0.97998046875
333
335 return new RAGreedyLegacy();
336}
337
339 return new RAGreedyLegacy(Ftor);
340}
341
342void RAGreedyLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
343 AU.setPreservesCFG();
368}
369
370//===----------------------------------------------------------------------===//
371// LiveRangeEdit delegate methods
372//===----------------------------------------------------------------------===//
373
374bool RAGreedy::LRE_CanEraseVirtReg(Register VirtReg) {
375 LiveInterval &LI = LIS->getInterval(VirtReg);
376 if (VRM->hasPhys(VirtReg)) {
377 Matrix->unassign(LI);
379 return true;
380 }
381 // Unassigned virtreg is probably in the priority queue.
382 // RegAllocBase will erase it after dequeueing.
383 // Nonetheless, clear the live-range so that the debug
384 // dump will show the right state for that VirtReg.
385 LI.clear();
386 return false;
387}
388
389void RAGreedy::LRE_WillShrinkVirtReg(Register VirtReg) {
390 if (!VRM->hasPhys(VirtReg))
391 return;
392
393 // Register is assigned, put it back on the queue for reassignment.
394 LiveInterval &LI = LIS->getInterval(VirtReg);
395 Matrix->unassign(LI);
397}
398
399void RAGreedy::LRE_DidCloneVirtReg(Register New, Register Old) {
400 ExtraInfo->LRE_DidCloneVirtReg(New, Old);
401}
402
404 // Cloning a register we haven't even heard about yet? Just ignore it.
405 if (!Info.inBounds(Old))
406 return;
407
408 // LRE may clone a virtual register because dead code elimination causes it to
409 // be split into connected components. The new components are much smaller
410 // than the original, so they should get a new chance at being assigned.
411 // same stage as the parent.
412 Info[Old].Stage = RS_Assign;
413 Info.grow(New.id());
414 Info[New] = Info[Old];
415}
416
418 SpillerInstance.reset();
419 GlobalCand.clear();
420}
421
422void RAGreedy::enqueueImpl(const LiveInterval *LI) { enqueue(Queue, LI); }
423
424void RAGreedy::enqueue(PQueue &CurQueue, const LiveInterval *LI) {
425 // Prioritize live ranges by size, assigning larger ranges first.
426 // The queue holds (size, reg) pairs.
427 const Register Reg = LI->reg();
428 assert(Reg.isVirtual() && "Can only enqueue virtual registers");
429
430 auto Stage = ExtraInfo->getOrInitStage(Reg);
431 if (Stage == RS_New) {
432 Stage = RS_Assign;
433 ExtraInfo->setStage(Reg, Stage);
434 }
435
436 unsigned Ret = PriorityAdvisor->getPriority(*LI);
437
438 // The virtual register number is a tie breaker for same-sized ranges.
439 // Give lower vreg numbers higher priority to assign them first.
440 CurQueue.push(std::make_pair(Ret, ~Reg.id()));
441}
442
443unsigned DefaultPriorityAdvisor::getPriority(const LiveInterval &LI) const {
444 const unsigned Size = LI.getSize();
445 const Register Reg = LI.reg();
446 unsigned Prio;
447 LiveRangeStage Stage = RA.getExtraInfo().getStage(LI);
448
449 if (Stage == RS_Split) {
450 // Unsplit ranges that couldn't be allocated immediately are deferred until
451 // everything else has been allocated.
452 Prio = Size;
453 } else {
454 // Giant live ranges fall back to the global assignment heuristic, which
455 // prevents excessive spilling in pathological cases.
456 const TargetRegisterClass &RC = *MRI->getRegClass(Reg);
457 bool ForceGlobal = RC.GlobalPriority ||
458 (!ReverseLocalAssignment &&
460 (2 * RegClassInfo.getNumAllocatableRegs(&RC)));
461 unsigned GlobalBit = 0;
462
463 if (Stage == RS_Assign && !ForceGlobal && !LI.empty() &&
464 LIS->intervalIsInOneMBB(LI)) {
465 // Allocate original local ranges in linear instruction order. Since they
466 // are singly defined, this produces optimal coloring in the absence of
467 // global interference and other constraints.
468 if (!ReverseLocalAssignment)
469 Prio = LI.beginIndex().getApproxInstrDistance(Indexes->getLastIndex());
470 else {
471 // Allocating bottom up may allow many short LRGs to be assigned first
472 // to one of the cheap registers. This could be much faster for very
473 // large blocks on targets with many physical registers.
474 Prio = Indexes->getZeroIndex().getApproxInstrDistance(LI.endIndex());
475 }
476 } else {
477 // Allocate global and split ranges in long->short order. Long ranges that
478 // don't fit should be spilled (or split) ASAP so they don't create
479 // interference. Mark a bit to prioritize global above local ranges.
480 Prio = Size;
481 GlobalBit = 1;
482 }
483
484 // Priority bit layout:
485 // 31 RS_Assign priority
486 // 30 Preference priority
487 // if (RegClassPriorityTrumpsGlobalness)
488 // 29-25 AllocPriority
489 // 24 GlobalBit
490 // else
491 // 29 Global bit
492 // 28-24 AllocPriority
493 // 0-23 Size/Instr distance
494
495 // Clamp the size to fit with the priority masking scheme
496 Prio = std::min(Prio, (unsigned)maxUIntN(24));
497 assert(isUInt<5>(RC.AllocationPriority) && "allocation priority overflow");
498
499 if (RegClassPriorityTrumpsGlobalness)
500 Prio |= RC.AllocationPriority << 25 | GlobalBit << 24;
501 else
502 Prio |= GlobalBit << 29 | RC.AllocationPriority << 24;
503
504 // Mark a higher bit to prioritize global and local above RS_Split.
505 Prio |= (1u << 31);
506
507 // Boost ranges that have a physical register hint.
508 if (VRM->hasKnownPreference(Reg))
509 Prio |= (1u << 30);
510 }
511
512 return Prio;
513}
514
515unsigned DummyPriorityAdvisor::getPriority(const LiveInterval &LI) const {
516 // Prioritize by virtual register number, lowest first.
517 Register Reg = LI.reg();
518 return ~Reg.virtRegIndex();
519}
520
521const LiveInterval *RAGreedy::dequeue() { return dequeue(Queue); }
522
523const LiveInterval *RAGreedy::dequeue(PQueue &CurQueue) {
524 if (CurQueue.empty())
525 return nullptr;
526 LiveInterval *LI = &LIS->getInterval(~CurQueue.top().second);
527 CurQueue.pop();
528 return LI;
529}
530
531//===----------------------------------------------------------------------===//
532// Direct Assignment
533//===----------------------------------------------------------------------===//
534
535/// tryAssign - Try to assign VirtReg to an available register.
536MCRegister RAGreedy::tryAssign(const LiveInterval &VirtReg,
537 AllocationOrder &Order,
539 const SmallVirtRegSet &FixedRegisters) {
540 MCRegister PhysReg;
541 for (auto I = Order.begin(), E = Order.end(); I != E && !PhysReg; ++I) {
542 assert(*I);
543 if (!Matrix->checkInterference(VirtReg, *I)) {
544 if (I.isHint())
545 return *I;
546 else
547 PhysReg = *I;
548 }
549 }
550 if (!PhysReg.isValid())
551 return PhysReg;
552
553 // PhysReg is available, but there may be a better choice.
554
555 // If we missed a simple hint, try to cheaply evict interference from the
556 // preferred register.
557 if (Register Hint = MRI->getSimpleHint(VirtReg.reg()))
558 if (Order.isHint(Hint)) {
559 MCRegister PhysHint = Hint.asMCReg();
560 LLVM_DEBUG(dbgs() << "missed hint " << printReg(PhysHint, TRI) << '\n');
561
562 if (EvictAdvisor->canEvictHintInterference(VirtReg, PhysHint,
563 FixedRegisters)) {
564 evictInterference(VirtReg, PhysHint, NewVRegs);
565 return PhysHint;
566 }
567
568 // We can also split the virtual register in cold blocks.
569 if (trySplitAroundHintReg(PhysHint, VirtReg, NewVRegs, Order))
570 return MCRegister();
571
572 // Record the missed hint, we may be able to recover
573 // at the end if the surrounding allocation changed.
574 SetOfBrokenHints.insert(&VirtReg);
575 }
576
577 // Try to evict interference from a cheaper alternative.
578 uint8_t Cost = RegCosts[PhysReg.id()];
579
580 // Most registers have 0 additional cost.
581 if (!Cost)
582 return PhysReg;
583
584 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << " is available at cost "
585 << (unsigned)Cost << '\n');
586 MCRegister CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost, FixedRegisters);
587 return CheapReg ? CheapReg : PhysReg;
588}
589
590//===----------------------------------------------------------------------===//
591// Interference eviction
592//===----------------------------------------------------------------------===//
593
595 MCRegister FromReg) const {
596 auto HasRegUnitInterference = [&](MCRegUnit Unit) {
597 // Instantiate a "subquery", not to be confused with the Queries array.
599 VirtReg, Matrix->getLiveUnions()[static_cast<unsigned>(Unit)]);
600 return SubQ.checkInterference();
601 };
602
603 for (MCRegister Reg :
605 if (Reg == FromReg)
606 continue;
607 // If no units have interference, reassignment is possible.
608 if (none_of(TRI->regunits(Reg), HasRegUnitInterference)) {
609 LLVM_DEBUG(dbgs() << "can reassign: " << VirtReg << " from "
610 << printReg(FromReg, TRI) << " to "
611 << printReg(Reg, TRI) << '\n');
612 return true;
613 }
614 }
615 return false;
616}
617
618/// evictInterference - Evict any interferring registers that prevent VirtReg
619/// from being assigned to Physreg. This assumes that canEvictInterference
620/// returned true.
621void RAGreedy::evictInterference(const LiveInterval &VirtReg,
622 MCRegister PhysReg,
623 SmallVectorImpl<Register> &NewVRegs) {
624 // Make sure that VirtReg has a cascade number, and assign that cascade
625 // number to every evicted register. These live ranges than then only be
626 // evicted by a newer cascade, preventing infinite loops.
627 unsigned Cascade = ExtraInfo->getOrAssignNewCascade(VirtReg.reg());
628
629 LLVM_DEBUG(dbgs() << "evicting " << printReg(PhysReg, TRI)
630 << " interference: Cascade " << Cascade << '\n');
631
632 // Collect all interfering virtregs first.
634 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
635 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, Unit);
636 // We usually have the interfering VRegs cached so collectInterferingVRegs()
637 // should be fast, we may need to recalculate if when different physregs
638 // overlap the same register unit so we had different SubRanges queried
639 // against it.
641 Intfs.append(IVR.begin(), IVR.end());
642 }
643
644 // Evict them second. This will invalidate the queries.
645 for (const LiveInterval *Intf : Intfs) {
646 // The same VirtReg may be present in multiple RegUnits. Skip duplicates.
647 if (!VRM->hasPhys(Intf->reg()))
648 continue;
649
650 Matrix->unassign(*Intf);
651 assert((ExtraInfo->getCascade(Intf->reg()) < Cascade ||
652 VirtReg.isSpillable() < Intf->isSpillable()) &&
653 "Cannot decrease cascade number, illegal eviction");
654 ExtraInfo->setCascade(Intf->reg(), Cascade);
655 ++NumEvicted;
656 NewVRegs.push_back(Intf->reg());
657 }
658}
659
660/// Returns true if the given \p PhysReg is a callee saved register and has not
661/// been used for allocation yet.
663 MCRegister CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg);
664 if (!CSR)
665 return false;
666
667 return !Matrix->isPhysRegUsed(PhysReg);
668}
669
670std::optional<unsigned>
672 const AllocationOrder &Order,
673 unsigned CostPerUseLimit) const {
674 unsigned OrderLimit = Order.getOrder().size();
675
676 if (CostPerUseLimit < uint8_t(~0u)) {
677 // Check of any registers in RC are below CostPerUseLimit.
678 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg());
679 uint8_t MinCost = RegClassInfo.getMinCost(RC);
680 if (MinCost >= CostPerUseLimit) {
681 LLVM_DEBUG(dbgs() << TRI->getRegClassName(RC) << " minimum cost = "
682 << MinCost << ", no cheaper registers to be found.\n");
683 return std::nullopt;
684 }
685
686 // It is normal for register classes to have a long tail of registers with
687 // the same cost. We don't need to look at them if they're too expensive.
688 if (RegCosts[Order.getOrder().back()] >= CostPerUseLimit) {
689 OrderLimit = RegClassInfo.getLastCostChange(RC);
690 LLVM_DEBUG(dbgs() << "Only trying the first " << OrderLimit
691 << " regs.\n");
692 }
693 }
694 return OrderLimit;
695}
696
698 MCRegister PhysReg) const {
699 if (RegCosts[PhysReg.id()] >= CostPerUseLimit)
700 return false;
701 // The first use of a callee-saved register in a function has cost 1.
702 // Don't start using a CSR when the CostPerUseLimit is low.
703 if (CostPerUseLimit == 1 && isUnusedCalleeSavedReg(PhysReg)) {
705 dbgs() << printReg(PhysReg, TRI) << " would clobber CSR "
706 << printReg(RegClassInfo.getLastCalleeSavedAlias(PhysReg), TRI)
707 << '\n');
708 return false;
709 }
710 return true;
711}
712
713/// tryEvict - Try to evict all interferences for a physreg.
714/// @param VirtReg Currently unassigned virtual register.
715/// @param Order Physregs to try.
716/// @return Physreg to assign VirtReg, or 0.
717MCRegister RAGreedy::tryEvict(const LiveInterval &VirtReg,
718 AllocationOrder &Order,
720 uint8_t CostPerUseLimit,
721 const SmallVirtRegSet &FixedRegisters) {
724
725 MCRegister BestPhys = EvictAdvisor->tryFindEvictionCandidate(
726 VirtReg, Order, CostPerUseLimit, FixedRegisters);
727 if (BestPhys.isValid())
728 evictInterference(VirtReg, BestPhys, NewVRegs);
729 return BestPhys;
730}
731
732//===----------------------------------------------------------------------===//
733// Region Splitting
734//===----------------------------------------------------------------------===//
735
736/// addSplitConstraints - Fill out the SplitConstraints vector based on the
737/// interference pattern in Physreg and its aliases. Add the constraints to
738/// SpillPlacement and return the static cost of this split in Cost, assuming
739/// that all preferences in SplitConstraints are met.
740/// Return false if there are no bundles with positive bias.
741bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf,
742 BlockFrequency &Cost) {
743 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
744
745 // Reset interference dependent info.
746 SplitConstraints.resize(UseBlocks.size());
747 BlockFrequency StaticCost = BlockFrequency(0);
748 for (unsigned I = 0; I != UseBlocks.size(); ++I) {
749 const SplitAnalysis::BlockInfo &BI = UseBlocks[I];
750 SpillPlacement::BlockConstraint &BC = SplitConstraints[I];
751
752 BC.Number = BI.MBB->getNumber();
753 Intf.moveToBlock(BC.Number);
755 BC.Exit = (BI.LiveOut &&
759 BC.ChangesValue = BI.FirstDef.isValid();
760
761 if (!Intf.hasInterference())
762 continue;
763
764 // Number of spill code instructions to insert.
765 unsigned Ins = 0;
766
767 // Interference for the live-in value.
768 if (BI.LiveIn) {
769 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) {
771 ++Ins;
772 } else if (Intf.first() < BI.FirstInstr) {
774 ++Ins;
775 } else if (Intf.first() < BI.LastInstr) {
776 ++Ins;
777 }
778
779 // Abort if the spill cannot be inserted at the MBB' start
780 if (((BC.Entry == SpillPlacement::MustSpill) ||
783 SA->getFirstSplitPoint(BC.Number)))
784 return false;
785 }
786
787 // Interference for the live-out value.
788 if (BI.LiveOut) {
789 if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) {
791 ++Ins;
792 } else if (Intf.last() > BI.LastInstr) {
794 ++Ins;
795 } else if (Intf.last() > BI.FirstInstr) {
796 ++Ins;
797 }
798 }
799
800 // Accumulate the total frequency of inserted spill code.
801 while (Ins--)
802 StaticCost += SpillPlacer->getBlockFrequency(BC.Number);
803 }
804 Cost = StaticCost;
805
806 // Add constraints for use-blocks. Note that these are the only constraints
807 // that may add a positive bias, it is downhill from here.
808 SpillPlacer->addConstraints(SplitConstraints);
809 return SpillPlacer->scanActiveBundles();
810}
811
812/// addThroughConstraints - Add constraints and links to SpillPlacer from the
813/// live-through blocks in Blocks.
814bool RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf,
815 ArrayRef<unsigned> Blocks) {
816 const unsigned GroupSize = 8;
817 SpillPlacement::BlockConstraint BCS[GroupSize];
818 unsigned TBS[GroupSize];
819 unsigned B = 0, T = 0;
820
821 for (unsigned Number : Blocks) {
822 Intf.moveToBlock(Number);
823
824 if (!Intf.hasInterference()) {
825 assert(T < GroupSize && "Array overflow");
826 TBS[T] = Number;
827 if (++T == GroupSize) {
828 SpillPlacer->addLinks(ArrayRef(TBS, T));
829 T = 0;
830 }
831 continue;
832 }
833
834 assert(B < GroupSize && "Array overflow");
835 BCS[B].Number = Number;
836
837 // Abort if the spill cannot be inserted at the MBB' start
838 MachineBasicBlock *MBB = MF->getBlockNumbered(Number);
839 auto FirstNonDebugInstr = MBB->getFirstNonDebugInstr();
840 if (FirstNonDebugInstr != MBB->end() &&
841 SlotIndex::isEarlierInstr(LIS->getInstructionIndex(*FirstNonDebugInstr),
842 SA->getFirstSplitPoint(Number)))
843 return false;
844 // Interference for the live-in value.
845 if (Intf.first() <= Indexes->getMBBStartIdx(Number))
847 else
849
850 // Interference for the live-out value.
851 if (Intf.last() >= SA->getLastSplitPoint(Number))
853 else
855
856 if (++B == GroupSize) {
857 SpillPlacer->addConstraints(ArrayRef(BCS, B));
858 B = 0;
859 }
860 }
861
862 SpillPlacer->addConstraints(ArrayRef(BCS, B));
863 SpillPlacer->addLinks(ArrayRef(TBS, T));
864 return true;
865}
866
867bool RAGreedy::growRegion(GlobalSplitCandidate &Cand) {
868 // Keep track of through blocks that have not been added to SpillPlacer.
869 BitVector Todo = SA->getThroughBlocks();
870 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks;
871 unsigned AddedTo = 0;
872#ifndef NDEBUG
873 unsigned Visited = 0;
874#endif
875
876 unsigned long Budget = GrowRegionComplexityBudget;
877 while (true) {
878 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive();
879 // Find new through blocks in the periphery of PrefRegBundles.
880 for (unsigned Bundle : NewBundles) {
881 // Look at all blocks connected to Bundle in the full graph.
882 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle);
883 // Limit compilation time by bailing out after we use all our budget.
884 if (Blocks.size() >= Budget)
885 return false;
886 Budget -= Blocks.size();
887 for (unsigned Block : Blocks) {
888 if (!Todo.test(Block))
889 continue;
890 Todo.reset(Block);
891 // This is a new through block. Add it to SpillPlacer later.
892 ActiveBlocks.push_back(Block);
893#ifndef NDEBUG
894 ++Visited;
895#endif
896 }
897 }
898 // Any new blocks to add?
899 if (ActiveBlocks.size() == AddedTo)
900 break;
901
902 // Compute through constraints from the interference, or assume that all
903 // through blocks prefer spilling when forming compact regions.
904 auto NewBlocks = ArrayRef(ActiveBlocks).slice(AddedTo);
905 if (Cand.PhysReg) {
906 if (!addThroughConstraints(Cand.Intf, NewBlocks))
907 return false;
908 } else {
909 // Providing that the variable being spilled does not look like a loop
910 // induction variable, which is expensive to spill around and better
911 // pushed into a condition inside the loop if possible, provide a strong
912 // negative bias on through blocks to prevent unwanted liveness on loop
913 // backedges.
914 bool PrefSpill = true;
915 if (SA->looksLikeLoopIV() && NewBlocks.size() >= 2) {
916 // Check that the current bundle is adding a Header + start+end of
917 // loop-internal blocks. If the block is indeed a header, don't make
918 // the NewBlocks as PrefSpill to allow the variable to be live in
919 // Header<->Latch.
920 MachineLoop *L = Loops->getLoopFor(MF->getBlockNumbered(NewBlocks[0]));
921 if (L && L->getHeader()->getNumber() == (int)NewBlocks[0] &&
922 all_of(NewBlocks.drop_front(), [&](unsigned Block) {
923 return L == Loops->getLoopFor(MF->getBlockNumbered(Block));
924 }))
925 PrefSpill = false;
926 }
927 if (PrefSpill)
928 SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true);
929 }
930 AddedTo = ActiveBlocks.size();
931
932 // Perhaps iterating can enable more bundles?
933 SpillPlacer->iterate();
934 }
935 LLVM_DEBUG(dbgs() << ", v=" << Visited);
936 return true;
937}
938
939/// calcCompactRegion - Compute the set of edge bundles that should be live
940/// when splitting the current live range into compact regions. Compact
941/// regions can be computed without looking at interference. They are the
942/// regions formed by removing all the live-through blocks from the live range.
943///
944/// Returns false if the current live range is already compact, or if the
945/// compact regions would form single block regions anyway.
946bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) {
947 // Without any through blocks, the live range is already compact.
948 if (!SA->getNumThroughBlocks())
949 return false;
950
951 // Compact regions don't correspond to any physreg.
952 Cand.reset(IntfCache, MCRegister::NoRegister);
953
954 LLVM_DEBUG(dbgs() << "Compact region bundles");
955
956 // Use the spill placer to determine the live bundles. GrowRegion pretends
957 // that all the through blocks have interference when PhysReg is unset.
958 SpillPlacer->prepare(Cand.LiveBundles);
959
960 // The static split cost will be zero since Cand.Intf reports no interference.
961 BlockFrequency Cost;
962 if (!addSplitConstraints(Cand.Intf, Cost)) {
963 LLVM_DEBUG(dbgs() << ", none.\n");
964 return false;
965 }
966
967 if (!growRegion(Cand)) {
968 LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n");
969 return false;
970 }
971
972 SpillPlacer->finish();
973
974 if (!Cand.LiveBundles.any()) {
975 LLVM_DEBUG(dbgs() << ", none.\n");
976 return false;
977 }
978
979 LLVM_DEBUG({
980 for (int I : Cand.LiveBundles.set_bits())
981 dbgs() << " EB#" << I;
982 dbgs() << ".\n";
983 });
984 return true;
985}
986
987/// calcBlockSplitCost - Compute how expensive it would be to split the live
988/// range in SA around all use blocks instead of forming bundle regions.
989BlockFrequency RAGreedy::calcBlockSplitCost() {
990 BlockFrequency Cost = BlockFrequency(0);
991 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
992 for (const SplitAnalysis::BlockInfo &BI : UseBlocks) {
993 unsigned Number = BI.MBB->getNumber();
994 // We normally only need one spill instruction - a load or a store.
995 Cost += SpillPlacer->getBlockFrequency(Number);
996
997 // Unless the value is redefined in the block.
998 if (BI.LiveIn && BI.LiveOut && BI.FirstDef)
999 Cost += SpillPlacer->getBlockFrequency(Number);
1000 }
1001 return Cost;
1002}
1003
1004/// calcGlobalSplitCost - Return the global split cost of following the split
1005/// pattern in LiveBundles. This cost should be added to the local cost of the
1006/// interference pattern in SplitConstraints.
1007///
1008BlockFrequency RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand,
1009 const AllocationOrder &Order) {
1010 BlockFrequency GlobalCost = BlockFrequency(0);
1011 const BitVector &LiveBundles = Cand.LiveBundles;
1012 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1013 for (unsigned I = 0; I != UseBlocks.size(); ++I) {
1014 const SplitAnalysis::BlockInfo &BI = UseBlocks[I];
1015 SpillPlacement::BlockConstraint &BC = SplitConstraints[I];
1016 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, false)];
1017 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, true)];
1018 unsigned Ins = 0;
1019
1020 Cand.Intf.moveToBlock(BC.Number);
1021
1022 if (BI.LiveIn)
1023 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg);
1024 if (BI.LiveOut)
1025 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg);
1026 while (Ins--)
1027 GlobalCost += SpillPlacer->getBlockFrequency(BC.Number);
1028 }
1029
1030 for (unsigned Number : Cand.ActiveBlocks) {
1031 bool RegIn = LiveBundles[Bundles->getBundle(Number, false)];
1032 bool RegOut = LiveBundles[Bundles->getBundle(Number, true)];
1033 if (!RegIn && !RegOut)
1034 continue;
1035 if (RegIn && RegOut) {
1036 // We need double spill code if this block has interference.
1037 Cand.Intf.moveToBlock(Number);
1038 if (Cand.Intf.hasInterference()) {
1039 GlobalCost += SpillPlacer->getBlockFrequency(Number);
1040 GlobalCost += SpillPlacer->getBlockFrequency(Number);
1041 }
1042 continue;
1043 }
1044 // live-in / stack-out or stack-in live-out.
1045 GlobalCost += SpillPlacer->getBlockFrequency(Number);
1046 }
1047 return GlobalCost;
1048}
1049
1050/// splitAroundRegion - Split the current live range around the regions
1051/// determined by BundleCand and GlobalCand.
1052///
1053/// Before calling this function, GlobalCand and BundleCand must be initialized
1054/// so each bundle is assigned to a valid candidate, or NoCand for the
1055/// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor
1056/// objects must be initialized for the current live range, and intervals
1057/// created for the used candidates.
1058///
1059/// @param LREdit The LiveRangeEdit object handling the current split.
1060/// @param UsedCands List of used GlobalCand entries. Every BundleCand value
1061/// must appear in this list.
1062void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit,
1063 ArrayRef<unsigned> UsedCands) {
1064 // These are the intervals created for new global ranges. We may create more
1065 // intervals for local ranges.
1066 const unsigned NumGlobalIntvs = LREdit.size();
1067 LLVM_DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs
1068 << " globals.\n");
1069 assert(NumGlobalIntvs && "No global intervals configured");
1070
1071 // Isolate even single instructions when dealing with a proper sub-class.
1072 // That guarantees register class inflation for the stack interval because it
1073 // is all copies.
1074 Register Reg = SA->getParent().reg();
1075 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
1076
1077 // First handle all the blocks with uses.
1078 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1079 for (const SplitAnalysis::BlockInfo &BI : UseBlocks) {
1080 unsigned Number = BI.MBB->getNumber();
1081 unsigned IntvIn = 0, IntvOut = 0;
1082 SlotIndex IntfIn, IntfOut;
1083 if (BI.LiveIn) {
1084 unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)];
1085 if (CandIn != NoCand) {
1086 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
1087 IntvIn = Cand.IntvIdx;
1088 Cand.Intf.moveToBlock(Number);
1089 IntfIn = Cand.Intf.first();
1090 }
1091 }
1092 if (BI.LiveOut) {
1093 unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)];
1094 if (CandOut != NoCand) {
1095 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
1096 IntvOut = Cand.IntvIdx;
1097 Cand.Intf.moveToBlock(Number);
1098 IntfOut = Cand.Intf.last();
1099 }
1100 }
1101
1102 // Create separate intervals for isolated blocks with multiple uses.
1103 if (!IntvIn && !IntvOut) {
1104 LLVM_DEBUG(dbgs() << printMBBReference(*BI.MBB) << " isolated.\n");
1105 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
1106 SE->splitSingleBlock(BI);
1107 continue;
1108 }
1109
1110 if (IntvIn && IntvOut)
1111 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1112 else if (IntvIn)
1113 SE->splitRegInBlock(BI, IntvIn, IntfIn);
1114 else
1115 SE->splitRegOutBlock(BI, IntvOut, IntfOut);
1116 }
1117
1118 // Handle live-through blocks. The relevant live-through blocks are stored in
1119 // the ActiveBlocks list with each candidate. We need to filter out
1120 // duplicates.
1121 BitVector Todo = SA->getThroughBlocks();
1122 for (unsigned UsedCand : UsedCands) {
1123 ArrayRef<unsigned> Blocks = GlobalCand[UsedCand].ActiveBlocks;
1124 for (unsigned Number : Blocks) {
1125 if (!Todo.test(Number))
1126 continue;
1127 Todo.reset(Number);
1128
1129 unsigned IntvIn = 0, IntvOut = 0;
1130 SlotIndex IntfIn, IntfOut;
1131
1132 unsigned CandIn = BundleCand[Bundles->getBundle(Number, false)];
1133 if (CandIn != NoCand) {
1134 GlobalSplitCandidate &Cand = GlobalCand[CandIn];
1135 IntvIn = Cand.IntvIdx;
1136 Cand.Intf.moveToBlock(Number);
1137 IntfIn = Cand.Intf.first();
1138 }
1139
1140 unsigned CandOut = BundleCand[Bundles->getBundle(Number, true)];
1141 if (CandOut != NoCand) {
1142 GlobalSplitCandidate &Cand = GlobalCand[CandOut];
1143 IntvOut = Cand.IntvIdx;
1144 Cand.Intf.moveToBlock(Number);
1145 IntfOut = Cand.Intf.last();
1146 }
1147 if (!IntvIn && !IntvOut)
1148 continue;
1149 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut);
1150 }
1151 }
1152
1153 ++NumGlobalSplits;
1154
1155 SmallVector<unsigned, 8> IntvMap;
1156 SE->finish(&IntvMap);
1157 DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
1158
1159 unsigned OrigBlocks = SA->getNumLiveBlocks();
1160
1161 // Sort out the new intervals created by splitting. We get four kinds:
1162 // - Remainder intervals should not be split again.
1163 // - Candidate intervals can be assigned to Cand.PhysReg.
1164 // - Block-local splits are candidates for local splitting.
1165 // - DCE leftovers should go back on the queue.
1166 for (unsigned I = 0, E = LREdit.size(); I != E; ++I) {
1167 const LiveInterval &Reg = LIS->getInterval(LREdit.get(I));
1168
1169 // Ignore old intervals from DCE.
1170 if (ExtraInfo->getOrInitStage(Reg.reg()) != RS_New)
1171 continue;
1172
1173 // Remainder interval. Don't try splitting again, spill if it doesn't
1174 // allocate.
1175 if (IntvMap[I] == 0) {
1176 ExtraInfo->setStage(Reg, RS_Spill);
1177 continue;
1178 }
1179
1180 // Global intervals. Allow repeated splitting as long as the number of live
1181 // blocks is strictly decreasing.
1182 if (IntvMap[I] < NumGlobalIntvs) {
1183 if (SA->countLiveBlocks(&Reg) >= OrigBlocks) {
1184 LLVM_DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks
1185 << " blocks as original.\n");
1186 // Don't allow repeated splitting as a safe guard against looping.
1187 ExtraInfo->setStage(Reg, RS_Split2);
1188 }
1189 continue;
1190 }
1191
1192 // Other intervals are treated as new. This includes local intervals created
1193 // for blocks with multiple uses, and anything created by DCE.
1194 }
1195
1196 if (VerifyEnabled)
1197 MF->verify(LIS, Indexes, "After splitting live range around region",
1198 &errs());
1199}
1200
1201MCRegister RAGreedy::tryRegionSplit(const LiveInterval &VirtReg,
1202 AllocationOrder &Order,
1203 SmallVectorImpl<Register> &NewVRegs) {
1204 if (!TRI->shouldRegionSplitForVirtReg(*MF, VirtReg))
1206 unsigned NumCands = 0;
1207 BlockFrequency SpillCost = calcBlockSplitCost();
1208 BlockFrequency BestCost;
1209
1210 // Check if we can split this live range around a compact region.
1211 bool HasCompact = calcCompactRegion(GlobalCand.front());
1212 if (HasCompact) {
1213 // Yes, keep GlobalCand[0] as the compact region candidate.
1214 NumCands = 1;
1215 BestCost = BlockFrequency::max();
1216 } else {
1217 // No benefit from the compact region, our fallback will be per-block
1218 // splitting. Make sure we find a solution that is cheaper than spilling.
1219 BestCost = SpillCost;
1220 LLVM_DEBUG(dbgs() << "Cost of isolating all blocks = "
1221 << printBlockFreq(*MBFI, BestCost) << '\n');
1222 }
1223
1224 unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost,
1225 NumCands, false /*IgnoreCSR*/);
1226
1227 // No solutions found, fall back to single block splitting.
1228 if (!HasCompact && BestCand == NoCand)
1230
1231 return doRegionSplit(VirtReg, BestCand, HasCompact, NewVRegs);
1232}
1233
1234unsigned RAGreedy::calculateRegionSplitCostAroundReg(MCRegister PhysReg,
1235 AllocationOrder &Order,
1236 BlockFrequency &BestCost,
1237 unsigned &NumCands,
1238 unsigned &BestCand) {
1239 // Discard bad candidates before we run out of interference cache cursors.
1240 // This will only affect register classes with a lot of registers (>32).
1241 if (NumCands == IntfCache.getMaxCursors()) {
1242 unsigned WorstCount = ~0u;
1243 unsigned Worst = 0;
1244 for (unsigned CandIndex = 0; CandIndex != NumCands; ++CandIndex) {
1245 if (CandIndex == BestCand || !GlobalCand[CandIndex].PhysReg)
1246 continue;
1247 unsigned Count = GlobalCand[CandIndex].LiveBundles.count();
1248 if (Count < WorstCount) {
1249 Worst = CandIndex;
1250 WorstCount = Count;
1251 }
1252 }
1253 --NumCands;
1254 GlobalCand[Worst] = GlobalCand[NumCands];
1255 if (BestCand == NumCands)
1256 BestCand = Worst;
1257 }
1258
1259 if (GlobalCand.size() <= NumCands)
1260 GlobalCand.resize(NumCands+1);
1261 GlobalSplitCandidate &Cand = GlobalCand[NumCands];
1262 Cand.reset(IntfCache, PhysReg);
1263
1264 SpillPlacer->prepare(Cand.LiveBundles);
1265 BlockFrequency Cost;
1266 if (!addSplitConstraints(Cand.Intf, Cost)) {
1267 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << "\tno positive bundles\n");
1268 return BestCand;
1269 }
1270 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI)
1271 << "\tstatic = " << printBlockFreq(*MBFI, Cost));
1272 if (Cost >= BestCost) {
1273 LLVM_DEBUG({
1274 if (BestCand == NoCand)
1275 dbgs() << " worse than no bundles\n";
1276 else
1277 dbgs() << " worse than "
1278 << printReg(GlobalCand[BestCand].PhysReg, TRI) << '\n';
1279 });
1280 return BestCand;
1281 }
1282 if (!growRegion(Cand)) {
1283 LLVM_DEBUG(dbgs() << ", cannot spill all interferences.\n");
1284 return BestCand;
1285 }
1286
1287 SpillPlacer->finish();
1288
1289 // No live bundles, defer to splitSingleBlocks().
1290 if (!Cand.LiveBundles.any()) {
1291 LLVM_DEBUG(dbgs() << " no bundles.\n");
1292 return BestCand;
1293 }
1294
1295 Cost += calcGlobalSplitCost(Cand, Order);
1296 LLVM_DEBUG({
1297 dbgs() << ", total = " << printBlockFreq(*MBFI, Cost) << " with bundles";
1298 for (int I : Cand.LiveBundles.set_bits())
1299 dbgs() << " EB#" << I;
1300 dbgs() << ".\n";
1301 });
1302 if (Cost < BestCost) {
1303 BestCand = NumCands;
1304 BestCost = Cost;
1305 }
1306 ++NumCands;
1307
1308 return BestCand;
1309}
1310
1311unsigned RAGreedy::calculateRegionSplitCost(const LiveInterval &VirtReg,
1312 AllocationOrder &Order,
1313 BlockFrequency &BestCost,
1314 unsigned &NumCands,
1315 bool IgnoreCSR) {
1316 unsigned BestCand = NoCand;
1317 for (MCRegister PhysReg : Order) {
1318 assert(PhysReg);
1319 if (IgnoreCSR && EvictAdvisor->isUnusedCalleeSavedReg(PhysReg))
1320 continue;
1321
1322 calculateRegionSplitCostAroundReg(PhysReg, Order, BestCost, NumCands,
1323 BestCand);
1324 }
1325
1326 return BestCand;
1327}
1328
1329MCRegister RAGreedy::doRegionSplit(const LiveInterval &VirtReg,
1330 unsigned BestCand, bool HasCompact,
1331 SmallVectorImpl<Register> &NewVRegs) {
1332 SmallVector<unsigned, 8> UsedCands;
1333 // Prepare split editor.
1334 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
1335 SE->reset(LREdit, SplitSpillMode);
1336
1337 // Assign all edge bundles to the preferred candidate, or NoCand.
1338 BundleCand.assign(Bundles->getNumBundles(), NoCand);
1339
1340 // Assign bundles for the best candidate region.
1341 if (BestCand != NoCand) {
1342 GlobalSplitCandidate &Cand = GlobalCand[BestCand];
1343 if (unsigned B = Cand.getBundles(BundleCand, BestCand)) {
1344 UsedCands.push_back(BestCand);
1345 Cand.IntvIdx = SE->openIntv();
1346 LLVM_DEBUG(dbgs() << "Split for " << printReg(Cand.PhysReg, TRI) << " in "
1347 << B << " bundles, intv " << Cand.IntvIdx << ".\n");
1348 (void)B;
1349 }
1350 }
1351
1352 // Assign bundles for the compact region.
1353 if (HasCompact) {
1354 GlobalSplitCandidate &Cand = GlobalCand.front();
1355 assert(!Cand.PhysReg && "Compact region has no physreg");
1356 if (unsigned B = Cand.getBundles(BundleCand, 0)) {
1357 UsedCands.push_back(0);
1358 Cand.IntvIdx = SE->openIntv();
1359 LLVM_DEBUG(dbgs() << "Split for compact region in " << B
1360 << " bundles, intv " << Cand.IntvIdx << ".\n");
1361 (void)B;
1362 }
1363 }
1364
1365 splitAroundRegion(LREdit, UsedCands);
1366 return MCRegister();
1367}
1368
1369// VirtReg has a physical Hint, this function tries to split VirtReg around
1370// Hint if we can place new COPY instructions in cold blocks.
1371bool RAGreedy::trySplitAroundHintReg(MCRegister Hint,
1372 const LiveInterval &VirtReg,
1373 SmallVectorImpl<Register> &NewVRegs,
1374 AllocationOrder &Order) {
1375 // Split the VirtReg may generate COPY instructions in multiple cold basic
1376 // blocks, and increase code size. So we avoid it when the function is
1377 // optimized for size.
1378 if (MF->getFunction().hasOptSize())
1379 return false;
1380
1381 // Don't allow repeated splitting as a safe guard against looping.
1382 if (ExtraInfo->getStage(VirtReg) >= RS_Split2)
1383 return false;
1384
1385 BlockFrequency Cost = BlockFrequency(0);
1386 Register Reg = VirtReg.reg();
1387
1388 // Compute the cost of assigning a non Hint physical register to VirtReg.
1389 // We define it as the total frequency of broken COPY instructions to/from
1390 // Hint register, and after split, they can be deleted.
1391
1392 // FIXME: This is miscounting the costs with subregisters. In particular, this
1393 // should support recognizing SplitKit formed copy bundles instead of direct
1394 // copy instructions, which will appear in the same block.
1395 for (const MachineOperand &Opnd : MRI->reg_nodbg_operands(Reg)) {
1396 const MachineInstr &Instr = *Opnd.getParent();
1397 if (!Instr.isCopy() || Opnd.isImplicit())
1398 continue;
1399
1400 // Look for the other end of the copy.
1401 const bool IsDef = Opnd.isDef();
1402 const MachineOperand &OtherOpnd = Instr.getOperand(IsDef);
1403 Register OtherReg = OtherOpnd.getReg();
1404 assert(Reg == Opnd.getReg());
1405 if (OtherReg == Reg)
1406 continue;
1407
1408 unsigned SubReg = Opnd.getSubReg();
1409 unsigned OtherSubReg = OtherOpnd.getSubReg();
1410 if (SubReg && OtherSubReg && SubReg != OtherSubReg)
1411 continue;
1412
1413 // Check if VirtReg interferes with OtherReg after this COPY instruction.
1414 if (Opnd.readsReg()) {
1415 SlotIndex Index = LIS->getInstructionIndex(Instr).getRegSlot();
1416
1417 if (SubReg) {
1418 LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg);
1419 if (IsDef)
1420 Mask = ~Mask;
1421
1422 if (any_of(VirtReg.subranges(), [=](const LiveInterval::SubRange &S) {
1423 return (S.LaneMask & Mask).any() && S.liveAt(Index);
1424 })) {
1425 continue;
1426 }
1427 } else {
1428 if (VirtReg.liveAt(Index))
1429 continue;
1430 }
1431 }
1432
1433 MCRegister OtherPhysReg =
1434 OtherReg.isPhysical() ? OtherReg.asMCReg() : VRM->getPhys(OtherReg);
1435 MCRegister ThisHint = SubReg ? TRI->getSubReg(Hint, SubReg) : Hint;
1436 if (OtherPhysReg == ThisHint)
1437 Cost += MBFI->getBlockFreq(Instr.getParent());
1438 }
1439
1440 // Decrease the cost so it will be split in colder blocks.
1441 BranchProbability Threshold(SplitThresholdForRegWithHint, 100);
1442 Cost *= Threshold;
1443 if (Cost == BlockFrequency(0))
1444 return false;
1445
1446 unsigned NumCands = 0;
1447 unsigned BestCand = NoCand;
1448 SA->analyze(&VirtReg);
1449 calculateRegionSplitCostAroundReg(Hint, Order, Cost, NumCands, BestCand);
1450 if (BestCand == NoCand)
1451 return false;
1452
1453 doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs);
1454 return true;
1455}
1456
1457//===----------------------------------------------------------------------===//
1458// Per-Block Splitting
1459//===----------------------------------------------------------------------===//
1460
1461/// tryBlockSplit - Split a global live range around every block with uses. This
1462/// creates a lot of local live ranges, that will be split by tryLocalSplit if
1463/// they don't allocate.
1464MCRegister RAGreedy::tryBlockSplit(const LiveInterval &VirtReg,
1465 AllocationOrder &Order,
1466 SmallVectorImpl<Register> &NewVRegs) {
1467 assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed");
1468 Register Reg = VirtReg.reg();
1469 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg));
1470 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
1471 SE->reset(LREdit, SplitSpillMode);
1472 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks();
1473 for (const SplitAnalysis::BlockInfo &BI : UseBlocks) {
1474 if (SA->shouldSplitSingleBlock(BI, SingleInstrs))
1475 SE->splitSingleBlock(BI);
1476 }
1477 // No blocks were split.
1478 if (LREdit.empty())
1479 return MCRegister();
1480
1481 // We did split for some blocks.
1482 SmallVector<unsigned, 8> IntvMap;
1483 SE->finish(&IntvMap);
1484
1485 // Tell LiveDebugVariables about the new ranges.
1486 DebugVars->splitRegister(Reg, LREdit.regs(), *LIS);
1487
1488 // Sort out the new intervals created by splitting. The remainder interval
1489 // goes straight to spilling, the new local ranges get to stay RS_New.
1490 for (unsigned I = 0, E = LREdit.size(); I != E; ++I) {
1491 const LiveInterval &LI = LIS->getInterval(LREdit.get(I));
1492 if (ExtraInfo->getOrInitStage(LI.reg()) == RS_New && IntvMap[I] == 0)
1493 ExtraInfo->setStage(LI, RS_Spill);
1494 }
1495
1496 if (VerifyEnabled)
1497 MF->verify(LIS, Indexes, "After splitting live range around basic blocks",
1498 &errs());
1499 return MCRegister();
1500}
1501
1502//===----------------------------------------------------------------------===//
1503// Per-Instruction Splitting
1504//===----------------------------------------------------------------------===//
1505
1506/// Get the number of allocatable registers that match the constraints of \p Reg
1507/// on \p MI and that are also in \p SuperRC.
1509 const MachineInstr *MI, Register Reg, const TargetRegisterClass *SuperRC,
1511 const RegisterClassInfo &RCI) {
1512 assert(SuperRC && "Invalid register class");
1513
1514 const TargetRegisterClass *ConstrainedRC =
1515 MI->getRegClassConstraintEffectForVReg(Reg, SuperRC, TII, TRI,
1516 /* ExploreBundle */ true);
1517 if (!ConstrainedRC)
1518 return 0;
1519 return RCI.getNumAllocatableRegs(ConstrainedRC);
1520}
1521
1523 const TargetRegisterInfo &TRI,
1524 const MachineInstr &FirstMI,
1525 Register Reg) {
1526 LaneBitmask Mask;
1528 (void)AnalyzeVirtRegInBundle(const_cast<MachineInstr &>(FirstMI), Reg, &Ops);
1529
1530 for (auto [MI, OpIdx] : Ops) {
1531 const MachineOperand &MO = MI->getOperand(OpIdx);
1532 assert(MO.isReg() && MO.getReg() == Reg);
1533 unsigned SubReg = MO.getSubReg();
1534 if (SubReg == 0 && MO.isUse()) {
1535 if (MO.isUndef())
1536 continue;
1538 }
1539
1540 LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(SubReg);
1541 if (MO.isDef()) {
1542 if (!MO.isUndef())
1543 Mask |= ~SubRegMask;
1544 } else
1545 Mask |= SubRegMask;
1546 }
1547
1548 return Mask;
1549}
1550
1551/// Return true if \p MI at \P Use reads a subset of the lanes live in \p
1552/// VirtReg.
1554 const MachineInstr *MI, const LiveInterval &VirtReg,
1556 const TargetInstrInfo *TII) {
1557 // Early check the common case. Beware of the semi-formed bundles SplitKit
1558 // creates by setting the bundle flag on copies without a matching BUNDLE.
1559
1560 auto DestSrc = TII->isCopyInstr(*MI);
1561 if (DestSrc && !MI->isBundled() &&
1562 DestSrc->Destination->getSubReg() == DestSrc->Source->getSubReg())
1563 return false;
1564
1565 // FIXME: We're only considering uses, but should be consider defs too?
1566 LaneBitmask ReadMask = getInstReadLaneMask(MRI, *TRI, *MI, VirtReg.reg());
1567
1568 LaneBitmask LiveAtMask;
1569 for (const LiveInterval::SubRange &S : VirtReg.subranges()) {
1570 if (S.liveAt(Use))
1571 LiveAtMask |= S.LaneMask;
1572 }
1573
1574 // If the live lanes aren't different from the lanes used by the instruction,
1575 // this doesn't help.
1576 return (ReadMask & ~(LiveAtMask & TRI->getCoveringLanes())).any();
1577}
1578
1579/// tryInstructionSplit - Split a live range around individual instructions.
1580/// This is normally not worthwhile since the spiller is doing essentially the
1581/// same thing. However, when the live range is in a constrained register
1582/// class, it may help to insert copies such that parts of the live range can
1583/// be moved to a larger register class.
1584///
1585/// This is similar to spilling to a larger register class.
1586MCRegister RAGreedy::tryInstructionSplit(const LiveInterval &VirtReg,
1587 AllocationOrder &Order,
1588 SmallVectorImpl<Register> &NewVRegs) {
1589 const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg());
1590 // There is no point to this if there are no larger sub-classes.
1591
1592 bool SplitSubClass = true;
1593 if (!RegClassInfo.isProperSubClass(CurRC)) {
1594 if (!VirtReg.hasSubRanges())
1595 return MCRegister();
1596 SplitSubClass = false;
1597 }
1598
1599 // Always enable split spill mode, since we're effectively spilling to a
1600 // register.
1601 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
1602 SE->reset(LREdit, SplitEditor::SM_Size);
1603
1604 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
1605 if (Uses.size() <= 1)
1606 return MCRegister();
1607
1608 LLVM_DEBUG(dbgs() << "Split around " << Uses.size()
1609 << " individual instrs.\n");
1610
1611 const TargetRegisterClass *SuperRC =
1612 TRI->getLargestLegalSuperClass(CurRC, *MF);
1613 unsigned SuperRCNumAllocatableRegs =
1614 RegClassInfo.getNumAllocatableRegs(SuperRC);
1615 // Split around every non-copy instruction if this split will relax
1616 // the constraints on the virtual register.
1617 // Otherwise, splitting just inserts uncoalescable copies that do not help
1618 // the allocation.
1619 for (const SlotIndex Use : Uses) {
1620 if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Use)) {
1621 if (TII->isFullCopyInstr(*MI) ||
1622 (SplitSubClass &&
1623 SuperRCNumAllocatableRegs ==
1624 getNumAllocatableRegsForConstraints(MI, VirtReg.reg(), SuperRC,
1625 TII, TRI, RegClassInfo)) ||
1626 // TODO: Handle split for subranges with subclass constraints?
1627 (!SplitSubClass && VirtReg.hasSubRanges() &&
1628 !readsLaneSubset(*MRI, MI, VirtReg, TRI, Use, TII))) {
1629 LLVM_DEBUG(dbgs() << " skip:\t" << Use << '\t' << *MI);
1630 continue;
1631 }
1632 }
1633 SE->openIntv();
1634 SlotIndex SegStart = SE->enterIntvBefore(Use);
1635 SlotIndex SegStop = SE->leaveIntvAfter(Use);
1636 SE->useIntv(SegStart, SegStop);
1637 }
1638
1639 if (LREdit.empty()) {
1640 LLVM_DEBUG(dbgs() << "All uses were copies.\n");
1641 return MCRegister();
1642 }
1643
1644 SmallVector<unsigned, 8> IntvMap;
1645 SE->finish(&IntvMap);
1646 DebugVars->splitRegister(VirtReg.reg(), LREdit.regs(), *LIS);
1647 // Assign all new registers to RS_Spill. This was the last chance.
1648 ExtraInfo->setStage(LREdit.begin(), LREdit.end(), RS_Spill);
1649 return MCRegister();
1650}
1651
1652//===----------------------------------------------------------------------===//
1653// Local Splitting
1654//===----------------------------------------------------------------------===//
1655
1656/// calcGapWeights - Compute the maximum spill weight that needs to be evicted
1657/// in order to use PhysReg between two entries in SA->UseSlots.
1658///
1659/// GapWeight[I] represents the gap between UseSlots[I] and UseSlots[I + 1].
1660///
1661void RAGreedy::calcGapWeights(MCRegister PhysReg,
1662 SmallVectorImpl<float> &GapWeight) {
1663 assert(SA->getUseBlocks().size() == 1 && "Not a local interval");
1664 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1665 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
1666 const unsigned NumGaps = Uses.size()-1;
1667
1668 // Start and end points for the interference check.
1669 SlotIndex StartIdx =
1671 SlotIndex StopIdx =
1673
1674 GapWeight.assign(NumGaps, 0.0f);
1675
1676 // Add interference from each overlapping register.
1677 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
1678 if (!Matrix->query(const_cast<LiveInterval &>(SA->getParent()), Unit)
1679 .checkInterference())
1680 continue;
1681
1682 // We know that VirtReg is a continuous interval from FirstInstr to
1683 // LastInstr, so we don't need InterferenceQuery.
1684 //
1685 // Interference that overlaps an instruction is counted in both gaps
1686 // surrounding the instruction. The exception is interference before
1687 // StartIdx and after StopIdx.
1688 //
1690 Matrix->getLiveUnions()[static_cast<unsigned>(Unit)].find(StartIdx);
1691 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) {
1692 // Skip the gaps before IntI.
1693 while (Uses[Gap+1].getBoundaryIndex() < IntI.start())
1694 if (++Gap == NumGaps)
1695 break;
1696 if (Gap == NumGaps)
1697 break;
1698
1699 // Update the gaps covered by IntI.
1700 const float weight = IntI.value()->weight();
1701 for (; Gap != NumGaps; ++Gap) {
1702 GapWeight[Gap] = std::max(GapWeight[Gap], weight);
1703 if (Uses[Gap+1].getBaseIndex() >= IntI.stop())
1704 break;
1705 }
1706 if (Gap == NumGaps)
1707 break;
1708 }
1709 }
1710
1711 // Add fixed interference.
1712 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
1713 const LiveRange &LR = LIS->getRegUnit(Unit);
1714 LiveRange::const_iterator I = LR.find(StartIdx);
1716
1717 // Same loop as above. Mark any overlapped gaps as HUGE_VALF.
1718 for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) {
1719 while (Uses[Gap+1].getBoundaryIndex() < I->start)
1720 if (++Gap == NumGaps)
1721 break;
1722 if (Gap == NumGaps)
1723 break;
1724
1725 for (; Gap != NumGaps; ++Gap) {
1726 GapWeight[Gap] = huge_valf;
1727 if (Uses[Gap+1].getBaseIndex() >= I->end)
1728 break;
1729 }
1730 if (Gap == NumGaps)
1731 break;
1732 }
1733 }
1734}
1735
1736/// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only
1737/// basic block.
1738///
1739MCRegister RAGreedy::tryLocalSplit(const LiveInterval &VirtReg,
1740 AllocationOrder &Order,
1741 SmallVectorImpl<Register> &NewVRegs) {
1742 // TODO: the function currently only handles a single UseBlock; it should be
1743 // possible to generalize.
1744 if (SA->getUseBlocks().size() != 1)
1745 return MCRegister();
1746
1747 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front();
1748
1749 // Note that it is possible to have an interval that is live-in or live-out
1750 // while only covering a single block - A phi-def can use undef values from
1751 // predecessors, and the block could be a single-block loop.
1752 // We don't bother doing anything clever about such a case, we simply assume
1753 // that the interval is continuous from FirstInstr to LastInstr. We should
1754 // make sure that we don't do anything illegal to such an interval, though.
1755
1756 ArrayRef<SlotIndex> Uses = SA->getUseSlots();
1757 if (Uses.size() <= 2)
1758 return MCRegister();
1759 const unsigned NumGaps = Uses.size()-1;
1760
1761 LLVM_DEBUG({
1762 dbgs() << "tryLocalSplit: ";
1763 for (const auto &Use : Uses)
1764 dbgs() << ' ' << Use;
1765 dbgs() << '\n';
1766 });
1767
1768 // If VirtReg is live across any register mask operands, compute a list of
1769 // gaps with register masks.
1770 SmallVector<unsigned, 8> RegMaskGaps;
1771 if (Matrix->checkRegMaskInterference(VirtReg)) {
1772 // Get regmask slots for the whole block.
1773 ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber());
1774 LLVM_DEBUG(dbgs() << RMS.size() << " regmasks in block:");
1775 // Constrain to VirtReg's live range.
1776 unsigned RI =
1777 llvm::lower_bound(RMS, Uses.front().getRegSlot()) - RMS.begin();
1778 unsigned RE = RMS.size();
1779 for (unsigned I = 0; I != NumGaps && RI != RE; ++I) {
1780 // Look for Uses[I] <= RMS <= Uses[I + 1].
1782 if (SlotIndex::isEarlierInstr(Uses[I + 1], RMS[RI]))
1783 continue;
1784 // Skip a regmask on the same instruction as the last use. It doesn't
1785 // overlap the live range.
1786 if (SlotIndex::isSameInstr(Uses[I + 1], RMS[RI]) && I + 1 == NumGaps)
1787 break;
1788 LLVM_DEBUG(dbgs() << ' ' << RMS[RI] << ':' << Uses[I] << '-'
1789 << Uses[I + 1]);
1790 RegMaskGaps.push_back(I);
1791 // Advance ri to the next gap. A regmask on one of the uses counts in
1792 // both gaps.
1793 while (RI != RE && SlotIndex::isEarlierInstr(RMS[RI], Uses[I + 1]))
1794 ++RI;
1795 }
1796 LLVM_DEBUG(dbgs() << '\n');
1797 }
1798
1799 // Since we allow local split results to be split again, there is a risk of
1800 // creating infinite loops. It is tempting to require that the new live
1801 // ranges have less instructions than the original. That would guarantee
1802 // convergence, but it is too strict. A live range with 3 instructions can be
1803 // split 2+3 (including the COPY), and we want to allow that.
1804 //
1805 // Instead we use these rules:
1806 //
1807 // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the
1808 // noop split, of course).
1809 // 2. Require progress be made for ranges with getStage() == RS_Split2. All
1810 // the new ranges must have fewer instructions than before the split.
1811 // 3. New ranges with the same number of instructions are marked RS_Split2,
1812 // smaller ranges are marked RS_New.
1813 //
1814 // These rules allow a 3 -> 2+3 split once, which we need. They also prevent
1815 // excessive splitting and infinite loops.
1816 //
1817 bool ProgressRequired = ExtraInfo->getStage(VirtReg) >= RS_Split2;
1818
1819 // Best split candidate.
1820 unsigned BestBefore = NumGaps;
1821 unsigned BestAfter = 0;
1822 float BestDiff = 0;
1823
1824 const float blockFreq =
1825 SpillPlacer->getBlockFrequency(BI.MBB->getNumber()).getFrequency() *
1826 (1.0f / MBFI->getEntryFreq().getFrequency());
1827 SmallVector<float, 8> GapWeight;
1828
1829 for (MCRegister PhysReg : Order) {
1830 assert(PhysReg);
1831 // Keep track of the largest spill weight that would need to be evicted in
1832 // order to make use of PhysReg between UseSlots[I] and UseSlots[I + 1].
1833 calcGapWeights(PhysReg, GapWeight);
1834
1835 // Remove any gaps with regmask clobbers.
1836 if (Matrix->checkRegMaskInterference(VirtReg, PhysReg))
1837 for (unsigned Gap : RegMaskGaps)
1838 GapWeight[Gap] = huge_valf;
1839
1840 // Try to find the best sequence of gaps to close.
1841 // The new spill weight must be larger than any gap interference.
1842
1843 // We will split before Uses[SplitBefore] and after Uses[SplitAfter].
1844 unsigned SplitBefore = 0, SplitAfter = 1;
1845
1846 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]).
1847 // It is the spill weight that needs to be evicted.
1848 float MaxGap = GapWeight[0];
1849
1850 while (true) {
1851 // Live before/after split?
1852 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn;
1853 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut;
1854
1855 LLVM_DEBUG(dbgs() << printReg(PhysReg, TRI) << ' ' << Uses[SplitBefore]
1856 << '-' << Uses[SplitAfter] << " I=" << MaxGap);
1857
1858 // Stop before the interval gets so big we wouldn't be making progress.
1859 if (!LiveBefore && !LiveAfter) {
1860 LLVM_DEBUG(dbgs() << " all\n");
1861 break;
1862 }
1863 // Should the interval be extended or shrunk?
1864 bool Shrink = true;
1865
1866 // How many gaps would the new range have?
1867 unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter;
1868
1869 // Legally, without causing looping?
1870 bool Legal = !ProgressRequired || NewGaps < NumGaps;
1871
1872 if (Legal && MaxGap < huge_valf) {
1873 // Estimate the new spill weight. Each instruction reads or writes the
1874 // register. Conservatively assume there are no read-modify-write
1875 // instructions.
1876 //
1877 // Try to guess the size of the new interval.
1878 const float EstWeight = normalizeSpillWeight(
1879 blockFreq * (NewGaps + 1),
1880 Uses[SplitBefore].distance(Uses[SplitAfter]) +
1881 (LiveBefore + LiveAfter) * SlotIndex::InstrDist,
1882 1);
1883 // Would this split be possible to allocate?
1884 // Never allocate all gaps, we wouldn't be making progress.
1885 LLVM_DEBUG(dbgs() << " w=" << EstWeight);
1886 if (EstWeight * Hysteresis >= MaxGap) {
1887 Shrink = false;
1888 float Diff = EstWeight - MaxGap;
1889 if (Diff > BestDiff) {
1890 LLVM_DEBUG(dbgs() << " (best)");
1891 BestDiff = Hysteresis * Diff;
1892 BestBefore = SplitBefore;
1893 BestAfter = SplitAfter;
1894 }
1895 }
1896 }
1897
1898 // Try to shrink.
1899 if (Shrink) {
1900 if (++SplitBefore < SplitAfter) {
1901 LLVM_DEBUG(dbgs() << " shrink\n");
1902 // Recompute the max when necessary.
1903 if (GapWeight[SplitBefore - 1] >= MaxGap) {
1904 MaxGap = GapWeight[SplitBefore];
1905 for (unsigned I = SplitBefore + 1; I != SplitAfter; ++I)
1906 MaxGap = std::max(MaxGap, GapWeight[I]);
1907 }
1908 continue;
1909 }
1910 MaxGap = 0;
1911 }
1912
1913 // Try to extend the interval.
1914 if (SplitAfter >= NumGaps) {
1915 LLVM_DEBUG(dbgs() << " end\n");
1916 break;
1917 }
1918
1919 LLVM_DEBUG(dbgs() << " extend\n");
1920 MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]);
1921 }
1922 }
1923
1924 // Didn't find any candidates?
1925 if (BestBefore == NumGaps)
1926 return MCRegister();
1927
1928 LLVM_DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] << '-'
1929 << Uses[BestAfter] << ", " << BestDiff << ", "
1930 << (BestAfter - BestBefore + 1) << " instrs\n");
1931
1932 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
1933 SE->reset(LREdit);
1934
1935 SE->openIntv();
1936 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]);
1937 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]);
1938 SE->useIntv(SegStart, SegStop);
1939 SmallVector<unsigned, 8> IntvMap;
1940 SE->finish(&IntvMap);
1941 DebugVars->splitRegister(VirtReg.reg(), LREdit.regs(), *LIS);
1942 // If the new range has the same number of instructions as before, mark it as
1943 // RS_Split2 so the next split will be forced to make progress. Otherwise,
1944 // leave the new intervals as RS_New so they can compete.
1945 bool LiveBefore = BestBefore != 0 || BI.LiveIn;
1946 bool LiveAfter = BestAfter != NumGaps || BI.LiveOut;
1947 unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter;
1948 if (NewGaps >= NumGaps) {
1949 LLVM_DEBUG(dbgs() << "Tagging non-progress ranges:");
1950 assert(!ProgressRequired && "Didn't make progress when it was required.");
1951 for (unsigned I = 0, E = IntvMap.size(); I != E; ++I)
1952 if (IntvMap[I] == 1) {
1953 ExtraInfo->setStage(LIS->getInterval(LREdit.get(I)), RS_Split2);
1954 LLVM_DEBUG(dbgs() << ' ' << printReg(LREdit.get(I)));
1955 }
1956 LLVM_DEBUG(dbgs() << '\n');
1957 }
1958 ++NumLocalSplits;
1959
1960 return MCRegister();
1961}
1962
1963//===----------------------------------------------------------------------===//
1964// Live Range Splitting
1965//===----------------------------------------------------------------------===//
1966
1967/// trySplit - Try to split VirtReg or one of its interferences, making it
1968/// assignable.
1969/// @return Physreg when VirtReg may be assigned and/or new NewVRegs.
1970MCRegister RAGreedy::trySplit(const LiveInterval &VirtReg,
1971 AllocationOrder &Order,
1972 SmallVectorImpl<Register> &NewVRegs,
1973 const SmallVirtRegSet &FixedRegisters) {
1974 // Ranges must be Split2 or less.
1975 if (ExtraInfo->getStage(VirtReg) >= RS_Spill)
1976 return MCRegister();
1977
1978 // Local intervals are handled separately.
1979 if (LIS->intervalIsInOneMBB(VirtReg)) {
1980 NamedRegionTimer T("local_split", "Local Splitting", TimerGroupName,
1982 SA->analyze(&VirtReg);
1983 MCRegister PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs);
1984 if (PhysReg || !NewVRegs.empty())
1985 return PhysReg;
1986 return tryInstructionSplit(VirtReg, Order, NewVRegs);
1987 }
1988
1989 NamedRegionTimer T("global_split", "Global Splitting", TimerGroupName,
1991
1992 SA->analyze(&VirtReg);
1993
1994 // First try to split around a region spanning multiple blocks. RS_Split2
1995 // ranges already made dubious progress with region splitting, so they go
1996 // straight to single block splitting.
1997 if (ExtraInfo->getStage(VirtReg) < RS_Split2) {
1998 MCRegister PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs);
1999 if (PhysReg || !NewVRegs.empty())
2000 return PhysReg;
2001 }
2002
2003 // Then isolate blocks.
2004 return tryBlockSplit(VirtReg, Order, NewVRegs);
2005}
2006
2007//===----------------------------------------------------------------------===//
2008// Last Chance Recoloring
2009//===----------------------------------------------------------------------===//
2010
2011/// Return true if \p reg has any tied def operand.
2013 for (const MachineOperand &MO : MRI->def_operands(reg))
2014 if (MO.isTied())
2015 return true;
2016
2017 return false;
2018}
2019
2020/// Return true if the existing assignment of \p Intf overlaps, but is not the
2021/// same, as \p PhysReg.
2023 const VirtRegMap &VRM,
2024 MCRegister PhysReg,
2025 const LiveInterval &Intf) {
2026 MCRegister AssignedReg = VRM.getPhys(Intf.reg());
2027 if (PhysReg == AssignedReg)
2028 return false;
2029 return TRI.regsOverlap(PhysReg, AssignedReg);
2030}
2031
2032/// mayRecolorAllInterferences - Check if the virtual registers that
2033/// interfere with \p VirtReg on \p PhysReg (or one of its aliases) may be
2034/// recolored to free \p PhysReg.
2035/// When true is returned, \p RecoloringCandidates has been augmented with all
2036/// the live intervals that need to be recolored in order to free \p PhysReg
2037/// for \p VirtReg.
2038/// \p FixedRegisters contains all the virtual registers that cannot be
2039/// recolored.
2040bool RAGreedy::mayRecolorAllInterferences(
2041 MCRegister PhysReg, const LiveInterval &VirtReg,
2042 SmallLISet &RecoloringCandidates, const SmallVirtRegSet &FixedRegisters) {
2043 const TargetRegisterClass *CurRC = MRI->getRegClass(VirtReg.reg());
2044
2045 for (MCRegUnit Unit : TRI->regunits(PhysReg)) {
2046 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, Unit);
2047 // If there is LastChanceRecoloringMaxInterference or more interferences,
2048 // chances are one would not be recolorable.
2052 LLVM_DEBUG(dbgs() << "Early abort: too many interferences.\n");
2053 CutOffInfo |= CO_Interf;
2054 return false;
2055 }
2056 for (const LiveInterval *Intf : reverse(Q.interferingVRegs())) {
2057 // If Intf is done and sits on the same register class as VirtReg, it
2058 // would not be recolorable as it is in the same state as
2059 // VirtReg. However there are at least two exceptions.
2060 //
2061 // If VirtReg has tied defs and Intf doesn't, then
2062 // there is still a point in examining if it can be recolorable.
2063 //
2064 // Additionally, if the register class has overlapping tuple members, it
2065 // may still be recolorable using a different tuple. This is more likely
2066 // if the existing assignment aliases with the candidate.
2067 //
2068 if (((ExtraInfo->getStage(*Intf) == RS_Done &&
2069 MRI->getRegClass(Intf->reg()) == CurRC &&
2070 !assignedRegPartiallyOverlaps(*TRI, *VRM, PhysReg, *Intf)) &&
2071 !(hasTiedDef(MRI, VirtReg.reg()) &&
2072 !hasTiedDef(MRI, Intf->reg()))) ||
2073 FixedRegisters.count(Intf->reg())) {
2074 LLVM_DEBUG(
2075 dbgs() << "Early abort: the interference is not recolorable.\n");
2076 return false;
2077 }
2078 RecoloringCandidates.insert(Intf);
2079 }
2080 }
2081 return true;
2082}
2083
2084/// tryLastChanceRecoloring - Try to assign a color to \p VirtReg by recoloring
2085/// its interferences.
2086/// Last chance recoloring chooses a color for \p VirtReg and recolors every
2087/// virtual register that was using it. The recoloring process may recursively
2088/// use the last chance recoloring. Therefore, when a virtual register has been
2089/// assigned a color by this mechanism, it is marked as Fixed, i.e., it cannot
2090/// be last-chance-recolored again during this recoloring "session".
2091/// E.g.,
2092/// Let
2093/// vA can use {R1, R2 }
2094/// vB can use { R2, R3}
2095/// vC can use {R1 }
2096/// Where vA, vB, and vC cannot be split anymore (they are reloads for
2097/// instance) and they all interfere.
2098///
2099/// vA is assigned R1
2100/// vB is assigned R2
2101/// vC tries to evict vA but vA is already done.
2102/// Regular register allocation fails.
2103///
2104/// Last chance recoloring kicks in:
2105/// vC does as if vA was evicted => vC uses R1.
2106/// vC is marked as fixed.
2107/// vA needs to find a color.
2108/// None are available.
2109/// vA cannot evict vC: vC is a fixed virtual register now.
2110/// vA does as if vB was evicted => vA uses R2.
2111/// vB needs to find a color.
2112/// R3 is available.
2113/// Recoloring => vC = R1, vA = R2, vB = R3
2114///
2115/// \p Order defines the preferred allocation order for \p VirtReg.
2116/// \p NewRegs will contain any new virtual register that have been created
2117/// (split, spill) during the process and that must be assigned.
2118/// \p FixedRegisters contains all the virtual registers that cannot be
2119/// recolored.
2120///
2121/// \p RecolorStack tracks the original assignments of successfully recolored
2122/// registers.
2123///
2124/// \p Depth gives the current depth of the last chance recoloring.
2125/// \return a physical register that can be used for VirtReg or ~0u if none
2126/// exists.
2127MCRegister RAGreedy::tryLastChanceRecoloring(
2128 const LiveInterval &VirtReg, AllocationOrder &Order,
2129 SmallVectorImpl<Register> &NewVRegs, SmallVirtRegSet &FixedRegisters,
2130 RecoloringStack &RecolorStack, unsigned Depth) {
2131 if (!TRI->shouldUseLastChanceRecoloringForVirtReg(*MF, VirtReg))
2132 return ~0u;
2133
2134 LLVM_DEBUG(dbgs() << "Try last chance recoloring for " << VirtReg << '\n');
2135
2136 const ssize_t EntryStackSize = RecolorStack.size();
2137
2138 // Ranges must be Done.
2139 assert((ExtraInfo->getStage(VirtReg) >= RS_Done || !VirtReg.isSpillable()) &&
2140 "Last chance recoloring should really be last chance");
2141 // Set the max depth to LastChanceRecoloringMaxDepth.
2142 // We may want to reconsider that if we end up with a too large search space
2143 // for target with hundreds of registers.
2144 // Indeed, in that case we may want to cut the search space earlier.
2146 LLVM_DEBUG(dbgs() << "Abort because max depth has been reached.\n");
2147 CutOffInfo |= CO_Depth;
2148 return ~0u;
2149 }
2150
2151 // Set of Live intervals that will need to be recolored.
2152 SmallLISet RecoloringCandidates;
2153
2154 // Mark VirtReg as fixed, i.e., it will not be recolored pass this point in
2155 // this recoloring "session".
2156 assert(!FixedRegisters.count(VirtReg.reg()));
2157 FixedRegisters.insert(VirtReg.reg());
2158 SmallVector<Register, 4> CurrentNewVRegs;
2159
2160 for (MCRegister PhysReg : Order) {
2161 assert(PhysReg.isValid());
2162 LLVM_DEBUG(dbgs() << "Try to assign: " << VirtReg << " to "
2163 << printReg(PhysReg, TRI) << '\n');
2164 RecoloringCandidates.clear();
2165 CurrentNewVRegs.clear();
2166
2167 // It is only possible to recolor virtual register interference.
2168 if (Matrix->checkInterference(VirtReg, PhysReg) >
2170 LLVM_DEBUG(
2171 dbgs() << "Some interferences are not with virtual registers.\n");
2172
2173 continue;
2174 }
2175
2176 // Early give up on this PhysReg if it is obvious we cannot recolor all
2177 // the interferences.
2178 if (!mayRecolorAllInterferences(PhysReg, VirtReg, RecoloringCandidates,
2179 FixedRegisters)) {
2180 LLVM_DEBUG(dbgs() << "Some interferences cannot be recolored.\n");
2181 continue;
2182 }
2183
2184 // RecoloringCandidates contains all the virtual registers that interfere
2185 // with VirtReg on PhysReg (or one of its aliases). Enqueue them for
2186 // recoloring and perform the actual recoloring.
2187 PQueue RecoloringQueue;
2188 for (const LiveInterval *RC : RecoloringCandidates) {
2189 Register ItVirtReg = RC->reg();
2190 enqueue(RecoloringQueue, RC);
2191 assert(VRM->hasPhys(ItVirtReg) &&
2192 "Interferences are supposed to be with allocated variables");
2193
2194 // Record the current allocation.
2195 RecolorStack.push_back(std::make_pair(RC, VRM->getPhys(ItVirtReg)));
2196
2197 // unset the related struct.
2198 Matrix->unassign(*RC);
2199 }
2200
2201 // Do as if VirtReg was assigned to PhysReg so that the underlying
2202 // recoloring has the right information about the interferes and
2203 // available colors.
2204 Matrix->assign(VirtReg, PhysReg);
2205
2206 // VirtReg may be deleted during tryRecoloringCandidates, save a copy.
2207 Register ThisVirtReg = VirtReg.reg();
2208
2209 // Save the current recoloring state.
2210 // If we cannot recolor all the interferences, we will have to start again
2211 // at this point for the next physical register.
2212 SmallVirtRegSet SaveFixedRegisters(FixedRegisters);
2213 if (tryRecoloringCandidates(RecoloringQueue, CurrentNewVRegs,
2214 FixedRegisters, RecolorStack, Depth)) {
2215 // Push the queued vregs into the main queue.
2216 llvm::append_range(NewVRegs, CurrentNewVRegs);
2217 // Do not mess up with the global assignment process.
2218 // I.e., VirtReg must be unassigned.
2219 if (VRM->hasPhys(ThisVirtReg)) {
2220 Matrix->unassign(VirtReg);
2221 return PhysReg;
2222 }
2223
2224 // It is possible VirtReg will be deleted during tryRecoloringCandidates.
2225 LLVM_DEBUG(dbgs() << "tryRecoloringCandidates deleted a fixed register "
2226 << printReg(ThisVirtReg) << '\n');
2227 FixedRegisters.erase(ThisVirtReg);
2228 return MCRegister();
2229 }
2230
2231 LLVM_DEBUG(dbgs() << "Fail to assign: " << VirtReg << " to "
2232 << printReg(PhysReg, TRI) << '\n');
2233
2234 // The recoloring attempt failed, undo the changes.
2235 FixedRegisters = SaveFixedRegisters;
2236 Matrix->unassign(VirtReg);
2237
2238 // For a newly created vreg which is also in RecoloringCandidates,
2239 // don't add it to NewVRegs because its physical register will be restored
2240 // below. Other vregs in CurrentNewVRegs are created by calling
2241 // selectOrSplit and should be added into NewVRegs.
2242 for (Register R : CurrentNewVRegs) {
2243 if (RecoloringCandidates.count(&LIS->getInterval(R)))
2244 continue;
2245 NewVRegs.push_back(R);
2246 }
2247
2248 // Roll back our unsuccessful recoloring. Also roll back any successful
2249 // recolorings in any recursive recoloring attempts, since it's possible
2250 // they would have introduced conflicts with assignments we will be
2251 // restoring further up the stack. Perform all unassignments prior to
2252 // reassigning, since sub-recolorings may have conflicted with the registers
2253 // we are going to restore to their original assignments.
2254 for (ssize_t I = RecolorStack.size() - 1; I >= EntryStackSize; --I) {
2255 const LiveInterval *LI;
2256 MCRegister PhysReg;
2257 std::tie(LI, PhysReg) = RecolorStack[I];
2258
2259 if (VRM->hasPhys(LI->reg()))
2260 Matrix->unassign(*LI);
2261 }
2262
2263 for (size_t I = EntryStackSize; I != RecolorStack.size(); ++I) {
2264 const LiveInterval *LI;
2265 MCRegister PhysReg;
2266 std::tie(LI, PhysReg) = RecolorStack[I];
2267 if (!LI->empty() && !MRI->reg_nodbg_empty(LI->reg()))
2268 Matrix->assign(*LI, PhysReg);
2269 }
2270
2271 // Pop the stack of recoloring attempts.
2272 RecolorStack.resize(EntryStackSize);
2273 }
2274
2275 // Last chance recoloring did not worked either, give up.
2276 return ~0u;
2277}
2278
2279/// tryRecoloringCandidates - Try to assign a new color to every register
2280/// in \RecoloringQueue.
2281/// \p NewRegs will contain any new virtual register created during the
2282/// recoloring process.
2283/// \p FixedRegisters[in/out] contains all the registers that have been
2284/// recolored.
2285/// \return true if all virtual registers in RecoloringQueue were successfully
2286/// recolored, false otherwise.
2287bool RAGreedy::tryRecoloringCandidates(PQueue &RecoloringQueue,
2288 SmallVectorImpl<Register> &NewVRegs,
2289 SmallVirtRegSet &FixedRegisters,
2290 RecoloringStack &RecolorStack,
2291 unsigned Depth) {
2292 while (!RecoloringQueue.empty()) {
2293 const LiveInterval *LI = dequeue(RecoloringQueue);
2294 LLVM_DEBUG(dbgs() << "Try to recolor: " << *LI << '\n');
2295 MCRegister PhysReg = selectOrSplitImpl(*LI, NewVRegs, FixedRegisters,
2296 RecolorStack, Depth + 1);
2297 // When splitting happens, the live-range may actually be empty.
2298 // In that case, this is okay to continue the recoloring even
2299 // if we did not find an alternative color for it. Indeed,
2300 // there will not be anything to color for LI in the end.
2301 if (PhysReg == ~0u || (!PhysReg && !LI->empty()))
2302 return false;
2303
2304 if (!PhysReg) {
2305 assert(LI->empty() && "Only empty live-range do not require a register");
2306 LLVM_DEBUG(dbgs() << "Recoloring of " << *LI
2307 << " succeeded. Empty LI.\n");
2308 continue;
2309 }
2310 LLVM_DEBUG(dbgs() << "Recoloring of " << *LI
2311 << " succeeded with: " << printReg(PhysReg, TRI) << '\n');
2312
2313 Matrix->assign(*LI, PhysReg);
2314 FixedRegisters.insert(LI->reg());
2315 }
2316 return true;
2317}
2318
2319//===----------------------------------------------------------------------===//
2320// Main Entry Point
2321//===----------------------------------------------------------------------===//
2322
2324 SmallVectorImpl<Register> &NewVRegs) {
2325 CutOffInfo = CO_None;
2326 LLVMContext &Ctx = MF->getFunction().getContext();
2327 SmallVirtRegSet FixedRegisters;
2328 RecoloringStack RecolorStack;
2329 MCRegister Reg =
2330 selectOrSplitImpl(VirtReg, NewVRegs, FixedRegisters, RecolorStack);
2331 if (Reg == ~0U && (CutOffInfo != CO_None)) {
2332 uint8_t CutOffEncountered = CutOffInfo & (CO_Depth | CO_Interf);
2333 if (CutOffEncountered == CO_Depth)
2334 Ctx.emitError("register allocation failed: maximum depth for recoloring "
2335 "reached. Use -fexhaustive-register-search to skip "
2336 "cutoffs");
2337 else if (CutOffEncountered == CO_Interf)
2338 Ctx.emitError("register allocation failed: maximum interference for "
2339 "recoloring reached. Use -fexhaustive-register-search "
2340 "to skip cutoffs");
2341 else if (CutOffEncountered == (CO_Depth | CO_Interf))
2342 Ctx.emitError("register allocation failed: maximum interference and "
2343 "depth for recoloring reached. Use "
2344 "-fexhaustive-register-search to skip cutoffs");
2345 }
2346 return Reg;
2347}
2348
2349/// calcSpillCost - Compute how expensive it would be to spill the live range in
2350/// LI into memory.
2351BlockFrequency RAGreedy::calcSpillCost(const LiveInterval &LI) {
2352 uint64_t SpillCost = 0;
2354
2356 I = MRI->reg_instr_nodbg_begin(LI.reg()),
2357 E = MRI->reg_instr_nodbg_end();
2358 I != E;) {
2359 MachineInstr *MI = &*(I++);
2360 if (MI->isMetaInstruction())
2361 continue;
2362 if (!Visited.insert(MI).second)
2363 continue;
2364
2365 auto [Reads, Writes] = MI->readsWritesVirtualRegister(LI.reg());
2366 auto MBBFreq = SpillPlacer->getBlockFrequency(MI->getParent()->getNumber());
2367 SpillCost += (Reads + Writes) * MBBFreq.getFrequency();
2368 }
2369
2370 return BlockFrequency(SpillCost);
2371}
2372
2373/// Using a CSR for the first time has a cost because it causes push|pop
2374/// to be added to prologue|epilogue. Splitting a cold section of the live
2375/// range can have lower cost than using the CSR for the first time;
2376/// Spilling a live range in the cold path can have lower cost than using
2377/// the CSR for the first time. Returns the physical register if we decide
2378/// to use the CSR; otherwise return MCRegister().
2379MCRegister RAGreedy::tryAssignCSRFirstTime(
2380 const LiveInterval &VirtReg, AllocationOrder &Order, MCRegister PhysReg,
2381 uint8_t &CostPerUseLimit, SmallVectorImpl<Register> &NewVRegs) {
2382 if (ExtraInfo->getStage(VirtReg) == RS_Spill && VirtReg.isSpillable()) {
2383 // We choose spill over using the CSR for the first time if the spill cost
2384 // is lower than CSRCost.
2385 SA->analyze(&VirtReg);
2386 if (calcSpillCost(VirtReg) >= CSRCost)
2387 return PhysReg;
2388
2389 // We are going to spill, set CostPerUseLimit to 1 to make sure that
2390 // we will not use a callee-saved register in tryEvict.
2391 CostPerUseLimit = 1;
2392 return MCRegister();
2393 }
2394 if (ExtraInfo->getStage(VirtReg) < RS_Split) {
2395 // We choose pre-splitting over using the CSR for the first time if
2396 // the cost of splitting is lower than CSRCost.
2397 SA->analyze(&VirtReg);
2398 unsigned NumCands = 0;
2399 BlockFrequency BestCost = CSRCost; // Don't modify CSRCost.
2400 unsigned BestCand = calculateRegionSplitCost(VirtReg, Order, BestCost,
2401 NumCands, true /*IgnoreCSR*/);
2402 if (BestCand == NoCand)
2403 // Use the CSR if we can't find a region split below CSRCost.
2404 return PhysReg;
2405
2406 // Perform the actual pre-splitting.
2407 doRegionSplit(VirtReg, BestCand, false/*HasCompact*/, NewVRegs);
2408 return MCRegister();
2409 }
2410 return PhysReg;
2411}
2412
2414 // Do not keep invalid information around.
2415 SetOfBrokenHints.remove(&LI);
2416}
2417
2418void RAGreedy::initializeCSRCost() {
2419 if (!CSRCostScale.getNumOccurrences() &&
2420 (CSRFirstTimeCost.getNumOccurrences() || TRI->getCSRCost())) {
2421 // We should deprecate the usage of CSRFirstTimeCost!
2422 // We use the command-line option if it is explicitly set, otherwise use the
2423 // larger one out of the command-line option and the value reported by TRI.
2424 CSRCost = BlockFrequency(
2425 CSRFirstTimeCost.getNumOccurrences()
2427 : std::max((unsigned)CSRFirstTimeCost, TRI->getCSRCost()));
2428 if (!CSRCost.getFrequency())
2429 return;
2430
2431 // Raw cost is relative to Entry == 2^14; scale it appropriately.
2432 uint64_t ActualEntry = MBFI->getEntryFreq().getFrequency();
2433 if (!ActualEntry) {
2434 CSRCost = BlockFrequency(0);
2435 return;
2436 }
2437 uint64_t FixedEntry = 1 << 14;
2438 if (ActualEntry < FixedEntry) {
2439 CSRCost *= BranchProbability(ActualEntry, FixedEntry);
2440 } else if (ActualEntry <= UINT32_MAX) {
2441 // Invert the fraction and divide.
2442 CSRCost /= BranchProbability(FixedEntry, ActualEntry);
2443 } else {
2444 // Can't use BranchProbability in general, since it takes 32-bit numbers.
2445 CSRCost =
2446 BlockFrequency(CSRCost.getFrequency() * (ActualEntry / FixedEntry));
2447 }
2448 } else {
2449 uint64_t EntryFreq = MBFI->getEntryFreq().getFrequency();
2450 CSRCost = BlockFrequency(TRI->getCSRFirstUseCost() * EntryFreq);
2451 if (CSRCostScale < 100)
2452 CSRCost *= BranchProbability(CSRCostScale, 100);
2453 else
2454 CSRCost /= BranchProbability(100, CSRCostScale);
2455 }
2456}
2457
2458/// Collect the hint info for \p Reg.
2459/// The results are stored into \p Out.
2460/// \p Out is not cleared before being populated.
2461void RAGreedy::collectHintInfo(Register Reg, HintsInfo &Out) {
2462 const TargetRegisterClass *RC = MRI->getRegClass(Reg);
2463
2464 for (const MachineOperand &Opnd : MRI->reg_nodbg_operands(Reg)) {
2465 const MachineInstr &Instr = *Opnd.getParent();
2466 if (!Instr.isCopy() || Opnd.isImplicit())
2467 continue;
2468
2469 // Look for the other end of the copy.
2470 const MachineOperand &OtherOpnd = Instr.getOperand(Opnd.isDef());
2471 Register OtherReg = OtherOpnd.getReg();
2472 if (OtherReg == Reg)
2473 continue;
2474 unsigned OtherSubReg = OtherOpnd.getSubReg();
2475 unsigned SubReg = Opnd.getSubReg();
2476
2477 // Get the current assignment.
2478 MCRegister OtherPhysReg;
2479 if (OtherReg.isPhysical()) {
2480 if (OtherSubReg)
2481 OtherPhysReg = TRI->getMatchingSuperReg(OtherReg, OtherSubReg, RC);
2482 else if (SubReg)
2483 OtherPhysReg = TRI->getMatchingSuperReg(OtherReg, SubReg, RC);
2484 else
2485 OtherPhysReg = OtherReg;
2486 } else {
2487 OtherPhysReg = VRM->getPhys(OtherReg);
2488 // TODO: Should find matching superregister, but applying this in the
2489 // non-hint case currently causes regressions
2490
2491 if (SubReg && OtherSubReg && SubReg != OtherSubReg)
2492 continue;
2493 }
2494
2495 // Push the collected information.
2496 if (OtherPhysReg) {
2497 Out.push_back(HintInfo(MBFI->getBlockFreq(Instr.getParent()), OtherReg,
2498 OtherPhysReg));
2499 }
2500 }
2501}
2502
2503/// Using the given \p List, compute the cost of the broken hints if
2504/// \p PhysReg was used.
2505/// \return The cost of \p List for \p PhysReg.
2506BlockFrequency RAGreedy::getBrokenHintFreq(const HintsInfo &List,
2507 MCRegister PhysReg) {
2508 BlockFrequency Cost = BlockFrequency(0);
2509 for (const HintInfo &Info : List) {
2510 if (Info.PhysReg != PhysReg)
2511 Cost += Info.Freq;
2512 }
2513 return Cost;
2514}
2515
2516/// Using the register assigned to \p VirtReg, try to recolor
2517/// all the live ranges that are copy-related with \p VirtReg.
2518/// The recoloring is then propagated to all the live-ranges that have
2519/// been recolored and so on, until no more copies can be coalesced or
2520/// it is not profitable.
2521/// For a given live range, profitability is determined by the sum of the
2522/// frequencies of the non-identity copies it would introduce with the old
2523/// and new register.
2524void RAGreedy::tryHintRecoloring(const LiveInterval &VirtReg) {
2525 // We have a broken hint, check if it is possible to fix it by
2526 // reusing PhysReg for the copy-related live-ranges. Indeed, we evicted
2527 // some register and PhysReg may be available for the other live-ranges.
2528 HintsInfo Info;
2529 Register Reg = VirtReg.reg();
2530 MCRegister PhysReg = VRM->getPhys(Reg);
2531 // Start the recoloring algorithm from the input live-interval, then
2532 // it will propagate to the ones that are copy-related with it.
2533 SmallSet<Register, 4> Visited = {Reg};
2534 SmallVector<Register, 2> RecoloringCandidates = {Reg};
2535
2536 LLVM_DEBUG(dbgs() << "Trying to reconcile hints for: " << printReg(Reg, TRI)
2537 << '(' << printReg(PhysReg, TRI) << ")\n");
2538
2539 do {
2540 Reg = RecoloringCandidates.pop_back_val();
2541
2542 MCRegister CurrPhys = VRM->getPhys(Reg);
2543
2544 // This may be a skipped register.
2545 if (!CurrPhys) {
2547 "We have an unallocated variable which should have been handled");
2548 continue;
2549 }
2550
2551 // Get the live interval mapped with this virtual register to be able
2552 // to check for the interference with the new color.
2553 LiveInterval &LI = LIS->getInterval(Reg);
2554 // Check that the new color matches the register class constraints and
2555 // that it is free for this live range.
2556 if (CurrPhys != PhysReg && (!MRI->getRegClass(Reg)->contains(PhysReg) ||
2557 Matrix->checkInterference(LI, PhysReg)))
2558 continue;
2559
2560 LLVM_DEBUG(dbgs() << printReg(Reg, TRI) << '(' << printReg(CurrPhys, TRI)
2561 << ") is recolorable.\n");
2562
2563 // Gather the hint info.
2564 Info.clear();
2565 collectHintInfo(Reg, Info);
2566 // Check if recoloring the live-range will increase the cost of the
2567 // non-identity copies.
2568 if (CurrPhys != PhysReg) {
2569 LLVM_DEBUG(dbgs() << "Checking profitability:\n");
2570 BlockFrequency OldCopiesCost = getBrokenHintFreq(Info, CurrPhys);
2571 BlockFrequency NewCopiesCost = getBrokenHintFreq(Info, PhysReg);
2572 LLVM_DEBUG(dbgs() << "Old Cost: " << printBlockFreq(*MBFI, OldCopiesCost)
2573 << "\nNew Cost: "
2574 << printBlockFreq(*MBFI, NewCopiesCost) << '\n');
2575 if (OldCopiesCost < NewCopiesCost) {
2576 LLVM_DEBUG(dbgs() << "=> Not profitable.\n");
2577 continue;
2578 }
2579 // At this point, the cost is either cheaper or equal. If it is
2580 // equal, we consider this is profitable because it may expose
2581 // more recoloring opportunities.
2582 LLVM_DEBUG(dbgs() << "=> Profitable.\n");
2583 // Recolor the live-range.
2584 Matrix->unassign(LI);
2585 Matrix->assign(LI, PhysReg);
2586 }
2587 // Push all copy-related live-ranges to keep reconciling the broken
2588 // hints.
2589 for (const HintInfo &HI : Info) {
2590 // We cannot recolor physical register.
2591 if (HI.Reg.isVirtual() && Visited.insert(HI.Reg).second)
2592 RecoloringCandidates.push_back(HI.Reg);
2593 }
2594 } while (!RecoloringCandidates.empty());
2595}
2596
2597/// Try to recolor broken hints.
2598/// Broken hints may be repaired by recoloring when an evicted variable
2599/// freed up a register for a larger live-range.
2600/// Consider the following example:
2601/// BB1:
2602/// a =
2603/// b =
2604/// BB2:
2605/// ...
2606/// = b
2607/// = a
2608/// Let us assume b gets split:
2609/// BB1:
2610/// a =
2611/// b =
2612/// BB2:
2613/// c = b
2614/// ...
2615/// d = c
2616/// = d
2617/// = a
2618/// Because of how the allocation work, b, c, and d may be assigned different
2619/// colors. Now, if a gets evicted later:
2620/// BB1:
2621/// a =
2622/// st a, SpillSlot
2623/// b =
2624/// BB2:
2625/// c = b
2626/// ...
2627/// d = c
2628/// = d
2629/// e = ld SpillSlot
2630/// = e
2631/// This is likely that we can assign the same register for b, c, and d,
2632/// getting rid of 2 copies.
2633void RAGreedy::tryHintsRecoloring() {
2634 for (const LiveInterval *LI : SetOfBrokenHints) {
2635 assert(LI->reg().isVirtual() &&
2636 "Recoloring is possible only for virtual registers");
2637 // Some dead defs may be around (e.g., because of debug uses).
2638 // Ignore those.
2639 if (!VRM->hasPhys(LI->reg()))
2640 continue;
2641 tryHintRecoloring(*LI);
2642 }
2643}
2644
2645MCRegister RAGreedy::selectOrSplitImpl(const LiveInterval &VirtReg,
2646 SmallVectorImpl<Register> &NewVRegs,
2647 SmallVirtRegSet &FixedRegisters,
2648 RecoloringStack &RecolorStack,
2649 unsigned Depth) {
2650 uint8_t CostPerUseLimit = uint8_t(~0u);
2651 // First try assigning a free register.
2652 auto Order =
2654 if (MCRegister PhysReg =
2655 tryAssign(VirtReg, Order, NewVRegs, FixedRegisters)) {
2656 // When NewVRegs is not empty, we may have made decisions such as evicting
2657 // a virtual register, go with the earlier decisions and use the physical
2658 // register.
2659 if (CSRCost.getFrequency() &&
2660 EvictAdvisor->isUnusedCalleeSavedReg(PhysReg) && NewVRegs.empty()) {
2661 MCRegister CSRReg = tryAssignCSRFirstTime(VirtReg, Order, PhysReg,
2662 CostPerUseLimit, NewVRegs);
2663 if (CSRReg || !NewVRegs.empty())
2664 // Return now if we decide to use a CSR or create new vregs due to
2665 // pre-splitting.
2666 return CSRReg;
2667 } else
2668 return PhysReg;
2669 }
2670 // Non empty NewVRegs means VirtReg has been split.
2671 if (!NewVRegs.empty())
2672 return MCRegister();
2673
2674 LiveRangeStage Stage = ExtraInfo->getStage(VirtReg);
2675 LLVM_DEBUG(dbgs() << StageName[Stage] << " Cascade "
2676 << ExtraInfo->getCascade(VirtReg.reg()) << '\n');
2677
2678 // Try to evict a less worthy live range, but only for ranges from the primary
2679 // queue. The RS_Split ranges already failed to do this, and they should not
2680 // get a second chance until they have been split.
2681 if (Stage != RS_Split) {
2682 if (MCRegister PhysReg =
2683 tryEvict(VirtReg, Order, NewVRegs, CostPerUseLimit,
2684 FixedRegisters)) {
2685 Register Hint = MRI->getSimpleHint(VirtReg.reg());
2686 // If VirtReg has a hint and that hint is broken record this
2687 // virtual register as a recoloring candidate for broken hint.
2688 // Indeed, since we evicted a variable in its neighborhood it is
2689 // likely we can at least partially recolor some of the
2690 // copy-related live-ranges.
2691 if (Hint && Hint != PhysReg)
2692 SetOfBrokenHints.insert(&VirtReg);
2693 return PhysReg;
2694 }
2695 }
2696
2697 assert((NewVRegs.empty() || Depth) && "Cannot append to existing NewVRegs");
2698
2699 // The first time we see a live range, don't try to split or spill.
2700 // Wait until the second time, when all smaller ranges have been allocated.
2701 // This gives a better picture of the interference to split around.
2702 if (Stage < RS_Split) {
2703 ExtraInfo->setStage(VirtReg, RS_Split);
2704 LLVM_DEBUG(dbgs() << "wait for second round\n");
2705 NewVRegs.push_back(VirtReg.reg());
2706 return MCRegister();
2707 }
2708
2709 if (Stage < RS_Spill && !VirtReg.empty()) {
2710 // Try splitting VirtReg or interferences.
2711 unsigned NewVRegSizeBefore = NewVRegs.size();
2712 MCRegister PhysReg = trySplit(VirtReg, Order, NewVRegs, FixedRegisters);
2713 if (PhysReg || (NewVRegs.size() - NewVRegSizeBefore))
2714 return PhysReg;
2715 }
2716
2717 // If we couldn't allocate a register from spilling, there is probably some
2718 // invalid inline assembly. The base class will report it.
2719 if (Stage >= RS_Done || !VirtReg.isSpillable()) {
2720 return tryLastChanceRecoloring(VirtReg, Order, NewVRegs, FixedRegisters,
2721 RecolorStack, Depth);
2722 }
2723
2724 // Finally spill VirtReg itself.
2725 NamedRegionTimer T("spill", "Spiller", TimerGroupName,
2727 LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this, &DeadRemats);
2728 spiller().spill(LRE, &Order);
2729 ExtraInfo->setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done);
2730
2731 // Tell LiveDebugVariables about the new ranges. Ranges not being covered by
2732 // the new regs are kept in LDV (still mapping to the old register), until
2733 // we rewrite spilled locations in LDV at a later stage.
2734 for (Register r : spiller().getSpilledRegs())
2735 DebugVars->splitRegister(r, LRE.regs(), *LIS);
2736 for (Register r : spiller().getReplacedRegs())
2737 DebugVars->splitRegister(r, LRE.regs(), *LIS);
2738
2739 if (VerifyEnabled)
2740 MF->verify(LIS, Indexes, "After spilling", &errs());
2741
2742 // The live virtual register requesting allocation was spilled, so tell
2743 // the caller not to allocate anything during this round.
2744 return MCRegister();
2745}
2746
2747void RAGreedy::RAGreedyStats::report(MachineOptimizationRemarkMissed &R) {
2748 using namespace ore;
2749 if (Spills) {
2750 R << NV("NumSpills", Spills) << " spills ";
2751 R << NV("TotalSpillsCost", SpillsCost) << " total spills cost ";
2752 }
2753 if (FoldedSpills) {
2754 R << NV("NumFoldedSpills", FoldedSpills) << " folded spills ";
2755 R << NV("TotalFoldedSpillsCost", FoldedSpillsCost)
2756 << " total folded spills cost ";
2757 }
2758 if (Reloads) {
2759 R << NV("NumReloads", Reloads) << " reloads ";
2760 R << NV("TotalReloadsCost", ReloadsCost) << " total reloads cost ";
2761 }
2762 if (FoldedReloads) {
2763 R << NV("NumFoldedReloads", FoldedReloads) << " folded reloads ";
2764 R << NV("TotalFoldedReloadsCost", FoldedReloadsCost)
2765 << " total folded reloads cost ";
2766 }
2767 if (ZeroCostFoldedReloads)
2768 R << NV("NumZeroCostFoldedReloads", ZeroCostFoldedReloads)
2769 << " zero cost folded reloads ";
2770 if (Copies) {
2771 R << NV("NumVRCopies", Copies) << " virtual registers copies ";
2772 R << NV("TotalCopiesCost", CopiesCost) << " total copies cost ";
2773 }
2774}
2775
2776RAGreedy::RAGreedyStats RAGreedy::computeStats(MachineBasicBlock &MBB) {
2777 RAGreedyStats Stats;
2778 const MachineFrameInfo &MFI = MF->getFrameInfo();
2779 int FI;
2780
2781 auto isSpillSlotAccess = [&MFI](const MachineMemOperand *A) {
2783 A->getPseudoValue())->getFrameIndex());
2784 };
2785 auto isPatchpointInstr = [](const MachineInstr &MI) {
2786 return MI.getOpcode() == TargetOpcode::PATCHPOINT ||
2787 MI.getOpcode() == TargetOpcode::STACKMAP ||
2788 MI.getOpcode() == TargetOpcode::STATEPOINT;
2789 };
2790 for (MachineInstr &MI : MBB) {
2791 auto DestSrc = TII->isCopyInstr(MI);
2792 if (DestSrc) {
2793 const MachineOperand &Dest = *DestSrc->Destination;
2794 const MachineOperand &Src = *DestSrc->Source;
2795 Register SrcReg = Src.getReg();
2796 Register DestReg = Dest.getReg();
2797 // Only count `COPY`s with a virtual register as source or destination.
2798 if (SrcReg.isVirtual() || DestReg.isVirtual()) {
2799 if (SrcReg.isVirtual()) {
2800 SrcReg = VRM->getPhys(SrcReg);
2801 if (SrcReg && Src.getSubReg())
2802 SrcReg = TRI->getSubReg(SrcReg, Src.getSubReg());
2803 }
2804 if (DestReg.isVirtual()) {
2805 DestReg = VRM->getPhys(DestReg);
2806 if (DestReg && Dest.getSubReg())
2807 DestReg = TRI->getSubReg(DestReg, Dest.getSubReg());
2808 }
2809 if (SrcReg != DestReg)
2810 ++Stats.Copies;
2811 }
2812 continue;
2813 }
2814
2815 SmallVector<const MachineMemOperand *, 2> Accesses;
2816 if (TII->isLoadFromStackSlot(MI, FI) && MFI.isSpillSlotObjectIndex(FI)) {
2817 ++Stats.Reloads;
2818 continue;
2819 }
2820 if (TII->isStoreToStackSlot(MI, FI) && MFI.isSpillSlotObjectIndex(FI)) {
2821 ++Stats.Spills;
2822 continue;
2823 }
2824 if (TII->hasLoadFromStackSlot(MI, Accesses) &&
2825 llvm::any_of(Accesses, isSpillSlotAccess)) {
2826 if (!isPatchpointInstr(MI)) {
2827 Stats.FoldedReloads += Accesses.size();
2828 continue;
2829 }
2830 // For statepoint there may be folded and zero cost folded stack reloads.
2831 std::pair<unsigned, unsigned> NonZeroCostRange =
2832 TII->getPatchpointUnfoldableRange(MI);
2833 SmallSet<unsigned, 16> FoldedReloads;
2834 SmallSet<unsigned, 16> ZeroCostFoldedReloads;
2835 for (unsigned Idx = 0, E = MI.getNumOperands(); Idx < E; ++Idx) {
2836 MachineOperand &MO = MI.getOperand(Idx);
2837 if (!MO.isFI() || !MFI.isSpillSlotObjectIndex(MO.getIndex()))
2838 continue;
2839 if (Idx >= NonZeroCostRange.first && Idx < NonZeroCostRange.second)
2840 FoldedReloads.insert(MO.getIndex());
2841 else
2842 ZeroCostFoldedReloads.insert(MO.getIndex());
2843 }
2844 // If stack slot is used in folded reload it is not zero cost then.
2845 for (unsigned Slot : FoldedReloads)
2846 ZeroCostFoldedReloads.erase(Slot);
2847 Stats.FoldedReloads += FoldedReloads.size();
2848 Stats.ZeroCostFoldedReloads += ZeroCostFoldedReloads.size();
2849 continue;
2850 }
2851 Accesses.clear();
2852 if (TII->hasStoreToStackSlot(MI, Accesses) &&
2853 llvm::any_of(Accesses, isSpillSlotAccess)) {
2854 Stats.FoldedSpills += Accesses.size();
2855 }
2856 }
2857 // Set cost of collected statistic by multiplication to relative frequency of
2858 // this basic block.
2859 float RelFreq = MBFI->getBlockFreqRelativeToEntryBlock(&MBB);
2860 Stats.ReloadsCost = RelFreq * Stats.Reloads;
2861 Stats.FoldedReloadsCost = RelFreq * Stats.FoldedReloads;
2862 Stats.SpillsCost = RelFreq * Stats.Spills;
2863 Stats.FoldedSpillsCost = RelFreq * Stats.FoldedSpills;
2864 Stats.CopiesCost = RelFreq * Stats.Copies;
2865 return Stats;
2866}
2867
2868RAGreedy::RAGreedyStats RAGreedy::reportStats(MachineLoop *L) {
2869 RAGreedyStats Stats;
2870
2871 // Sum up the spill and reloads in subloops.
2872 for (MachineLoop *SubLoop : *L)
2873 Stats.add(reportStats(SubLoop));
2874
2875 for (MachineBasicBlock *MBB : L->getBlocks())
2876 // Handle blocks that were not included in subloops.
2877 if (Loops->getLoopFor(MBB) == L)
2878 Stats.add(computeStats(*MBB));
2879
2880 if (!Stats.isEmpty()) {
2881 using namespace ore;
2882
2883 ORE->emit([&]() {
2884 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "LoopSpillReloadCopies",
2885 L->getStartLoc(), L->getHeader());
2886 Stats.report(R);
2887 R << "generated in loop";
2888 return R;
2889 });
2890 }
2891 return Stats;
2892}
2893
2894void RAGreedy::reportStats() {
2895 if (!ORE->allowExtraAnalysis(DEBUG_TYPE))
2896 return;
2897 RAGreedyStats Stats;
2898 for (MachineLoop *L : *Loops)
2899 Stats.add(reportStats(L));
2900 // Process non-loop blocks.
2901 for (MachineBasicBlock &MBB : *MF)
2902 if (!Loops->getLoopFor(&MBB))
2903 Stats.add(computeStats(MBB));
2904 if (!Stats.isEmpty()) {
2905 using namespace ore;
2906
2907 ORE->emit([&]() {
2908 DebugLoc Loc;
2909 if (auto *SP = MF->getFunction().getSubprogram())
2910 Loc = DILocation::get(SP->getContext(), SP->getLine(), 1, SP);
2911 MachineOptimizationRemarkMissed R(DEBUG_TYPE, "SpillReloadCopies", Loc,
2912 &MF->front());
2913 Stats.report(R);
2914 R << "generated in function";
2915 return R;
2916 });
2917 }
2918}
2919
2920bool RAGreedy::hasVirtRegAlloc() {
2921 for (unsigned I = 0, E = MRI->getNumVirtRegs(); I != E; ++I) {
2923 if (MRI->reg_nodbg_empty(Reg))
2924 continue;
2926 return true;
2927 }
2928
2929 return false;
2930}
2931
2933 LLVM_DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n"
2934 << "********** Function: " << mf.getName() << '\n');
2935
2936 MF = &mf;
2937 TII = MF->getSubtarget().getInstrInfo();
2938
2939 if (VerifyEnabled)
2940 MF->verify(LIS, Indexes, "Before greedy register allocator", &errs());
2941
2942 RegAllocBase::init(*this->VRM, *this->LIS, *this->Matrix);
2943
2944 // Early return if there is no virtual register to be allocated to a
2945 // physical register.
2946 if (!hasVirtRegAlloc())
2947 return false;
2948
2949 // Renumber to get accurate and consistent results from
2950 // SlotIndexes::getApproxInstrDistance.
2951 Indexes->packIndexes();
2952
2953 initializeCSRCost();
2954
2955 RegCosts = TRI->getRegisterCosts(*MF);
2956 RegClassPriorityTrumpsGlobalness =
2957 GreedyRegClassPriorityTrumpsGlobalness.getNumOccurrences()
2959 : TRI->regClassPriorityTrumpsGlobalness(*MF);
2960
2961 ReverseLocalAssignment = GreedyReverseLocalAssignment.getNumOccurrences()
2963 : TRI->reverseLocalAssignment();
2964
2965 ExtraInfo.emplace();
2966
2967 EvictAdvisor = EvictProvider->getAdvisor(*MF, *this, MBFI, Loops);
2968 PriorityAdvisor = PriorityProvider->getAdvisor(*MF, *this, *Indexes);
2969
2970 VRAI = std::make_unique<VirtRegAuxInfo>(*MF, *LIS, *VRM, *Loops, *MBFI);
2971 SpillerInstance.reset(createInlineSpiller({*LIS, *LSS, *DomTree, *MBFI}, *MF,
2972 *VRM, *VRAI, Matrix));
2973
2974 VRAI->calculateSpillWeightsAndHints();
2975
2976 LLVM_DEBUG(LIS->dump());
2977
2978 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops));
2979 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree, *MBFI, *VRAI));
2980
2981 IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI);
2982 GlobalCand.resize(32); // This will grow as needed.
2983 SetOfBrokenHints.clear();
2984
2986 tryHintsRecoloring();
2987
2988 if (VerifyEnabled)
2989 MF->verify(LIS, Indexes, "Before post optimization", &errs());
2991 reportStats();
2992
2993 releaseMemory();
2994 return true;
2995}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
DXIL Forward Handle Accesses
#define DEBUG_TYPE
const HexagonInstrInfo * TII
#define _
IRTranslator LLVM IR MI
This file implements an indexed map.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Live Register Matrix
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
block placement Basic Block Placement Stats
===- MachineOptimizationRemarkEmitter.h - Opt Diagnostics -*- C++ -*-—===//
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
MachineInstr unsigned OpIdx
#define P(N)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This header defines classes/functions to handle pass execution timing information with interfaces for...
static DominatorTree getDomTree(Function &F)
static bool hasTiedDef(MachineRegisterInfo *MRI, Register reg)
Return true if reg has any tied def operand.
static cl::opt< bool > GreedyRegClassPriorityTrumpsGlobalness("greedy-regclass-priority-trumps-globalness", cl::desc("Change the greedy register allocator's live range priority " "calculation to make the AllocationPriority of the register class " "more important then whether the range is global"), cl::Hidden)
static cl::opt< bool > ExhaustiveSearch("exhaustive-register-search", cl::NotHidden, cl::desc("Exhaustive Search for registers bypassing the depth " "and interference cutoffs of last chance recoloring"), cl::Hidden)
const float Hysteresis
static cl::opt< unsigned > CSRCostScale("regalloc-csr-cost-scale", cl::desc("Scale for the callee-saved register cost, in percentage."), cl::init(80), cl::Hidden)
static cl::opt< unsigned > LastChanceRecoloringMaxInterference("lcr-max-interf", cl::Hidden, cl::desc("Last chance recoloring maximum number of considered" " interference at a time"), cl::init(8))
static bool readsLaneSubset(const MachineRegisterInfo &MRI, const MachineInstr *MI, const LiveInterval &VirtReg, const TargetRegisterInfo *TRI, SlotIndex Use, const TargetInstrInfo *TII)
Return true if MI at \P Use reads a subset of the lanes live in VirtReg.
static bool assignedRegPartiallyOverlaps(const TargetRegisterInfo &TRI, const VirtRegMap &VRM, MCRegister PhysReg, const LiveInterval &Intf)
Return true if the existing assignment of Intf overlaps, but is not the same, as PhysReg.
static cl::opt< unsigned > CSRFirstTimeCost("regalloc-csr-first-time-cost", cl::desc("Cost for first time use of callee-saved register."), cl::init(0), cl::Hidden)
static cl::opt< unsigned > LastChanceRecoloringMaxDepth("lcr-max-depth", cl::Hidden, cl::desc("Last chance recoloring max depth"), cl::init(5))
static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", createGreedyRegisterAllocator)
static cl::opt< unsigned long > GrowRegionComplexityBudget("grow-region-complexity-budget", cl::desc("growRegion() does not scale with the number of BB edges, so " "limit its budget and bail out once we reach the limit."), cl::init(10000), cl::Hidden)
static cl::opt< unsigned > SplitThresholdForRegWithHint("split-threshold-for-reg-with-hint", cl::desc("The threshold for splitting a virtual register with a hint, in " "percentage"), cl::init(75), cl::Hidden)
static cl::opt< SplitEditor::ComplementSpillMode > SplitSpillMode("split-spill-mode", cl::Hidden, cl::desc("Spill mode for splitting live ranges"), cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"), clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"), clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed")), cl::init(SplitEditor::SM_Speed))
static unsigned getNumAllocatableRegsForConstraints(const MachineInstr *MI, Register Reg, const TargetRegisterClass *SuperRC, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, const RegisterClassInfo &RCI)
Get the number of allocatable registers that match the constraints of Reg on MI and that are also in ...
static cl::opt< bool > GreedyReverseLocalAssignment("greedy-reverse-local-assignment", cl::desc("Reverse allocation order of local live ranges, such that " "shorter local live ranges will tend to be allocated first"), cl::Hidden)
static LaneBitmask getInstReadLaneMask(const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI, const MachineInstr &FirstMI, Register Reg)
Remove Loads Into Fake Uses
SI Lower i1 Copies
SI optimize exec mask operations pre RA
SI Optimize VGPR LiveRange
This file defines the SmallSet class.
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:114
void printPipeline(raw_ostream &OS, function_ref< StringRef(StringRef)> MapClassName2PassName) const
PreservedAnalyses run(MachineFunction &F, MachineFunctionAnalysisManager &AM)
bool isHint(Register Reg) const
Return true if Reg is a preferred physical register.
ArrayRef< MCPhysReg > getOrder() const
Get the allocation order without reordered hints.
Iterator end() const
static AllocationOrder create(Register VirtReg, const VirtRegMap &VRM, const RegisterClassInfo &RegClassInfo, const LiveRegMatrix *Matrix)
Create a new AllocationOrder for VirtReg.
Iterator begin() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool test(unsigned Idx) const
Definition BitVector.h:480
BitVector & reset()
Definition BitVector.h:411
static BlockFrequency max()
Returns the maximum possible frequency, the saturation value.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Cursor - The primary query interface for the block interference cache.
SlotIndex first()
first - Return the starting index of the first interfering range in the current block.
SlotIndex last()
last - Return the ending index of the last interfering range in the current block.
bool hasInterference()
hasInterference - Return true if the current block has any interference.
void moveToBlock(unsigned MBBNum)
moveTo - Move cursor to basic block MBBNum.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Query interferences between a single live virtual register and a live interval union.
const SmallVectorImpl< const LiveInterval * > & interferingVRegs(unsigned MaxInterferingRegs=std::numeric_limits< unsigned >::max())
LiveSegments::iterator SegmentIter
A live range for subregisters.
LiveInterval - This class represents the liveness of a register, or stack slot.
Register reg() const
bool isSpillable() const
isSpillable - Can this interval be spilled?
bool hasSubRanges() const
Returns true if subregister liveness information is available.
LLVM_ABI unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
iterator_range< subrange_iterator > subranges()
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction associated with the given index.
LiveInterval & getInterval(Register Reg)
unsigned size() const
Register get(unsigned idx) const
ArrayRef< Register > regs() const
iterator end() const
iterator begin() const
Segments::const_iterator const_iterator
bool liveAt(SlotIndex index) const
bool empty() const
SlotIndex beginIndex() const
beginIndex - Return the lowest numbered slot covered.
SlotIndex endIndex() const
endNumber - return the maximum point of the range of the whole, exclusive.
LLVM_ABI iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
@ IK_VirtReg
Virtual register interference.
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isValid() const
Definition MCRegister.h:84
static constexpr unsigned NoRegister
Definition MCRegister.h:60
constexpr unsigned id() const
Definition MCRegister.h:82
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
An RAII based helper class to modify MachineFunctionProperties when running pass.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
LLVM_ABI iterator getFirstNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the first non-debug instruction in the basic block, or end().
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
bool isSpillSlotObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a spill slot.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
bool isImplicitDef() const
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
bool isFI() const
isFI - Tests if this is a MO_FrameIndex operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static reg_instr_nodbg_iterator reg_instr_nodbg_end()
defusechain_instr_iterator< true, true, true, true > reg_instr_nodbg_iterator
reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk all defs and uses of the sp...
iterator_range< def_iterator > def_operands(Register Reg) const
LLVM_ABI LaneBitmask getMaxLaneMaskForVReg(Register Reg) const
Returns a mask covering all bits that can appear in lane masks of subregisters of the virtual registe...
reg_instr_nodbg_iterator reg_instr_nodbg_begin(Register RegNo) const
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void LRE_DidCloneVirtReg(Register New, Register Old)
bool run(MachineFunction &mf)
Perform register allocation.
Spiller & spiller() override
MCRegister selectOrSplit(const LiveInterval &, SmallVectorImpl< Register > &) override
RAGreedy(RequiredAnalyses &Analyses, const RegAllocFilterFunc F=nullptr)
const LiveInterval * dequeue() override
dequeue - Return the next unassigned register, or NULL.
void enqueueImpl(const LiveInterval *LI) override
enqueue - Add VirtReg to the priority queue of unassigned registers.
void aboutToRemoveInterval(const LiveInterval &) override
Method called when the allocator is about to remove a LiveInterval.
RegAllocBase(const RegAllocFilterFunc F=nullptr)
void enqueue(const LiveInterval *LI)
enqueue - Add VirtReg to the priority queue of unassigned registers.
void init(VirtRegMap &vrm, LiveIntervals &lis, LiveRegMatrix &mat)
SmallPtrSet< MachineInstr *, 32 > DeadRemats
Inst which is a def of an original reg and whose defs are already all dead after remat is saved in De...
const TargetRegisterInfo * TRI
LiveIntervals * LIS
static const char TimerGroupName[]
static const char TimerGroupDescription[]
LiveRegMatrix * Matrix
virtual void postOptimization()
VirtRegMap * VRM
RegisterClassInfo RegClassInfo
MachineRegisterInfo * MRI
bool shouldAllocateRegister(Register Reg)
Get whether a given register should be allocated.
static bool VerifyEnabled
VerifyEnabled - True when -verify-regalloc is given.
ImmutableAnalysis abstraction for fetching the Eviction Advisor.
A MachineFunction analysis for fetching the Eviction Advisor.
Common provider for legacy and new pass managers.
const TargetRegisterInfo *const TRI
std::optional< unsigned > getOrderLimit(const LiveInterval &VirtReg, const AllocationOrder &Order, unsigned CostPerUseLimit) const
const RegisterClassInfo & RegClassInfo
bool isUnusedCalleeSavedReg(MCRegister PhysReg) const
Returns true if the given PhysReg is a callee saved register and has not been used for allocation yet...
bool canReassign(const LiveInterval &VirtReg, MCRegister FromReg) const
bool canAllocatePhysReg(unsigned CostPerUseLimit, MCRegister PhysReg) const
Common provider for getting the priority advisor and logging rewards.
unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const
getNumAllocatableRegs - Returns the number of actually allocatable registers in RC in the current fun...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
Definition Register.h:107
unsigned virtRegIndex() const
Convert a virtual register number to a 0-based index.
Definition Register.h:87
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:66
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
static bool isEarlierInstr(SlotIndex A, SlotIndex B)
isEarlierInstr - Return true if A refers to an instruction earlier than B.
@ InstrDist
The default distance between instructions as returned by distance().
bool isValid() const
Returns true if this is a valid index.
SlotIndex getBoundaryIndex() const
Returns the boundary index for associated with this index.
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
int getApproxInstrDistance(SlotIndex other) const
Return the scaled distance from this index to the given one, where all slots on the same instruction ...
SlotIndexes pass.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition SmallSet.h:176
bool erase(const T &V)
Definition SmallSet.h:200
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
size_type size() const
Definition SmallSet.h:171
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
@ MustSpill
A register is impossible, variable must be spilled.
@ DontCare
Block doesn't care / variable not live.
@ PrefReg
Block entry/exit prefers a register.
@ PrefSpill
Block entry/exit prefers a stack slot.
virtual void spill(LiveRangeEdit &LRE, AllocationOrder *Order=nullptr)=0
spill - Spill the LRE.getParent() live interval.
SplitAnalysis - Analyze a LiveInterval, looking for live range splitting opportunities.
Definition SplitKit.h:96
SplitEditor - Edit machine code and LiveIntervals for live range splitting.
Definition SplitKit.h:263
@ SM_Partition
SM_Partition(Default) - Try to create the complement interval so it doesn't overlap any other interva...
Definition SplitKit.h:286
@ SM_Speed
SM_Speed - Overlap intervals to minimize the expected execution frequency of the inserted copies.
Definition SplitKit.h:298
@ SM_Size
SM_Size - Overlap intervals to minimize the number of inserted COPY instructions.
Definition SplitKit.h:293
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:140
TargetInstrInfo - Interface to description of machine instruction set.
const uint8_t AllocationPriority
Classes with a higher priority value are assigned first by register allocators using a greedy heurist...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:91
bool hasPhys(Register virtReg) const
returns true if the specified virtual register is mapped to a physical register
Definition VirtRegMap.h:87
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
Pass manager infrastructure for declaring and invalidating analyses.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
DiagnosticInfoOptimizationBase::Argument NV
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1765
std::function< bool(const TargetRegisterInfo &TRI, const MachineRegisterInfo &MRI, const Register Reg)> RegAllocFilterFunc
Filter function for register classes during regalloc.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1739
constexpr uint64_t maxUIntN(uint64_t N)
Gets the maximum value for a N-bit unsigned integer.
Definition MathExtras.h:207
InstructionCost Cost
SmallSet< Register, 16 > SmallVirtRegSet
LLVM_ABI FunctionPass * createGreedyRegisterAllocator()
Greedy register allocation pass - This pass implements a global register allocator for optimized buil...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
LLVM_ABI bool TimePassesIsEnabled
If the user specifies the -time-passes argument on an LLVM tool command line then the value of this b...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
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:1746
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
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:1753
@ RS_Split2
Attempt more aggressive live range splitting that is guaranteed to make progress.
@ RS_Spill
Live range will be spilled. No more splitting will be attempted.
@ RS_Split
Attempt live range splitting if assignment is impossible.
@ RS_New
Newly created live range that has never been queued.
@ RS_Done
There is nothing more we can do to this live range.
@ RS_Assign
Only attempt assignment and eviction. Then requeue as RS_Split.
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:189
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI VirtRegInfo AnalyzeVirtRegInBundle(MachineInstr &MI, Register Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses a virtual register.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2052
Spiller * createInlineSpiller(const Spiller::RequiredAnalyses &Analyses, MachineFunction &MF, VirtRegMap &VRM, VirtRegAuxInfo &VRAI, LiveRegMatrix *Matrix=nullptr)
Create and return a spiller that will insert spill code directly instead of deferring though VirtRegM...
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI Printable printBlockFreq(const BlockFrequencyInfo &BFI, BlockFrequency Freq)
Print the block frequency Freq relative to the current functions entry frequency.
LLVM_ABI char & RAGreedyLegacyID
Greedy register allocator.
static float normalizeSpillWeight(float UseDefFreq, unsigned Size, unsigned NumInstr)
Normalize the spill weight of a live interval.
LLVM_ABI Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
LLVM_ABI Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
MachineBlockFrequencyInfo * MBFI
RegAllocEvictionAdvisorProvider * EvictProvider
MachineOptimizationRemarkEmitter * ORE
RegAllocPriorityAdvisorProvider * PriorityProvider
constexpr bool any() const
Definition LaneBitmask.h:53
This class is basically a combination of TimeRegion and Timer.
Definition Timer.h:175
BlockConstraint - Entry and exit constraints for a basic block.
BorderConstraint Exit
Constraint on block exit.
bool ChangesValue
True when this block changes the value of the live range.
BorderConstraint Entry
Constraint on block entry.
unsigned Number
Basic block number (from MBB::getNumber()).
Additional information about basic blocks where the current variable is live.
Definition SplitKit.h:121
SlotIndex FirstDef
First non-phi valno->def, or SlotIndex().
Definition SplitKit.h:125
bool LiveOut
Current reg is live out.
Definition SplitKit.h:127
bool LiveIn
Current reg is live in.
Definition SplitKit.h:126
MachineBasicBlock * MBB
Definition SplitKit.h:122
SlotIndex LastInstr
Last instr accessing current reg.
Definition SplitKit.h:124
SlotIndex FirstInstr
First instr accessing current reg.
Definition SplitKit.h:123