@@ -781,6 +781,25 @@ char RISCVInsertVSETVLI::ID = 0;
781
781
INITIALIZE_PASS (RISCVInsertVSETVLI, DEBUG_TYPE, RISCV_INSERT_VSETVLI_NAME,
782
782
false , false )
783
783
784
+ // Return a VSETVLIInfo representing the changes made by this VSETVLI or
785
+ // VSETIVLI instruction.
786
+ static VSETVLIInfo getInfoForVSETVLI(const MachineInstr &MI) {
787
+ VSETVLIInfo NewInfo;
788
+ if (MI.getOpcode () == RISCV::PseudoVSETIVLI) {
789
+ NewInfo.setAVLImm (MI.getOperand (1 ).getImm ());
790
+ } else {
791
+ assert (MI.getOpcode () == RISCV::PseudoVSETVLI ||
792
+ MI.getOpcode () == RISCV::PseudoVSETVLIX0);
793
+ Register AVLReg = MI.getOperand (1 ).getReg ();
794
+ assert ((AVLReg != RISCV::X0 || MI.getOperand (0 ).getReg () != RISCV::X0) &&
795
+ " Can't handle X0, X0 vsetvli yet" );
796
+ NewInfo.setAVLReg (AVLReg);
797
+ }
798
+ NewInfo.setVTYPE (MI.getOperand (2 ).getImm ());
799
+
800
+ return NewInfo;
801
+ }
802
+
784
803
static VSETVLIInfo computeInfoForInstr (const MachineInstr &MI, uint64_t TSFlags,
785
804
const MachineRegisterInfo *MRI) {
786
805
VSETVLIInfo InstrInfo;
@@ -841,6 +860,21 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
841
860
#endif
842
861
InstrInfo.setVTYPE (VLMul, SEW, TailAgnostic, MaskAgnostic);
843
862
863
+ // If AVL is defined by a vsetvli with the same VLMAX, we can replace the
864
+ // AVL operand with the AVL of the defining vsetvli. We avoid general
865
+ // register AVLs to avoid extending live ranges without being sure we can
866
+ // kill the original source reg entirely.
867
+ if (InstrInfo.hasAVLReg () && InstrInfo.getAVLReg ().isVirtual ()) {
868
+ MachineInstr *DefMI = MRI->getVRegDef (InstrInfo.getAVLReg ());
869
+ if (DefMI && isVectorConfigInstr (*DefMI)) {
870
+ VSETVLIInfo DefInstrInfo = getInfoForVSETVLI (*DefMI);
871
+ if (DefInstrInfo.hasSameVLMAX (InstrInfo) &&
872
+ (DefInstrInfo.hasAVLImm () || DefInstrInfo.getAVLReg () == RISCV::X0)) {
873
+ InstrInfo.setAVL (DefInstrInfo);
874
+ }
875
+ }
876
+ }
877
+
844
878
return InstrInfo;
845
879
}
846
880
@@ -851,25 +885,6 @@ void RISCVInsertVSETVLI::insertVSETVLI(MachineBasicBlock &MBB, MachineInstr &MI,
851
885
insertVSETVLI (MBB, MachineBasicBlock::iterator (&MI), DL, Info, PrevInfo);
852
886
}
853
887
854
- // Return a VSETVLIInfo representing the changes made by this VSETVLI or
855
- // VSETIVLI instruction.
856
- static VSETVLIInfo getInfoForVSETVLI (const MachineInstr &MI) {
857
- VSETVLIInfo NewInfo;
858
- if (MI.getOpcode () == RISCV::PseudoVSETIVLI) {
859
- NewInfo.setAVLImm (MI.getOperand (1 ).getImm ());
860
- } else {
861
- assert (MI.getOpcode () == RISCV::PseudoVSETVLI ||
862
- MI.getOpcode () == RISCV::PseudoVSETVLIX0);
863
- Register AVLReg = MI.getOperand (1 ).getReg ();
864
- assert ((AVLReg != RISCV::X0 || MI.getOperand (0 ).getReg () != RISCV::X0) &&
865
- " Can't handle X0, X0 vsetvli yet" );
866
- NewInfo.setAVLReg (AVLReg);
867
- }
868
- NewInfo.setVTYPE (MI.getOperand (2 ).getImm ());
869
-
870
- return NewInfo;
871
- }
872
-
873
888
void RISCVInsertVSETVLI::insertVSETVLI (MachineBasicBlock &MBB,
874
889
MachineBasicBlock::iterator InsertPt, DebugLoc DL,
875
890
const VSETVLIInfo &Info, const VSETVLIInfo &PrevInfo) {
@@ -1065,27 +1080,8 @@ void RISCVInsertVSETVLI::transferBefore(VSETVLIInfo &Info,
1065
1080
// to prevent extending live range of an avl register operand.
1066
1081
// TODO: We can probably relax this for immediates.
1067
1082
if (Demanded.VLZeroness && !Demanded.VLAny && PrevInfo.isValid () &&
1068
- PrevInfo.hasEquallyZeroAVL (Info, *MRI) && Info.hasSameVLMAX (PrevInfo)) {
1083
+ PrevInfo.hasEquallyZeroAVL (Info, *MRI) && Info.hasSameVLMAX (PrevInfo))
1069
1084
Info.setAVL (PrevInfo);
1070
- return ;
1071
- }
1072
-
1073
- // If AVL is defined by a vsetvli with the same VLMAX, we can
1074
- // replace the AVL operand with the AVL of the defining vsetvli.
1075
- // We avoid general register AVLs to avoid extending live ranges
1076
- // without being sure we can kill the original source reg entirely.
1077
- if (!Info.hasAVLReg () || !Info.getAVLReg ().isVirtual ())
1078
- return ;
1079
- MachineInstr *DefMI = MRI->getVRegDef (Info.getAVLReg ());
1080
- if (!DefMI || !isVectorConfigInstr (*DefMI))
1081
- return ;
1082
-
1083
- VSETVLIInfo DefInfo = getInfoForVSETVLI (*DefMI);
1084
- if (DefInfo.hasSameVLMAX (Info) &&
1085
- (DefInfo.hasAVLImm () || DefInfo.getAVLReg () == RISCV::X0)) {
1086
- Info.setAVL (DefInfo);
1087
- return ;
1088
- }
1089
1085
}
1090
1086
1091
1087
// Given a state with which we evaluated MI (see transferBefore above for why
0 commit comments