Skip to content

Conversation

@pedroclobo
Copy link
Member

Calls to @llvm.abs(undef, i1 true) and @llvm.abs(INT_MIN, i1 true) can be optimized to poison instead of undef.

Alive2

@llvmbot llvmbot added llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:analysis Includes value tracking, cost tables and constant folding llvm:transforms labels Dec 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 4, 2024

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: Pedro Lobo (pedroclobo)

Changes

Calls to @<!-- -->llvm.abs(undef, i1 true) and @<!-- -->llvm.abs(INT_MIN, i1 true) can be optimized to poison instead of undef.

Alive2


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/ConstantFolding.cpp (+2-2)
  • (modified) llvm/test/Transforms/InstSimplify/ConstProp/abs.ll (+2-2)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 47b96e00c7765a..efbccee76f2c51 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3018,9 +3018,9 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
       assert(C1 && "Must be constant int");
       assert((C1->isOne() || C1->isZero()) && "Must be 0 or 1");
 
-      // Undef or minimum val operand with poison min --> undef
+      // Undef or minimum val operand with poison min --> poison
       if (C1->isOne() && (!C0 || C0->isMinSignedValue()))
-        return UndefValue::get(Ty);
+        return PoisonValue::get(Ty);
 
       // Undef operand with no poison min --> 0 (sign bit must be clear)
       if (!C0)
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/abs.ll b/llvm/test/Transforms/InstSimplify/ConstProp/abs.ll
index e313bd453a152b..37233b0f293427 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/abs.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/abs.ll
@@ -6,7 +6,7 @@ declare <8 x i8> @llvm.abs.v8i8(<8 x i8>, i1)
 
 define i8 @undef_val_min_poison() {
 ; CHECK-LABEL: @undef_val_min_poison(
-; CHECK-NEXT:    ret i8 undef
+; CHECK-NEXT:    ret i8 poison
 ;
   %r = call i8 @llvm.abs.i8(i8 undef, i1 true)
   ret i8 %r
@@ -22,7 +22,7 @@ define i8 @undef_val_min_not_poison() {
 
 define i8 @min_val_min_poison() {
 ; CHECK-LABEL: @min_val_min_poison(
-; CHECK-NEXT:    ret i8 undef
+; CHECK-NEXT:    ret i8 poison
 ;
   %r = call i8 @llvm.abs.i8(i8 -128, i1 true)
   ret i8 %r

@pedroclobo pedroclobo changed the title [InstSimplify][NFC] Refine abs(min/undef, true) to poison` [InstSimplify][NFC] Refine abs(min/undef, true) to poison Dec 4, 2024
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@nikic nikic changed the title [InstSimplify][NFC] Refine abs(min/undef, true) to poison [InstSimplify] Refine abs(min/undef, true) to poison Dec 4, 2024
@nunoplopes nunoplopes merged commit 0d1e762 into llvm:main Dec 4, 2024
6 of 7 checks passed
@pedroclobo pedroclobo deleted the abs-poison branch December 18, 2024 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding llvm:instcombine Covers the InstCombine, InstSimplify and AggressiveInstCombine passes llvm:transforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants