[llvm] [RegisterCoalescer] Materialize undef subrange PHI inputs (PR #204867)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 19 10:23:04 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-regalloc
Author: Dominik Montada (gargaroff)
<details>
<summary>Changes</summary>
Coalescing can expose a lane-specific subrange PHI input whose value is undef on one predecessor edge. The joined interval then has a subrange PHI live-in to the successor even though the predecessor has no live-out value for that lane. Materialize that incoming undef with explicit IMPLICIT_DEF instructions so the maintained LiveIntervals stay consistent with the rewritten MachineInstrs.
Repair missing lanes per predecessor/successor edge after operands and subranges have been rewritten, but before shrinking the main range. Use a whole-register def when all lanes are missing. Otherwise, ask the target for covering subregister indexes and fall back to a greedy cover that avoids clobbering live-out lanes where possible. Any extra lanes defined by a covering subregister are recorded as dead defs.
If the normal PHI-copy insertion point would clobber a later read, or would define the register on other live-out successor paths, split the edge and insert the repair in the edge block.
Before mutating the real intervals, model the join on temporary intervals and leave the copy uncoalesced if the required repair has no safe covering subregister set or would need an unavailable edge split. The pre-coalescing check also treats shrink-marked lanes as missing, matching the subrange shrinking that happens before the real repair.
Add MIR regressions for the basic missing-lane repair, edge splitting, an unsplittable inlineasm-br target, and the shrink-marked-lane preflight case. Update checks in affected tests.
---
Patch is 840.56 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/204867.diff
24 Files Affected:
- (modified) llvm/lib/CodeGen/RegisterCoalescer.cpp (+840-51)
- (modified) llvm/test/CodeGen/AArch64/O3-pipeline.ll (+1)
- (modified) llvm/test/CodeGen/AMDGPU/amdgcn.bitcast.1024bit.ll (+1862-1849)
- (modified) llvm/test/CodeGen/AMDGPU/amdgcn.bitcast.96bit.ll (+2)
- (modified) llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fadd.ll (+938-731)
- (modified) llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmax.ll (+950-741)
- (modified) llvm/test/CodeGen/AMDGPU/buffer-fat-pointer-atomicrmw-fmin.ll (+950-741)
- (modified) llvm/test/CodeGen/AMDGPU/coalescer-subregjoin-fullcopy.mir (+1-1)
- (modified) llvm/test/CodeGen/AMDGPU/extract-subvector-16bit.ll (+18)
- (modified) llvm/test/CodeGen/AMDGPU/extract-subvector.ll (+12)
- (modified) llvm/test/CodeGen/AMDGPU/itofp.i128.ll (+50-40)
- (modified) llvm/test/CodeGen/AMDGPU/llc-pipeline.ll (+4)
- (modified) llvm/test/CodeGen/AMDGPU/rename-independent-subregs-unused-lanes.ll (+5-1)
- (modified) llvm/test/CodeGen/AMDGPU/srem.ll (+10-9)
- (modified) llvm/test/CodeGen/AMDGPU/xor-r600.ll (+6-6)
- (added) llvm/test/CodeGen/ARM/regcoalescer-undef-subrange-phi.mir (+103)
- (modified) llvm/test/CodeGen/Thumb2/LowOverheadLoops/fast-fp-loops.ll (+4)
- (modified) llvm/test/CodeGen/Thumb2/LowOverheadLoops/mov-operand.ll (+10-6)
- (modified) llvm/test/CodeGen/Thumb2/mve-float16regloops.ll (+23-23)
- (modified) llvm/test/CodeGen/Thumb2/mve-float32regloops.ll (+28-28)
- (modified) llvm/test/CodeGen/Thumb2/mve-masked-ldst.ll (+4)
- (modified) llvm/test/CodeGen/Thumb2/mve-postinc-dct.ll (+369-283)
- (added) llvm/test/CodeGen/X86/regcoalescer-undef-subrange-phi-shrink-preflight.mir (+13)
- (added) llvm/test/CodeGen/X86/regcoalescer-undef-subrange-phi-split.mir (+188)
``````````diff
diff --git a/llvm/lib/CodeGen/RegisterCoalescer.cpp b/llvm/lib/CodeGen/RegisterCoalescer.cpp
index 4b4ba2144f964..febef57518cf4 100644
--- a/llvm/lib/CodeGen/RegisterCoalescer.cpp
+++ b/llvm/lib/CodeGen/RegisterCoalescer.cpp
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "RegisterCoalescer.h"
+#include "PHIEliminationUtils.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseSet.h"
@@ -23,8 +24,10 @@
#include "llvm/CodeGen/CalcSpillWeights.h"
#include "llvm/CodeGen/LiveInterval.h"
#include "llvm/CodeGen/LiveIntervals.h"
+#include "llvm/CodeGen/LiveRangeCalc.h"
#include "llvm/CodeGen/LiveRangeEdit.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineDomTreeUpdater.h"
#include "llvm/CodeGen/MachineDominators.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
@@ -125,6 +128,25 @@ namespace {
class JoinVals;
+struct UndefPHIRepair {
+ MachineBasicBlock *PredMBB;
+ MachineBasicBlock *SuccMBB;
+ // The lanes that have a subrange PHI def in SuccMBB but are not live-out of
+ // PredMBB.
+ LaneBitmask LaneMask;
+};
+
+enum class SubRangeJoinResult {
+ /// No subranges were created or merged.
+ None,
+ /// One or both joined intervals already had subranges, so RHS subranges were
+ /// merged into LHS subranges.
+ JoinedSubRanges,
+ /// Subrange liveness was introduced while joining a source subregister into a
+ /// whole destination register. Only LHS-side subrange pruning is needed.
+ JoinedSubRegToWhole,
+};
+
class RegisterCoalescer : private LiveRangeEdit::Delegate {
MachineFunction *MF = nullptr;
MachineRegisterInfo *MRI = nullptr;
@@ -132,7 +154,9 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
const TargetInstrInfo *TII = nullptr;
LiveIntervals *LIS = nullptr;
SlotIndexes *SI = nullptr;
- const MachineLoopInfo *Loops = nullptr;
+ MachineLoopInfo *Loops = nullptr;
+ MachineDominatorTree *DomTree = nullptr;
+ bool ChangedCFG = false;
RegisterClassInfo RegClassInfo;
/// Position and VReg of a PHI instruction during coalescing.
@@ -248,6 +272,44 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
/// Attempt joining two virtual registers. Return true on success.
bool joinVirtRegs(CoalescerPair &CP);
+ /// Materialize undef subrange PHI inputs that coalescing made visible.
+ ///
+ /// A PHI value in a subrange means that every predecessor must provide a
+ /// value for that lane at the predecessor boundary. When a predecessor's
+ /// value is intentionally undef, the MIR needs an IMPLICIT_DEF in that
+ /// predecessor so LiveIntervals maintained here match the intervals that
+ /// would be recomputed from the rewritten MIR.
+ bool repairUndefSubRangePHIs(LiveInterval &LI);
+
+ /// Return true if coalescing \p CP could expose an undef subrange PHI repair
+ /// that would be impossible to recover from, for example if the repair would
+ /// need to split an edge which cannot be split.
+ bool hasUnsafeUndefPHIRepairAfterCoalescing(CoalescerPair &CP);
+
+ /// Return true if any repair for \p LI would be illegal to do, for example if
+ /// it would need an unavailable critical-edge split. If \p CP is set, reads
+ /// are interpreted as they would look after coalescing \p CP, before operands
+ /// have actually been rewritten.
+ /// \p TreatLiveOutAsMissing conservatively treats matching live-out subrange
+ /// PHI lanes as if shrinkToUses() had removed them.
+ bool hasUnavailableUndefPHIRepair(
+ LiveInterval &LI, const TargetRegisterClass *RC, LaneBitmask MaxMask,
+ const CoalescerPair *CP = nullptr,
+ LaneBitmask TreatLiveOutAsMissing = LaneBitmask::getNone());
+
+ /// Return lanes read in [Begin, End), after applying the virtual-register
+ /// substitutions that would be performed for \p CP.
+ LaneBitmask getReadLanesInRangeAfterCoalescing(
+ const CoalescerPair &CP, LaneBitmask MaxMask,
+ MachineBasicBlock::iterator Begin, MachineBasicBlock::iterator End);
+
+ /// Return the PHI-copy insertion point that would be used after coalescing
+ /// \p CP, before operands have actually been rewritten.
+ MachineBasicBlock::iterator
+ findPHICopyInsertPointAfterCoalescing(const CoalescerPair &CP,
+ MachineBasicBlock *MBB,
+ MachineBasicBlock *SuccMBB);
+
/// If a live interval has many valnos and is coalesced with other
/// live intervals many times, we regard such live interval as having
/// high compile time cost.
@@ -265,6 +327,12 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
LaneBitmask LaneMask, CoalescerPair &CP,
unsigned DstIdx);
+ /// Perform the subrange construction and merging needed by joinVirtRegs().
+ SubRangeJoinResult mergeSubRangesForJoin(LiveInterval &LHS, LiveInterval &RHS,
+ CoalescerPair &CP,
+ bool TrackSubRegLiveness,
+ bool EmitDebug = true);
+
/// Join the liveranges of two subregisters. Joins @p RRange into
/// @p LRange, @p RRange may be invalid afterwards.
void joinSubRegRanges(LiveRange &LRange, LiveRange &RRange,
@@ -381,9 +449,11 @@ class RegisterCoalescer : private LiveRangeEdit::Delegate {
RegisterCoalescer() = default;
RegisterCoalescer &operator=(RegisterCoalescer &&Other) = default;
- RegisterCoalescer(LiveIntervals *LIS, SlotIndexes *SI,
- const MachineLoopInfo *Loops)
- : LIS(LIS), SI(SI), Loops(Loops) {}
+ RegisterCoalescer(LiveIntervals *LIS, SlotIndexes *SI, MachineLoopInfo *Loops,
+ MachineDominatorTree *DomTree)
+ : LIS(LIS), SI(SI), Loops(Loops), DomTree(DomTree) {}
+
+ bool changedCFG() const { return ChangedCFG; }
bool run(MachineFunction &MF);
};
@@ -415,6 +485,7 @@ INITIALIZE_PASS_BEGIN(RegisterCoalescerLegacy, "register-coalescer",
INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
+INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
INITIALIZE_PASS_END(RegisterCoalescerLegacy, "register-coalescer",
"Register Coalescer", false, false)
@@ -593,14 +664,14 @@ bool CoalescerPair::isCoalescable(const MachineInstr *MI) const {
}
void RegisterCoalescerLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
- AU.setPreservesCFG();
AU.addUsedIfAvailable<SlotIndexesWrapperPass>();
AU.addRequired<LiveIntervalsWrapperPass>();
AU.addPreserved<LiveIntervalsWrapperPass>();
AU.addPreserved<SlotIndexesWrapperPass>();
AU.addRequired<MachineLoopInfoWrapperPass>();
AU.addPreserved<MachineLoopInfoWrapperPass>();
- AU.addPreservedID(MachineDominatorsID);
+ AU.addRequired<MachineDominatorTreeWrapperPass>();
+ AU.addPreserved<MachineDominatorTreeWrapperPass>();
MachineFunctionPass::getAnalysisUsage(AU);
}
@@ -2182,6 +2253,13 @@ bool RegisterCoalescer::joinCopy(
ShrinkMask = LaneBitmask::getNone();
ShrinkMainRange = false;
+ // If a later repair would need an unavailable edge split, leave the copy
+ // alone before joinIntervals() mutates the intervals and erases the copy.
+ if (hasUnsafeUndefPHIRepairAfterCoalescing(CP)) {
+ LLVM_DEBUG(dbgs() << "\tUnsafe undef PHI repair; skipping join.\n");
+ return false;
+ }
+
// Okay, attempt to join these two intervals. On failure, this returns false.
// Otherwise, if one of the intervals being joined is a physreg, this method
// always canonicalizes DstInt to be it. The output "SrcInt" will not have
@@ -2271,6 +2349,15 @@ bool RegisterCoalescer::joinCopy(
if (ToBeUpdated.count(CP.getSrcReg()))
ShrinkMainRange = true;
+ if (!CP.isPhys()) {
+ LiveInterval &LI = LIS->getInterval(CP.getDstReg());
+ // Joining the intervals above can turn a previously implicit undef PHI
+ // operand into a lane-specific PHI value on DstReg. Repair before main
+ // range shrinking so shrinkToUses() does not see a PHI edge whose incoming
+ // lane value is still implicit in the MIR.
+ repairUndefSubRangePHIs(LI);
+ }
+
if (ShrinkMainRange) {
LiveInterval &LI = LIS->getInterval(CP.getDstReg());
shrinkToUses(&LI);
@@ -3681,6 +3768,60 @@ void RegisterCoalescer::mergeSubRangeInto(LiveInterval &LI,
*LIS->getSlotIndexes(), *TRI, ComposeSubRegIdx);
}
+SubRangeJoinResult RegisterCoalescer::mergeSubRangesForJoin(
+ LiveInterval &LHS, LiveInterval &RHS, CoalescerPair &CP,
+ bool TrackSubRegLiveness, bool EmitDebug) {
+ const TargetRegisterClass *NewRC = CP.getNewRC();
+
+ if (RHS.hasSubRanges() || LHS.hasSubRanges()) {
+ BumpPtrAllocator &Allocator = LIS->getVNInfoAllocator();
+
+ // Transform lanemasks from the LHS to masks in the coalesced register and
+ // create initial subranges if necessary.
+ unsigned DstIdx = CP.getDstIdx();
+ if (!LHS.hasSubRanges()) {
+ LaneBitmask Mask = DstIdx == 0 ? NewRC->getLaneMask()
+ : TRI->getSubRegIndexLaneMask(DstIdx);
+ // LHS must support subregs or we wouldn't be in this codepath.
+ assert(Mask.any());
+ LHS.createSubRangeFrom(Allocator, Mask, LHS);
+ } else if (DstIdx != 0) {
+ // Transform LHS lanemasks to new register class if necessary.
+ for (LiveInterval::SubRange &R : LHS.subranges())
+ R.LaneMask = TRI->composeSubRegIndexLaneMask(DstIdx, R.LaneMask);
+ }
+ if (EmitDebug)
+ LLVM_DEBUG(dbgs() << "\t\tLHST = " << printReg(CP.getDstReg()) << ' '
+ << LHS << '\n');
+
+ // Determine lanemasks of RHS in the coalesced register and merge subranges.
+ unsigned SrcIdx = CP.getSrcIdx();
+ if (!RHS.hasSubRanges()) {
+ LaneBitmask Mask = SrcIdx == 0 ? NewRC->getLaneMask()
+ : TRI->getSubRegIndexLaneMask(SrcIdx);
+ mergeSubRangeInto(LHS, RHS, Mask, CP, DstIdx);
+ } else {
+ // Pair up subranges and merge.
+ for (LiveInterval::SubRange &R : RHS.subranges()) {
+ LaneBitmask Mask = TRI->composeSubRegIndexLaneMask(SrcIdx, R.LaneMask);
+ mergeSubRangeInto(LHS, R, Mask, CP, DstIdx);
+ }
+ }
+
+ return SubRangeJoinResult::JoinedSubRanges;
+ }
+
+ if (TrackSubRegLiveness && !CP.getDstIdx() && CP.getSrcIdx()) {
+ LHS.createSubRangeFrom(LIS->getVNInfoAllocator(), NewRC->getLaneMask(),
+ LHS);
+ mergeSubRangeInto(LHS, RHS, TRI->getSubRegIndexLaneMask(CP.getSrcIdx()), CP,
+ CP.getDstIdx());
+ return SubRangeJoinResult::JoinedSubRegToWhole;
+ }
+
+ return SubRangeJoinResult::None;
+}
+
bool RegisterCoalescer::isHighCostLiveInterval(LiveInterval &LI) {
if (LI.valnos.size() < LargeIntervalSizeThreshold)
return false;
@@ -3692,6 +3833,682 @@ bool RegisterCoalescer::isHighCostLiveInterval(LiveInterval &LI) {
return true;
}
+static LaneBitmask
+getDefMaskForUndefPHIRepairSubReg(const TargetRegisterInfo &TRI,
+ LaneBitmask MaxMask, unsigned SubReg) {
+ // If no subregister is used, the mask is the full mask of the register class
+ // as given by MaxMask
+ if (SubReg == 0)
+ return MaxMask;
+
+ // Otherwise ask the target for the lane mask for the given subregister and
+ // make sure it is a subset of the maximum mask for the register class
+ LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg);
+ return DefMask & MaxMask;
+}
+
+static bool getUndefPHIRepairSubRegIndexes(const TargetRegisterInfo &TRI,
+ const TargetRegisterClass *RC,
+ LaneBitmask MissingMask,
+ LaneBitmask AllowedMask,
+ SmallVectorImpl<unsigned> &Indexes) {
+ assert((MissingMask & ~AllowedMask).none() &&
+ "missing lanes must be legal to repair");
+
+ // Find the best subregister indexes to cover the missing lanes where each
+ // index covers as many missing lanes as possible, and as few extra lanes as
+ // possible. When legal single-lane indexes exist, this can fall back to one
+ // index per missing lane.
+ LaneBitmask LanesLeft = MissingMask;
+ while (LanesLeft.any()) {
+ unsigned BestIdx = 0;
+ unsigned BestCover = 0;
+ unsigned BestExtra = std::numeric_limits<unsigned>::max();
+
+ for (unsigned Idx = 1, E = TRI.getNumSubRegIndices(); Idx < E; ++Idx) {
+ // Skip subregister indexes that don't belong to the register class
+ if (TRI.getSubClassWithSubReg(RC, Idx) != RC)
+ continue;
+
+ LaneBitmask SubRegMask = TRI.getSubRegIndexLaneMask(Idx);
+ LaneBitmask Covered = SubRegMask & LanesLeft;
+
+ // The subregister doesn't cover any of the lanes we're still missing
+ if (Covered.none())
+ continue;
+
+ // The chosen subregister would clobber lanes that are not allowed to be
+ // defined by the repair
+ if ((SubRegMask & ~AllowedMask).any())
+ continue;
+
+ // Skip subregisters that overlap with missing lanes we have already
+ // covered with other subregisters
+ if ((SubRegMask & (MissingMask & ~LanesLeft)).any())
+ continue;
+
+ unsigned Cover = Covered.getNumLanes();
+ unsigned Extra = (SubRegMask & ~LanesLeft).getNumLanes();
+ if (Cover > BestCover || (Cover == BestCover && Extra < BestExtra)) {
+ BestIdx = Idx;
+ BestCover = Cover;
+ BestExtra = Extra;
+ }
+ }
+
+ if (BestIdx == 0)
+ return false;
+
+ Indexes.push_back(BestIdx);
+ LanesLeft &= ~TRI.getSubRegIndexLaneMask(BestIdx);
+ }
+
+ return true;
+}
+
+static void copyLiveInterval(LiveInterval &Dst, const LiveInterval &Src,
+ BumpPtrAllocator &Allocator) {
+ Dst.assign(Src, Allocator);
+ for (const LiveInterval::SubRange &SR : Src.subranges())
+ Dst.createSubRangeFrom(Allocator, SR.LaneMask, SR);
+}
+
+static void addUndefPHIRepair(SmallVectorImpl<UndefPHIRepair> &Repairs,
+ MachineBasicBlock *PredMBB,
+ MachineBasicBlock *SuccMBB,
+ LaneBitmask LaneMask) {
+ // A predecessor can be missing several lanes for the same successor PHI.
+ // Combining them lets us emit the fewest IMPLICIT_DEFs that cover the
+ // register class instead of repairing each subrange independently.
+ for (UndefPHIRepair &R : Repairs) {
+ if (R.PredMBB == PredMBB && R.SuccMBB == SuccMBB) {
+ R.LaneMask |= LaneMask;
+ return;
+ }
+ }
+ Repairs.push_back({PredMBB, SuccMBB, LaneMask});
+}
+
+static void collectUndefPHIRepairs(
+ LiveInterval &LI, const MachineRegisterInfo &MRI,
+ const SlotIndexes &Indexes, SmallVectorImpl<UndefPHIRepair> &Repairs,
+ LaneBitmask TreatLiveOutAsMissing = LaneBitmask::getNone()) {
+ LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(LI.reg());
+
+ for (LiveInterval::SubRange &SR : LI.subranges()) {
+ if (SR.empty())
+ continue;
+
+ // Check if the lanes of this subrange are marked as undefined by a
+ // subregister read-undef definition. We can use that to determine whether
+ // a repair will really be needed further down
+ SmallVector<SlotIndex, 4> Undefs;
+ LaneBitmask UndefMask = SR.LaneMask & VRegMask;
+ if (UndefMask.any())
+ LI.computeSubRangeUndefs(Undefs, UndefMask, MRI, Indexes);
+
+ for (const VNInfo *VNI : SR.valnos) {
+ if (VNI->isUnused() || !VNI->isPHIDef())
+ continue;
+
+ MachineBasicBlock *MBB = Indexes.getMBBFromIndex(VNI->def);
+ for (MachineBasicBlock *PredMBB : MBB->predecessors()) {
+ SlotIndex PredEnd = Indexes.getMBBEndIdx(PredMBB);
+
+ // For the pre-coalescing check, treat any lane of this subrange that
+ // overlaps with TreatLiveOutAsMissing as missing
+ bool ForceMissing =
+ (SR.LaneMask & TreatLiveOutAsMissing & VRegMask).any();
+
+ // PHI subranges are live-in to the block only if the same lane is live
+ // out of each predecessor. A missing live-out lane means this edge has
+ // an undef PHI input that must be made explicit by an IMPLICIT_DEF
+ if (!ForceMissing && SR.liveAt(PredEnd.getPrevSlot()))
+ continue;
+
+ // No need to repair anything if the predecessor is dominated by all the
+ // blocks corresponding to the SlotIndexes in Undefs
+ if (!ForceMissing &&
+ LiveRangeCalc::isJointlyDominated(PredMBB, Undefs, Indexes))
+ continue;
+
+ LLVM_DEBUG({
+ if (ForceMissing)
+ dbgs() << "\t\tTreating shrink-marked PHI lanes as missing on "
+ << printMBBReference(*PredMBB) << " -> "
+ << printMBBReference(*MBB) << ": "
+ << PrintLaneMask(SR.LaneMask & TreatLiveOutAsMissing &
+ VRegMask)
+ << '\n';
+ });
+
+ addUndefPHIRepair(Repairs, PredMBB, MBB, SR.LaneMask);
+ }
+ }
+ }
+}
+
+static bool repairNeedsEdgeSplit(const LiveInterval &LI,
+ const SlotIndexes &Indexes,
+ const UndefPHIRepair &Repair) {
+ // No need to split if the edge recorded by Repair is the only outgoing edge
+ // from the predecessor
+ if (Repair.PredMBB->succ_size() <= 1)
+ return false;
+
+ // The predecessor has multiple successors. Inserting an IMPLICIT_DEF would
+ // affect all outgoing edges, not just the one edge whose PHI input is undef.
+ // If the main interval is live at the end of the predecessor, there is a real
+ // live out value for that vreg on some outgoing path. Require an edge split
+ // to avoid clobbering that value.
+ SlotIndex LastPredSlot = Indexes.getMBBEndIdx(Repair.PredMBB).getPrevSlot();
+ return LI.liveAt(LastPredSlot);
+}
+
+static bool containsPHIDef(const LiveRange &LR) {
+ for (const VNInfo *VNI : LR.valnos)
+ if (!VNI->isUnused() && VNI->isPHIDef())
+ return true;
+ return false;
+}
+
+static bool containsPHIDef(const LiveInterval &LI) {
+ if (containsPHIDef(static_cast<const LiveRange &>(LI)))
+ return true;
+ for (const LiveInterval::SubRange &SR : LI.subranges())
+ if (containsPHIDef(SR))
+ return true;
+ return false;
+}
+
+static bool getUndefPHIRepairSubRegsForRepair(
+ const TargetRegisterInfo &TRI, const TargetRegisterClass *RC,
+ LiveInterval &LI, const UndefPHIRepair &Repair, LaneBitmask MaxMask,
+ const SlotIndexes &Indexes, SmallVectorImpl<unsigned> &SubRegs) {
+
+ LaneBitmask MissingMask = Repair.LaneMask;
+ assert((MissingMask & ~MaxMask).none() &&
+ "Repair contains lanes outside the virtual register");
+
+ // If all lanes are missing, use the full register
+ if (MissingMask == MaxMask) {
+ SubRegs.push_back(0);
+ return true;
+ }
+
+ // Ask the target if it can cover the missing lanes with subregisters
+ if (TRI.getCoveringSubRegIndexes(RC, MissingMask, SubRegs))
+ return true;
+
+ SubRegs.clear();
+
+ // Find the lanes that are live-out of the predecessor
+ LaneBitmask LiveOutMask = LaneBitmask::getNone();
+ SlotIndex LastPredSlot = Indexes.getMBBEndIdx(Repair.PredMBB).getPrevSlot();
+ for (LiveInterval::SubRange &SR : LI.subranges()) {
+ if (!SR.empty() && SR.liveAt(LastPredSlot))
+ LiveOutMask |= SR.LaneMask;
+ }
+
+ // Try to build a set of subregisters that cover the missing lanes without
+ // clobbering any lanes that are live-out
+ LaneBitmask AllowedMask = MissingMask | (MaxMask & ~LiveOutMask);
+ return getUndefPHIRepairSubRegIndexes(TRI, RC, MissingMask, AllowedMask,
+ SubRegs);
+}
+
+static LaneBitmask getReadLanesInRange(const TargetRegisterInfo &TRI,
+ Register Reg, LaneBitmask MaxMask,
+ MachineBasicBlock::iterator Begin,
+ MachineBasicBlock::iterator End) {
+ // Find all lanes of Reg that are read in the range [Begin, End)
+ LaneBitmask ReadMask = LaneBitmask::getNone();
+ for (MachineInstr &MI : make_range(Begin, End...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/204867
More information about the llvm-commits
mailing list