aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
AgeCommit message (Collapse)AuthorFilesLines
3 days[NVPTX] Fix NaN + overflow semantics of f2ll/d2i (#159530)Lewis Crawford1-8/+15
Fix the NaN-handling semantics of various NVVM intrinsics converting from fp types to integer types. Previously in ConstantFolding, NaN inputs would be constant-folded to 0. However, v9.0 of the PTX spec states that: In float-to-integer conversions, depending upon conversion types, NaN input results in following value: * Zero if source is not `.f64` and destination is not `.s64`, .`u64`. * Otherwise `1 << (BitWidth(dst) - 1)` corresponding to the value of `(MAXINT >> 1) + 1` for unsigned type or `MININT` for signed type. Also, support for constant-folding +/-Inf and values which overflow/underflow the integer output type has been added (they clamp to min/max int). Because of this NaN-handling semantic difference, we also need to disable transforming several intrinsics to FPToSI/FPToUI, as the LLVM intstruction will return poison, but the intrinsics have defined behaviour for these edge-cases like NaN/Inf/overflow.
5 days[ConstantFolding] Avoid use of isNonIntegralPointerType()Alexander Richardson1-7/+7
Avoiding any new inttoptr is unnecessarily restrictive for "plain" non-integral pointers, but it is important for unstable pointers and pointers with external state. Fixes another test codegen regression from https://github.com/llvm/llvm-project/pull/105735. Reviewed By: nikic Pull Request: https://github.com/llvm/llvm-project/pull/159959
2025-09-11[ConstFold] Don't crash on ConstantExprs when folding get_active_lane_m.Florian Hahn1-3/+3
Check if operands are ConstantInt to avoid crashing on constant expression after https://github.com/llvm/llvm-project/pull/156659.
2025-09-11[ConstantFolding] Fold scalable get_active_lane_masks (#156659)Matthew Devereau1-0/+7
Scalable get_active_lane_mask intrinsics with a range of 0 can be lowered to zeroinitializer. This helps remove no-op scalable masked stores and loads.
2025-09-10[AMDGPU] Propagate Constants for Wave Reduction Intrinsics (#150395)Aaditya1-0/+14
2025-09-08[InstCombine][VectorCombine][NFC] Unify uses of lossless inverse cast (#156597)Hongyu Chen1-0/+51
This patch addresses https://github.com/llvm/llvm-project/pull/155216#discussion_r2297724663. This patch adds a helper function to put the inverse cast on constants, with cast flags preserved(optional). Follow-up patches will add trunc/ext handling on VectorCombine and flags preservation on InstCombine.
2025-08-22[NVPTX] Constant fold NVVM add/mul/div/fma (#152544)Lewis Crawford1-0/+178
Constant fold the NVVM intrinsics for add, mul, div, fma with specific rounding modes.
2025-08-14Reapply "[WebAssembly] Constant fold wasm.dot" (#153070)Jasmine Tang1-0/+25
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-08[IR] Introduce the `ptrtoaddr` instructionAlexander Richardson1-0/+3
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-06Revert "[WebAssembly] Constant fold wasm.dot" (#152382)Andrew Lazarev1-25/+0
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-05[WebAssembly] Constant fold wasm.dot (#149619)Jasmine Tang1-0/+25
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-01[ConstantFolding] Fix incorrect nvvm_round folding (#151563)Lewis Crawford1-3/+4
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-07-30[ConstantFolding] Fix typo in GetNVVMDenormMode (#151297)Lewis Crawford1-4/+4
Fix typo in function name of GetNVVMDenormMode (Denrom vs Denorm).
2025-07-29[ConstantFolding][SVE] Do not fold fcmp of denormal without known mode. ↵Paul Walker1-1/+4
(#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-25[ConstantFolding] Merge constant gep `inrange` attributes (#150546)Pedro Lobo1-6/+5
When folding a gep+gep into a single gep, intersect their `inrange` attributes.
2025-07-23[Analysis] Drop const from return types (NFC) (#150258)Kazu Hirata1-5/+4
We don't need const on APFloat.
2025-07-21[ConstantFolding] Fix nvvm_round folding on PPC (#149837)Lewis Crawford1-5/+8
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-21Add constant-folding for unary NVVM intrinsics (#141233)Lewis Crawford1-5/+163
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-15[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.