33072 Commits

Author SHA1 Message Date
Florian Hahn
1d65d9ce06
[VPlan] Match legacy CM in ::computeCost if load is used by load/store.
If a load is scalarized because it is used by a load/store address, the
legacy cost model does not pass ScalarEvolution to getAddressComputationCost.

Match the behavior in VPReplicateRecipe::computeCost.
2025-10-03 22:01:46 +01:00
ManuelJBrito
be29612ffc
[NewGVN] Remove returned arg simplification (#161865)
Replacing uses of the return value with the argument is already handled
in other passes, additionally it causes issues with memory value
numbering when the call is a memory defining intrinsic.
fixes #159918
2025-10-03 20:52:29 +01:00
Alan Zhao
3b38314a2b
Reapply "[InstCombine] Preserve profile after folding select instructions with conditionals" (#161885) (#161890)
This reverts commit 572b579632fb79ea6eb562a537c9ff1280b3d4f5.

This is a reland of #159666 but with a fix moving the `extern`
declaration of the flag under the LLVM namespace, which is needed to fix
a linker error caused by #161240.
2025-10-03 12:26:09 -07:00
Alexey Bataev
2e67f5ceb8 [SLP][NFC]Add udiv/srem test cases, NFC 2025-10-03 10:49:16 -07:00
Mehdi Amini
572b579632
Revert "[InstCombine] Preserve profile after folding select instructions with conditionals" (#161885)
Reverts llvm/llvm-project#159666

Many bots are broken right now.
2025-10-03 19:37:23 +02:00
Alan Zhao
c54d0d7a96
[InstCombine] Preserve profile after folding select instructions with conditionals (#159666)
If `select` simplification produces the transform:

```
(select A && B, T, F) -> (select A, T, F)
```

or

```
(select A || B, T, F) -> (select A, T, F)
```

it stands to reason that if the branches are the same, then the branch
weights remain the same since the net effect is a simplification of the
conditional.

There are also cases where InstCombine negates the conditional (and
therefore reverses the branches); this PR asserts that the branch
weights are reversed in this case.

Tracking issue: #147390
2025-10-03 10:19:08 -07:00
Ramkumar Ramachandra
36b543ab20
[InstComb] Handle undef in simplifyMasked(Store|Scatter) (#161825) 2025-10-03 16:52:48 +01:00
Florian Hahn
7ceef762c8
[LAA] Check if Ptr can be freed between Assume and CtxI. (#161725)
When using information from dereferenceable assumptions, we need to make
sure that the memory is not freed between the assume and the specified
context instruction. Instead of just checking canBeFreed, check if there
any calls that may free between the assume and the context instruction.

This patch introduces a willNotFreeBetween to check for calls that may
free between an assume and a context instructions, to also be used in
https://github.com/llvm/llvm-project/pull/161255.

PR: https://github.com/llvm/llvm-project/pull/161725
2025-10-03 13:44:58 +00:00
Matthew Devereau
30c578a051
[GVN] Teach GVN simple masked load/store forwarding (#157689)
This patch teaches GVN how to eliminate redundant masked loads and
forward previous loads or instructions with a select. This is possible
when the same mask is used for masked stores/loads that write to the
same memory location
2025-10-03 10:55:06 +01:00
Sander de Smalen
cc9c64d525
[AArch64] Refactor and refine cost-model for partial reductions (#158641)
This cost-model takes into account any type-legalisation that would
happen on vectors such as splitting and promotion. This results in wider
VFs being chosen for loops that can use partial reductions.

The cost-model now also assumes that when SVE is available, the SVE dot
instructions for i16 -> i64 dot products can be used for fixed-length
vectors. In practice this means that loops with non-scalable VFs are
vectorized using partial reductions where they wouldn't before, e.g.

```
  int64_t foo2(int8_t *src1, int8_t *src2, int N) {
    int64_t sum = 0;
    for (int i=0; i<N; ++i)
      sum += (int64_t)src1[i] * (int64_t)src2[i];
    return sum;
  }
```

These changes also fix an issue where previously a partial reduction
would be used for mixed sign/zero-extends (USDOT), even when +i8mm was
not available.
2025-10-03 10:07:07 +01:00
Florian Hahn
2b2bc6320f
[LV] Add tests with multiple F(Max|Min)Num reductions w/o fast-math.
Pre-commits extra test coverage for loops with multiple F(Max|Min)Num
reductions w/o fast-math-flags for follow-up PR.
2025-10-02 21:30:54 +01:00
Brandon
11faf88d8f
[InstCombine] Fold icmp with clamp into unsigned bound check (#161303)
Fix #157315 

alive2: https://alive2.llvm.org/ce/z/TEnuFV

The equality comparison of `min(max(X, Lo), Hi)` and `X` is actually a
range check on `X`. This PR folds this into an unsigned bound check `(X
- Lo) u< (Hi - Lo + 1)`.

---------

Co-authored-by: Yingwei Zheng <dtcxzyw@qq.com>
2025-10-02 21:51:39 +02:00
Florian Hahn
9133fc8cb0
[LAA,LV] Add early-exit tests with deref assumes and nofree via context.
Add tests with early exits and dereferenceable assumptions that need
proving no-free via the context.
2025-10-02 19:24:56 +01:00
Yingwei Zheng
0b7129afcc
[InstCombine] Fix FMF propagation in foldFCmpFSubIntoFCmp (#161539)
Proof: https://alive2.llvm.org/ce/z/orSP-S
Closes https://github.com/llvm/llvm-project/issues/161525.
2025-10-03 01:44:03 +08:00
Joel E. Denny
afb262855e
[LoopPeel] Fix branch weights' effect on block frequencies (#128785)
[LoopPeel] Fix branch weights' effect on block frequencies

This patch implements the LoopPeel changes discussed in [[RFC] Fix Loop
Transformations to Preserve Block
Frequencies](https://discourse.llvm.org/t/rfc-fix-loop-transformations-to-preserve-block-frequencies/85785).

In summary, a loop's latch block can have branch weight metadata that
encodes an estimated trip count that is derived from application profile
data. Initially, the loop body's block frequencies agree with the
estimated trip count, as expected. However, sometimes loop
transformations adjust those branch weights in a way that correctly
maintains the estimated trip count but that corrupts the block
frequencies. This patch addresses that problem in LoopPeel, which it
changes to:

- Maintain branch weights consistently with the original loop for the
sake of preserving the total frequency of the original loop body.
- Store the new estimated trip count in the
`llvm.loop.estimated_trip_count` metadata, introduced by PR #148758.
2025-10-02 16:07:55 +00:00
Florian Hahn
f646d766de
[LV] Extend test coverage for tail-folding with recurrences.
Add additional test coverage for tail-folding loops with first-order
recurrences and users outside the loop. Test a combination of
vectorization factors and interleave counts.

Also update check lines in reduction-order.ll and adjust naming for
clarity.

This adds extra test coverage for
https://github.com/llvm/llvm-project/pull/149042.
2025-10-02 16:27:16 +01:00
Nikita Popov
db39ef9d56 [GVN] Add additional tests for inverted condition propagation (NFC) 2025-10-02 14:35:14 +02:00
Florian Hahn
7c4f188f27
[LV] Support multiplies by constants when forming scaled reductions. (#161092)
We can create partial reductions for multiplies with constants, if the
constant is small enough to be extended from source to destination type
w/o changing the value.

This only handles constant on the right side of a multiply, relying on
other passes to canonicalize the input.

Alive2 Proofs: https://alive2.llvm.org/ce/z/iWRMr6

PR: https://github.com/llvm/llvm-project/pull/161092
2025-10-02 10:53:17 +00:00
Florian Hahn
1a850279c5
[LV] Re-compute cost of scalarized load users.
If there are direct memory op users of the newly scalarized load,
their cost may have changed because there's no scalarization
overhead for the operand. Update it.

This ensures assigning consistent costs to scalarized memory
instructions that themselves have scalarized memory instructions as
operands.
2025-10-01 22:30:18 +01:00
Mikhail Gudim
15dc80fda7
[SLPVectorizer][NFC] A test for widening constant strided loads. (#160552)
Precommit a test.
2025-10-01 16:09:45 -04:00
Alexey Bader
2d6e7ef567
[LV] Add additional tests for replicating load/store costs.
Includes test for https://github.com/llvm/llvm-project/issues/161404
2025-10-01 19:15:19 +01:00
Yingwei Zheng
57a9f79336
[ValueTracking] Take PHI's poison-generating flags into account (#161530)
ninf/nnan in the phi node may produce poison values. They should be
considered in `isGuaranteedNotToBeUndefOrPoison`.
Closes https://github.com/llvm/llvm-project/issues/161524.
2025-10-02 01:11:51 +08:00
Florian Hahn
8907b6d393
[VPlan] Remove original loop blocks if dead. (#155497)
Build on top of https://github.com/llvm/llvm-project/pull/154510 to
completely remove the blocks of dead scalar loops.

Depends on https://github.com/llvm/llvm-project/pull/154510. 

PR: https://github.com/llvm/llvm-project/pull/155497
2025-10-01 16:53:59 +00:00
Ramkumar Ramachandra
bb16c56019
[LoopIdiom] Fix a DL-related crash in optimizeCRCLoop (#161509) 2025-10-01 17:14:25 +01:00
Sam Tebbs
664b227089
[LV] Keep duplicate recipes in VPExpressionRecipe (#156976)
The VPExpressionRecipe class uses a set to store its bundled recipes. If
repeated recipes are bundled then the duplicates will be lost, causing
the following recipes to not be at the expected place in the set.

When printing a reduce.add(mul(ext, ext)) bundle, for example, if the
extends are the same then the 3rd element of the set will be the
reduction, rather than the expected mul, causing a cast error. With this
change, the recipes are at the expected index in the set.

Fixes #156464
2025-10-01 16:01:54 +01:00
Paul Walker
9e0c0a0939
[LLVM][SCEV] udiv (mul nuw a, vscale), (mul nuw b, vscale) -> udiv a, b (#157836) 2025-10-01 15:46:12 +01:00
Hongyu Chen
a05e004b28
[DFAJumpThreading] Unfold select to the incoming block of phi user (#160987)
Fixes #160250
We previously assumed the select to unfold is defined in the incoming
block of phi user, as `isValidSelectInst` filters other cases at the
initial stage. However, the selects not defined in the incoming block
may occur after unfolding the arms of the unfolded select.
This patch sinks the select into the incoming block of the phi user and
unfolds it at the incoming block.
2025-10-01 14:06:08 +00:00
Yingwei Zheng
73d9974c91
[InstCombine] Avoid self-replacing in getUndefReplacement (#161500)
Self-replacing has a different meaning in InstCombine. It will replace
all uses with poison.
Closes https://github.com/llvm/llvm-project/issues/161492.
2025-10-01 22:02:30 +08:00
Yingwei Zheng
c09054866a
[InstCombine] Drop poison-generating flags when reusing existing or instruction (#161504)
Closes https://github.com/llvm/llvm-project/issues/161493.
2025-10-01 22:02:08 +08:00
Hongyu Chen
f80e7e139e
[GlobalOpt] Check if users are CallBase when changing CC (#161399)
Fixes https://github.com/llvm/llvm-project/issues/156656
`hasChangeableCCImpl` guarantees the address of the function is not
taken, but it ignores assume-like calls.
This patch ignores assume-like calls when changing CC.
2025-10-01 08:47:41 +00:00
mikael-nilsson-arm
69586331e8
[InstCombine] Opt phi(freeze(undef), C) -> phi(C, C) (#161181)
Try to choose a value for freeze that enables the PHI to be replaced
with its input constants if they are equal.
2025-10-01 10:13:42 +02:00
Nikita Popov
63ca8483d0
[IR] Introduce !captures metadata (#160913)
This introduces `!captures` metadata on stores, which looks like this:

```
store ptr %x, ptr %y, !captures !{!"address", !"read_provenance"}
```

The semantics are the same as replacing the store with a call like this:
```
call void @llvm.store(ptr captures(address, read_provenance) %x, ptr %y)
```

This metadata is intended for annotation by frontends -- it's not
something we can feasibly infer at this point, as it would require
analyzing uses of the pointer stored in memory.

The motivating use case for this is Rust's `println!()` machinery, which
involves storing a reference to the value inside a structure. This means
that printing code (including conditional debugging code), can inhibit
optimizations because the pointer escapes. With the new metadata we can
annotate this as a read-only capture, which has less impact on
optimizations.
2025-10-01 08:58:47 +02:00
Yingwei Zheng
d62776d033
[LVI] Handle constant value lattice in getEdgeValueLocal (#161410)
Closes https://github.com/llvm/llvm-project/issues/161367.

In https://github.com/llvm/llvm-project/pull/157614, we ignored cases
where OpLatticeVal might be a constant or notconstant. Directly
returning the result causes a type mismatch. I apologize for the
oversight in the previous code review.

This patch applies the cast op to constants. For notconstant value
lattices, I'd leave it as a todo (it is similar to the constant case,
except for trunc without nsw/nuw).
2025-10-01 14:20:10 +08:00
Florian Mayer
1c11f72344
[NFC] [IndVarSimplify] add overflowing tests (#159877)
Also use UTC for test instead.
2025-09-30 15:19:53 -07:00
Mikhail Gudim
e485d5e77a
[SLPVectorizer] Clear TreeEntryToStridedPtrInfoMap. (#160544)
We need to clear `TreeEntryToStridedPtrInfoMap` in `deleteTree`.
2025-09-30 09:25:32 -04:00
Mikhail Gudim
5e4eb334af
[SLPVectorizer] Remove align 16 in a test. (#161251)
It is not necessary.
2025-09-30 09:23:16 -04:00
Ramkumar Ramachandra
2f7252a841
[LV] Preserve GEP nusw when widening memory (#160885) 2025-09-30 10:42:45 +00:00
Florian Hahn
45ce88758d
[LV] Don't preserve LCSSA in SCEVExpander for runtime checks. (#159556)
LV does not preserve LCSSA, it constructs it just before processing a
loop to vectorize. Runtime check expressions are invariant to that loop,
so expanding them should not break LCSSA form for the loop we are about
to vectorize.

This fixes a crash when discarding instructions generated when expanding
runtime checks, if the expansion introduces LCSSA phis for values from
other loops which are not in LCSSA form: we would introduce new LCSSA
phis and update all outside users, some of which are not created by the
expander and cannot be cleaned up.

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

PR: https://github.com/llvm/llvm-project/pull/159556
2025-09-30 10:03:55 +00:00
Sam Tebbs
88658dbbc5
[LV] Add ExtNegatedMulAccReduction expression type (#160154)
This PR adds the ExtNegatedMulAccReduction expression type for
VPExpressionRecipe so that extend-multiply-accumulate reductions with a
negated multiply can be bundled.

Stacked PRs:

1. https://github.com/llvm/llvm-project/pull/156976
2. -> https://github.com/llvm/llvm-project/pull/160154
3. https://github.com/llvm/llvm-project/pull/147302
2025-09-30 10:10:37 +01:00
Hongyu Chen
87bc0f7431
[VectorCombine] Preserve cast flags in foldBitOpOfCastConstant (#161237)
Follow-up of #157822.
2025-09-30 16:38:03 +08:00
Jin Huang
39f292ffa1
[profcheck] Add unknown branch weight for inlined memchr calls. (#160964)
The memchr inliner creates new switch branches but was failling to add
profile metada. This patch fixes the issue by explicitly adding unknown
branch weights to these branches.

Issue [#147390](https://github.com/llvm/llvm-project/issues/147390)
2025-09-30 00:24:50 +00:00
Alan Zhao
045e09f22b
[InstCombine] Set !prof metadata on Selects identified by add.ll test (#158743)
These select instructions are created from non-branching instructions,
so their branch weights are unknown.

Tracking issue: #147390
2025-09-29 20:37:06 +00:00
Florian Hahn
8dde784135
[LV] Add test for more precise no-free checks w/o nosync attribute.
Also filter out uninteresting parts from check lines.
2025-09-29 19:17:48 +01:00
Alexey Bataev
1f82553e38 [SLP]Fix mixing xor instructions in the same opcode analysis
Xor with 0 operand should not be compatible with multiplications-based
instructions, only with or/xor/add/sub.

Fixes #161140
2025-09-29 11:14:06 -07:00
Alexey Bataev
0457644dfb [SLP][NFC]Add a test with the incorrect combination of Xor/Mul vector instructions, NFC 2025-09-29 10:37:39 -07:00
Florian Hahn
b51b967671
[LV] Add test for more precise no-free checks. 2025-09-29 17:46:22 +01:00
Alina Sbirlea
dd3507b6c0
Extend LVI to cache ranges per BB predecessor. (#159432)
Currently LVI does the union of value ranges from block predecessors.
When storing the ranges per predecessor, the resulting ranges may be
more restricted and enable additional optimizations.
However this is costly (memory + compile time), so place this under a
flag disabled by default.

See: https://github.com/llvm/llvm-project/issues/158139.
2025-09-29 08:58:46 -07:00
Chaitanya Koparkar
766c90f439
[VectorCombine] foldShuffleOfCastops - handle unary shuffles (#160009)
Fixes #156853.
2025-09-29 14:21:59 +01:00
Antonio Frighetto
5ff9f7b886
[SimplifyCFG] Ensure selects have not been constant folded in foldSwitchToSelect
Make sure selects do exist prior to assigning weights to edges.

Fixes: https://github.com/llvm/llvm-project/issues/161137.
2025-09-29 14:36:56 +02:00
Leon Clark
8df643f663
[VectorCombine] Fix rotation in phi narrowing. (#160465)
Fix bug in #140188 where incoming vectors are rotated in the wrong
direction.

Co-authored-by: Leon Clark <leoclark@amd.com>
2025-09-29 13:26:35 +01:00