Skip to content

Commit 528831d

Browse files
committed
[CSKY] Optimize ANDI/ORI to BSETI/BCLRI for specific immediates
Reviewed By: zixuan-wu Differential Revision: https://reviews.llvm.org/D153614
1 parent f3b9b94 commit 528831d

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

llvm/lib/Target/CSKY/CSKYInstrInfo.td

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ def uimm_shift : Operand<i32>, ImmLeaf<i32, "return isUInt<2>(Imm);"> {
158158
let DecoderMethod = "decodeImmShiftOpValue";
159159
}
160160

161+
// Optimize (or x, imm) to (BSETI x, log2(imm)). We should exclude the
162+
// case can be opimized to (ORI32/ORI16 x, imm).
163+
def imm32_1_pop_bit_XFORM : SDNodeXForm<imm, [{
164+
uint32_t I = N->getZExtValue();
165+
return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
166+
N->getValueType(0));
167+
}]>;
168+
def imm32_1_pop_bit : PatLeaf<(imm), [{
169+
uint32_t I = N->getZExtValue();
170+
return llvm::popcount(I) == 1 && I > 0xffff;
171+
}]>;
172+
173+
// Optimize (and x, imm) to (BCLRI x, log2(~imm)). We should exclude the
174+
// case can be opimized to (ANDNI x, ~imm).
175+
def imm32_1_zero_bit_XFORM : SDNodeXForm<imm, [{
176+
uint32_t I = ~N->getZExtValue();
177+
return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
178+
N->getValueType(0));
179+
}]>;
180+
def imm32_1_zero_bit : PatLeaf<(imm), [{
181+
uint32_t I = ~N->getZExtValue();
182+
return llvm::popcount(I) == 1 && I > 0xfff;
183+
}]>;
184+
161185
def CSKYSymbol : AsmOperandClass {
162186
let Name = "CSKYSymbol";
163187
let RenderMethod = "addImmOperands";
@@ -1422,6 +1446,14 @@ let Predicates = [iHasE2] in
14221446
def : Pat<(i32 imm:$imm),
14231447
(ORI32 (MOVIH32 (uimm32_hi16 imm:$imm)), (uimm32_lo16 imm:$imm))>;
14241448

1449+
// Bit operations.
1450+
let Predicates = [iHasE2] in {
1451+
def : Pat<(or GPR:$rs, imm32_1_pop_bit:$imm),
1452+
(BSETI32 GPR:$rs, (imm32_1_pop_bit_XFORM imm32_1_pop_bit:$imm))>;
1453+
def : Pat<(and GPR:$rs, imm32_1_zero_bit:$imm),
1454+
(BCLRI32 GPR:$rs, (imm32_1_zero_bit_XFORM imm32_1_zero_bit:$imm))>;
1455+
}
1456+
14251457
// Other operations.
14261458
let Predicates = [iHasE2] in {
14271459
def : Pat<(rotl GPR:$rs1, GPR:$rs2),

llvm/test/CodeGen/CSKY/bseti_bclri.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ define i32 @test_or_128(i32 noundef %0) {
1313
define i32 @test_or_131072(i32 noundef %0) {
1414
; CHECK-LABEL: test_or_131072:
1515
; CHECK: # %bb.0:
16-
; CHECK-NEXT: movih32 a1, 2
17-
; CHECK-NEXT: or16 a0, a1
16+
; CHECK-NEXT: bseti16 a0, 17
1817
; CHECK-NEXT: rts16
1918
%2 = or i32 %0, 131072
2019
ret i32 %2
@@ -71,9 +70,7 @@ define i32 @test_andnot_128(i32 noundef %0) {
7170
define i32 @test_andnot_131072(i32 noundef %0) {
7271
; CHECK-LABEL: test_andnot_131072:
7372
; CHECK: # %bb.0:
74-
; CHECK-NEXT: movih32 a1, 65533
75-
; CHECK-NEXT: ori32 a1, a1, 65535
76-
; CHECK-NEXT: and16 a0, a1
73+
; CHECK-NEXT: bclri16 a0, 17
7774
; CHECK-NEXT: rts16
7875
%2 = and i32 %0, -131073
7976
ret i32 %2

0 commit comments

Comments
 (0)