[llvm] [InstCombine] Fold (X == 0 ? Y : 0) | X to X == 0 ? Y : X (PR #138373)

via llvm-commits llvm-commits at lists.llvm.org
Sat May 3 20:13:56 PDT 2025


https://github.com/YLChenZ updated https://github.com/llvm/llvm-project/pull/138373

>From 6fdcdeadb11c973b07ea1dd58db5b872aa0b394b Mon Sep 17 00:00:00 2001
From: YLChenZ <chentongyongcz at gmail.com>
Date: Sat, 3 May 2025 09:51:12 +0800
Subject: [PATCH 1/4] [InstCombine] Fold (X == 0 ? Y : 0) | X to X == 0 ? Y : X

---
 .../InstCombine/InstCombineAndOrXor.cpp       |  40 +++++
 .../InstCombine/or-select-zero-icmp.ll        | 144 ++++++++++++++++++
 2 files changed, 184 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index f059d87e581fd..77e886c98261c 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3575,6 +3575,41 @@ static Value *foldOrOfInversions(BinaryOperator &I,
   return nullptr;
 }
 
+// Optimize patterns where an OR operation combines a select-based zero check
+// with its condition value. This handles both scalar and vector types.
+//
+// Given:
+//   (X == 0 ? Y : 0) | X  -->  X == 0 ? Y : X
+//   X | (X == 0 ? Y : 0)  -->  X == 0 ? Y : X
+//
+// Also handles cases where X might be wrapped in zero/sign extensions.
+static Instruction *foldOrOfSelectZero(BinaryOperator &BO, Value *Op0,
+                                       Value *Op1) {
+  CmpPredicate Pred;
+  Value *X, *Y;
+
+  // Check both operand orders to handle commutative OR
+  for (Value *SelVal : {Op0, Op1}) {
+    // The other operand in the OR operation (potentially X or extended X)
+    Value *Other = (SelVal == Op0) ? Op1 : Op0;
+
+    // Attempt to match the select pattern:
+    // select(icmp eq X, 0), Y, 0
+    // Where X might be:
+    // - Original value
+    // - Zero extended value (zext)
+    // - Sign extended value (sext)
+    if (match(SelVal, m_Select(m_c_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
+                               m_Zero())) &&
+        Pred == ICmpInst::ICMP_EQ &&
+        match(Other, m_ZExtOrSExtOrSelf(m_Specific(X)))) {
+      return SelectInst::Create(cast<SelectInst>(SelVal)->getCondition(), Y,
+                                Other);
+    }
+  }
+  return nullptr;
+}
+
 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
 // here. We should standardize that construct where it is needed or choose some
 // other way to ensure that commutated variants of patterns are not missed.
@@ -3657,6 +3692,11 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
                                    /*NSW=*/true, /*NUW=*/true))
       return R;
   }
+  
+  // (X == 0 ? Y : 0) | X -> X == 0 ? Y : X
+  // X | (X == 0 ? Y : 0) -> X == 0 ? Y : X
+  if (Instruction *R = foldOrOfSelectZero(I, Op0, Op1))
+    return R;
 
   Value *X, *Y;
   const APInt *CV;
diff --git a/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll b/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll
new file mode 100644
index 0000000000000..fc6b47f0eeb17
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll
@@ -0,0 +1,144 @@
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+; Basic functional test
+define i32 @basic(i32 %a, i32 %b) {
+; CHECK-LABEL: @basic(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[A]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %cmp = icmp eq i32 %a, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or = or i32 %sel, %a
+  ret i32 %or
+}
+
+; Operand order swap test
+define i32 @swap_operand_order(i32 %x, i32 %y) {
+; CHECK-LABEL: @swap_operand_order(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y:%.*]], i32 [[X]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %cmp = icmp eq i32 %x, 0
+  %sel = select i1 %cmp, i32 %y, i32 0
+  %or = or i32 %x, %sel
+  ret i32 %or
+}
+
+; Negative test: Non-zero false value in select
+define i32 @negative_non_zero_false_val(i32 %a, i32 %b) {
+; CHECK-LABEL: @negative_non_zero_false_val(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 1
+; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SEL]], [[A]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %cmp = icmp eq i32 %a, 0
+  %sel = select i1 %cmp, i32 %b, i32 1
+  %or = or i32 %sel, %a
+  ret i32 %or
+}
+
+; Negative test: Incorrect comparison predicate (NE)
+define i32 @negative_wrong_predicate(i32 %a, i32 %b) {
+; CHECK-LABEL: @negative_wrong_predicate(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 0, i32 [[B:%.*]]
+; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SEL]], [[A]]
+; CHECK-NEXT:    ret i32 [[OR]]
+;
+  %cmp = icmp ne i32 %a, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or = or i32 %sel, %a
+  ret i32 %or
+}
+
+; Comparison direction swap test (0 == X)
+define i32 @cmp_swapped(i32 %x, i32 %y) {
+; CHECK-LABEL: @cmp_swapped(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y:%.*]], i32 [[X]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %cmp = icmp eq i32 0, %x
+  %sel = select i1 %cmp, i32 %y, i32 0
+  %or = or i32 %x, %sel
+  ret i32 %or
+}
+
+; Complex expression test
+define i32 @complex_expression(i32 %a, i32 %b) {
+; CHECK-LABEL: @complex_expression(
+; CHECK-NEXT:    [[X:%.*]] = add i32 [[A:%.*]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[X]]
+; CHECK-NEXT:    ret i32 [[RES]]
+;
+  %x = add i32 %a, 1
+  %cmp = icmp eq i32 %x, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or = or i32 %sel, %x
+  ret i32 %or
+}
+
+; zext test
+define i32 @zext_cond(i8 %a, i32 %b) {
+; CHECK-LABEL: @zext_cond(
+; CHECK-NEXT:    [[Z:%.*]] = zext i8 [[A:%.*]] to i32
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[Z]]
+; CHECK-NEXT:    ret i32 [[OR]]
+  %z   = zext i8 %a to i32
+  %cmp = icmp eq i32 %z, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or  = or i32 %sel, %z
+  ret i32 %or
+}
+
+; sext test
+define i32 @sext_cond(i8 %a, i32 %b) {
+; CHECK-LABEL: @sext_cond(
+; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A:%.*]] to i32
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[S]]
+; CHECK-NEXT:    ret i32 [[OR]]
+  %s   = sext i8 %a to i32
+  %cmp = icmp eq i32 %s, 0
+  %sel = select i1 %cmp, i32 %b, i32 0
+  %or  = or i32 %sel, %s
+  ret i32 %or
+}
+
+; Vector type test
+define <2 x i32> @vector_type(<2 x i32> %a, <2 x i32> %b) {
+; CHECK-LABEL: @vector_type(
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], zeroinitializer
+; CHECK-NEXT:    [[RES:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[B:%.*]], <2 x i32> [[A]]
+; CHECK-NEXT:    ret <2 x i32> [[RES]]
+;
+  %cmp = icmp eq <2 x i32> %a, zeroinitializer
+  %sel = select <2 x i1> %cmp, <2 x i32> %b, <2 x i32> zeroinitializer
+  %or = or <2 x i32> %sel, %a
+  ret <2 x i32> %or
+}
+
+; Pointer type test (should not trigger optimization)
+define i32* @pointer_type(i32* %p, i32* %q) {
+; CHECK-LABEL: @pointer_type(
+; CHECK-NEXT:    [[A:%.*]] = ptrtoint ptr [[P:%.*]] to i64
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[P]], null
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], ptr [[Q:%.*]], ptr null
+; CHECK-NEXT:    [[SEL_INT:%.*]] = ptrtoint ptr [[SEL]] to i64
+; CHECK-NEXT:    [[OR:%.*]] = or i64 [[A]], [[SEL_INT]]
+; CHECK-NEXT:    [[RET:%.*]] = inttoptr i64 [[OR]] to ptr
+; CHECK-NEXT:    ret ptr [[RET]]
+;
+  %a = ptrtoint i32* %p to i64
+  %cmp = icmp eq i64 %a, 0
+  %sel = select i1 %cmp, i32* %q, i32* null
+  %sel_int = ptrtoint i32* %sel to i64
+  %or_val = or i64 %a, %sel_int
+  %ret = inttoptr i64 %or_val to i32*
+  ret i32* %ret
+}

>From b10c3f07a949a27dfd7433378e8b00f58163f558 Mon Sep 17 00:00:00 2001
From: YLChenZ <chentongyongcz at gmail.com>
Date: Sun, 4 May 2025 11:08:31 +0800
Subject: [PATCH 2/4] add the fold support in FoldOpIntoSelect

---
 .../InstCombine/InstCombineAndOrXor.cpp       | 46 ++++---------------
 .../InstCombine/InstructionCombining.cpp      | 31 +++++++++++++
 2 files changed, 39 insertions(+), 38 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index 77e886c98261c..af4ba17903056 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -3575,41 +3575,6 @@ static Value *foldOrOfInversions(BinaryOperator &I,
   return nullptr;
 }
 
-// Optimize patterns where an OR operation combines a select-based zero check
-// with its condition value. This handles both scalar and vector types.
-//
-// Given:
-//   (X == 0 ? Y : 0) | X  -->  X == 0 ? Y : X
-//   X | (X == 0 ? Y : 0)  -->  X == 0 ? Y : X
-//
-// Also handles cases where X might be wrapped in zero/sign extensions.
-static Instruction *foldOrOfSelectZero(BinaryOperator &BO, Value *Op0,
-                                       Value *Op1) {
-  CmpPredicate Pred;
-  Value *X, *Y;
-
-  // Check both operand orders to handle commutative OR
-  for (Value *SelVal : {Op0, Op1}) {
-    // The other operand in the OR operation (potentially X or extended X)
-    Value *Other = (SelVal == Op0) ? Op1 : Op0;
-
-    // Attempt to match the select pattern:
-    // select(icmp eq X, 0), Y, 0
-    // Where X might be:
-    // - Original value
-    // - Zero extended value (zext)
-    // - Sign extended value (sext)
-    if (match(SelVal, m_Select(m_c_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
-                               m_Zero())) &&
-        Pred == ICmpInst::ICMP_EQ &&
-        match(Other, m_ZExtOrSExtOrSelf(m_Specific(X)))) {
-      return SelectInst::Create(cast<SelectInst>(SelVal)->getCondition(), Y,
-                                Other);
-    }
-  }
-  return nullptr;
-}
-
 // FIXME: We use commutative matchers (m_c_*) for some, but not all, matches
 // here. We should standardize that construct where it is needed or choose some
 // other way to ensure that commutated variants of patterns are not missed.
@@ -3692,11 +3657,15 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
                                    /*NSW=*/true, /*NUW=*/true))
       return R;
   }
-  
+
   // (X == 0 ? Y : 0) | X -> X == 0 ? Y : X
   // X | (X == 0 ? Y : 0) -> X == 0 ? Y : X
-  if (Instruction *R = foldOrOfSelectZero(I, Op0, Op1))
-    return R;
+  for (Value *Op : {Op0, Op1}) {
+    if (auto *SI = dyn_cast<SelectInst>(Op)) {
+      if (auto *R = FoldOpIntoSelect(I, SI, /* FoldWithMultiUse */ false))
+        return R;
+    }
+  }
 
   Value *X, *Y;
   const APInt *CV;
@@ -5078,3 +5047,4 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
 
   return nullptr;
 }
+
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f807f5f4519fc..cbbe9f29706ee 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1714,6 +1714,36 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
     }
   }
 
+  // Optimize patterns where an OR operation combines a select-based zero check
+  // with its condition value. This handles both scalar and vector types.
+  //
+  // Given:
+  //   (X == 0 ? Y : 0) | X  -->  X == 0 ? Y : X
+  //   X | (X == 0 ? Y : 0)  -->  X == 0 ? Y : X
+  //
+  // Also handles cases where X might be wrapped in zero/sign extensions.
+  if (Op.getOpcode() == Instruction::Or) {
+    // Check both operand orders to handle commutative OR
+    // The other operand in the OR operation (potentially X or extended X)
+    Value *Other = Op.getOperand(0) == SI ? Op.getOperand(1) : Op.getOperand(0);
+
+    CmpPredicate Pred;
+    Value *X, *Y;
+    // Attempt to match the select pattern:
+    // select(icmp eq X, 0), Y, 0
+    // Where X might be:
+    // - Original value
+    // - Zero extended value (zext)
+    // - Sign extended value (sext)
+    if (match(SI, m_Select(m_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
+                                    m_Zero())) &&
+              Pred == ICmpInst::ICMP_EQ &&
+              match(Other, m_ZExtOrSExtOrSelf(m_Specific(X)))) {
+      return SelectInst::Create(SI->getCondition(), Y,
+                                        Other);
+    }
+  }
+
   // Make sure that one of the select arms folds successfully.
   Value *NewTV = simplifyOperationIntoSelectOperand(Op, SI, /*IsTrueArm=*/true);
   Value *NewFV =
@@ -5791,3 +5821,4 @@ void llvm::initializeInstCombine(PassRegistry &Registry) {
 FunctionPass *llvm::createInstructionCombiningPass() {
   return new InstructionCombiningPass();
 }
+

>From 74b7a41dd0fdf7afdad26a8a4917967fcf79f237 Mon Sep 17 00:00:00 2001
From: YLChenZ <chentongyongcz at gmail.com>
Date: Sun, 4 May 2025 11:10:30 +0800
Subject: [PATCH 3/4] clang format

---
 .../lib/Transforms/InstCombine/InstCombineAndOrXor.cpp |  1 -
 .../Transforms/InstCombine/InstructionCombining.cpp    | 10 ++++------
 2 files changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
index af4ba17903056..ba2ae050aed65 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp
@@ -5047,4 +5047,3 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
 
   return nullptr;
 }
-
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index cbbe9f29706ee..492d897d65c44 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1736,11 +1736,10 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
     // - Zero extended value (zext)
     // - Sign extended value (sext)
     if (match(SI, m_Select(m_ICmp(Pred, m_Value(X), m_Zero()), m_Value(Y),
-                                    m_Zero())) &&
-              Pred == ICmpInst::ICMP_EQ &&
-              match(Other, m_ZExtOrSExtOrSelf(m_Specific(X)))) {
-      return SelectInst::Create(SI->getCondition(), Y,
-                                        Other);
+                           m_Zero())) &&
+        Pred == ICmpInst::ICMP_EQ &&
+        match(Other, m_ZExtOrSExtOrSelf(m_Specific(X)))) {
+      return SelectInst::Create(SI->getCondition(), Y, Other);
     }
   }
 
@@ -5821,4 +5820,3 @@ void llvm::initializeInstCombine(PassRegistry &Registry) {
 FunctionPass *llvm::createInstructionCombiningPass() {
   return new InstructionCombiningPass();
 }
-

>From 5f32b8fc754f3df1ae0a92ef783b66969738404f Mon Sep 17 00:00:00 2001
From: YLChenZ <chentongyongcz at gmail.com>
Date: Sun, 4 May 2025 11:12:50 +0800
Subject: [PATCH 4/4] add multi-use test

---
 .../InstCombine/or-select-zero-icmp.ll        | 121 +++++++++++-------
 1 file changed, 77 insertions(+), 44 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll b/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll
index fc6b47f0eeb17..2a648217d07b0 100644
--- a/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll
+++ b/llvm/test/Transforms/InstCombine/or-select-zero-icmp.ll
@@ -1,10 +1,12 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 
 ; Basic functional test
 define i32 @basic(i32 %a, i32 %b) {
-; CHECK-LABEL: @basic(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
-; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[A]]
+; CHECK-LABEL: define i32 @basic(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[A]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %cmp = icmp eq i32 %a, 0
@@ -15,9 +17,10 @@ define i32 @basic(i32 %a, i32 %b) {
 
 ; Operand order swap test
 define i32 @swap_operand_order(i32 %x, i32 %y) {
-; CHECK-LABEL: @swap_operand_order(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
-; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y:%.*]], i32 [[X]]
+; CHECK-LABEL: define i32 @swap_operand_order(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y]], i32 [[X]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %cmp = icmp eq i32 %x, 0
@@ -28,10 +31,11 @@ define i32 @swap_operand_order(i32 %x, i32 %y) {
 
 ; Negative test: Non-zero false value in select
 define i32 @negative_non_zero_false_val(i32 %a, i32 %b) {
-; CHECK-LABEL: @negative_non_zero_false_val(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 1
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SEL]], [[A]]
+; CHECK-LABEL: define i32 @negative_non_zero_false_val(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[A]], 1
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[OR]]
 ;
   %cmp = icmp eq i32 %a, 0
@@ -42,10 +46,11 @@ define i32 @negative_non_zero_false_val(i32 %a, i32 %b) {
 
 ; Negative test: Incorrect comparison predicate (NE)
 define i32 @negative_wrong_predicate(i32 %a, i32 %b) {
-; CHECK-LABEL: @negative_wrong_predicate(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A:%.*]], 0
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 0, i32 [[B:%.*]]
-; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SEL]], [[A]]
+; CHECK-LABEL: define i32 @negative_wrong_predicate(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[A]], 0
+; CHECK-NEXT:    [[TMP1:%.*]] = or i32 [[B]], [[A]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 0, i32 [[TMP1]]
 ; CHECK-NEXT:    ret i32 [[OR]]
 ;
   %cmp = icmp ne i32 %a, 0
@@ -56,9 +61,10 @@ define i32 @negative_wrong_predicate(i32 %a, i32 %b) {
 
 ; Comparison direction swap test (0 == X)
 define i32 @cmp_swapped(i32 %x, i32 %y) {
-; CHECK-LABEL: @cmp_swapped(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
-; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y:%.*]], i32 [[X]]
+; CHECK-LABEL: define i32 @cmp_swapped(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[Y]], i32 [[X]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %cmp = icmp eq i32 0, %x
@@ -69,10 +75,11 @@ define i32 @cmp_swapped(i32 %x, i32 %y) {
 
 ; Complex expression test
 define i32 @complex_expression(i32 %a, i32 %b) {
-; CHECK-LABEL: @complex_expression(
-; CHECK-NEXT:    [[X:%.*]] = add i32 [[A:%.*]], 1
+; CHECK-LABEL: define i32 @complex_expression(
+; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[X:%.*]] = add i32 [[A]], 1
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X]], 0
-; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[X]]
+; CHECK-NEXT:    [[RES:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[X]]
 ; CHECK-NEXT:    ret i32 [[RES]]
 ;
   %x = add i32 %a, 1
@@ -84,37 +91,42 @@ define i32 @complex_expression(i32 %a, i32 %b) {
 
 ; zext test
 define i32 @zext_cond(i8 %a, i32 %b) {
-; CHECK-LABEL: @zext_cond(
-; CHECK-NEXT:    [[Z:%.*]] = zext i8 [[A:%.*]] to i32
+; CHECK-LABEL: define i32 @zext_cond(
+; CHECK-SAME: i8 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[Z:%.*]] = zext i8 [[A]] to i32
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[Z]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[Z]]
 ; CHECK-NEXT:    ret i32 [[OR]]
-  %z   = zext i8 %a to i32
-  %cmp = icmp eq i32 %z, 0
+;
+  %z = zext i8 %a to i32
+  %cmp = icmp eq i8 %a, 0
   %sel = select i1 %cmp, i32 %b, i32 0
-  %or  = or i32 %sel, %z
+  %or = or i32 %sel, %z
   ret i32 %or
 }
 
 ; sext test
 define i32 @sext_cond(i8 %a, i32 %b) {
-; CHECK-LABEL: @sext_cond(
-; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A:%.*]] to i32
+; CHECK-LABEL: define i32 @sext_cond(
+; CHECK-SAME: i8 [[A:%.*]], i32 [[B:%.*]]) {
+; CHECK-NEXT:    [[S:%.*]] = sext i8 [[A]] to i32
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i8 [[A]], 0
-; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B:%.*]], i32 [[S]]
+; CHECK-NEXT:    [[OR:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[S]]
 ; CHECK-NEXT:    ret i32 [[OR]]
-  %s   = sext i8 %a to i32
-  %cmp = icmp eq i32 %s, 0
+;
+  %s = sext i8 %a to i32
+  %cmp = icmp eq i8 %a, 0
   %sel = select i1 %cmp, i32 %b, i32 0
-  %or  = or i32 %sel, %s
+  %or = or i32 %sel, %s
   ret i32 %or
 }
 
 ; Vector type test
 define <2 x i32> @vector_type(<2 x i32> %a, <2 x i32> %b) {
-; CHECK-LABEL: @vector_type(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[A:%.*]], zeroinitializer
-; CHECK-NEXT:    [[RES:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[B:%.*]], <2 x i32> [[A]]
+; CHECK-LABEL: define <2 x i32> @vector_type(
+; CHECK-SAME: <2 x i32> [[A:%.*]], <2 x i32> [[B:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[A]], zeroinitializer
+; CHECK-NEXT:    [[RES:%.*]] = select <2 x i1> [[CMP]], <2 x i32> [[B]], <2 x i32> [[A]]
 ; CHECK-NEXT:    ret <2 x i32> [[RES]]
 ;
   %cmp = icmp eq <2 x i32> %a, zeroinitializer
@@ -124,21 +136,42 @@ define <2 x i32> @vector_type(<2 x i32> %a, <2 x i32> %b) {
 }
 
 ; Pointer type test (should not trigger optimization)
-define i32* @pointer_type(i32* %p, i32* %q) {
-; CHECK-LABEL: @pointer_type(
-; CHECK-NEXT:    [[A:%.*]] = ptrtoint ptr [[P:%.*]] to i64
+define ptr @pointer_type(ptr %p, ptr %q) {
+; CHECK-LABEL: define ptr @pointer_type(
+; CHECK-SAME: ptr [[P:%.*]], ptr [[Q:%.*]]) {
+; CHECK-NEXT:    [[A:%.*]] = ptrtoint ptr [[P]] to i64
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr [[P]], null
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], ptr [[Q:%.*]], ptr null
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], ptr [[Q]], ptr null
 ; CHECK-NEXT:    [[SEL_INT:%.*]] = ptrtoint ptr [[SEL]] to i64
 ; CHECK-NEXT:    [[OR:%.*]] = or i64 [[A]], [[SEL_INT]]
 ; CHECK-NEXT:    [[RET:%.*]] = inttoptr i64 [[OR]] to ptr
 ; CHECK-NEXT:    ret ptr [[RET]]
 ;
-  %a = ptrtoint i32* %p to i64
+  %a = ptrtoint ptr %p to i64
   %cmp = icmp eq i64 %a, 0
-  %sel = select i1 %cmp, i32* %q, i32* null
-  %sel_int = ptrtoint i32* %sel to i64
+  %sel = select i1 %cmp, ptr %q, ptr null
+  %sel_int = ptrtoint ptr %sel to i64
   %or_val = or i64 %a, %sel_int
-  %ret = inttoptr i64 %or_val to i32*
-  ret i32* %ret
+  %ret = inttoptr i64 %or_val to ptr
+  ret ptr %ret
 }
+
+; Multi-use test (should not trigger optimization)
+define i32 @multi_use_test(i32 %x, i32 %m) {
+; CHECK-LABEL: define i32 @multi_use_test(
+; CHECK-SAME: i32 [[X:%.*]], i32 [[M:%.*]]) {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X]], 0
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP]], i32 [[M]], i32 0
+; CHECK-NEXT:    [[OR:%.*]] = or i32 [[SEL]], [[X]]
+; CHECK-NEXT:    [[ADD:%.*]] = add i32 [[SEL]], [[X]]
+; CHECK-NEXT:    [[O2:%.*]] = sub i32 [[OR]], [[ADD]]
+; CHECK-NEXT:    ret i32 [[O2]]
+;
+  %cmp = icmp eq i32 %x, 0
+  %sel = select i1 %cmp, i32 %m, i32 0
+  %or = or i32 %sel, %x
+  %add = add i32 %sel, %x
+  %res = sub i32 %or, %add
+  ret i32 %res
+}
+



More information about the llvm-commits mailing list