521 Commits

Author SHA1 Message Date
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
Nikita Popov
a001e9718f [SimplifyLibCalls] Don't try to manually reprocess calls
The current code for reprocessing the result of fortified libcall
simplifications is not correct, because we might simplify to an
argument of the original call, and if that is again a libcall,
mistakenly think that this is actually the simplification result.

Instead of trying to fix this, simply remove the code entirely,
because InstCombine nowadays correctly handles reprocessing of
SimplifyLibCall results.

Fixes https://github.com/llvm/llvm-project/issues/77064.
2024-01-05 10:45:26 +01:00
Youngsuk Kim
2ac6b268da [llvm][Transforms][Utils] Remove no-op ptr-to-ptr bitcasts (NFC)
Opaque ptr cleanup effort (NFC).
2023-11-24 18:34:42 -06:00
Simon Pilgrim
3ca4fe80d4 [Transforms] Use StringRef::starts_with/ends_with instead of startswith/endswith. NFC.
startswith/endswith wrap starts_with/ends_with and will eventually go away (to more closely match string_view)
2023-11-06 16:50:18 +00:00
Bjorn Pettersson
07ece4ed94 [SimplifyLibCalls] Drop redundant CreatePointerCast in optimizeSPrintFString
Given the transition into using opaque pointers we no longer need to
add casts to handle "mismatched pointer types" as all pointers types
involved now are the same.
2023-09-18 22:17:10 +02:00
Fangrui Song
111fcb0df0 [llvm] Fix duplicate word typos. NFC
Those fixes were taken from https://reviews.llvm.org/D137338
2023-09-01 18:25:16 -07:00
Alexander Kornienko
0b779b0daa Revert "[AggressiveInstCombine] Fold strcmp for short string literals"
This reverts commit 5dde755188e34c0ba5304365612904476c8adfda,
cbfcf90152de5392a36d0a0241eef25f5e159eef and
8981520b19f2d2fe3d2bc80cf26318ee6b5b7473 due to a miscompile introduced in
8981520b19f2d2fe3d2bc80cf26318ee6b5b7473 (see
https://reviews.llvm.org/D154725#4568845 for details)

Differential Revision: https://reviews.llvm.org/D157430
2023-08-08 22:53:45 +02:00
Bjorn Pettersson
4ce7c4a92a [llvm] Drop some typed pointer handling/bitcasts
Differential Revision: https://reviews.llvm.org/D157016
2023-08-03 22:54:33 +02:00
Maksim Kita
8981520b19 [AggressiveInstCombine] Fold strcmp for short string literals
Fold strcmp() against 1-char string literals.

This designates AggressiveInstCombine as the pass for libcalls
simplifications that may need to change the control flow graph.

Fixes https://github.com/llvm/llvm-project/issues/58003.

Differential Revision: https://reviews.llvm.org/D154725
2023-07-19 17:12:27 +02:00
Elliot Goodrich
f0fa2d7c29 [llvm] Move AttributeMask to a separate header
Move `AttributeMask` out of `llvm/IR/Attributes.h` to a new file
`llvm/IR/AttributeMask.h`.  After doing this we can remove the
`#include <bitset>` and `#include <set>` directives from `Attributes.h`.
Since there are many headers including `Attributes.h`, but not needing
the definition of `AttributeMask`, this causes unnecessary bloating of
the translation units and slows down compilation.

This commit adds in the include directive for `llvm/IR/AttributeMask.h`
to the handful of source files that need to see the definition.

This reduces the total number of preprocessing tokens across the LLVM
source files in lib from (roughly) 1,917,509,187 to 1,902,982,273 - a
reduction of ~0.76%. This should result in a small improvement in
compilation time.

Differential Revision: https://reviews.llvm.org/D153728
2023-06-27 15:26:17 +01:00
Elliot Goodrich
b0abd4893f [llvm] Add missing StringExtras.h includes
In preparation for removing the `#include "llvm/ADT/StringExtras.h"`
from the header to source file of `llvm/Support/Error.h`, first add in
all the missing includes that were previously included transitively
through this header.
2023-06-25 15:42:22 +01:00
Youngsuk Kim
d22a236ae7 [llvm] Replace use of Type::getPointerTo() (NFC)
Partial progress towards replacing in-tree uses of
`Type::getPointerTo()`.

If `getPointerTo()` is used solely to support an unnecessary bitcast,
remove the bitcast.

Reviewed By: barannikov88, nikic

Differential Revision: https://reviews.llvm.org/D153307
2023-06-23 22:32:29 -04:00
Nikita Popov
0930ee8e86 [SimplifyLibCalls] Fix isKnownNeverInfinity() call after ORE removal
Missed this in 97b5cc214aee48e30391bfcd2cde4252163d7406.
2023-06-02 09:20:57 +02:00
Matt Arsenault
591ba11b93 Reapply "SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity"
This reverts commit b357f379c81811409348dd0e0273a248b055bb7a.
2023-05-23 08:48:25 +01:00
Alina Sbirlea
b357f379c8 Revert "SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity"
This reverts commit faa32734bf9a55fa3f91d91f6fdf0f8a951a9c0e.
Revert due to test failures introduced by 73925ef8b0eacc6792f0e3ea21a3e6d51f5ee8b0
2023-05-18 23:31:51 -07:00
Matt Arsenault
faa32734bf SimplifyLibCalls: Pass AssumptionCache to isKnownNeverInfinity
Let's assumes work for determining no infinities.
2023-05-18 19:44:56 +01:00
Matt Arsenault
86d0b524f3 ValueTracking: Expand signature of isKnownNeverInfinity/NaN
This is in preparation for replacing the implementation
with a wrapper around computeKnownFPClass.
2023-05-16 20:42:58 +01:00
Teresa Johnson
19c5740a5a [MemProf] Set hot/cold new values with option
Adds support to set the hot/cold new hint values with an option. Change
the defaults slightly to make it easier to distinguish between compiler
synthesized vs manually inserted calls to the interface.

Differential Revision: https://reviews.llvm.org/D150488
2023-05-12 15:47:10 -07:00
Dávid Bolvanský
9892094a8b [SLC] Use unsigned char to fix test failures on some platforms 2023-05-07 15:50:49 +02:00
Dávid Bolvanský
6321e4ddf7 [SimplifyLibCalls] Transform memchr(STR, C, N) to chain of ORs
Motivation:

```
#include <string_view>

size_t findFirst_ABCDEF(std::string_view sv) {
  return sv.find_first_of("ABCDEF");
}
```

memchr("ABCDEF", C,  6) != NULL -> (C == 'A' || C == 'B' || C == 'C' || C == 'D' || C == 'E' || C == 'F') != 0

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D128011
2023-05-07 15:12:03 +02:00
Teresa Johnson
39f7b48671 [MemProf] Use updated version of hot/cold operator new
Switch to the just updated versions of the API in tcmalloc that change
the name of the hot cold paramter to a reserved identifier __hot_cold_t.
This was based on feedback from Richard Smith, as I also need to add
some follow-on handling to clang so they are annotated properly.

Differential Revision: https://reviews.llvm.org/D149475
2023-04-28 13:35:46 -07:00
Teresa Johnson
16036f6cc9 [MemProf] Fix bot failure about missing return value along all paths
Fixes warning treated as error from
https://lab.llvm.org/buildbot/#/builders/13/builds/34440:
  'llvm::LibCallSimplifier::optimizeNew': not all control paths return a value
after a35206d78280e0ebcf48cd21bc5fff5c3b9c73fa.

Change assert in default case to a return nullptr, so we have a
reasonable fallback if invoked on a different library function, and no
longer get the above warning.
2023-04-19 14:09:05 -07:00
Teresa Johnson
a35206d782 [MemProf] Optionally pass hot/cold hints to operator new
Optionally (off by default) replace operator new() calls marked with a
hot or cold memprof attribute with an operator new() call that takes a
hot_cold_t parameter.

Currently this is supported by the open source version of tcmalloc, see:
https://github.com/google/tcmalloc/blob/master/tcmalloc/new_extension.h

Differential Revision: https://reviews.llvm.org/D148718
2023-04-19 13:33:46 -07:00