diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.td b/llvm/lib/Target/RISCV/RISCVInstrInfo.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfo.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.td @@ -155,6 +155,16 @@ let ParserMethod = "parseFenceArg"; } +class UImmOperand : Operand { + let ParserMatchClass = UImmAsmOperand; + let DecoderMethod = "decodeUImmOperand<" # bitsNum # ">"; + let OperandType = "OPERAND_UIMM" # bitsNum; + let OperandNamespace = "RISCVOp"; +} + +class UImmLeafOperand: UImmOperand, + ImmLeaf(Imm);}], bitsNum)>; + def fencearg : Operand { let ParserMatchClass = FenceArg; let PrintMethod = "printFenceArg"; @@ -189,18 +199,15 @@ let OperandNamespace = "RISCVOp"; } -def uimm1 : Operand, ImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<1>; - let DecoderMethod = "decodeUImmOperand<1>"; - let OperandType = "OPERAND_UIMM1"; - let OperandNamespace = "RISCVOp"; +def InsnDirectiveOpcode : AsmOperandClass { + let Name = "InsnDirectiveOpcode"; + let ParserMethod = "parseInsnDirectiveOpcode"; + let RenderMethod = "addImmOperands"; + let PredicateMethod = "isImm"; } -def uimm2 : Operand, ImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<2>; - let DecoderMethod = "decodeUImmOperand<2>"; - let OperandType = "OPERAND_UIMM2"; - let OperandNamespace = "RISCVOp"; +def uimm1 : UImmLeafOperand<1>; +def uimm2 : UImmLeafOperand<2> { let MCOperandPredicate = [{ int64_t Imm; if (!MCOp.evaluateAsConstantImm(Imm)) @@ -208,62 +215,15 @@ return isUInt<2>(Imm); }]; } - -def uimm3 : Operand { - let ParserMatchClass = UImmAsmOperand<3>; - let DecoderMethod = "decodeUImmOperand<3>"; - let OperandType = "OPERAND_UIMM3"; - let OperandNamespace = "RISCVOp"; -} - -def uimm4 : Operand { - let ParserMatchClass = UImmAsmOperand<4>; - let DecoderMethod = "decodeUImmOperand<4>"; - let OperandType = "OPERAND_UIMM4"; - let OperandNamespace = "RISCVOp"; -} - -def uimm5 : Operand, ImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<5>; - let DecoderMethod = "decodeUImmOperand<5>"; - let OperandType = "OPERAND_UIMM5"; - let OperandNamespace = "RISCVOp"; -} - -def InsnDirectiveOpcode : AsmOperandClass { - let Name = "InsnDirectiveOpcode"; - let ParserMethod = "parseInsnDirectiveOpcode"; - let RenderMethod = "addImmOperands"; - let PredicateMethod = "isImm"; -} - -def uimm6 : Operand { - let ParserMatchClass = UImmAsmOperand<6>; - let DecoderMethod = "decodeUImmOperand<6>"; - let OperandType = "OPERAND_UIMM6"; - let OperandNamespace = "RISCVOp"; -} - -def uimm7_opcode : Operand { +def uimm3 : UImmOperand<3>; +def uimm4 : UImmOperand<4>; +def uimm5 : UImmLeafOperand<5>; +def uimm6 : UImmOperand<6>; +def uimm7_opcode : UImmOperand<7> { let ParserMatchClass = InsnDirectiveOpcode; - let DecoderMethod = "decodeUImmOperand<7>"; - let OperandType = "OPERAND_UIMM7"; - let OperandNamespace = "RISCVOp"; -} - -def uimm7 : Operand { - let ParserMatchClass = UImmAsmOperand<7>; - let DecoderMethod = "decodeUImmOperand<7>"; - let OperandType = "OPERAND_UIMM7"; - let OperandNamespace = "RISCVOp"; -} - -def uimm8 : Operand { - let ParserMatchClass = UImmAsmOperand<8>; - let DecoderMethod = "decodeUImmOperand<8>"; - let OperandType = "OPERAND_UIMM8"; - let OperandNamespace = "RISCVOp"; } +def uimm7 : UImmOperand<7>; +def uimm8 : UImmOperand<8>; def simm12 : Operand, ImmLeaf(Imm);}]> { let ParserMatchClass = SImmAsmOperand<12>; diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoXSf.td b/llvm/lib/Target/RISCV/RISCVInstrInfoXSf.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfoXSf.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoXSf.td @@ -29,26 +29,12 @@ // The payload and tsimm5 operands are all marked as ImmArg in the IR // intrinsic and will be target constant, so use TImmLeaf rather than ImmLeaf. -def payload1 : Operand, TImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<1>; - let DecoderMethod = "decodeUImmOperand<1>"; - let OperandType = "OPERAND_UIMM1"; - let OperandNamespace = "RISCVOp"; -} - -def payload2 : Operand, TImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<2>; - let DecoderMethod = "decodeUImmOperand<2>"; - let OperandType = "OPERAND_UIMM2"; - let OperandNamespace = "RISCVOp"; -} +class TImmLeafOperand: UImmOperand, + TImmLeaf(Imm);}], bitsNum)>; -def payload5 : Operand, TImmLeaf(Imm);}]> { - let ParserMatchClass = UImmAsmOperand<5>; - let DecoderMethod = "decodeUImmOperand<5>"; - let OperandType = "OPERAND_UIMM5"; - let OperandNamespace = "RISCVOp"; -} +def payload1 : TImmLeafOperand<1>; +def payload2 : TImmLeafOperand<2>; +def payload5 : TImmLeafOperand<5>; def tsimm5 : Operand, TImmLeaf(Imm);}]> { let ParserMatchClass = SImmAsmOperand<5>; diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZb.td @@ -171,41 +171,22 @@ SDLoc(N), N->getValueType(0)); }]>; -def C3LeftShift : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - return C > 3 && (C >> llvm::countr_zero(C)) == 3; -}]>; - -def C5LeftShift : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - return C > 5 && (C >> llvm::countr_zero(C)) == 5; -}]>; - -def C9LeftShift : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - return C > 9 && (C >> llvm::countr_zero(C)) == 9; -}]>; - -// Constant of the form (3 << C) where C is less than 32. -def C3LeftShiftUW : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - unsigned Shift = llvm::countr_zero(C); - return 1 <= Shift && Shift < 32 && (C >> Shift) == 3; -}]>; +foreach i = [3, 5, 9] in { + def C # i # LeftShift : PatLeaf<(imm), !format([{ + uint64_t C = N->getZExtValue(); + return C > {0} && (C >> llvm::countr_zero(C)) == {0}; + }], i)>; +} -// Constant of the form (5 << C) where C is less than 32. -def C5LeftShiftUW : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - unsigned Shift = llvm::countr_zero(C); - return 1 <= Shift && Shift < 32 && (C >> Shift) == 5; -}]>; +// Constant of the form (i << C) where C is less than 32. +foreach i = [3, 5, 9] in { + def C # i # LeftShiftUW : PatLeaf<(imm), !format([{ + uint64_t C = N->getZExtValue(); + unsigned Shift = llvm::countr_zero(C); + return 1 <= Shift && Shift < 32 && (C >> Shift) == {0}; + }], i)>; +} -// Constant of the form (9 << C) where C is less than 32. -def C9LeftShiftUW : PatLeaf<(imm), [{ - uint64_t C = N->getZExtValue(); - unsigned Shift = llvm::countr_zero(C); - return 1 <= Shift && Shift < 32 && (C >> Shift) == 9; -}]>; def CSImm12MulBy4 : PatLeaf<(imm), [{ if (!N->hasOneUse())