@@ -48,13 +48,10 @@ static cl::opt<bool> DisableInsertVSETVLPHIOpt(
4848namespace {
4949
5050// / Given a virtual register \p Reg, return the corresponding VNInfo for it.
51- // / This will return nullptr if the virtual register is an implicit_def or
52- // / if LiveIntervals is not available.
51+ // / This will return nullptr if the virtual register is an implicit_def.
5352static VNInfo *getVNInfoFromReg (Register Reg, const MachineInstr &MI,
5453 const LiveIntervals *LIS) {
5554 assert (Reg.isVirtual ());
56- if (!LIS)
57- return nullptr ;
5855 auto &LI = LIS->getInterval (Reg);
5956 SlotIndex SI = LIS->getSlotIndexes ()->getInstructionIndex (MI);
6057 return LI.getVNInfoBefore (SI);
@@ -515,8 +512,7 @@ DemandedFields getDemanded(const MachineInstr &MI, const RISCVSubtarget *ST) {
515512// / values of the VL and VTYPE registers after insertion.
516513class VSETVLIInfo {
517514 struct AVLDef {
518- // Every AVLDef should have a VNInfo, unless we're running without
519- // LiveIntervals in which case this will be nullptr.
515+ // Every AVLDef should have a VNInfo.
520516 const VNInfo *ValNo;
521517 Register DefReg;
522518 };
@@ -530,7 +526,7 @@ class VSETVLIInfo {
530526 AVLIsReg,
531527 AVLIsImm,
532528 AVLIsVLMAX,
533- Unknown, // AVL and VTYPE are fully unknown
529+ Unknown,
534530 } State = Uninitialized;
535531
536532 // Fields from VTYPE.
@@ -556,7 +552,7 @@ class VSETVLIInfo {
556552 bool isUnknown () const { return State == Unknown; }
557553
558554 void setAVLRegDef (const VNInfo *VNInfo, Register AVLReg) {
559- assert (AVLReg.isVirtual ());
555+ assert (VNInfo && AVLReg.isVirtual ());
560556 AVLRegDef.ValNo = VNInfo;
561557 AVLRegDef.DefReg = AVLReg;
562558 State = AVLIsReg;
@@ -586,11 +582,9 @@ class VSETVLIInfo {
586582 }
587583 // Most AVLIsReg infos will have a single defining MachineInstr, unless it was
588584 // a PHI node. In that case getAVLVNInfo()->def will point to the block
589- // boundary slot. If LiveIntervals isn't available, then nullptr is returned.
585+ // boundary slot.
590586 const MachineInstr *getAVLDefMI (const LiveIntervals *LIS) const {
591587 assert (hasAVLReg ());
592- if (!LIS)
593- return nullptr ;
594588 auto *MI = LIS->getInstructionFromIndex (getAVLVNInfo ()->def );
595589 assert (!(getAVLVNInfo ()->isPHIDef () && MI));
596590 return MI;
@@ -634,15 +628,10 @@ class VSETVLIInfo {
634628 return (hasNonZeroAVL (LIS) && Other.hasNonZeroAVL (LIS));
635629 }
636630
637- bool hasSameAVLLatticeValue (const VSETVLIInfo &Other) const {
638- if (hasAVLReg () && Other.hasAVLReg ()) {
639- assert (!getAVLVNInfo () == !Other.getAVLVNInfo () &&
640- " we either have intervals or we don't" );
641- if (!getAVLVNInfo ())
642- return getAVLReg () == Other.getAVLReg ();
631+ bool hasSameAVL (const VSETVLIInfo &Other) const {
632+ if (hasAVLReg () && Other.hasAVLReg ())
643633 return getAVLVNInfo ()->id == Other.getAVLVNInfo ()->id &&
644634 getAVLReg () == Other.getAVLReg ();
645- }
646635
647636 if (hasAVLImm () && Other.hasAVLImm ())
648637 return getAVLImm () == Other.getAVLImm ();
@@ -653,21 +642,6 @@ class VSETVLIInfo {
653642 return false ;
654643 }
655644
656- // Return true if the two lattice values are guaranteed to have
657- // the same AVL value at runtime.
658- bool hasSameAVL (const VSETVLIInfo &Other) const {
659- // Without LiveIntervals, we don't know which instruction defines a
660- // register. Since a register may be redefined, this means all AVLIsReg
661- // states must be treated as possibly distinct.
662- if (hasAVLReg () && Other.hasAVLReg ()) {
663- assert (!getAVLVNInfo () == !Other.getAVLVNInfo () &&
664- " we either have intervals or we don't" );
665- if (!getAVLVNInfo ())
666- return false ;
667- }
668- return hasSameAVLLatticeValue (Other);
669- }
670-
671645 void setVTYPE (unsigned VType) {
672646 assert (isValid () && !isUnknown () &&
673647 " Can't set VTYPE for uninitialized or unknown" );
@@ -767,7 +741,7 @@ class VSETVLIInfo {
767741 if (Other.isUnknown ())
768742 return isUnknown ();
769743
770- if (!hasSameAVLLatticeValue (Other))
744+ if (!hasSameAVL (Other))
771745 return false ;
772746
773747 // If the SEWLMULRatioOnly bits are different, then they aren't equal.
@@ -875,7 +849,6 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
875849 const RISCVSubtarget *ST;
876850 const TargetInstrInfo *TII;
877851 MachineRegisterInfo *MRI;
878- // Possibly null!
879852 LiveIntervals *LIS;
880853
881854 std::vector<BlockData> BlockInfo;
@@ -890,9 +863,9 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
890863 void getAnalysisUsage (AnalysisUsage &AU) const override {
891864 AU.setPreservesCFG ();
892865
893- AU.addUsedIfAvailable <LiveIntervals>();
866+ AU.addRequired <LiveIntervals>();
894867 AU.addPreserved <LiveIntervals>();
895- AU.addUsedIfAvailable <SlotIndexes>();
868+ AU.addRequired <SlotIndexes>();
896869 AU.addPreserved <SlotIndexes>();
897870 AU.addPreserved <LiveDebugVariables>();
898871 AU.addPreserved <LiveStacks>();
@@ -1088,8 +1061,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
10881061 .addReg (RISCV::X0, RegState::Kill)
10891062 .addImm (Info.encodeVTYPE ())
10901063 .addReg (RISCV::VL, RegState::Implicit);
1091- if (LIS)
1092- LIS->InsertMachineInstrInMaps (*MI);
1064+ LIS->InsertMachineInstrInMaps (*MI);
10931065 return ;
10941066 }
10951067
@@ -1106,8 +1078,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11061078 .addReg (RISCV::X0, RegState::Kill)
11071079 .addImm (Info.encodeVTYPE ())
11081080 .addReg (RISCV::VL, RegState::Implicit);
1109- if (LIS)
1110- LIS->InsertMachineInstrInMaps (*MI);
1081+ LIS->InsertMachineInstrInMaps (*MI);
11111082 return ;
11121083 }
11131084 }
@@ -1119,8 +1090,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11191090 .addReg (RISCV::X0, RegState::Define | RegState::Dead)
11201091 .addImm (Info.getAVLImm ())
11211092 .addImm (Info.encodeVTYPE ());
1122- if (LIS)
1123- LIS->InsertMachineInstrInMaps (*MI);
1093+ LIS->InsertMachineInstrInMaps (*MI);
11241094 return ;
11251095 }
11261096
@@ -1130,10 +1100,8 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11301100 .addReg (DestReg, RegState::Define | RegState::Dead)
11311101 .addReg (RISCV::X0, RegState::Kill)
11321102 .addImm (Info.encodeVTYPE ());
1133- if (LIS) {
1134- LIS->InsertMachineInstrInMaps (*MI);
1135- LIS->createAndComputeVirtRegInterval (DestReg);
1136- }
1103+ LIS->InsertMachineInstrInMaps (*MI);
1104+ LIS->createAndComputeVirtRegInterval (DestReg);
11371105 return ;
11381106 }
11391107
@@ -1143,14 +1111,12 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
11431111 .addReg (RISCV::X0, RegState::Define | RegState::Dead)
11441112 .addReg (AVLReg)
11451113 .addImm (Info.encodeVTYPE ());
1146- if (LIS) {
1147- LIS->InsertMachineInstrInMaps (*MI);
1148- // Normally the AVL's live range will already extend past the inserted
1149- // vsetvli because the pseudos below will already use the AVL. But this
1150- // isn't always the case, e.g. PseudoVMV_X_S doesn't have an AVL operand.
1151- LIS->getInterval (AVLReg).extendInBlock (
1152- LIS->getMBBStartIdx (&MBB), LIS->getInstructionIndex (*MI).getRegSlot ());
1153- }
1114+ LIS->InsertMachineInstrInMaps (*MI);
1115+ // Normally the AVL's live range will already extend past the inserted vsetvli
1116+ // because the pseudos below will already use the AVL. But this isn't always
1117+ // the case, e.g. PseudoVMV_X_S doesn't have an AVL operand.
1118+ LIS->getInterval (AVLReg).extendInBlock (
1119+ LIS->getMBBStartIdx (&MBB), LIS->getInstructionIndex (*MI).getRegSlot ());
11541120}
11551121
11561122// / Return true if a VSETVLI is required to transition from CurInfo to Require
@@ -1264,14 +1230,10 @@ void RISCVInsertVSETVLI::transferAfter(VSETVLIInfo &Info,
12641230 if (RISCV::isFaultFirstLoad (MI)) {
12651231 // Update AVL to vl-output of the fault first load.
12661232 assert (MI.getOperand (1 ).getReg ().isVirtual ());
1267- if (LIS) {
1268- auto &LI = LIS->getInterval (MI.getOperand (1 ).getReg ());
1269- SlotIndex SI =
1270- LIS->getSlotIndexes ()->getInstructionIndex (MI).getRegSlot ();
1271- VNInfo *VNI = LI.getVNInfoAt (SI);
1272- Info.setAVLRegDef (VNI, MI.getOperand (1 ).getReg ());
1273- } else
1274- Info.setAVLRegDef (nullptr , MI.getOperand (1 ).getReg ());
1233+ auto &LI = LIS->getInterval (MI.getOperand (1 ).getReg ());
1234+ SlotIndex SI = LIS->getSlotIndexes ()->getInstructionIndex (MI).getRegSlot ();
1235+ VNInfo *VNI = LI.getVNInfoAt (SI);
1236+ Info.setAVLRegDef (VNI, MI.getOperand (1 ).getReg ());
12751237 return ;
12761238 }
12771239
@@ -1365,9 +1327,6 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
13651327 if (!Require.hasAVLReg ())
13661328 return true ;
13671329
1368- if (!LIS)
1369- return true ;
1370-
13711330 // We need the AVL to have been produced by a PHI node in this basic block.
13721331 const VNInfo *Valno = Require.getAVLVNInfo ();
13731332 if (!Valno->isPHIDef () || LIS->getMBBFromIndex (Valno->def ) != &MBB)
@@ -1443,29 +1402,27 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
14431402 MachineOperand &VLOp = MI.getOperand (getVLOpNum (MI));
14441403 if (VLOp.isReg ()) {
14451404 Register Reg = VLOp.getReg ();
1405+ LiveInterval &LI = LIS->getInterval (Reg);
14461406
14471407 // Erase the AVL operand from the instruction.
14481408 VLOp.setReg (RISCV::NoRegister);
14491409 VLOp.setIsKill (false );
1450- if (LIS) {
1451- LiveInterval &LI = LIS->getInterval (Reg);
1452- SmallVector<MachineInstr *> DeadMIs;
1453- LIS->shrinkToUses (&LI, &DeadMIs);
1454- // We might have separate components that need split due to
1455- // needVSETVLIPHI causing us to skip inserting a new VL def.
1456- SmallVector<LiveInterval *> SplitLIs;
1457- LIS->splitSeparateComponents (LI, SplitLIs);
1458-
1459- // If the AVL was an immediate > 31, then it would have been emitted
1460- // as an ADDI. However, the ADDI might not have been used in the
1461- // vsetvli, or a vsetvli might not have been emitted, so it may be
1462- // dead now.
1463- for (MachineInstr *DeadMI : DeadMIs) {
1464- if (!TII->isAddImmediate (*DeadMI, Reg))
1465- continue ;
1466- LIS->RemoveMachineInstrFromMaps (*DeadMI);
1467- DeadMI->eraseFromParent ();
1468- }
1410+ SmallVector<MachineInstr *> DeadMIs;
1411+ LIS->shrinkToUses (&LI, &DeadMIs);
1412+ // We might have separate components that need split due to
1413+ // needVSETVLIPHI causing us to skip inserting a new VL def.
1414+ SmallVector<LiveInterval *> SplitLIs;
1415+ LIS->splitSeparateComponents (LI, SplitLIs);
1416+
1417+ // If the AVL was an immediate > 31, then it would have been emitted
1418+ // as an ADDI. However, the ADDI might not have been used in the
1419+ // vsetvli, or a vsetvli might not have been emitted, so it may be
1420+ // dead now.
1421+ for (MachineInstr *DeadMI : DeadMIs) {
1422+ if (!TII->isAddImmediate (*DeadMI, Reg))
1423+ continue ;
1424+ LIS->RemoveMachineInstrFromMaps (*DeadMI);
1425+ DeadMI->eraseFromParent ();
14691426 }
14701427 }
14711428 MI.addOperand (MachineOperand::CreateReg (RISCV::VL, /* isDef*/ false ,
@@ -1522,9 +1479,6 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
15221479 if (!UnavailablePred || !AvailableInfo.isValid ())
15231480 return ;
15241481
1525- if (!LIS)
1526- return ;
1527-
15281482 // If we don't know the exact VTYPE, we can't copy the vsetvli to the exit of
15291483 // the unavailable pred.
15301484 if (AvailableInfo.hasSEWLMULRatioOnly ())
@@ -1671,7 +1625,7 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
16711625
16721626 // The def of DefReg moved to MI, so extend the LiveInterval up to
16731627 // it.
1674- if (DefReg.isVirtual () && LIS ) {
1628+ if (DefReg.isVirtual ()) {
16751629 LiveInterval &DefLI = LIS->getInterval (DefReg);
16761630 SlotIndex MISlot = LIS->getInstructionIndex (MI).getRegSlot ();
16771631 VNInfo *DefVNI = DefLI.getVNInfoAt (DefLI.beginIndex ());
@@ -1700,15 +1654,13 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
17001654
17011655 if (OldVLReg && OldVLReg.isVirtual ()) {
17021656 // NextMI no longer uses OldVLReg so shrink its LiveInterval.
1703- if (LIS)
1704- LIS->shrinkToUses (&LIS->getInterval (OldVLReg));
1657+ LIS->shrinkToUses (&LIS->getInterval (OldVLReg));
17051658
17061659 MachineInstr *VLOpDef = MRI->getUniqueVRegDef (OldVLReg);
17071660 if (VLOpDef && TII->isAddImmediate (*VLOpDef, OldVLReg) &&
17081661 MRI->use_nodbg_empty (OldVLReg)) {
17091662 VLOpDef->eraseFromParent ();
1710- if (LIS)
1711- LIS->removeInterval (OldVLReg);
1663+ LIS->removeInterval (OldVLReg);
17121664 }
17131665 }
17141666 MI.setDesc (NextMI->getDesc ());
@@ -1724,8 +1676,7 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
17241676
17251677 NumCoalescedVSETVL += ToDelete.size ();
17261678 for (auto *MI : ToDelete) {
1727- if (LIS)
1728- LIS->RemoveMachineInstrFromMaps (*MI);
1679+ LIS->RemoveMachineInstrFromMaps (*MI);
17291680 MI->eraseFromParent ();
17301681 }
17311682}
@@ -1740,14 +1691,12 @@ void RISCVInsertVSETVLI::insertReadVL(MachineBasicBlock &MBB) {
17401691 auto ReadVLMI = BuildMI (MBB, I, MI.getDebugLoc (),
17411692 TII->get (RISCV::PseudoReadVL), VLOutput);
17421693 // Move the LiveInterval's definition down to PseudoReadVL.
1743- if (LIS) {
1744- SlotIndex NewDefSI =
1745- LIS->InsertMachineInstrInMaps (*ReadVLMI).getRegSlot ();
1746- LiveInterval &DefLI = LIS->getInterval (VLOutput);
1747- VNInfo *DefVNI = DefLI.getVNInfoAt (DefLI.beginIndex ());
1748- DefLI.removeSegment (DefLI.beginIndex (), NewDefSI);
1749- DefVNI->def = NewDefSI;
1750- }
1694+ SlotIndex NewDefSI =
1695+ LIS->InsertMachineInstrInMaps (*ReadVLMI).getRegSlot ();
1696+ LiveInterval &DefLI = LIS->getInterval (VLOutput);
1697+ VNInfo *DefVNI = DefLI.getVNInfoAt (DefLI.beginIndex ());
1698+ DefLI.removeSegment (DefLI.beginIndex (), NewDefSI);
1699+ DefVNI->def = NewDefSI;
17511700 }
17521701 // We don't use the vl output of the VLEFF/VLSEGFF anymore.
17531702 MI.getOperand (1 ).setReg (RISCV::X0);
@@ -1765,7 +1714,7 @@ bool RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
17651714
17661715 TII = ST->getInstrInfo ();
17671716 MRI = &MF.getRegInfo ();
1768- LIS = getAnalysisIfAvailable <LiveIntervals>();
1717+ LIS = &getAnalysis <LiveIntervals>();
17691718
17701719 assert (BlockInfo.empty () && " Expect empty block infos" );
17711720 BlockInfo.resize (MF.getNumBlockIDs ());
0 commit comments