1701 Commits

Author SHA1 Message Date
Matt Arsenault
2ec19b86b5
ValueTracking: x - floor(x) cannot introduce overflow (#189003)
This returns a value with an absolute value less than 1 so it
should be possible to propagate no-infs.
2026-04-02 19:26:22 +00:00
Matt Arsenault
6c923741c1
ValueTracking: llvm.amdgcn.fract cannot introduce overflow (#189002)
This returns a value with an absolute value less than 1.
2026-04-01 21:07:57 +00:00
zGoldthorpe
0b500d5446
[Support] Move KnownFPClass inference from KnownBits to Support (#189414)
Move logic for inferring `KnownFPClass` from known bits into the Support
library so the logic may be used e.g., for analogous value tracking
functions in SelectionDAG.
2026-03-30 12:36:48 -06:00
Alexis Engelke
7581430722
[IR] Require well-formed IR for BasicBlock::getTerminator (#189416)
BasicBlock::getTerminator() is frequently called on valid IR, yet the
function has to check that the last instruction is in fact a terminator,
even in release builds. This check can only be optimized away when the
instruction is dereferenced.

Therefore, introduce the functions hasTerminator() and
getTerminatorOrNull() as replacement and require (assert) that
getTerminator() always returns a valid terminator. As a side effect,
this forces explicit expression of intent at call sites when unfinished
basic blocks should be supported.
2026-03-30 18:57:37 +02:00
Henrik G. Olsson
e9119107cf
[LLVM] remove redundant uses of dyn_cast (NFC) (#189105)
This removes dyn_cast invocations where the argument is already of the
target type (including through subtyping). This was created by adding a
static assert in dyn_cast and letting an LLM iterate until the code base
compiled. I then went through each example and cleaned it up. This does
not commit the static assert in dyn_cast, because it would prevent a lot
of uses in templated code. To prevent backsliding we should instead add
an LLVM aware version of
https://clang.llvm.org/extra/clang-tidy/checks/readability/redundant-casting.html
(or expand the existing one).
2026-03-29 18:26:54 +00:00
134ARG
331c1c0b84
[ValueTracking] Refine SIToFP/UIToFP FPClass inference with KnownBits (#187185)
This patch propagates the KnownBits of the source integer to improve
floating-point class inference for sitofp and uitofp instructions.

Specifically,
1. The result is never -0.0.
2. The result is not +0.0 if the source integer is known non-zero.
3. The result is not negative if the source integer is known
non-negative (or for uitofp).
4. The result is not Infinity if the largest possible integer magnitude
fits within the target FP type's exponent limits.

alive2 results for added testcases:
testcase 1: https://alive2.llvm.org/ce/z/eM34LB
testcase 2: https://alive2.llvm.org/ce/z/ext7XF 
testcase 3: https://alive2.llvm.org/ce/z/g8yb6q
testcase 4: https://alive2.llvm.org/ce/z/cyFYRy
testcase 5: https://alive2.llvm.org/ce/z/LePFrm

alive2 for updated testcase in binop-itofp:

updated 1: https://alive2.llvm.org/ce/z/KPQ5bZ
udpated 2: https://alive2.llvm.org/ce/z/bGf43t
updated 3: https://alive2.llvm.org/ce/z/YKnCwU
updaetd 4: https://alive2.llvm.org/ce/z/mqKaq-
updated 5: https://alive2.llvm.org/ce/z/jYSAB5

Fix #186952
2026-03-26 18:14:58 +01:00
Abu
6f68daa42c
[InstCombine] Recognize non-negative subtraction patterns (#182597)
Alive2 proofs:
smin pattern: https://alive2.llvm.org/ce/z/-E2Tpc
2026-03-17 16:44:19 +00:00
134ARG
b005ff76f3
[ValueTracking] frem in computeKnownFPClass can not return +/-Inf (#186748)
`frem` only produces finite numbers or NaN, never +/-Inf. Before the
patch `computeKnownFPClass` failed to clear the `fcInf` mask for
`Instruction::FRem`, causing potential missed optimizations.

Fix #186746.
2026-03-17 04:07:48 +00:00
Alexis Engelke
e8a4050263
[IR] Drop BasicBlockEdge::isSingleEdge (#186767)
This was only called on CondBr instructions, where it is always faster
to access the successors directly than to use successors().

Multi-edges don't dominate anything, so this rare case is often already
handled by dominates().

There is also a very small (hardly measurable) performance
improvement here (it did show up in profiles at 0.03% or so).
2026-03-16 13:28:48 +01:00
Alexis Engelke
94da4039cb
[Analysis][NFC] Drop use of BranchInst (#186374)
Largely straight-forward replacement.
2026-03-13 13:42:19 +00:00
Alexis Engelke
4fd826d1f9
[IR] Split Br into UncondBr and CondBr (#184027)
BranchInst currently represents both unconditional and conditional
branches. However, these are quite different operations that are often
handled separately. Therefore, split them into separate opcodes and
classes to allow distinguishing these operations in the type system.
Additionally, this also slightly improves compile-time performance.
2026-03-11 12:31:10 +00:00
Matt Arsenault
a212ebd471
ValueTracking: Handle constant structs in computeKnownFPClass (#184192)
Also fix attributor not bothering to deal with structs.
2026-03-04 20:12:28 +01:00
Matt Arsenault
20902f0b72
ValueTracking: Teach computeKnownFPClass to look at bitcast + integer max (#184073)
The returned class will still be one of the bitpatterns.

This pattern is used in rocm device libraries in assorted functions,
e.g.,

https://github.com/ROCm/llvm-project/blob/amd-staging/amd/device-libs/ocml/src/rlen3F.cl#L20

I believe it is blocking the eliminationg of finite checks in some of
the more complex functions.
2026-03-04 19:06:47 +00:00
Matt Arsenault
d9d6b16cc6
ValueTracking: Handle ConstantDataSequential in computeKnownFPClass (#184191) 2026-03-04 17:43:08 +00:00
Gergo Stomfai
eb1e808fdb
[IR] Mark reduction intrinsics as nocreateundeforpoison (#184173)
In investigating #156233, it came up that select folds like here:
https://alive2.llvm.org/ce/z/Y6jzj6 cannot be carried out, or easily
fixed for now, because integer reductions do not propagate noundef, even
if their arguments are noundef. This patch adds this propagation.
2026-03-03 09:51:12 +01:00
serge-sans-paille
0504af9e3b
[llvm] Turn misc copy-assign to move-assign (#184143)
That's an automated patch generated from clang-tidy
performance-use-std-move as a follow-up to #184136
2026-03-03 06:47:28 +00:00
Ruiling, Song
686987a540
ValueTracking/AMDGPU: handle mbcnt in computeKnownBitsFromOperator (#183229)
This helps canonicalize some address calculation. This would further
help immediate folding into memory load instructions in the backend.

The order changes to v_mad_u32_u24 is just because
@llvm.amdgcn.mul.u24.i32 was used in codegen prepare after this change.
It does not really change anything important.
2026-03-02 10:48:15 +08:00
Kshitij Paranjape
6301243a5d
Reapply "[ValueTracking] Propagate sign information out of loop" (#182512)
LLVM converts sqrt libcall to intrinsic call if the argument is within
the range(greater than or equal to 0.0). In this case the compiler is
not able to deduce the non-negativity on its own. Extended ValueTracking
to understand such loops.

Have created new ABI's for matching Intrinsics with three operands
(those existed only for 2 operands)
`matchSimpleTernaryIntrinsicRecurrence` and `matchThreeInputRecurrence`.

Fixes https://github.com/llvm/llvm-project/issues/174813
2026-02-27 19:33:59 +01:00
Matt Arsenault
5bbfce06e0
ValueTracking: Special case fmul by llvm.amdgcn.trig.preop (#183373)
This is another instance of the logic from #183159. If we know
one source is not-infinity, and the other source is less than or
equal to 1, this cannot overflow. Special case llvm.amdgcn.trig.preop,
as a substitute for proper range tracking. This almost enables pruning
edge case handling in trig function implementations, if not for the
recursion depth limit (but that's a problem for another day).
2026-02-26 20:42:26 +01:00
Matt Arsenault
f02c6ea833
AMDGPU: llvm.amdgcn.trig.preop cannot return negative values (#183306)
This returns a positive value less than 1.
2026-02-26 12:45:45 +01:00
Matt Arsenault
f6135213d7
ValueTracking: Move fmul constant special case to KnownFPClass (#183157) 2026-02-25 18:59:05 +01:00
Paul Walker
8645065007
[LLVM][ValueTracking] Add vector ConstantInt/FP support to isBytewiseValue(). (#182519) 2026-02-24 11:41:40 +00:00
Matt Arsenault
4a4912294e
ValueTracking: Handle tracking nan through powi (#179311) 2026-02-21 11:16:17 +01:00
Hans Wennborg
163ff50d6b
Revert "[ValueTracking] Fix crash in PR175590" (#182478)
Reverts llvm/llvm-project#180355

It caused assert failures:

```
llvm/include/llvm/IR/InstrTypes.h:2351:
Value *llvm::CallBase::getOperand(unsigned int) const:
Assertion `i_nocapture < OperandTraits<CallBase>::operands(this) &&
"getOperand() out of range!"' failed.
```

See comment on the PR for a reproducer.
2026-02-20 12:13:02 +00:00
Kshitij Paranjape
c1d25df74b
[ValueTracking] Fix crash in PR175590 (#180355)
Continuation of PR #175590 
Fixes the crash.
2026-02-20 09:56:50 +01:00
Shilei Tian
70905e0afa
[RFC][IR] Remove Constant::isZeroValue (#181521)
`Constant::isZeroValue` currently behaves same as
`Constant::isNullValue` for all types except floating-point, where it
additionally returns true for negative zero (`-0.0`). However, in
practice, almost all callers operate on integer/pointer types where the
two are equivalent, and the few FP-relevant callers have no meaningful
dependence on the `-0.0` behavior.

This PR removes `isZeroValue` to eliminate the confusing API. All
callers are changed to `isNullValue` with no test failures.

`isZeroValue` will be reintroduced in a future change with clearer
semantics: when null pointers may have non-zero bit patterns,
`isZeroValue` will check for bitwise-all-zeros, while `isNullValue` will
check for the semantic null (which
may be non-zero).
2026-02-15 12:06:42 -05:00
Qinkun Bao
43905d6c8a
Revert "[ValueTracking] Propagate sign information out of loop" (#180354)
Reverts llvm/llvm-project#175590

Break https://lab.llvm.org/buildbot/#/builders/55/builds/23820 and clang
2026-02-07 17:02:17 +00:00
Kshitij Paranjape
7054a4b8f9
[ValueTracking] Propagate sign information out of loop (#175590)
LLVM converts sqrt libcall to intrinsic call if the argument is within
the range(greater than or equal to 0.0). In this case the compiler is
not able to deduce the non-negativity on its own. Extended ValueTracking
to understand such loops.

Fixes llvm/llvm-project#174813
2026-02-07 14:36:00 +01:00
Matt Arsenault
7df2bd648e
AMDGPU: Implement computeKnownFPClass for llvm.amdgcn.fract (#179134) 2026-02-04 18:02:50 +01:00
Matt Arsenault
e747287f61
AMDGPU: Implement computeKnownFPClass for llvm.amdgcn.trig.preop (#179026)
Surprisingly this doesn't consider the special cases, and literally
just extracts the exponent and proceeds as normal.
2026-02-04 17:57:10 +01:00
Matt Arsenault
880ed01100
ValueTracking: Move powi logic to KnownFPClass (#179301)
This case is kind of ugly because we still need to look at the
known bits to short circuit the source check
2026-02-02 19:18:49 +00:00
Matt Arsenault
32ccf4eed0
ValueTracking: Move ldexp KnownFPClass handling to support (#179235)
Will enable code sharing with SImplifyDemandedFPClass, SelectionDAG
and GlobalISel.
2026-02-02 18:23:07 +00:00
Matt Arsenault
b556e41ac6
ValueTracking: Use computeKnownBits for ldexp integer handling (#179234)
Switch to using computeKnownBits instead of computeConstantRange
in computeKnownFPClass's ldexp handling. This is preparation to
move the handling into KnownFPClass. Since KnownFPClass is in Support,
it can make use of KnownBits as the input argument. ConstantRange is in
IR, so it cannot be used from Support.
2026-02-02 18:39:27 +01:00
Matt Arsenault
07ec2fa144
ValueTracking: Revert noundef checks in computeKnownFPClass for fmul/fma (#178850)
This functionally reverts fd5cfcc41311c6287e9dc408b8aae499501660e1 and
35ce17b6f6ca5dd321af8e6763554b10824e4ac4.

This was correct and necessary, but is causing performance regressions
since isGuaranteedNotToBeUndef is apparently not smart enough to detect
through recurrences. Revert this for the release branch.

Also the test coverage was inadequate for the fma case, so add a new
case which changes with and without the check.
2026-01-30 11:48:36 +01:00
Matt Arsenault
a692d171ec
ValueTracking: log never returns denormal (#178234) 2026-01-27 17:56:23 +01:00
Matt Arsenault
81fa743e3d
ValueTracking: Move sin/cos computeKnownFPClass handling to support (#178240)
These are the same for now but keep the separate names for future
use.
2026-01-27 17:47:29 +01:00
Lleu Yang
b8cccd6cef
[Analysis] computeKnownBitsFromOperator - add @llvm.clmul knownbits handling and tests (#177893)
This adds knownbits handling for `Intrinsic::clmul` inside
`computeKnownBitsFromOperator`. Tests are also included.

Closes #177550
2026-01-27 11:54:24 +00:00
Matt Arsenault
528c99df3a
ValueTracking: Use m_CheckedFp in isKnownIntegral (#178019) 2026-01-26 19:24:28 +00:00
Matt Arsenault
a25c7d7ade
ValueTracking: Extract isKnownIntegral out of AMDGPU (#177912)
Also do some basic conversions to use SimplifyQuery and add tests to
show assume works in a new context.
2026-01-26 19:55:14 +01:00
Matt Arsenault
f8a4091f15
ValueTracking: Improve nan tracking for fma square special case (#175999)
In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
2026-01-24 12:42:25 +01:00
Matt Arsenault
0993d69bc3
InstCombine: Handle fdiv in SimplifyDemandedFPClass (#175946) 2026-01-24 11:37:04 +00:00
Matt Arsenault
aa53f6f3db
ValueTracking: Improve handling for fma/fmuladd (#175614)
The handling for fma was very basic and only handled the
repeated input case. Re-use the fmul and fadd handling for more
accurate sign bit and nan handling.
2026-01-24 11:35:14 +01:00
Matt Arsenault
403502f478
InstCombine: Implement SimplifyDemandedFPClass for frexp (#176122) 2026-01-24 08:11:50 +01:00
Matt Arsenault
c991fdd4bc
InstCombine: Handle fsub in SimplifyDemandedFPClass (#175852)
alive2 fails on some of the tests, but this is due to existing
folds in instsimplify and
https://github.com/AliveToolkit/alive2/issues/1273
2026-01-23 23:59:02 +01:00
Matt Arsenault
6000703ba8
InstCombine: Handle fptrunc in SimplifyDemandedFPClass (#175421)
Also handle llvm.fptrunc.round since it's the same.
2026-01-23 23:40:59 +01:00
Matt Arsenault
6934ed51b3
IR: Add !nofpclass metadata (#177140)
This adds the analogous metadata to the nofpclass attribute
to assert values are not a certain set of floating-point classes.
This allows the same information to be expressed if a function
argument is passed indirectly. This matches the bitmask encoding
of nofpclass.

I also think this should be allowed for stores to symmetrically handle
sret, but leave that for later.

Alternatively we could add a more expressive !fprange metadata,
but that would be much more complex. It's useful to match the attribute,
and more annotations can always be added.

Fixes #133560
2026-01-22 20:49:34 +01:00
Nathan Corbyn
a8098629bc
[IR] Teach drop/hasPoisonGeneratingAnnotations() about abs, ctlz and cttz (#175941) 2026-01-15 00:01:58 +00:00
Nikolas Klauser
d2afc3e84b
[ValueTracking] Allow dereferenceable(0) to be applied to a null pointer (#175913)
`dereferenceable(<n>)` with n being potentially zero can come up when
using an operand bundle with a variable size. Currently this implies
that the pointer is non-null, even though `[nullptr, nullptr)` is a
valid range in any programming language I'm aware of. This patch removes
this implication and updates the language reference to reflect that
`dereferenceable` with a zero argument is valid.
2026-01-14 15:58:39 +01:00
Matt Arsenault
c620b47751
ValueTracking: Account for undef in adjustKnownFPClassForSelectArm (#175372)
This needs to consider undef like the KnownBits case does.
2026-01-13 20:36:00 +01:00
Justin Lebar
bbcab0bf57
[InstCombine] Fix i1 ssub.sat compare folding (#173742)
For every type other than i1, ssub.sat x, y = 0 implies x == y.  But
ssub.sat.i1 0, -1 = 0 (because the result of 1 saturates to 0).

The changes to instcombine are not strictly necessary.  Instcombine
canonicalizes the ssub.sat.i1 before we arrive at these pattern-matches.
The real fix is in ValueTracking.

Nonetheless we agreed in review it makes sense to add these checks to
instcombine, even though they're currently unreachable:
https://github.com/llvm/llvm-project/pull/173742#issuecomment-3696631396

This was found by a fuzzer I'm working on!
2026-01-12 11:03:00 -08:00