665 Commits

Author SHA1 Message Date
Lewis Crawford
729701b885
[NVPTX] Constant fold NVVM add/mul/div/fma (#152544)
Constant fold the NVVM intrinsics for add, mul, div, fma with specific
rounding modes.
2025-08-22 17:33:00 +01:00
Jasmine Tang
10d9e7b1b7
Reapply "[WebAssembly] Constant fold wasm.dot" (#153070)
In #149619, for the test of `@dot_follow_modulo_spec_2`, constant
folding the addition of two i32 1073741824 causes an overflow from 2^32
to -2^32=-2147483648, which triggers the UB sanitizer. This PR reapplies
the previous PR, explicitly casting the addition operand to int64_t
first before performing the addition before producing a int32 number via
`Constant *C = get(cast<IntegerType>(Ty->getScalarType()), V, isSigned)`
2025-08-14 18:52:35 -07:00
Alexander Richardson
3a4b351ba1
[IR] Introduce the ptrtoaddr instruction
This introduces a new `ptrtoaddr` instruction which is similar to
`ptrtoint` but has two differences:

1) Unlike `ptrtoint`, `ptrtoaddr` does not capture provenance
2) `ptrtoaddr` only extracts (and then extends/truncates) the low
   index-width bits of the pointer

For most architectures, difference 2) does not matter since index (address)
width and pointer representation width are the same, but this does make a
difference for architectures that have pointers that aren't just plain
integer addresses such as AMDGPU fat pointers or CHERI capabilities.

This commit introduces textual and bitcode IR support as well as basic code
generation, but optimization passes do not handle the new instruction yet
so it may result in worse code than using ptrtoint. Follow-up changes will
update capture tracking, etc. for the new instruction.

RFC: https://discourse.llvm.org/t/clarifiying-the-semantics-of-ptrtoint/83987/54

Reviewed By: nikic

Pull Request: https://github.com/llvm/llvm-project/pull/139357
2025-08-08 10:12:39 -07:00
Andrew Lazarev
f61526971f
Revert "[WebAssembly] Constant fold wasm.dot" (#152382)
Reverts llvm/llvm-project#149619

It breaks ubsan bot:
https://lab.llvm.org/buildbot/#/builders/25/builds/10523

Earlier today the failure was hidden by another breakage that is fixed
now.
2025-08-06 15:16:19 -07:00
Jasmine Tang
9c6bb18040
[WebAssembly] Constant fold wasm.dot (#149619)
Constant fold wasm.dot of constant vectors/splats.

Test case added in
`llvm/test/Transforms/InstSimplify/ConstProp/WebAssembly/dot.ll`

Related to https://github.com/llvm/llvm-project/issues/55933
2025-08-05 15:22:37 -07:00
Lewis Crawford
5146917407
[ConstantFolding] Fix incorrect nvvm_round folding (#151563)
The `nvvm_round` intrinsic should round to the nearest even number in
the case of ties. It lowers to PTX `cvt.rni`, which will "round to
nearest integer, choosing even integer if source is equidistant between
two integers", so it matches the semantics of `rint` (and not `round` as
the name suggests).
2025-08-01 10:31:43 +01:00
Lewis Crawford
c5327b935b
[ConstantFolding] Fix typo in GetNVVMDenormMode (#151297)
Fix typo in function name of GetNVVMDenormMode
(Denrom vs Denorm).
2025-07-30 10:48:09 +01:00
Paul Walker
1528ddbe76
[ConstantFolding][SVE] Do not fold fcmp of denormal without known mode. (#150614)
This is a follow on to
https://github.com/llvm/llvm-project/pull/115407 that introduced code
which bypasses the splat handling for scalable vectors. To maintain
existing tests I have moved the early return until after the splat
handling so all vector types are treated equally.
2025-07-29 12:37:59 +01:00
Pedro Lobo
67658af1cc
[ConstantFolding] Merge constant gep inrange attributes (#150546)
When folding a gep+gep into a single gep, intersect their `inrange`
attributes.
2025-07-25 20:02:06 +01:00
Kazu Hirata
31281da34b
[Analysis] Drop const from return types (NFC) (#150258)
We don't need const on APFloat.
2025-07-23 15:18:38 -07:00
Lewis Crawford
0823f4ff08
[ConstantFolding] Fix nvvm_round folding on PPC (#149837)
Fix a failing test for constant-folding the nvvm_round intrinsic. The
original implementation added in #141233 used a native libm call to the
"round" function, but on PPC this produces +0.0 if the input is -0.0,
which caused a test failure.

This patch updates it to use APFloat functions instead of native libm
calls to ensure cross-platform consistency.
2025-07-21 17:48:45 +01:00
Lewis Crawford
fd8ae2cb76
Add constant-folding for unary NVVM intrinsics (#141233)
Add support for constant-folding numerous NVVM unary arithmetic
intrinsics (including f, d, and ftz_f variants):
  - nvvm.ceil.*
  - nvvm.fabs.*
  - nvvm.floor.*
  - nvvm.rcp.*
  - nvvm.round.*
  - nvvm.saturate.*
  - nvvm.sqrt.f
  - nvvm.sqrt.rn.*
2025-07-21 11:32:09 +01:00
jjasmine
18286e0c2a
[WebAssembly] Constant fold SIMD intrinsics: any/alltrue (#148074)
Constant fold  SIMD wasm intrinsics: any/alltrue

Added test in `llvm/test/Transforms/InstSimplify`
2025-07-15 02:13:22 +08:00
Kazu Hirata
57cb9265bf
[Analysis] Drop const from a return type (NFC) (#147098)
We don't need const on a return type.
2025-07-04 18:38:28 -07: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
Lei Huang
4a31f7f8ea
[ConstantFold] Special case atan +/-0.0 (#143962)
C's Annex F specifies that atan +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.

Ref:
https://pubs.opengroup.org/onlinepubs/9799919799/functions/atan.html
2025-06-25 16:03:18 -04:00
Nikita Popov
80b79ce432
[ConstantFolding] Handle reading from type padding (#144330)
ReadDataFromGlobal() did not handle reads from the padding of types (in
the sense of type store size != type alloc size, rather than struct
padding).

Return zero in that case.

Fixes https://github.com/llvm/llvm-project/issues/144279.
2025-06-17 09:30:28 +02:00
Craig Topper
417ab37d85
[ConstantFolding] Fold deinterleave2 of any splat vector not just zeroinitializer (#144144)
While there remove an unnecessary dyn_cast from Constant to Constant.
Reverse a branch condition into an early out to reduce nesting.
2025-06-13 17:11:52 -07:00
Kazu Hirata
bcfbba12e6
[llvm] Compare std::optional<T> to values directly (NFC) (#143913)
This patch transforms:

  X && *X == Y

to:

  X == Y

where X is of std::optional<T>, and Y is of T or similar.
2025-06-13 08:11:20 -07:00
Nikolay Panchenko
ee35e34294
[ConstantFolding] Add folding for [de]interleave2, insert and extract (#141301)
The change adds folding for 4 vector intrinsics: `interleave2`,
`deinterleave2`, `vector_extract` and `vector_insert`. For the last 2
intrinsics the change does not use `ShuffleVector` fold mechanism as
it's much simpler to construct result vector explicitly.
2025-06-11 19:00:29 -04:00
Luke Lau
19b0e1227c
[ConstantFolding] Fold sqrt poison -> poison (#141821)
I noticed this when a sqrt produced by VectorCombine with a poison
operand wasn't getting folded away to poison.

Most intrinsics in general could probably be folded to poison if one of
their arguments are poison too. Are there any exceptions to this we need
to be aware of?
2025-06-11 13:27:14 +02:00
Craig Topper
112490f06a
[ConstantFolding] Add support for llvm.atan in constant folding. (#143416)
Fixes #143360
2025-06-09 12:38:08 -07:00
Lewis Crawford
1f7885cf9c
[ConstantFolding] Add flag to disable call folding (#140270)
Add an optional flag to disable constant-folding for function calls.
This applies to both intrinsics and libcalls.

This is not necessary in most cases, so is disabled by default, but in
cases that require bit-exact precision between the result from
constant-folding and run-time execution, having this flag can be useful,
and may help with debugging. Cases where mismatches can occur include
GPU execution vs host-side folding, cross-compilation scenarios, or
compilation vs execution environments with different math library
versions.

This applies only to calls, rather than all FP arithmetic. Methods such
as fast-math-flags can be used to limit reassociation, fma-fusion etc,
and basic arithmetic operations are precisely defined in IEEE 754.
However, other math operations such as sqrt, sin, pow etc. represented
by either libcalls or intrinsics are less well defined, and may vary
more between different architectures/library implementations.

As this option is not intended for most common use-cases, this patch
takes the more conservative approach of disabling constant-folding even
for operations like fmax, copysign, fabs etc. in order to keep the
implementation simple, rather than sprinkling checks for this flag
throughout.

The use-cases for this option are similar to StrictFP, but it is only
limited to FP call folding, rather than all FP operations, as it is
about precise arithmetic results, rather than FP environment behaviours.
It also can be used to when linking .bc files compiled with different
StrictFP settings with llvm-link.
2025-05-30 11:27:18 +01:00
Luke Lau
64106581b9
[Constant] Make Constant::getSplatValue return poison on poison (#141870)
This is a follow up from #141845. 

TargetTransformInfo::getOperandInfo needs to be updated to check for
undef values as otherwise a splat is considered a constant, and some
RISC-V cost model tests will start adding a cost to materialize the
constant.
2025-05-29 15:01:28 +01:00
Luke Lau
79ae407c95
[ConstantFolding] Fold intrinsics of scalable vectors with splatted operands (#141845)
As noted in
https://github.com/llvm/llvm-project/pull/141821#issuecomment-2917328924,
whilst we currently constant fold intrinsics of fixed-length vectors via
their scalar counterpart, we don't do the same for scalable vectors.

This handles the scalable vector case when the operands are splats.

One weird snag in ConstantVector::getSplat was that it produced a undef
if passed in poison, so this also contains a fix by checking for
PoisonValue before UndefValue.
2025-05-28 23:29:41 +01:00
Lewis Crawford
9c88b6d689
[ConstantFolding] Fold maximumnum and minimumnum (#138700)
Add constant-folding support for the maximumnum and minimumnum
intrinsics, and extend the tests to show the qnan vs snan behavior
differences between maxnum/maximum/maximumnum.
2025-05-08 18:00:49 +02:00
Luke Lau
d33c676468
[ConstantFolding] Constify ConstantFoldInstOperands and ConstantFoldInstruction argument. NFC (#138108)
I tried to use these with a const reference in a separate patch, but the
pointers weren't marked as const. It turns out that these don't mutate
the instruction.
2025-05-01 22:39:37 +08:00
Iris
3295970d84
[ConstantFolding] Add support for sinh and cosh intrinsics in constant folding (#132671)
Closes #132503.
2025-04-03 08:34:09 +02:00
Nikita Popov
8758e5fe47
[ConstantFolding] Fix handling of index width != pointer width (#130608)
Per LangRef:

> The offsets are then added to the low bits of the base address up to
the index type width, with silently-wrapping two’s complement
arithmetic. If the pointer size is larger than the index size, this
means that the bits outside the index type width will not be affected.

The transform as implemented was doubly wrong, because it just truncated
the original base pointer to the index width, losing the top bits
entirely. Make sure we preserve the bits and use wrapping arithmetic
within the low bits.
2025-03-11 09:02:34 +01:00
Florian Hahn
55815b621b
[Value] Look through inttoptr (add ..) in accumulateConstantOffsets (#124981)
Look through inttoptr (add (ptrtoint P), C) when accumulating offsets.

Adds a missing fold after
https://github.com/llvm/llvm-project/pull/123518

Alive2 for the tests with changes: https://alive2.llvm.org/ce/z/VvPrzv

PR: https://github.com/llvm/llvm-project/pull/124981
2025-01-30 20:58:38 +00:00
Kshitij Paranjape
19bd2d6102
[ConstantFolding] Add ilogb in isMathLibCallNoop (#122582)
ilogb libcall was not being constant folded correctly. This patch adds 
ilogb case in isMathLibCallNoop with correct error condition.

Fixes #101873
2025-01-20 12:19:07 -05:00
Lewis Crawford
cea92446ac
[NVPTX] Constant fold NVVM fmin and fmax (#121966)
Add constant-folding for nvvm float/double fmin + fmax intrinsics,
including all combinations of xorsign.abs, nan-propagation, and ftz.
2025-01-16 14:38:51 +00:00
Fangrui Song
1e53f9523d
[CMake] Remove some always-true HAVE_XXX_H
These are unneeded even on AIX, PURE_WINDOWS, and ZOS (per #104706)

* HAVE_ERRNO_H: introduced by 1a93330ffa2ae2aa0b49461f05e6f0d51e8443f8 (2009) but unneeded.
  The guarded ABI is unconditionally used by lldb.
* HAVE_FCNTL_H
* HAVE_FENV_H
* HAVE_SYS_STAT_H

Pull Request: https://github.com/llvm/llvm-project/pull/123087
2025-01-15 09:53:21 -08:00
Lewis Crawford
a629d9e102
[NVPTX] Constant-folding for f2i, d2ui, f2ll etc. (#118965)
Add constant-folding support for the NVVM intrinsics for converting
float/double to signed/unsigned int32/int64 types, including all
rounding-modes and ftz modifiers.
2025-01-07 13:17:36 +00:00
Finn Plummer
45c01e8a33
[NFC][TargetTransformInfo][VectorUtils] Consolidate isVectorIntrinsic... api (#117635)
- update `VectorUtils:isVectorIntrinsicWithScalarOpAtArg` to use TTI for
all uses, to allow specifiction of target specific intrinsics
- add TTI to the `isVectorIntrinsicWithStructReturnOverloadAtField` api
- update TTI api to provide `isTargetIntrinsicWith...` functions and
  consistently name them
- move `isTriviallyScalarizable` to VectorUtils
  
- update all uses of the api and provide the TTI parameter

Resolves #117030
2024-12-19 11:54:26 -08:00
Paul Walker
3654f1baa6
[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:FoldBitCast. (#117163) 2024-12-10 15:42:05 +00:00
Nikita Popov
10f315dc9c
[ConstantFolding] Infer getelementptr nuw flag (#119214)
Infer nuw from nusw and nneg. This is the constant expression variant of
https://github.com/llvm/llvm-project/pull/111144.

Proof: https://alive2.llvm.org/ce/z/ihztLy
2024-12-09 16:44:05 +01:00
Benjamin Maxwell
24561f44df
Reland "[InstSimplify] Add basic constant folding for llvm.sincos" (#119192)
This calls into the existing constant folding for `llvm.sin` and
`llvm.cos`, which currently does not fold for any non-finite values, so
most tests are negative tests at the moment.

Note: The constant folding does not consider the `afn` fast-math flag
and will produce the same result regardless of if the flag is set.

This is a reland of #114527 that updates the syntax of one of the tests
from: `<float 1.000000e+00, float 1.000000e+00>` to `splat (float
1.000000e+00)`.
2024-12-09 11:16:14 +00:00
Benjamin Maxwell
c4aa67e866
Revert "[InstSimplify] Add basic constant folding for llvm.sincos" (#119149)
Reverts llvm/llvm-project#114527

Reverting due to buildbot failures (e.g.
https://lab.llvm.org/buildbot/#/builders/180/builds/9685)
2024-12-08 21:29:30 +00:00
Benjamin Maxwell
47df46b1e7
[InstSimplify] Add basic constant folding for llvm.sincos (#114527)
This calls into the existing constant folding for `llvm.sin` and
`llvm.cos`, which currently does not fold for any non-finite values, so
most tests are negative tests at the moment.

Note: The constant folding does not consider the `afn` fast-math flag
and will produce the same result regardless of if the flag is set.
2024-12-08 21:16:50 +00:00
Pedro Lobo
0d1e762da7
[InstSimplify] Refine abs(min/undef, true) to poison (#118669)
Calls to `@llvm.abs(undef, i1 true)` and `@llvm.abs(INT_MIN, i1 true)`
can be optimized to `poison` instead of `undef`.

[Alive2](https://alive2.llvm.org/ce/z/Hg-2ug)
2024-12-04 18:41:05 +00:00
Paul Walker
a88653a2cd
[LLVM][IR] When evaluating GEP offsets don't assume ConstantInt is a scalar. (#117162) 2024-12-04 12:45:30 +00:00
Matt Arsenault
cd88bfcb59
ConstantFolding: Do not fold fcmp of denormal without known mode (#115407)
Fixes #114947
2024-11-13 07:43:50 -08:00
Hubert Tong
5091a359d9
[ConstantFold] Special case log1p +/-0.0 (#114635)
C's Annex F specifies that log1p +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.
2024-11-02 20:06:39 -04:00
c8ef
cf0b6cc711
Revert "[ConstantFold] Fold tgamma and tgammaf when the input parameter is a constant value." (#114496)
Reverts llvm/llvm-project#114065
2024-11-01 09:26:11 +08:00
c8ef
1f07f995cc
[ConstantFold] Fold tgamma and tgammaf when the input parameter is a constant value. (#114065)
This patch adds support for constant folding for the `tgamma` and
`tgammaf` libc functions.
2024-11-01 09:07:55 +08:00
c8ef
b90ea5caad
[ConstantFold] Fold erf and erff when the input parameter is a constant value. (#113079)
This patch adds support for constant folding for the `erf` and `erff`
libc functions.
2024-10-22 12:58:11 +08:00
Nikita Popov
a18dd29077 [ConstantFolding] Set signed/implicitTrunc when handling GEP offsets
GEP offsets have sext_or_trunc semantics. We were already doing
this for the outer-most GEP, but not for the inner ones.

I believe one of the sanitizer buildbot failures was due to this,
but I did not manage to reproduce the issue or come up with a
test case. Usually the problematic case will already be folded
away due to index type canonicalization.
2024-10-21 12:47:02 +02:00
c8ef
1336e3d0b9
[ConstantFold] Fold ilogb and ilogbf when the input parameter is a constant value. (#113014)
This patch adds support for constant folding for the `ilogb` and
`ilogbf` libc functions.
2024-10-20 10:46:35 +08:00
Mohammed Keyvanzadeh
721b796809
[llvm] prefer isa_and_nonnull over v && isa (#112541)
Use `isa_and_nonnull<T>(v)` instead of `v && isa<T>(v)`, where `v` is
evaluated twice in the latter.
2024-10-18 19:12:04 +03:30