1485 Commits

Author SHA1 Message Date
Luke Lau
0ef61ed54d [RISCV] Move NodeExtensionHelper assert to getOrCreateExtendedOp. NFC
Move the narrow types assert from the ZERO_EXTEND/SIGN_EXTEND case in
fillUpExtensionSupport to getOrCreateExtendedOp so we check the other nodes
too.
2024-03-11 18:00:29 +08:00
Luke Lau
58dd59a282
[RISCV] Don't run combineBinOp_VLToVWBinOp_VL until after legalize types. NFCI (#84125)
I noticed this from a discrepancy in fillUpExtensionSupport between how
we apparently need to check for legal types for ISD::{ZERO,SIGN}_EXTEND,
but we don't need to for RISCVISD::V{Z,S}EXT_VL.

Prior to #72340, combineBinOp_VLToVWBinOp_VL only ran after type
legalization because it only operated on _VL nodes. _VL nodes are only
emitted during op legalization, which takes place **after** type
legalization, which is presumably why the existing code didn't need to
check for legal types.

After #72340 we now handle generic ops like ISD::ADD that exist before
op legalization and thus **before** type legalization. This meant that
we needed to add extra checks that the narrow type was legal in #76785.

I think the easiest thing to do here is to just maintain the invariant
that the types are legal and only run the combine after type
legalization.
2024-03-11 17:43:02 +08:00
Craig Topper
d8d2dea7fc
[RISCV] Handle FP riscv_masked_strided_load with 0 stride. (#84576)
Previously, we tried to create an integer extending load. We need to a
non-extending FP load instead.

Fixes #84541.
2024-03-10 21:22:37 -07:00
Craig Topper
909ab0e0d1
[RISCV] Insert a freeze before converting select to AND/OR. (#84232)
Select blocks poison, but AND/OR do not. We need to insert a freeze
to block poison propagation.

This creates suboptimal codegen which I will try to fix with other
patches. I'm prioritizing the correctness fix since we have 2 bug reports.

Fixes #84200 and #84350
2024-03-07 15:03:51 -08:00
Michael Maitland
96049fcf4e [GISEL] Add IRTranslation for shufflevector on scalable vector types (#80378)
Recommits llvm/llvm-project#80378 which was reverted in
llvm/llvm-project#84330. The problem was that the change in
llvm/test/CodeGen/AArch64/GlobalISel/legalizer-info-validation.mir used
217 as an opcode instead of a regex.
2024-03-07 09:10:03 -08:00
Michael Maitland
552da24843
Revert "[GISEL] Add IRTranslation for shufflevector on scalable vector types" (#84330)
Reverts llvm/llvm-project#80378

causing Buildbot failures that did not show up with check-llvm or CI.
2024-03-07 10:16:31 -05:00
Michael Maitland
2b8aaef09e
[GISEL] Add IRTranslation for shufflevector on scalable vector types (#80378)
This patch is stacked on
https://github.com/llvm/llvm-project/pull/80372,
https://github.com/llvm/llvm-project/pull/80307, and
https://github.com/llvm/llvm-project/pull/80306.

ShuffleVector on scalable vector types gets IRTranslate'd to
G_SPLAT_VECTOR since a ShuffleVector that has operates on scalable
vectors is a splat vector where the value of the splat vector is the 0th
element of the first operand, because the index mask operand is the
zeroinitializer (undef and poison are treated as zeroinitializer here).
This is analogous to what happens in SelectionDAG for ShuffleVector.

`buildSplatVector` is renamed to`buildBuildVectorSplatVector`. I did not
make this a separate patch because it would cause problems to revert
that change without reverting this change too.
2024-03-07 09:50:29 -05:00
Luke Lau
c59129a7c7
[RISCV] Recursively split concat_vector into smaller LMULs (#83035)
This is the concat_vector equivalent of #81312, in that we recursively
split concat_vectors with more than two operands into smaller
concat_vectors.

This allows us to break up the chain of vslideups, as well as perform
the vslideups at a smaller LMUL, which in turn reduces register pressure
as the previous lowering performed N vslideups at the highest result
LMUL. For now, it stops splitting past MF2.

This is done as a DAG combine so that any undef operands are combined
away: If we do this during lowering then we end up with unnecessary
vslideups of undefs.
2024-03-07 16:50:26 +08:00
Luke Lau
0207270494
[RISCV] Don't remove extends for i1 indices in mgather/mscatter (#83951) 2024-03-06 08:52:27 +08:00
Craig Topper
58d8805ff9
[RISCV] Always use signed APSInt in getExactInteger. (#84070)
We were setting based on whether the FP value is positive/negative, but
we really want to know whether the resulting integer will be treated as
a signed or unsigned value. Since we use SINT_TO_FP to convert the
integer to FP, we should always used signed here.

Without this we convert +2147483648.0 to an integer 0x80000000 and
convert it using sint_to_fp which produces -2147483648.0.
2024-03-05 14:36:37 -08:00
Craig Topper
4dd9c2ed32
[RISCV] Use NewVL in splatPartsI64WithVL. (#83690)
In 7b5cf52f32c09, I added this NewVL and checked that it had been set,
but I didn't use it for the VL of the splat.
2024-03-02 17:08:48 -08:00
Yeting Kuo
14d8c4563e
[RISCV] Add more intrinsics into canSplatOperand. (#83106)
This patch adds smin/smax/umin/umax/sadd_sat/ssub_sat/uadd_sat/usub_sat
into canSplatOperand. It can help llvm fold vv instructions with one
splat operand to vx instructions.
2024-02-29 12:57:34 +08:00
Luke Lau
9617da88ab
[RISCV] Use a ta vslideup if inserting over end of InterSubVT (#83230)
The description in #83146 is slightly inaccurate: it relaxes a tail
undisturbed vslideup to tail agnostic if we are inserting over the
entire tail of the vector **and** we didn't shrink the LMUL of the
vector being inserted into.

This handles the case where we did shrink down the LMUL via InterSubVT
by checking if we inserted over the entire tail of InterSubVT, the
actual type that we're performing the vslideup on, not VecVT.
2024-02-28 15:58:55 +08:00
Luke Lau
91d23370cd
[RISCV] Use a tail agnostic vslideup if possible for scalable insert_subvector (#83146)
If we know that an insert_subvector inserting a fixed subvector will
overwrite the entire tail of the vector, we use a tail agnostic
vslideup. This was added in https://reviews.llvm.org/D147347, but we can
do the same thing for scalable vectors too.

The `Policy` variable is defined in a slightly weird place but this is
to mirror the fixed length subvector code path as closely as possible. I
think we may be able to deduplicate them in future.
2024-02-28 10:26:54 +08:00
Luke Lau
2e564840e0
[RISCV] Use getVectorIdxConstant in RISCVISelLowering.cpp. NFC (#83019)
We use getVectorIdxConstant() in some places and getConstant(XLenVT) or
getIntPtrConstant() in others, but getVectorIdxTy() == getPointerTy() ==
XLenVT.

This refactors RISCVISelLowering to use the former for nodes that use
getVectorIdxTy(), i.e. INSERT_SUBVECTOR, EXTRACT_SUBVECTOR,
INSERT_VECTOR_ELT and EXTRACT_VECTOR_ELT, so that we're consistent.
2024-02-27 10:34:04 +08:00
Yeting Kuo
e510fc7753
[VP][RISCV] Introduce vp.lrint/llrint and RISC-V support. (#82627)
RISC-V implements vector lrint/llrint by vfcvt.x.f.v.
2024-02-26 16:37:41 +08:00
Yeting Kuo
850dde063b
[RISCV][VP] Introduce vp saturating addition/subtraction and RISC-V support. (#82370)
This patch also pick the MatchContext framework from DAGCombiner to an
indiviual header file to make the framework be used from other files in
llvm/lib/CodeGen/SelectionDAG/.
2024-02-23 14:17:15 +08:00
Luke Lau
2d50703ddd [RISCV] Use RISCVSubtarget::getRealVLen() in more places. NFC
Catching a couple of more places where we can use the new query added in
8603a7b2.
2024-02-23 12:47:28 +08:00
Craig Topper
de41eae41f
[SelectionDAG][RISCV] Use FP type for legality query for LRINT/LLRINT in LegalizeVectorOps. (#82728)
This matches how LRINT/LLRINT is queried for scalar types in
LegalizeDAG.

It's confusing if they do different things since a "Legal" vector
LRINT/LLRINT would get through to LegalizeDAG which would then consider
it illegal. This doesn't happen currently because RISC-V uses Custom.
2024-02-22 20:18:52 -08:00
Philip Reames
ac518c7c99
[RISCV] Vector sub (zext, zext) -> sext (sub (zext, zext)) (#82455)
This is legal as long as the inner zext retains at least one bit of
increase so that the sub overflow case (0 - UINT_MAX) can be
represented. Alive2 proof: https://alive2.llvm.org/ce/z/BKeV3W

For RVV, restrict this to power of two sizes with the operation type
being at least e8 to stick to legal extends. We could arguably handle i1
source types with some care if we wanted to.

This is likely profitable because it may allow us to perform the sub
instruction in a narrow LMUL (equivalently, in fewer DLEN-sized pieces)
before widening for the user. We could arguably avoid narrowing below
DLEN, but the transform should at worst introduce one extra extend and
one extra vsetvli toggle if the source could previously be handled via
loads explicit w/EEW.
2024-02-22 16:17:48 -08:00
Yingwei Zheng
0107c8824b
[RISCV][SDAG] Improve codegen of select with constants if zicond is available (#82456)
This patch uses `add + czero.eqz/nez` to lower select with constants if
zicond is available.
```
(select c, c1, c2) -> (add (czero_nez c2 - c1, c), c1)
(select c, c1, c2) -> (add (czero_eqz c1 - c2, c), c2)
```
The above code sequence is suggested by [RISCV Optimization
Guide](https://riscv-optimization-guide-riseproject-c94355ae3e6872252baa952524.gitlab.io/riscv-optimization-guide.html#_avoid_branches_using_conditional_moves).
2024-02-23 00:18:56 +08:00
Luke Lau
edd4aee4dd
[RISCV] Compute integers once in isSimpleVIDSequence. NFCI (#82590)
We need to iterate through the integers twice in isSimpleVIDSequence, so
instead of computing them twice just compute them once at the start.

This also replaces the individual checks that each element is constant
with a single call to BuildVectorSDNode::isConstant.
2024-02-22 15:57:57 +08:00
Luke Lau
815644b4dd
[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (#82506)
This fixes the miscompile reported in #82430 by telling
isSimpleVIDSequence to sign extend to XLen instead of the width of the
indices, since the "sequence" of indices generated by a strided load
will be at XLen.

This was the simplest way I could think of getting isSimpleVIDSequence
to treat the indexes as if they were zero extended to XLenVT.

Another way we could do this is by refactoring out the "get constant
integers" part from isSimpleVIDSequence and handle them as APInts so we
can separately zero extend it.

Fixes #82430
2024-02-22 11:50:27 +08:00
Simon Pilgrim
e3d4cac2bc Fix MSVC "not all control paths return a value" warning 2024-02-21 13:53:48 +00:00
Simon Pilgrim
88a18556ae Fix MSVC signed/unsigned mismatch warning 2024-02-21 13:53:48 +00:00
Chia
c50ca3daa4
[RISCV][ISel] Combine vector fadd/fsub/fmul with fp extend. (#81248)
Extend D133739 and #76785 to support vector widening floating-point
add/sub/mul instructions.

Specifically, this patch works for the below optimization case:

### Source code
```
define void @vfwmul_v2f32_multiple_users(ptr %x, ptr %y, ptr %z, <2 x float> %a, <2 x float> %b, <2 x float> %b2) {
  %c = fpext <2 x float> %a to <2 x double>
  %d = fpext <2 x float> %b to <2 x double>
  %d2 = fpext <2 x float> %b2 to <2 x double>
  %e = fmul <2 x double> %c, %d
  %f = fadd <2 x double> %c, %d2
  %g = fsub <2 x double> %d, %d2
  store <2 x double> %e, ptr %x
  store <2 x double> %f, ptr %y
  store <2 x double> %g, ptr %z
  ret void
}
```

### Before this patch
[Compiler Explorer](https://godbolt.org/z/aaEMs5s9h)
```
vfwmul_v2f32_multiple_users:
        vsetivli        zero, 2, e32, mf2, ta, ma
        vfwcvt.f.f.v    v11, v8
        vfwcvt.f.f.v    v8, v9
        vfwcvt.f.f.v    v9, v10
        vsetvli zero, zero, e64, m1, ta, ma
        vfmul.vv        v10, v11, v8
        vfadd.vv        v11, v11, v9
        vfsub.vv        v8, v8, v9
        vse64.v v10, (a0)
        vse64.v v11, (a1)
        vse64.v v8, (a2)
        ret
```

### After this patch
```
vfwmul_v2f32_multiple_users:
        vsetivli zero, 2, e32, mf2, ta, ma
        vfwmul.vv v11, v8, v9
        vfwadd.vv v12, v8, v10
        vfwsub.vv v8, v9, v10
        vse64.v v11, (a0)
        vse64.v v12, (a1)
        vse64.v v8, (a2)
```
2024-02-21 22:06:40 +09:00
Yingwei Zheng
02fad0565f
[RISCV][SDAG] Fold select c, ~x, x into xor -c, x (#82462)
This patch lowers select of constants if `TrueV == ~FalseV`.
Address the comment in
https://github.com/llvm/llvm-project/pull/82456#discussion_r1496881603.
2024-02-21 16:27:43 +08:00
Wang Pengcheng
a445474d3f
[RISCV] Use TImmLeaf for csr_sysreg (#82463)
And use `getTargetConstant` to create operands.

This PR addresses comments after committing #82322.
2024-02-21 15:04:29 +08:00
Wang Pengcheng
b8ed69ecc0 [RISCV] Support llvm.readsteadycounter intrinsic
This intrinsic was introduced by #81331, which is a lot like
`llvm.readcyclecounter`.

For the RISCV implementation, we rename `ReadCycleWide` pseudo to
`ReadCounterWide` and make it accept two operands (the low and high
parts of the counter). As for legalization and lowering parts, we
reuse the code of `ISD::READCYCLECOUNTER` (make it able to handle
both intrinsics), and we use `time` CSR for `ISD::READSTEADYCOUNTER`.

Tests using Clang builtins are runned on real hardware and it works
as excepted.

Reviewers: asb, MaskRay, dtcxzyw, preames, topperc, jhuber6

Reviewed By: jhuber6, asb, MaskRay, dtcxzyw

Pull Request: https://github.com/llvm/llvm-project/pull/82322
2024-02-21 13:12:14 +08:00
Philip Reames
8603a7b21f [RISCV] Add a query for exact VLEN to RISCVSubtarget [nfc]
We've now got enough of these in tree that we can see which patterns
appear to be idiomatic.  As such, extract a helper for checking
if we know the exact VLEN.
2024-02-20 17:27:47 -08:00
Michael Maitland
44a46a0b68
[RISCV][GISEL] Add IRTranslation for insertelement with scalable vector type (#80377)
This patch is stacked on #80372, #80307, and #80306.
2024-02-19 15:30:48 -05:00
Luke Lau
50b82ef1db
[RISCV] Use DAG.getElementCount in lowerEXTRACT_SUBVECTOR. NFCI (#81698)
Following up on the review comment in
https://github.com/llvm/llvm-project/pull/79949#discussion_r1482071745
2024-02-19 13:31:52 +08:00
Philip Reames
db836f6094
[RISCV] Narrow vector absolute value (#82041)
If we have a abs(sext a) we can legally perform this as a zext (abs a). 
(See the same combine in instcombine - note that the IntMinIsPoison flag
 doesn't exist in SDAG yet.)

On RVV, this is likely profitable because it may allow us to perform the arithmetic
operations involved in the abs at a narrower LMUL before widening for the user.
We could arguably avoid narrowing below DLEN, but the transform should
at worst move around the extend and create one extra vsetvli toggle if the source
could previously be handled via loads explicit w/EEW.
2024-02-16 19:26:06 -08:00
Philip Reames
4265ad1215
[RISCV] Adjust sdloc when creating an extend for widening instructions (#82040)
We were using the SDLoc corresponding to the original arithmetic
instruction, but here using the SDLoc corresponding to the original
extend if we need to introduce a new narrower extend seems cleaner.

As can be seen in the test diffs, this very minorly impacts scheduling
and register allocation by given the scheduler a hint from original
program order.
2024-02-16 13:00:25 -08:00
Yingwei Zheng
a300a1a711
[RISCV][ISel] Add codegen support for the experimental zabha extension (#80192)
This patch implements the codegen support of zabha (Byte and Halfword
Atomic Memory Operations) v1.0-rc1 extension.
See also https://github.com/riscv/riscv-zabha/blob/v1.0-rc1/zabha.adoc.

---------

Co-authored-by: Craig Topper <craig.topper@sifive.com>
2024-02-16 15:35:09 +08:00
Craig Topper
b57ba8ec51
[RISCV] Use APInt in useInversedSetcc to prevent crashes when mask is larger than UINT64_MAX. (#81888)
There are no checks that the type is legal so we need to handle any
type.
2024-02-15 10:48:52 -08:00
Philip Reames
275eeda32f
[RISCV] Split long build_vector sequences to reduce critical path (#81312)
If we have a long chain of vslide1down instructions to build e.g. a <16
x i8> from scalar, we end up with a critical path going through the
entire chain. We can instead build two halves, and then combine them
with a vselect. This costs one additional temporary register, but
reduces the critical path by roughly half.

To avoid needing to change VL, we fill each half with undefs for the
elements which will come from the other half. The vselect will at worst
become a vmerge, but is often folded back into the final instruction of
the sequence building the lower half.

A couple notes on the heuristic here:
* This is restricted to LMUL1 to avoid quadratic costing reasoning.
* This only splits once. In future work, we can explore recursive
splitting here, but I'm a bit worried about register pressure and thus
decided to be conservative. It also happens to be "enough" at the
default zvl of 128.
* "8" is picked somewhat arbitrarily as being "long". In practice, our
build_vector codegen for 2 defined elements in a VL=4 vector appears to
need some work. 4 defined elements in a VL=8 vector seems to generally
produce reasonable results.
* Halves may not be an optimal split point. I went down the rabit hole
of trying to find the optimal one, and decided it wasn't worth the
effort to start with.

---------

Co-authored-by: Luke Lau <luke_lau@icloud.com>
2024-02-14 10:15:24 -08:00
Luke Lau
b9567bc78f
[RISCV] Use SelectionDAG::getVScale in lowerVPReverseExperimental. NFCI (#81694)
Use a slightly more idiomatic way of getting vscale. getVScale
performs additional constant folding, but I presume computeKnownBits
also catches these cases too.
2024-02-14 12:30:29 +08:00
Luke Lau
208edf7672 [RISCV] Fix assertion in lowerEXTRACT_SUBVECTOR
This fixes a crash when lowering an extract_subvector like:

t0:v1i64 = extract_subvector t1:v2i64, 1

Whilst we never need a vslidedown with M1 on scalable vector types, we might
need to do it for v1i64/v1f64, since the smallest container type for it is
nxv1i64/nxv1f64.

The lowering code is still correct for this case, but the assertion was too
strict. The actual invariant we're relying on is that ContainerSubVecVT's LMUL
<= M1, not < M1. Hence why we handled v2i32 fine, because its container type
was nxv1i32 and MF2.
2024-02-13 20:40:31 +08:00
Luke Lau
bb77047a3b
[RISCV] Handle fixed length vectors with exact VLEN in loweringEXTRACT_SUBVECTOR (#79949)
This is a revival of #65392. When we lower an extract_subvector, we
extract the
subregister that the subvector is contained in first and then do a
vslidedown
with LMUL=1. We can currently only do this for scalable vectors though
because
the index is scaled by vscale and thus we will know what subregister the
subvector lies in.

For fixed length vectors, the index isn't scaled by vscale and so the
subvector
could lie in any arbitrary subregister, so we have to do a vslidedown
with the
full LMUL.

The exception to this is when we know the exact VLEN: in which case, we
can
still work out the exact subregister and do the LMUL=1 vslidedown on it.

This patch handles this case by scaling the index by 1/vscale before
computing
the subregister, and extending the LMUL=1 path to handle fixed length
vectors.
2024-02-13 14:29:08 +08:00
Luke Lau
06c89bd59c
[RISCV] Check type is legal before combining mgather to vlse intrinsic (#81107)
Otherwise we will crash since target intrinsics don't have their types
legalized. Let the mgather get legalized first, then do the combine on
the legal type.
Fixes #81088

Co-authored-by: Craig Topper <craig.topper@sifive.com>
2024-02-09 06:51:11 +08:00
Philip Reames
b8545e1ece
[RISCV] Consider all subvector extracts within a single VREG cheap (#81032)
This adjusts the isSubVectorExtractCheap callback to consider any
extract which fits entirely within the first VLEN bits of the src vector
(and uses a 5 bit immediate for the slide) as cheap. These can be done
via a single m1 vslide1down.vi instruction.

This allows our generic DAG combine logic to kick in and recognize a few
more cases where shuffle source is longer than the dest, but that using
a wider shuffle is still profitable. (Or as shown in the test diff, we
can split the wider source and do two narrower shuffles.)
2024-02-08 12:15:33 -08:00
Philip Reames
d0f72f8860
[RISCV] Consider truncate semantics in performBUILD_VECTORCombine (#81168)
Fixes https://github.com/llvm/llvm-project/issues/80910.

Per the documentation in ISDOpcodes.h, for BUILD_VECTOR "The types of
the operands must match the vector element type, except that integer
types are allowed to be larger than the element type, in which case the
operands are implicitly truncated."

This transform was assuming that the scalar operand type matched the
result type. This resulted in essentially performing a truncate before a
binop, instead of after. As demonstrated by the test case changes, this
is often not legal.
2024-02-08 11:28:06 -08:00
Yeting Kuo
0716d31649
[RISCV][NFC] Use maybe_unused instead of casting to void to fix unused variable warning. (#80651) 2024-02-06 14:41:47 +08:00
Craig Topper
8ed046fc15 [RISCV] Custom type legalize i32 SADDSAT/SSUBSAT without Zbb.
While working on -riscv-experimental-rv64-legal-i32, I noticed this
missed optimization in our current codegen.

This expands to SADDO/SSUBO+select while still in i32. These will
be type legalized individually.
2024-02-04 23:15:58 -08:00
Craig Topper
5afeba051e [RISCV] Custom legalize i32 UADDSAT/USUBSAT for -riscv-experimental-rv64-legal-i32 with Zbb.
This matches the codegen we get from type legalization without
-riscv-experimental-rv64-legal-i32.
2024-02-04 21:37:38 -08:00
Chia
db060ab053
[RISCV][ISel] Remove redundant vmerge for vwsub(u).wv. (#80523) 2024-02-05 13:59:11 +09:00
Craig Topper
32b99617ac [RISCV] Custom promote i32 UADDSAT/USUBSAT for -riscv-experimental-rv64-legal-i32 with Zbb. 2024-02-04 12:39:13 -08:00
Craig Topper
859b09da08 [RISCV] Promote i32 ISD::VAARG to i64 for -riscv-experimental-rv64-legal-i32. 2024-02-04 11:03:12 -08:00
Craig Topper
08e942aca6 [RISCV] Combine (xor (trunc (X cc Y)) 1) -> (trunc (X !cc Y)) for RV64LegalI32.
This is needed with RV64LegalI32 when the setcc is created after type
legalization. An i1 xor would have been promoted to i32, but the setcc
would have i64 result.
2024-02-03 13:57:47 -08:00