summaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
AgeCommit message (Collapse)Author
2025-06-03[ValueTracking] Make Depth last default arg (NFC) (#142384)Ramkumar Ramachandra
Having a finite Depth (or recursion limit) for computeKnownBits is very limiting, but is currently a load-bearing necessity, as all KnownBits are recomputed on each call and there is no caching. As a prerequisite for an effort to remove the recursion limit altogether, either using a clever caching technique, or writing a easily-invalidable KnownBits analysis, make the Depth argument in APIs in ValueTracking uniformly the last argument with a default value. This would aid in removing the argument when the time comes, as many callers that currently pass 0 explicitly are now updated to omit the argument altogether.
2025-05-09[SimplifyLibCalls] Shrink sin, cos to sinf, cosf when allowed (#139082)Guy David
This optimization already exists, but for the libcall versions of these functions and not for their intrinsic form. Solves https://github.com/llvm/llvm-project/issues/139044. There are probably more opportunities for other intrinsics, because the switch-case in `LibCallSimplifier::optimizeCall` covers only `pow`, `exp2`, `log`, `log2`, `log10`, `sqrt`, `memset`, `memcpy` and `memmove`.
2025-04-16[Utils] Use StringRef::ends_with (NFC) (#135934)Kazu Hirata
2025-04-13SimplifyLibCalls: Skip sincospi optimization for ConstantData (#134688)Matt Arsenault
Avoids looking at the uselist, and it would probably be more productive to constant fold this.
2025-03-28[Analysis][NFC] Extract KnownFPClass (#133457)Tim Gymnich
- extract KnownFPClass for future use inside of GISelKnownBits --------- Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-02-28[SimplifyLibCalls][NFC] Fix Typo in Header Comment (#114314)Veera
2025-01-29[IR] Convert from nocapture to captures(none) (#123181)Nikita Popov
This PR removes the old `nocapture` attribute, replacing it with the new `captures` attribute introduced in #116990. This change is intended to be essentially NFC, replacing existing uses of `nocapture` with `captures(none)` without adding any new analysis capabilities. Making use of non-`none` values is left for a followup. Some notes: * `nocapture` will be upgraded to `captures(none)` by the bitcode reader. * `nocapture` will also be upgraded by the textual IR reader. This is to make it easier to use old IR files and somewhat reduce the test churn in this PR. * Helper APIs like `doesNotCapture()` will check for `captures(none)`. * MLIR import will convert `captures(none)` into an `llvm.nocapture` attribute. The representation in the LLVM IR dialect should be updated separately.
2025-01-14[SimplifyLibCalls] Don't infer call-site nocapture on atoiNikita Popov
This is already inferred on the function declaration by BLC, there is no need to also do it at the call-site.
2025-01-06[IRBuilder] Refactor FMF interface (#121657)Yingwei Zheng
Up to now, the only way to set specified FMF flags in IRBuilder is to use `FastMathFlagGuard`. It makes the code ugly and hard to maintain. This patch introduces a helper class `FMFSource` to replace the original parameter `Instruction *FMFSource` in IRBuilder. To maximize the compatibility, it accepts an instruction or a specified FMF. This patch also removes the use of `FastMathFlagGuard` in some simple cases. Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=f87a9db8322643ccbc324e317a75b55903129b55&to=9397e712f6010be15ccf62f12740e9b4a67de2f4&stat=instructions%3Au
2024-12-21Revert "SimplifyLibCalls: Use default globals address space when building ↵Owen Anderson
new global strings. (#118729)" (#119616) This reverts commit cfa582e8aaa791b52110791f5e6504121aaf62bf.
2024-12-12TargetLibraryInfo: Use pointer index size to determine getSizeTSize(). (#118747)Owen Anderson
When using non-integral pointer types, such as on CHERI targets, size_t is equivalent to the index size, which is allowed to be smaller than the size of the pointer.
2024-12-06SimplifyLibCalls: Use default globals address space when building new global ↵Owen Anderson
strings. (#118729) Writing a test for this transitively exposed a number of places in BuildLibCalls where we were failing to propagate address spaces properly, which are additionally fixed.
2024-10-28Check hasOptSize() in shouldOptimizeForSize() (#112626)Ellis Hoag
2024-10-17[SimplifyLibCall][Attribute] Fix bug where we may keep `range` attr with ↵goldsteinn
incompatible type (#112649) In a variety of places we change the bitwidth of a parameter but don't update the attributes. The issue in this case is from the `range` attribute when inlining `__memset_chk`. `optimizeMemSetChk` will replace an `i32` with an `i8`, and if the `i32` had a `range` attr assosiated it will cause an error. Fixes #112633
2024-10-17[LLVM] Make more use of IRBuilder::CreateIntrinsic. NFC. (#112706)Jay Foad
Convert many instances of: Fn = Intrinsic::getOrInsertDeclaration(...); CreateCall(Fn, ...) to the equivalent CreateIntrinsic call.
2024-10-11[NFC] Rename `Intrinsic::getDeclaration` to `getOrInsertDeclaration` (#111752)Rahul Joshi
Rename the function to reflect its correct behavior and to be consistent with `Module::getOrInsertFunction`. This is also in preparation of adding a new `Intrinsic::getDeclaration` that will have behavior similar to `Module::getFunction` (i.e, just lookup, no creation).
2024-10-10[SimplifyLibCalls] fdim constant fold (#109235)braw-lee
2nd PR to fix #108695 based on #108702 --------- Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-10-10[InstCombine] Convert @log to @llvm.log if the input is known positive. ↵David Green
(#111428) Similar to 112aac4e8961b9626bb84f36deeaa5a674f03f5a, this converts log libcalls to llvm.log.f64 intrinsics if we know they do not set errno, as the input is not zero and not negative. As log will produce errno if the input is 0 (returning -inf) or if the input is negative (returning nan), we also perform the conversion when we have noinf and nonan.
2024-10-08[InstCombine] Minor cleanup for optimizeFMod. NFCDavid Green
2024-09-18[InstCombine] Return FRem, as opposed to substituteInParent.David Green
This attempts to fix the ASan buildbot, which is detecting that CI is used after it is removed in substituteInParent. The idea was to make sure it was removed even if it had side-effects writing errno, but that appears to happen if we return FRem directly as usual.
2024-09-18[InstCombine] Fold fmod to frem if we know it does not set errno. (#107912)David Green
fmod will be folded to frem in clang under -fno-math-errno and can be constant folded in llvm if the operands are known. It can be relatively common to have fp code that handles special values before doing some calculation: ``` if (isnan(f)) return handlenan; if (isinf(f)) return handleinf; .. fmod(f, 2.0) ``` This patch enables the folding of fmod to frem in instcombine if the first parameter is not inf and the second is not zero. Other combinations do not set errno. The same transform is performed for fmod with the nnan flag, which implies the input is known to not be inf/zero.
2024-09-13[InstCombine] Pass DomTree and DomTreeCacheto LibCallSimplifier (#108446)David Green
This allows any combines to pick up Known states from dominating conditions.
2024-08-27[SimplifyLibCalls] Fix memchr misoptimization (#106121)Sergei Barannikov
The `ch` argument of memcmp should be truncated to `unsigned char` before using it in comparisons. This didn't happen on all code paths. The following program miscompiled at -O1 and higher: ```C++ #include <cstring> #include <iostream> char ch = '\x81'; int main() { bool found = std::strchr("\x80\x81\x82", ch) != nullptr; std::cout << std::boolalpha << found << '\n'; } ```
2024-08-16[InstCombine] Handle commuted variant of sqrt transformNikita Popov
2024-08-15Allow optimization of __size_returning_new variants. (#102258)Snehasish Kumar
https://github.com/llvm/llvm-project/pull/101564 added support to TLI to detect variants of operator new which provide feedback on the actual size of memory allocated (http://wg21.link/P0901R5). This patch extends SimplifyLibCalls to handle hot cold hinting of these variants.
2024-08-04[InstCombine] Don't add extra 0 to string in str[np]cpy optimization (#101884)Sergei Barannikov
It is unused by subsequent memcpy.
2024-08-01[SimplifyLibCalls] Constant fold nan libcall (#101459)Yingwei Zheng
Reference: https://en.cppreference.com/w/c/numeric/math/nan The logic is copied from clang frontend: https://github.com/llvm/llvm-project/blob/1d2b2d29d733200b704f38d220d22ecc07d6cf42/clang/lib/AST/ExprConstant.cpp#L14741-L14777 --------- Co-authored-by: Nikita Popov <github@npopov.com>
2024-07-30[TLI] Add support for inferring attr `cold` on `exit`/`abort`Noah Goldstein
`abort` can be assumed always cold and assume non-zero `exit` status as a `cold` path as well. Closes #101003
2024-07-24[SimplifyLibCalls] Constant fold `remquo` (#99647)Yingwei Zheng
This patch adds constant folding support for `remquo`. Reference: https://en.cppreference.com/w/cpp/numeric/math/remquo Closes https://github.com/llvm/llvm-project/issues/99497.
2024-07-11[SimplifyLibCalls] Simplify cabs libcall if real or imaginary part of input ↵Hendrik Hübner
is zero (#97976) cabs(a + i0) -> abs(a) cabs(0 +ib) -> abs(b) Closes #97336
2024-06-27[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)Nikita Popov
This is a helper to avoid writing `getModule()->getDataLayout()`. I regularly try to use this method only to remember it doesn't exist... `getModule()->getDataLayout()` is also a common (the most common?) reason why code has to include the Module.h header.
2024-06-24Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"Stephen Tozer
Reverts the above commit, as it updates a common header function and did not update all callsites: https://lab.llvm.org/buildbot/#/builders/29/builds/382 This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24[IR][NFC] Update IRBuilder to use InsertPosition (#96497)Stephen Tozer
Uses the new InsertPosition class (added in #94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
2024-06-13SimplifyLibCalls: Simplify fp immediate checking code (NFC)Matt Arsenault
Re-use already queried call arguments and matched APFloat, instead of re-matching the original argument.
2024-06-13SimplifyLibCalls: Don't require ldexp to emit intrinsic in pow combine (#95277)Matt Arsenault
Do not require a libm ldexp libcall to emit the ldexp intrinsic when transforming pow(2, x) -> ldexp(1, x) This enables the half intrinsic case to fold.
2024-06-10SimplifyLibCalls: Don't require ldexp to emit intrinsic in exp2 combine (#92707)Matt Arsenault
When folding exp2(itofp(x)) to ldexp(1, x), don't require an ldexp libcall to emit the intrinsic. The intrinsic needs to be handled regardless of whether the system has a libcall, and we have an inline implementation of ldexp already. This fixes the instance in the exp2->ldexp fold. Another instance exists for the pow(2) -> ldexp case The LTO test change isn't ideal, since it's just moving the problem to another instance where we're relying on implied libm behavior for an intrinsic transform. Use exp10 since that's a harder case to solve in the libcall house of cards we have.
2024-05-19SimplifyLibCalls: Permit pow(2, x) -> ldexp(1, x) fold for vectors (#92532)Matt Arsenault
2024-05-17SimplifyLibCalls: Prefer to emit intrinsic in pow(2, x) -> ldexp(1, x) (#92363)Matt Arsenault
2024-05-16SimplifyLibCalls: Emit vector ldexp intrinsics in exp2->ldexp combine (#92219)Matt Arsenault
Co-authored-by: Nikita Popov <github@npopov.com>
2024-05-16SimplifyLibCalls: Use IRBuilder helpers for creating intrinsics (#92288)Matt Arsenault
2024-05-16InstCombine: Try to use exp10 intrinsic instead of libcall (#92287)Matt Arsenault
Addresses old TODO about the exp10 intrinsic not existing.
2024-05-15InstCombine: Emit ldexp intrinsic in exp2->ldexp combine (#92039)Matt Arsenault
Prefer to emit the intrinsic over a libcall in the intrinsic or no-math-errno case.
2024-05-08[MemProf] Optionally update hints on existing hot/cold new calls (#91047)Teresa Johnson
If directed by an option, update hints on calls to new that already provide a hot/cold hint.
2024-04-16[ValueTracking] Restore isKnownNonZero parameter order. (#88873)Harald van Dijk
Prior to #85863, the required parameters of llvm::isKnownNonZero were Value and DataLayout. After, they are Value, Depth, and SimplifyQuery, where SimplifyQuery is implicitly constructible from DataLayout. The change to move Depth before SimplifyQuery needed callers to be updated unnecessarily, and as commented in #85863, we actually want Depth to be after SimplifyQuery anyway so that it can be defaulted and the caller does not need to specify it.
2024-04-12[ValueTracking] Convert `isKnownNonZero` to use SimplifyQuery (#85863)Yingwei Zheng
This patch converts `isKnownNonZero` to use SimplifyQuery. Then we can use the context information from `DomCondCache`. Fixes https://github.com/llvm/llvm-project/issues/85823. Alive2: https://alive2.llvm.org/ce/z/QUvHVj
2024-02-24[InstCombine] Handle more even/odd math functions (#81324)Artem Tyurin
At the moment this PR adds support only for `erf` function. Fixes #77220.
2024-02-06[Transforms] Expand optimizeTan to fold more inverse trig pairs (#77799)AtariDreams
optimizeTan has been renamed to optimizeTrigInversionPairs as a result. Sadly, this is not mathematically true that all inverse pairs fold to x. For example, asin(sin(x)) does not fold to x if x is over 2pi.
2024-02-06[SimplifyLibCalls] Merge sqrt into the power of exp (#79146)Anton Sidorenko
Under fast-math flags it's possible to convert `sqrt(exp(X)) `into `exp(X * 0.5)`. I suppose that this transformation is always profitable. This is similar to the optimization existing in GCC.
2024-02-06[Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine ↵AtariDreams
(#79699) Add cos(fabs(x)) -> cos(x) and cos(copysign(x, y)) -> cos(x).
2024-02-06[ValueTracking][NFC] Pass `SimplifyQuery` to `computeKnownFPClass` family ↵Yingwei Zheng
(#80657) This patch refactors the interface of the `computeKnownFPClass` family to pass `SimplifyQuery` directly. The motivation of this patch is to compute known fpclass with `DomConditionCache`, which was introduced by https://github.com/llvm/llvm-project/pull/73662. With `DomConditionCache`, we can do more optimization with context-sensitive information. Example (extracted from [fmt/format.h](https://github.com/fmtlib/fmt/blob/e17bc67547a66cdd378ca6a90c56b865d30d6168/include/fmt/format.h#L3555-L3566)): ``` define float @test(float %x, i1 %cond) { %i32 = bitcast float %x to i32 %cmp = icmp slt i32 %i32, 0 br i1 %cmp, label %if.then1, label %if.else if.then1: %fneg = fneg float %x br label %if.end if.else: br i1 %cond, label %if.then2, label %if.end if.then2: br label %if.end if.end: %value = phi float [ %fneg, %if.then1 ], [ %x, %if.then2 ], [ %x, %if.else ] %ret = call float @llvm.fabs.f32(float %value) ret float %ret } ``` We can prove the signbit of `%value` is always zero. Then the fabs can be eliminated.