Skip to content
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

[ValueTracking] Use ConstantRange::toKnownBits when computing from Range Metadata; NFC #85574

Closed
wants to merge 1 commit into from

Conversation

goldsteinn
Copy link
Contributor

Just replaces some bespoke logic with a better tested API.

@llvmbot
Copy link
Collaborator

llvmbot commented Mar 17, 2024

@llvm/pr-subscribers-llvm-analysis

Author: None (goldsteinn)

Changes

Just replaces some bespoke logic with a better tested API.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/ValueTracking.cpp (+1-11)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index edbeede910d7f7..7f25f358aa8b30 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -423,23 +423,13 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
   unsigned NumRanges = Ranges.getNumOperands() / 2;
   assert(NumRanges >= 1);
 
-  Known.Zero.setAllBits();
-  Known.One.setAllBits();
-
   for (unsigned i = 0; i < NumRanges; ++i) {
     ConstantInt *Lower =
         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
     ConstantInt *Upper =
         mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
     ConstantRange Range(Lower->getValue(), Upper->getValue());
-
-    // The first CommonPrefixBits of all values in Range are equal.
-    unsigned CommonPrefixBits =
-        (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
-    APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
-    APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
-    Known.One &= UnsignedMax & Mask;
-    Known.Zero &= ~UnsignedMax & Mask;
+    Known = Known.unionWith(Range.toKnownBits());
   }
 }
 

APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
Known.One &= UnsignedMax & Mask;
Known.Zero &= ~UnsignedMax & Mask;
CR = CR.intersectWith(Range.zextOrTrunc(BitWidth));
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be unionWith (see https://llvm.org/docs/LangRef.html#range-metadata). (And initialize to empty above.)

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.

Failed Tests (40):
LLVM :: Analysis/ScalarEvolution/shift-op.ll
LLVM :: Analysis/ValueTracking/known-bits-from-range-md.ll
LLVM :: CodeGen/AArch64/GlobalISel/prelegalizer-combiner-icmp-to-true-false-known-bits.mir
LLVM :: CodeGen/AArch64/setcc_knownbits.ll
LLVM :: CodeGen/AMDGPU/array-ptr-calc-i32.ll
LLVM :: CodeGen/AMDGPU/divrem24-assume.ll
LLVM :: CodeGen/AMDGPU/global-saddr-load.ll
LLVM :: CodeGen/AMDGPU/load-range-metadata-sign-bits.ll
LLVM :: CodeGen/AMDGPU/simple-indirect-call.ll
LLVM :: CodeGen/AMDGPU/waterfall_kills_scc.ll
LLVM :: CodeGen/PowerPC/BreakableToken-reduced.ll
LLVM :: CodeGen/PowerPC/global-address-non-got-indirect-access.ll
LLVM :: CodeGen/X86/pr12360.ll
LLVM :: CodeGen/X86/pr48458.ll
LLVM :: CodeGen/X86/pr48888.ll
LLVM :: Transforms/GuardWidening/range-check-merging.ll
LLVM :: Transforms/IndVarSimplify/shift-range-checks.ll
LLVM :: Transforms/InferAddressSpaces/AMDGPU/ptrmask.ll
LLVM :: Transforms/InstCombine/ARM/mve-v2i2v.ll
LLVM :: Transforms/InstCombine/add2.ll
LLVM :: Transforms/InstCombine/binop-cast.ll
LLVM :: Transforms/InstCombine/bitreverse-known-bits.ll
LLVM :: Transforms/InstCombine/gep-sext.ll
LLVM :: Transforms/InstCombine/icmp-range.ll
LLVM :: Transforms/InstCombine/icmp_sdiv_with_and_without_range.ll
LLVM :: Transforms/InstCombine/ldexp.ll
LLVM :: Transforms/InstCombine/narrow-math.ll
LLVM :: Transforms/InstCombine/pr12251.ll
LLVM :: Transforms/InstCombine/pr75129.ll
LLVM :: Transforms/InstCombine/zext.ll
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/ComputeKnownBitsAddWithRange
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/ComputeKnownBitsAddWithRangeNoOverlap
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/ComputeKnownBitsGEPWithRange
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/ComputeKnownBitsGEPWithRangeNoOverlap
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/ComputeKnownBitsReturnedRangeConflict
LLVM-Unit :: Analysis/./AnalysisTests/ComputeKnownBitsTest/KnownNonZeroShift
LLVM-Unit :: CodeGen/./CodeGenTests/AArch64SelectionDAGTest/computeKnownBits_extload_known01
LLVM-Unit :: CodeGen/./CodeGenTests/AArch64SelectionDAGTest/computeKnownBits_extload_knownnegative
LLVM-Unit :: CodeGen/GlobalISel/./GlobalISelTests/AArch64GISelMITest/TestMetadata
LLVM-Unit :: CodeGen/GlobalISel/./GlobalISelTests/AArch64GISelMITest/TestVectorMetadata

@@ -423,24 +423,17 @@ void llvm::computeKnownBitsFromRangeMetadata(const MDNode &Ranges,
unsigned NumRanges = Ranges.getNumOperands() / 2;
assert(NumRanges >= 1);

Known.Zero.setAllBits();
Known.One.setAllBits();
ConstantRange CR = ConstantRange::getFull(BitWidth);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ConstantRange CR = ConstantRange::getFull(BitWidth);
ConstantRange CR = ConstantRange::getEmpty(BitWidth);

It should be initialized to empty :)

…Range Metadata; NFC

Just replaces some bespoke logic with a better tested API.
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.

None yet

4 participants