234 Commits

Author SHA1 Message Date
Jeremy Morse
7e88d51760
[NFC][RemoveDIs] Have CreateNeg only accept iterators (#82999)
Removing debug-intrinsics requires that we always insert with an
iterator, not with an instruction position. To enforce that, we need to
eliminate the `Instruction *` taking functions. It's safe to leave the
insert-at-end-of-block functions as the intention is clear for debug
info purposes (i.e., insert after both instructions and debug-info at
the end of the function).

This patch demonstrates how that needs to happen. At a variety of
call-sites to the `CreateNeg` constructor we need to consider:
* Has this instruction been selected because of the operation it
performs? In that case, just call `getIterator` and pass an iterator in.
* Has this instruction been selected because of it's position? If so, we
need to keep the iterator identifying that position (see the 3rd hunk
changing Reassociate.cpp, although it's coincidentally not debug-info
significant).

This also demonstrates what we'll try and do with the constructor
methods going forwards: have one fully explicit set of parameters
including iterator, and another with default-arguments where the
block-to-insert-into argument defaults to nullptr / no-position,
creating an instruction that hasn't been inserted yet.
2024-02-29 13:00:29 +00:00
Yingwei Zheng
cc83927516
[CVP] Canonicalize signed minmax into unsigned (#82478)
This patch turns signed minmax to unsigned to match the behavior for
signed icmps.
Alive2: https://alive2.llvm.org/ce/z/UAAM42
2024-02-23 02:42:49 +08:00
Yingwei Zheng
3ef63a71ad
[CVP] Refactor processMinMaxIntrinsic to check non-strict predicate in both directions (#82596)
This patch uses `getConstantRangeAtUse` in `processMinMaxIntrinsic` to
address the comment
https://github.com/llvm/llvm-project/pull/82478#discussion_r1497300920.
After this patch we can reuse the range result in
https://github.com/llvm/llvm-project/pull/82478.
2024-02-22 20:57:34 +08:00
Yingwei Zheng
ca61e6a71d
Revert "[CVP] Check whether the default case is reachable (#79993)" (#81585)
This reverts commit a034e65e972175a2465deacb8c78bc7efc99bd23.

Some protobuf users reported that this patch caused a significant
compile-time regression because `TailDuplicator` works poorly with a
specific pattern.

We will reland it once the codegen issue is fixed.
2024-02-13 17:28:06 +08:00
Yingwei Zheng
a034e65e97
[CVP] Check whether the default case is reachable (#79993)
This patch eliminates unreachable default cases using context-sensitive
range information.
2024-01-31 13:11:10 +08:00
Yingwei Zheng
848d7af956
[CVP] Improve the value solving of select at use (#76700)
This patch improves the value solving of select at use if the condition
is an icmp and we know the result of comparison from
`LVI->getPredicateAt`.

Compile-time impact:
http://llvm-compile-time-tracker.com/compare.php?from=7e405eb722e40c79b7726201d0f76b5dab34ba0f&to=3c315b1ddcb0ad82554b33f08b9356679fae4bb7&stat=instructions:u

|stage1-O3|stage1-ReleaseThinLTO|stage1-ReleaseLTO-g|stage1-O0-g|stage2-O3|stage2-O0-g|stage2-clang|
|--|--|--|--|--|--|--|
|-0.01%|+0.01%|-0.00%|-0.00%|-0.08%|+0.02%|-0.01%|
2024-01-06 02:32:57 +08:00
Shan Huang
06a9c6738a
[CVP] Fix #76058: missing debug location in processSDiv function (#76118)
This PR fixes #76058.
2023-12-22 09:26:32 +01:00
Nikita Popov
6ab663be8d [LVI] Require UndefAllowed argument to getConstantRangeAtUse() (NFC)
For the remaining uses set it to true, matching the current
behavior.
2023-12-12 12:45:49 +01:00
Nikita Popov
967e84eee3 [CVP] Don't use undef range for LHS of div/rem transforms
Using it for RHS is fine, as undef is UB in that case.
2023-12-12 12:06:19 +01:00
Nikita Popov
84df226c4a [CVP] Don't use undef ranges in willNotOverflow() 2023-12-12 11:54:33 +01:00
Nikita Popov
4949fb7954 [CVP] Don't allow undef range when inferring nowrap flags 2023-12-12 11:26:00 +01:00
Nikita Popov
4275da2278 [ValueTracking] Add isGuaranteedNotToBeUndef() variant (NFC)
We have a bunch of places where we have to guard against undef
to avoid multi-use issues, but would be fine with poison. Use a
different function for these to make it clear, and to indicate that
this check can be removed once we no longer support undef. I've
replaced some of the obvious cases, but there's probably more.

For now, the implementation is the same as UndefOrPoison, it just
has a more precise name.
2023-12-04 12:04:41 +01:00
Nikita Popov
2b646b5989
[CVP] Don't try to fold load/store operands to constant (#73338)
CVP currently tries to fold load/store pointer operands to constants
using LVI. If there is a dominating condition of the form `icmp eq ptr
%p, @g`, then `%p` will be replaced with `@g`.

LVI is geared towards range-based optimizations, and is *very*
inefficient at handling simple pointer equality conditions. We have
other passes that can handle this optimization in a more efficient way,
such as IPSCCP and GVN.

Removing this optimization gives a geomean 0.4-1.2% compile-time
improvement depending on configuration. At the same time, there
is no impact on codegen.
2023-11-27 09:17:03 +01:00
Yingwei Zheng
dc6d077396
[CVP] Infer nneg on existing zext (#72052)
This patch infers `nneg` flags for existing zext instructions in CVP.
After https://github.com/llvm/llvm-project/pull/71534 and this patch, we
can drop `zext -> zext nneg` transform in `RISCVCodeGenPrepare`:


40671bbdef/llvm/lib/Target/RISCV/RISCVCodeGenPrepare.cpp (L74-L83)

This is an alternative to #72049.
2023-11-13 22:41:37 +08:00
Nikita Popov
2c61f9cab5 [CVP] Fix use after scope
Store the result of ConstantRange::sdiv() in a variable, as
getSingleElement() will return a pointer to the APInt it contains.
2023-11-08 16:53:47 +01:00
Nikita Popov
d687057de8 [CVP] Try to fold sdiv to constant
If we know that the sdiv result is a single constant, directly
use that instead of performing narrowing.

Fixes https://github.com/llvm/llvm-project/issues/71659.
2023-11-08 14:49:24 +01:00
Craig Topper
55c9f24344
[CVP] Infer nneg on zext when forming from non-negative sext. (#70715)
Builds on #67982 which recently introduced the nneg flag on a zext
instruction.
2023-10-30 13:48:27 -07:00
XChy
b61d655353
[CVP] Flip signedness icmp predicate in use level (#69948)
Resolve #69928.
[Alive2 Proof](https://alive2.llvm.org/ce/z/zoqaqf).
2023-10-24 19:30:00 +08:00
DianQK
2ad9a65800
[LVI][CVP] Treat undef like a full range on abs(x, false) (#68711)
Fixes #68682.
2023-10-16 06:25:23 +08:00
Nikita Popov
103f1ac406 [CVP] Make select fold use-site specific (PR63756)
We may be able to replace the select with one of its select arms
at some but not all uses.

For uses in phi nodes this was already handled in the getValueOnEdge()
fold (which we can't drop entirely because it also handles an additional
case).

Fixes https://github.com/llvm/llvm-project/issues/63756.
2023-07-10 17:17:09 +02:00
luxufan
7b80a322ab [CVP] Don't process sext or ashr if value state including undef
similar to D152773

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D152774
2023-06-21 22:50:56 +08:00
Nikita Popov
7cfc82f331 [CVP] Use simpler urem expansion when LHS >= RHS (PR63330)
In this case we don't need to emit the comparison and select.

This is papering over a weakness in CVP in that newly added
instructions don't get revisited. If they were revisited, the
icmp would be folded at that point.

However, even without that it makes sense to handle this explicitly,
because it avoids the need to insert freeze, which may prevent
further analysis of the operation by LVI.

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

Fixes https://github.com/llvm/llvm-project/issues/63330.
2023-06-19 17:16:49 +02:00
Nikita Popov
90b55f2bba [CVP] Don't freeze value if guaranteed non-undef
Avoid inserting the freeze if not necessary, as this allows LVI
to continue reasoning about the expression.
2023-06-19 15:44:51 +02:00
Bjorn Pettersson
a20f7efbc5 Remove several no longer needed includes. NFCI
Mostly removing includes of InitializePasses.h and Pass.h in
passes that no longer has support for the legacy PM.
2023-04-17 13:54:19 +02:00
Arthur Eubanks
7c3c981442 [Passes] Remove some legacy passes
DFAJumpThreading
JumpThreading
LibCallsShrink
LoopVectorize
SLPVectorizer
DeadStoreElimination
AggressiveDCE
CorrelatedValuePropagation
IndVarSimplify

These are part of the optimization pipeline, of which the legacy version is deprecated and being removed.
2023-03-10 17:17:00 -08:00
Kazu Hirata
4a05edd410 [llvm] Use APInt::getZero instead of APInt::getNullValue (NFC)
Note that APInt::getNullValue has been soft-deprecated in favor of
APInt::getZero.
2023-02-19 22:42:01 -08:00
Kazu Hirata
dc6238e035 [Scalar] Remove an unused local varable (NFC)
The last uses of CRs were removed on Jan 17, 2023 in commit
61bb549cfd43fe4753fc78075b626ae18106fbeb.
2023-01-29 10:03:15 -08:00
Roman Lebedev
43a59be45d
[CVP] Expand bound udiv's, symmetrically with urem's
Symmetrical with the `urem` case, added in 66efb986322b206834e7c9e1eb777fa053912c39.

Simple case: https://alive2.llvm.org/ce/z/gRumLd / https://alive2.llvm.org/ce/z/rxEeC5
Second variant of precondition: https://alive2.llvm.org/ce/z/cAm9TD
2023-01-20 21:52:16 +03:00
Nikita Popov
61bb549cfd [CVP] Avoid duplicate range calculation (NFC)
Calculate the range once for all the sdiv/srem transforms.
2023-01-17 16:54:51 +01:00
Nikita Popov
004e613ce4 [CVP] Avoid duplicate range calculation (NFC)
Calculate the range once and use it in processURem() and
narrowUDivOrURem().
2023-01-17 16:39:27 +01:00
Nikita Popov
a444fe07dd [CVP] Handle use-site conditions in domain-based folds
As a side-effect, this switchem them to use getConstantRange() rather
than getPredicateAt(). getPredicateAt() is not supposed to be more
powerful than getConstantRange() for non-equality comparisons (as
long as block values are used).
2023-01-17 16:35:18 +01:00
Nikita Popov
5c38c6a3aa [CVP] Handle use-site conditions in more folds 2023-01-17 16:14:55 +01:00
Nikita Popov
d34537a732 [CVP] Handle use-site conditions in urem folds 2023-01-13 15:45:34 +01:00
Nikita Popov
0daa1142ea [CVP] Avoid duplicate range fetch (NFC)
In preparation for switching this to use getConstantRangeAtUse().
2023-01-13 14:56:07 +01:00
Nikita Popov
4f772b0955 [LVI][CVP] Make use of condition known at use
When an instruction is only used in a select or phi operand, we might
be able to make use of additional information from the select/branch
condition. For example in

  %sub = call i16 @llvm.usub.sat.i16(i16 %x, i16 10)
  %cmp = icmp uge i16 %x, 10
  %sel = select i1 %cmp, i16 %sub, i16 42

the usub.sat is only used in a select where %x uge 10 is known to
hold, so we can fold it based on that knowledge.

This addresses the regression reported at
https://reviews.llvm.org/D140798#4039748, but also provides a
solution to a recurring problem we've had, where we fail to make
use of range information after a branch+phi has been converted
into a select. Our current solution to this is to hope that IPSCCP
can perform the fold before that happens, but handling this in LVI
is a somewhat more general solution.

Currently we only make use of this for the willNotOverflow() fold,
but I plan to adjust other folds to use the new API as well.

Differential Revision: https://reviews.llvm.org/D141482
2023-01-12 16:41:31 +01:00
luxufan
5b25a0bcb1 [CVP] Simplify SRem when constantrange abs(lhs) < abs(rhs)
For `srem x, y`, if abs(constant range of x) less than abs(constant
range of y), we can simplify it as:
`srem x, y => x` if y is guaranteed to be positive.
'srem x, y => -x' if y is guaranteed to be negative.

Differential Revision: https://reviews.llvm.org/D140405
2023-01-03 22:51:12 +08:00
Roman Lebedev
08c2f4eb7a
[CVP] When expanding urem, always freeze the nominator
As per the post-commit feedback - that was not the correct precondition
to avoid it here. I think we should generally start changing mentality
about `freeze`, the fact that we have been conditioned to be afraid of it
(or of anything in LLVM in general) is the key problem here.
2022-12-31 05:00:43 +03:00
Roman Lebedev
66efb98632
[CVP] Expand bound urems
This kind of thing happens really frequently in LLVM's very own
shuffle combining methods, and it is even considered bad practice
to use `%` there, instead of using this expansion directly.
Though, many of the cases there have variable divisors,
so this won't help everything.

Simple case: https://alive2.llvm.org/ce/z/PjvYf-
There's alternative expansion via `umin`:
https://alive2.llvm.org/ce/z/hWCVPb

BUT while we can transform the first expansion
into the `umin` one (e.g. for SCEV):
https://alive2.llvm.org/ce/z/iNxKmJ
... we can't go in the opposite direction.

Also, the non-`umin` expansion seems somewhat more codegen-friendly:
https://godbolt.org/z/qzjx5bqWK
https://godbolt.org/z/a7bj1axbx

There's second variant of precondition:
https://alive2.llvm.org/ce/z/zE6cbM
but there the numerator must be non-undef / must be frozen.
2022-12-30 19:40:46 +03:00
Roman Lebedev
3cb827f9d3
[NFC][CVP] processURem(): add statistic and increase readability 2022-12-30 19:40:46 +03:00
Joshua Cao
cca01df291 [CVP] Eliminate urem when LHS < RHS
Fol `X % Y -> X` when we can determine `X < Y` based on constant
range information.

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

Differential Revision: https://reviews.llvm.org/D138360
2022-12-13 11:40:44 +01:00
Fangrui Song
a996cc217c Remove unused #include "llvm/ADT/Optional.h" 2022-12-05 06:31:11 +00:00
Kazu Hirata
edace862f6 [Scalar] Use std::optional in CorrelatedValuePropagation.cpp (NFC)
This is part of an effort to migrate from llvm::Optional to
std::optional:

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-11-25 23:52:49 -08:00
Nikita Popov
68b24c3b44 [CVP] Simplify comparisons without constant operand
CVP currently only tries to simplify comparisons if there is a
constant operand. However, even if both are non-constant, we may
be able to determine the result of the comparison based on range
information.

IPSCCP is already capable of doing this, but because it runs very
early, it may miss some cases.

Differential Revision: https://reviews.llvm.org/D137253
2022-11-03 15:35:27 +01:00
Yuanbo Li
ebd0249fcf [DebugInfo] Missing debug location after replacement in processSRem function
This patch fixes an issue in which CorrelatedValuePropagation::processSRem
would create new instructions to represent the SRem instruction, but would not
correctly copy any existing debug location metadata to the new instruction.

Differential Revision: https://reviews.llvm.org/D132218
2022-09-01 13:18:17 +01:00
Kazu Hirata
0e37ef0186 [Transforms] Fix comment typos (NFC) 2022-08-07 23:55:24 -07:00
Nuno Lopes
373571dbb4 [NFC] Switch a few uses of undef to poison as placeholders for unreachble code 2022-06-30 23:01:43 +01:00
Simon Moll
b8c2781ff6 [NFC] format InstructionSimplify & lowerCaseFunctionNames
Clang-format InstructionSimplify and convert all "FunctionName"s to
"functionName".  This patch does touch a lot of files but gets done with
the cleanup of InstructionSimplify in one commit.

This is the alternative to the less invasive clang-format only patch: D126783

Reviewed By: spatel, rengolin

Differential Revision: https://reviews.llvm.org/D126889
2022-06-09 16:10:08 +02:00
Jay Foad
6bec3e9303 [APInt] Remove all uses of zextOrSelf, sextOrSelf and truncOrSelf
Most clients only used these methods because they wanted to be able to
extend or truncate to the same bit width (which is a no-op). Now that
the standard zext, sext and trunc allow this, there is no reason to use
the OrSelf versions.

The OrSelf versions additionally have the strange behaviour of allowing
extending to a *smaller* width, or truncating to a *larger* width, which
are also treated as no-ops. A small amount of client code relied on this
(ConstantRange::castOp and MicrosoftCXXNameMangler::mangleNumber) and
needed rewriting.

Differential Revision: https://reviews.llvm.org/D125557
2022-05-19 11:23:13 +01:00
Craig Topper
4b36d9bde7 [CVP] Preserve exact name when converting sext->zext and ashr->lshr.
Previously we took the old name and always appended a numberic suffix.
Since we're doing a 1:1 replacement, it's clearer to keep the original
name exactly.

Reviewed By: fhahn

Differential Revision: https://reviews.llvm.org/D125281
2022-05-10 09:13:59 -07:00
serge-sans-paille
59630917d6 Cleanup includes: Transform/Scalar
Estimated impact on preprocessor output line:
before: 1062981579
after:  1062494547

Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup
Differential Revision: https://reviews.llvm.org/D120817
2022-03-03 07:56:34 +01:00