aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
AgeCommit message (Collapse)AuthorFilesLines
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.
2024-07-30[NFC] fix build failure (#100993)Chen Zheng1-2/+2
Fix the build failure caused by https://github.com/llvm/llvm-project/pull/94944 Fixes https://github.com/llvm/llvm-project/issues/100296
2024-07-25Remove the `x86_mmx` IR type. (#98505)James Y Knight1-5/+3
It is now translated to `<1 x i64>`, which allows the removal of a bunch of special casing. This _incompatibly_ changes the ABI of any LLVM IR function with `x86_mmx` arguments or returns: instead of passing in mmx registers, they will now be passed via integer registers. However, the real-world incompatibility caused by this is expected to be minimal, because Clang never uses the x86_mmx type -- it lowers `__m64` to either `<1 x i64>` or `double`, depending on ABI. This change does _not_ eliminate the SelectionDAG `MVT::x86mmx` type. That type simply no longer corresponds to an IR type, and is used only by MMX intrinsics and inline-asm operands. Because SelectionDAGBuilder only knows how to generate the operands/results of intrinsics based on the IR type, it thus now generates the intrinsics with the type MVT::v1i64, instead of MVT::x86mmx. We need to fix this before the DAG LegalizeTypes, and thus have the X86 backend fix them up in DAGCombine. (This may be a short-lived hack, if all the MMX intrinsics can be removed in upcoming changes.) Works towards issue #98272.
2024-07-15Fix assertion failure in PR98681 (#98860)Yingwei Zheng1-1/+1
See https://en.cppreference.com/w/cpp/numeric/math/pow: ``` C++98 added overloads where exp has type int on top of C [pow()](https://en.cppreference.com/w/c/numeric/math/pow), and the return type of std::pow(float, int) was float. However, the additional overloads introduced in C++11 specify that std::pow(float, int) should return double. [LWG issue 550](https://cplusplus.github.io/LWG/issue550) was raised to target this conflict, and the resolution is to removed the extra int exp overloads. ```
2024-07-15[ConstantFold] Fix result type when folding powi.f16 (#98681)Yingwei Zheng1-18/+19
Fixes #98665.
2024-06-28[IR] Add getDataLayout() helpers to Function and GlobalValue (#96919)Nikita Popov1-4/+4
Similar to https://github.com/llvm/llvm-project/pull/96902, this adds `getDataLayout()` helpers to Function and GlobalValue, replacing the current `getParent()->getDataLayout()` pattern.
2024-06-18[Analysis] Fix -Wunused-function in ConstantFolding.cpp (NFC)Jie Fu1-1/+1
/llvm-project/llvm/lib/Analysis/ConstantFolding.cpp:1747:11: error: unused function 'GetConstantFoldFPValue128' [-Werror,-Wunused-function] Constant *GetConstantFoldFPValue128(float128 V, Type *Ty) { ^ 1 error generated.
2024-06-18Fixup __float128 uses and if defined guard in ConstantFoldingMatt Devereau1-5/+4
Uses of __float128 in (#94944) should be float128. Although ConstantFoldFP128 is not reliant on HAS_LOGF128, it is only used by conditional code controlled by HAS_LOGF128, and will cause unused errors on buildbots.
2024-06-18[Analysis] Fix -Wunused-function in ConstantFolding.cpp (NFC)Jie Fu1-0/+1
/llvm-project/llvm/lib/Analysis/ConstantFolding.cpp:1787:11: error: unused function 'ConstantFoldFP128' [-Werror,-Wunused-function] Constant *ConstantFoldFP128(long double (*NativeFP)(long double), ^ 1 error generated.
2024-06-18ConstantFold logl calls (#94944)Matthew Devereau1-8/+35
This is a follow up patch from #90611 which folds logl calls in the same manner as log.f128 calls. logl suffers from the same problem as logf128 of having slow calls to fp128 log functions which can be constant folded. However, logl is emitted with -fmath-errno and log.f128 is emitted by -fno-math-errno by certain intrinsics.
2024-06-11[ConstantFolding] Preserve nowrap flags in gep of gep foldNikita Popov1-7/+16
A caveat here is that we can only preserve nusw if the offset additions did not overflow. Proofs: https://alive2.llvm.org/ce/z/u56z_u
2024-06-04[ConstantFolding] Preserve all flags in CastGEPIndices()Nikita Popov1-4/+4
This preserves the flags during that transform, but currently they will still end up getting dropped at a later stage.
2024-06-04[IR] Remove support for icmp and fcmp constant expressions (#93038)Nikita Popov1-5/+1
Remove support for the icmp and fcmp constant expressions. This is part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179 As usual, many of the updated tests will no longer test what they were originally intended to -- this is hard to preserve when constant expressions get removed, and in many cases just impossible as the existence of a specific kind of constant expression was the cause of the issue in the first place.
2024-05-31Reapply [IR] Avoid creating icmp/fcmp constant expressions (#92885)Nikita Popov1-1/+1
Reapply after https://github.com/llvm/llvm-project/pull/93548, which should address the lldb failure on macos. ----- Do not create icmp/fcmp constant expressions in IRBuilder etc anymore, i.e. treat them as "undesirable". This is in preparation for removing them entirely. Part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179
2024-05-30[InstSimplify] Add constant folding support for `ucmp`/`scmp` intrinsics ↵Poseydon421-0/+17
(#93730) This PR adds support for folding calls to `ucmp`/`scmp` intrinsics with constant arguments.
2024-05-29Constant Fold logf128 calls (#90611)Matthew Devereau1-0/+11
This is a second attempt to land #84501 which failed on several targets. This patch adds the HAS_IEE754_FLOAT128 define which makes the check for typedef'ing float128 more precise by checking whether __uint128_t is available and checking if the host does not use __ibm128 which is prevalent on power pc targets and replaces IEEE754 float128s.
2024-05-27[IR] Add getelementptr nusw and nuw flags (#90824)Nikita Popov1-1/+2
This implements the `nusw` and `nuw` flags for `getelementptr` as proposed at https://discourse.llvm.org/t/rfc-add-nusw-and-nuw-flags-for-getelementptr/78672. The three possible flags are encapsulated in the new `GEPNoWrapFlags` class. Currently this class has a ctor from bool, interpreted as the InBounds flag. This ctor should be removed in the future, as code gets migrated to handle all flags. There are a few places annotated with `TODO(gep_nowrap)`, where I've had to touch code but opted to not infer or precisely preserve the new flags, so as to keep this as NFC as possible and make sure any changes of that kind get test coverage when they are made.
2024-05-22Revert "[IR] Avoid creating icmp/fcmp constant expressions" (#93087)Daniel Thornburgh1-1/+1
Reverts llvm/llvm-project#92885 due to LLDB CI breakages.
2024-05-22[IR] Avoid creating icmp/fcmp constant expressions (#92885)Nikita Popov1-1/+1
Do not create icmp/fcmp constant expressions in IRBuilder etc anymore, i.e. treat them as "undesirable". This is in preparation for removing them entirely. Part of: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179
2024-05-20[ConstantFolding] Canonicalize constexpr GEPs to i8 (#89872)Nikita Popov1-36/+10
This patch canonicalizes constant expression GEPs to use i8 source element type, aka ptradd. This is the ConstantFolding equivalent of the InstCombine canonicalization introduced in #68882. I believe all our optimizations working on constant expression GEPs (like GlobalOpt etc) have already been switched to work on offsets, so I don't expect any significant fallout from this change. This is part of: https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699
2024-05-20[SCEV] Don't use non-deterministic constant folding for trip counts (#90942)Nikita Popov1-11/+40
When calculating the exit count exhaustively, if any of the involved operations is non-deterministic, the exit count we compute at compile-time and the exit count at run-time may differ. Using these non-deterministic constant folding results is only correct if we actually replace all uses of the instruction with the value. SCEV (or its consumers) generally don't do this. Handle this by adding a new AllowNonDeterministic flag to the constant folding API, and disabling it in SCEV. If non-deterministic results are not allowed, do not fold FP lib calls in general, and FP operations returning NaNs in particular. This could be made more precise (some FP libcalls like fabs are fully deterministic), but I don't think this that precise handling here is worthwhile. Fixes the interesting part of https://github.com/llvm/llvm-project/issues/89885.
2024-05-09Replace uses of ConstantExpr::getCompare. (#91558)Eli Friedman1-4/+4
Use ICmpInst::compare() where possible, ConstantFoldCompareInstOperands in other places. This only changes places where the either the fold is guaranteed to succeed, or the code doesn't use the resulting compare if we fail to fold.
2024-05-01Revert "Constant Fold logf128 calls"Matt Devereau1-11/+0
This reverts commit 088aa81a545421933254f19cd3c8914a0373b493.
2024-05-01Constant Fold logf128 callsMatt Devereau1-0/+11
This is a second attempt to land #84501 which failed on several targets. This patch adds the HAS_IEE754_FLOAT128 define which makes the check for typedef'ing float128 more precise by checking whether __uint128_t is available and checking if the host does not use __ibm128 which is prevalent on power pc targets and replaces IEEE754 float128s.
2024-04-18Revert "Constant Fold Logf128 calls (#84501)"Matt Devereau1-11/+0
This reverts commit e90bc9cfd4d22c89dd993f62ede700ae25df49c5.
2024-04-18Constant Fold Logf128 calls (#84501)Matthew Devereau1-0/+11
This patch enables constant folding for 128 bit floating-point logf calls. This is achieved by querying if the host system has the logf128() symbol available with a CMake test. If so, replace the runtime call with the compile time value returned from logf128.
2024-03-23[LLVM] Use `std::move` for APInt. NFC. (#86257)Yingwei Zheng1-1/+1
This patch adjusts argument passing for `APInt` to improve the compile-time. Compile-time improvement: https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=ba3e326def3a6e5cd6d72ff5a49c74fba18de1df&stat=instructions:u
2024-03-20[IR] Change representation of getelementptr inrange (#84341)Nikita Popov1-24/+19
As part of the migration to ptradd (https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699), we need to change the representation of the `inrange` attribute, which is used for vtable splitting. Currently, inrange is specified as follows: ``` getelementptr inbounds ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, inrange i32 1, i64 2) ``` The `inrange` is placed on a GEP index, and all accesses must be "in range" of that index. The new representation is as follows: ``` getelementptr inbounds inrange(-16, 16) ({ [4 x ptr], [4 x ptr] }, ptr @vt, i64 0, i32 1, i64 2) ``` This specifies which offsets are "in range" of the GEP result. The new representation will continue working when canonicalizing to ptradd representation: ``` getelementptr inbounds inrange(-16, 16) (i8, ptr @vt, i64 48) ``` The inrange offsets are relative to the return value of the GEP. An alternative design could make them relative to the source pointer instead. The result-relative format was chosen on the off-chance that we want to extend support to non-constant GEPs in the future, in which case this variant is more expressive. This implementation "upgrades" the old inrange representation in bitcode by simply dropping it. This is a very niche feature, and I don't think trying to upgrade it is worthwhile. Let me know if you disagree.
2024-03-15[IRBuilder] Fold binary intrinsics (#80743)Artem Tyurin1-55/+83
Fixes https://github.com/llvm/llvm-project/issues/61240.
2024-02-15[ConstantFolding] Do not consider padded-in-memory types as uniform (#81854)Björn Pettersson1-6/+11
Teaching ConstantFoldLoadFromUniformValue that types that are padded in memory can't be considered as uniform. Using the big hammer to prevent optimizations when loading from a constant for which DataLayout::typeSizeEqualsStoreSize would return false. Main problem solved would be something like this: store i17 -1, ptr %p, align 4 %v = load i8, ptr %p, align 1 If for example the i17 occupies 32 bits in memory, then LLVM IR doesn't really tell where the padding goes. And even if we assume that the 15 most significant bits are padding, then they should be considered as undefined (even if LLVM backend typically would pad with zeroes). Anyway, for a big-endian target the load would read those most significant bits, which aren't guaranteed to be one's. So it would be wrong to constant fold the load as returning -1. If LLVM IR had been more explicit about the placement of padding, then we could allow the constant fold of the load in the example, but only for little-endian. Fixes: https://github.com/llvm/llvm-project/issues/81793
2023-11-21[AMDGPU] - Add constant folding to s_wqm intrinsic (#72382)Jessica Del1-0/+10
Fold any constant input to the `s_wqm` intrinsic.
2023-11-17Fix MSVC "result of 32-bit shift implicitly converted to 64 bits" warning.Simon Pilgrim1-1/+1
2023-11-17[AMDGPU] - Add constant folding for s_quadmask (#72381)Jessica Del1-0/+13
If the input is a constant we can constant fold the `s_quadmask` intrinsic.