2121#include " llvm/CodeGen/LiveDebugVariables.h"
2222#include " llvm/CodeGen/LiveInterval.h"
2323#include " llvm/CodeGen/LiveIntervals.h"
24+ #include " llvm/CodeGen/LiveRegMatrix.h"
2425#include " llvm/CodeGen/LiveStacks.h"
2526#include " llvm/CodeGen/MachineBasicBlock.h"
2627#include " llvm/CodeGen/MachineFrameInfo.h"
@@ -203,6 +204,7 @@ class VirtRegRewriter : public MachineFunctionPass {
203204 MachineRegisterInfo *MRI = nullptr ;
204205 SlotIndexes *Indexes = nullptr ;
205206 LiveIntervals *LIS = nullptr ;
207+ LiveRegMatrix *LRM = nullptr ;
206208 VirtRegMap *VRM = nullptr ;
207209 LiveDebugVariables *DebugVars = nullptr ;
208210 DenseSet<Register> RewriteRegs;
@@ -215,9 +217,6 @@ class VirtRegRewriter : public MachineFunctionPass {
215217 void handleIdentityCopy (MachineInstr &MI);
216218 void expandCopyBundle (MachineInstr &MI) const ;
217219 bool subRegLiveThrough (const MachineInstr &MI, MCRegister SuperPhysReg) const ;
218- bool needLiveOutUndefSubregDef (const LiveInterval &LI,
219- const MachineBasicBlock &MBB, unsigned SubReg,
220- MCPhysReg PhysReg) const ;
221220 LaneBitmask liveOutUndefPhiLanesForUndefSubregDef (
222221 const LiveInterval &LI, const MachineBasicBlock &MBB, unsigned SubReg,
223222 MCPhysReg PhysReg, const MachineInstr &MI) const ;
@@ -253,6 +252,7 @@ INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
253252INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
254253INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
255254INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
255+ INITIALIZE_PASS_DEPENDENCY(LiveRegMatrixWrapperLegacy)
256256INITIALIZE_PASS_DEPENDENCY(LiveStacks)
257257INITIALIZE_PASS_DEPENDENCY(VirtRegMapWrapperLegacy)
258258INITIALIZE_PASS_END(VirtRegRewriter, " virtregrewriter" ,
@@ -268,6 +268,7 @@ void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
268268 AU.addRequired <LiveStacks>();
269269 AU.addPreserved <LiveStacks>();
270270 AU.addRequired <VirtRegMapWrapperLegacy>();
271+ AU.addRequired <LiveRegMatrixWrapperLegacy>();
271272
272273 if (!ClearVirtRegs)
273274 AU.addPreserved <LiveDebugVariables>();
@@ -282,6 +283,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
282283 MRI = &MF->getRegInfo ();
283284 Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI ();
284285 LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
286+ LRM = &getAnalysis<LiveRegMatrixWrapperLegacy>().getLRM ();
285287 VRM = &getAnalysis<VirtRegMapWrapperLegacy>().getVRM ();
286288 DebugVars = &getAnalysis<LiveDebugVariables>();
287289 LLVM_DEBUG (dbgs () << " ********** REWRITE VIRTUAL REGISTERS **********\n "
@@ -554,26 +556,6 @@ bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI,
554556 return false ;
555557}
556558
557- // / Check if we need to maintain liveness for undef subregister lanes that are
558- // / live out of a block.
559- bool VirtRegRewriter::needLiveOutUndefSubregDef (const LiveInterval &LI,
560- const MachineBasicBlock &MBB,
561- unsigned SubReg,
562- MCPhysReg PhysReg) const {
563- LaneBitmask UndefMask = ~TRI->getSubRegIndexLaneMask (SubReg);
564- for (const LiveInterval::SubRange &SR : LI.subranges ()) {
565- LaneBitmask NeedImpDefLanes = UndefMask & SR.LaneMask ;
566- if (NeedImpDefLanes.any () && !LIS->isLiveOutOfMBB (SR, &MBB)) {
567- for (const MachineBasicBlock *Succ : MBB.successors ()) {
568- if (LIS->isLiveInToMBB (SR, Succ))
569- return true ;
570- }
571- }
572- }
573-
574- return false ;
575- }
576-
577559// / Compute a lanemask for undef lanes which need to be preserved out of the
578560// / defining block for a register assignment.
579561LaneBitmask VirtRegRewriter::liveOutUndefPhiLanesForUndefSubregDef (
@@ -591,20 +573,17 @@ LaneBitmask VirtRegRewriter::liveOutUndefPhiLanesForUndefSubregDef(
591573 }
592574 }
593575 }
594- if (LiveOutUndefLanes.none ())
595- return LiveOutUndefLanes;
596576
597577 SlotIndex MIIndex = LIS->getInstructionIndex (MI);
598578 SlotIndex BeforeMIUses = MIIndex.getBaseIndex ();
599- SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex ();
600-
601- for (MCRegUnitMaskIterator MCRU (PhysReg, TRI); MCRU.isValid (); ++MCRU) {
602- auto [RU, PhysRegMask] = *MCRU;
579+ LaneBitmask InterferingLanes =
580+ LRM->checkInterferenceLanes (BeforeMIUses, MIIndex.getRegSlot (), PhysReg);
581+ LiveOutUndefLanes &= ~InterferingLanes;
603582
604- const LiveRange &UnitRange = LIS-> getRegUnit (RU);
605- if (UnitRange. liveAt (AfterMIDefs) && UnitRange. liveAt (BeforeMIUses) )
606- LiveOutUndefLanes &= ~PhysRegMask ;
607- }
583+ LLVM_DEBUG ( if (LiveOutUndefLanes. any ()) {
584+ dbgs () << " Need live out undef defs for " << printReg (PhysReg )
585+ << LiveOutUndefLanes << " from " << printMBBReference (MBB) << ' \n ' ;
586+ });
608587
609588 return LiveOutUndefLanes;
610589}
@@ -672,33 +651,21 @@ void VirtRegRewriter::rewrite() {
672651 if (LiveOutUndefLanes.any ()) {
673652 SmallVector<unsigned , 16 > CoveringIndexes;
674653
675- // TODO: Just use the super register if
676- if (TRI->getCoveringSubRegIndexes (
654+ // TODO: Just use one super register def if none of the lanes
655+ // are needed?
656+ if (!TRI->getCoveringSubRegIndexes (
677657 *MRI, MRI->getRegClass (VirtReg), LiveOutUndefLanes,
678- CoveringIndexes)) {
679- // Try to represent the minimum needed live out def as a
680- // sequence of subregister defs.
681- //
682- // FIXME: It would be better if we could directly represent
683- // liveness with a lanemask instead of spamming operands.
684- for (unsigned SubIdx : CoveringIndexes)
685- SuperDefs.push_back (TRI->getSubReg (PhysReg, SubIdx));
686- } else {
687- // If we could not represent this as a sequence of
688- // subregisters, it's safe to replace all the lanes with a
689- // full def of the super register.
690- SuperDefs.push_back (PhysReg);
691- }
692- }
693-
694- if (false &&
695- needLiveOutUndefSubregDef (LI, *MBBI, SubReg, PhysReg)) {
696- SuperDefs.push_back (PhysReg);
697-
698- for (MCRegister AssignedSubReg : TRI->subregs (PhysReg)) {
699- if (subRegLiveThrough (MI, AssignedSubReg))
700- SuperKills.push_back (AssignedSubReg);
701- }
658+ CoveringIndexes))
659+ llvm_unreachable (
660+ " cannot represent required subregister defs" );
661+
662+ // Try to represent the minimum needed live out def as a
663+ // sequence of subregister defs.
664+ //
665+ // FIXME: It would be better if we could directly represent
666+ // liveness with a lanemask instead of spamming operands.
667+ for (unsigned SubIdx : CoveringIndexes)
668+ SuperDefs.push_back (TRI->getSubReg (PhysReg, SubIdx));
702669 }
703670 }
704671 }
0 commit comments