aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
AgeCommit message (Collapse)AuthorFilesLines
5 days[WebAssembly] Constant fold SIMD intrinsics: any/alltrue (#148074)jjasmine1-1/+18
Constant fold SIMD wasm intrinsics: any/alltrue Added test in `llvm/test/Transforms/InstSimplify`
2025-07-04[Analysis] Drop const from a return type (NFC) (#147098)Kazu Hirata1-1/+1
We don't need const on a return type.
2025-07-04[ConstantFolding] Consolidate poison propagation for intrinsics (#146878)Nikita Popov1-34/+5
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-06-25 [ConstantFold] Special case atan +/-0.0 (#143962)Lei Huang1-0/+6
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-17[ConstantFolding] Handle reading from type padding (#144330)Nikita Popov1-0/+4
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-13[ConstantFolding] Fold deinterleave2 of any splat vector not just ↵Craig Topper1-22/+21
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[llvm] Compare std::optional<T> to values directly (NFC) (#143913)Kazu Hirata1-2/+2
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-11[ConstantFolding] Add folding for [de]interleave2, insert and extract (#141301)Nikolay Panchenko1-0/+97
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[ConstantFolding] Fold sqrt poison -> poison (#141821)Luke Lau1-1/+6
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-09[ConstantFolding] Add support for llvm.atan in constant folding. (#143416)Craig Topper1-0/+3
Fixes #143360
2025-05-30[ConstantFolding] Add flag to disable call folding (#140270)Lewis Crawford1-3/+23
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-29[Constant] Make Constant::getSplatValue return poison on poison (#141870)Luke Lau1-5/+0
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-28[ConstantFolding] Fold intrinsics of scalable vectors with splatted operands ↵Luke Lau1-1/+29
(#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-08[ConstantFolding] Fold maximumnum and minimumnum (#138700)Lewis Crawford1-0/+8
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-01[ConstantFolding] Constify ConstantFoldInstOperands and ↵Luke Lau1-3/+4
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-04-03[ConstantFolding] Add support for `sinh` and `cosh` intrinsics in constant ↵Iris1-0/+6
folding (#132671) Closes #132503.
2025-03-11[ConstantFolding] Fix handling of index width != pointer width (#130608)Nikita Popov1-3/+6
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-01-30[Value] Look through inttoptr (add ..) in accumulateConstantOffsets (#124981)Florian Hahn1-4/+9
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-20[ConstantFolding] Add ilogb in isMathLibCallNoop (#122582)Kshitij Paranjape1-0/+3
ilogb libcall was not being constant folded correctly. This patch adds ilogb case in isMathLibCallNoop with correct error condition. Fixes #101873
2025-01-16[NVPTX] Constant fold NVVM fmin and fmax (#121966)Lewis Crawford1-3/+136
Add constant-folding for nvvm float/double fmin + fmax intrinsics, including all combinations of xorsign.abs, nan-propagation, and ftz.
2025-01-15[CMake] Remove some always-true HAVE_XXX_HFangrui Song1-2/+2
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-07[NVPTX] Constant-folding for f2i, d2ui, f2ll etc. (#118965)Lewis Crawford1-0/+139
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.
2024-12-19[NFC][TargetTransformInfo][VectorUtils] Consolidate `isVectorIntrinsic...` ↵Finn Plummer1-1/+1
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-10[LLVM][IR] Add support for vector ConstantInt/FP to ↵Paul Walker1-5/+10
ConstandFolding:FoldBitCast. (#117163)
2024-12-09[ConstantFolding] Infer getelementptr nuw flag (#119214)Nikita Popov1-1/+4
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-09Reland "[InstSimplify] Add basic constant folding for `llvm.sincos`" (#119192)Benjamin Maxwell1-0/+35
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-08Revert "[InstSimplify] Add basic constant folding for `llvm.sincos`" (#119149)Benjamin Maxwell1-35/+0
Reverts llvm/llvm-project#114527 Reverting due to buildbot failures (e.g. https://lab.llvm.org/buildbot/#/builders/180/builds/9685)
2024-12-08[InstSimplify] Add basic constant folding for `llvm.sincos` (#114527)Benjamin Maxwell1-0/+35
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-04[InstSimplify] Refine `abs(min/undef, true)` to `poison` (#118669)Pedro Lobo1-2/+2
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[LLVM][IR] When evaluating GEP offsets don't assume ConstantInt is a scalar. ↵Paul Walker1-1/+1
(#117162)
2024-11-13ConstantFolding: Do not fold fcmp of denormal without known mode (#115407)Matt Arsenault1-40/+105
Fixes #114947
2024-11-02[ConstantFold] Special case log1p +/-0.0 (#114635)Hubert Tong1-0/+3
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-01Revert "[ConstantFold] Fold `tgamma` and `tgammaf` when the input parameter ↵c8ef1-16/+3
is a constant value." (#114496) Reverts llvm/llvm-project#114065
2024-11-01[ConstantFold] Fold `tgamma` and `tgammaf` when the input parameter is a ↵c8ef1-3/+16
constant value. (#114065) This patch adds support for constant folding for the `tgamma` and `tgammaf` libc functions.
2024-10-22[ConstantFold] Fold `erf` and `erff` when the input parameter is a constant ↵c8ef1-3/+7
value. (#113079) This patch adds support for constant folding for the `erf` and `erff` libc functions.
2024-10-21[ConstantFolding] Set signed/implicitTrunc when handling GEP offsetsNikita Popov1-1/+2
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-20[ConstantFold] Fold `ilogb` and `ilogbf` when the input parameter is a ↵c8ef1-1/+9
constant value. (#113014) This patch adds support for constant folding for the `ilogb` and `ilogbf` libc functions.
2024-10-18[llvm] prefer isa_and_nonnull over v && isa (#112541)Mohammed Keyvanzadeh1-2/+2
Use `isa_and_nonnull<T>(v)` instead of `v && isa<T>(v)`, where `v` is evaluated twice in the latter.
2024-10-17[APInt] Fix APInt constructions where value does not fit bitwidth (NFCI) ↵Nikita Popov1-1/+2
(#80309) This fixes all the places that hit the new assertion added in https://github.com/llvm/llvm-project/pull/106524 in tests. That is, cases where the value passed to the APInt constructor is not an N-bit signed/unsigned integer, where N is the bit width and signedness is determined by the isSigned flag. The fixes either set the correct value for isSigned, set the implicitTrunc flag, or perform more calculations inside APInt. Note that the assertion is currently still disabled by default, so this patch is mostly NFC.
2024-10-16[ConstantFold] Fold `log1p` and `log1pf` when the input parameter is a ↵c8ef1-3/+9
constant value. (#112113) This patch adds support for constant folding for the `log1p` and `log1pf` libc functions.
2024-10-10[ConstantFold] Fold `logb` and `logbf` when the input parameter is a ↵c8ef1-3/+8
constant value. (#111232) This patch adds support for constant folding for the `logb` and `logbf` libc functions.
2024-10-08[LLVM][ConstFolds] Verify a scalar src before attempting scalar->vector ↵Paul Walker1-1/+2
bitcast transformation. (#111149) It was previously safe to assume isa<Constant{Int,FP}> meant a scalar value. This is not true when use-constant-##-for-###-splat are enabled.
2024-08-25Revert "Enable logf128 constant folding for hosts with 128bit long double ↵NAKAMURA Takumi1-24/+6
(#104929)" ConstantFolding behaves differently depending on host's `HAS_IEE754_FLOAT128`. LLVM should not change the behavior depending on host configurations. This reverts commit 14c7e4a1844904f3db9b2dc93b722925a8c66b27. (llvmorg-20-init-3262-g14c7e4a18449 and llvmorg-20-init-3498-g001e423ac626)
2024-08-24[ConstantFolding] Ensure TLI is valid when simplifying fp128 intrinsics.David Green1-1/+1
TLI might not be valid for all contexts that constant folding is performed. Add a quick guard that it is not null.
2024-08-22Enable logf128 constant folding for hosts with 128bit long double (#104929)Matthew Devereau1-6/+24
This is a reland of (#96287). This patch attempts to reduce the reverted patch's clang compile time by removing #includes of float128.h and inlining convertToQuad functions instead.
2024-08-14Revert "Reland logf128 constant folding (#103217)"Nikita Popov1-2/+2
This reverts commit 3cab7c555ad6451f2b1b4dc918a4b4f4e4a3e45d. The modified test fails on ppc64le buildbots.
2024-08-14Reland logf128 constant folding (#103217)Matthew Devereau1-2/+2
This is a reland of #96287. This change makes tests in logf128.ll ignore the sign of NaNs for negative value tests and moves an #include <cmath> to be blocked behind #ifndef _GLIBCXX_MATH_H.
2024-08-12[ConstantFolding] Use getSigned()Nikita Popov1-2/+3
Split out from https://github.com/llvm/llvm-project/pull/80309.
2024-08-09Revert "Enable logf128 constant folding for hosts with 128bit floats (#96287)"Nikita Popov1-2/+2
This reverts commit ccb2b011e577e861254f61df9c59494e9e122b38. Causes buildbot failures, e.g. on ppc64le builders.
2024-08-09Enable logf128 constant folding for hosts with 128bit floats (#96287)Matthew Devereau1-2/+2
Hosts which support a float size of 128 bits can benefit from constant fp128 folding.