342 Commits

Author SHA1 Message Date
Nikita Popov
dc6876fc98
[ValueTracking] Use isSafeToSpeculativelyExecuteWithVariableReplaced() in more places (#109149)
This replaces some uses of isSafeToSpeculativelyExecute() with
isSafeToSpeculativelyExecuteWithVariableReplaced(), in cases where we
are guarding against operand changes rather plain speculation.

I believe that this is NFC with the current implementation of the
function (as it only does something different from loads), but this
makes us more defensive against future generalizations.
2024-09-19 09:38:20 +02:00
Maciej Gabka
95d2d1cba0
Move stepvector intrinsic out of experimental namespace (#98043)
This patch is moving out stepvector intrinsic from the experimental
namespace.

This intrinsic exists in LLVM for several years now, and is widely used.
2024-08-28 12:48:20 +01:00
Nikita Popov
4d2ae88d16 [InstCombine] Fix invalid scalarization of div
If the binop is not speculatable, and the extract index is out of
range, then scalarizing will perform the operation on a poison
operand, resulting in immediate UB, instead of the previous
poison result.

Fixes https://github.com/llvm/llvm-project/issues/97053.
2024-07-03 11:05:33 +02:00
Simon Pilgrim
5b4000dc58
[VectorUtils] Add llvm::scaleShuffleMaskElts wrapper for narrowShuffleMaskElts/widenShuffleMaskElts, NFC. (#96646)
Using the target number of vector elements, scaleShuffleMaskElts will try to use narrowShuffleMaskElts/widenShuffleMaskElts to scale the shuffle mask accordingly.

Working on #58895 I didn't want to create yet another case where we have to handle both re-scaling cases.
2024-06-26 10:43:58 +01: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
Nikita Popov
6be6c3a37b [InstCombine] Use disjoint flag for alternate binops
Check the or disjoint flag instead of the weaker MaskedValueIsZero
query.
2024-06-18 16:51:37 +02:00
Nikita Popov
8052e94946 [InstCombine] Avoid use of ConstantExpr::getShl()
Use the constant folding API instead. Use ImmConstant to make
sure it actually folds.
2024-06-18 16:43:19 +02:00
Nikita Popov
530d4c9bf3 [InstCombine] Use m_Poison() instead of m_Undef() (NFCI)
In this case the shuffle mask checks should already guarantee a
single-source shuffle, so this is just for clarity.
2024-05-21 16:15:30 +02:00
Nikita Popov
8d5b7d4d11 [InstCombine] Use m_Poison() instead of m_Undef() (NFCI)
In this case, the isIdentityWithExtract() checks should already
guarantee that these are single-source shuffles, so this is just
for clarity.
2024-05-21 16:07:46 +02:00
Nikita Popov
d0e0205bfc [InstCombine] Check for poison instead of undef in single shuffle fold
Otherwise we'll convert undef to poison. Alive2 was already flagging
the existing test8 test as a miscompile.
2024-05-21 16:03:20 +02:00
Nikita Popov
fbc798e442 [InstCombine] Use m_Poison instead of m_Undef (NFCI)
In this case, isIdentityWithExtract() should already ensure that
this is a single-source shuffle. This just makes things more
explicit.
2024-05-21 15:48:35 +02:00
Nikita Popov
8f1c984325 [InstCombine] Check for poison instead of undef in shuffle of unop transform
Otherwise this may not actually be a single-source shuffle.
2024-05-21 15:43:50 +02:00
Nikita Popov
2f1e2325cf [InstCombine] Use m_Poison instead of m_Undef in some places (NFCI)
I believe that in these cases other conditions already ensure that
the second operand is not used, this is mostly for clarity.
2024-05-21 15:33:25 +02:00
Nikita Popov
ecd269e830 [InstCombine] Check for poison instead of undef in splat shuffle fold
We can't canonicalize these to a splat shuffle, as doing so would
convert undef -> poison.
2024-05-21 15:21:31 +02:00
Nikita Popov
263224e448 [InstCombine] Require poison operand in canEvaluateShuffled transform
This transform works on single-source shuffles, which require that
the second operand is poison, not undef. Otherwise we may convert
undef to poison.

Fixes https://github.com/llvm/llvm-project/issues/92887.
2024-05-21 15:00:01 +02:00
Marc Auberer
b3fe27f2be
[InstCombine] Copy flags of extractelement for extelt -> icmp combine (#86366)
Fixes #86164
2024-03-24 16:14:56 +01:00
Michele Scandale
536cb1fad3
[InstCombine] Fix for folding select-like shufflevector into floating point binary operators. (#85452)
Folding a select-like `shufflevector` into a floating point binary
operators can only be done if the result is preserved for both case. In
particular, if the common operand of the `shufflevector` and the
floating point binary operator can be a NaN, then the transformation
won't preserve the result value.
2024-03-21 12:40:18 -07:00
Jeremy Morse
2fe81edef6 [NFC][RemoveDIs] Insert instruction using iterators in Transforms/
As part of the RemoveDIs project we need LLVM to insert instructions using
iterators wherever possible, so that the iterators can carry a bit of
debug-info. This commit implements some of that by updating the contents of
llvm/lib/Transforms/Utils to always use iterator-versions of instruction
constructors.

There are two general flavours of update:
 * Almost all call-sites just call getIterator on an instruction
 * Several make use of an existing iterator (scenarios where the code is
   actually significant for debug-info)
The underlying logic is that any call to getFirstInsertionPt or similar
APIs that identify the start of a block need to have that iterator passed
directly to the insertion function, without being converted to a bare
Instruction pointer along the way.

Noteworthy changes:
 * FindInsertedValue now takes an optional iterator rather than an
   instruction pointer, as we need to always insert with iterators,
 * I've added a few iterator-taking versions of some value-tracking and
   DomTree methods -- they just unwrap the iterator. These are purely
   convenience methods to avoid extra syntax in some passes.
 * A few calls to getNextNode become std::next instead (to keep in the
   theme of using iterators for positions),
 * SeparateConstOffsetFromGEP has it's insertion-position field changed.
   Noteworthy because it's not a purely localised spelling change.

All this should be NFC.
2024-03-05 15:12:22 +00:00
Nikita Popov
92fc4b482f [InstCombine] Preserve poison in bitcast of insertelement fold
If the base was poison, retain the poison value.
2023-12-19 13:06:04 +01:00
Nikita Popov
67fd4e3408 [InstCombine] Check for poison instead of undef in shuffle transform
This one doesn't seem to make a practical difference because we'd
canonicalize undef -> poison in the relevant cases anywy.
2023-12-19 12:56:52 +01:00
Nikita Popov
9d25b28b9e [InstCombine] Explicitly canonicalize splat shuffles to use poison RHS
This is usually handled by demanded elements simplification. However,
as that is not supported for scalable vectors, also handle it
explicitly here.
2023-12-18 16:30:40 +01:00
Nikita Popov
e93d324adb [InstCombine] Preserve poison in evaluateInDifferentElementOrder()
Don't unnecessarily replace poison with undef.
2023-12-18 15:36:22 +01:00
Nikita Popov
6c9813aa02 [InstCombine] Check for poison instead of undef in shuffle combine
Otherwise we may replace undef with poison.

Note that a lot of tests regressing here already have variants
that use poison instead of undef (often in a separate
inseltpoison file), which is why I'm not adjusting them to the
new pattern.
2023-12-18 15:19:16 +01:00
Paul Walker
dea16ebd26
[LLVM][IR] Replace ConstantInt's specialisation of getType() with getIntegerType(). (#75217)
The specialisation will not be valid when ConstantInt gains native
support for vector types.

This is largely a mechanical change but with extra attention paid to constant
folding, InstCombineVectorOps.cpp, LoopFlatten.cpp and Verifier.cpp to
remove the need to call `getIntegerType()`.

Co-authored-by: Nikita Popov <github@npopov.com>
2023-12-18 11:58:42 +00:00
Nikita Popov
465ecf872e [InstCombine] Rename UndefElts -> PoisonElts (NFC)
In line with updated shufflevector semantics, this represents the
poison elements rather than undef elements now. This commit is a
pure rename, without any logic changes.
2023-12-18 12:36:19 +01:00
Alexey Bataev
e22818d5c9 [IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst.
Need to add NumSrcElts param to is..Mask functions in
ShuffleVectorInstruction class for better mask analysis. Mask.size() not
always matches the sizes of the permuted vector(s). Allows to better
estimate the cost in SLP and fix uses of the functions in other cases.

Differential Revision: https://reviews.llvm.org/D158449
2023-10-05 06:17:07 -07:00
Arthur Eubanks
07389535a7 Revert "[IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst."
This reverts commit b186f1f68be11630355afb0c08b80374a6d31782.

Causes crashes, see https://reviews.llvm.org/D158449.
2023-10-04 14:37:16 -07:00
Alexey Bataev
b186f1f68b [IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst.
Need to add NumSrcElts param to is..Mask functions in
ShuffleVectorInstruction class for better mask analysis. Mask.size() not
always matches the sizes of the permuted vector(s). Allows to better
estimate the cost in SLP and fix uses of the functions in other cases.

Differential Revision: https://reviews.llvm.org/D158449
2023-10-04 07:53:30 -07:00
Alexey Bataev
1129dec778 Revert "[IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst."
This reverts commit 6f43d28f3452b3ef598bc12b761cfc2dbd0f34c9 to fix
a crash reported in https://reviews.llvm.org/D158449.
2023-10-03 13:02:16 -07:00
Alexey Bataev
6f43d28f34 [IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst.
Need to add NumSrcElts param to is..Mask functions in
ShuffleVectorInstruction class for better mask analysis. Mask.size() not
always matches the sizes of the permuted vector(s). Allows to better
estimate the cost in SLP and fix uses of the functions in other cases.

Differential Revision: https://reviews.llvm.org/D158449
2023-10-03 10:26:11 -07:00
Alexey Bataev
ebcb5d59fc Revert "[IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst."
This reverts commit 9f5960e004ff54082ccfa9396522e07358f5b66b to fix
buildbots reported here https://lab.llvm.org/buildbot/#/builders/230/builds/19412.
2023-09-29 15:03:46 -07:00
Alexey Bataev
9f5960e004 [IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst.
Need to add NumSrcElts param to is..Mask functions in
ShuffleVectorInstruction class for better mask analysis. Mask.size() not
always matches the sizes of the permuted vector(s). Allows to better
estimate the cost in SLP and fix uses of the functions in other cases.

Differential Revision: https://reviews.llvm.org/D158449
2023-09-29 13:16:03 -07:00
Alexey Bataev
3204f88a8b Revert "[IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst."
This reverts commit c88c281cf1ac1a01c55231b93826d7c8ae83985b to fix the
crash revealed by https://lab.llvm.org/buildbot/#/builders/230/builds/19353.
2023-09-28 11:57:32 -07:00
Alexey Bataev
c88c281cf1 [IR]Add NumSrcElts param to is..Mask static function in ShuffleVectorInst.
Need to add NumSrcElts param to is..Mask functions in
ShuffleVectorInstruction class for better mask analysis. Mask.size() not
always matches the sizes of the permuted vector(s). Allows to better
estimate the cost in SLP and fix uses of the functions in other cases.

Differential Revision: https://reviews.llvm.org/D158449
2023-09-28 11:03:21 -07:00
Jeremy Morse
e54277fa10 [NFC][RemoveDIs] Use iterators over inst-pointers when using IRBuilder
This patch adds a two-argument SetInsertPoint method to IRBuilder that
takes a block/iterator instead of an instruction, and updates many call
sites to use it. The motivating reason for doing this is given here [0],
we'd like to pass around more information about the position of debug-info
in the iterator object. That necessitates passing iterators around most of
the time.

[0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939

Differential Revision: https://reviews.llvm.org/D152468
2023-09-11 20:01:19 +01:00
Jeremy Morse
d529943a27 [NFC][RemoveDIs] Prefer iterators over inst-pointers in InstCombine
As per my proposal for how to eliminate debug intrinsics [0], for various
places in InstCombine prefer to insert using an instruction iterator rather
than an instruction pointer. This is so that we can eventually pass more
information in the iterator class. These call-sites where I've changed the
spelling are those that necessary to build a stage2clang to produce an
identical binary in the coming no-debug-intrinsics mode.

[0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939

Differential Revision: https://reviews.llvm.org/D152543
2023-09-11 15:04:51 +01:00
Nuno Lopes
eb1617a582 [InstCombineVectorOps] Use poison instead of undef as placeholder [NFC]
It's used to create a vector where only 1 element is used
While at it, change OOB extractelement to yield poison per LangRef
2023-07-29 15:28:13 +01:00
Nuno Lopes
9324e1be07 [InstCombineVectorOps] Use poison instead of undef as placeholder [NFC]
Undef was being used to populate unused vector lanes.
While at it, switch extractelement to use poison as the OOB value (per LangRef)
2023-07-20 08:14:55 +01:00
Nikita Popov
b7e38ff223 [InstCombine] Add old extract to worklist for DCE
To make sure it is removed in the same InstCombine iteration.
2023-07-05 17:00:40 +02:00
Nikita Popov
ab94c1bad3 [InstCombine] Add created extracts to worklist
Use InstCombine's insertion helper for the created extracts, so
they become part of the worklist and will be revisited.
2023-06-23 16:11:47 +02:00
Nikita Popov
57a8ea8553 [InstCombine] Avoid infinite loop in insert/extract combine
Fix the infinite loop reported on https://reviews.llvm.org/D151807#4420467.

collectShuffleElements() will widen vectors and replace extracts
via replaceExtractElements(), to allow the next call of
collectShuffleElements() to fold. However, it's possible for another
fold to run first, and break the expected sequence again. To ensure
this does not happen, directly rerun the collectShuffleElements()
fold if we have adjusted extracts.
2023-06-14 14:58:49 +02:00
Nikita Popov
7c878f4504 [InstCombine] Directly iterate over users (NFC)
After 3a223f1eafe331508d171b519df8a4984791ab48, it's no longer
necessary to put the users into a vector. We can directly iterate
them instead.
2023-05-24 10:39:32 +02:00
Nikita Popov
3a223f1eaf [InstCombine] Fix crash due to early extractvalue removal
Fixes the issue reported at 4b8320868c (commitcomment-114671248).

The extractvalue instructions may still be used by the calling code
in some cases. Rather than trying to figure out which extracts are
safe to remove and which aren't, add them to the worklist so they
will get DCEd by the main loop.
2023-05-24 09:55:52 +02:00
Nikita Popov
4b8320868c [InstCombine] Remove dead extractelements (NFCI)
Directly remove these dead extractelement instructions, rather than
leaving them for the next InstCombine iteration to clean up.

Should be mostly NFC, apart from worklist order differences.
2023-05-23 15:40:48 +02:00
Nikita Popov
86a5a75049 [InstCombine] Use canonical index type in more places
Also fixes an issue with 80cd3e4e2059f8deda735c32bf7dfa5b9d75c4e0
pointed out by Roman Divacky: For one of the occurrences I only
changed the name of the type variable, without actually changing
the type...
2023-05-17 18:06:08 +02:00
Nikita Popov
605f0a46dc [InstCombine] Use IRBuilder in evaluateInDifferentElementOrder()
This ensures that the new instructions get reprocessed in the same
iteration.

This should be largely NFC, apart from worklist order effects and
naming changes, as seen in the test diff.
2023-05-17 15:07:36 +02:00
Nikita Popov
3b8f442289 [InstCombine] Fix worklist management for multi-use demanded element fold
Add the old instruction to the worklist, so it can be DCEd in the
same iteration.
2023-05-17 14:54:27 +02:00
Nikita Popov
80cd3e4e20 [InstCombine] Use canonical type in insertelement (NFC)
We can directly create these with the correct type.
2023-05-17 14:37:32 +02:00
ManuelJBrito
e335e8a432 [InstCombine] Update instcombine for vectorOps to use new shufflevector semantics
This patch updates the transformations in InstCombineVectorOps to use the new
hufflevector semantics that say that undefined values in the mask yield poison.

To prevent miscompilations we have to match with m_Poison instead of m_Undef.
Otherwise, we might introduce poison where there was previously undef.

Differential Revision: https://reviews.llvm.org/D150039
2023-05-17 07:56:45 +01:00