3944 Commits

Author SHA1 Message Date
Manoj Gupta
9fb9c7776e Revert "[SCEV] Replace IsAvailableOnEntry with block disposition"
This reverts commit 103fc0f629aa6218783f65dff0197f257137cade.
Causes a clang crash in ChromeOS builds.
Testcase provided at D149344.
2023-05-10 09:57:48 -07:00
Joshua Cao
9c1d5e4ae3 [SCEV][reland] More precise trip multiples
We currently have getMinTrailingZeros(), from which we can get a SCEV's
multiple by computing 1 << MinTrailingZeroes. However, this only gets us
multiples that are a power of 2. This patch introduces a way to get max
constant multiples that are not just a power of 2. The logic is similar
to that of getMinTrailingZeros. getMinTrailingZerosImpl is replaced by
computing the max constant multiple, and counting the number of trailing
bits.

I have so far found this useful in two places:

1) Computing unsigned constant ranges. For example, if we have i8
   {10,+,10}<nuw>, we know the max constant it can be is 250.

2) My original intent was to use this in getSmallConstantTripMultiples,
   but it has no effect right now due to change from D110587. For
   example, if we have backedge count `(6 * %N) - 1`, the trip count
   becomes `1 + zext((6 * %N) - 1)`, and we cannot say that 6 is a
   multiple of the SCEV. I plan to look further into this separately.

The implementation assumes the value is unsigned. It can probably be
extended to handle signed values as well.

If the code sees that a SCEV does not have <nuw>, it will fall back to
finding the max multiple that is a power of 2. Multiples that are a
power of 2 will still be a multiple even after the SCEV overflows. This
does not apply to other values. This is the 1st commit message:

---

This relands https://reviews.llvm.org/D141823. The verification fails
when expensive checks are turned on. This can occur when:

1. SCEV S's multiple is cached
2. SCEV S's no wrap flags are strengthened, and the multiple changes
3. SCEV verifier finds that S's cached and recomputed multiple are
   different

We eliminate most cases by forgetting SCEVAddRecExpr's cached values
when the flags are modified, but there are still cases for other SCEV
types. We relax the check by making sure the cached multiple divides the
recomputed multiple, ensuring the cached multiple is correct,
conservative multiple.

Reviewed By: mkazantsev

Differential Revision: https://reviews.llvm.org/D149529
2023-05-07 22:01:04 -07:00
Noah Goldstein
7770b0abfd [KnownBits] Improve KnownBits::rem(X, Y) in cases where we can deduce low-bits of output
The first `cttz(Y)` bits in `X` are translated 1-1 in the output.

Alive2 Links:
    https://alive2.llvm.org/ce/z/Qc47p7
    https://alive2.llvm.org/ce/z/19ut5H

Reviewed By: RKSimon

Differential Revision: https://reviews.llvm.org/D149421
2023-05-07 19:11:53 -05:00
Noah Goldstein
7b1f123b15 [KnownBits] Add tests for getting lowbits of rem X, Y; NFC
Reviewed By: foad

Differential Revision: https://reviews.llvm.org/D149420
2023-05-07 19:11:53 -05:00
Philip Reames
c501aa8843 [RISCV][TTI] Model shuffle mask materialization with correct index type
We were modeling these as if the index type was always e8, but the actual
lowering uses the data type width if legal.  We also weren't accounting for
i64 on xlen32 correctly.

Noticed via inspection while working on the shuffle/buildvec lowering.  Note
that this costing is also wrong in a more major way - we don't actually use
a constant pool load in many cases.  But that's a separate issue.
2023-05-05 12:26:21 -07:00
Evgenii Kudriashov
a82d27a9a6 [X86] Support llvm.{min,max}imum.f{16,32,64}
Addresses https://github.com/llvm/llvm-project/issues/53353

Reviewed By: RKSimon, pengfei

Differential Revision: https://reviews.llvm.org/D145634
2023-05-04 21:04:48 +08:00
Florian Hahn
293b4834b5
[SCEV] Add test where loop guards can be used to improve BTC. 2023-05-03 22:05:30 +01:00
Noah Goldstein
5eedfff695 [ValueTracking] Add additional cases for isKnownNonZero(mul X, Y)
If either `X` or `Y` is odd and the other is non-zero, the result is
non-zero.

Alive2 Link:
    https://alive2.llvm.org/ce/z/9V7-es

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149418
2023-04-30 10:06:46 -05:00
Noah Goldstein
d840391401 [ValueTracking] Add logic for isKnownNonZero(smin/smax X, Y)
For `smin` if either `X` or `Y` is negative, the result is non-zero.
For `smax` if either `X` or `Y` is strictly positive, the result is
non-zero.

For both if `X != 0` and `Y != 0` the result is non-zero.

Alive2 Link:
    https://alive2.llvm.org/ce/z/7yvbgN
    https://alive2.llvm.org/ce/z/zizbvq

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149417
2023-04-30 10:06:46 -05:00
Noah Goldstein
e78c30a10f [ValueTracking] Add logic for isKnownNonZero(umin X, Y)
`(umin X, Y) != 0` -> `X != 0 && Y != 0`

Alive2 Link:
    https://alive2.llvm.org/ce/z/AQh67i

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149416
2023-04-30 10:06:46 -05:00
Noah Goldstein
883daa7ac4 [ValueTracking] Add logic for isKnownNonZero(umax X, Y)
`(umax X, Y) != 0` -> `X != 0 || Y != 0`

Alive2 Link:
    https://alive2.llvm.org/ce/z/_Z9AUT

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149415
2023-04-30 10:06:46 -05:00
Noah Goldstein
167ecdaa2c [ValueTracking] Add logic for isKnownNonZero(sadd.sat X, Y)
The logic here is the same for `add` so reuse the existing helper
`isNonZeroAdd`

Alive2 Link:
    https://alive2.llvm.org/ce/z/mhKvC5

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149414
2023-04-30 10:06:46 -05:00
Noah Goldstein
461ded4631 [ValueTracking] Add logic for isKnownNonZero(ssub.sat X, Y)
The logic here is the same for normal `(sub X, Y)`, so just reused
`isNonZeroSub`.

Alive2 Link:
    https://alive2.llvm.org/ce/z/9kSkMv

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149412
2023-04-30 10:06:45 -05:00
Noah Goldstein
f1dfa4938a [ValueTracking] Add logic for isKnownNonZero(sshl.sat/ushl.sat X, Y)
`(sshl/ushl X, Y) != 0` -> `X != 0`

Alive2 Links
    https://alive2.llvm.org/ce/z/4WLM2p
    https://alive2.llvm.org/ce/z/BHFng4

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149411
2023-04-30 10:06:45 -05:00
Noah Goldstein
ea5a0d4b90 [ValueTracking] Add logic for isKnownNonZero(ctlz/cttz X)
for `cttz` if `X[0]` is non-zero, then the expression is non-zero.
for `ctlz` if `X[SignBit]` is non-zero, then the expression in
non-zero.

Alive2 Links:
    cttz (false): https://alive2.llvm.org/ce/z/ySQzbg
    cttz (true): https://alive2.llvm.org/ce/z/auiTCJ
    ctlz (false): https://alive2.llvm.org/ce/z/yk3sTJ
    ctlz (true): https://alive2.llvm.org/ce/z/-JuDty

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149410
2023-04-30 10:06:45 -05:00
Noah Goldstein
c7f7f601f2 [ValueTracking] Handle bitcasts between vec-int-ptr in isKnownNonZero
We where missing these cases so something like:
`(bitcast to i32 (or v216 x, <2, 1>))`

would not be found to be non-zero.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149409
2023-04-30 10:06:45 -05:00
Noah Goldstein
f7bf984ed3 [ValueTracking] Add more tests for isKnownNonZero cases; NFC
Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149408
2023-04-30 10:06:45 -05:00
Florian Hahn
b14be1e7c0
[SCEV] Use object size for globals to sharpen ranges.
The highest address the object can start is ObjSize bytes before the
end (unsigned max value). If this value is not a multiple of the
alignment, the last possible start value is the next lowest multiple
of the alignment. Note: The computations cannot overflow,
because if they would there's no possible start address for the
object.

At the moment, this is limited to GlobalVariables, because I could not
find a API similar to getObjectSize to also get the alignment of the
object. With such an API, this can be generalized to general addresses.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149483
2023-04-29 21:33:30 +01:00
Florian Hahn
02c5eb565f
[SCEV] Add tests for ptrtoint with different globals. 2023-04-28 15:29:07 +01:00
Nikita Popov
103fc0f629 [SCEV] Replace IsAvailableOnEntry with block disposition
As far as I understand, the IsAvailableOnEntry() function basically
implements the same functionality as the properlyDominates() block
disposition. The primary difference (apart from a weaker
implementation) seems to be in this comment at the top:

    // Checks if the SCEV S is available at BB.  S is considered available at BB
    // if S can be materialized at BB without introducing a fault.

However, I don't really understand why there would be such a
requirement. It's my understanding that SCEV explicitly does not
care about trapping udiv instructions itself, and it's the job of
SCEVExpander's isSafeToExpand() to make sure these don't get
expanded if they may trap.

Differential Revision: https://reviews.llvm.org/D149344
2023-04-28 11:02:03 +02:00
Noah Goldstein
4cd1b67491 [ValueTracking] Add logic for fshl/fshr(A, B, C) != 0 if A == B && A ! = 0
Having `A == B` is quite common for rotate patterns.

Alive2 Links:
    - https://alive2.llvm.org/ce/z/mPXi9c
    - https://alive2.llvm.org/ce/z/UfDHoI

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149372
2023-04-28 01:57:37 -05:00
Noah Goldstein
74157bf6e2 [ValueTracking] Add tests for proving fshr/fshl is non-zero; NFC
Differential Revision: https://reviews.llvm.org/D149371
2023-04-28 01:57:31 -05:00
Noah Goldstein
d8e9dd33b2 [ValueTracking] Add logic for udiv x,y != 0 if y u<= x
Alive2 Link:
        https://alive2.llvm.org/ce/z/2DKh46

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149203
2023-04-27 13:48:41 -05:00
ManuelJBrito
8b56da5e9f [IR] Change shufflevector undef mask to poison
With this patch an undefined mask in a shufflevector will be printed as poison.
This change is done to support the new shufflevector semantics
for undefined mask elements.

Differential Revision: https://reviews.llvm.org/D149210
2023-04-27 14:41:10 +01:00
Nikita Popov
fa0014a68b [SCEV] Drop LCSSA check in createNodeFromSelectLikePHI()
SCEV expressions no longer try to preserve LCSSA form. SCEV
construction will try to look through LCSSA phi nodes. As such,
we also no longer need to limit this special-case fold.
2023-04-27 15:18:07 +02:00
Nikita Popov
079c525f20 [SCEV] Try simplifying phi before createNodeFromSelectLikePHI()
Sometimes a phi can both be trivial and match the
createNodeFromSelectLikePHI() fold. In that case it is generally
more profitable to look through the phi node.
2023-04-27 15:07:19 +02:00
Nikita Popov
fe3ed6550e [SCEV] Regenerate test checks (NFC) 2023-04-27 12:57:28 +02:00
Noah Goldstein
75b48b4077 [ValueTracking] Add logic for add nuw x,y != 0 -> x != 0 || y != 0
Alive2 Link:
    https://alive2.llvm.org/ce/z/TKpqxc

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149204
2023-04-26 23:48:20 -05:00
Noah Goldstein
9b3c865d32 [ValueTracking] Add logic for (sub x, y) != 0 if we know KnownX != KnownY
Alive2 Link:
    https://alive2.llvm.org/ce/z/TAFcjF

Differential Revision: https://reviews.llvm.org/D149202
2023-04-26 23:48:17 -05:00
Noah Goldstein
73f5f1a8fa [ValueTracking] Add some additional tests for isKnownNonZero; NFC
Differential Revision: https://reviews.llvm.org/D149201
2023-04-26 23:48:10 -05:00
Dmitry Makogon
cbbd00b47a [Test] Add test showing bug in SCEV::getBackedgeTakenInfo (NFC)
Test for https://github.com/llvm/llvm-project/issues/62380/.
2023-04-26 18:20:14 +07:00
Nikita Popov
8acd874477 [SCEV] Regenerate test checks (NFC)
I was very confused by this change while working on a patch --
turns out that it's pre-existing, and we currently just match the
"1" part of "13".
2023-04-25 14:27:06 +02:00
Joshua Cao
a4e420ea64 Revert "[SCEV] Precise trip multiples"
This reverts commit 027a4c8b96c7f97df8e98b1dac069b956810ab94.
2023-04-24 01:41:53 -07:00
Joshua Cao
027a4c8b96 [SCEV] Precise trip multiples
We currently have getMinTrailingZeros(), from which we can get a SCEV's
multiple by computing 1 << MinTrailingZeroes. However, this only gets us
multiples that are a power of 2. This patch introduces a way to get max
constant multiples that are not just a power of 2. The logic is similar
to that of getMinTrailingZeros. getMinTrailingZeros is replaced by
computing the max constant multiple, and counting the number of trailing
bits.

This is applied in two places:

1) Computing unsigned constant ranges. For example, if we have i8
   {10,+,10}<nuw>, we know the max constant it can be is 250.

2) Computing trip multiples as shown in SCEV output. This is useful if
   for example, we are unrolling a loop by a factor of 5, and we know
   the trip multiple is 5, then we don't need a loop epilog.

If the code sees that a SCEV does not have <nuw>, it will fall back to
finding the max multiple that is a power of 2. Multiples that are a
power of 2 will still be a multiple even after the SCEV overflows.

Differential Revision: https://reviews.llvm.org/D141823
2023-04-24 00:21:59 -07:00
Joshua Cao
da36d1f099 [SCEV] Add trip multiple tests 2023-04-24 00:21:59 -07:00
Joshua Cao
fd77fab07a [SCEV] Add ranges tests 2023-04-24 00:21:59 -07:00
Simon Pilgrim
aca5f9aeea [CostModel][X86] getMemoryOpCost - increase cost of sub-32-bit vector load/stores
For 8-bit/16-bit vector loads/stores we scalarize and transfer to/from the vector unit, or use the (usually slow) PINSR/PEXTR instructions.

Fixes #59867
2023-04-23 21:48:25 +01:00
Simon Pilgrim
fed28ada47 [CostModel][X86] Add i64 MUL latency/codesize/size-latency cost estimates 2023-04-21 15:55:22 +01:00
Simon Pilgrim
ceccc59aac [CostModel][X86] Add i32 MUL latency/codesize/size-latency cost estimates 2023-04-21 15:42:41 +01:00
Simon Pilgrim
3e9d046bfc [CostModel][X86] Improve i16 and vXi16 MUL costs
Use a modified version of the D103695 script to determine more accurate throughput/latency/codesize/size-latency cost estimates
2023-04-21 15:42:40 +01:00
Nikita Popov
4cdb91f9e7 [SCEV] Clarify inference in isAddRecNeverPoison()
The justification in isAddRecNeverPoison() no longer applies, as
it dates back to a time where LLVM had an unconditional forward
progress guarantee. However, we also no longer need it, because we
can exploit branch on poison UB instead.

For a single exit loop (without abnormal exits) we know that all
instructions dominating the exit will be executed, so if any of
them trigger UB on poison that means that addrec is not poison.

This is slightly stronger than the previous code, because a) we
don't need the exit to also be the latch and b) we don't need the
value to be used in the exit branch in particular, any UB-producing
instruction is fine.

I don't expect much practical impact from this change, this is
mainly to clarify the reasoning behind this logic.

Differential Revision: https://reviews.llvm.org/D148633
2023-04-21 15:31:00 +02:00
Simon Pilgrim
4060042384 [CostModel][X86] Improve i8 and vXi8 MUL costs
We were treating vXi8 multiply as the sum of a trunc(mul(extend(),extend())) which diverged from the costs from llvm-mcaonce we extended beyond legal types

Use a modified version of the D103695 script to determine more accurate throughput/latency/codesize/size-latency cost estimates

Helps address some of the regressions identified in D148806
2023-04-20 19:38:51 +01:00
Zain Jaffal
3d3d8fef07
[AArch64] Improve fsh(l|r) cost modeling if 3rd arg is constant.
In that case, the cost for i32 and i64 should be 1 (a single EXTR
instruction). For v4i32 and v2i64 it should be 3 (USHR + SHL + ORR).

Other sizes smaller than 64 bits require an extra instruction for
conversion to i32/i64.

This recovers a SLP regression revealed by D140392.

Reviewed By: dmgreen

Differential Revision: https://reviews.llvm.org/D147322
2023-04-20 18:20:01 +01:00
Alexey Bataev
0e1312fbe0 [SLP][X86]Fix the cost of reused gathers/buildvectors and floats insert.
There are 2 problems in the cost estimation for buildvector/gather.
1. If the buildvector/gather node is the same as another one node, need
   to estimate the cost of this node as 0.
2. The cost of inserting float point register to non-poison vector is
   not 0, it should not be considered free.

Differential Revision: https://reviews.llvm.org/D148801
2023-04-20 09:34:46 -07:00
Florian Hahn
3310da0a4c
[AArch64] Add extra fshr tests with large types.
Extra tests discussed in D147322.
2023-04-20 16:39:04 +01:00
Florian Hahn
8d4f92601c
[AArch64] Add extra fshl tests with large types.
Extra tests discussed in D147322.
2023-04-20 16:34:59 +01:00
David Sherwood
afc2b7db02 [AArch64][CostModel] Make sext/zext free if folded into a masked load
The BasicTTIImpl implementation of getCastInstrCost ensures
that the cost of zext/sext is 0 when following a load if we
know the combined extending load is legal. For SVE we can do
the same for masked loads too, since they use exactly the
same underlying instruction.

Differential Revision: https://reviews.llvm.org/D148123
2023-04-20 08:48:57 +00:00
David Sherwood
a1ed8e369f [NFC][AArch64] Add cost model tests for extending loads
Differential Revision: https://reviews.llvm.org/D148122
2023-04-19 15:44:06 +00:00
Hassnaa Hamdi
045eec61f3 [AArch64][CostModel]: Add costs for zero/sign extend.
Add cost for extending to illegal scalable vector types.
Add testing file for the extend operations.

Reviewed By: sdesmalen

Differential Revision: https://reviews.llvm.org/D142456
2023-04-19 10:26:43 +00:00
Yeting Kuo
35c877a6f0 [RISCV] Customed lower vector nearbyint and rint in RISC-V.
The patch lowers vector rint/nearbyint like vp.rint/nearbyint.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D148619
2023-04-19 11:07:23 +08:00