1609 Commits

Author SHA1 Message Date
Florian Hahn
20db716418 [ValueTracking] Only check up to CtxIter in willNotFreeBetween.
Only check up to CtxI (CtxIter) when checking for calls that may free
in CtxI's block.

Missed update in https://github.com/llvm/llvm-project/pull/167965.

This should be NFC, as all current callers pass a terminator that is
guaranteed to not free as CtxI
2025-11-15 13:58:16 +00:00
Yingwei Zheng
76c69ca29c
[ValueTracking] Bail out on non-immediate constant expressions (#168084)
In https://github.com/llvm/llvm-project/pull/165748 constant expressions
were allowed in `collectPossibleValues` because we are still using
insertelement + shufflevector idioms to represent a scalable vector
splat. However, it also accepts some unresolved constants like ptrtoint
of globals or pointer difference between two globals. Absolutely we can
ask the user to check this case with the constant folding API. However,
since we don't observe the real-world usefulness of handling constant
expressions, I decide to be more conservative and only handle immediate
constants in the helper function. With this patch, we don't need to
touch the SimplifyCFG part, as the values can only be either ConstantInt
or undef/poison values (NB: switch on undef condition is UB).

Fix the miscompilation reported by
https://github.com/llvm/llvm-project/pull/165748#issuecomment-3532245218
2025-11-15 21:14:29 +08:00
Florian Hahn
eb98b65e82
[ValueTracking] Check across single predecessors in willNotFreeBetween. (#167965)
Extend willNotFreeBetween to perform simple checking across blocks to
support the case where CtxI is in a successor of the block that contains
the assume, but the assume's parent is the single predecessor of CtxI's
block.

This enables using _builtin_assume_dereferenceable to vectorize
std::find_if and co in practice.

End-to-end reproducer: https://godbolt.org/z/6jbsd4EjT

PR: https://github.com/llvm/llvm-project/pull/167965
2025-11-15 12:11:52 +00:00
Valeriy Savchenko
3e90ecaa2f
[ValueTracking] Refine known bits for linear interpolation patterns (#166378)
In this patch, we try to detect the lerp pattern: a * (b - c) + c * d
where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.

In that particular case, we can use the following chain of reasoning:

a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a,
d)

Since that is true for arbitrary a, b, c and d within our constraints,
we can
conclude that:

  max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U

Considering that any result of the lerp would be less or equal to U, it
would
have at least the number of leading 0s as in U.

While being quite a specific situation, it is fairly common in computer
graphics in the shape of alpha blending.

In conjunction with #165877, increases vectorization factor for lerp
loops.
2025-11-07 09:03:55 +00:00
Yingwei Zheng
8a84b285f6
[SimplifyCFG] Eliminate dead edges of switches according to the domain of conditions (#165748)
In simplifycfg/cvp/sccp, we eliminate dead edges of switches according
to the knownbits/range info of conditions. However, these approximations
may not meet the real-world needs when the domain of condition values is
sparse. For example, if the condition can only be either -3 or 3, we
cannot prove that the condition never evaluates to 1 (knownbits:
???????1, range: [-3, 4)).
This patch adds a helper function `collectPossibleValues` to enumerate
all the possible values of V. To fix the motivating issue,
`eliminateDeadSwitchCases` will use the result to remove dead edges.

Note: In
https://discourse.llvm.org/t/missed-optimization-due-to-overflow-check/88700
I proposed a new value lattice kind to represent such values. But I find
it hard to apply because the transition becomes much complicated.

Compile-time impact looks neutral:
https://llvm-compile-time-tracker.com/compare.php?from=32d6b2139a6c8f79e074e8c6cfe0cc9e79c4c0c8&to=e47c26e3f1bf9eb062684dda4fafce58438e994b&stat=instructions:u
This patch removes many dead error-handling codes:
https://github.com/dtcxzyw/llvm-opt-benchmark/pull/3012
Closes https://github.com/llvm/llvm-project/issues/165179.
2025-11-04 20:55:33 +08:00
Jay Foad
f037f41350
[IR] Add new function attribute nocreateundeforpoison (#164809)
Also add a corresponding intrinsic property that can be used to mark
intrinsics that do not introduce poison, for example simple arithmetic
intrinsics that propagate poison just like a simple arithmetic
instruction.

As a smoke test this patch adds the new property to
llvm.amdgcn.fmul.legacy.
2025-11-04 12:00:44 +00:00
Cullen Rhodes
5b5eacc579
[ValueTracking] Teach isGuaranteedNotToBeUndefOrPoison about splats (#163570)
Splats include two poison values, but only the poison-ness of the
splatted value actually matters.
2025-10-20 11:20:09 +01:00
Lewis Crawford
17efa572c3
[InstSimplify] Optimize maximumnum and minimumnum (#139581)
Add support for the new maximumnum and minimumnum intrinsics in various
optimizations in InstSimplify.

Also, change the behavior of optimizing maxnum(sNaN, x) to simplify to
qNaN instead of x to better match the LLVM IR spec, and add more tests
for sNaN behavior for all 3 max/min intrinsic types.
2025-10-07 14:23:32 +01:00
Yingwei Zheng
ca5ece8939
[InstSimplify] Simplify fcmp implied by dominating fcmp (#161090)
This patch simplifies an fcmp into true/false if it is implied by a
dominating fcmp.
As an initial support, it only handles two cases:
+ `fcmp pred1, X, Y -> fcmp pred2, X, Y`: use set operations.
+ `fcmp pred1, X, C1 -> fcmp pred2, X, C2`: use `ConstantFPRange` and
set operations.

Note: It doesn't fix https://github.com/llvm/llvm-project/issues/70985,
as the second fcmp in the motivating case is not dominated by the edge.
We may need to adjust JumpThreading to handle this case.

Comptime impact (~+0.1%):
https://llvm-compile-time-tracker.com/compare.php?from=a728f213c863e4dd19f8969a417148d2951323c0&to=8ca70404fb0d66a824f39d83050ac38e2f1b25b9&stat=instructions:u
IR diff: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/2848
2025-10-05 16:15:51 +08:00
Florian Hahn
7ceef762c8
[LAA] Check if Ptr can be freed between Assume and CtxI. (#161725)
When using information from dereferenceable assumptions, we need to make
sure that the memory is not freed between the assume and the specified
context instruction. Instead of just checking canBeFreed, check if there
any calls that may free between the assume and the context instruction.

This patch introduces a willNotFreeBetween to check for calls that may
free between an assume and a context instructions, to also be used in
https://github.com/llvm/llvm-project/pull/161255.

PR: https://github.com/llvm/llvm-project/pull/161725
2025-10-03 13:44:58 +00:00
Yingwei Zheng
57a9f79336
[ValueTracking] Take PHI's poison-generating flags into account (#161530)
ninf/nnan in the phi node may produce poison values. They should be
considered in `isGuaranteedNotToBeUndefOrPoison`.
Closes https://github.com/llvm/llvm-project/issues/161524.
2025-10-02 01:11:51 +08:00
Yingwei Zheng
4d7694a740
[ValueTracking] a - b == NonZero -> a != b (#159792)
Alive2: https://alive2.llvm.org/ce/z/8rX5Rk
Closes https://github.com/llvm/llvm-project/issues/118106.
2025-09-20 06:27:53 +00:00
Craig Topper
ef1372af43
[KnownBits] Add setAllConflict to set all bits in Zero and One. NFC (#159815)
This is a common pattern to initialize Knownbits that occurs before
loops that call intersectWith.
2025-09-19 13:15:54 -07:00
Ramkumar Ramachandra
7fb3a91418
[PatternMatch] Introduce match functor (NFC) (#159386)
A common idiom is the usage of the PatternMatch match function within a
functional algorithm like all_of. Introduce a match functor to shorten
this idiom.

Co-authored-by: Luke Lau <luke@igalia.com>
2025-09-17 21:04:33 +01:00
Yingwei Zheng
ea59be552f
[ValueTracking] Don't take sign bit from NaN operands (#157250)
Closes https://github.com/llvm/llvm-project/issues/157238.
2025-09-16 20:59:42 +08:00
Aethezz
ef1539c1d4
[InstCombine] Fold out-of-range bits for squaring signed integers (#153484)
Fixes an issue where bits next to the sign bit were not constant-folded
when squaring a sign- or zero-extended small integer. Added logic to
detect when both operands of a multiplication are the same extended
value, allowing InstCombine to mark bits above the maximum possible
square as known zero. This enables correct folding of (x * x) & (1 << N)
to 0 when N is out of range.

Proof: https://alive2.llvm.org/ce/z/YGou44

Fixes #152061
2025-09-05 16:48:57 +02:00
Craig Topper
396aa58afa
[ValueTracking] Use unionWith when calculating known bits for abs. (#155896)
We may have already gotten information from range metadata, don't
overwrite it.
2025-08-28 19:36:22 -07:00
Craig Topper
cec0d5b217
[ValueTracking][SelectionDAG] Use KnownBits::reverseBits/byteSwap. NFC (#155847) 2025-08-28 13:26:32 -07:00
Craig Topper
9472225fa6
[KnownBits] Add operator<<=(unsigned) and operator>>=(unsigned). NFC (#155751)
Add operators to shift left or right and insert unknown bits.
2025-08-28 10:08:28 -07:00
Kazu Hirata
11b4f110e0
[llvm] Remove unused includes of SmallSet.h (NFC) (#154893)
We just replaced SmallSet<T *, N> with SmallPtrSet<T *, N>, bypassing
the redirection found in SmallSet.h.  With that, we no longer need to
include SmallSet.h in many files.
2025-08-22 10:33:46 -07:00
AZero13
f94290cbff
[ValueTracking][GlobalISel] UCMP and SCMP cannot create undef or poison (#154404)
They cannot make poison or undef, same for IR. They can only make -1, 0,
or 1

Alive 2: https://alive2.llvm.org/ce/z/--Jd78
2025-08-20 08:41:27 +09:00
David Green
a7df02f83c
[InstCombine] Make strlen optimization more resilient to different gep types. (#153623)
This makes the optimization in optimizeStringLength for strlen(gep
@glob, %x) -> sub endof@glob, %x a little more resilient, and maybe a
bit more correct for geps with non-array types.
2025-08-19 10:37:17 +01:00
Kazu Hirata
07eb7b7692
[llvm] Replace SmallSet with SmallPtrSet (NFC) (#154068)
This patch replaces SmallSet<T *, N> with SmallPtrSet<T *, N>.  Note
that SmallSet.h "redirects" SmallSet to SmallPtrSet for pointer
element types:

  template <typename PointeeType, unsigned N>
class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N>
{};

We only have 140 instances that rely on this "redirection", with the
vast majority of them under llvm/. Since relying on the redirection
doesn't improve readability, this patch replaces SmallSet with
SmallPtrSet for pointer element types.
2025-08-18 07:01:29 -07:00
Ivan R. Ivanov
7c141e2118
[ValueTracking] Add missing check for two-value PN recurrence matching (#152700)
When InstTy is a type like IntrinsicInst which can have a variable
number of arguments, we can encounter a case where Operation will have
fewer than two arguments and error at the getOperand() calls.

Fixes: https://github.com/llvm/llvm-project/issues/152725.
2025-08-08 17:39:24 +02:00
Abhishek Kaushik
30728eb26b
[Reland][ValueTracking] Improve Bitcast handling to match SDAG (#145223)
Fixes #125228

---------

Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-08-04 14:51:03 +05:30
Jasmine Tang
e7ac49977a
[InstSimplify] Add poison propagation for trivially vectorizable intrinsics (#149243)
Fixes https://github.com/llvm/llvm-project/issues/146769

Test cases added to
`llvm/test/Transforms/InstSimplify/fold-intrinsics.ll`
2025-07-19 19:37:21 -07:00
jjasmine
2206c7d4af
[InstSimplify] Fold trig functions call of poison to poison (#148969)
Fold trig functions call of poison to poison.

This includes sin, cos, asin, acos, atan, atan2, sinh, cosh, sincos,
sincospi.

Test cases are fixed and also added to
llvm/test/Transforms/InstSimplify/fold-intrinsics.ll just like in
https://github.com/llvm/llvm-project/pull/146750
2025-07-16 08:35:13 -07:00
Ramkumar Ramachandra
1693ac35aa
[ValueTracking] Improve code using dropSameSign (NFC) (#147367) 2025-07-08 14:39:33 +01:00
jjasmine
68309adef3
[NFC] Clean up poison folding in simplifyBinaryIntrinsic (#147259)
Fixes #147116.
2025-07-07 23:21:09 +08:00
AZero13
16dad11f50
[ValueTracking] Have sub and xor in KnownNonZero take the same exact path (#146975)
If x - y == 0, then x ^ y == 0. Therefore, we can do the exact same
checks.

https://alive2.llvm.org/ce/z/MtBRoj
2025-07-07 15:18:48 +02:00
jjasmine
07286b1fcd
[InstCombine] Propagate poison pow[i], [us]add, [us]sub and [us]mul (#146750)
Fixes #146560 as well as propagate poison for [us]add, [us]sub and
[us]mul
2025-07-04 22:55:07 +01:00
Nikita Popov
1d5d125648
[ConstantFolding] Consolidate poison propagation for intrinsics (#146878)
This consolidates the "fold poison arg to poison result" constant
folding logic for intrinsics, based on a common
intrinsicPropagatesPoison() helper, which is also used for poison
propagation reasoning in ValueTracking. This ensures that the set of
supported intrinsics is consistent.

This add ucmp, scmp, smul.fix, smul.fix.sat, canonicalize and sqrt to
the intrinsicPropagatesPoison list, as these were handled by
ConstantFolding but not ValueTracking. The ctpop test is an example of
the converse, where it was handled by ValueTracking but not
ConstantFolding.
2025-07-04 09:16:28 +02:00
Antonio Frighetto
c11ea449e5 [ValueTracking] Add matchSimpleBinaryIntrinsicRecurrence helper
Similarly to what it is being done to match simple recurrence cycle
relations, attempt to match value-accumulating recurrences of kind:
```
  %umax.acc = phi i8 [ %umax, %backedge ], [ %a, %entry ]
  %umax = call i8 @llvm.umax.i8(i8 %umax.acc, i8 %b)
```
Preliminary work to let InstCombine avoid folding such recurrences,
so that simple loop-invariant computation may get hoisted. Minor
opportunity to refactor out code as well.
2025-06-27 19:04:28 +02:00
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