463 Commits

Author SHA1 Message Date
Sam Elliott
0cac3e381d
[CodeGen][TII] Delete analyzeSelect hook (#175828)
The only caller of this function (`PeepholeOptimizer::optimizeSelect`)
did not use most of the parameters, was broadly equivalent to
`MI->isSelect()`, and the `optimizeSelect` hook can return `nullptr`
anyway.

Update `optimizeSelect` to return `nullptr` by default rather than
asserting when not implemented.
2026-02-04 14:14:45 -08:00
Min-Yih Hsu
4f04770984
[RISCV] Print MIR comments for AVL and VEC_RM operands (#179542)
Such that we can now have something like:
```
PseudoVFMACC_VV_M2_E64 %1, %28, %28, 7 /* frm=dyn */, %21 /* vl */, 6 /* e64 */, 0 /* tu, mu */
```
or
```
PseudoVFMACC_VV_M2_E64 %1, %28, %28, 7 /* frm=dyn */, -1 /* vl=VLMAX */, 6 /* e64 */, 0 /* tu, mu */
```
Hopefully this could make reading RISC-V MIR (a little) less painful.
2026-02-03 22:11:22 +00:00
Nemanja Ivanovic
674b020196
[RISC-V] Fix outliner candidate analysis (#177126)
When analyzing outliner candidates, there is no check that the tail-call
expansion register is live across the candidate call site. That can
result in a situation where the original function sets the volatile
register and uses it in the section that gets outlined. This of course
results in the use of the register receiving the incorrect value. Namely
the address of the outlined function since that is what the tail-call
sequence placed in the register.
2026-01-26 19:47:18 +01:00
Alexander Richardson
c563a88413
[RISC-V] Reduce code duplication for uimm*_lsb* operands. NFC
Use a common tablegen class instead of duplicating all the data and add a
new case macro to handle the isShiftedUInt<>() call. This refactoring was
motivated by adding RVY support since I needed to add uimm{9,10}_lsb0000.

Pull Request: https://github.com/llvm/llvm-project/pull/177743
2026-01-25 21:45:00 -08:00
Sam Elliott
7184229fea
[NFC][MI] Tidy Up RegState enum use (2/2) (#177090)
This Change makes `RegState` into an enum class, with bitwise operators.
It also:
- Updates declarations of flag variables/arguments/returns from
`unsigned` to `RegState`.
- Updates empty RegState initializers from 0 to `{}`.

If this is causing problems in downstream code:
- Adopt the `RegState getXXXRegState(bool)` functions instead of using a
ternary operator such as `bool ? RegState::XXX : 0`.
- Adopt the `bool hasRegState(RegState, RegState)` function instead of
using a bitwise check of the flags.
2026-01-23 00:19:03 -08:00
Sam Elliott
2042887709
Reland "[NFC][MI] Tidy Up RegState enum use (1/2)" (#176277)
This Change is to prepare to make RegState into an enum class. It:
- Updates documentation to match the order in the code.
- Brings the `get<>RegState` functions together and makes them
`constexpr`.
- Adopts the `get<>RegState` where RegStates were being chosen with
ternary operators in backend code.
- Introduces `hasRegState` to make querying RegState easier once it is
an enum class.
- Adopts `hasRegState` where equivalent was done with bitwise
arithmetic.
- Introduces `RegState::NoFlags`, which will be used for the lack of
flags.
- Documents that `0x1` is a reserved flag value used to detect if
someone is passing `true` instead of flags (due to implicit bool to
unsigned conversions).
- Updates two calls to `MachineInstrBuilder::addReg` which were passing
`false` to the flags operand, to no longer pass a value.
- Documents that `getRegState` seems to have forgotten a call to
`getEarlyClobberRegState`.

This PR relands llvm/llvm-project#176091 (commit
1d616cdca3aba9d22f120888bb6b09b75ca90b92) which was reverted in
llvm/llvm-project#176190 (commit
6309cd8668fc2ae589f156b23f86821f4ce5b7ea).
2026-01-16 13:05:06 -08:00
Sam Elliott
6309cd8668
Revert "[NFC][MI] Tidy Up RegState enum use (1/2)" (#176190)
Reverts llvm/llvm-project#176091

Reverting because some compilers were erroring on the call to
`Reg.isReg()` (which is not `constexpr`) in a `constexpr` function.
2026-01-15 07:58:05 -08:00
Sam Elliott
1d616cdca3
[NFC][MI] Tidy Up RegState enum use (1/2) (#176091)
This Change is to prepare to make RegState into an enum class. It:
- Updates documentation to match the order in the code.
- Brings the `get<>RegState` functions together and makes them
`constexpr`.
- Adopts the `get<>RegState` where RegStates were being chosen with
ternary operators in backend code.
- Introduces `hasRegState` to make querying RegState easier once it is
an enum class.
- Adopts `hasRegState` where equivalent was done with bitwise
arithmetic.
- Introduces `RegState::NoFlags`, which will be used for the lack of
flags.
- Documents that `0x1` is a reserved flag value used to detect if
someone is passing `true` instead of flags (due to implicit bool to
unsigned conversions).
- Updates two calls to `MachineInstrBuilder::addReg` which were passing
`false` to the flags operand, to no longer pass a value.
- Documents that `getRegState` seems to have forgotten a call to
`getEarlyClobberRegState`.
2026-01-15 07:47:05 -08:00
Christudasan Devadasan
9e1606026c
[CodeGen][InlineSpiller] Add SubReg argument to loadRegFromStackSlot for subreg-reload (#175581)
This preparatory patch introduces an additional argument to the target hook
loadRegFromStackSlot. Ths is essential for targets to handle subregister-specific
reload in the future. See how this is used for AMDGPU target with PR #175002.
2026-01-13 08:21:58 +05:30
Sudharsan Veeravalli
9596c9242f
[RISCV] Use LD_RV32/SD_RV32 for spills and reloads when Zilsd is enabled (#153595)
We are currently only using `PseudoRV32ZdinxSD/LD` for spills and
reloads when the register class is `GPRPairRegClass` . However, we can
use `LD_RV32/SD_RV32` when the `Zilsd` extension is enabled and certain
alignment requirements are met.
2026-01-13 06:18:21 +05:30
Pengcheng Wang
67601a43b5
[RISCV][NFC] Add RISCVVSETVLIInfoAnalysis
This can be reused by #95924.

Reviewers: BeMg, topperc, lukel97, preames, mshockwave

Reviewed By: mshockwave, topperc

Pull Request: https://github.com/llvm/llvm-project/pull/172615
2026-01-12 12:00:55 +08:00
Min-Yih Hsu
e742015f43
[RISCV] Assign separate latencies for vector COPYs in SpacemitX60 scheduling model (#172556)
Currently, we assign the same scheduling info to COPY regardless of
whether it's a scalar or vector one. But this might cause vector COPY
from physical registers to schedule too closed to its consumer,
prolonging the physical register live range and running out of registers
during RA as seen in #167008 .

This patch addresses this issue by creating schedule variants for COPY
instructions of vector register classes so that they can have the same
latency as simple vector arithmetics (WriteVIALUV). It is worth noting
that we _only_ need latency in this case -- keeping processor resources
in (vector) COPYs still causes the aforementioned register shortage
issue, because these COPY might then be blocked by structural hazards
and again, got sunk further down than we want.
2025-12-18 07:04:42 -08:00
quic_hchandel
8a0cdb88f9
[RISCV] Add short forward branch support for qc.e.lb(u), qc.e.lh(u) and qc.e.lw (#172629) 2025-12-18 09:38:31 +05:30
quic_hchandel
5bf5657a97
[RISCV] Add short forward branch support for lb, lbu, lh, lhu, lw, lwu and ld (#170829) 2025-12-17 08:57:20 +05:30
Craig Topper
fa9247526a
[RISCV] Add an OperandType for ordering for atomic pseudos. (#171744) 2025-12-10 19:44:22 -08:00
Craig Topper
c5ac7d66cb
[RISCV] Add OperandType for XSfmm TWiden. (#171572)
Use the same twiden format for PseudoSF_VSETTM and PseudoSF_VSETTK
as other XSfmm pseudos. Though I don't think we use the operand from
these instructions.
2025-12-10 13:05:55 -08:00
Craig Topper
2d80486656
[RISCV] Use a switch in RISCVInstrInfo::verifyInstruction. NFC (#170961)
The immediate only operands handled with a range check and a switch
nested under the default case.
2025-12-07 18:59:05 -08:00
Jesse Huang
71de78315f
[RISCV] Inserting indirect jumps with X7 for Zicfilp (#170683)
`BranchRelxation` uses `RISCVInstrInfo::insertIndirectBranch` to insert
an indirect branch if the jump target is out of range. Currently it uses
register scavenging to find a free register to use for the indirect
target. If Zicfilp is enabled, we need to use X7 so that the jump will
be treated as a software guarded branch.

Co-authored-by: Yeting Kuo <46629943+yetingk@users.noreply.github.com>
2025-12-05 11:54:40 +08:00
Craig Topper
8f9ef4b52d
[RISCV] Add OPERAND_AVL to RISCVInstrInfo::verifyInstruction. (#170770) 2025-12-04 16:28:09 -08:00
Craig Topper
6969ac83e2
[RISCV] Make RISCVInstrInfo::verifyInstruction stricter for immediate-only operands (#170736)
Most of the immediate operands can only be an immediate, but we were
allowing any non-register operand.

Split the immediate-only from the immediate or non-register operands.
The non-register cases could be made even stricter, but I'll leave that
as a TODO.
2025-12-04 22:32:59 +00:00
Craig Topper
9c273dc626
[RISCV] Correct the register class for the VL op check in RISCVInstrInfo::verifyInstruction. (#170751)
The VL operand cannot be X0. We represent VLMAX with -1.
2025-12-04 22:16:26 +00:00
Piotr Fusik
cb5ccabbad
[RISCV][NFC] Simplify Imm range checks (#170497) 2025-12-04 06:04:49 +01:00
Sam Elliott
a15a6c870b
[RISCV] Rename SFB Base Feature (#169607)
New SFB subsets are being added with the scheduler class name as a
suffix, so now is the time to go back to the base extension and add IALU
to its name.

This also:
- Drops a hyphen from the other SFB features for mul and minmax, to more
closely match their scheduling classes.
- Updates the predicates on specific SFB pseudos so we get verifier
errors if we introduce the pseudos when we don't have the right
subtarget feature.
- Updates the SFB Documentation comment to make it no longer
SiFive-specific.
2025-12-01 09:18:02 -08:00
Sam Elliott
8d920725ca
[RISCV] Use FMV.D for moving GPRPairs on RV32_Zdinx (#169556)
This is noted by the specification, and should save a dynamic
instruction.

Code size should be no worse than before, as the pairs of moves can
usually be turned into two 16-bit moves, but `fmv.d` is always a 32-bit
instruction.

LLVM can look through a `FSGNJ_D_IN32X`, in
`RISCVInstrInfo::isCopyInstrImpl` which helps copy propagation.
2025-11-25 20:03:56 +00:00
quic_hchandel
b1343e33c7
[RISCV] Add short forward branch support for lui, qc.li, and qc.e.li (#167481) 2025-11-12 15:47:49 +05:30
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
Matt Arsenault
1f3f522866
CodeGen: Remove TRI arguments from stack load/store hooks (#158240)
This is directly available in TargetInstrInfo
2025-11-10 16:24:39 -08:00
Matt Arsenault
11ab23c33d
CodeGen: Keep reference to TargetRegisterInfo in TargetInstrInfo (#158224)
Both conceptually belong to the same subtarget, so it should not
be necessary to pass in the context TargetRegisterInfo to any
TargetInstrInfo member. Add this reference so those superfluous
arguments can be removed.

Most targets placed their TargetRegisterInfo as a member
in TargetInstrInfo. A few had this owned by the TargetSubtargetInfo,
so unify all targets to look the same.
2025-11-10 22:40:39 +00:00
Sudharsan Veeravalli
6145b9d192
[RISCV] Support outlining of CFI instructions in the machine outliner (#166149)
Add support for outlining CFI instructions if

  a) the outlined function is being tail called
  b) all of the CFI instructions in the function are being outlined

This is similar to what is being done on AArch64 and X86.

---------

Co-authored-by: Craig Topper <craig.topper@sifive.com>
2025-11-07 12:35:39 +05:30
quic_hchandel
04619db16b
[RISCV] Add short forward branch support for mul instruction (#166300) 2025-11-04 12:38:50 +05:30
Philip Reames
e72876a519
[RISCV] Mask integer and float loads as canFoldAsLoad for stackmaps (#165761)
We have two mechanisms used in inline spilled for folding a load into a
consuming instruction. One is used for stack reloads, the other for
other load instructions (usually argument loads). We currently only
implement optimizations for the first case, but stackmaps have generic
support in target independent code for the other. We can go ahead and
set the flag to enable that optimization.

The primary motivation for this is that if we enable load
rematerialization without it, we run into crashes where we can't make
progress through rematerialization.

We probably should enable the other foldMemoryOperand hook for RISCV
specific instructions, but that's a separate optimization.
2025-10-31 14:42:16 +00:00
quic_hchandel
fa21fcbb5b
[RISCV] Add short forward branch support for min, max, maxu and minu (#164394) 2025-10-31 10:19:25 +05:30
Craig Topper
1a9aba29b0
[RISCV] Remove unreachable break statements. NFC (#164481) 2025-10-21 17:14:19 -07:00
Brandon Wu
0727e7a8a9
[RISCV] Support Zvfbfa codegen (#161158)
- [RISCV][llvm] Support Zvfbfa codegen and vsetvli insertion
- [RISCV][clang] Support Zvfbfa C intrinsics
The original PR is split into 2 PRs, this is codegen PR and other is
here: https://github.com/llvm/llvm-project/pull/164094

Co-authored-by: Craig Topper <craig.topper@sifive.com>
2025-10-18 15:22:21 +00:00
Brandon Wu
50aac2cd93
[RISCV] Add XSfmm pseudo instruction and vset* insertion support (#143068)
This patch supports the naive vset* insertion. If the state(tm, tn, tk,
sew, widen) changes, it emits all of the vset* instructions that are
needed, partial compatibility is not supported yet.

This is follow up patch for:
https://github.com/llvm/llvm-project/pull/133031
Co-authored-by: Piyou Chen <piyou.chen@sifive.com>
Co-authored-by: Craig Topper <craig.topper@sifive.com>
2025-10-13 15:06:31 +00:00
Piotr Fusik
3a3a524f54
[RISCV][NFC] Avoid iteration and division while selecting SHXADD instructions (#158851)
Should improve compilation time.
2025-10-07 07:26:41 +02:00
Craig Topper
62791b4377
[RISCV] Replace uses of RISCV::NoRegister with Register() or isValid. NFC (#161781) 2025-10-03 08:53:58 -07:00
quic_hchandel
1098a5cefd
[RISCV] Add commutative support for Qualcomm uC Xqcics extension (#161328)
This is a follow-up to #160653 doing similar changes for Xqcics.
2025-10-01 12:43:32 +05:30
quic_hchandel
46ea03997d
[RISCV] Add commutative support for Qualcomm uC Xqcicm extension (#160653)
This is a follow-up to #145643. See
https://github.com/llvm/llvm-project/pull/145643#issuecomment-3009300419.
2025-09-30 10:44:37 +05:30
Sam Elliott
df77a86f9b
[RISCV][NFC] Rename getOppositeBranchCondition (#160972) 2025-09-29 19:15:41 +01:00
Philip Reames
8b7a76a2ac [CodeGen] Rename isReallyTriviallyReMaterializable [nfc]
.. to isReMaterializableImpl.  The "Really" naming has always been
awkward, and we're working towards removing the "Trivial" part now,
so go ehead and remove both pieces in a single rename.

Note that this doesn't change any aspect of the current
implementation; we still "mostly" only return instructions which
are trivial (meaning no virtual register uses), but some targets
do lie about that today.
2025-09-23 11:58:37 -07:00
Boyao Wang
27f8f9e1f1
[RISCV][CodeGen] Add CodeGen support of Zibi experimental extension (#146858)
This adds the CodeGen support of Zibi v0.1 experimental extension, which
depends on #127463.
2025-09-18 11:03:48 +08:00
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
Jesse Huang
6ef4a7bf16
[RISCV] Exclude LPAD in machine outliner (#157220)
After https://github.com/llvm/llvm-project/pull/139993, the
RISCVIndirectBranchTracking pass is also ran before the Machine Outliner
pass, this yield a possibility that the outliner could also outline the
LPAD instruction that should be placed at the target of an indirect
branch. This patch excludes LPAD instruction from the candidate of
machine outliner.
2025-09-09 22:50:22 +08:00
Matt Arsenault
727e9f5ea5
CodeGen: Pass SubtargetInfo to TargetGenInstrInfo constructors (#157337)
This will make it possible for tablegen to make subtarget
dependent decisions without adding new arguments to every
target.

---------

Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2025-09-08 12:12:19 +09:00
Luke Lau
633233c04c [RISCV] Use MRI from MachineFunction in isVLKnownLE. NFC
TIL that MachineFunction actually stores a reference to
MachineRegisterInfo, so use that instead of plumbing it through. This
helps avoid the need to plumb MRI through static functions in #151285
2025-09-05 14:44:56 +08:00
Luke Lau
dc2ed00432
[RISCV] Handle non uimm5 VL constants in isVLKnownLE (#156639)
If a VL operand is > 31 then it will be materialized into an ADDI $x0,
imm. We can reason about it by peeking at the virtual register
definition which allows RISCVVectorPeephole and RISCVVLOptimizer to
catch more cases.

There's a separate issue with RISCVVLOptimizer where the materialized
immediate may not always dominate the instruction we want to reduce the
VL of, but this is left to another patch.
2025-09-05 00:25:48 +00:00
Pengcheng Wang
17a98f85c2
[RISCV] Optimize the spill/reload of segment registers (#153184)
The simplest way is:

1. Save `vtype` to a scalar register.
2. Insert a `vsetvli`.
3. Use segment load/store.
4. Restore `vtype` via `vsetvl`.

But `vsetvl` is usually slow, so this PR is not in this way.

Instead, we use wider whole load/store instructions if the register
encoding is aligned. We have done the same optimization for COPY in
https://github.com/llvm/llvm-project/pull/84455.

We found this suboptimal implementation when porting some video codec
kernels via RVV intrinsics.
2025-08-21 16:38:53 +08:00
Craig Topper
f58bc72759 Revert "[X86][ARM][RISCV][XCore][M68K] Invert the low bit to get the inverse predicate (NFC) (#151748)"
This reverts commit 518703806286c98bac7b84156738839f8bd55bef.

Failing M68k build bot.
2025-08-04 15:24:52 -07:00
AZero13
5187038062
[X86][ARM][RISCV][XCore][M68K] Invert the low bit to get the inverse predicate (NFC) (#151748)
All these platforms defined their predicate in such a way to allow bit
twiddling to get inverse predicates
2025-08-04 14:45:04 -07:00