1633 Commits

Author SHA1 Message Date
Nikita Popov
30a1ffb7ab
[ValueTracking] Support ptrtoaddr in inequality implication (#173362)
`ptrtoaddr(p1) - ptrtoaddr(p2) == non-zero` implies `p1 != p2`, same as
for ptrtoint.
2026-01-05 09:39:16 +01:00
Nikita Popov
e65317acbb
[ValueTracking] Support ptrtoaddr in computeKnownBits() (#173358)
ptrtoaddr can be handled the same as ptrtoint here. The pointer known
bits cover the full pointer width, and ptrtoaddr either passes those
through directly or truncates to the address size.
2026-01-05 09:37:34 +01:00
Matt Arsenault
c03b6f3d2a
Partially reapply "ValueTracking: Improve handling of fadd in computeKnownFPClass." (#174290) (#174332)
This partially reverts commit 108a22ed5fa1836b4cfcd05e9d96f98a533068d5.

Restore the sign-bit tracking for both inputs known-negative case,
and leave the 0 handling for later. There is a libc test improperly
relying on running code compiled for IEEE behavior that changed
the output denormal mode.
2026-01-04 15:02:33 +00:00
Aiden Grossman
108a22ed5f
Revert "ValueTracking: Improve handling of fadd in computeKnownFPClass." (#174290)
Reverts llvm/llvm-project#174123

This caused test failures within LLVM libc. They can be reproduced by
doing a libc build against a clang with this commit included and running
`ninja -k 0 libc.test.src.math.smoke.log1p_test.__unit__
libc.test.src.math.smoke.log1p_test.__unit__.__NO_FMA_OPT`.
2026-01-03 14:12:21 -08:00
Matt Arsenault
00fdb29752
ValueTracking: Avoid unnecessary denormal mode lookup for fadd (#174272)
The mode was already queried, so don't do it again.
2026-01-03 10:59:32 +00:00
Matt Arsenault
5cbc6a63a1
ValueTracking: Improve handling of fadd in computeKnownFPClass. (#174123)
This already recognized that if both inputs are positive, the
result is positive. Extend this to the mirror situation with
negative inputs.

Also special case fadd x, x. Canonically, fmul x, 2 is fadd x, x.
We can tell the sign bit won't change, and 0 will propagate.
2026-01-03 10:57:01 +00:00
Victor Chernyakin
c438773432
[LLVM][ADT] Migrate users of make_scope_exit to CTAD (#174030)
This is a followup to #173131, which introduced the CTAD functionality.
2026-01-02 20:42:56 -08:00
Matt Arsenault
e4722c69b8
InstCombine: Handle exp/exp2/exp10 in SimplifyDemandedFPClass (#173432)
I'm working on optimizing out the tail sequences in the
implementations of the 4 different flavors of pow. These
include chains of selects on the various edge cases.

Related to #64870
2025-12-29 15:03:13 +00:00
Matt Arsenault
3b09719963
ValueTracking: Improve handling of exp intrinsic for overflow (#173430)
Teach exp handling that positive inputs cannot introduce overflow,
and negative inputs cannot introduce underflow.
2025-12-29 14:24:24 +00:00
Matt Arsenault
c8d7e53749
InstCombine: Handle canonicalize in SimplifyDemandedFPClass (#173189) 2025-12-29 14:32:12 +01:00
Matt Arsenault
559e8131ec
ValueTracking: log may never return -0 (#173380) 2025-12-29 11:08:08 +01:00
Matt Arsenault
6785c40bfd
ValueTracking: Avoid calling computeKnownFPClass on matched constant (#173248)
The fmul case already tries to match a literal value, we don't
need to match it twice.
2025-12-24 23:01:56 +00:00
Matt Arsenault
5020e0ff14
ValueTracking: Improve computeKnownFPClass fmul handling (#173247)
Improve known non-nan sign bit tracking. Handle cases with
a known 0 or inf input of indeterminate sign.

The tails of some library functions have sign management
for special cases.
2025-12-24 22:17:58 +00:00
Nikita Popov
261d2da617
[ValueTracking] Support ptrtoaddr in isKnownNonZero() (#173275)
Add support for ptrtoaddr in isKnownNonZero(). We can directly forward
to isKnownNonZero() for the pointer here, as we define nonnull as
applying to the address bits.

Also adjust the ptrtoint implementation to match, by requiring that the
result type >= address size (rather than >= pointer size). This is just
for clarity, in practice this is a non-canonical form.
2025-12-23 11:21:50 +01:00
Matt Arsenault
42b3483ac4
ValueTracking: Improve accuracy of 0 handling with PreserveSign (#173165)
If the source value is known not subnormal and not zero with the
same sign, we can infer the result is also not zero with the same
sign.
2025-12-21 11:14:24 +00:00
Sayan Sivakumaran
6d63f12dfe
[InstCombine] Propagate poison through fshl and fshr intrinsics (#172859)
Currently these intrinsics output `undef` on poison, which triggers CI
errors on PRs that want to add poison tests for funnel shifts (such as
#172723). Let's make `fshl` and `fshr` propagate poison instead.
2025-12-20 23:53:01 +08:00
Matt Arsenault
c68fa5ebab
AMDGPU: Handle amdgcn_rcp in computeKnownFPClass (#172490) 2025-12-18 13:50:11 +01:00
Matt Arsenault
7b9168caa9
ValueTracking: Handle amdgcn_exp2 in computeKnownFPClass (#172495) 2025-12-17 23:17:13 +01:00
Matt Arsenault
82cdc300fa
ValueTracking: Handle amdgcn_log in computeKnownFPClass (#172492) 2025-12-17 23:16:25 +01:00
Matt Arsenault
44c0469e5f
ValueTracking: Handle amdgcn.rsq intrinsic in computeKnownFPClass (#171837)
We have other target intrinsics already in ValueTracking functions,
and no access to TTI.
2025-12-12 16:48:12 +01:00
Matt Arsenault
39fcd2cb81
ValueTracking: Teach computeKnownFPClass that multiply can avoid denormals (#171730)
Multiply by large constant can be used to scale denormal inputs into
a normal range. This pattern appears frequently in math function library
implementations to make use of hardware instructions that do not support
denormals. We already handle this case for ldexp, but now canonicalize
ldexp by a constant to an fmul.

The test cases are mostly the existing nofpclass test for ldexp,
run through the new instcombine to replace ldexp with fmul.
2025-12-11 12:39:58 +00:00
Adar Dagan
488202935e
[ValueTracking] Enhance overflow computation for unsigned mul (#171568)
Changed the range computation in computeOverflowForUnsignedMul to use
computeConstantRange as well.

This expands the patterns that InstCombine manages to narrow a mul that
has values that come from zext, for example if a value comes from a div
operation then the known bits doesn't give the narrowest possible range
for that value.

---------

Co-authored-by: Adar Dagan <adar.dagan@mobileye.com>
2025-12-11 08:13:23 +01:00
Yingwei Zheng
6af1c3f3a9
[ValueTracking] Support scalable vector splats in computeKnownBits (#170345)
Similar to https://github.com/llvm/llvm-project/pull/170325, this patch
adds support for scalable vector splats in computeKnownBits.
2025-12-03 20:37:30 +08:00
Yingwei Zheng
e0f330293e
[ValueTracking] Support scalable vector splats in computeKnownFPClass (#170325)
Address comment
https://github.com/llvm/llvm-project/pull/169904#discussion_r2576299467
2025-12-02 17:56:38 +00:00
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