364 Commits

Author SHA1 Message Date
Antonio Frighetto
6ae4fcfd4c [SCCP] Extend visitBinaryOperator to overflowing binary ops
Leverage more refined ranges results when handling overflowing
binary operators.
2024-03-14 16:02:29 +01:00
Yingwei Zheng
d9e92765c5
[ConstantRange] Improve ConstantRange::binaryXor (#80146)
`ConstantRange::binaryXor` gives poor results as it currently depends on
`KnownBits::operator^`.
Since `sub A, B` is canonicalized into `xor A, B` if `B` is the subset
of `A`, this patch reverts the transform in `ConstantRange::binaryXor`,
which will give better results.

Alive2: https://alive2.llvm.org/ce/z/bmTMV9
Fixes #79696.
2024-02-08 22:34:52 +08:00
Yingwei Zheng
d218092543
[SCCP] Check whether the default case is reachable (#76295)
This patch eliminates unreachable default cases using range information.
Fixes #76085.
2024-01-08 20:08:42 +08:00
Florian Hahn
b46638dc76
[Local] Handle undef FP constant in getExpressionForConstant.
Check for FP constant instead of checking for floating point types, as
Undef/Poison values can have floating point types while not being
FPConstants.

This fixes a crash introduced by #66745 (f3b20cb).
2023-12-31 13:42:47 +00:00
Jeremy Morse
d2d9dc8eb4
[DebugInfo][RemoveDIs] Make debugify pass convert to/from RemoveDIs mode (#73251)
Debugify is extremely useful as a testing and debugging tool, and a good
number of LLVM-IR transform tests use it. We need it to support "new"
non-instruction debug-info to get test coverage, but it's not important
enough to completely convert right now (and it'd be a large
undertaking). Thus: convert to/from dbg.value/DPValue mode on entry and
exit of the pass, which gives us the functionality without any further
work. The cost is compile-time, but again this is only happening during
tests.

Tested by: the large set of debugify tests enabled here. Note the
InstCombine test (cast-mul-select.ll) that hasn't been fully enabled:
this is because there's a debug-info sinking piece of code there that
hasn't been instrumented.
2023-11-29 13:19:50 +00:00
Jeremy Morse
f42482def2
[DebugInfo][RemoveDIs] Don't convert debug-intrinsics to Unreachable (#72380)
It might seem obvious, but it's not a good idea to convert a
debug-intrinsic instruction into an UnreachableInst, as this means
things operate differently with and without the -g option. However this
can happen due to the "mutate the next instruction" API calls we make.
With RemoveDIs eliminating debug intrinsics, this behaviour is at risk
of changing, hence this patch ensures we only ever mutate the next _non_
debuginfo instruction into an Unreachable.

The tests instrumented with the --try... flag all exercise this, I've
added some metadata to a SCCP test to ensure it's exercised.
2023-11-20 20:53:24 +00:00
Yingwei Zheng
c3b9c36f3a
[SCCP] Propagate exact flags (#72432)
This patch propagates exact flags for `ashr->lshr` and `sdiv->udiv` in
SCCP.

This missed optimization is discovered with the help of
https://github.com/AliveToolkit/alive2/pull/962.
2023-11-16 14:05:28 +08:00
Yingwei Zheng
ed96430402
[SCCP] Infer nneg on existing zext (#72143)
This patch infers `nneg` flags for existing zext instructions in SCCP.

Similar patch: https://github.com/llvm/llvm-project/pull/72052
2023-11-14 10:18:23 +08:00
Nikita Popov
e4a4122eb6
[IR] Remove zext and sext constant expressions (#71040)
Remove support for zext and sext constant expressions. All places
creating them have been removed beforehand, so this just removes the
APIs and uses of these constant expressions in tests.

There is some additional cleanup that can be done on top of this, e.g.
we can remove the ZExtInst vs ZExtOperator footgun.

This is part of
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179.
2023-11-03 10:46:07 +01:00
Craig Topper
b1c59b516c
[SCCP] Infer nneg on zext when forming from non-negative sext. (#70730)
Builds on #67982 which recently introduced the nneg flag on a zext
instruction.
2023-10-30 15:07:22 -07:00
Carlos Alberto Enciso
f3b20cb16a
[IPSCCP] Variable not visible at Og. (#66745)
https://bugs.llvm.org/show_bug.cgi?id=51559
https://github.com/llvm/llvm-project/issues/50901

IPSCCP pass removes the global variable and does not create a constant
expression for the initializer value.
2023-10-24 06:22:18 +01:00
Nikita Popov
4dd392fcd7 [ConstantRange] Make shl() for negative LHS more precise
This differs from the positive case in that shifting by a larger
amount makes the result smaller, not larger.
2023-08-29 14:48:33 +02:00
Zhongyunde
915439f593 [tests] precommit tests for D156845
Reviewed By: arsenm
Differential Revision: https://reviews.llvm.org/D156865
2023-08-24 18:49:06 +08:00
Rahul Anand Radhakrishnan
18423c7e1f [SCCP] Do not attempt to create constexpr for a scalable vector GEP
Scalable vector GEPs are not constants and trying to create one for
these GEPs causes an assertion failure.

Reviewed By: nikic, paulwalker-arm

Differential Revision: https://reviews.llvm.org/D157590
2023-08-11 11:06:07 +00:00
luxufan
03dec91794 [NFC][SCCP] Regenerate test case 2023-08-07 22:06:27 +08: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
Nikita Popov
664b7a4cd5 [SCCP] Fix conversion of range to constant for vectors (PR63380)
The ConstantRange specifies the range of the scalar elements in the
vector. When converting into a Constant, we need to create a vector
splat with the correct type. For that purpose, pass in the expected
type for the constant.

Fixes https://github.com/llvm/llvm-project/issues/63380.
2023-06-19 12:29:44 +02:00
luxufan
18eb31c096 [SCCP][NFC] Regenerate test case 2023-06-19 14:15:01 +08:00
luxufan
22224acec8 [SCCP][NFC] Regenerate test case 2023-06-19 14:08:33 +08:00
luxufan
f2d9f84b3f [SCCP][NFC] Regenerate test case 2023-06-18 23:14:20 +08:00
Vitaly Buka
23ea58f374 Revert "[SCCP] Replace new value's value state with removed value's"
Breaks all sanitizers bootstrap bots:
https://lab.llvm.org/buildbot/#/waterfall?tags=sanitizer

This reverts commit cf79773a9006a7e22f3919268b7db381ddcb3abc.
2023-06-12 11:07:46 -07:00
luxufan
cf79773a90 [SCCP] Replace new value's value state with removed value's
In replaceSignedInst, if a signed instruction can be repalced with
unsigned instruction, we created a new instruction and removed the old
instruction's value state. If the following instructions has this new
instruction as a use operand, transformations like replaceSignedInst and
refineInstruction would be blocked. The reason is there is no value
state for the new instrution.

This patch set the new instruction's value state with the removed
instruction's value state. I believe it is correct bacause when we
repalce a signed instruction with unsigned instruction, the value state
is not changed.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D152337
2023-06-12 11:40:47 +08:00
luxufan
25d9fde22e [SCCP] Skip computing intrinsics if one of its args is unknownOrUndef
For constant range supported intrinsics, we got consantrange from args
no matter if they are unknown or undef. And the constant range computed
from unknown or undef value state is full range.

I think compute with full constant range is harmful since although we
can do mergeIn after these args value state are changed, the merge
operation of two constant ranges is union.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D152499
2023-06-10 15:48:46 +08:00
luxufan
3abdd40129 [SCCP][NFC] Precommit test 2023-06-10 15:37:09 +08:00
Alexandros Lamprineas
475ddca56e Reland "[FuncSpec] Replace LoopInfo with BlockFrequencyInfo"
Using AvgLoopIters on any loop is too imprecise making the cost model
favor users inside loop nests regardless of the actual tripcount.

Differential Revision: https://reviews.llvm.org/D150375
2023-06-08 17:44:47 +01:00
Mikhail Gudim
d37d4072f2 Reapply [SCCP] Constant propagation through freeze instruction
Reapply with extra check for struct types, which caused buildbot
failures last time.

-----

The freeze instruction has not been handled by SCCPInstVisitor.
This patch adds SCCPInstVisitor::visitFreezeInst(FreezeInst &I)
method to handle freeze instructions.

Differential Revision: https://reviews.llvm.org/D151659
2023-06-05 11:47:36 +02:00
Alexandros Lamprineas
b1f41685a6 [IPSCCP] Decouple queries for function analysis results.
The SCCPSolver is using a structure (AnalysisResultsForFn) where it keeps
pointers to various analyses needed by the IPSCCP pass. These analyses are
requested all at the same time, which can become problematic in some cases.
For example one could be retrieved via getCachedAnalysis() prior to the
actual execution of the analysis. In more detail:

The IPSCCP pass uses a DomTreeUpdater to preserve the PostDominatorTree
in case the PostDominatorTreeAnalysis had run before IPSCCP. Starting with
commit 1b1232047e83b the IPSCCP pass may use BlockFrequencyAnalysis for
some functions in the module. As a result, the PostDominatorTreeAnalysis
may not run until the BlockFrequencyAnalysis has run, since the latter
analysis depends on the former. Currently, we setup the DomTreeUpdater
using getCachedAnalysis to retrieve a PostDominatorTree. This happens
before BlockFrequencyAnalysis has run, therefore the cached analysis can
become invalid by the time we use it.

Differential Revision: https://reviews.llvm.org/D151666
2023-06-01 16:38:04 +01:00
Nikita Popov
223f9b096e Revert "[SCCP] Constant propagation through freeze instruction"
This reverts commit 559d47a1790e1a9f9b1f8838a443eb7624ef1ac7.

Caused failure on sanitizer-aarch64-linux-bootstrap-ubsan:

    clang++: /b/sanitizer-aarch64-linux-bootstrap-ubsan/build/llvm-project/llvm/lib/Transforms/Utils/SCCPSolver.cpp:442: llvm::ValueLatticeElement &llvm::SCCPInstVisitor::getValueState(llvm::Value *): Assertion `!V->getType()->isStructTy() && "Should use getStructValueState"' failed.
2023-06-01 15:30:46 +02:00
Mikhail Gudim
559d47a179 [SCCP] Constant propagation through freeze instruction
The freeze instruction has not been handled by SCCPInstVisitor.
This patch adds SCCPInstVisitor::visitFreezeInst(FreezeInst &I)
method to handle freeze instructions.

Differential Revision: https://reviews.llvm.org/D151659
2023-06-01 15:06:59 +02:00
Nikita Popov
af101f9ae0 [IR] Allow !range on vector of integer instructions
Inspired by https://reviews.llvm.org/D144467#4188310, this allows
!range on vector of integer instructions, with the usual
element-wise interpretation, which is already used by various
analysis APIs that support vectors.

Differential Revision: https://reviews.llvm.org/D145920
2023-03-14 09:41:56 +01:00
Nikita Popov
e0282cb4da [IPSCCP] Don't add !range metadata for vector returns
!range metadata is currenlty not supported on vector types. This
fixes verifier failures exposed by D144467.
2023-03-13 11:12:57 +01:00
Nikita Popov
6b545db83c [IPSCCP] Don't require noundef for !range annotation
Since https://reviews.llvm.org/D141386 !range violations return
poison instead of causing immediate undefined behavior. As such,
it is fine for IPSCCP to infer !range even if the value might be
poison. (The value cannot be undef as this would promote undef to
poison, but this is already checked separately.)

This basically undoes the late change done to D83952, restoring
it to its original version (which is now valid).

Differential Revision: https://reviews.llvm.org/D144467
2023-03-09 10:25:04 +01:00
Nikita Popov
92ef545b8d [IPSCCP] Remove noundef when zapping return values
When replacing return values with undef, we should also drop the
noundef attribute (and other UB implying attributes).

Differential Revision: https://reviews.llvm.org/D144461
2023-02-21 14:38:30 +01:00
Nikita Popov
dbc2f65ad5 [IPSCCP] Add tests for noundef attribute on zapped returns (NFC)
We replace the return value with undef without dropping the
noundef attribute.
2023-02-21 10:54:00 +01:00
Florian Hahn
7d10213317
Recommit "[SCCP] Support NUW/NSW inference for all overflowing binary operators."
This reverts commit 43acb61a08fffada31fb2e20e45fcc8492ef76b9.

Recommit the patch after fixing the issue causing the revert in 4e607ec4987.
Extra tests have been added in 5c6cb61ad416a544.

Original commit message:

   Extend the NUW/NSW inference logic add in 72121a20cd and cdeaf5f28c3dc
    to all overflowing binary operators.

    Reviewed By: nikic

    Differential Revision: https://reviews.llvm.org/D142721
2023-01-30 20:15:28 +00:00
Florian Hahn
5c6cb61ad4
[SCCP] Add sub tests for NUW/NSW flag inference.
Those tests add coverage for a miscompile with 024115ab14822a97c.
2023-01-30 19:40:56 +00:00
Florian Hahn
43acb61a08
Revert "[SCCP] Support NUW/NSW inference for all overflowing binary operators."
This reverts commit 024115ab14822a97c09adcd2545c14e78b843b36.

I suspect that this may be causing some buildbot bootstrapping failures.
Revert while I investigate.
2023-01-28 21:33:28 +00:00
Florian Hahn
024115ab14
[SCCP] Support NUW/NSW inference for all overflowing binary operators.
Extend the NUW/NSW inference logic add in 72121a20cd and cdeaf5f28c3dc
to all overflowing binary operators.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142721
2023-01-28 17:40:41 +00:00
Florian Hahn
7296eae946
[SCCP] Add test cases with switch on range containing undef. 2023-01-27 16:02:29 +00:00
Florian Hahn
72121a20cd
[SCCP] Use range info to prove AddInst has NSW flag.
This patch updates SCCP to use the value ranges of AddInst operands to
try to prove the AddInst does not overflow in the signed sense and
adds the NSW flag. The reasoning is done with
makeGuaranteedNoWrapRegion (thanks @nikic for point it out!).

Follow-ups will include extending this to more
OverflowingBinaryOperators.

Depends on D142387.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142390
2023-01-27 14:09:26 +00:00
Florian Hahn
ef4628c66e
[SCCP] Regenerate check lines for test.
The auto-generated check lines for the test are missing `align`.
Re-generate the check lines to avoid unrelated changes in upcoming
change.
2023-01-26 15:28:49 +00:00
Florian Hahn
19f512055c
[SCCP] Adjust test after cdeaf5f28c3dca. 2023-01-26 12:04:45 +00:00
Florian Hahn
cdeaf5f28c
Recommit "[SCCP] Use range info to prove AddInst has NUW flag."
This reverts commit 531756b9544b3e164b9ab998292fce3ebbc7c746.

The recommitted version fixes a crash when one of the operands is a
constant other than a ConstantInt. Test for that case have been added
in 5b16cd97b8e1c273.

It splits off the new logic into a separate function because setting the
flags is quite different compared to the other cases handled in replaceSignedInst
which all require replacing an existing instruction.

It also guards makeGuaranteedNoWrapRegion by `if (!Inst.hasNoUnsignedWrap())`
as discussed in the review.

Fixes #60280.
Fixes #60278.

Original message:
    This patch updates SCCP to use the value ranges of AddInst operands to
    try to prove the AddInst does not overflow in the unsigned sense and
    adds the NUW flag. The reasoning is done with
    makeGuaranteedNoWrapRegion (thanks @nikic for point it out!).

    Follow-ups will include adding NSW and extension to more
    OverflowingBinaryOperators.

    Reviewed By: nikic

    Differential Revision: https://reviews.llvm.org/D142387
2023-01-26 11:45:10 +00:00
Florian Hahn
54ee1ff254
[SCCP] Add additional tests for Add NUW/NSW flag inference. 2023-01-25 21:41:15 +00:00
Florian Hahn
531756b954
Revert "Recommit "[SCCP] Use range info to prove AddInst has NUW flag.""
This reverts commit 366e1faa2fffd5b2284e25b09b6a26bcd2aca2b7.

It looks like this exposes another set of crashes on the buildbots.
Revert while I investigate.
2023-01-25 18:22:24 +00:00
Florian Hahn
366e1faa2f
Recommit "[SCCP] Use range info to prove AddInst has NUW flag."
The recommitted version fixes a crash when one of the operands is a
constant other than a ConstantInt. Test for that case have been added
in 5b16cd97b8e1c273.

It splits off the new logic into a separate function because setting the
flags is quite different compared to the other cases handled in replaceSignedInst
which all require replacing an existing instruction. Instructions are
now refined before any replacements are done, which has the advantage
that we should have lattice values for all operands (fixing the crashes
and simplifies the logic) and also allows optimizing more cases where one
of the operands also gets replaced (see improvements in
@sge_with_sext_to_zext_conversion).

It also guards makeGuaranteedNoWrapRegion by `if (!Inst.hasNoUnsignedWrap())`
as discussed in the review.

Fixes #60280.
Fixes #60278.

Original message:
    This patch updates SCCP to use the value ranges of AddInst operands to
    try to prove the AddInst does not overflow in the unsigned sense and
    adds the NUW flag. The reasoning is done with
    makeGuaranteedNoWrapRegion (thanks @nikic for point it out!).

    Follow-ups will include adding NSW and extension to more
    OverflowingBinaryOperators.

    Reviewed By: nikic

    Differential Revision: https://reviews.llvm.org/D142387
2023-01-25 18:07:25 +00:00
Florian Hahn
5b16cd97b8
[SCCP] Add extra tests for Add flag inference.
Add extra tests from #60280 and #60278 as well as test showing missed
optimization opportunity.
2023-01-25 12:22:57 +00:00
Douglas Yung
c9401f2ebe Revert "[SCCP] Use range info to prove AddInst has NUW flag."
This reverts commit de122cb920080fd9e24b2777114271fbef932d5e.

This change causes assertion failures in many of our internal tests.
I have filed #60280 for this issue.
2023-01-24 21:23:03 -08:00
Florian Hahn
de122cb920
[SCCP] Use range info to prove AddInst has NUW flag.
This patch updates SCCP to use the value ranges of AddInst operands to
try to prove the AddInst does not overflow in the unsigned sense and
adds the NUW flag. The reasoning is done with
makeGuaranteedNoWrapRegion (thanks @nikic for point it out!).

Follow-ups will include adding NSW and extension to more
OverflowingBinaryOperators.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D142387
2023-01-24 20:53:07 +00:00
Florian Hahn
feea695653
[SCCP] Add vector add tests for nuw/nsw inference.
Also removes a dead argument from some tests.
2023-01-24 14:14:28 +00:00