[llvm] [ValueTracking] Handle intrinsics in `computeConstantRange` (PR #100870)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 27 10:53:30 PDT 2024
dtcxzyw wrote:
> for the [#95255 (comment)](https://github.com/llvm/llvm-project/issues/95255#issuecomment-2166944909) is it not enough to do a non Zero check something like this in the foldCtpop
>
> ```
> unsigned KnownMinPop = Known.countMinPopulation();
> unsigned Lower =
> KnownMinPop ? KnownMinPop
> : isKnownNonZero(
> Op0, IC.getSimplifyQuery().getWithInstruction(&II));
> ConstantRange Range(APInt(BitWidth, Lower),
> APInt(BitWidth, Known.countMaxPopulation() + 1));
> ```
Are you willing to file a PR with this method? I drafted an alternative patch before submitting this PR:
```
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index f6c4b6e18093..9f04ed85e831 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -692,12 +692,17 @@ static Instruction *foldCtpop(IntrinsicInst &II, InstCombinerImpl &IC) {
Ty);
// Add range attribute since known bits can't completely reflect what we know.
- if (BitWidth != 1 && !II.hasRetAttr(Attribute::Range) &&
- !II.getMetadata(LLVMContext::MD_range)) {
- ConstantRange Range(APInt(BitWidth, Known.countMinPopulation()),
- APInt(BitWidth, Known.countMaxPopulation() + 1));
- II.addRangeRetAttr(Range);
- return ⅈ
+ if (BitWidth != 1) {
+ ConstantRange OldRange = II.getRange().value_or(ConstantRange::getNonEmpty(APInt::getZero(BitWidth),
+ APInt(BitWidth, BitWidth + 1)));
+ ConstantRange Range = OldRange;
+ Range = Range.intersectWith(ConstantRange(APInt(BitWidth, Known.countMinPopulation()), APInt(BitWidth, Known.countMaxPopulation() + 1)), ConstantRange::Unsigned);
+ if (Range.contains(APInt::getZero(BitWidth)) && isKnownNonZero(Op0, IC.getSimplifyQuery().getWithInstruction(&II)))
+ Range = Range.intersectWith(ConstantRange(APInt(BitWidth, 1), APInt(BitWidth, BitWidth + 1)), ConstantRange::Unsigned);
+ if (Range != OldRange) {
+ II.addRangeRetAttr(Range);
+ return ⅈ
+ }
}
return nullptr;
```
https://github.com/llvm/llvm-project/pull/100870
More information about the llvm-commits
mailing list