39813 Commits

Author SHA1 Message Date
Ramkumar Ramachandra
c807395011
[LAA/SLP] Don't truncate APInt in getPointersDiff (#139941)
Change getPointersDiff to return an std::optional<int64_t>, and fill
this value with using APInt::trySExtValue. This simple change requires
changes to other functions in LAA, and major changes in SLPVectorizer
changing types from 32-bit to 64-bit.

Fixes #139202.
2025-05-15 10:08:05 +01:00
George Chaltas
c4f7ab1d2e
[LV] Initialize IR block pointers in ILV. (NFC) (#139807)
Setting unitialized pointers to nullptr in InnerLoopVectorizer()
constructor. These were noticed during a review of the code. Seems like
a good idea to clean them up.
2025-05-15 09:18:45 +01:00
Kazu Hirata
690a30f3fd
[llvm] Construct SmallVector with ArrayRef (NFC) (#139992) 2025-05-14 22:30:38 -07:00
Jessica Clarke
864f0ff4ef
[clang][IR] Overload @llvm.thread.pointer to support non-AS0 targets (#132489)
Thread-local globals live, by default, in the default globals address
space, which may not be 0, so we need to overload @llvm.thread.pointer
to support other address spaces, and use the default globals address
space in Clang.
2025-05-14 21:51:56 +01:00
Min-Yih Hsu
0ab67ec191
[LV][EVL] Introduce the EVLIndVarSimplify Pass for EVL-vectorized loops (#131005)
When we enable EVL-based loop vectorization w/ predicated tail-folding,
each vectorized loop has effectively two induction variables: one
calculates the step using (VF x vscale) and the other one increases the
IV by values returned from experiment.get.vector.length. The former,
also known as canonical IV, is more favorable for analyses as it's
"countable" in the sense of SCEV; the latter (EVL-based IV), however, is
more favorable to codegen, at least for those that support scalable
vectors like AArch64 SVE and RISC-V.

The idea is that we use canonical IV all the way until the end of all
vectorizers, where we replace it with EVL-based IV using EVLIVSimplify
introduced here. Such that we can have the best from both worlds.

This Pass is enabled by default in RISC-V. However, since we haven't
really vectorize loops with predicate tail-folding by default, this Pass
is no-op at this moment.
2025-05-14 13:49:50 -07:00
Alexey Bataev
a05cf2927a [SLP][NFC]Use WeakTrackVH instead of Instruction in EntryToLastInstruction
Use WEakTrackVH to prevent instability in the vectorizer.

Fixes #139729
2025-05-14 11:19:54 -07:00
Philip Reames
cf552a1d82 [LIR] Simplify processLoopStridedStore [nfc]
Just move the assertions and exits down under the existing if clauses.
This improves readability.
2025-05-14 11:13:34 -07:00
Vladislav Dzhidzhoev
47144a0dc1
[CloneFunction][DebugInfo] Ensure DILocalVariables of inlined functions are not cloned (NFC) (#138590)
This change was separated from
https://github.com/llvm/llvm-project/pull/119001.

When cloning functions, use IdentityMDPredicate to ensure that if
DISubprogram is not cloned, then its DILocalVariables are not cloned
either.

This is currently expected to be an NFC, as DILocalVariables only
reference their subprograms (via DILocalScopes) and types, and inlined
DISubprograms and DITypes are not cloned. Thus, DILocalVariables are
mapped to self in ValueMapper (in mapTopLevelUniquedNode).

However, it will be needed for the original PR
https://github.com/llvm/llvm-project/pull/119001, where a
DILocalVariable may refer to a local type of a DISubprogram that is
being cloned. In that case, ValueMapper will clone DILocalVariable even
if it belongs to an inlined DISubprogram that is not cloned, which
should be avoided.
I'm making this change into a separate PR to make the original PR a bit
smaller, and because this has more to do with variables than with types.
2025-05-14 19:41:56 +02:00
Yingwei Zheng
aa054c6810
[ConstraintElim] Simplify and/or instead of replacing its operand (#139874)
In `checkOrAndOpImpliedByOther`, replacing an operand of a disjoint or
is unsafe: https://alive2.llvm.org/ce/z/4R4hxN
This patch performs the simplification directly, to avoid miscompilation
and unnecessary canonicalization.

Closes https://github.com/llvm/llvm-project/issues/137937.
2025-05-14 23:37:41 +08:00
Graham Hunter
5b9246517f
[LV] Fix ScalarIVSteps vplan pattern matcher, remove m_CanonicalIV() (#138298)
783a846 changed VPScalarIVStepsRecipe to take 3 arguments (adding
VF explicitly) instead of 2, but didn't change the corresponding
pattern matcher.

This matcher was only used in vputils::isHeaderMask, and no test
ever reached that function with a ScalarIVSteps recipe for the
value being matched -- it was always a WideCanonicalIV. So the
matcher bailed out immediately before checking arguments and
asserting that the number of arguments in the recipe was the
same provided by the matcher.

Since the constructors for ScalarIVSteps take 3 values, we should
be safe to update the matcher and guard it with a dedicated gtest.

m_CanonicalIV() on the other hand is removed; as a phi recipe it
may not have a consistent number of arguments to match, only
requiring one (the start value) when being constructed with the
assumption that a second incoming value is added for the backedge
later. In order to keep the matcher we would need to add multiple
matchers with different numbers of arguments for it depending on
what phase of vplan construction we were in, and ensure that we
never reorder matcher usage vs. vplan transformation. Since the
main IR PatternMatch.h doesn't contain any matchers for PHI nodes,
I think we can just remove it and match via m_Specific() using the
VPValue we get from Plan.getCanonicalIV().
2025-05-14 15:01:03 +01:00
Florian Hahn
7a9fd62278
[VPlan] Use VPlan operand order for VPBlendRecipes. (#139475)
Don't use the order of incoming values of IR phis when creating 
VPBlendRecipes. Instead, simply use the incoming operands and
blocks from the VPWidenPHIRecipe.

Note that this changes the order of the incoming operands/masks for some
blends.

PR: https://github.com/llvm/llvm-project/pull/139475
2025-05-14 14:56:35 +01:00
Marina Taylor
1914184e5c
Reland "[ObjCARC][Contract] Optimize bundled RetainRV to ClaimRV" (#139889)
This teaches ObjCARCContract to transform attachedcall bundles
referencing objc_retainAutoreleasedReturnValue to instead reference
objc_claimAutoreleasedReturnValue.

The only distinction between the two is that the latter is required to
be guaranteed to immediately follow the call it's attached to, and, by
construction, the bundles always achieve that by:
- not being separable from the call through IR and the backend
- not getting the marker emitted when claimARV is the attachedcall.

This is enabled only for arm64, arm64e, and arm64_32 on macOS13/iOS16
and related operating systems.

Co-authored-by: Ahmed Bougacha <ahmed@bougacha.org>
2025-05-14 14:21:52 +01:00
Florian Hahn
98683b0a48
[VPlan] Construct VPBlendRecipe from VPWidenPHIRecipe (NFC).
Update VPRecipeBuilder to construct VPBlendRecipe from VPWidenPHIRecipe,
starting to thread recipes through the builder instead of the
underlying IR instruction up-front.

Landing first part of approved
https://github.com/llvm/llvm-project/pull/139475 separately as NFC as
suggested.
2025-05-14 11:17:26 +01:00
Usman Nadeem
b93173185d
[InstCombine] Narrow trunc(lshr) in more cases (#139645)
We can narrow `trunc(lshr(i32)) to i8` to `trunc(lshr(i16)) to i8` even
when the bits that we are shifting in are not zero, in the cases where
the MSBs of the shifted value don't actually matter and actually end up
being truncated away.

This kind of narrowing does not remove the trunc but can help the
vectorizer generate better code in a smaller type.
Motivation: libyuv, functions like ARGBToUV444Row_C().

Proof: https://alive2.llvm.org/ce/z/9Ao2aJ
2025-05-13 13:42:42 -07:00
Florian Hahn
8767d55ff3
[VPlan] Consistently use VPlanTransforms::runPass if possible (NFC).
Update some more transforms to use ::runPass.
2025-05-13 20:50:27 +01:00
Marina Taylor
5bb8e9db5f
Revert "[ObjCARC][Contract] Optimize bundled RetainRV to ClaimRV" (#139780)
Reverts llvm/llvm-project#139762 for breaking bots
2025-05-13 20:20:06 +01:00
Marina Taylor
810148cb07
[ObjCARC][Contract] Optimize bundled RetainRV to ClaimRV (#139762)
This teaches ObjCARCContract to transform attachedcall bundles
referencing objc_retainAutoreleasedReturnValue to instead reference
objc_claimAutoreleasedReturnValue.

The only distinction between the two is that the latter is required to
be guaranteed to immediately follow the call it's attached to, and, by
construction, the bundles always achieve that by:
- not being separable from the call through IR and the backend
- not getting the marker emitted when claimARV is the attachedcall.

This is enabled only for arm64, arm64e, and arm64_32 on macOS13/iOS16
and related operating systems.

Co-authored-by: Ahmed Bougacha <ahmed@bougacha.org>
2025-05-13 19:25:28 +01:00
Ryotaro Kasuga
e99ca74dc1
[LoopInterchange] Relax the legality check to accept more patterns (#139690)
When proving the legality of exchanging two loops, it doesn't need to
check the elements of the direction vectors associated with the loops
outside of the two target loops. Before this patch, the legality check
looked at all elements of a direction vector to calculate the
lexicographically order of the vector, which may reject some legal
exchanges. For example, if a direction vector is `[* < =]`, it is safe
to swap the last two loops because the corresponding subsequence of the
vector (`[< =]`) is lexicographically positive for both before and after
the exchange. However, the its order is unknown if we don't drop the
prefix since the first element is `*`. This patch improves the logic of
legality check to ignore such unrelated prefixes of direction vectors.
2025-05-13 21:14:14 +09:00
David Green
671cef029f
[AggressiveInstcombine] Fold away shift in or reduction chain. (#137875)
If we have `icmp eq or(a, shl(b)), 0` then the shift can be removed so
long as it is nuw or nsw. It is still comparing that some bits are
non-zero.
https://alive2.llvm.org/ce/z/nhrBVX.

This is also true of ne, and true for longer or chains.
2025-05-13 10:33:38 +01:00
Matt Arsenault
6d35ec2335
ObjCARC: Fix regression from using ConstantData uselists (#139609)
Fixes regression after 9383fb23e18bb983d0024fb956a0a724ef9eb03d
2025-05-13 10:52:49 +02:00
Florian Hahn
ba2dacd276
[VPlan] Print use and definition in verifier on violation.
Improves the error message when a use comes before the def by including
the use and def, when print utilities are available.
2025-05-13 09:52:02 +01:00
Antonio Frighetto
adfd59fdb8 [InstCombine] Introduce foldICmpBinOpWithConstantViaTruthTable folding
Match icmps of binops where both operands are select with constant arms,
i.e., `icmp pred (select A ? C1 : C2) binop (select B ? C3 : C4), C5`.
Fold such patterns by creating a truth table of the possible four
constant variants, and materialize back the optimal logic from it via
`createLogicFromTable` helper. This also generalizes an existing fold,
which has therefore been dropped.

Proofs: https://alive2.llvm.org/ce/z/NS7Vzu.

Fixes: https://github.com/llvm/llvm-project/issues/138212.
2025-05-13 09:04:25 +02:00
Ryotaro Kasuga
6dc6ca3302
[LoopInterchange] Skip legality check if surrounding loops already guarantee dependency (NFC) (#139254)
The legality check in LoopInterchange allows two loops to be exchanged
if all direction vectors are lexicographically positive (or zero) for
both before and after the swap. The current implementation performs this
routine naively. However, if a direction vector is lexicographically
positive due to an element corresponding to a loop that is outside the
given two loops (i.e., if there is an element `<` before the loops we
are trying to interchange), then obviously it is also positive after
exchanging them. For example, for a direction vector `[< < >]`, swapping
the last two elements doesn't make it lexicographically negative because
the first element is `<`.
This patch adds a code to skip legality check if surrounding loops
already guarantee that the direction vector is lexicographically
positive. Note that this is only a small improvement on its own, but
it's necessary to relax the legality check I'm working on.

Split off from #118267

---------

Co-authored-by: Michael Kruse <llvm-project@meinersbur.de>
2025-05-13 10:19:17 +09:00
Yingwei Zheng
6f1f6d184f
[InstCombine][DebugInfo] Update debug value uses in freelyInvertAllUsersOf (#137013)
This patch updates all debug value uses in `freelyInvertAllUsersOf` by
inserting `DW_OP_not` at the front of the DIExpression.

Related issue: https://github.com/llvm/llvm-project/issues/71065
2025-05-13 08:56:22 +08:00
Rahul Joshi
86c5112409
[NFCI][LLVM/MLIR] Adopt TrailingObjects convenience API (#138554)
Adopt `TrailingObjects` convenience API that was added in
https://github.com/llvm/llvm-project/pull/138970 in LLVM and MLIR code.
2025-05-12 16:17:43 -07:00
Luke Lau
cf3242f3b0
[InstCombine] Pull shuffles out of binops with splatted ops (#137948)
Given a binary op on splatted vector and a splatted constant,
InstCombine will normally pull the shuffle out in
`InstCombinerImpl::foldVectorBinop`:

```llvm
define <4 x i32> @f(i32 %x) {
  %x.insert = insertelement <4 x i32> poison, i32 %x, i64 0
  %x.splat = shufflevector <4 x i32> %x.insert, <4 x i32> poison, <4 x i32> zeroinitializer
  %res = add <4 x i32> %x.splat, splat (i32 42)
  ret <4 x i32> %res
}
```

```llvm
define <4 x i32> @f(i32 %x) {
  %x.insert = insertelement <4 x i32> poison, i32 %x, i64 0
  %1 = add <4 x i32> %x.insert, <i32 42, i32 poison, i32 poison, i32 poison>
  %res = shufflevector <4 x i32> %1, <4 x i32> poison, <4 x i32> zeroinitializer
  ret <4 x i32> %res
}
```

However, this currently only operates on fixed length vectors. Splats of
scalable vectors don't currently have their shuffle pulled out, e.g:

```llvm
define <vscale x 4 x i32> @f(i32 %x) {
  %x.insert = insertelement <vscale x 4 x i32> poison, i32 %x, i64 0
  %x.splat = shufflevector <vscale x 4 x i32> %x.insert, <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
  %res = add <vscale x 4 x i32> %x.splat, splat (i32 42)
  ret <vscale x 4 x i32> %res
}
```

Having this canonical form with the shuffle pulled out is important as
VectorCombine relies on it in order to scalarize binary ops in
`scalarizeBinopOrCmp`, which would prevent the need for #137786. This
also brings it in line for scalable binary ops with two non-constant
operands: https://godbolt.org/z/M9f7ebzca

This adds a combine just after the fixed-length version, but restricted
to splats at index 0 so that it also handles the scalable case:

So the whilst the existing combine looks like: `Op(shuffle(V1, Mask), C)
-> shuffle(Op(V1, NewC), Mask)`

This patch adds: `Op(shuffle(V1, 0), (splat C)) -> shuffle(Op(V1, (splat
C)), 0)`

I think this could be generalized to other splat indexes that aren't
zero, but I think it would be dead code since only fixed-length vectors
can have non-zero shuffle indices, which would be covered by the
existing combine.
2025-05-13 00:11:55 +01:00
Florian Hahn
045fdda39d
[VPlan] Replace TTI::getOperandInfo with Ctx.getOperandInfo (NFC).
Update to use VPlan-based implementation of getOperandInfo, removing
uses of underlying IR references.
2025-05-12 22:44:54 +01:00
Ellis Hoag
78f0af5d89
[SimplifyCFG][swifterror] Don't sink calls with swifterror params (#139015)
We've encountered an LLVM verification failure when building Swift with
the SimplifyCFG pass enabled. I found that
https://reviews.llvm.org/D158083 fixed this pass by preventing sinking
loads or stores of swifterror values, but it did not implement the same
protection for call or invokes.
In `Verifier.cpp`
[here](c685355811/llvm/lib/IR/Verifier.cpp (L4360-L4364))
and
[here](c685355811/llvm/lib/IR/Verifier.cpp (L3661-L3662))
we can see that swifterror values must also be used directly by call
instructions.
2025-05-12 14:37:26 -07:00
Alexey Bataev
e1ea86e849 [SLP]Do not try to use interleaved loads, if reordering is required
If the interleaved loads require reordering, better to avoid generate
load + shuffle sequence, which in this case cannot be recognized as
interleaved load. Also, it fixes the issue with the incorrect codegen.

Fixes #138923
2025-05-12 14:12:51 -07:00
Florian Hahn
fb017a52e7
[VPlan] Use load/store opcode for VPWiden(Load|Store)EVLRecipe (NFC).
Removes unnecessary uses of Ingredient.
2025-05-12 21:30:18 +01:00
Ramkumar Ramachandra
4f0be9414c
[LV] Improve code in selectInterleaveCount (NFC) (#128002)
Use the fact that getSmallBestKnownTC returns an exact trip count, if
possible, and falls back to returning an estimate, to factor some code
in selectInterleaveCount.
2025-05-12 17:20:10 +01:00
Han-Kuan Chen
53df6400af
[SLP] Fix incorrect operand order in interchangeable instruction. (#139225) 2025-05-12 20:03:45 +08:00
Florian Hahn
2f55123cbb
[VPlan] Handle early exit before forming regions. (NFC) (#138393)
Move early-exit handling up front to original VPlan construction, before
introducing early exits.

This builds on https://github.com/llvm/llvm-project/pull/137709, which
adds exiting edges to the original VPlan, instead of adding exit blocks
later.

This retains the exit conditions early, and means we can handle early
exits before forming regions, without the reliance on VPRecipeBuilder.

Once we retain all exits initially, handling early exits before region
construction ensures the regions are valid; otherwise we would leave
edges exiting the region from elsewhere than the latch.

Removing the reliance on VPRecipeBuilder removes the dependence on
mapping IR BBs to VPBBs and unblocks predication as VPlan transform:
https://github.com/llvm/llvm-project/pull/128420.

Depends on https://github.com/llvm/llvm-project/pull/137709 (included in
PR).

PR: https://github.com/llvm/llvm-project/pull/138393
2025-05-12 12:53:20 +01:00
Mel Chen
688bccb290
[TTI][LV] Simplify the prototype of preferPredicatedReductionSelect. nfc (#139265) 2025-05-12 17:24:37 +08:00
John Brawn
7d867c6d09
[LoopVersioningLICM] Only mark pointers with generated checks as noalias (#135168)
Currently when we version a loop all loads and stores have the noalias
metadata added to them. If there were some pointers that could not be
analysed, and thus we could not generate runtime aliasing checks for,
then we should not mark loads and stores using these pointers as
noalias.

This is done by getting rid of setNoAliasToLoop and instead using
annotateLoopWithNoAlias, as that already correctly handles partial alias
information. This does result in slightly different aliasing metadata
being generated, but it looks like it's more precise.

Currently this doesn't result in any change to the transforms that
LoopVersioningLICM does, as LoopAccessAnalysis discards all results if
it couldn't analyse every pointer leading to no loop versioning
happening, but an upcoming patch will change that and we need this first
otherwise we incorrectly mark some pointers as noalias even when they
aren't.
2025-05-12 10:15:22 +01:00
Kazu Hirata
1ecba5bd62
[llvm] Use std::tie to implement operator< (NFC) (#139487) 2025-05-11 21:28:47 -07:00
Shan Huang
5971b41919
[DebugInfo][LICM] Salvage dbg_values for the dead instructions to erase (#138796)
fix #138684 .
2025-05-12 09:07:30 +08:00
Shan Huang
86ceed7112
[DebugInfo][LoopDistribute] Salvage debug values using dead instructions in the distributed loops (#137124)
Fix #136532 .
2025-05-12 09:06:42 +08:00
Florian Hahn
7500cead4e
[VPlan] Flatten the CFG separately after creating wide recipes (NFC).
Move flattening of the CFG out of the loop that creates the wide
recipes. This simplifies the already large loop and prepares for moving
flattening to a separate transform.
2025-05-11 21:30:01 +01:00
Alexey Bataev
c870b675db [SLP][NFC]Extract values state/operands analysis into separate class
Extract values state and operands analysis/building into a separate
class. This class allows to localize instrutions state and operands
building for future support of copyable elements vectorization.

Recommit after revert 10f512074fb13ab5da9f49c25965508f51c8452a

Recommit after revert 6a2a8ebe27c1941f5b952313239fc6d155f58e9d

Reviewers: HanKuanChen, RKSimon

Reviewed By: HanKuanChen

Pull Request: https://github.com/llvm/llvm-project/pull/138724
2025-05-11 08:14:05 -07:00
Florian Hahn
9a9a78eacb
[VPlan] Handle most bin-ops in VPReplicateRecipe::computeCost. (NFC)
Directly compute costs for binary ops and GEPs in
VPReplicateRecipe::computeCost. This simply ports the legacy cost
computation for uniform/replicating binary ops to the VPlan cost model.
2025-05-11 13:51:14 +01:00
Florian Hahn
2acecfe653
[VPlan] Use VPBBs to look up masks for newly created recipes (NFC).
Update recipe construction to use VPBBs to look up masks, in preparation
for https://github.com/llvm/llvm-project/pull/128420.
2025-05-11 13:04:33 +01:00
Kazu Hirata
85f3d60d81
[Utils] Use range-based for loops (NFC) (#139426) 2025-05-10 22:50:46 -07:00
Florian Hahn
cfde685e22
[VPlan] Sink VPB2IRBB lookups to VPRecipeBuilder (NFC).
This allows migrating some more code to be based on VPBBs in
VPRecipeBuilder, in preparation for
https://github.com/llvm/llvm-project/pull/128420.
2025-05-10 22:00:58 +01:00
Alex Bradbury
6a2a8ebe27 Revert "[SLP][NFC]Extract values state/operands analysis into separate class"
This reverts commit 512a5d0b8aa82749995204f4852e93757192288a.

It broke RISC-V vector code generation on some inputs (oggenc.c from
llvm-test-suite), as found by our CI. Reduced test case and more
information posted in #138274.
2025-05-10 16:02:47 +01:00
Kazu Hirata
6e4f501b1b
[Utils] Remove redundant calls to std::unique_ptr<T>::get (NFC) (#139352) 2025-05-10 07:27:29 -07:00
Florian Hahn
5fa64d65e9
[VPlan] Use printPhiOperands for VPPhi.
Split off from  https://github.com/llvm/llvm-project/pull/139151 to land
printing improvements separately.

Updates printing of VPPhi operands to be consistent with
VPWidenPHIRecipe.
2025-05-10 12:49:29 +01:00
Florian Hahn
f2e62cfca5
[VPlan] Add VPPhi subclass for VPInstruction with PHI opcodes.(NFC) (#139151)
Similarly to VPInstructionWithType and VPIRPhi, add VPPhi as a subclass
for VPInstruction. This allows implementing the VPPhiAccessors trait,
making available helpers for generic printing of incoming values /
blocks and accessors for incoming blocks and values.

It will also allow properly verifying def-uses for values used by
VPInstructions with PHI opcodes via
https://github.com/llvm/llvm-project/pull/124838.

PR: https://github.com/llvm/llvm-project/pull/139151
2025-05-10 11:08:00 +01:00
Teresa Johnson
8836d68a0d
[MemProf] Optionally discard small non-cold contexts (#139113)
Adds a new option -memprof-callsite-cold-threshold that allows
specifying a percent that will cause non-cold contexts to be discarded
if the percent cold bytes at a callsite including that context exceeds
the given threshold. Default is 100% (no discarding).

This reduces the amount of cloning needed to expose cold allocation
contexts when parts of the context are dominantly cold.

This motivated the change in PR138792, since discarding a context might
require a different decision about which not-cold contexts must be kept
to expose cloning requirements, so we need to determine that on the fly.

Additionally, this required a change to include the context size
information in the alloc trie in more cases, so we now guard the
inclusion of this information in the generated metadata on the option
values.
2025-05-09 15:56:54 -07:00
Matt Arsenault
83551626d0
LoopRotationUtils: Use initializer list instead of repeated push_back (#139312) 2025-05-10 00:21:44 +02:00