1576 Commits

Author SHA1 Message Date
Ross Kirsling
7d2293d1d9
[InstCombine] KnownBits::isNonNegative should recognize b - a after a <= b (#145105)
Alive2: https://alive2.llvm.org/ce/z/an9npN
Fixes #142283.
2025-06-24 18:51:49 +02:00
Wenju He
9d570d568b
[ValueTracking] Return true for AddrSpaceCast in canCreateUndefOrPoison (#144686)
In our downstream GPU target, following IR is valid before instcombine
although the second addrspacecast causes UB.
  define i1 @test(ptr addrspace(1) noundef %v) {
    %0 = addrspacecast ptr addrspace(1) %v to ptr addrspace(4)
    %1 = call i32 @llvm.xxxx.isaddr.shared(ptr addrspace(4) %0)
    %2 = icmp eq i32 %1, 0
    %3 = addrspacecast ptr addrspace(4) %0 to ptr addrspace(3)
    %4 = select i1 %2, ptr addrspace(3) null, ptr addrspace(3) %3
    %5 = icmp eq ptr addrspace(3) %4, null
    ret i1 %5
  }
We have a custom optimization that replaces invalid addrspacecast with
poison, and IR is still valid since `select` stops poison propagation.

However, instcombine pass optimizes `select` to `or`:
    %0 = addrspacecast ptr addrspace(1) %v to ptr addrspace(4)
    %1 = call i32 @llvm.xxxx.isaddr.shared(ptr addrspace(4) %0)
    %2 = icmp eq i32 %1, 0
    %3 = addrspacecast ptr addrspace(1) %v to ptr addrspace(3)
    %4 = icmp eq ptr addrspace(3) %3, null
    %5 = or i1 %2, %4
    ret i1 %5
The transform is invalid for our target.

---------

Co-authored-by: Nikita Popov <github@npopov.com>
2025-06-24 08:43:47 +08:00
Iris Shi
32f911f3e8
[InstCombine] Fold ceil(X / (2 ^ C)) == 0 -> X == 0 (#143683)
Co-authored-by: Yingwei Zheng <dtcxzyw2333@gmail.com>
2025-06-23 10:51:17 +08:00
Arthur Eubanks
5708851283
Revert "[ValueTracking] Improve Bitcast handling to match SDAG" (#145191)
Reverts llvm/llvm-project#125935

Causes miscompiles, see comments in #125935
2025-06-21 16:02:26 -07:00
Abhishek Kaushik
0b8179b2ad
[ValueTracking] Improve Bitcast handling to match SDAG (#125935)
Closes #125228
2025-06-19 18:35:40 +01:00
Jeremy Morse
9eb0020555
[DebugInfo][RemoveDIs] Remove a swathe of debug-intrinsic code (#144389)
Seeing how we can't generate any debug intrinsics any more: delete a
variety of codepaths where they're handled. For the most part these are
plain deletions, in others I've tweaked comments to remain coherent, or
added a type to (what was) type-generic-lambdas.

This isn't all the DbgInfoIntrinsic call sites but it's most of the
simple scenarios.

Co-authored-by: Nikita Popov <github@npopov.com>
2025-06-17 15:55:14 +01:00
Ricardo Jesus
f12dd8f86a
[ValueTracking] Remove unused variable in matchSimpleRecurrence (NFC). (#144316) 2025-06-16 09:57:21 +01:00
Ricardo Jesus
cca454b54c
[ValueTracking] Remove opcode whitelist from matchSimpleRecurrence. (#144031)
This also patches HashRecognize to avoid it mishandling some opcodes.
2025-06-16 09:12:42 +01:00
AZero13
30a41a6423
[ValueTracking] Add subtraction support for setLimitsForBinOp (#143618)
We can determine the range from a subtraction if it has nsw or nuw.

https://alive2.llvm.org/ce/z/tXAKVV
2025-06-15 15:32:34 +08:00
Yingwei Zheng
149cb5c43c
[ValueTracking] Infer X | Y != 0 from X != Y (#117443)
Alive2: https://alive2.llvm.org/ce/z/cJ75Ya

Closes https://github.com/llvm/llvm-project/issues/117436.
2025-06-15 15:17:53 +08:00
Yingwei Zheng
2f15637e04
[ValueTracking] Update Ordered when both operands are non-NaN. (#143349)
When the original predicate is ordered and both operands are non-NaN,
`Ordered` should be set to true. This variable still matters even if
both operands are non-NaN because FMF only applies to select, not fcmp.

Closes https://github.com/llvm/llvm-project/issues/143123.
2025-06-09 15:46:09 +08:00
Craig Topper
b8a4a3b99c
[ValueTracking] Support scalable vector splats of ConstantInt/ConstantFP in isGuaranteedNotToBeUndefOrPoison. (#142894)
Scalable vectors use insertelt+shufflevector ConstantExpr to
represent a splat.
2025-06-05 22:08:03 -07:00
Craig Topper
d5d6f60632
[ValueTracking] Support scalable vectors for ExtractElement in computeKnownFPClass. (#143051)
We can support scalable vectors by setting the demanded mask to APInt(1,
1) to demand the whole vector.
2025-06-05 20:48:07 -07:00
Ramkumar Ramachandra
b40e4ceaa6
[ValueTracking] Make Depth last default arg (NFC) (#142384)
Having a finite Depth (or recursion limit) for computeKnownBits is very
limiting, but is currently a load-bearing necessity, as all KnownBits
are recomputed on each call and there is no caching. As a prerequisite
for an effort to remove the recursion limit altogether, either using a
clever caching technique, or writing a easily-invalidable KnownBits
analysis, make the Depth argument in APIs in ValueTracking uniformly the
last argument with a default value. This would aid in removing the
argument when the time comes, as many callers that currently pass 0
explicitly are now updated to omit the argument altogether.
2025-06-03 17:12:24 +01:00
Hassnaa Hamdi
67c67cd053
[ValueTracking][NFC]: Use injected condition to compute known FPClass (#139832) 2025-06-02 11:59:43 +01:00
Yingwei Zheng
1984c7539e
[ValueTracking] Do not use FMF from fcmp (#142266)
This patch introduces an FMF parameter for
`matchDecomposedSelectPattern` to pass FMF flags from select, instead of
fcmp.

Closes https://github.com/llvm/llvm-project/issues/137998.
Closes https://github.com/llvm/llvm-project/issues/141017.
2025-06-02 18:21:14 +08:00
Cullen Rhodes
b9fa1dfd38
[ValueTracking][NFC] Simplify binops in canCreateUndefOrPoison switch (#139906)
Already handled by default case.
2025-06-02 10:41:32 +01:00
Yingwei Zheng
6c86b7d7d8
[ValueTracking][InstCombine] Generalize ignoreSignBitOfZero/NaN to handle more cases (#141015)
This patch was originally part of
https://github.com/llvm/llvm-project/pull/139861. It generalizes
`ignoreSignBitOfZero/NaN` to handle more instructions/intrinsics.

BTW, I find it mitigates performance regressions caused by
https://github.com/llvm/llvm-project/pull/141010 (IR diff
https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2365/files). We don't
need to propagate FMF from fcmp into select, since we can infer demanded
properties from the user of select.
2025-05-28 19:17:51 +08:00
Andreas Jonson
58ead2cee8
[ValueTracking] Support trunc nuw condition in isImpliedCondition (#141528)
Proof: https://alive2.llvm.org/ce/z/oqQyxC
2025-05-27 18:53:29 +02:00
Tim Gymnich
571a24c314
Reland [llvm] add GenericFloatingPointPredicateUtils #140254 (#141065)
#140254 was previously missing 2 files in the bazel build config.
2025-05-22 17:17:02 +02:00
Kewen12
c47a5fbb22
Revert "[llvm] add GenericFloatingPointPredicateUtils (#140254)" (#140968)
This reverts commit d00d74bb2564103ae3cb5ac6b6ffecf7e1cc2238. 

The PR breaks our buildbots and blocks downstream merge.
2025-05-21 19:31:14 -04:00
Tim Gymnich
d00d74bb25
[llvm] add GenericFloatingPointPredicateUtils (#140254)
add `GenericFloatingPointPredicateUtils` in order to generalize
effects of floating point comparisons on `KnownFPClass` for both IR and
MIR.

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-05-21 23:45:31 +02:00
Cullen Rhodes
059b0c2efb
[ValueTracking][NFC] Drop outdated TODO in canCreateUndefOrPoison (#139915)
The inrange constexpr GEP case is handled since 425cbbc602c9.
2025-05-15 15:09:15 +01:00
Matt Arsenault
89d13f87c7
Analysis: Remove no-AssumptionCache path in getKnowledgeForValue (#139232)
As requested in https://github.com/llvm/llvm-project/pull/138961#discussion_r2078483175
2025-05-09 12:23:38 +02:00
Matt Arsenault
609a8331a0
ValueTracking: Handle minimumnum and maximumnum in computeKnownFPClass (#138737)
For now use the same treatment as minnum/maxnum, but these should
diverge. alive2 seems happy with this, except for some preexisting bugs
with weird denormal modes.
2025-05-07 08:02:24 +02:00
Matt Arsenault
51e157f581
ValueTracking: Handle minimumnum/maximumnum in canCreateUndefOrPoison (#138729) 2025-05-06 22:26:56 +02:00
Yingwei Zheng
830cf36bd4
[LVI][ValueTracking] Take UB-implying attributes into account in isSafeToSpeculativelyExecute (#137604)
Closes https://github.com/llvm/llvm-project/issues/137582.

In the original case, LVI uses the edge information in `%entry ->
%if.end` to get a more precise result. However, since the call to `smin`
has an `noundef` return attribute, an immediate UB will be triggered
after optimization.

Currently, `isSafeToSpeculativelyExecuteWithOpcode(%min)` returns true
because
6a288c1e32
only checks whether the function is speculatable. However, it is not
enough in this case.

This patch takes UB-implying attributes into account if
`IgnoreUBImplyingAttrs` is set to false. If it is set to true, the
caller is responsible for correctly propagating UB-implying attributes.
2025-04-30 11:53:38 +08:00
Nikita Popov
c5a5f4330a Reapply [ValueTracking] Drop ucmp/scmp from getIntrinsicRange() (NFCI)
Reapply after d51b2785abf77978d9218a7b6fb5b8ec6c770c31, which should
fix optimization regressions.

After #135642 we have a range attribute on the intrinsic declaration,
so we should not need the special handling here.
2025-04-22 16:55:42 +02:00
Hans Wennborg
dba8acde6d Revert "[ValueTracking] Drop ucmp/scmp from getIntrinsicRange() (NFCI)"
This does seem to cause some functionality to change, see comment on
278c429d11

This reverts commit 278c429d11e63bc709ea8c537b23c4e350ce2a07.
2025-04-22 15:19:49 +02:00
Nikita Popov
278c429d11 [ValueTracking] Drop ucmp/scmp from getIntrinsicRange() (NFCI)
After #135642 we have a range attribute on the intrinsic declaration,
so we should not need the special handling here.
2025-04-22 10:56:04 +02:00
Yingwei Zheng
b1b065f2bf
[ValueTracking] Refactor isKnownNonEqualFromContext (#127388)
This patch avoids adding RHS for comparisons with two variable operands
(https://github.com/llvm/llvm-project/pull/118493#discussion_r1949397482).
Instead, we iterate over related dominating conditions of both V1 and V2
in `isKnownNonEqualFromContext`, as suggested by goldsteinn
(https://github.com/llvm/llvm-project/pull/117442#discussion_r1944058002).

Compile-time improvement:
https://llvm-compile-time-tracker.com/compare.php?from=c6d95c441a29a45782ff72d6cb82839b86fd0e4a&to=88464baedd7b1731281eaa0ce4438122b4d218a7&stat=instructions:u
2025-04-18 22:14:06 +08:00
Andreas Jonson
39562de510
[ValueTracking] Handle assume(trunc x to i1) in ComputeKnownBits (#118406)
proof: https://alive2.llvm.org/ce/z/zAspzb
2025-04-10 19:29:28 +02:00
Matt Arsenault
258aa65197
ValueTracking: Do not look at users of constants for ephemeral values (#134618) 2025-04-09 19:15:16 +07:00
LU-JOHN
6a46c6c865
Ensure KnownBits passed when calculating from range md has right size (#132985)
KnownBits passed to computeKnownBitsFromRangeMetadata must have the same
bit width as the range metadata bit width. Otherwise the calculated
results will be incorrect.

---------

Signed-off-by: John Lu <John.Lu@amd.com>
2025-04-03 10:17:14 +07:00
Tim Gymnich
049f179606
[Analysis][NFC] Extract KnownFPClass (#133457)
- extract KnownFPClass for future use inside of GISelKnownBits

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-03-28 18:10:02 +01:00
pzzp
d6a2cca77e
[llvm:ir] Add support for constant data exceeding 4GiB (#126481)
The test file is over 4GiB, which is too big, so I didn’t submit it.
2025-03-21 11:44:01 -07:00
Yingwei Zheng
029e10289a
[ValueTracking] Bail out on x86_fp80 when computing fpclass with knownbits (#130477)
In https://github.com/llvm/llvm-project/pull/97762, we assume the
minimum possible value of X is NaN implies X is NaN. But it doesn't hold
for x86_fp80 format. If the knownbits of X are
`?'011111111111110'????????????????????????????????????????????????????????????????`,
the minimum possible value of X is NaN/unnormal. However, it can be a
normal value.

Closes https://github.com/llvm/llvm-project/issues/130408.
2025-03-09 21:10:35 +08:00
DianQK
462eb7e28e
[ValueTracking] Skip incoming values that are the same as the phi in isGuaranteedNotToBeUndefOrPoison (#130111)
Fixes (keep it open) #130110.

If the incoming value is PHI itself, we can skip this. If we can
guarantee that the other incoming values are neither undef nor poison,
then we can also guarantee that the value isn't either. If we cannot
guarantee that, it makes no sense in calculating it.
2025-03-07 05:46:32 +08:00
Narayan
6311e3fcc8
[ValueTracking] ComputeNumSignBitsImpl - add basic handling of BITCAST nodes (#127218)
When a wider scalar/vector type containing all sign bits is bitcast to a
narrower vector type, we can deduce that the resulting narrow elements
will also be all sign bits. This matches existing behavior in
SelectionDAG and helps optimize cases involving SSE intrinsics where
sign-extended values are bitcast between different vector types.

The current implementation fails to recognize that an arithmetic right
shift is redundant when applied to elements that are already known to be
all sign bits. This PR improves ComputeNumSignBitsImpl to track this
information through bitcasts, enabling the optimization of such cases.

```
%ext = sext <1 x i1> %cmp to <1 x i8>
%sub = bitcast <1 x i8> %ext to <4 x i2>
%sra = ashr <4 x i2> %sub, <i2 1, i2 1, i2 1, i2 1>
; Can be simplified to just:
%sub = bitcast <1 x i8> %ext to <4 x i2>
```

Closes #87624
2025-03-06 08:30:36 +00:00
Craig Topper
0cc532b79e
[RISCV] Move the RISCVII namespaced enums into RISCVVType namespace in RISCVTargetParser.h. NFC (#127585)
The VLMUL and policy enums originally lived in RISCVBaseInfo.h in the
backend which is where everything else in the RISCVII namespace is
defined.

RISCVTargetParser.h is used by much more of the compiler and it
doesn't really make sense to have 2 different namespaces exposed.
These enums are both associated with VTYPE so using the RISCVVType
namespace seems like a good home for them.
2025-02-18 08:27:25 -08:00
Kazu Hirata
6a3007683b
[Analysis] Remove getGuaranteedNonPoisonOps (#127461)
commit 0517772b4ac20c5d3a0de0d4703354a179833248
  Author: Philip Reames <preames@rivosinc.com>
  Date:   Thu Dec 19 14:14:11 2024 -0800
2025-02-17 08:26:33 -08:00
Kazu Hirata
e0545b5c6d
[Analysis] Remove getGuaranteedWellDefinedOps (#127453)
The last use was removed in:

  commit ac9e67756e0157793d565c2cceaf82e4403f58ba
  Author: Yingwei Zheng <dtcxzyw2333@gmail.com>
  Date:   Mon Feb 26 01:53:16 2024 +0800
2025-02-17 01:22:39 -08:00
Yingwei Zheng
324e27e8ba
[ValueTracking] Infer NonEqual from dominating conditions/assumptions (#117442)
This patch adds context-sensitive analysis support for
`isKnownNonEqual`. It is required for
https://github.com/llvm/llvm-project/issues/117436.
2025-02-12 20:15:14 +08:00
Andreas Jonson
cf87eb9d9b
[ValueTracking] Handle trunc to i1 as condition in dominating condition. (#126414)
proof: https://alive2.llvm.org/ce/z/gALGmv
2025-02-11 18:11:23 +01:00
Andreas Jonson
9e0077c921
[ValueTracking] Handle not in dominating condition. (#126423)
General handling of not in dominating condition.

proof: https://alive2.llvm.org/ce/z/FjJN8q
2025-02-10 18:14:09 +01:00
Ramkumar Ramachandra
7f21c77024
ValueTracking: modernize isKnownInversion (NFC) (#126234) 2025-02-07 13:28:23 +00:00
Yingwei Zheng
8bba8a50f8
[NFC][ValueTracking] Hoist the matching of RHS constant (#125818) 2025-02-05 16:56:31 +08:00
Yingwei Zheng
837bf32552
[ValueTracking] Remove unused V ^ Mask == C from computeKnownBitsFromCmp. NFCI. (#125666)
I believe it is unused since we always convert it into `V == Mask ^ C`.
Code coverage:
https://dtcxzyw.github.io/llvm-opt-benchmark/coverage/data/zyw/opt-ci/actions-runner/_work/llvm-opt-benchmark/llvm-opt-benchmark/llvm/llvm-project/llvm/lib/Analysis/ValueTracking.cpp.html#L706
2025-02-05 05:27:05 +08:00
Nikita Popov
3bd11b502c
[ValueTracking] Fix bit width handling in computeKnownBits() for GEPs (#125532)
For GEPs, we have three bit widths involved: The pointer bit width, the
index bit width, and the bit width of the GEP operands.

The correct behavior here is:
* We need to sextOrTrunc the GEP operand to the index width *before*
multiplying by the scale.
* If the index width and pointer width differ, GEP only ever modifies
the low bits. Adds should not overflow into the high bits.

I'm testing this via unit tests because it's a bit tricky to test in IR
with InstCombine canonicalization getting in the way.
2025-02-04 14:29:58 +01:00
Yingwei Zheng
711fcae67a
[ValueTracking] Handle trunc nuw in computeKnownBitsFromICmpCond (#125414)
This patch extends https://github.com/llvm/llvm-project/pull/82803 to
further infer high bits when `nuw` is set. It will save some and
instructions on induction variables. No real-world benefit is observed
for `trunc nsw`.
Alive2: https://alive2.llvm.org/ce/z/j-YFvt
2025-02-03 14:00:28 +08:00