@@ -542,11 +542,29 @@ static bool isScalarMoveInstr(const MachineInstr &MI) {
542542 }
543543}
544544
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+
545565static VSETVLIInfo computeInfoForInstr (const MachineInstr &MI, uint64_t TSFlags,
546566 const MachineRegisterInfo *MRI) {
547567 VSETVLIInfo InstrInfo;
548- unsigned NumOperands = MI.getNumExplicitOperands ();
549- bool HasPolicy = RISCVII::hasVecPolicyOp (TSFlags);
550568
551569 // If the instruction has policy argument, use the argument.
552570 // 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,
561579 // vsetvli between mask and nomasked instruction sequence.
562580 bool MaskAgnostic = UsesMaskPolicy;
563581 unsigned UseOpIdx;
564- if (HasPolicy ) {
582+ if (RISCVII::hasVecPolicyOp (TSFlags) ) {
565583 const MachineOperand &Op = MI.getOperand (MI.getNumExplicitOperands () - 1 );
566584 uint64_t Policy = Op.getImm ();
567585 assert (Policy <= (RISCVII::TAIL_AGNOSTIC | RISCVII::MASK_AGNOSTIC) &&
@@ -593,13 +611,9 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
593611 TailAgnostic = true ;
594612 }
595613
596- // Remove the tail policy so we can find the SEW and VL.
597- if (HasPolicy)
598- --NumOperands;
599-
600614 RISCVII::VLMUL VLMul = RISCVII::getLMul (TSFlags);
601615
602- unsigned Log2SEW = MI.getOperand (NumOperands - 1 ).getImm ();
616+ unsigned Log2SEW = MI.getOperand (getSEWOpNum (MI) ).getImm ();
603617 // A Log2SEW of 0 is an operation on mask registers only.
604618 bool MaskRegOp = Log2SEW == 0 ;
605619 unsigned SEW = Log2SEW ? 1 << Log2SEW : 8 ;
@@ -611,7 +625,7 @@ static VSETVLIInfo computeInfoForInstr(const MachineInstr &MI, uint64_t TSFlags,
611625 bool ScalarMovOp = isScalarMoveInstr (MI);
612626
613627 if (RISCVII::hasVLOp (TSFlags)) {
614- const MachineOperand &VLOp = MI.getOperand (NumOperands - 2 );
628+ const MachineOperand &VLOp = MI.getOperand (getVLOpNum (MI) );
615629 if (VLOp.isImm ()) {
616630 int64_t Imm = VLOp.getImm ();
617631 // Conver the VLMax sentintel to X0 register.
@@ -1103,11 +1117,7 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) {
11031117 if (RISCVII::hasSEWOp (TSFlags)) {
11041118 VSETVLIInfo NewInfo = computeInfoForInstr (MI, TSFlags, MRI);
11051119 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));
11111121 if (VLOp.isReg ()) {
11121122 // Erase the AVL operand from the instruction.
11131123 VLOp.setReg (RISCV::NoRegister);
0 commit comments