[llvm] [RISCV] Add CompressPat for c.beqz/bnez with X0 in the first operand. (PR #69042)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 13 21:15:44 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-risc-v
Author: Craig Topper (topperc)
<details>
<summary>Changes</summary>
Copy propagation during codegen can cause the first operand to become X0. We should be able to compress that.
---
Full diff: https://github.com/llvm/llvm-project/pull/69042.diff
2 Files Affected:
- (modified) llvm/lib/Target/RISCV/RISCVInstrInfoC.td (+6)
- (modified) llvm/test/MC/RISCV/compress-rv32i.s (+24-4)
``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoC.td b/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
index aff6e77e0cfc480..07137031d9fc712 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
@@ -956,8 +956,14 @@ def : CompressPat<(JAL X0, simm12_lsb0:$offset),
(C_J simm12_lsb0:$offset)>;
def : CompressPat<(BEQ GPRC:$rs1, X0, simm9_lsb0:$imm),
(C_BEQZ GPRC:$rs1, simm9_lsb0:$imm)>;
+let isCompressOnly = true in
+def : CompressPat<(BEQ X0, GPRC:$rs1, simm9_lsb0:$imm),
+ (C_BEQZ GPRC:$rs1, simm9_lsb0:$imm)>;
def : CompressPat<(BNE GPRC:$rs1, X0, simm9_lsb0:$imm),
(C_BNEZ GPRC:$rs1, simm9_lsb0:$imm)>;
+let isCompressOnly = true in
+def : CompressPat<(BNE X0, GPRC:$rs1, simm9_lsb0:$imm),
+ (C_BNEZ GPRC:$rs1, simm9_lsb0:$imm)>;
} // Predicates = [HasStdExtCOrZca]
// Quadrant 2
diff --git a/llvm/test/MC/RISCV/compress-rv32i.s b/llvm/test/MC/RISCV/compress-rv32i.s
index 7869481bb78d690..165defc3435fcc9 100644
--- a/llvm/test/MC/RISCV/compress-rv32i.s
+++ b/llvm/test/MC/RISCV/compress-rv32i.s
@@ -154,16 +154,36 @@ jal zero, -2048
# CHECK: # encoding: [0x01,0xd0]
beq s0, zero, -256
+# CHECK-BYTES: 01 d0
+# CHECK-ALIASASM: beqz s0, -256
+# CHECK-ALIASOBJ32: beqz s0, 0xffffff2a
+# CHECK-ALIASOBJ64: beqz s0, 0xffffffffffffff2a
+# CHECK-INSTASM: c.beqz s0, -256
+# CHECK-INSTOBJ32: c.beqz s0, 0xffffff2a
+# CHECK-INSTOBJ64: c.beqz s0, 0xffffffffffffff2a
+# CHECK: # encoding: [0x01,0xd0]
+beq zero, s0, -256
+
# CHECK-BYTES: 7d ec
# CHECK-ALIASASM: bnez s0, 254
-# CHECK-ALIASOBJ32: bnez s0, 0x128
-# CHECK-ALIASOBJ64: bnez s0, 0x128
+# CHECK-ALIASOBJ32: bnez s0, 0x12a
+# CHECK-ALIASOBJ64: bnez s0, 0x12a
# CHECK-INSTASM: c.bnez s0, 254
-# CHECK-INSTOBJ32: c.bnez s0, 0x128
-# CHECK-INSTOBJ64: c.bnez s0, 0x128
+# CHECK-INSTOBJ32: c.bnez s0, 0x12a
+# CHECK-INSTOBJ64: c.bnez s0, 0x12a
# CHECK: # encoding: [0x7d,0xec]
bne s0, zero, 254
+# CHECK-BYTES: 7d ec
+# CHECK-ALIASASM: bnez s0, 254
+# CHECK-ALIASOBJ32: bnez s0, 0x12c
+# CHECK-ALIASOBJ64: bnez s0, 0x12c
+# CHECK-INSTASM: c.bnez s0, 254
+# CHECK-INSTOBJ32: c.bnez s0, 0x12c
+# CHECK-INSTOBJ64: c.bnez s0, 0x12c
+# CHECK: # encoding: [0x7d,0xec]
+bne zero, s0, 254
+
# CHECK-BYTES: 7e 04
# CHECK-ALIAS: slli s0, s0, 31
# CHECK-INST: c.slli s0, 31
``````````
</details>
https://github.com/llvm/llvm-project/pull/69042
More information about the llvm-commits
mailing list