[llvm] [TableGen][RISCV] Support sub-operands in CompressInstEmitter.cpp. (PR #133039)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 27 10:59:14 PDT 2025


================
@@ -0,0 +1,126 @@
+// RUN: llvm-tblgen -gen-compress-inst-emitter -I %p/../../../include %s 2>&1 | FileCheck %s
+
+include "llvm/Target/Target.td"
+
+def ArchInstrInfo : InstrInfo { }
+def ArchAsmWriter : AsmWriter {
+  int PassSubtarget = 1;
+}
+
+def Arch : Target {
+  let InstructionSet = ArchInstrInfo;
+  let AssemblyWriters = [ArchAsmWriter];
+}
+
+def Reg0 : Register<"reg0"> {
+  let HWEncoding{4-0} = 0;
+}
+def Reg1 : Register<"reg1"> {
+  let HWEncoding{4-0} = 1;
+}
+
+def Regs : RegisterClass<"Arch", [i32], 32, (add Reg0, Reg1)>;
+def RegsC : RegisterClass<"Arch", [i32], 32, (sub Regs, Reg0)>;
+
+def simm6 : Operand<i32>, ImmLeaf<i32, [{return isInt<6>(Imm);}]> {
+  let MCOperandPredicate = [{
+    int64_t Imm;
+    if (!MCOp.evaluateAsConstantImm(Imm))
+      return false;
+    return isInt<6>(Imm);
+  }];
+}
+
+def simm12 : Operand<i32>, ImmLeaf<i32, [{return isInt<12>(Imm);}]> {
+  let MCOperandPredicate = [{
+    int64_t Imm;
+    if (!MCOp.evaluateAsConstantImm(Imm))
+      return false;
+    return isInt<12>(Imm);
+  }];
+}
+
+def MemOpnd : Operand<iPTR> {
+  let MIOperandInfo = (ops Regs, simm12);
+}
+
+def MemOpndC : Operand<iPTR> {
+  let MIOperandInfo = (ops RegsC, simm6);
+}
+
+def BigInst : Instruction {
+  let Namespace = "MyNS";
+  let OutOperandList = (outs Regs:$dst);
+  let InOperandList = (ins MemOpnd:$addr);
+  let Size = 4;
+  let AsmString = "big $dst, $addr";
+}
+
+def SmallInst : Instruction {
+  let Namespace = "MyNS";
+  let OutOperandList = (outs RegsC:$dst);
+  let InOperandList = (ins MemOpndC:$addr);
+  let Size = 2;
+  let AsmString = "small $dst, $addr";
+}
+
+def : CompressPat<(BigInst RegsC:$dst, RegsC:$rs, simm6:$imm),
+                  (SmallInst RegsC:$dst, RegsC:$rs, simm6:$imm)>;
+
----------------
mshockwave wrote:

this patch allows a normal instruction with sub-operands to be compressed into an instruction without any sub-operands, right? Could you add a test for that as well?

https://github.com/llvm/llvm-project/pull/133039


More information about the llvm-commits mailing list