Skip to content

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented Aug 28, 2025

We may have already gotten information from range metadata, don't
overwrite it.

We may have already gotten information from range metadata, don't
overwrite it.
@topperc topperc requested review from jayfoad and RKSimon August 28, 2025 18:01
@topperc topperc requested a review from nikic as a code owner August 28, 2025 18:01
@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 Aug 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-analysis

Author: Craig Topper (topperc)

Changes

We may have already gotten information from range metadata, don't
overwrite it.


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+1-1)
  • (modified) llvm/test/Transforms/InstCombine/abs-intrinsic.ll (+13)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 7fe129b8456f6..864b972e06a10 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1829,7 +1829,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
       case Intrinsic::abs: {
         computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
         bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
-        Known = Known2.abs(IntMinIsPoison);
+        Known = Known.unionWith(Known2.abs(IntMinIsPoison));
         break;
       }
       case Intrinsic::bitreverse:
diff --git a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
index d32f0e4690502..346111d892975 100644
--- a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -847,3 +847,16 @@ cond.end:
   %r = phi i32 [ %0, %cond.true ], [ 0, %entry ]
   ret i32 %r
 }
+
+; The AND should be removable based on range metadata. Make sure
+; computeKnownBits doesn't lose this.
+define i32 @abs_range_metadata(i32 %x) {
+; CHECK-LABEL: @abs_range_metadata(
+; CHECK-NEXT:    [[B:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false), !range [[RNG0:![0-9]+]]
+; CHECK-NEXT:    ret i32 [[B]]
+;
+  %a = call i32 @llvm.abs.i32(i32 %x, i1 false), !range !1
+  %b = and i32 %a, 15
+  ret i32 %b
+}
+!1 = !{i32 0, i32 16}

@topperc topperc merged commit 396aa58 into llvm:main Aug 29, 2025
13 checks passed
@topperc topperc deleted the pr/abs branch August 29, 2025 02:36
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.

3 participants