569 Commits

Author SHA1 Message Date
Craig Topper
e6a72a1d42
[RISCV] Combine ADDD+WMULSU to WMACCSU (#180454)
Extend the existing combineADDDToWMACC DAG combine to also match
RISCVISD::WMULSU and produce RISCVISD::WMACCSU. This is similar to
how ADDD+UMUL_LOHI is combined to WMACCU and ADDD+SMUL_LOHI is
combined to WMACC.

This patch was generated by AI, but I reviewed it.
2026-02-09 08:51:27 -08:00
Craig Topper
769b734c02
[RISCV] Combine ADDD with UMUL_LOHI/SMUL_LOHI into WMACCU/WMACC (#180383)
Combine the pattern:
  ADDD(addlo, addhi, UMUL_LOHI(x, y).0, UMUL_LOHI(x, y).1)
into:
  WMACCU(x, y, addlo, addhi)

And similarly for SMUL_LOHI -> WMACC.


This patch was written with AI, but I reviewed it carefully.
2026-02-08 13:39:32 -08:00
Craig Topper
a563e6bb7e
[RISCV] Add support for forming WMULSU during type legalization. (#180331)
Add a DAG combine to turn it into MULHSU if the lower half result
is unused.
2026-02-08 12:38:56 -08:00
Craig Topper
370764c8cb
[RISCV] Use addd/subd for i64 add/sub for RV32+P. (#180129)
Add RISCVISD opcodes and custom type legalize to them.
2026-02-06 12:42:11 -08:00
Craig Topper
22c5c2583d
[RISCV] Reorder the operands for RISCVISD::PPAIRE_DB. NFC (#180111)
Order the operands so the the low and high part of the rs1 regpair are
first, followed by the low and high part of the rs2 regpair.

Also change the type to use v4i8 for the result so that it's only
shuffling elements not combining elements into a larger elment.

I'm planning to add ADDD and SUBD opcodes that will be defined with the
same operand order allowing RISCVISelDAGToDAG.cpp code to be shared.
2026-02-05 21:35:47 -08:00
Craig Topper
1ad20b9428
[RISCV] Rename RISCVISD::PPACK_DH->PPAIRE_DB. NFC (#180089)
The instruction was renamed, but we hadn't renamed the ISD opcode.
2026-02-05 17:35:12 -08:00
Craig Topper
313d9ac1cf
[RISCV] Add wmul(u) codegen for RV32+P (#180032)
mulh tests are to make sure we continue to use mulh when only the
upper half is used.
2026-02-05 17:34:25 -08:00
Craig Topper
551d55397a
[RISCV] Add SimplifyDemandedBits and hasAllNBitUsers support for CLSW. (#174542)
This matches what we do for CLZW and other W instructions.
2026-01-06 17:03:23 -08:00
Craig Topper
5f18a06d10
[RISCV] P extension should not call hasAllWUsers or create non-simm32 constants on RV32. (#172802)
This causes a regression due to incomplete handling of RV32 for the P
extension in RISCVMatInt. I'll fix that in a follow up.
2025-12-19 08:37:58 -08:00
Craig Topper
a256c03206
[RISCV] Rename -enable-p-ext-codegen -riscv-enable-p-ext-simd-codegen. (#172790)
Make it clear this only applies to SIMD code and that it belongs to
RISC-V.
2025-12-18 07:11:16 -08:00
Craig Topper
6d405d6b5e
[RISCV] Replace enablePExtCodeGen with hasStdExtP for scalar code in RISCVISelDAGToDAG.cpp (#172785)
The enablePExtCodeGen was only intended to block vector code while
it is still in development. This code uses scalar types so we only
need to check for the extension.
2025-12-17 22:22:05 -08:00
Craig Topper
4d7382f1e9 [RISCV] Make an assert in RISCVDAGToDAGISel::Select stricter. NFC
We no longer expect i32 on RV64 here.
2025-12-17 21:58:02 -08:00
Craig Topper
88461e8f58
[RISCV] Prevent unnecessary calls to hasAllBUsers/AllHUsers. NFC (#172768)
Make sure the constant isn't already sign extended before calling these
functions.

Also add some elses to prevent checks where we already know the value
has been optimized.
2025-12-17 17:34:37 -08:00
Craig Topper
3b04094f36
[RISCV] Add Xsfmm vlte and vste intrinsics to getTgtMemIntrinsics. (#171747)
Replace dyn_cast with cast. The dyn_cast can never fail now. Previously
it never succeeded.
2025-12-10 20:47:31 -08:00
Craig Topper
46341d5938
[RISCV] Update P extension to the 018 version of the spec. (#170399)
Rename the PPACK* instructions to PPAIR*. Rename PDIF* to PABD*. Remove
Zba/Zbb instructions from P.

https://www.jhauser.us/RISCV/ext-P/
2025-12-07 22:57:39 -08:00
Piotr Fusik
d5072b94dd
[RISCV] Select (and (shl X, 8), 0xff00) -> (packh zero, X) (#170654)
Similar transforms with "pack" and "packw" are not useful:
(pack  zero, X) == (slli  X, XLEN/2)
(packw zero, X) == (slliw X, 16)
2025-12-04 20:25:16 +01:00
Piotr Fusik
cb5ccabbad
[RISCV][NFC] Simplify Imm range checks (#170497) 2025-12-04 06:04:49 +01:00
Brandon Wu
d889b97f2b
[RISCV][llvm] Support BUILD_VECTOR codegen for P extension (#169083) 2025-11-26 10:35:04 +08:00
Brandon Wu
3e5fafdc22
[RISCV][llvm] Select splat_vector(constant) with PLI (#168204)
Default DAG combiner combine BUILD_VECTOR with same elements to
SPLAT_VECTOR, we can just map constant splat to PLI if possible.
2025-11-20 15:02:40 +08:00
Craig Topper
c764ee6d1e
[RISCV] Remove custom legalization of v2i16/v4i8 loads for P extension. (#167651)
We can use the default legalization which will create an i32 load
followed by a v2i32 scalar_to_vector followed by a bitcast. We can isel
the scalar_to_vector like a bitcast and not generate any instructions
for it.
2025-11-12 19:32:06 -08:00
Brandon Wu
dfdc69b4c2
[RISCV][llvm] Preliminary P extension codegen support (#162668)
This is the initial support of P extension codegen, it only includes
small part of instructions:
PADD_H, PADD_B,
PSADD_H, PSADD_B,
PAADD_H, PAADD_B,
PSADDU_H, PSADDU_B,
PAADDU_H, PAADDU_B,
PSUB_H, PSUB_B,
PDIF_H, PDIF_B,
PSSUB_H, PSSUB_B,
PASUB_H, PASUB_B,
PDIFU_H, PDIFU_B,
PSSUBU_H, PSSUBU_B,
PASUBU_H, PASUBU_B
2025-11-11 17:01:45 +08:00
Craig Topper
5e8a0d6495
[RISCV] Use reportFatalUsageError in a few places (#166218)
Makes it consistent with feedback given in the equivalent GISel code.
https://github.com/llvm/llvm-project/pull/165876
2025-11-03 13:37:46 -08:00
Craig Topper
521fb93ec2
[RISCV] Support P extension ABSW instruction. (#165047) 2025-10-30 08:42:38 -07:00
Brandon Wu
6cec3622f6
[RISCV] Support XSfmm LLVM IR and CodeGen (#143069)
stack on: https://github.com/llvm/llvm-project/pull/143068

Co-authored-by: Piyou Chen <piyou.chen@sifive.com>
2025-10-14 02:01:57 +00:00
Jim Lin
1c00a001f6
[RISCV] Merge ADDI and SIGN_EXTEND_INREG to ADDIW during selectSETCC. (#162614)
That we can merge ADDI with its LHS to ADDIW if its LHS is a
SIGN_EXTEND_INREG.
2025-10-14 08:45:00 +08:00
quic_hchandel
a664e75993
[RISCV] Refactor DAG-to-DAG Selection: Port lowering code for qc.insb/qc.insbi to RISCVISelLowering.cpp (#157618)
This is a follow-up to #154135 and does similar changes for
`qc.insb/qc.insbi`.
2025-09-24 08:32:59 +05:30
Piotr Fusik
1dd94a2358
[RISCV] Add helper method for shift-and-add extensions (#158638)
Not an NFC, as it improves consistency, enabling some cases
for XAndesPerf and XTheadBa.
2025-09-16 08:53:27 +02:00
Craig Topper
c6947dad53
[RISCV] Add helper method for detecting BEXTI or TH_TST is supported. NFC (#157915)
These instructions both extract single bit to bit 0 and fill the upper
bits with 0.

There's at least one place where we check for BEXTI but not TH_TST. I
wanted to keep this patch NFC so that will be a follow up fix.
2025-09-10 12:16:46 -07:00
Craig Topper
f069700928
[RISCV] Prevent folding ADD_LO into load/store if we can't fold all uses. (#155935)
If we don't fold all uses, we end up with an LUI that is used by an ADDI
and some loads/stores. This requires the LUI to write a different
register than the ADDI or the load/stores uses have to be scheduled
between the LUI and ADDI. It prevents macrofusion of the LUI+ADDI on
CPUs that support it. It prevents the use of PseudoMovAddr which
prevents the LUI+ADDI from being rematerializable.

This is based on a patch we have had in our downstream for a while that
we originally wrote because of macrofusion and rematerialization. I no
longer have any relevant performance or code size numbers for it.

---------

Co-authored-by: Jesse Huang <jesse.huang@sifive.com>
Co-authored-by: Luke Lau <luke_lau@icloud.com>
2025-09-09 09:25:36 -07:00
Craig Topper
faab8f76d5
[RISCV] Add th.ext(u) to RISCVDAGToDAGISel::hasAllNBitUsers. (#157133)
These instructions are being used by the scalar efficiency SIG to
determine if we should have a standard instruction. Improving our
support for these instructions may give better data for their analysis.

I'll follow up by adding this to RISCVOptWInstrs too which will remove
the 'w' suffixes added here.
2025-09-05 11:57:26 -07:00
quic_hchandel
d0363815dd
[RISCV] Add changes to have better coverage for qc.insb and qc.insbi (#154135)
Before this patch, the selection for `QC_INSB` and `QC_INSBI` entirely
happens in C++, and does not support more than one non-constant input.

This patch seeks to rectify this shortcoming, by moving the C++ into a
target-specific DAGCombine, and adding `RISCV::QC_INSB`. One advantage
is this simplifies the code for handling `QC_INSBI`, as the C++ no
longer needs to choose between the two instructions based on the
inserted value (this is still done, but via ISel Patterns).

Another advantage of the DAGCombine is that this introduction can also
shift the inserted value to the `QC_INSB`, which our patterns need (and
were previously doing to the constant), and this shift can be
CSE'd/optimised with any prior shifts, if they exist. This allows the
inserted value to be variable, rather than a constant.
2025-09-03 12:35:38 +05:30
Sudharsan Veeravalli
c00b04a7e0
[RISCV] Generate QC_INSB/QC_INSBI instructions from OR of AND Imm (#154023)
Generate QC_INSB/QC_INSBI from `or (and X, MaskImm), OrImm` iff the
value being inserted only sets known zero bits. This is based on a
similar DAG to DAG transform done in `AArch64`.
2025-08-19 11:14:14 +05:30
Sudharsan Veeravalli
8495018a85
[RISCV] Use sd_match in trySignedBitfieldInsertInMask (#154152)
This keeps everything in APInt and makes it easier to understand and
maintain.
2025-08-19 08:22:06 +05:30
Craig Topper
6c0518a88f
[RISCV] Prioritize zext.h/zext.w over XTheadBb th.extu. (#154186)
Fixes #154125.
2025-08-18 16:56:57 -07:00
quic_hchandel
71b066e3a2
[RISCV] Add CodeGen support for qc.insbi and qc.insb insert instructions (#152447)
This patch adds CodeGen support for qc.insbi and qc.insb instructions
defined in the Qualcomm uC Xqcibm extension. qc.insbi and qc.insb
inserts bits into destination register from immediate and register
operand respectively.
A sequence of `xor`, `and` & `xor` depending on appropriate conditions
are converted to `qc.insbi` or `qc.insb` which depends on the
immediate's value.
2025-08-14 12:08:28 +05:30
Craig Topper
a90d653e59
[RISCV] Add RISCVISD::LD_RV32/SD_RV32 to isWorthFoldingAdd. (#151606) 2025-07-31 22:03:29 -07:00
UmeshKalappa
66b34bc943
[RISCV] Handled the uimm9 offset while FrameIndex folding. (#149303)
Reverted the https://github.com/llvm/llvm-project/pull/148779 changes
and

- handled the uimm9 offset in eliminateFrameIndex ()
- updated the testcase.
2025-07-30 17:43:10 +02:00
Craig Topper
f3c531c676 [RISCV] Use SDValue::getOperand instead of SDNode::getOperand for consistency. NFC 2025-07-29 12:30:15 -07:00
Craig Topper
c9714d2035
[RISCV] Add profitability checks to SelectAddrRegRegScale. (#150135)
-Only fold if the ADD can be folded into all uses.
-Don't reassociate an ADDI if the shl+add can be a shxadd or similar
instruction.
-Only reassociate a single ADDI. If there are 2 addis it's the same
number of instructions as shl+add. If there are more than 2 that it
would increase instructions over folding the addis into the
loads/stores.
2025-07-22 21:10:52 -07:00
Craig Topper
ff0cbecb68 [RISCV] Add a non-template version of SelectAddrRegZextRegScale and move code there. NFC
The template versions now call the non-template version. This
avoids duplicating the code for each template.
2025-07-19 17:53:39 -07:00
Craig Topper
0f71424280
[RISCV] Teach SelectAddrRegRegScale that ADD is commutable. (#149231) 2025-07-17 07:13:50 -07:00
Craig Topper
dbb6ed7631 [RISCV] Refactor SelectAddrRegRegScale. NFC
Rename UnwrapShl->SelectShl. Make it only responsible for matching
a SHL by constant.

Handle the fallback case of reg+reg with no scale outside of SelectShl.

Reorder the check so RHS is checked for shift first. The base pointer
is most likely on the LHS. It's very unlikely both operands are shifts.

This is preparation for adding better costing decisions to this code.
2025-07-15 22:43:56 -07:00
Craig Topper
5ff99f2757 [RISCV] Remove duplicate check in an if statement. NFC 2025-07-15 18:52:57 -07:00
Craig Topper
a87b8398f9 [RISCV] Simplify conversion from ISD::Constant to ISD::TargetConstant in SelectAddrRegRegScale. NFC
Directly copy the underlying ConstantInt instead of reconstructing it.
2025-07-15 18:41:28 -07:00
Craig Topper
4bd0e9e7f3 [RISCV] Add early out to reduce indentation in SelectAddrRegRegScale. NFC 2025-07-15 17:01:08 -07:00
Sudharsan Veeravalli
d67d91a990
[RISCV] Fix issues in ORI to QC.INSBI transformation (#148809)
The transformation done in #147349 was incorrect since we were not
passing the input node of the `OR` instruction to the `QC.INSBI`
instruction leading to the generated instruction doing the wrong thing.
In order to do this we first needed to add the output register to
`QC.INSBI` as being both an input and output.

The code produced after the above fix will need a copy (mv) to preserve
the register input to the OR instruction if it has more than one use
making the transformation net neutral ( `6-byte QC.E.ORI/ORAI` vs
`2-byte C.MV + 4-byte QC.INSB`I). Avoid doing the transformation if
there is more than one use of the input register to the OR instruction.
2025-07-15 12:01:33 -07:00
Craig Topper
63d099af14
[RISCV] Remove incorrect and untested FrameIndex support from SelectAddrRegImm9. (#148779)
To fold a FrameIndex, we need to teach eliminateFrameIndex to respect
the uimm9 range.
2025-07-15 10:49:23 -07:00
Craig Topper
9ba45c5c5e [RISCV] Move RISCVDAGToDAGISel::SelectAddrRegRegScale definition later. NFC
This function was placed between some static functions and their
callers. Reorder to keep the related code together.
2025-07-14 21:12:10 -07:00
Craig Topper
4923313727
[RISCV] Fix typo in comment. NFC (#148754)
'unsigned' was misspelled, but it seemed easier to write uimm9 than to
spell it out.
2025-07-14 20:56:07 -07:00
Craig Topper
3265a36c55
[RISCV] Refactor RISCVDAGToDAGISel::selectSimm5Shl2. NFC (#148731)
Return from the for loop body instead of using a break and checking the
shift amount after.
2025-07-14 20:54:06 -07:00