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"
@@ -187,6 +188,7 @@ class VirtRegRewriter : public MachineFunctionPass {
187188 MachineRegisterInfo *MRI = nullptr ;
188189 SlotIndexes *Indexes = nullptr ;
189190 LiveIntervals *LIS = nullptr ;
191+ LiveRegMatrix *LRM = nullptr ;
190192 VirtRegMap *VRM = nullptr ;
191193 LiveDebugVariables *DebugVars = nullptr ;
192194 DenseSet<Register> RewriteRegs;
@@ -199,9 +201,6 @@ class VirtRegRewriter : public MachineFunctionPass {
199201 void handleIdentityCopy (MachineInstr &MI);
200202 void expandCopyBundle (MachineInstr &MI) const ;
201203 bool subRegLiveThrough (const MachineInstr &MI, MCRegister SuperPhysReg) const ;
202- bool needLiveOutUndefSubregDef (const LiveInterval &LI,
203- const MachineBasicBlock &MBB, unsigned SubReg,
204- MCPhysReg PhysReg) const ;
205204 LaneBitmask liveOutUndefPhiLanesForUndefSubregDef (
206205 const LiveInterval &LI, const MachineBasicBlock &MBB, unsigned SubReg,
207206 MCPhysReg PhysReg, const MachineInstr &MI) const ;
@@ -237,6 +236,7 @@ INITIALIZE_PASS_BEGIN(VirtRegRewriter, "virtregrewriter",
237236INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
238237INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
239238INITIALIZE_PASS_DEPENDENCY(LiveDebugVariables)
239+ INITIALIZE_PASS_DEPENDENCY(LiveRegMatrix)
240240INITIALIZE_PASS_DEPENDENCY(LiveStacks)
241241INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
242242INITIALIZE_PASS_END(VirtRegRewriter, " virtregrewriter" ,
@@ -252,6 +252,7 @@ void VirtRegRewriter::getAnalysisUsage(AnalysisUsage &AU) const {
252252 AU.addRequired <LiveStacks>();
253253 AU.addPreserved <LiveStacks>();
254254 AU.addRequired <VirtRegMap>();
255+ AU.addRequired <LiveRegMatrix>();
255256
256257 if (!ClearVirtRegs)
257258 AU.addPreserved <LiveDebugVariables>();
@@ -266,6 +267,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
266267 MRI = &MF->getRegInfo ();
267268 Indexes = &getAnalysis<SlotIndexesWrapperPass>().getSI ();
268269 LIS = &getAnalysis<LiveIntervalsWrapperPass>().getLIS ();
270+ LRM = &getAnalysis<LiveRegMatrix>();
269271 VRM = &getAnalysis<VirtRegMap>();
270272 DebugVars = &getAnalysis<LiveDebugVariables>();
271273 LLVM_DEBUG (dbgs () << " ********** REWRITE VIRTUAL REGISTERS **********\n "
@@ -538,26 +540,6 @@ bool VirtRegRewriter::subRegLiveThrough(const MachineInstr &MI,
538540 return false ;
539541}
540542
541- // / Check if we need to maintain liveness for undef subregister lanes that are
542- // / live out of a block.
543- bool VirtRegRewriter::needLiveOutUndefSubregDef (const LiveInterval &LI,
544- const MachineBasicBlock &MBB,
545- unsigned SubReg,
546- MCPhysReg PhysReg) const {
547- LaneBitmask UndefMask = ~TRI->getSubRegIndexLaneMask (SubReg);
548- for (const LiveInterval::SubRange &SR : LI.subranges ()) {
549- LaneBitmask NeedImpDefLanes = UndefMask & SR.LaneMask ;
550- if (NeedImpDefLanes.any () && !LIS->isLiveOutOfMBB (SR, &MBB)) {
551- for (const MachineBasicBlock *Succ : MBB.successors ()) {
552- if (LIS->isLiveInToMBB (SR, Succ))
553- return true ;
554- }
555- }
556- }
557-
558- return false ;
559- }
560-
561543// / Compute a lanemask for undef lanes which need to be preserved out of the
562544// / defining block for a register assignment.
563545LaneBitmask VirtRegRewriter::liveOutUndefPhiLanesForUndefSubregDef (
@@ -575,20 +557,17 @@ LaneBitmask VirtRegRewriter::liveOutUndefPhiLanesForUndefSubregDef(
575557 }
576558 }
577559 }
578- if (LiveOutUndefLanes.none ())
579- return LiveOutUndefLanes;
580560
581561 SlotIndex MIIndex = LIS->getInstructionIndex (MI);
582562 SlotIndex BeforeMIUses = MIIndex.getBaseIndex ();
583- SlotIndex AfterMIDefs = MIIndex.getBoundaryIndex ();
584-
585- for (MCRegUnitMaskIterator MCRU (PhysReg, TRI); MCRU.isValid (); ++MCRU) {
586- auto [RU, PhysRegMask] = *MCRU;
563+ LaneBitmask InterferingLanes =
564+ LRM->checkInterferenceLanes (BeforeMIUses, MIIndex.getRegSlot (), PhysReg);
565+ LiveOutUndefLanes &= ~InterferingLanes;
587566
588- const LiveRange &UnitRange = LIS-> getRegUnit (RU);
589- if (UnitRange. liveAt (AfterMIDefs) && UnitRange. liveAt (BeforeMIUses) )
590- LiveOutUndefLanes &= ~PhysRegMask ;
591- }
567+ LLVM_DEBUG ( if (LiveOutUndefLanes. any ()) {
568+ dbgs () << " Need live out undef defs for " << printReg (PhysReg )
569+ << LiveOutUndefLanes << " from " << printMBBReference (MBB) << ' \n ' ;
570+ });
592571
593572 return LiveOutUndefLanes;
594573}
@@ -656,33 +635,21 @@ void VirtRegRewriter::rewrite() {
656635 if (LiveOutUndefLanes.any ()) {
657636 SmallVector<unsigned , 16 > CoveringIndexes;
658637
659- // TODO: Just use the super register if
660- if (TRI->getCoveringSubRegIndexes (
638+ // TODO: Just use one super register def if none of the lanes
639+ // are needed?
640+ if (!TRI->getCoveringSubRegIndexes (
661641 *MRI, MRI->getRegClass (VirtReg), LiveOutUndefLanes,
662- CoveringIndexes)) {
663- // Try to represent the minimum needed live out def as a
664- // sequence of subregister defs.
665- //
666- // FIXME: It would be better if we could directly represent
667- // liveness with a lanemask instead of spamming operands.
668- for (unsigned SubIdx : CoveringIndexes)
669- SuperDefs.push_back (TRI->getSubReg (PhysReg, SubIdx));
670- } else {
671- // If we could not represent this as a sequence of
672- // subregisters, it's safe to replace all the lanes with a
673- // full def of the super register.
674- SuperDefs.push_back (PhysReg);
675- }
676- }
677-
678- if (false &&
679- needLiveOutUndefSubregDef (LI, *MBBI, SubReg, PhysReg)) {
680- SuperDefs.push_back (PhysReg);
681-
682- for (MCRegister AssignedSubReg : TRI->subregs (PhysReg)) {
683- if (subRegLiveThrough (MI, AssignedSubReg))
684- SuperKills.push_back (AssignedSubReg);
685- }
642+ CoveringIndexes))
643+ llvm_unreachable (
644+ " cannot represent required subregister defs" );
645+
646+ // Try to represent the minimum needed live out def as a
647+ // sequence of subregister defs.
648+ //
649+ // FIXME: It would be better if we could directly represent
650+ // liveness with a lanemask instead of spamming operands.
651+ for (unsigned SubIdx : CoveringIndexes)
652+ SuperDefs.push_back (TRI->getSubReg (PhysReg, SubIdx));
686653 }
687654 }
688655 }
0 commit comments