[llvm] Support replacing `add rd, Zero, Zero` with `c.li rd, 0` (PR #86937)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 23 08:05:57 PDT 2024
https://github.com/MarkGoncharovAl updated https://github.com/llvm/llvm-project/pull/86937
>From 1c56bd1737eff32c4262104a8fd99fc4c2bc0a98 Mon Sep 17 00:00:00 2001
From: Mark Goncharov <mark.goncharov at syntacore.com>
Date: Thu, 28 Mar 2024 12:49:41 +0300
Subject: [PATCH] Support replacing `add rd, Zero, Zero` with `c.li rd, 0`
---
llvm/lib/Target/RISCV/RISCVInstrInfoC.td | 30 ++++++++
llvm/test/MC/RISCV/compress-rv32i.s | 96 ++++++++++++++++++++++++
2 files changed, 126 insertions(+)
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoC.td b/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
index 18d38348f7214f..0fac2ad9dfa9e1 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoC.td
@@ -915,8 +915,16 @@ def : CompressPat<(LUI GPRNoX0X2:$rd, c_lui_imm:$imm),
(C_LUI GPRNoX0X2:$rd, c_lui_imm:$imm)>;
def : CompressPat<(SRLI GPRC:$rs1, GPRC:$rs1, uimmlog2xlennonzero:$imm),
(C_SRLI GPRC:$rs1, uimmlog2xlennonzero:$imm)>;
+def : CompressPat<(SRL GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(SRL GPRNoX0:$rs1, X0, GPR),
+ (C_LI GPRNoX0:$rs1, 0)>;
def : CompressPat<(SRAI GPRC:$rs1, GPRC:$rs1, uimmlog2xlennonzero:$imm),
(C_SRAI GPRC:$rs1, uimmlog2xlennonzero:$imm)>;
+def : CompressPat<(SRA GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(SRA GPRNoX0:$rs1, X0, GPR),
+ (C_LI GPRNoX0:$rs1, 0)>;
def : CompressPat<(ANDI GPRC:$rs1, GPRC:$rs1, simm6:$imm),
(C_ANDI GPRC:$rs1, simm6:$imm)>;
def : CompressPat<(SUB GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
@@ -926,8 +934,20 @@ def : CompressPat<(XOR GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
let isCompressOnly = true in
def : CompressPat<(XOR GPRC:$rs1, GPRC:$rs2, GPRC:$rs1),
(C_XOR GPRC:$rs1, GPRC:$rs2)>;
+def : CompressPat<(XOR GPRNoX0:$rs1, X0, GPRNoX0:$rs2),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(XOR GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(XOR GPRNoX0:$rs1, X0, X0),
+ (C_LI GPRNoX0:$rs1, 0)>;
def : CompressPat<(OR GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
(C_OR GPRC:$rs1, GPRC:$rs2)>;
+def : CompressPat<(OR GPRNoX0:$rs1, X0, GPRNoX0:$rs2),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(OR GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(OR GPRNoX0:$rs1, X0, X0),
+ (C_LI GPRNoX0:$rs1, 0)>;
let isCompressOnly = true in
def : CompressPat<(OR GPRC:$rs1, GPRC:$rs2, GPRC:$rs1),
(C_OR GPRC:$rs1, GPRC:$rs2)>;
@@ -936,6 +956,10 @@ def : CompressPat<(AND GPRC:$rs1, GPRC:$rs1, GPRC:$rs2),
let isCompressOnly = true in
def : CompressPat<(AND GPRC:$rs1, GPRC:$rs2, GPRC:$rs1),
(C_AND GPRC:$rs1, GPRC:$rs2)>;
+def : CompressPat<(AND GPRNoX0:$rs1, X0, GPR),
+ (C_LI GPRNoX0:$rs1, 0)>;
+def : CompressPat<(AND GPRNoX0:$rs1, GPR, X0),
+ (C_LI GPRNoX0:$rs1, 0)>;
} // Predicates = [HasStdExtCOrZca]
let Predicates = [HasStdExtCOrZca, IsRV64] in {
@@ -970,6 +994,10 @@ def : CompressPat<(BNE X0, GPRC:$rs1, simm9_lsb0:$imm),
let Predicates = [HasStdExtCOrZca] in {
def : CompressPat<(SLLI GPRNoX0:$rs1, GPRNoX0:$rs1, uimmlog2xlennonzero:$imm),
(C_SLLI GPRNoX0:$rs1, uimmlog2xlennonzero:$imm)>;
+def : CompressPat<(SLL GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
+ (C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
+def : CompressPat<(SLL GPRNoX0:$rs1, X0, GPR),
+ (C_LI GPRNoX0:$rs1, 0)>;
} // Predicates = [HasStdExtCOrZca]
let Predicates = [HasStdExtCOrZcd, HasStdExtD] in {
@@ -996,6 +1024,8 @@ let Predicates = [HasStdExtCOrZca] in {
def : CompressPat<(JALR X0, GPRNoX0:$rs1, 0),
(C_JR GPRNoX0:$rs1)>;
let isCompressOnly = true in {
+def : CompressPat<(ADD GPRNoX0:$rs1, X0, X0),
+ (C_LI GPRNoX0:$rs1, 0)>;
def : CompressPat<(ADD GPRNoX0:$rs1, X0, GPRNoX0:$rs2),
(C_MV GPRNoX0:$rs1, GPRNoX0:$rs2)>;
def : CompressPat<(ADD GPRNoX0:$rs1, GPRNoX0:$rs2, X0),
diff --git a/llvm/test/MC/RISCV/compress-rv32i.s b/llvm/test/MC/RISCV/compress-rv32i.s
index b4fd72a0f81c4d..42c829a33f58b5 100644
--- a/llvm/test/MC/RISCV/compress-rv32i.s
+++ b/llvm/test/MC/RISCV/compress-rv32i.s
@@ -202,6 +202,12 @@ lw ra, 252(sp)
# CHECK: # encoding: [0x82,0x80]
jalr zero, 0(ra)
+# CHECK-BYTES: 81 40
+# CHECK-ALIAS: li ra, 0
+# CHECK-INST: c.li ra, 0
+# CHECK: # encoding: [0x81,0x40]
+add ra, zero, zero
+
# CHECK-BYTES: 92 80
# CHECK-ALIAS: mv ra, tp
# CHECK-INST: c.mv ra, tp
@@ -249,3 +255,93 @@ sw zero, 252(sp)
# CHECK-INST: c.unimp
# CHECK: # encoding: [0x00,0x00]
unimp
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+and s0, a5, x0
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+and s0, x0, a5
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+and s0, x0, x0
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+or s0, a5, x0
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+or s0, x0, a5
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+or s0, x0, x0
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+xor s0, a5, x0
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+xor s0, x0, a5
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+xor s0, x0, x0
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+sll s0, a5, x0
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+sll s0, x0, a5
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+srl s0, a5, x0
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+srl s0, x0, a5
+
+# CHECK-BYTES: 3e 84
+# CHECK-ALIAS: mv s0, a5
+# CHECK-INST: c.mv s0, a5
+# CHECK: # encoding: [0x3e,0x84]
+sra s0, a5, x0
+
+# CHECK-BYTES: 01 44
+# CHECK-ALIAS: li s0, 0
+# CHECK-INST: c.li s0, 0
+# CHECK: # encoding: [0x01,0x44]
+sra s0, x0, a5
More information about the llvm-commits
mailing list