[PATCH] D148415: [InstCombine] Improve cost calculation in foldSelectICmpAndBinOp

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 27 14:47:51 PDT 2023


goldstein.w.n updated this revision to Diff 535151.
goldstein.w.n added a comment.

Rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D148415

Files:
  llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
  llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll


Index: llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
===================================================================
--- llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
+++ llvm/test/Transforms/InstCombine/select-with-bitwise-ops.ll
@@ -1582,10 +1582,10 @@
 ; Tests factoring in cost of saving the `and`
 define i64 @xor_i8_to_i64_shl_save_and_eq(i8 %x, i64 %y) {
 ; CHECK-LABEL: @xor_i8_to_i64_shl_save_and_eq(
-; CHECK-NEXT:    [[XX:%.*]] = and i8 [[X:%.*]], 1
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[XX]], 0
-; CHECK-NEXT:    [[Z:%.*]] = xor i64 [[Y:%.*]], -9223372036854775808
-; CHECK-NEXT:    [[R:%.*]] = select i1 [[CMP]], i64 [[Z]], i64 [[Y]]
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i8 [[X:%.*]] to i64
+; CHECK-NEXT:    [[TMP2:%.*]] = xor i64 [[TMP1]], -1
+; CHECK-NEXT:    [[TMP3:%.*]] = shl i64 [[TMP2]], 63
+; CHECK-NEXT:    [[R:%.*]] = xor i64 [[TMP3]], [[Y:%.*]]
 ; CHECK-NEXT:    ret i64 [[R]]
 ;
   %xx = and i8 %x, 1
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -736,13 +736,25 @@
 
   unsigned C2Log = C2->logBase2();
 
+  bool SavesAnd = false;
+  if (IC->isEquality() && V->hasOneUse()) {
+    // If we are shifting by bitwidth - 1, the `and` can be optimized out.
+    if (C2Log > C1Log) {
+      SavesAnd =
+          (C2Log - C1Log) == (Y->getType()->getScalarSizeInBits() - 1);
+    } else if (C1Log > C2Log) {
+      SavesAnd =
+          (C1Log - C2Log) == (Y->getType()->getScalarSizeInBits() - 1);
+    }
+  }
+
   bool NeedShift = C1Log != C2Log;
   bool NeedZExtTrunc = Y->getType()->getScalarSizeInBits() !=
                        V->getType()->getScalarSizeInBits();
 
   // Make sure we don't create more instructions than we save.
   if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) >
-      (IC->hasOneUse() + BinOp->hasOneUse()))
+      (IC->hasOneUse() + BinOp->hasOneUse() + SavesAnd))
     return nullptr;
 
   if (NeedAnd) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D148415.535151.patch
Type: text/x-patch
Size: 2105 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230627/d3017983/attachment.bin>


More information about the llvm-commits mailing list