@@ -48,13 +48,10 @@ static cl::opt<bool> DisableInsertVSETVLPHIOpt(
48
48
namespace {
49
49
50
50
// / 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.
53
52
static VNInfo *getVNInfoFromReg (Register Reg, const MachineInstr &MI,
54
53
const LiveIntervals *LIS) {
55
54
assert (Reg.isVirtual ());
56
- if (!LIS)
57
- return nullptr ;
58
55
auto &LI = LIS->getInterval (Reg);
59
56
SlotIndex SI = LIS->getSlotIndexes ()->getInstructionIndex (MI);
60
57
return LI.getVNInfoBefore (SI);
@@ -515,8 +512,7 @@ DemandedFields getDemanded(const MachineInstr &MI, const RISCVSubtarget *ST) {
515
512
// / values of the VL and VTYPE registers after insertion.
516
513
class VSETVLIInfo {
517
514
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.
520
516
const VNInfo *ValNo;
521
517
Register DefReg;
522
518
};
@@ -530,7 +526,7 @@ class VSETVLIInfo {
530
526
AVLIsReg,
531
527
AVLIsImm,
532
528
AVLIsVLMAX,
533
- Unknown, // AVL and VTYPE are fully unknown
529
+ Unknown,
534
530
} State = Uninitialized;
535
531
536
532
// Fields from VTYPE.
@@ -556,7 +552,7 @@ class VSETVLIInfo {
556
552
bool isUnknown () const { return State == Unknown; }
557
553
558
554
void setAVLRegDef (const VNInfo *VNInfo, Register AVLReg) {
559
- assert (AVLReg.isVirtual ());
555
+ assert (VNInfo && AVLReg.isVirtual ());
560
556
AVLRegDef.ValNo = VNInfo;
561
557
AVLRegDef.DefReg = AVLReg;
562
558
State = AVLIsReg;
@@ -586,11 +582,9 @@ class VSETVLIInfo {
586
582
}
587
583
// Most AVLIsReg infos will have a single defining MachineInstr, unless it was
588
584
// 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.
590
586
const MachineInstr *getAVLDefMI (const LiveIntervals *LIS) const {
591
587
assert (hasAVLReg ());
592
- if (!LIS)
593
- return nullptr ;
594
588
auto *MI = LIS->getInstructionFromIndex (getAVLVNInfo ()->def );
595
589
assert (!(getAVLVNInfo ()->isPHIDef () && MI));
596
590
return MI;
@@ -634,15 +628,10 @@ class VSETVLIInfo {
634
628
return (hasNonZeroAVL (LIS) && Other.hasNonZeroAVL (LIS));
635
629
}
636
630
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 ())
643
633
return getAVLVNInfo ()->id == Other.getAVLVNInfo ()->id &&
644
634
getAVLReg () == Other.getAVLReg ();
645
- }
646
635
647
636
if (hasAVLImm () && Other.hasAVLImm ())
648
637
return getAVLImm () == Other.getAVLImm ();
@@ -653,21 +642,6 @@ class VSETVLIInfo {
653
642
return false ;
654
643
}
655
644
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
-
671
645
void setVTYPE (unsigned VType) {
672
646
assert (isValid () && !isUnknown () &&
673
647
" Can't set VTYPE for uninitialized or unknown" );
@@ -767,7 +741,7 @@ class VSETVLIInfo {
767
741
if (Other.isUnknown ())
768
742
return isUnknown ();
769
743
770
- if (!hasSameAVLLatticeValue (Other))
744
+ if (!hasSameAVL (Other))
771
745
return false ;
772
746
773
747
// If the SEWLMULRatioOnly bits are different, then they aren't equal.
@@ -875,7 +849,6 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
875
849
const RISCVSubtarget *ST;
876
850
const TargetInstrInfo *TII;
877
851
MachineRegisterInfo *MRI;
878
- // Possibly null!
879
852
LiveIntervals *LIS;
880
853
881
854
std::vector<BlockData> BlockInfo;
@@ -890,9 +863,9 @@ class RISCVInsertVSETVLI : public MachineFunctionPass {
890
863
void getAnalysisUsage (AnalysisUsage &AU) const override {
891
864
AU.setPreservesCFG ();
892
865
893
- AU.addUsedIfAvailable <LiveIntervals>();
866
+ AU.addRequired <LiveIntervals>();
894
867
AU.addPreserved <LiveIntervals>();
895
- AU.addUsedIfAvailable <SlotIndexes>();
868
+ AU.addRequired <SlotIndexes>();
896
869
AU.addPreserved <SlotIndexes>();
897
870
AU.addPreserved <LiveDebugVariables>();
898
871
AU.addPreserved <LiveStacks>();
@@ -1088,8 +1061,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
1088
1061
.addReg (RISCV::X0, RegState::Kill)
1089
1062
.addImm (Info.encodeVTYPE ())
1090
1063
.addReg (RISCV::VL, RegState::Implicit);
1091
- if (LIS)
1092
- LIS->InsertMachineInstrInMaps (*MI);
1064
+ LIS->InsertMachineInstrInMaps (*MI);
1093
1065
return ;
1094
1066
}
1095
1067
@@ -1106,8 +1078,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
1106
1078
.addReg (RISCV::X0, RegState::Kill)
1107
1079
.addImm (Info.encodeVTYPE ())
1108
1080
.addReg (RISCV::VL, RegState::Implicit);
1109
- if (LIS)
1110
- LIS->InsertMachineInstrInMaps (*MI);
1081
+ LIS->InsertMachineInstrInMaps (*MI);
1111
1082
return ;
1112
1083
}
1113
1084
}
@@ -1119,8 +1090,7 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
1119
1090
.addReg (RISCV::X0, RegState::Define | RegState::Dead)
1120
1091
.addImm (Info.getAVLImm ())
1121
1092
.addImm (Info.encodeVTYPE ());
1122
- if (LIS)
1123
- LIS->InsertMachineInstrInMaps (*MI);
1093
+ LIS->InsertMachineInstrInMaps (*MI);
1124
1094
return ;
1125
1095
}
1126
1096
@@ -1130,10 +1100,8 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
1130
1100
.addReg (DestReg, RegState::Define | RegState::Dead)
1131
1101
.addReg (RISCV::X0, RegState::Kill)
1132
1102
.addImm (Info.encodeVTYPE ());
1133
- if (LIS) {
1134
- LIS->InsertMachineInstrInMaps (*MI);
1135
- LIS->createAndComputeVirtRegInterval (DestReg);
1136
- }
1103
+ LIS->InsertMachineInstrInMaps (*MI);
1104
+ LIS->createAndComputeVirtRegInterval (DestReg);
1137
1105
return ;
1138
1106
}
1139
1107
@@ -1143,14 +1111,12 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB,
1143
1111
.addReg (RISCV::X0, RegState::Define | RegState::Dead)
1144
1112
.addReg (AVLReg)
1145
1113
.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 ());
1154
1120
}
1155
1121
1156
1122
// / Return true if a VSETVLI is required to transition from CurInfo to Require
@@ -1264,14 +1230,10 @@ void RISCVInsertVSETVLI::transferAfter(VSETVLIInfo &Info,
1264
1230
if (RISCV::isFaultFirstLoad (MI)) {
1265
1231
// Update AVL to vl-output of the fault first load.
1266
1232
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 ());
1275
1237
return ;
1276
1238
}
1277
1239
@@ -1365,9 +1327,6 @@ bool RISCVInsertVSETVLI::needVSETVLIPHI(const VSETVLIInfo &Require,
1365
1327
if (!Require.hasAVLReg ())
1366
1328
return true ;
1367
1329
1368
- if (!LIS)
1369
- return true ;
1370
-
1371
1330
// We need the AVL to have been produced by a PHI node in this basic block.
1372
1331
const VNInfo *Valno = Require.getAVLVNInfo ();
1373
1332
if (!Valno->isPHIDef () || LIS->getMBBFromIndex (Valno->def ) != &MBB)
@@ -1443,29 +1402,27 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
1443
1402
MachineOperand &VLOp = MI.getOperand (getVLOpNum (MI));
1444
1403
if (VLOp.isReg ()) {
1445
1404
Register Reg = VLOp.getReg ();
1405
+ LiveInterval &LI = LIS->getInterval (Reg);
1446
1406
1447
1407
// Erase the AVL operand from the instruction.
1448
1408
VLOp.setReg (RISCV::NoRegister);
1449
1409
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 ();
1469
1426
}
1470
1427
}
1471
1428
MI.addOperand (MachineOperand::CreateReg (RISCV::VL, /* isDef*/ false ,
@@ -1522,9 +1479,6 @@ void RISCVInsertVSETVLI::doPRE(MachineBasicBlock &MBB) {
1522
1479
if (!UnavailablePred || !AvailableInfo.isValid ())
1523
1480
return ;
1524
1481
1525
- if (!LIS)
1526
- return ;
1527
-
1528
1482
// If we don't know the exact VTYPE, we can't copy the vsetvli to the exit of
1529
1483
// the unavailable pred.
1530
1484
if (AvailableInfo.hasSEWLMULRatioOnly ())
@@ -1671,7 +1625,7 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
1671
1625
1672
1626
// The def of DefReg moved to MI, so extend the LiveInterval up to
1673
1627
// it.
1674
- if (DefReg.isVirtual () && LIS ) {
1628
+ if (DefReg.isVirtual ()) {
1675
1629
LiveInterval &DefLI = LIS->getInterval (DefReg);
1676
1630
SlotIndex MISlot = LIS->getInstructionIndex (MI).getRegSlot ();
1677
1631
VNInfo *DefVNI = DefLI.getVNInfoAt (DefLI.beginIndex ());
@@ -1700,15 +1654,13 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
1700
1654
1701
1655
if (OldVLReg && OldVLReg.isVirtual ()) {
1702
1656
// NextMI no longer uses OldVLReg so shrink its LiveInterval.
1703
- if (LIS)
1704
- LIS->shrinkToUses (&LIS->getInterval (OldVLReg));
1657
+ LIS->shrinkToUses (&LIS->getInterval (OldVLReg));
1705
1658
1706
1659
MachineInstr *VLOpDef = MRI->getUniqueVRegDef (OldVLReg);
1707
1660
if (VLOpDef && TII->isAddImmediate (*VLOpDef, OldVLReg) &&
1708
1661
MRI->use_nodbg_empty (OldVLReg)) {
1709
1662
VLOpDef->eraseFromParent ();
1710
- if (LIS)
1711
- LIS->removeInterval (OldVLReg);
1663
+ LIS->removeInterval (OldVLReg);
1712
1664
}
1713
1665
}
1714
1666
MI.setDesc (NextMI->getDesc ());
@@ -1724,8 +1676,7 @@ void RISCVInsertVSETVLI::coalesceVSETVLIs(MachineBasicBlock &MBB) const {
1724
1676
1725
1677
NumCoalescedVSETVL += ToDelete.size ();
1726
1678
for (auto *MI : ToDelete) {
1727
- if (LIS)
1728
- LIS->RemoveMachineInstrFromMaps (*MI);
1679
+ LIS->RemoveMachineInstrFromMaps (*MI);
1729
1680
MI->eraseFromParent ();
1730
1681
}
1731
1682
}
@@ -1740,14 +1691,12 @@ void RISCVInsertVSETVLI::insertReadVL(MachineBasicBlock &MBB) {
1740
1691
auto ReadVLMI = BuildMI (MBB, I, MI.getDebugLoc (),
1741
1692
TII->get (RISCV::PseudoReadVL), VLOutput);
1742
1693
// 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;
1751
1700
}
1752
1701
// We don't use the vl output of the VLEFF/VLSEGFF anymore.
1753
1702
MI.getOperand (1 ).setReg (RISCV::X0);
@@ -1765,7 +1714,7 @@ bool RISCVInsertVSETVLI::runOnMachineFunction(MachineFunction &MF) {
1765
1714
1766
1715
TII = ST->getInstrInfo ();
1767
1716
MRI = &MF.getRegInfo ();
1768
- LIS = getAnalysisIfAvailable <LiveIntervals>();
1717
+ LIS = &getAnalysis <LiveIntervals>();
1769
1718
1770
1719
assert (BlockInfo.empty () && " Expect empty block infos" );
1771
1720
BlockInfo.resize (MF.getNumBlockIDs ());
0 commit comments