719 Commits

Author SHA1 Message Date
Yingwei Zheng
892b4beeac
[GVN] Drop nsw/nuw flags when replacing the result of a with.overflow intrinsic with a overflowing binary operator (#82935)
Alive2: https://alive2.llvm.org/ce/z/gyL7mn
Fixes https://github.com/llvm/llvm-project/issues/82884.
2024-02-26 15:55:56 +08:00
Nikita Popov
2d69827c5c [Transforms] Convert tests to opaque pointers (NFC) 2024-02-05 11:57:34 +01:00
Nikita Popov
383e35048e [CaptureTracking] Treat vector GEPs as captures
Because AA does not support vectors of pointers, we have to
treat pointers that are inserted into a vector as captures. We
mostly already do so, but missed the case where getelementptr
is used to produce a vector.
2023-12-05 10:09:52 +01:00
Nikita Popov
292ecb8b81 [GVN] Add test for capture via vector GEP (NFC)
Reported at:
https://github.com/llvm/llvm-project/pull/69931#issuecomment-1839475854
2023-12-05 10:09:52 +01:00
Nikita Popov
9a09c737a0
[BasicAA] Make isNotCapturedBeforeOrAt() check for calls more precise (#69931)
For calls, we are only interested in captures before the call, not
captures by the call itself -- arguments that get passed to the call are
checked explicitly.

In particular, the current implementation is not optimal if the pointer
is captured via a readonly argument -- in that case, we know that even
if the argument is captured, the call will not modify the argument (at
least not via that argument).

Make this more precise by renaming to isCapturedBefore() and adding an
OrAt argument that allows us to toggle whether to consider captures in
the instruction itself or not.
2023-11-21 09:28:08 +01:00
Harvin Iriawan
211dc4ad40
[Analysis] Add Scalable field in MemoryLocation.h (#69716)
This is the first of a series of patch to improve Alias Analysis on
  Scalable quantities.
  Keep Scalable information from TypeSize which
  will be used in Alias Analysis.
2023-10-24 18:18:51 +01:00
Nikita Popov
178270b7be [GVN] Add additional captured before tests (NFC) 2023-10-23 16:45:14 +02:00
Nikita Popov
2ad9fde418
[MemDep] Use EarliestEscapeInfo (#69727)
Use BatchAA with EarliestEscapeInfo instead of callCapturesBefore() in
MemDepAnalysis. The advantage of this is that it will also take
not-captured-before information into account for non-calls (see
test_store_before_capture for a representative example), and that this
is a cached analysis. The disadvantage is that EII is slightly less
precise than full CapturedBefore analysis.

In practice the impact is positive, with gvn.NumGVNLoad going from 22022
to 22808 on test-suite. 

The impact to compile-time is also positive, mainly in the ThinLTO
configuration.
2023-10-23 09:57:26 +02:00
Nikita Popov
97e06a0d83 [GVN] Add tests for captured-before analysis (NFC) 2023-10-20 15:18:57 +02:00
Nikita Popov
7f1733a252
[GVN] Fix use-after-free in load PRE with select available value (#69314)
replaceValuesPerBlockEntry() only handled simple and coerced load
values, however the load may also be referenced by a select value.

Additionally, I suspect that the previous code might have been incorrect
if a load had an offset, as it always constructed the AvailableValue
from scratch.

Fixes https://github.com/llvm/llvm-project/issues/69301.
2023-10-19 09:08:59 +02:00
Sergey Kachkov
9f12f655c4
[GVN] Drop Clobber dependency if store may overwrite only the same value (#68322)
In some cases clobbering store can be safely skipped if it can only must
or no alias with memory location and it writes the same value. This
patch supports simple case when the value from memory location was
loaded in the same basic block before the store and there are no
modifications between them.
2023-10-10 19:22:00 +03:00
Sergey Kachkov
9ce8103e87 [GVN][NFC] Add pre-commit test 2023-10-10 19:07:38 +03:00
Nikita Popov
46aac949bc [GVN] Remove users from ICF when RAUWing loads
When performing store to load forwarding, replacing users of the
load may turn an indirect call into one with a known callee, in
which case it might become willreturn, invalidating cached ICF
information. Avoid this by removing users.

This is a bit more aggressive than strictly necessary (e.g. this
shouldn't be necessary when doing load-load CSE), but better safe
than sorry.

Fixes https://github.com/llvm/llvm-project/issues/48805.
2023-10-05 11:21:33 +02:00
Nikita Popov
18e77760ce [GVN] Also remove phi nodes from VN table (PR65447)
Followup to D158849: We also need to remove the phi node from the
VN table, which is not handled by removeInstruction().

Fixes https://github.com/llvm/llvm-project/issues/65447.
2023-09-15 11:50:34 +02:00
Nikita Popov
7c229f6e85 [GVN] Invalidate MDA when deduplicating phi nodes
Duplicate phi nodes were being directly removed, without
invalidating MDA. This could result in a new phi node being
allocated at the same address, incorrectly reusing a cache entry.

Fix this by optionally allowing EliminateDuplicatePHINodes() to
collect phi nodes to remove into a vector, which allows GVN to
handle removal itself.

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

Differential Revision: https://reviews.llvm.org/D158849
2023-09-15 07:04:32 +02:00
Paul Walker
c7d65e4466 [IR] Enable load/store/alloca for arrays of scalable vectors.
Differential Revision: https://reviews.llvm.org/D158517
2023-09-14 13:49:01 +00:00
Sergey Kachkov
4b14148d24
[GVN] Skip debug instructions in findDominatingValue function (#65977)
findDominatingValue has a search limit, and when it is reached, optimization is
not applied. This patch fixes the issue that this limit also takes into account
debug intrinsics, so the result of optimization can depend from the presence of
debug info.
2023-09-13 11:23:26 +03:00
Nikita Popov
edb2fc6dab [llvm] Remove explicit -opaque-pointers flag from tests (NFC)
Opaque pointers mode is enabled by default, no need to explicitly
enable it.
2023-07-12 14:35:55 +02:00
ManuelJBrito
1d70ad5d0b [GVN] Add tests for PR63059 (NFC)
Besides the example from the issue, this adds some other related wrong transformations.

Differential Revision: https://reviews.llvm.org/D154103
2023-06-30 15:52:40 +01:00
Arthur Eubanks
1876592ce3 [test] Regenerate some tests 2023-06-27 16:53:50 -07:00
Jay Foad
18f5446a45 [GVN] Fix typo in freeze test case
Differential Revision: https://reviews.llvm.org/D152539
2023-06-09 15:41:32 +01:00
Guozhi Wei
84bcfa0e1b [GVN] Improve PRE on load instructions
This patch implements the enhancement proposed by
https://github.com/llvm/llvm-project/issues/59312.

Suppose we have following code

   v0 = load %addr
   br %LoadBB

LoadBB:
   v1 = load %addr
   ...

PredBB:
   ...
   br %cond, label %LoadBB, label %SuccBB

SuccBB:
   v2 = load %addr
   ...

Instruction v1 in LoadBB is partially redundant, edge (PredBB, LoadBB) is a
critical edge. SuccBB is another successor of PredBB, it contains another load
v2 which is identical to v1. Current GVN splits the critical edge
(PredBB, LoadBB) and inserts a new load in it. A better method is move the load
of v2 into PredBB, then v1 can be changed to a PHI instruction.

If there are two or more similar predecessors, like the test case in the bug
entry, current GVN simply gives up because otherwise it needs to split multiple
critical edges. But we can move all loads in successor blocks into predecessors.

Differential Revision: https://reviews.llvm.org/D141712
2023-06-06 19:45:34 +00:00
Nikita Popov
97f0e7b06e [AA] Fix comparison of AliasResults (PR63019)
Comparison between two AliasResults implicitly decayed to comparison
of AliasResult::Kind. As a result, MergeAliasResults() ended up
considering two PartialAlias results with different offsets as
equivalent.

Fix this by adding an operator== implementation. To stay
compatible with extensive use of comparisons between AliasResult
and AliasResult::Kind, add an overload for that as well, which
will ignore the offset. In the future, it would probably be a
good idea to remove these implicit decays to AliasResult::Kind
and add dedicated methods to check for specific AliasResult kinds.

Fixes https://github.com/llvm/llvm-project/issues/63019.
2023-05-31 15:03:41 +02:00
Nikita Popov
4d64ffa941 [GVN] Add test for PR63019 (NFC) 2023-05-31 14:55:25 +02:00
Jay Foad
c86a1e6903 [GVN] Do not combine convergent calls in GVN/NewGVN
Note that this is very conservative since it will not even combine
convergent calls that appear in the same basic block, but EarlyCSE will
handle that case.

Differential Revision: https://reviews.llvm.org/D150974
2023-05-19 21:27:35 +01:00
Jay Foad
41a2c66aee [GVN] Precommit tests for convergent calls in GVN/NewGVN 2023-05-19 16:39:53 +01:00
Jay Foad
7084fb1e28 [GVN] Fix test case for convergent calls
Without "willreturn" this did not get as far as testing the convergent
handling in GVN.
2023-05-19 16:15:06 +01:00
Guozhi Wei
a3fbe5f7e6 Revert "[GVN] Improve PRE on load instructions"
This reverts commit d6811826371d82de074d0c3e10040114eee69897.

It caused sanitized bootstrap failure.
2023-05-17 07:38:22 +00:00
Guozhi Wei
d681182637 [GVN] Improve PRE on load instructions
This patch implements the enhancement proposed by
https://github.com/llvm/llvm-project/issues/59312.

Suppose we have following code

     v0 = load %addr
     br %LoadBB

  LoadBB:
     v1 = load %addr
     ...

  PredBB:
     ...
     br %cond, label %LoadBB, label %SuccBB

  SuccBB:
     v2 = load %addr
     ...

Instruction v1 in LoadBB is partially redundant, edge (PredBB, LoadBB) is a
critical edge. SuccBB is another successor of PredBB, it contains another load
v2 which is identical to v1. Current GVN splits the critical edge
(PredBB, LoadBB) and inserts a new load in it. A better method is move the load
of v2 into PredBB, then v1 can be changed to a PHI instruction.

If there are two or more similar predecessors, like the test case in the bug
entry, current GVN simply gives up because otherwise it needs to split multiple
critical edges. But we can move all loads in successor blocks into predecessors.

Differential Revision: https://reviews.llvm.org/D141712
2023-05-17 01:09:01 +00:00
Mingming Liu
b3cb950cf3 [PGO]Implement metadata combine for 'branch_weights' of direct
callsites when none of the instructions folds the rest away.

- Merge cases are added for simplify-cfg {sink,hoist}, based on https://gcc.godbolt.org/z/avGvc38W7 and https://gcc.godbolt.org/z/dbWbjGhaE
- When one instruction folds the others in, do not update branch_weights
  with sum (see test/Transforms/GVN/calls-readonly.ll)

Differential Revision: https://reviews.llvm.org/D148877
2023-04-27 13:04:17 -07:00
Nikita Popov
20e9b31f88 [GVN] Fix metadata combining for non-local loads
Make MaterializeAdjustedValue() responsible for adjusting load
metadata in all cases, so it also covers the non-local case.

In conjunction with that, we no longer need to call
patchReplacementInstruction() for the local case, which would
unnecessarily drop metadata if the replacement value just happened
to be a load (without actual load CSE).
2023-04-20 12:32:10 +02:00
Nikita Popov
998e823613 [GVN] Add additional metadata combining tests (NFC) 2023-04-20 12:17:53 +02:00
Nikita Popov
8cdca96690 [GVN] Adjust metadata for coerced load CSE
When reusing a load in a way that requires coercion (i.e. casts or
bit extraction) we currently fail to adjust metadata. Unfortunately,
none of our existing tooling for this is really suitable, because
combineMetadataForCSE() expects both loads to have the same type.
In this case we may work on loads of different types and possibly
offset memory location.

As such, what this patch does is to simply drop all metadata, with
the following exceptions:

* Metadata for which violation is known to always cause UB.
* If the load is !noundef, keep all metadata, as this will turn
  poison-generating metadata into UB as well.

This fixes the miscompile that was exposed by D146629.

Differential Revision: https://reviews.llvm.org/D148129
2023-04-17 12:52:31 +02:00
Nikita Popov
e4251fc6bb [LangRef][Local] dereferenceable metadata violation is UB
I believe !dereferencable violation is immediate undefined behavior,
but this was not explicitly spelled out in LangRef. We already
assume that !dereferenceable is implicitly !noundef and cannot
return poison in isGuaranteedNotToBeUndefOrPoison().

The reason why we made dereferenceable implicitly noundef is that
the purpose of this metadata is to allow speculation, and that
would not be legal on a potential poison pointer.

Differential Revision: https://reviews.llvm.org/D148202
2023-04-14 10:54:01 +02:00
Nikita Popov
1c656df581 [GVN][SimplifyCFG] Add tests for dereferenceable metadata combining (NFC) 2023-04-13 10:55:34 +02:00
Nikita Popov
6e56cdac30 [GVN] Regenerate test checks (NFC) 2023-04-12 16:16:29 +02:00
Nikita Popov
6e6dbae3ed [GVN] Add additional metadata adjustment tests (NFC) 2023-04-12 15:46:25 +02:00
Nikita Popov
e7f4ad13ae [Transforms] Convert some tests to opaque pointers (NFC) 2023-04-11 16:49:12 +02:00
Nikita Popov
d68800d15d [Local] Preserve !invariant.load of dominating instruction
Per LangRef:

> If a load instruction tagged with the !invariant.load metadata
> is executed, the memory location referenced by the load has to
> contain the same value at all points in the program where the
> memory location is dereferenceable; otherwise, the behavior is
> undefined.

As invariant.load violation is immediate undefined behavior, it
is sufficient for it to be present on the dominating load (for
the case where K does not move).
2023-04-03 16:05:02 +02:00
Nikita Popov
d527ace7d9 [GVN] Regenerate test checks (NFC)
And add an additional metadata preservation test.
2023-04-03 16:05:02 +02:00
luxufan
f44c7dec67 [Local] Use most generic range if K does not dominate J or K doesn't have a !noundef
Since D141386 has changed the return value of !range from IUB to poison,
metadata !range shouldn't be preserved even if K dominates J.

If this patch was accepted, I plan to adjust metadata !nonnull as well.
BTW, I found that metadata !noundef is not handled in combineMetadata,
is this intentional?

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142687
2023-03-23 19:31:55 +08:00
Nikita Popov
e7618a6361 [GVN] Fix change reporting when removing assume (PR61574)
Report a change when removing a true/false assume.

Fixes https://github.com/llvm/llvm-project/issues/61574.
2023-03-22 15:23:31 +01:00
Nikita Popov
1a9d49524a [GVN] Regenerate test checks (NFC) 2023-03-22 11:40:07 +01:00
Nikita Popov
475f30dfc8 [ConstantFolding] Preserve inbounds when casting GEP indices
This canonicalization just makes the implicit sext/trunc explicit,
and does not affect the inbounds-ness of the GEP.
2023-03-09 13:08:19 +01:00
Sergey Kachkov
1aece0e5ed Revert "[GVN] Support address translation through select instructions"
This reverts commit b5bf6f6392a3408be1b7b7e036eb69358c5a2c29.
2023-02-27 12:57:59 +03:00
Sergey Kachkov
b5bf6f6392 [GVN] Support address translation through select instructions
Process cases when phi incoming in predecessor block has select
instruction, and this select address is unavailable, but there
are addresses translated from both sides of select instruction.

Differential Revision: https://reviews.llvm.org/D142705
2023-02-27 12:08:15 +03:00
Daniel Woodworth
a33f018b89 [Local][SimplifyCFG][GVN] Handle !nontemporal in combineMetadata
SimplifyCFG currently drops !nontemporal metadata when sinking
common instructions. With this change, SimplifyCFG and similar
transforms will preserve !nontemporal metadata as long as it is
set on both original instructions.

Differential Revision: https://reviews.llvm.org/D144298
2023-02-22 14:47:00 +01:00
Nick Desaulniers
c062e97acf [llvm][test] restrict 2 GVN tests to just test GVN (NFC)
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D143681
2023-02-14 13:30:23 -08:00
Sergey Kachkov
203cc665cf [PHITransAddr] Simplify casts in PHITransAddr
Try to simplify cast in similar way as for GEP and ADD with
constant (e.g. sext/zext + trunc).

Differential Revision: https://reviews.llvm.org/D143167
2023-02-07 12:43:19 +03:00
Sergey Kachkov
2c8362b2a2 [NFC] Add pre-commit test for simplifyCastInst in PHITransAddr 2023-02-07 12:43:19 +03:00