250 Commits

Author SHA1 Message Date
Yingwei Zheng
acf86c5c4d [CVP] Keep ReachableCaseCount in sync with range of condition (#142302)
https://github.com/llvm/llvm-project/pull/79993 assumes that a reachable
case must be contained by `CR`. However, it doesn't hold for some edge
cases. This patch adds additional checks to ensure `ReachableCaseCount`
is correct.

Note: Similar optimization in SCCP isn't affected by this bug because it
uses `CR` to compute `ReachableCaseCount`.

Closes https://github.com/llvm/llvm-project/issues/142286.
2025-06-11 15:55:43 -07:00
Han Qi
b7db403e70
[CVP] Add samesign flag to icmp (#115642)
Closes #114820.
2024-11-11 08:35:14 +01:00
Nikita Popov
46ccefb123 [CVP] Fix APInt ctor assertion with i1 urem
This is creating an APInt with value 2, which may not be well-defined
for i1 values. Fix this by replacing the Y.umul_sat(2) with
Y.uadd_sat(Y).
2024-11-05 10:03:37 +01:00
Kazu Hirata
94f9cbbe49
[Scalar] Remove unused includes (NFC) (#114645)
Identified with misc-include-cleaner.
2024-11-02 08:32:26 -07:00
Nikita Popov
3127b659fa
[CVP] Infer range return attribute (#99620)
We already infer this in IPSCCP (which runs very early, so cannot
benefit from inlining and simplifications) and SCCP (which runs without
PredicateInfo, so does not use assumes). Do it in CVP as well, so it can
handle cases that IPSCCP/SCCP can't.

Fixes https://github.com/llvm/llvm-project/issues/98946 (everything
apart from f2, where the assume is dropped by the frontend).
2024-09-20 14:29:19 +02:00
Poseydon42
01623860c5
[NFC] Add CmpIntrinsic class to represent calls to UCMP/SCMP intrinsics (#98177) 2024-07-09 21:05:53 +02:00
Nikita Popov
79d6f52c4f [LVI] Use Constant instead of Tristate for predicate results
A lot of the users just end up converting it into a Constant
themselves. Doing this in the API leaves less room for error
with vector types, and brings getPredicateResult() closer to
LatticeValueElement::getCompare(), hopefully allowing us to
consolidate them.
2024-07-04 17:28:09 +02:00
Nikita Popov
c2072d993a [CVP] Support vectors for and elision 2024-07-03 14:40:48 +02:00
Nikita Popov
1eec81a831
[CVP][LVI] Add support for vectors (#97428)
The core change here is to add support for converting vector constants
into constant ranges. The rest is just relaxing isIntegerTy() checks and
making sure we don't use APIs that assume vectors.

There are a couple of places that don't support vectors yet, most
notably the "simplest" fold (comparisons to a constant) isn't supported
yet. I'll leave these to a followup.
2024-07-03 12:41:49 +02:00
Poseydon42
a616f57c1f
[CorrelatedValuePropagation] Fold calls to UCMP/SCMP when we know that ranges of operands do not overlap (#97235)
This patch adds folds for calls to `ucmp`/`scmp` intrinsics where we can
establish that the range of the first operand is strictly to the left or
strictly to the right of the range of the second operand.
2024-07-02 12:18:52 +02:00
Yingwei Zheng
394e321a2e
Reland "[CVP] Check whether the default case is reachable (#79993)" (#96089)
This patch reverts https://github.com/llvm/llvm-project/pull/81585 as
https://github.com/llvm/llvm-project/pull/78582 has been landed.
Now clang works well with reproducer
https://github.com/llvm/llvm-project/pull/79993#issuecomment-1936822679.
2024-06-20 13:45:18 +08:00
Noah Goldstein
925a11128c [CVP] Convert sitofp -> uitofp nneg and add nneg flag to uitofp
Similiar to the `InstCombine` changes, just furthering the scope of
the canonicalization/`uitofp nneg` support
2024-05-07 14:57:29 -05:00
Yingwei Zheng
df9c00beea
[CVP] Use at-use info in processBinOp (#88523)
This patch uses `getConstantRangeAtUse` to infer nsw/nuw flags with
at-use info. It will enables more optimizations in InstCombine.

Compile-time impact:
http://llvm-compile-time-tracker.com/compare.php?from=a5ed14bc8e122fa5ac0aa81f8d8390931bd6b4e4&to=a83d3402b663439b91cb37a046fb7ac0220ba5c7&stat=instructions%3Au

Related issue: #87854
2024-04-13 15:07:55 +08:00
Yingwei Zheng
caa2258250
[LLVM] Remove nuw neg (#86295)
This patch removes APIs that creating NUW neg. It is a trivial case
because `sub nuw 0, X` always gets simplified into zero.
I believe there is no optimization opportunities in the real-world
applications that we can take advantage of the nuw flag.

Motivated by
https://github.com/llvm/llvm-project/pull/84792#discussion_r1524891134.

Compile-time improvement:
https://llvm-compile-time-tracker.com/compare.php?from=d1f182c895728d89c5c3d198b133e212a5d9d4a3&to=da7b7478b7cbb32c09d760f6b8d0e67901e0d533&stat=instructions:u
2024-03-26 20:56:16 +08:00
Philip Reames
49b1fc4f83
[CVP] Freeze Y when expanding urem x, y with X < 2Y (#84390)
We're going from a single use to two independent uses, we need these two
to see consistent values for undef. As an example, consider x = 0x2 when
y = 0b00u1. If the sub use picks 0b0001 and the cmp use picks 0b0011,
that would be incorrect.
2024-03-07 14:37:10 -08:00
Jeremy Morse
2fe81edef6 [NFC][RemoveDIs] Insert instruction using iterators in Transforms/
As part of the RemoveDIs project we need LLVM to insert instructions using
iterators wherever possible, so that the iterators can carry a bit of
debug-info. This commit implements some of that by updating the contents of
llvm/lib/Transforms/Utils to always use iterator-versions of instruction
constructors.

There are two general flavours of update:
 * Almost all call-sites just call getIterator on an instruction
 * Several make use of an existing iterator (scenarios where the code is
   actually significant for debug-info)
The underlying logic is that any call to getFirstInsertionPt or similar
APIs that identify the start of a block need to have that iterator passed
directly to the insertion function, without being converted to a bare
Instruction pointer along the way.

Noteworthy changes:
 * FindInsertedValue now takes an optional iterator rather than an
   instruction pointer, as we need to always insert with iterators,
 * I've added a few iterator-taking versions of some value-tracking and
   DomTree methods -- they just unwrap the iterator. These are purely
   convenience methods to avoid extra syntax in some passes.
 * A few calls to getNextNode become std::next instead (to keep in the
   theme of using iterators for positions),
 * SeparateConstOffsetFromGEP has it's insertion-position field changed.
   Noteworthy because it's not a purely localised spelling change.

All this should be NFC.
2024-03-05 15:12:22 +00:00
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