@@ -158,6 +158,30 @@ def uimm_shift : Operand<i32>, ImmLeaf<i32, "return isUInt<2>(Imm);"> {
158
158
let DecoderMethod = "decodeImmShiftOpValue";
159
159
}
160
160
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
+
161
185
def CSKYSymbol : AsmOperandClass {
162
186
let Name = "CSKYSymbol";
163
187
let RenderMethod = "addImmOperands";
@@ -1422,6 +1446,14 @@ let Predicates = [iHasE2] in
1422
1446
def : Pat<(i32 imm:$imm),
1423
1447
(ORI32 (MOVIH32 (uimm32_hi16 imm:$imm)), (uimm32_lo16 imm:$imm))>;
1424
1448
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
+
1425
1457
// Other operations.
1426
1458
let Predicates = [iHasE2] in {
1427
1459
def : Pat<(rotl GPR:$rs1, GPR:$rs2),
0 commit comments