[RISCV] Verify VL operand on instructions if present
authorPhilip Reames <preames@rivosinc.com>
Thu, 15 Sep 2022 19:47:58 +0000 (12:47 -0700)
committerPhilip Reames <listmail@philipreames.com>
Thu, 15 Sep 2022 20:06:52 +0000 (13:06 -0700)
These should only be immediate values or GPR registers.

Differential Revision: https://reviews.llvm.org/D133953

llvm/lib/Target/RISCV/RISCVInstrInfo.cpp

index a98bd5a..e205a89 100644 (file)
@@ -1216,6 +1216,21 @@ bool RISCVInstrInfo::verifyInstruction(const MachineInstr &MI,
   }
 
   const uint64_t TSFlags = Desc.TSFlags;
+  if (RISCVII::hasVLOp(TSFlags)) {
+    const MachineOperand &Op = MI.getOperand(RISCVII::getVLOpNum(Desc));
+    if (!Op.isImm() && !Op.isReg())  {
+      ErrInfo = "Invalid operand type for VL operand";
+      return false;
+    }
+    if (Op.isReg() && Op.getReg() != RISCV::NoRegister) {
+      const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
+      auto *RC = MRI.getRegClass(Op.getReg());
+      if (!RISCV::GPRRegClass.hasSubClassEq(RC)) {
+        ErrInfo = "Invalid register class for VL operand";
+        return false;
+      }
+    }
+  }
   if (RISCVII::hasSEWOp(TSFlags)) {
     unsigned OpIdx = RISCVII::getSEWOpNum(Desc);
     uint64_t Log2SEW = MI.getOperand(OpIdx).getImm();