aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-09-12[MemProf] Optionally allow transformation of nobuiltin operator new (#158396)Teresa Johnson1-11/+32
For cases where we can guarantee the application does not override operator new.
2025-09-06[MemProf] Always add hints to allocations with memprof attributes (#157222)Teresa Johnson1-40/+35
Apply hints even if the attribute is the default "notcold" or "ambiguous", to enable better tracking through the allocator. Add an option to control the ambiguous allocation hint value.
2025-09-02[MemProf] Allow hint update on existing calls to nobuiltin hot/cold new ↵Teresa Johnson1-2/+36
(#156476) Explicit calls to ::operator new are marked nobuiltin and cannot be elided or updated as they may call user defined versions. However, existing calls to the hot/cold versions of new only need their hint parameter value updated, which does not mutate the call.
2025-08-19[InstCombine] Make strlen optimization more resilient to different gep ↵David Green1-6/+11
types. (#153623) This makes the optimization in optimizeStringLength for strlen(gep @glob, %x) -> sub endof@glob, %x a little more resilient, and maybe a bit more correct for geps with non-array types.
2025-08-15[PatternMatch] Allow `m_ConstantInt` to match integer splats (#153692)zGoldthorpe1-3/+3
When matching integers, `m_ConstantInt` is a convenient alternative to `m_APInt` for matching unsigned 64-bit integers, allowing one to simplify ```cpp const APInt *IntC; if (match(V, m_APInt(IntC))) { if (IntC->ule(UINT64_MAX)) { uint64_t Int = IntC->getZExtValue(); // ... } } ``` to ```cpp uint64_t Int; if (match(V, m_ConstantInt(Int))) { // ... } ``` However, this simplification is only true if `V` is a scalar type. Specifically, `m_APInt` also matches integer splats, but `m_ConstantInt` does not. This patch ensures that the matching behaviour of `m_ConstantInt` parallels that of `m_APInt`, and also incorporates it in some obvious places.
2025-06-03[ValueTracking] Make Depth last default arg (NFC) (#142384)Ramkumar Ramachandra1-8/+5
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 David1-0/+5
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 Hirata1-1/+1
2025-04-13SimplifyLibCalls: Skip sincospi optimization for ConstantData (#134688)Matt Arsenault1-0/+3
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 Gymnich1-3/+9
- 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)Veera1-1/+1
2025-01-29[IR] Convert from nocapture to captures(none) (#123181)Nikita Popov1-2/+4
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 Popov1-2/+0
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 Zheng1-33/+19
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 Anderson1-43/+37
new global strings. (#118729)" (#119616) This reverts commit cfa582e8aaa791b52110791f5e6504121aaf62bf.
2024-12-12TargetLibraryInfo: Use pointer index size to determine getSizeTSize(). (#118747)Owen Anderson1-53/+29
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 Anderson1-37/+43
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 Hoag1-10/+5
2024-10-17[SimplifyLibCall][Attribute] Fix bug where we may keep `range` attr with ↵goldsteinn1-1/+7
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 Foad1-3/+2
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 Joshi1-1/+1
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-lee1-0/+31
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 Green1-14/+32
(#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 Green1-3/+2
2024-09-18[InstCombine] Return FRem, as opposed to substituteInParent.David Green1-1/+1
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 Green1-0/+33
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 Green1-7/+7
This allows any combines to pick up Known states from dominating conditions.
2024-08-27[SimplifyLibCalls] Fix memchr misoptimization (#106121)Sergei Barannikov1-2/+4
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 Popov1-8/+11
2024-08-15Allow optimization of __size_returning_new variants. (#102258)Snehasish Kumar1-1/+31
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 Barannikov1-1/+2
It is unused by subsequent memcpy.
2024-08-01[SimplifyLibCalls] Constant fold nan libcall (#101459)Yingwei Zheng1-0/+20
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 Goldstein1-0/+14
`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 Zheng1-0/+35
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übner1-7/+36
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 Popov1-3/+3
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 Tozer1-2/+2
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 Tozer1-2/+2
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 Arsenault1-3/+3
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 Arsenault1-1/+5
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 Arsenault1-4/+9
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 Arsenault1-3/+4
2024-05-17SimplifyLibCalls: Prefer to emit intrinsic in pow(2, x) -> ldexp(1, x) (#92363)Matt Arsenault1-5/+13
2024-05-16SimplifyLibCalls: Emit vector ldexp intrinsics in exp2->ldexp combine (#92219)Matt Arsenault1-8/+9
Co-authored-by: Nikita Popov <github@npopov.com>
2024-05-16SimplifyLibCalls: Use IRBuilder helpers for creating intrinsics (#92288)Matt Arsenault1-53/+29
2024-05-16InstCombine: Try to use exp10 intrinsic instead of libcall (#92287)Matt Arsenault1-2/+9
Addresses old TODO about the exp10 intrinsic not existing.
2024-05-15InstCombine: Emit ldexp intrinsic in exp2->ldexp combine (#92039)Matt Arsenault1-4/+13
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 Johnson1-22/+115
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 Dijk1-10/+10
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 Zheng1-10/+10
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