@@ -542,11 +542,29 @@ static bool isScalarMoveInstr(const MachineInstr &MI) {
542
542
}
543
543
}
544
544
545
+ static unsigned getVLOpNum (const MachineInstr &MI) {
546
+ const uint64_t TSFlags = MI.getDesc ().TSFlags ;
547
+ // This method is only called if we expect to have a VL operand, and all
548
+ // instructions with VL also have SEW.
549
+ assert (RISCVII::hasSEWOp (TSFlags) && RISCVII::hasVLOp (TSFlags));
550
+ unsigned Offset = 2 ;
551
+ if (RISCVII::hasVecPolicyOp (TSFlags))
552
+ Offset = 3 ;
553
+ return MI.getNumExplicitOperands () - Offset;
554
+ }
555
+
556
+ static unsigned getSEWOpNum (const MachineInstr &MI) {
557
+ const uint64_t TSFlags = MI.getDesc ().TSFlags ;
558
+ assert (RISCVII::hasSEWOp (TSFlags));
559
+ unsigned Offset = 1 ;
560
+ if (RISCVII::hasVecPolicyOp (TSFlags))
561
+ Offset = 2 ;
562
+ return MI.getNumExplicitOperands () - Offset;
563
+ }
564
+
545
565
static VSETVLIInfo computeInfoForInstr (const MachineInstr &MI, uint64_t TSFlags,
546
566
const MachineRegisterInfo *MRI) {
547
567
VSETVLIInfo InstrInfo;
548
- unsigned NumOperands = MI.getNumExplicitOperands ();
549
- bool HasPolicy = RISCVII::hasVecPolicyOp (TSFlags);
550
568
551
569
// If the instruction has policy argument, use the argument.
552
570
// If there is no policy argument, default to tail agnostic unless the
@@ -561,7 +579,7 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
561
579
// vsetvli between mask and nomasked instruction sequence.
562
580
bool MaskAgnostic = UsesMaskPolicy;
563
581
unsigned UseOpIdx;
564
- if (HasPolicy ) {
582
+ if (RISCVII::hasVecPolicyOp (TSFlags) ) {
565
583
const MachineOperand &Op = MI.getOperand (MI.getNumExplicitOperands () - 1 );
566
584
uint64_t Policy = Op.getImm ();
567
585
assert (Policy <= (RISCVII::TAIL_AGNOSTIC | RISCVII::MASK_AGNOSTIC) &&
@@ -593,13 +611,9 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
593
611
TailAgnostic = true ;
594
612
}
595
613
596
- // Remove the tail policy so we can find the SEW and VL.
597
- if (HasPolicy)
598
- --NumOperands;
599
-
600
614
RISCVII::VLMUL VLMul = RISCVII::getLMul (TSFlags);
601
615
602
- unsigned Log2SEW = MI.getOperand (NumOperands - 1 ).getImm ();
616
+ unsigned Log2SEW = MI.getOperand (getSEWOpNum (MI) ).getImm ();
603
617
// A Log2SEW of 0 is an operation on mask registers only.
604
618
bool MaskRegOp = Log2SEW == 0 ;
605
619
unsigned SEW = Log2SEW ? 1 << Log2SEW : 8 ;
@@ -611,7 +625,7 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
611
625
bool ScalarMovOp = isScalarMoveInstr (MI);
612
626
613
627
if (RISCVII::hasVLOp (TSFlags)) {
614
- const MachineOperand &VLOp = MI.getOperand (NumOperands - 2 );
628
+ const MachineOperand &VLOp = MI.getOperand (getVLOpNum (MI) );
615
629
if (VLOp.isImm ()) {
616
630
int64_t Imm = VLOp.getImm ();
617
631
// Conver the VLMax sentintel to X0 register.
@@ -1103,11 +1117,7 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
1103
1117
if (RISCVII::hasSEWOp (TSFlags)) {
1104
1118
VSETVLIInfo NewInfo = computeInfoForInstr (MI, TSFlags, MRI);
1105
1119
if (RISCVII::hasVLOp (TSFlags)) {
1106
- unsigned Offset = 2 ;
1107
- if (RISCVII::hasVecPolicyOp (TSFlags))
1108
- Offset = 3 ;
1109
- MachineOperand &VLOp =
1110
- MI.getOperand (MI.getNumExplicitOperands () - Offset);
1120
+ MachineOperand &VLOp = MI.getOperand (getVLOpNum (MI));
1111
1121
if (VLOp.isReg ()) {
1112
1122
// Erase the AVL operand from the instruction.
1113
1123
VLOp.setReg (RISCV::NoRegister);
0 commit comments