[PATCH] D153614: [CSKY] Optimize ANDI/ORI to BCLRI/BSETI for specific immediates

Ben Shi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 23 00:54:46 PDT 2023


benshi001 created this revision.
benshi001 added a reviewer: zixuan-wu.
Herald added a subscriber: hiraditya.
Herald added a project: All.
benshi001 requested review of this revision.
Herald added subscribers: llvm-commits, jacquesguan.
Herald added a project: LLVM.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153614

Files:
  llvm/lib/Target/CSKY/CSKYInstrInfo.td
  llvm/test/CodeGen/CSKY/bseti_bclri.ll


Index: llvm/test/CodeGen/CSKY/bseti_bclri.ll
===================================================================
--- llvm/test/CodeGen/CSKY/bseti_bclri.ll
+++ llvm/test/CodeGen/CSKY/bseti_bclri.ll
@@ -13,8 +13,7 @@
 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_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
Index: llvm/lib/Target/CSKY/CSKYInstrInfo.td
===================================================================
--- llvm/lib/Target/CSKY/CSKYInstrInfo.td
+++ llvm/lib/Target/CSKY/CSKYInstrInfo.td
@@ -146,6 +146,30 @@
   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 uimm_bseti_1_XFORM : SDNodeXForm<imm, [{
+  uint32_t I = N->getZExtValue();
+  return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
+                                   N->getValueType(0));
+}]>;
+def uimm_bseti_1 : 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 uimm_bclri_1_XFORM : SDNodeXForm<imm, [{
+  uint32_t I = ~N->getZExtValue();
+  return CurDAG->getTargetConstant(llvm::Log2_32(I), SDLoc(N),
+                                   N->getValueType(0));
+}]>;
+def uimm_bclri_1 : PatLeaf<(imm), [{
+  uint32_t I = ~N->getZExtValue();
+  return llvm::popcount(I) == 1 && I > 0xfff;
+}]>;
+
 def CSKYSymbol : AsmOperandClass {
   let Name = "CSKYSymbol";
   let RenderMethod = "addImmOperands";
@@ -1290,6 +1314,14 @@
   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, uimm_bseti_1:$imm),
+             (BSETI32 GPR:$rs, (uimm_bseti_1_XFORM uimm_bseti_1:$imm))>;
+  def : Pat<(and GPR:$rs, uimm_bclri_1:$imm),
+            (BCLRI32 GPR:$rs, (uimm_bclri_1_XFORM uimm_bclri_1:$imm))>;
+}
+
 // Other operations.
 let Predicates = [iHasE2] in {
   def : Pat<(rotl GPR:$rs1, GPR:$rs2),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D153614.533881.patch
Type: text/x-patch
Size: 2622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230623/75bd7695/attachment.bin>


More information about the llvm-commits mailing list