[PATCH] D111532: [RISCV] Optimize immediate materialisation with BCLRI/BSETI

Ben Shi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 11 21:58:10 PDT 2021


benshi001 updated this revision to Diff 378878.
benshi001 retitled this revision from "[RISCV] Optimize immediate materialisation with BCLRI" to "[RISCV] Optimize immediate materialisation with BCLRI/BSETI".

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D111532/new/

https://reviews.llvm.org/D111532

Files:
  llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
  llvm/test/CodeGen/RISCV/imm.ll
  llvm/test/MC/RISCV/rv64zbs-aliases-valid.s


Index: llvm/test/MC/RISCV/rv64zbs-aliases-valid.s
===================================================================
--- llvm/test/MC/RISCV/rv64zbs-aliases-valid.s
+++ llvm/test/MC/RISCV/rv64zbs-aliases-valid.s
@@ -30,3 +30,19 @@
 # CHECK-S-OBJ-NOALIAS: bexti t0, t1, 8
 # CHECK-S-OBJ: bexti t0, t1, 8
 bext x5, x6, 8
+
+# CHECK-S-OBJ-NOALIAS: lui t0, 174763
+# CHECK-S-OBJ-NOALIAS-NEXT: addiw t0, t0, -1366
+# CHECK-S-OBJ-NOALIAS-NEXT: bseti t0, t0, 31
+# CHECK-S-OBJ: lui t0, 174763
+# CHECK-S-OBJ-NEXT: addiw t0, t0, -1366
+# CHECK-S-OBJ-NEXT: bseti t0, t0, 31
+li x5, 2863311530 # 0xaaaaaaaa
+
+# CHECK-S-OBJ-NOALIAS: lui t0, 873813
+# CHECK-S-OBJ-NOALIAS-NEXT: addiw t0, t0, 1366
+# CHECK-S-OBJ-NOALIAS-NEXT: bclri t0, t0, 31
+# CHECK-S-OBJ: lui t0, 873813
+# CHECK-S-OBJ-NEXT: addiw t0, t0, 1366
+# CHECK-S-OBJ-NEXT: bclri t0, t0, 31
+li x5, -2863311530 # 0xffffffff55555556
Index: llvm/test/CodeGen/RISCV/imm.ll
===================================================================
--- llvm/test/CodeGen/RISCV/imm.ll
+++ llvm/test/CodeGen/RISCV/imm.ll
@@ -1444,10 +1444,9 @@
 ;
 ; RV64IZBS-LABEL: imm_2863311530:
 ; RV64IZBS:       # %bb.0:
-; RV64IZBS-NEXT:    lui a0, 171
-; RV64IZBS-NEXT:    addiw a0, a0, -1365
-; RV64IZBS-NEXT:    slli a0, a0, 12
-; RV64IZBS-NEXT:    addi a0, a0, -1366
+; RV64IZBS-NEXT:    lui a0, 174763
+; RV64IZBS-NEXT:    addiw a0, a0, -1366
+; RV64IZBS-NEXT:    bseti a0, a0, 31
 ; RV64IZBS-NEXT:    ret
 	ret i64 2863311530 ; #0xaaaaaaaa
 }
@@ -1478,10 +1477,9 @@
 ;
 ; RV64IZBS-LABEL: imm_neg_2863311530:
 ; RV64IZBS:       # %bb.0:
-; RV64IZBS-NEXT:    lui a0, 1048405
-; RV64IZBS-NEXT:    addiw a0, a0, 1365
-; RV64IZBS-NEXT:    slli a0, a0, 12
-; RV64IZBS-NEXT:    addi a0, a0, 1366
+; RV64IZBS-NEXT:    lui a0, 873813
+; RV64IZBS-NEXT:    addiw a0, a0, 1366
+; RV64IZBS-NEXT:    bclri a0, a0, 31
 ; RV64IZBS-NEXT:    ret
 	ret i64 -2863311530 ; #0xffffffff55555556
 }
Index: llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
===================================================================
--- llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
+++ llvm/lib/Target/RISCV/MCTargetDesc/RISCVMatInt.cpp
@@ -182,6 +182,34 @@
     }
   }
 
+  // Perform the following optimization with the Zbs extension.
+  // 1. For values in range 0xffffffff 7fffffff ~ 0xffffffff 00000000,
+  //    call generateInstSeqImpl with Val|0x80000000 (which is expected be
+  //    an int32), then emit (BCLRI r, 31).
+  // 2. For values in range 0x80000000 ~ 0xffffffff, call generateInstSeqImpl
+  //    with Val&~0x80000000 (which is expected to be an int32), then
+  //    emit (BSETI r, 31).
+  if (Res.size() > 2 && ActiveFeatures[RISCV::FeatureStdExtZbs]) {
+    assert(ActiveFeatures[RISCV::Feature64Bit] &&
+           "Expected RV32 to only need 2 instructions");
+    int64_t NewVal;
+    unsigned Opc;
+    if (Val < 0) {
+      Opc = RISCV::BCLRI;
+      NewVal = Val | 0x80000000ll;
+    } else {
+      Opc = RISCV::BSETI;
+      NewVal = Val & ~0x80000000ll;
+    }
+    if (isInt<32>(NewVal)) {
+      RISCVMatInt::InstSeq TmpSeq;
+      generateInstSeqImpl(NewVal, ActiveFeatures, TmpSeq);
+      TmpSeq.push_back(RISCVMatInt::Inst(Opc, 31));
+      if (TmpSeq.size() < Res.size())
+        Res = TmpSeq;
+    }
+  }
+
   return Res;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111532.378878.patch
Type: text/x-patch
Size: 3276 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211012/296d084c/attachment.bin>


More information about the llvm-commits mailing list