[llvm] 528831d - [CSKY] Optimize ANDI/ORI to BSETI/BCLRI for specific immediates
Ben Shi via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 4 01:11:45 PDT 2023
Author: Ben Shi
Date: 2023-08-04T16:10:36+08:00
New Revision: 528831dd1ada271d1ae52eef2803ba0996efc324
URL: https://github.com/llvm/llvm-project/commit/528831dd1ada271d1ae52eef2803ba0996efc324
DIFF: https://github.com/llvm/llvm-project/commit/528831dd1ada271d1ae52eef2803ba0996efc324.diff
LOG: [CSKY] Optimize ANDI/ORI to BSETI/BCLRI for specific immediates
Reviewed By: zixuan-wu
Differential Revision: https://reviews.llvm.org/D153614
Added:
Modified:
llvm/lib/Target/CSKY/CSKYInstrInfo.td
llvm/test/CodeGen/CSKY/bseti_bclri.ll
Removed:
################################################################################
diff --git a/llvm/lib/Target/CSKY/CSKYInstrInfo.td b/llvm/lib/Target/CSKY/CSKYInstrInfo.td
index 70b83c6392ce95..2e475d1f1b6d01 100644
--- a/llvm/lib/Target/CSKY/CSKYInstrInfo.td
+++ b/llvm/lib/Target/CSKY/CSKYInstrInfo.td
@@ -158,6 +158,30 @@ def uimm_shift : Operand<i32>, ImmLeaf<i32, "return isUInt<2>(Imm);"> {
let DecoderMethod = "decodeImmShiftOpValue";
}
+// Optimize (or x, imm) to (BSETI x, log2(imm)). We should exclude the
+// case can be opimized to (ORI32/ORI16 x, imm).
+def imm32_1_pop_bit_XFORM : SDNodeXForm<imm, [{
+ uint32_t I = N->getZExtValue();
+ return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
+ N->getValueType(0));
+}]>;
+def imm32_1_pop_bit : PatLeaf<(imm), [{
+ uint32_t I = N->getZExtValue();
+ return llvm::popcount(I) == 1 && I > 0xffff;
+}]>;
+
+// Optimize (and x, imm) to (BCLRI x, log2(~imm)). We should exclude the
+// case can be opimized to (ANDNI x, ~imm).
+def imm32_1_zero_bit_XFORM : SDNodeXForm<imm, [{
+ uint32_t I = ~N->getZExtValue();
+ return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
+ N->getValueType(0));
+}]>;
+def imm32_1_zero_bit : PatLeaf<(imm), [{
+ uint32_t I = ~N->getZExtValue();
+ return llvm::popcount(I) == 1 && I > 0xfff;
+}]>;
+
def CSKYSymbol : AsmOperandClass {
let Name = "CSKYSymbol";
let RenderMethod = "addImmOperands";
@@ -1422,6 +1446,14 @@ let Predicates = [iHasE2] in
def : Pat<(i32 imm:$imm),
(ORI32 (MOVIH32 (uimm32_hi16 imm:$imm)), (uimm32_lo16 imm:$imm))>;
+// Bit operations.
+let Predicates = [iHasE2] in {
+ def : Pat<(or GPR:$rs, imm32_1_pop_bit:$imm),
+ (BSETI32 GPR:$rs, (imm32_1_pop_bit_XFORM imm32_1_pop_bit:$imm))>;
+ def : Pat<(and GPR:$rs, imm32_1_zero_bit:$imm),
+ (BCLRI32 GPR:$rs, (imm32_1_zero_bit_XFORM imm32_1_zero_bit:$imm))>;
+}
+
// Other operations.
let Predicates = [iHasE2] in {
def : Pat<(rotl GPR:$rs1, GPR:$rs2),
diff --git a/llvm/test/CodeGen/CSKY/bseti_bclri.ll b/llvm/test/CodeGen/CSKY/bseti_bclri.ll
index fcf9b4efa3ed62..faade372f57c35 100644
--- a/llvm/test/CodeGen/CSKY/bseti_bclri.ll
+++ b/llvm/test/CodeGen/CSKY/bseti_bclri.ll
@@ -13,8 +13,7 @@ define i32 @test_or_128(i32 noundef %0) {
define i32 @test_or_131072(i32 noundef %0) {
; CHECK-LABEL: test_or_131072:
; CHECK: # %bb.0:
-; CHECK-NEXT: movih32 a1, 2
-; CHECK-NEXT: or16 a0, a1
+; CHECK-NEXT: bseti16 a0, 17
; CHECK-NEXT: rts16
%2 = or i32 %0, 131072
ret i32 %2
@@ -71,9 +70,7 @@ define i32 @test_andnot_128(i32 noundef %0) {
define i32 @test_andnot_131072(i32 noundef %0) {
; CHECK-LABEL: test_andnot_131072:
; CHECK: # %bb.0:
-; CHECK-NEXT: movih32 a1, 65533
-; CHECK-NEXT: ori32 a1, a1, 65535
-; CHECK-NEXT: and16 a0, a1
+; CHECK-NEXT: bclri16 a0, 17
; CHECK-NEXT: rts16
%2 = and i32 %0, -131073
ret i32 %2
More information about the llvm-commits
mailing list