309 Commits

Author SHA1 Message Date
Luke Lau
5f2145adf9 [RISCV] Use non-strided load if VL=1 for optimized zero stride loads
When optimizing vmv.s.x/vmv.v.x's of scalar loads, if VL is known to be
1 then we don't need to perform a stride of x0, and can just do a
regular load.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D147609
2023-04-06 11:22:14 +01:00
Mikhail R. Gadelha
6217f472a6 [RISCV] Lower inline asm m with offset to register+imm.
As part of D145584, we noticed that llvm was generating suboptimal code
for constraint m when the operand can be be lowered to reg+imm form: it
was being selected as a single register rather than register+imm. This
caused an unnecessary 'addi' to be gen for each m constraint.

This patch changes llvm to select register+imm. This might generate code
that cannot be assembled, but matches gcc's behavior.

Reviewed By: craig.topper, kito-cheng

Differential Revision: https://reviews.llvm.org/D146245
2023-03-31 13:20:13 -03:00
Craig Topper
0f4c9c016c [RISCV] Replace RISCV->RISC-V in strings.
To be consistent with RISC-V branding guidelines
https://riscv.org/about/risc-v-branding-guidelines/
Think we should be using RISC-V where possible.

D146449 already updated comments. Strings may have more user impact.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D146451
2023-03-27 09:50:17 -07:00
Craig Topper
29463612d2 [RISCV] Replace RISCV -> RISC-V in comments. NFC
To be consistent with RISC-V branding guidelines
https://riscv.org/about/risc-v-branding-guidelines/
Think we should be using RISC-V where possible.

More patches will follow.

Reviewed By: asb

Differential Revision: https://reviews.llvm.org/D146449
2023-03-27 09:50:17 -07:00
Craig Topper
b50c6857a4 [RISCV] Move fli selection in RISCVISelDAGToDAG.cpp. NFC
We custom isel for ConstantFP that has higher priority than isel
patterns. We were previously detecting valid FP constants for fli
to early exit from the custom code. This detection called
getLoadFPImm. Then we would run the isel patterns which would call
getLoadFPImm a second time.

With a little bit more code we can directly select the fli instruction
in the custom handler and avoid a second call.

Remove the incorrect mayRaiseFPException flag from the FLI instructions.

Reviewed By: joshua-arch1

Differential Revision: https://reviews.llvm.org/D146093
2023-03-21 19:33:27 -07:00
Craig Topper
30705e9770 [RISCV] Support Zfa fli instructions with vector splats.
-Return false from RISCVDAGToDAGISel::selectFPImm for fli
 constants so we don't try to use integer expansion.
-Support fli.h with Zvfh+Zfhmin.

Reviewed By: reames

Differential Revision: https://reviews.llvm.org/D145766
2023-03-10 09:16:21 -08:00
Craig Topper
17e0926d6a [RISCV] Don't try to use fli.h with Zfa+Zfhmin.
fli.h requires Zfh or Zvfh. We need to check for this in
isFPImmLegal. Zvfh support will come in another patch.

I had to split the test file because there are other issues with
Zfhmin and some intrinsics.
2023-03-08 22:54:25 -08:00
Craig Topper
006f88d05d [RISCV] Remove seemingly unneeded !isPosZero from Zfa code in isFPImmLegal.
This was added after the patch was approved. I'm not sure why its
there. It doesn't fire in any lit test.
2023-03-08 22:06:05 -08:00
Craig Topper
08b65c5c9e [RISCV] Remove some trailing whitespace. NFC 2023-03-08 21:34:10 -08:00
Craig Topper
8fa1e5e673 [RISCV] Teach performCombineVMergeAndVOps to combine unmasked TU vpmerge with a masked MU TA op.
We can form a MU TU operation and remove the merge if they use the
same merge value.

My primary interest was a case involving VP intrinsics from our downstream,
but it requires another optimization that isn't in upstream yet. So I've used
RVV intrinsics to get the desired instructions.

Co-authored-by: Nitin John Raj <nitin.raj@sifive.com>

Reviewed By: fakepaper56

Differential Revision: https://reviews.llvm.org/D145272
2023-03-07 08:59:48 -08:00
Jun Sha (Joshua)
ada2641460 [RISCV][CodeGen] Add codegen pattern for FLI instruction in experimental zfa extension
This patch implements experimental support for the RISCV Zfa extension as specified here: https://github.com/riscv/riscv-isa-manual/releases/download/draft-20221119-5234c63/riscv-spec.pdf, Ch. 25. This extension has not been ratified. Once ratified, it'll move out of experimental status.

This change adds codegen support for load-immediate instructions (fli.s/fli.d/fli.h).

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D141560
2023-03-07 14:27:48 +08:00
Craig Topper
465a48fecb [RISCV] Reuse the condop/invcondop ComplexPatterns for seteq/setne isel. NFC NFC NFC NFC
To do this we need to remove the always matching behavior from condop.
This requires us to add more 'select' isel patterns with a bare GPR
as the condition.

Rename condop/invcondop to riscv_setne/riscv_seteq.

This centralizes the ADDI/XORI/XOR tricks into one place.
2023-02-25 12:05:48 -08:00
Craig Topper
58de24ebbb [RISCV] Add a check for integer setcc to RISCVDAGToDAGISel::selectCondOp 2023-02-25 10:39:57 -08:00
Craig Topper
3caa427f8e [RISCV] Use ComplexPattern to reduce the number of patterns for XVentanaCondOps.
XVentanaCondOps check the condition operand for zero or non-zero.
We use this to optimize seteq/setne that would otherwise becomes
xor/xori/addi+snez/seqz. These patterns avoid the snez/seqz.

This patch adds two ComplexPatterns to match the varous cases and
emit the xor/xori/addi instruction.

These patterns can also be used by D144681.

Reviewed By: philipp.tomsich

Differential Revision: https://reviews.llvm.org/D144700
2023-02-24 09:36:58 -08:00
Manolis Tsamis
f6262201d8 [RISCV] Add vendor-defined XTheadMemIdx (Indexed Memory Operations) extension
The vendor-defined XTHeadMemIdx (no comparable standard extension exists
at the time of writing) extension adds indexed load/store instructions
as well as load/store and update register instructions.

It is supported by the C9xx cores (e.g., found in the wild in the
Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for this
extension is available at:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=27cfd142d0a7e378d19aa9a1278e2137f849b71b

Depends on D144002

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D144249
2023-02-24 00:17:58 +01:00
Fangrui Song
e4f4f34e7a [SelectionDAG] Migrate away from soft-deprecated functions. NFC 2023-02-21 11:01:34 -08:00
Craig Topper
78f106a554 [RISCV] Handle RISCVISD::SplitF64 and RISCVISD::BuildPairF64 during isel with Zfa.
Instead of special casing Zfa in the custom inserters, select the
correct instructions during isel.

BuildPairF64 we can do with pattern, but SplitF64 requires custom
selection due to the two destinations.

If we didn't need SplitF64 without Zfa, I would have an extract low
and extract high ISD opcode for Zfa to avoid that issue.
2023-02-18 17:03:55 -08:00
Philipp Tomsich
10b7cd660c [RISCV] Select signed and unsigned bitfield extracts for XTHeadBb
The XTHeadBb extension hab both signed and unsigned bitfield
extraction instructions (TH.EXT and TH.EXTU, respectively) which have
previously only been supported for sign extension on byte, halfword,
and word-boundaries.

This adds the infrastructure to use TH.EXT and TH.EXTU for arbitrary
bitfield extraction.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D144229
2023-02-17 21:46:26 +01:00
Manolis Tsamis
d4012bc43f [RISCV] Add vendor-defined XTheadMAC (multiply-accumulate) extension
The vendor-defined XTHeadMAC (no comparable standard extension exists
at the time of writing) extension adds multiply accumulate instructions.

It is supported by the C9xx cores (e.g., found in the wild in the
Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for this
extension is available at:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=4041e11db3ec3611921d10150572a92689aa3154

Co-authored-by: Philipp Tomsich <philipp.tomsich@vrull.eu>

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D143847
2023-02-14 20:25:47 +01:00
Craig Topper
7638409d43 [RISCV] Make vsetvli intrinsics default to MA.
The vsetvli insertion pass can replace it with MU if needed by
a using instruction. The vsetvli insertion pass will not convert
MU to MA so we need to start at MA.

Reviewed By: eopXD

Differential Revision: https://reviews.llvm.org/D143790
2023-02-13 10:39:55 -08:00
Philipp Tomsich
fc02eeb24f [RISCV] Add vendor-defined XTheadBb (basic bit-manipulation) extension
The vendor-defined XTHeadBb (predating the standard Zbb extension)
extension adds some bit-manipulation extensions with somewhat similar
semantics as some of the Zbb instructions.

It is supported by the C9xx cores (e.g., found in the wild in the
Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for XTHeadBb is
available from:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=8254c3d2c94ae5458095ea6c25446ba89134b9da

Depends on D143036

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D143439
2023-02-13 17:02:09 +01:00
Philipp Tomsich
04a2baf58f [RISCV] Add vendor-defined XTHeadBs (single-bit) extension
The vendor-defined XTHeadBs (predating the standard Zbs extension)
extension adds a bit-test instruction (th.tst) with similar semantics
as bexti from Zbs.  It is supported by the C9xx cores (e.g., found in
the wild in the Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for XTHeadBs is
available from:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=8254c3d2c94ae5458095ea6c25446ba89134b9da

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D143036
2023-02-13 16:28:26 +01:00
Craig Topper
c8ad1de4f0 [RISCV] Remove dead code from RISCVDAGToDAGISel::selectVSETVLI. NFC
vsetvli no longer has side effects so we don't need code for
handling INSTRINSIC_W_CHAIN.
2023-02-11 16:51:35 -08:00
Craig Topper
7e772e12d1 [RISCV] Fix mistake in comment. NFC 2023-02-11 12:32:54 -08:00
Philipp Tomsich
0bda199285 Revert "[RISCV] Add vendor-defined XTHeadBs (single-bit) extension"
This reverts commit 656188ddc4075eb50260607b3497589873f373d2.
2023-02-08 08:00:45 +01:00
Philipp Tomsich
b0c3132226 Revert "[RISCV] Add vendor-defined XTheadBb (basic bit-manipulation) extension"
This reverts commit 19a59099095b3cbc9846e5330de26fca0a44ccbe.
2023-02-08 08:00:34 +01:00
Philipp Tomsich
19a5909909 [RISCV] Add vendor-defined XTheadBb (basic bit-manipulation) extension
The vendor-defined XTHeadBb (predating the standard Zbb extension)
extension adds some bit-manipulation extensions with somewhat similar
semantics as some of the Zbb instructions.

It is supported by the C9xx cores (e.g., found in the wild in the
Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for XTHeadBb is
available from:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=8254c3d2c94ae5458095ea6c25446ba89134b9da

Depends on D143036

Differential Revision: https://reviews.llvm.org/D143439
2023-02-08 07:57:27 +01:00
Philipp Tomsich
656188ddc4 [RISCV] Add vendor-defined XTHeadBs (single-bit) extension
The vendor-defined XTHeadBs (predating the standard Zbs extension)
extension adds a bit-test instruction (th.tst) with similar semantics
as bexti from Zbs.  It is supported by the C9xx cores (e.g., found in
the wild in the Allwinner D1) by Alibaba T-Head.

The current (as of this commit) public documentation for XTHeadBs is
available from:
  https://github.com/T-head-Semi/thead-extension-spec/releases/download/2.2.2/xthead-2023-01-30-2.2.2.pdf

Support for these instructions has already landed in GNU Binutils:
  https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=8254c3d2c94ae5458095ea6c25446ba89134b9da

Depends on D143394

Differential Revision: https://reviews.llvm.org/D143036
2023-02-08 07:57:27 +01:00
Craig Topper
b9a2893b55 [RISCV] Move all isel cases for ISD::ConstantFP into RISCVDAGToDAGISel::Select.
After D142953, non-zero cases were handled in RISCVDAGToDAGISel::Select
and zeros were handled with isel patterns. The zeros cases are
sufficiently similar to zero that we might as well handle them all
together. We already needed to detect the cases to skip out to
tablegen.
2023-02-04 17:31:39 -08:00
Craig Topper
712e143883 [RISCV] Fix crash splatting f64 -0.0 into a vector on RV32 after D142953.
For RV32, we now use scalar fcvt of x0, scalar fneg, splat scalar fp to vector.
For RV64, we use li of 1, slli by 63, splat GPR to vector.
2023-02-04 16:34:06 -08:00
Craig Topper
fe6dd5d311 [RISCV] Make selectImm return SDValue instead of SDNode*.
This avoids multiple places needing to convert it to SDValue. It's
simpler to convert it to SDNode * in the places that need it.
2023-02-04 16:26:18 -08:00
Craig Topper
65ece07e02 [RISCV] Replace condition that should alwasy be true with an assert. NFC 2023-02-04 16:26:18 -08:00
Craig Topper
301db4f920 [RISCV] Use MVT enum directly instead of converting to bit width. NFC 2023-02-04 16:26:18 -08:00
Han-Kuan Chen
d02b9869b2 [RISCV] Don't use constantpool for floating-point value if the value can be easily constructed by integer sequence and a floating-point move.
In addition, this commit does the following combine

vfmv.v.f + fmv.[dhw].x -> vmv.v.x
vfmv.s.f + fmv.[dhw].x -> vmv.s.x
vfmerge.vfm + fmv.[dhw].x -> vmerge.vxm

Differential Revision: https://reviews.llvm.org/D142953
2023-02-03 22:42:08 -08:00
Craig Topper
2919ec041f [RISCV] Remove side effects from vsetvli intrinsics.
Delete the opt intrinsics since they are now identical.

I left the side effects due to user expectations about how these
interact with things like inline assembly or function calls. Or
that they wouldn't be hoisted. I think we should look at other
ways to address thoughs.

If I could, I'd rename them these somehow to distance them from
the vsetvli instruction. In some sense they only query the VL for
a particular SEW and LMUL. They don't guarantee a vsetvli
instruction will be emitted.

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

Reviewed By: rogfer01, kito-cheng

Differential Revision: https://reviews.llvm.org/D143220
2023-02-03 13:03:56 -08:00
Kazu Hirata
e078201835 [Target] Use llvm::count{l,r}_{zero,one} (NFC) 2023-01-28 09:23:07 -08:00
Craig Topper
cbbcb10e08 [RISCV] Refine the (mul (zext.w X), C) -> mulhu isel heuristic.
We try to shift both X and C left by 32 to replace the zext.w
with a SLLI and use mulhu. If C is already a simm32, this likely
makes a constant that is more expensive to materialize.
2023-01-28 00:05:19 -08:00
Kazu Hirata
188ec33726 [llvm] Use llvm::bit_width (NFC) 2023-01-21 14:48:32 -08:00
Kazu Hirata
8666c201eb [RISCV] Use llvm::bit_width (NFC)
I've verified that the arguments to llvm::bit_width are all of
uint64_t with:

  static_assert(std::is_same_v<uint64_t, decltype(Mask)>)
2023-01-21 10:54:09 -08:00
Craig Topper
7cd725858b [RISCV] RISCVDAGToDAGISel::selectShiftMask to shift by (sub size-1, X).
If the shift amount is (sub C, X) where C is -1 modulo the size of
the shift, we can replace the sub with a NOT.

We could also use XORI X, size-1, but NOT would work better with
c.not from the future Zce extension.
2022-12-29 16:33:18 -08:00
Craig Topper
e50976e569 [RISCV] Teach RISCVDAGToDAGISel::selectShiftMask to bypass adds with constant.
If the shift amount is (add X, C) where C is 0 modulo the size of
the shift, we can bypass the add.

Similar to other targets like AArch64 and X86.
2022-12-29 15:10:36 -08:00
Craig Topper
0e9855c1f2 [RISCV] Add SH1ADD/SH2ADD/SH3ADD to RISCVDAGToDAGISel::hasAllNBitUsers. 2022-12-28 23:38:33 -08:00
Craig Topper
79d6e9c713 [RISCV] Prefer ADDI over ORI if the known bits are disjoint.
There is no compressed form of ORI but there is a compressed form
for ADDI.

This also works for XORI since DAGCombine will turn Xor with disjoint
bits in Or.

Note: The compressed forms require a simm6 immediate, but I'm doing
this for the full simm12 range.

Reviewed By: kito-cheng

Differential Revision: https://reviews.llvm.org/D140674
2022-12-28 19:59:42 -08:00
Craig Topper
6357b63735 [RISCV] Add RISCV::XORI to RISCVDAGToDAGISel::hasAllNBitUsers. 2022-12-28 15:17:41 -08:00
Craig Topper
cdf09ce7e7 [RISCV] Support SRLI in hasAllNBitUsers.
We can recursively look through SRLI if the shift amount is less
than the demanded bits. We can reduce the demanded bit count by
the shift amount and check the users of the SRLI.
2022-12-28 13:10:52 -08:00
Nick Desaulniers
19a004b468 [llvm][SelectionDAGISel] support -{start|stop}-{before|after}= for remaining targets
Follow up to the series:
1. https://reviews.llvm.org/D140161
2. https://reviews.llvm.org/D140349
3. https://reviews.llvm.org/D140331
4. https://reviews.llvm.org/D140323

Completes the work from the previous two for remaining targets.

This creates the following named passes that can be run via
`llc -{start|stop}-{before|after}`:
- arc-isel
- arm-isel
- avr-isel
- bpf-isel
- csky-isel
- hexagon-isel
- lanai-isel
- loongarch-isel
- m68k-isel
- msp430-isel
- mips-isel
- nvptx-isel
- ppc-codegen
- riscv-isel
- sparc-isel
- systemz-isel
- ve-isel
- wasm-isel
- xcore-isel

A nice way to write tests for SelectionDAGISel might be to use a RUN:
line like:
llc -mtriple=<triple> -start-before=<arch>-isel -stop-after=finalize-isel -o -

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

Reviewed By: asb, zixuan-wu

Differential Revision: https://reviews.llvm.org/D140364
2022-12-21 13:25:15 -08:00
Craig Topper
c09edce1b3 [SelectionDAG] Give all the target specific subclasses of SelectionDAGISel their own pass ID.
Previously we had a shared ID in SelectionDAGISel. AMDGPU has an
initializePass function for its subclass of SelectionDAGISel. No
other target does.

This causes all target specific SelectionDAGISel passes to be known
as "amdgpu-isel".

I'm not sure what would happen if another target tried to implement
an initializePass function too since the ID is already claimed.

This patch gives all targets their own ID and passes it down to
SelectionDAGISel constructor to MachineFunctionPass's constructor.

Unfortunately, I think this causes most targets to lose
print-before/after-all support for their SelectionDAGISel pass.
And they probably no longer support start/stop-before/after. We
can add initializePass functions to fix this as a follow up. NOTE:
This was probably also broken if the AMDGPU target isn't compiled in.

Step 1 to fixing PR59538.

Reviewed By: arsenm

Differential Revision: https://reviews.llvm.org/D140161
2022-12-15 15:48:55 -08:00
Nitin John Raj
d741a31a39 [RISCV][CodeGen][SelectionDAG] Recursively check hasAllNBitUsers for logical machine opcodes
We don’t have W versions of AND/OR/XOR/ANDN/ORN/XNOR so we should recursively check their users. We should limit the recursion to SelectionDAG::MaxRecursionDepth levels.

We need to add a Depth argument, all existing callers should pass 0 to the Depth. The new recursive calls should increment it by 1. At the top of the function we should give up and return false if Depth >= SelectionDAG::MaxRecursionDepth.

Reviewed By: craig.topper

Differential Revision: https://reviews.llvm.org/D139462
2022-12-14 15:15:30 -08:00
Craig Topper
f2ffdbeb9c [RISCV] Add accessors to RISCVMatInt::Inst.
Make fields private. This helps hide that the Imm field doesn't
store a full int64_t.
2022-12-07 19:02:01 -08:00
Craig Topper
9e0f9f1132 [RISCV] Preserve chain output when selecting splat as x0 strided load.
We need the vlse node to have a chain output and it should replace
the chain output of the original load.
2022-11-29 18:09:55 -08:00