Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[InstSimplify] Fold {u,s}{min,max} x, poison -> poison #138166

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025

Conversation

lukel97
Copy link
Contributor

@lukel97 lukel97 commented May 1, 2025

Following from the discussion in #138095 (comment), these intrinsics are poison if any of their operands are poison, and are marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when scalarizing intrinsics.

Following from the discussion in llvm#138095 (comment), these intrinsics are poison if any of their operands are poison, and are marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when scalarizing intrinsics.
@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Luke Lau (lukel97)

Changes

Following from the discussion in #138095 (comment), these intrinsics are poison if any of their operands are poison, and are marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when scalarizing intrinsics.


Full diff: https://github.com/llvm/llvm-project/pull/138166.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+4)
  • (modified) llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll (+4-4)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 8ffb471070d91..275ca8fa4dbc2 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6549,6 +6549,10 @@ Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
     if (match(Op0, m_ImmConstant()))
       std::swap(Op0, Op1);
 
+    // Propagate poison.
+    if (isa<PoisonValue>(Op1))
+      return Op1;
+
     // Assume undef is the limit value.
     if (Q.isUndefValue(Op1))
       return ConstantInt::get(
diff --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index ddd4140b3aedd..98bfe5329c290 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -75,7 +75,7 @@ define i81 @smax_undef(i81 %x) {
 
 define i81 @smax_poison(i81 %x) {
 ; CHECK-LABEL: @smax_poison(
-; CHECK-NEXT:    ret i81 1208925819614629174706175
+; CHECK-NEXT:    ret i81 poison
 ;
   %r = call i81 @llvm.smax.i81(i81 poison, i81 %x)
   ret i81 %r
@@ -91,7 +91,7 @@ define i3 @smin_undef(i3 %x) {
 
 define i3 @smin_poison(i3 %x) {
 ; CHECK-LABEL: @smin_poison(
-; CHECK-NEXT:    ret i3 -4
+; CHECK-NEXT:    ret i3 poison
 ;
   %r = call i3 @llvm.smin.i3(i3 %x, i3 poison)
   ret i3 %r
@@ -107,7 +107,7 @@ define <2 x i8> @umax_undef(<2 x i8> %x) {
 
 define <2 x i8> @umax_poison(<2 x i8> %x) {
 ; CHECK-LABEL: @umax_poison(
-; CHECK-NEXT:    ret <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    ret <2 x i8> poison
 ;
   %r = call <2 x i8> @llvm.umax.v2i8(<2 x i8> poison, <2 x i8> %x)
   ret <2 x i8> %r
@@ -123,7 +123,7 @@ define <2 x i8> @umin_undef(<2 x i8> %x) {
 
 define <2 x i8> @umin_poison(<2 x i8> %x) {
 ; CHECK-LABEL: @umin_poison(
-; CHECK-NEXT:    ret <2 x i8> zeroinitializer
+; CHECK-NEXT:    ret <2 x i8> poison
 ;
   %r = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> poison)
   ret <2 x i8> %r

@llvmbot
Copy link
Member

llvmbot commented May 1, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Luke Lau (lukel97)

Changes

Following from the discussion in #138095 (comment), these intrinsics are poison if any of their operands are poison, and are marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when scalarizing intrinsics.


Full diff: https://github.com/llvm/llvm-project/pull/138166.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+4)
  • (modified) llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll (+4-4)
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 8ffb471070d91..275ca8fa4dbc2 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -6549,6 +6549,10 @@ Value *llvm::simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
     if (match(Op0, m_ImmConstant()))
       std::swap(Op0, Op1);
 
+    // Propagate poison.
+    if (isa<PoisonValue>(Op1))
+      return Op1;
+
     // Assume undef is the limit value.
     if (Q.isUndefValue(Op1))
       return ConstantInt::get(
diff --git a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
index ddd4140b3aedd..98bfe5329c290 100644
--- a/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
+++ b/llvm/test/Transforms/InstSimplify/maxmin_intrinsics.ll
@@ -75,7 +75,7 @@ define i81 @smax_undef(i81 %x) {
 
 define i81 @smax_poison(i81 %x) {
 ; CHECK-LABEL: @smax_poison(
-; CHECK-NEXT:    ret i81 1208925819614629174706175
+; CHECK-NEXT:    ret i81 poison
 ;
   %r = call i81 @llvm.smax.i81(i81 poison, i81 %x)
   ret i81 %r
@@ -91,7 +91,7 @@ define i3 @smin_undef(i3 %x) {
 
 define i3 @smin_poison(i3 %x) {
 ; CHECK-LABEL: @smin_poison(
-; CHECK-NEXT:    ret i3 -4
+; CHECK-NEXT:    ret i3 poison
 ;
   %r = call i3 @llvm.smin.i3(i3 %x, i3 poison)
   ret i3 %r
@@ -107,7 +107,7 @@ define <2 x i8> @umax_undef(<2 x i8> %x) {
 
 define <2 x i8> @umax_poison(<2 x i8> %x) {
 ; CHECK-LABEL: @umax_poison(
-; CHECK-NEXT:    ret <2 x i8> splat (i8 -1)
+; CHECK-NEXT:    ret <2 x i8> poison
 ;
   %r = call <2 x i8> @llvm.umax.v2i8(<2 x i8> poison, <2 x i8> %x)
   ret <2 x i8> %r
@@ -123,7 +123,7 @@ define <2 x i8> @umin_undef(<2 x i8> %x) {
 
 define <2 x i8> @umin_poison(<2 x i8> %x) {
 ; CHECK-LABEL: @umin_poison(
-; CHECK-NEXT:    ret <2 x i8> zeroinitializer
+; CHECK-NEXT:    ret <2 x i8> poison
 ;
   %r = call <2 x i8> @llvm.umin.v2i8(<2 x i8> %x, <2 x i8> poison)
   ret <2 x i8> %r

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@lukel97 lukel97 merged commit aa6d541 into llvm:main May 1, 2025
13 of 15 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Following from the discussion in
llvm#138095 (comment),
these intrinsics are poison if any of their operands are poison, and are
marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when
scalarizing intrinsics.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Following from the discussion in
llvm#138095 (comment),
these intrinsics are poison if any of their operands are poison, and are
marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when
scalarizing intrinsics.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Following from the discussion in
llvm#138095 (comment),
these intrinsics are poison if any of their operands are poison, and are
marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when
scalarizing intrinsics.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Following from the discussion in
llvm#138095 (comment),
these intrinsics are poison if any of their operands are poison, and are
marked as such in propagatesPoison in ValueTracking.cpp.

This will help fold away leftover vectors produced by VectorCombine when
scalarizing intrinsics.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants