[llvm] [InstSimplify] Move Select with bittest folds. (PR #122944)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 10:29:50 PST 2025


https://github.com/andjo403 created https://github.com/llvm/llvm-project/pull/122944

Preparation to handle `trunc to i1` in this fold.

results in no changes in local run of llvm-opt-benchmark

>From fdf24cc75bc2ae5a42e219f56a9a33661c9e7b30 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sat, 11 Jan 2025 07:06:04 +0100
Subject: [PATCH] [InstSimplify] Move Select with bittest folds.

Preparation to handle trunc to i1 in this fold.
---
 llvm/lib/Analysis/InstructionSimplify.cpp | 26 +++++++++++------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index d69747e30f884d7..05dedd219c2667d 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4612,14 +4612,16 @@ static Value *simplifyCmpSelOfMaxMin(Value *CmpLHS, Value *CmpRHS,
   return nullptr;
 }
 
-/// An alternative way to test if a bit is set or not uses sgt/slt instead of
-/// eq/ne.
-static Value *simplifySelectWithFakeICmpEq(Value *CmpLHS, Value *CmpRHS,
-                                           CmpPredicate Pred, Value *TrueVal,
-                                           Value *FalseVal) {
-  if (auto Res = decomposeBitTestICmp(CmpLHS, CmpRHS, Pred))
-    return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
-                                 Res->Pred == ICmpInst::ICMP_EQ);
+/// An alternative way to test if a bit is set or not uses e.g. sgt/slt instead
+/// of eq/ne.
+static Value *simplifySelectWithBitTest(Value *CondVal, Value *TrueVal,
+                                        Value *FalseVal) {
+
+  if (auto *ICmp = dyn_cast<ICmpInst>(CondVal))
+    if (auto Res = decomposeBitTestICmp(
+            ICmp->getOperand(0), ICmp->getOperand(1), ICmp->getPredicate()))
+      return simplifySelectBitTest(TrueVal, FalseVal, Res->X, &Res->Mask,
+                                   Res->Pred == ICmpInst::ICMP_EQ);
 
   return nullptr;
 }
@@ -4728,11 +4730,6 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
       return FalseVal;
   }
 
-  // Check for other compares that behave like bit test.
-  if (Value *V =
-          simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal))
-    return V;
-
   // If we have a scalar equality comparison, then we know the value in one of
   // the arms of the select. See if substituting this value into the arm and
   // simplifying the result yields the same value as the other arm.
@@ -4984,6 +4981,9 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
           simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
     return V;
 
+  if (Value *V = simplifySelectWithBitTest(Cond, TrueVal, FalseVal))
+    return V;
+
   if (Value *V = simplifySelectWithFCmp(Cond, TrueVal, FalseVal, Q, MaxRecurse))
     return V;
 



More information about the llvm-commits mailing list