535 Commits

Author SHA1 Message Date
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
Craig Topper
5a95ec6dc1
[RISCV] Add riscv_vlm/vsm to RISCVTargetLowering::getTgtMemIntrinsic. (#148265) 2025-07-11 16:59:47 -07:00
Craig Topper
574b66f241
[RISCV] Use Selection::haveNoCommonBitsSet in RISCVDAGToDAGISel::orDisjoint. (#147838) 2025-07-09 16:18:51 -07:00
Jim Lin
7a6568dcd5
[RISCV] Support LLVM IR intrinsics for XAndesVSIntLoad (#147493)
This patch adds LLVM IR intrinsic support for XAndesVSIntLoad.

The document for the intrinsics can be found at:
https://github.com/andestech/andes-vector-intrinsic-doc/blob/ast-v5_4_0-release-v5/auto-generated/andes-v5/intrinsic_funcs/04_andes_vector_int4_load_extension.adoc

The clang part will be added in a later patch.

---------

Co-authored-by: Lino Hsing-Yu Peng <linopeng@andestech.com>
2025-07-09 13:02:57 +08:00
Craig Topper
4d0c25f4a6
[RISCV] Select disjoint_or+not as xnor. (#147636)
A disjoint OR can be converted to XOR. And a XOR+NOT is XNOR. Idea
taken from #147279.
    
I changed the existing xnor pattern to have the not on the outside
instead of the inside. These are equivalent for xor since xor is
associative. Tablegen was already generating multiple variants
of the isel pattern using associativity.
    
There are some issues here. The disjoint flag isn't preserved
through type legalization. I was hoping we could recover it
manually for the masked merge cases, but that doesn't work either.
2025-07-08 21:50:23 -07:00
Craig Topper
b7248b5cd4
[RISCV] Use cast instead of dyn_cast to MemSDNode in RISCVISelDAGToDAG.cpp. (#147643)
All of these are guaranteed to be MemSDNode. The only intrinsics that
aren't are vlm and vsm. We should add those to
RISCVTargetLowering::getTgtMemIntrinsic to fix that.
2025-07-08 21:44:39 -07:00
Sudharsan Veeravalli
6b95aa6444
[RISCV] Use QC.INSBI for OR with immediate when ORI isn't possible (#147349)
When the immediate to the ORI is a ShiftedMask_32 that does not fit in
12-bits we can use the QC.INSBI instruction instead. We do not do this
for cases where the ORI can be replaced with a BSETI since these can be
compressesd when the Xqcibm extension (which QC.INSBI is a part of) is
enabled.
2025-07-08 11:38:04 +05:30
Luke Lau
07ae19c132
[RISCV] Move performCombineVMergeAndVOps to RISCVVectorPeephole (#144076)
This moves the peephole that folds vmerges into its operands into
RISCVVectorPeephole. This will also allow us to eventually commute
instructions to allow folding, see #141885 and #70042

Most of the test diffs are due to the slight change in instruction
ordering.

For now doPeepholeMaskedRVV is kept even though its a duplicate of
RISCVVectorPeephole::convertToUnmasked to minimize the diff, I plan on
removing it in a separate patch as it causes some instructions to be
shuffled around.

Similarly, this runs foldVMergeToMask before the other peepholes to
minimize the diff for now.

rvv-peephole-vmerge-vops-mir.ll was replaced with a dedicated
vmerge-peephole.mir test.
2025-07-04 23:02:31 +01:00
Craig Topper
d0d84c4150
[RISCV] Add SF_ to SiFive instructions in RISCVGenInstrInfo.inc. NFC (#146939) 2025-07-03 13:38:27 -07:00
UmeshKalappa
032966ff56
[RISCV] Added the MIPS prefetch extensions for MIPS RV64 P8700. (#145647)
the extension enabled with xmipscbop.

Please refer "MIPS RV64 P8700/P8700-F Multiprocessing System
Programmer’s Guide" for more info on the extension at
https://mips.com/wp-content/uploads/2025/06/P8700_Programmers_Reference_Manual_Rev1.84_5-31-2025.pdf
2025-07-03 10:59:10 +02:00
Craig Topper
4d7510c335
[RISCV] Remove untested code from SelectAddrRegRegScale. (#146185)
This code handled load/store addresses that are a SHL instruction. That
seems very unlikely to occur unless you're accessing an array that
starts at address 0. I'm not even sure if you can represent that in llvm
IR.
2025-06-27 21:55:11 -07:00
Jim Lin
842377882a
[RISCV] Select signed bitfield insert for XAndesPerf (#143356)
This patch is similar to #142737

The XAndesPerf extension includes signed bitfield extraction
instruction `NDS.BFOS, which can extract the bits from 0 to Len - 1,
place them starting at bit Lsb, zero-filled the bits from 0 to Lsb -1, 
and sign-extend the result.

When Lsb == Msb, it is a special case where the Lsb will be set to 0
instead of being equal to the Msb.
2025-06-11 13:32:49 +08:00
Sudharsan Veeravalli
94c241614f
[RISCV] Select signed bitfield extract for Xqcibm (#143536)
The Xqcibm Bit Manipulation extension has the `qc.ext` instruction that
can extract a subset of bits from the source register to the destination
register.

Unlike the corresponding instructions in `XTHeadbb` and `XAndesPerf`
which extract the bits between `Msb` and `Lsb`, the `qc.ext` instruction
extracts `width` bits from an offset that is determined by the `shamt`.
2025-06-10 21:30:56 +05:30
Piotr Fusik
39a7664fc1
[RISCV] Select (add/or C, x) -> (add.uw C|0xffffffff00000000, x) (#143375)
Emits fewer instructions for certain constants.
2025-06-10 13:28:49 +02:00
Sudharsan Veeravalli
34b985f635
[RISCV] Select unsigned bitfield extract for Xqcibm (#143354)
The Xqcibm Bit Manipulation extension has the `qc.extu` instruction that
can extract a subset of bits from the source register to the destination
register.

Unlike the corresponding instructions in XTHeadbb and XAndesPerf which
extract the bits between `Msb` and `Lsb`, the `qc.extu` instruction
extracts `width` bits from an offset that is determined by the `shamt`.
2025-06-10 09:38:01 +05:30
Craig Topper
58cce436d6 [RISCV] Pass SDLoc by const reference. NFC 2025-06-09 09:14:50 -07:00
Jim Lin
d395043300
[RISCV] Select unsigned bitfield insert for XAndesPerf (#142737)
The XAndesPerf extension includes unsigned bitfield extraction
instruction `NDS.BFOZ`, which can extract the bits from 0 to Len -1,
place them starting at bit Msb, and zero-fills the remaining bits.

This patch handles the cases where Msb < Lsb for `NDS.BFOZ`.

Instruction Sytax:

    nds.bfoz Rd, Rs1, Msb, Lsb

The operation is:

    if Msb < Lsb:
        Lenm1 = Lsb - Msb;
        Rd[Lsb:Msb] = Rs1[Lenm1:0];
        if (Lsb < (XLen -1)) Rd[XLen-1:Lsb+1]=0;
        Rd[Msb-1:0]=0;

When Len == 1, it is a special case where the Msb is set to 0 instead of
being equal to the Lsb.
2025-06-06 09:02:18 +08:00
Craig Topper
a23bd179cc
[RISCV] Remove artificial restriction on ShAmt from (shl (and X, C2), C) -> (srli (slli X, C4), C4-C) isel. (#143010)
This code unnecessarily inherited a `ShAmt <= 32` check from an earlier
pattern.
2025-06-05 17:48:35 -07:00
Piotr Fusik
afa517a3c2
[RISCV] Fold SRLIW+SLLI+ADD into SRLIW+SHXADD (#142611)
This handles RV64 SRLIW, similarly to SRLI.

Also fixing the comments for SRLI combines that did not match the code.
2025-06-04 17:32:40 +02:00
Jim Lin
f90cfb1350
[RISCV] Select signed bitfield extracts for XAndesPerf (#142303)
The XAndesPerf extension includes signed bitfield extraction
instruction `NDS.BFOS`, which can extract the bits from LSB to MSB,
places them starting at bit 0, and sign-extends the result.

The testcase includes the two patterns that can be selected as
signed bitfield extracts: `ashr+shl` and `ashr+sext_inreg`
2025-06-03 14:09:56 +08:00
Jim Lin
dd885f1f68
[RISCV] Select unsigned bitfield extracts for XAndesPerf (#141398)
The XAndesPerf extension includes unsigned bitfield extraction
instruction `NDS.BFOZ`, which can extract the bits from LSB to MSB, 
places them starting at bit 0, and zero-extends the result.

The testcase includes the three patterns that can be selected as
unsigned bitfield extracts: `and`, `and+lshr` and `lshr+and`
2025-05-29 10:52:33 +08:00
Craig Topper
cf3201c84d
[RISCV] Use X0_Pair for storing 0 using Zilsd. (#141847)
When we're creating a Zilsd store from a split i64 value, check if both
inputs are 0 and use X0_Pair instead of a REG_SEQUENCE.
2025-05-28 18:13:18 -07:00
Craig Topper
0b75a20990
[RISCV] Use X0_Pair for 0.0 and -0.0 with Zdinx. (#141641) 2025-05-27 21:52:20 -07:00
Craig Topper
540cf25a6d
[RISCV] Split f64 loads/stores for RV32+Zdinx during isel instead of post-RA. (#139840)
This avoids a bunch of complexity around making sure the offset doesn't
exceed 4093 so we can add 4 after splitting later. By splitting early,
the split loads/stores will get selected independently.

There's a bit of follow up work to do, particularly around splitting a
constant pool load. Overall I think this is cleaner with less edge
cases.
2025-05-15 08:35:43 -07:00
Craig Topper
bcf3654847
[RISCV] Lower i64 load/stores to ld/sd with Zilsd. (#139808)
Don't split i64 load/store when we have Zilsd.

In the future, we should enhanced the LoadStoreOptimizer pass to do
this, but this is a good starting point. Even if we support it in
LoadStoreOptimizer, we might still want this for volatile loads/stores
to guarantee the use of Zilsd.
2025-05-14 09:54:07 -07:00
Piotr Fusik
3cfdf2ccdf
[RISCV] Handle more (add x, C) -> (sub x, -C) cases (#138705)
This is a follow-up to #137309, adding:
- multi-use of the constant with different adds
- vectors (vadd.vx -> vsub.vx)
2025-05-13 09:12:24 +02:00
Sudharsan Veeravalli
595544b79d
[RISCV] Use QC_E_ADDI to improve codegen for icmp {eq, ne} with a constant (#139558)
Use the QC_E_ADDI instruction when the constant is not a signed 12 bit
value but is a signed 26 bit one. Don't use it if a single LUI
instruction can be used instead.
2025-05-13 07:46:16 +05:30
Sam Elliott
c60db55568
[RISCV] TableGen-erate RISC-V SDNodes (#138381)
This commit moves RISC-V to auto-generate its target-specific SDNode
types. The biggest change is that SDNodes can now be validated against
their expected type profiles, and that we don't need to edit several
different files when declaring a new one.

This takes Sergei's work in #119709 and "finishes" it - by moving the
final five RISCVISD opcodes into tablegen (including defining their
types), and by ensuring the tablegen has expected closing scope
comments.

Co-authored-by: Sergei Barannikov <barannikov88@gmail.com>
2025-05-09 12:36:59 -07:00
Piotr Fusik
2319a1eb81
[RISCV] Select (add x, C) -> (sub x, -C) if -C cheaper to materialize (#137309)
RV64 only. For 32-bit constants, a negated constant is never cheaper.

This change is similar to how #120221 selects inverted bitwise instructions.
2025-04-28 10:14:12 +02:00
Craig Topper
ff6a23d65e
[RISCV] Return false for Zalasr load/store in isWorthFoldingAdd. (#136799)
The Zalasr load/store don't support reg-imm addressing modes so they
can't fold an ADDI.
2025-04-23 14:47:42 -07:00
Jim Lin
698cd48db9
[RISCV] Fix Lsb > Msb case in (sra (sext_inreg X, _), C) for th.ext (#136287)
According the
[spec](https://github.com/XUANTIE-RV/thead-extension-spec/releases/tag/2.3.0),
the operation of `th.ext rd, rs1, msb, lsb` is

  reg[rd] := sign_extend(reg[rs1][msb:lsb])

The spec doesn't specify if lsb is greater than msb.

I don't think lsb can be greater than msb. So that If the shift-right
amount is greater than msb, we can set lsb equal to msb to extract the
bit rs1[msb] and sign-extend it.
2025-04-22 09:17:37 +08:00
Jim Lin
f88fd89909 [RISCV] Extract tryUnsignedBitfieldExtract as a member function of RISCVDAGToDAGISel. NFC. 2025-04-20 13:35:22 +08:00
Sudharsan Veeravalli
a6e61ce239
[RISCV] Remove duplicate check in SelectAddrRegImmLsb00000. NFC (#133372) 2025-03-28 12:39:59 +05:30
Craig Topper
b9666cf203
[RISCV] Reverse the order of Base and Offset in Core-V RegReg operand. (#133209)
This puts the base before the offset to match the order we use for base
ISA where the offset is an immediate.

I'm investigating using sub-operands for the base ISA loads and stores
too so having a consistent operand order will allow more sharing.
2025-03-27 07:12:54 -07:00
Philip Reames
fa315eceb7
[RISCV] Convert vsub.vx to vadd.vi if possible (#130669)
We'd already had this transform for the intrinsics, but hadn't added it
for either fixed length or scalable vectors coming from normal IR.

For the record, the fact we have three different sets of patterns here
really is quite ugly.
2025-03-10 16:20:14 -07:00