217 Commits

Author SHA1 Message Date
serge-sans-paille
dc4185fe2f
[TLI] Add support for reallocarray (#114818)
reallocarray is available in glibc since 2.29 under _DEFAULT_SOURCE and
under _GNU_SOURCE before, let's model it appropriately.
2024-11-13 20:57:29 +00:00
Kenji Mouri / 毛利 研二
7e877fc0ac
[Reland][TLI] Add support for hypot libcall. (#114343)
This patch adds basic support for `hypot`. Constant folding support will
be submitted in a subsequent patch.

Related issue: https://github.com/llvm/llvm-project/issues/113711

Note: It's my first time contributing to the LLVM with encouragement
from one of my friends, @fawdlstty. I learned a lot from
https://github.com/llvm/llvm-project/pull/99611, and thanks for that.

Note: I had created the same PR and merged
(https://github.com/llvm/llvm-project/pull/113724), but reverted caused
by the merging issue. (The CI issue happened in 3 A.M. at my timezone.
So, I need to fall asleep again after I replied about why issue
happened.) So, I rebased to the latest main branch and recreate the PR
and hope I won't have the third time to create the same PR.

I hope @arsenm can help me review the code again. I’m sorry for that.

Kenji Mouri
2024-10-31 07:50:29 -07:00
gulfemsavrun
36d5692570
Revert "[TLI] Add support for hypot libcall." (#114312)
Reverts llvm/llvm-project#113724
2024-10-30 15:10:29 -07:00
Kenji Mouri / 毛利 研二
feb2d867fa
[TLI] Add support for hypot libcall. (#113724)
This patch adds basic support for `hypot`. Constant folding support will
be submitted in a subsequent patch.

Related issue: https://github.com/llvm/llvm-project/issues/113711

Note: It's my first time contributing to the LLVM with encouragement
from one of my friends, @fawdlstty. I learned a lot from
https://github.com/llvm/llvm-project/pull/99611, and thanks for that.

Kenji Mouri
2024-10-30 10:34:32 -07:00
c8ef
0c1c37bfbe
[TLI] Add support for the tgamma libcall. (#113791)
This patch adds the `tgamma` libcall.
2024-10-29 10:08:38 +08:00
Fawdlstty
20bda93e43
[TLI] Add basic support for scalbnxx (#112936)
This patch adds basic support for `scalbln, scalblnf, scalblnl, scalbn,
scalbnf, scalbnl`. Constant folding support will be submitted in a
subsequent patch.

Related issue: <#112631>
2024-10-20 14:17:15 -07:00
c8ef
761fa5844e
[TLI] Add support for the ilogb libcall. (#112725)
This patch adds the `ilogb` libcall. Constant folding will be handled in
subsequent patches.
2024-10-18 14:20:34 +08:00
Alex Voicu
4852374135
[llvm][opt][Transforms] Replacement calloc should match replaced malloc (#110524)
Currently DSE unconditionally emits `calloc` as returning a pointer to
AS0. However, this is incorrect for targets that have a non-zero default
AS, as it'd not match the `malloc` signature. This patch addresses that
by piping through the AS for the pointer returned by `malloc` into the
`calloc` insertion call.
2024-10-01 02:05:28 +01:00
braw-lee
173841cc56
[TLI] Add basic support for fdim libcall (#108702)
first PR to fix #108695

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
2024-09-20 10:22:33 +04:00
Benjamin Maxwell
43c9203d49
[TLI] Support inferring function attributes for sincos[f|l] (#108554) 2024-09-18 09:40:29 +01:00
Noah Goldstein
c6e16a49ef [TLI] Add support for inferring attr cold/noreturn on std::terminate and __cxa_throw
These functions are both inherently on the error path so `cold` seems
appropriate. `noreturn` is definitional.

Closes #101622
2024-08-18 15:37:56 -07: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
Yingwei Zheng
6aa723daa9
[TLI] Add support for nan libfunc (#101356)
Reference: https://en.cppreference.com/w/cpp/numeric/math/nan
2024-08-01 01:49:38 +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
e8fbefe15b
[TLI] Add basic support for remquo libcall (#99611)
This patch adds basic support for `remquo`. Constant folding support
will be submitted in a subsequent patch.

Related issue: https://github.com/llvm/llvm-project/issues/99497
2024-07-19 16:35:59 +08:00
Yingwei Zheng
13a8f8d519
[InferAttrs] Set attributes for remainder (#99521)
Fixes one of the issues in
https://github.com/llvm/llvm-project/issues/99497.
2024-07-19 01:08:51 +08:00
Kees Cook
869ffcf3f6
[CodeGen][i386] Move -mregparm storage earlier and fix Runtime calls (#89707)
When building the Linux kernel for i386, the -mregparm=3 option is
enabled. Crashes were observed in the sanitizer handler functions, and
the problem was found to be mismatched calling convention.

As was fixed in commit c167c0a4dcdb ("[BuildLibCalls] infer inreg param
attrs from NumRegisterParameters"), call arguments need to be marked as
"in register" when -mregparm is set. Use the same helper developed there
to update the function arguments.

Since CreateRuntimeFunction() is actually part of CodeGenModule, storage
of the -mregparm value is also moved to the constructor, as doing this
in Release() is too late.

Fixes: https://github.com/llvm/llvm-project/issues/89670
2024-04-29 14:54:10 -07: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
Bjorn Pettersson
f205950e5c [BuildLibCalls] Use getPtrTy instead of getInt8PtrTy
Stop using getInt8PtrTy() when referring to char* types when
building libcalls. As in many other files we can simply use
getPtrTy() instead, as part of transition into opaque pointers.

This patch is using thing like
  Type *CharPtrTy = B.getPtrTy();
  Type *VoidPtrTy = B.getPtrTy();
in several places, to give the pointer type a name.
The idea is to make the mapping to the libc function prototypes clear.
2023-11-15 21:16:32 +01: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
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
Daniel Woodworth
750e20e171 [InferAttrs] Mark frexp and modf as memory(argmem: write)
These two math library functions can't write to errno, but they do
produce two results and store the second result to a pointer passed as
their second argument. Appropriately marking them as
memory(argmem: write) enables more optimizations on calls to them.

Reviewed By: xbolva00

Differential Revision: https://reviews.llvm.org/D148043
2023-04-19 15:24:45 -04:00
Craig Topper
9b2fecec40 [BuildLibCalls][RISCV] Sign extend return value of bcmp on riscv64.
riscv64 wants callees to sign extend signed and unsigned int returns.

The caller can use this to avoid a sign extend if the result is
used by a comparison since riscv64 only has 64-bit compares.

InstCombine/SimplifyLibCalls aggressively turn memcmps that are only
used by an icmp eq 0 into bcmp, but we lose the signext attribute that
would have been present on the memcmp. This causes an unneeded sext.w
in the generated assembly.

This looks even sillier if bcmp is implemented alias to memcmp. In
that case, not only did we not get any savings by using bcmp, we added
an instruction.

This probably applies to other functions, this just happens to be
the one I noticed so far.

See also the discussion here https://discourse.llvm.org/t/can-we-preserve-signext-return-attribute-when-converting-memcmp-to-bcmp/67126

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D139901
2022-12-20 11:47:04 -08:00
Nikita Popov
b3df1ce389 [MemoryBuiltins] Remove unused TLI parameters (NFC) 2022-12-09 12:57:40 +01:00
Krzysztof Parzyszek
26424c96c0 Attributes: convert Optional to std::optional 2022-12-02 08:15:45 -06:00
Nikita Popov
304f1d59ca [IR] Switch everything to use memory attribute
This switches everything to use the memory attribute proposed in
https://discourse.llvm.org/t/rfc-unify-memory-effect-attributes/65579.
The old argmemonly, inaccessiblememonly and inaccessiblemem_or_argmemonly
attributes are dropped. The readnone, readonly and writeonly attributes
are restricted to parameters only.

The old attributes are auto-upgraded both in bitcode and IR.
The bitcode upgrade is a policy requirement that has to be retained
indefinitely. The IR upgrade is mainly there so it's not necessary
to update all tests using memory attributes in this patch, which
is already large enough. We could drop that part after migrating
tests, or retain it longer term, to make it easier to import IR
from older LLVM versions.

High-level Function/CallBase APIs like doesNotAccessMemory() or
setDoesNotAccessMemory() are mapped transparently to the memory
attribute. Code that directly manipulates attributes (e.g. via
AttributeList) on the other hand needs to switch to working with
the memory attribute instead.

Differential Revision: https://reviews.llvm.org/D135780
2022-11-04 10:21:38 +01:00
Bjorn Pettersson
491ac8f3e8 [LibCalls] Cast Char argument to 'int' before calling emitFPutC
The helpers in BuildLibCalls normally expect that the Value
arguments already have the correct type (matching the lib call
signature). And exception has been emitFPutC which casted the Char
argument to 'int' using CreateIntCast. This patch moves the cast to
the caller instead of doing it inside emitFPutC.

I think it makes sense to make the BuildLibCall API:s a bit
more consistent this way, despite the need to handle the int cast
in two different places now.

Differential Revision: https://reviews.llvm.org/D135066
2022-10-04 12:52:05 +02:00
Bjorn Pettersson
aa1b64cc42 [BuildLibCalls] Use TLI to get 'int' and 'size_t' type sizes
Stop assuming that an 'int' is 32 bits in helpers that emit libcalls
to lib functions that had 'int' in the signature. For most targets
this is NFC. For a target with 16 bit 'int' type this could help out
detecting if trying to emit a libcall with incorrect signature.

Similarly we now derive the type mapping to 'size_t' by asking TLI
about the size of 'size_t'. This should be NFC (at least for in-tree
targets) since getSizeTSize(), in TLI, is deriving the size in the
same way as DataLayout::getIntPtrType().

Differential Revision: https://reviews.llvm.org/D135065
2022-10-04 12:52:05 +02:00
Bjorn Pettersson
73e8d95d28 [BuildLibCalls] Name types to identify when 'int' and 'size_t' is assumed. NFC
Lots of BuildLibCalls helpers are using Builder::getInt32Ty to get
a type matching an 'int', and DataLayout::getIntPtrType to get a
type matching 'size_t'. The former is not true for all targets, since
and 'int' isn't always 32 bits. And the latter is a bit weird as well
as the definition of DataLayout::getIntPtrType isn't clearly mapping
it to 'size_t'.

This patch is not aiming at solving any such problems. It is merely
highlighting when a libcall is expecting to use 'int' and 'size_t'
by naming the types as IntTy and SizeTTy when preparing the type
signatures for the emitted libcalls.

Differential Revision: https://reviews.llvm.org/D135064
2022-10-04 12:52:05 +02:00
Martin Sebor
e858f5120d [InstCombine] Remove assumptions about int having 32 bits
Reviewed By: bjope

Differential Revision: https://reviews.llvm.org/D131731
2022-08-16 15:35:08 -06:00
Fangrui Song
de9d80c1c5 [llvm] LLVM_FALLTHROUGH => [[fallthrough]]. NFC
With C++17 there is no Clang pedantic warning or MSVC C5051.
2022-08-08 11:24:15 -07:00
Augie Fackler
bd6aa67e02 BuildLibCalls: move inference of freeing memory later
This probably should have been part of D123089, but the effects of it
don't show up until we start removing functions from the table in
D130107. Oops.

Differential Revision: https://reviews.llvm.org/D130184
2022-07-21 15:31:16 -04:00
Martin Sebor
0d68ff87d2 [InstCombine] Transform strrchr to memrchr for constant strings
Add an emitter for the memrchr common extension and simplify the strrchr
call handler to use it. This enables transforming calls with the empty
string to the test C ? S : 0.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D128954
2022-07-01 11:10:00 -06:00
Augie Fackler
73f664601c BuildLibCalls: infer allockind attributes on relevant functions
Differential Revision: https://reviews.llvm.org/D123089
2022-05-31 10:01:17 -04:00
Nick Desaulniers
c167c0a4dc [BuildLibCalls] infer inreg param attrs from NumRegisterParameters
We're having a hard time booting the ARCH=i386 Linux kernel with clang
after removing -ffreestanding because instcombine was dropping inreg
from callers during libcall simplification, but not the callees defined
in different translation units. This led the callers and callees to have
wildly different calling conventions, which (predictably) blew up at
runtime.

Infer the inreg param attrs on function declarations from the module
metadata "NumRegisterParameters." This allows us to boot the ARCH=i386
Linux kernel (w/ -ffreestanding removed).

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

Reviewed By: efriedma

Differential Revision: https://reviews.llvm.org/D125285
2022-05-10 16:21:17 -07:00
Augie Fackler
1deea714b3 BuildLibCalls: simplify switch statement slightly
Per feedback on D123086 after submit.

Also added a test for vec_malloc et al attribute inference to show it's
doing the right thing.

The new tests exposed a defect, corrected by adding vec_free to the list of
free functions in MemoryBuiltins.cpp, which had been overlooked all the
way back in D94710, over a year ago.

Differential Revision: https://reviews.llvm.org/D124859
2022-05-03 13:17:33 -04:00
Jonas Paulsson
304378fd09 Reapply "[BuildLibCalls] Introduce getOrInsertLibFunc() for use when building
libcalls." (was 0f8c626). This reverts commit 14d9390.

The patch previously failed to recognize cases where user had defined a
function alias with an identical name as that of the library
function. Module::getFunction() would then return nullptr which is what the
sanitizer discovered.

In this updated version a new function isLibFuncEmittable() has as well been
introduced which is now used instead of TLI->has() anytime a library function
is to be emitted . It additionally also makes sure there is e.g. no function
alias with the same name in the module.

Reviewed By: Eli Friedman

Differential Revision: https://reviews.llvm.org/D123198
2022-05-02 19:37:00 +02:00
Augie Fackler
c7ae423e39 BuildLibCalls: add alloc-family attribute to many allocator functions
Differential Revision: https://reviews.llvm.org/D123086
2022-05-02 11:12:55 -04:00
Augie Fackler
e940456531 BuildLibCalls: infer allocptr attribute for free and realloc() family functions
Differential Revision: https://reviews.llvm.org/D123084
2022-05-02 09:43:21 -04:00
Fangrui Song
14d9390721 Revert D123198 "[BuildLibCalls] Introduce getOrInsertLibFunc() for use when building libcalls."
test/Transforms/InstCombine/pr39177.ll failed in a -DLLVM_USE_SANITIZER=Undefined build.
```
lib/Transforms/Utils/BuildLibCalls.cpp:1217:17: runtime error: reference binding to null pointer of type 'llvm::Function'
```
`Function &F = *M->getFunction(Name);`

This reverts commit 0f8c626723d2bbd547e78dcab5ab260dfbc437e1.
2022-04-19 22:26:10 -07:00
Jonas Paulsson
0f8c626723 [BuildLibCalls] Introduce getOrInsertLibFunc() for use when building libcalls.
A new set of overloaded functions named getOrInsertLibFunc() are now supposed
to be used instead of getOrInsertFunction() when building a libcall from
within an LLVM optimizer(). The idea is that this new function also makes
sure that any mandatory argument attributes are added to the function
prototype (after calling getOrInsertFunction()).

inferLibFuncAttributes() is renamed to inferNonMandatoryLibFuncAttrs() as it
only adds attributes that are not necessary for correctness but merely
helping with later optimizations.

Generally, the front end is responsible for building a correct function
prototype with the needed argument attributes. If the middle end however is
the one creating the call, e.g. when replacing one libcall with another, it
then must take this responsibility.

This continues the work of properly handling argument extension if required
by the target ABI when building a lib call. getOrInsertLibFunc() now does
this for all libcalls currently built by any LLVM optimizer. It is expected
that when in the future a new optimization builds a new libcall with an
integer argument it is to be added to getOrInsertLibFunc() with the proper
handling. Note that not all targets have it in their ABI to sign/zero extend
integer arguments to the full register width, but this will be done
selectively as determined by getExtAttrForI32Param().

Review: Eli Friedman, Nikita Popov, Dávid Bolvanský

Differential Revision: https://reviews.llvm.org/D123198
2022-04-19 21:22:07 +02:00
Augie Fackler
b916414096 BuildLibCalls: also set allocsize() attributes
This is part of being able to get rid of two more columns in
MemoryBuiltins.cpp's large table. We'll have two more changes before
we can finish the job.

Differential Revision: https://reviews.llvm.org/D119582
2022-04-07 12:38:44 -04:00
Jonas Paulsson
dbb6a75fbb [LibCalls] Respect TLI.getExtAttrForI32Param() in inferLibFuncAttributes().
getExtAttrForI32Param() is the method to be used for determining the type of
extension attribute (if any) that is to be added for a signed/unsigned
argument.

Previously, the SExt attribute was always added to the i32 ldexp* argument as
it was expected to be ignored by targets not needing it. This patch now
changes this so that it is only added for the targets that need it in the
first place.

Putchar() argument is now also extended as required by the target (SystemZ in
the test), to fix the issue below. Many more libcalls will be handled
similarly in a following patch.

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

Differential Revision: https://reviews.llvm.org/D123030

Review: Eli Friedman
2022-04-05 10:29:42 +02:00
Augie Fackler
b32735d599 BuildLibCalls: add allocalign attributes for memalign and aligned_alloc
This gets us close to being able to remove a column from the table in
MemoryBuiltins.cpp.

Differential Revision: https://reviews.llvm.org/D117923
2022-03-04 15:57:53 -05:00
serge-sans-paille
a494ae43be Cleanup includes: TransformsUtils
Estimation on the impact on preprocessor output:
before: 1065307662
after:  1064800684

Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D120741
2022-03-01 21:00:07 +01:00
Nikita Popov
5ba73c924d [BuildLibCalls] Mark calloc as inaccessiblememonly
Now that DSE handles inaccessiblememonly calloc, mark it as such,
as we do with other memory allocation functions.
2022-01-19 12:55:09 +01:00
Florian Hahn
e3275cfa94
[BuildLibCalls] Add nounwind,willreturn to memset_pattern{4,8,16}.
Similar to memset, memset_pattern{4,8,16} all will return and do not
unwind. Use fallthrough to include all attributes also set for memset.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D114904
2022-01-12 10:32:53 +00:00
Nikita Popov
348bc76e35 [LibCalls] Infer same attrs for reallocf() as realloc()
reallocf() is the same as realloc() but frees the input pointer
on failure as well. We can infer the same attributes.

Also combine some cases that infer the same attributes and are
logically related.
2022-01-07 09:51:15 +01:00
Philip Reames
c16fd6a376 Rename doesNotReadMemory to onlyWritesMemory globally [NFC]
The naming has come up as a source of confusion in several recent reviews.  onlyWritesMemory is consist with onlyReadsMemory which we use for the corresponding readonly case as well.
2022-01-05 08:52:55 -08:00
Benjamin Kramer
5f0a349738 Revert "Revert "[InferAttrs] Add writeonly to all the math functions""
This reverts commit 29b6e967f3e99ac45340ea37a70262c70e4e7528. The bug it
found in PartiallyInlineLibCalls was fixed in
c8ffc73350dbb6044ca947bbead127b9b914cdf3.
2022-01-05 12:16:35 +01:00