543 Commits

Author SHA1 Message Date
Guy David
a1beb61940
[SimplifyLibCalls] Shrink sin, cos to sinf, cosf when allowed (#139082)
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-05-09 07:25:35 +03:00
Kazu Hirata
0577240615
[Utils] Use StringRef::ends_with (NFC) (#135934) 2025-04-16 08:39:07 -07:00
Matt Arsenault
a24ef4b07e SimplifyLibCalls: Skip sincospi optimization for ConstantData (#134688)
Avoids looking at the uselist, and it would probably be more
productive to constant fold this.
2025-04-13 16:47:07 +02:00
Tim Gymnich
049f179606
[Analysis][NFC] Extract KnownFPClass (#133457)
- extract KnownFPClass for future use inside of GISelKnownBits

---------

Co-authored-by: Matt Arsenault <arsenm2@gmail.com>
2025-03-28 18:10:02 +01:00
Veera
dfca4f9519
[SimplifyLibCalls][NFC] Fix Typo in Header Comment (#114314) 2025-02-28 23:05:13 -05:00
Nikita Popov
29441e4f5f
[IR] Convert from nocapture to captures(none) (#123181)
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-29 16:56:47 +01:00
Nikita Popov
193ea83dd7 [SimplifyLibCalls] Don't infer call-site nocapture on atoi
This is already inferred on the function declaration by BLC, there
is no need to also do it at the call-site.
2025-01-14 17:13:45 +01:00
Yingwei Zheng
a77346bad0
[IRBuilder] Refactor FMF interface (#121657)
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
2025-01-06 14:37:04 +08:00
Owen Anderson
bc8fa9c443
Revert "SimplifyLibCalls: Use default globals address space when building new global strings. (#118729)" (#119616)
This reverts commit cfa582e8aaa791b52110791f5e6504121aaf62bf.
2024-12-21 09:33:39 +13:00
Owen Anderson
22f0ebb19c
TargetLibraryInfo: Use pointer index size to determine getSizeTSize(). (#118747)
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-12 15:45:44 +13:00
Owen Anderson
cfa582e8aa
SimplifyLibCalls: Use default globals address space when building new global 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-12-06 10:51:14 +13:00
Ellis Hoag
6ab26eab4f
Check hasOptSize() in shouldOptimizeForSize() (#112626) 2024-10-28 09:45:03 -07:00
goldsteinn
c85611e858
[SimplifyLibCall][Attribute] Fix bug where we may keep range attr with 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 10:32:55 -05:00
Jay Foad
85c17e4092
[LLVM] Make more use of IRBuilder::CreateIntrinsic. NFC. (#112706)
Convert many instances of:
  Fn = Intrinsic::getOrInsertDeclaration(...);
  CreateCall(Fn, ...)
to the equivalent CreateIntrinsic call.
2024-10-17 16:20:43 +01:00
Rahul Joshi
fa789dffb1
[NFC] Rename Intrinsic::getDeclaration to getOrInsertDeclaration (#111752)
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-11 05:26:03 -07:00
braw-lee
3645c64d87
[SimplifyLibCalls] fdim constant fold (#109235)
2nd PR to fix #108695 

based on #108702

---------

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-10-10 14:44:39 +04:00
David Green
5184d763c7
[InstCombine] Convert @log to @llvm.log if the input is known positive. (#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-10 09:54:25 +01:00
David Green
db98be3c71 [InstCombine] Minor cleanup for optimizeFMod. NFC 2024-10-08 15:00:30 +01:00
David Green
403897484f [InstCombine] Return FRem, as opposed to substituteInParent.
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 12:32:47 +01:00
David Green
112aac4e89
[InstCombine] Fold fmod to frem if we know it does not set errno. (#107912)
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-18 09:38:28 +01:00
David Green
c0e308ba3d
[InstCombine] Pass DomTree and DomTreeCacheto LibCallSimplifier (#108446)
This allows any combines to pick up Known states from dominating
conditions.
2024-09-13 08:36:48 +01:00
Sergei Barannikov
7134d2e9ac
[SimplifyLibCalls] Fix memchr misoptimization (#106121)
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-27 00:11:23 +03:00
Nikita Popov
60bffe221a [InstCombine] Handle commuted variant of sqrt transform 2024-08-16 16:47:13 +02:00
Snehasish Kumar
95daf1aedf
Allow optimization of __size_returning_new variants. (#102258)
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-15 08:06:41 -07:00
Sergei Barannikov
5c48f6fa54
[InstCombine] Don't add extra 0 to string in str[np]cpy optimization (#101884)
It is unused by subsequent memcpy.
2024-08-04 16:12:14 +03:00
Yingwei Zheng
4f42deb5f4
[SimplifyLibCalls] Constant fold nan libcall (#101459)
Reference: https://en.cppreference.com/w/c/numeric/math/nan
The logic is copied from clang frontend:


1d2b2d29d7/clang/lib/AST/ExprConstant.cpp (L14741-L14777)

---------

Co-authored-by: Nikita Popov <github@npopov.com>
2024-08-01 17:24:48 +08:00
Noah Goldstein
67fb7c34f1 [TLI] Add support for inferring attr cold on exit/abort
`abort` can be assumed always cold and assume non-zero `exit` status
as a `cold` path as well.

Closes #101003
2024-07-30 00:56:53 +08:00
Yingwei Zheng
9d45b450f2
[SimplifyLibCalls] Constant fold remquo (#99647)
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-24 13:32:20 +08:00
Hendrik Hübner
e980990ee4
[SimplifyLibCalls] Simplify cabs libcall if real or imaginary part of input is zero (#97976)
cabs(a + i0) -> abs(a)
cabs(0 +ib) -> abs(b)

Closes #97336
2024-07-11 19:01:06 +08:00
Nikita Popov
2d209d964a
[IR] Add getDataLayout() helpers to BasicBlock and Instruction (#96902)
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-27 16:38:15 +02:00
Stephen Tozer
d75f9dd1d2 Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"
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 18:00:22 +01:00
Stephen Tozer
6481dc5761
[IR][NFC] Update IRBuilder to use InsertPosition (#96497)
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-24 17:27:43 +01:00
Matt Arsenault
53c06c5644 SimplifyLibCalls: Simplify fp immediate checking code (NFC)
Re-use already queried call arguments and matched APFloat, instead
of re-matching the original argument.
2024-06-13 10:47:35 +02:00
Matt Arsenault
b446f9079b
SimplifyLibCalls: Don't require ldexp to emit intrinsic in pow combine (#95277)
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-13 10:11:18 +02:00
Matt Arsenault
8788b666aa
SimplifyLibCalls: Don't require ldexp to emit intrinsic in exp2 combine (#92707)
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-06-10 18:21:20 +02:00
Matt Arsenault
c1c1567d60
SimplifyLibCalls: Permit pow(2, x) -> ldexp(1, x) fold for vectors (#92532) 2024-05-19 17:35:42 +02:00
Matt Arsenault
f5c8242042
SimplifyLibCalls: Prefer to emit intrinsic in pow(2, x) -> ldexp(1, x) (#92363) 2024-05-17 14:28:03 +02:00
Matt Arsenault
0ea178b085
SimplifyLibCalls: Emit vector ldexp intrinsics in exp2->ldexp combine (#92219)
Co-authored-by: Nikita Popov <github@npopov.com>
2024-05-16 10:24:56 +02:00
Matt Arsenault
8389177710
SimplifyLibCalls: Use IRBuilder helpers for creating intrinsics (#92288) 2024-05-16 09:20:18 +02:00
Matt Arsenault
ce1ce5d30c
InstCombine: Try to use exp10 intrinsic instead of libcall (#92287)
Addresses old TODO about the exp10 intrinsic not existing.
2024-05-16 09:09:02 +02:00
Matt Arsenault
d7bb0723fe
InstCombine: Emit ldexp intrinsic in exp2->ldexp combine (#92039)
Prefer to emit the intrinsic over a libcall in the
intrinsic or no-math-errno case.
2024-05-15 07:41:28 +02:00
Teresa Johnson
cec6665f2b
[MemProf] Optionally update hints on existing hot/cold new calls (#91047)
If directed by an option, update hints on calls to new that already
provide a hot/cold hint.
2024-05-08 13:41:29 -07:00
Harald van Dijk
60de56c743
[ValueTracking] Restore isKnownNonZero parameter order. (#88873)
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-16 15:21:09 +01:00
Yingwei Zheng
e0a628715a
[ValueTracking] Convert isKnownNonZero to use SimplifyQuery (#85863)
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-04-12 23:47:20 +08:00
Artem Tyurin
1901f442ca
[InstCombine] Handle more even/odd math functions (#81324)
At the moment this PR adds support only for `erf` function.

Fixes #77220.
2024-02-24 21:35:35 +08:00
AtariDreams
c6b5ea339d
[Transforms] Expand optimizeTan to fold more inverse trig pairs (#77799)
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 15:30:35 +05:30
Anton Sidorenko
933247d9d6
[SimplifyLibCalls] Merge sqrt into the power of exp (#79146)
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 14:32:06 +05:30
AtariDreams
3746294451
[Transforms] Add more cos combinations to SimplifyLibCalls and InstCombine (#79699)
Add cos(fabs(x)) -> cos(x) and cos(copysign(x, y)) -> cos(x).
2024-02-06 00:30:18 +05:30
Yingwei Zheng
930996e9e4
[ValueTracking][NFC] Pass SimplifyQuery to computeKnownFPClass family (#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](e17bc67547/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.
2024-02-06 02:30:12 +08:00
Tanmay
4426a1b759
[InstCombine] Add log-pow simplification for FP exponent edge case. (#76641)
Fixes https://github.com/llvm/llvm-project/issues/76549

The cause of the optimization miss was -
1. `optimizePow` converting almost integer FP exponents to integer, and
turning `pow` to `powi`.
2. `optimizeLog` not accepting `Intrinsic::powi` as a target.

This patch converts constantInt back to constantFP where applicable and
adds a test.
2024-01-17 16:50:10 +07:00