LLVM 24.0.0git
RISCVISelDAGToDAG.cpp
Go to the documentation of this file.
1//===-- RISCVISelDAGToDAG.cpp - A dag to dag inst selector for RISC-V -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an instruction selector for the RISC-V target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "RISCVISelDAGToDAG.h"
17#include "RISCVISelLowering.h"
18#include "RISCVInstrInfo.h"
21#include "llvm/IR/IntrinsicsRISCV.h"
23#include "llvm/Support/Debug.h"
26
27using namespace llvm;
28
29#define DEBUG_TYPE "riscv-isel"
30#define PASS_NAME "RISC-V DAG->DAG Pattern Instruction Selection"
31
32#define GET_DAGISEL_BODY RISCVDAGToDAGISel
33#include "RISCVGenDAGISel.inc"
34
36 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
37
38 bool MadeChange = false;
39 while (Position != CurDAG->allnodes_begin()) {
40 SDNode *N = &*--Position;
41 if (N->use_empty())
42 continue;
43
44 SDValue Result;
45 switch (N->getOpcode()) {
46 case ISD::SPLAT_VECTOR: {
47 if (Subtarget->hasStdExtP())
48 break;
49 // Convert integer SPLAT_VECTOR to VMV_V_X_VL and floating-point
50 // SPLAT_VECTOR to VFMV_V_F_VL to reduce isel burden.
51 MVT VT = N->getSimpleValueType(0);
52 unsigned Opc =
53 VT.isInteger() ? RISCVISD::VMV_V_X_VL : RISCVISD::VFMV_V_F_VL;
54 SDLoc DL(N);
55 SDValue VL = CurDAG->getRegister(RISCV::X0, Subtarget->getXLenVT());
56 SDValue Src = N->getOperand(0);
57 if (VT.isInteger())
58 Src = CurDAG->getNode(ISD::ANY_EXTEND, DL, Subtarget->getXLenVT(),
59 N->getOperand(0));
60 Result = CurDAG->getNode(Opc, DL, VT, CurDAG->getUNDEF(VT), Src, VL);
61 break;
62 }
63 case RISCVISD::SPLAT_VECTOR_SPLIT_I64_VL: {
64 // Lower SPLAT_VECTOR_SPLIT_I64 to two scalar stores and a stride 0 vector
65 // load. Done after lowering and combining so that we have a chance to
66 // optimize this to VMV_V_X_VL when the upper bits aren't needed.
67 assert(N->getNumOperands() == 4 && "Unexpected number of operands");
68 MVT VT = N->getSimpleValueType(0);
69 SDValue Passthru = N->getOperand(0);
70 SDValue Lo = N->getOperand(1);
71 SDValue Hi = N->getOperand(2);
72 SDValue VL = N->getOperand(3);
73 assert(VT.getVectorElementType() == MVT::i64 && VT.isScalableVector() &&
74 Lo.getValueType() == MVT::i32 && Hi.getValueType() == MVT::i32 &&
75 "Unexpected VTs!");
76 MachineFunction &MF = CurDAG->getMachineFunction();
77 SDLoc DL(N);
78
79 // Create temporary stack for each expanding node.
80 SDValue StackSlot =
81 CurDAG->CreateStackTemporary(TypeSize::getFixed(8), Align(8));
82 int FI = cast<FrameIndexSDNode>(StackSlot.getNode())->getIndex();
84
85 SDValue Chain = CurDAG->getEntryNode();
86 Lo = CurDAG->getStore(Chain, DL, Lo, StackSlot, MPI, Align(8));
87
88 SDValue OffsetSlot =
89 CurDAG->getMemBasePlusOffset(StackSlot, TypeSize::getFixed(4), DL);
90 Hi = CurDAG->getStore(Chain, DL, Hi, OffsetSlot, MPI.getWithOffset(4),
91 Align(8));
92
93 Chain = CurDAG->getNode(ISD::TokenFactor, DL, MVT::Other, Lo, Hi);
94
95 SDVTList VTs = CurDAG->getVTList({VT, MVT::Other});
96 SDValue IntID =
97 CurDAG->getTargetConstant(Intrinsic::riscv_vlse, DL, MVT::i64);
98 SDValue Ops[] = {Chain,
99 IntID,
100 Passthru,
101 StackSlot,
102 CurDAG->getRegister(RISCV::X0, MVT::i64),
103 VL};
104
105 Result = CurDAG->getMemIntrinsicNode(ISD::INTRINSIC_W_CHAIN, DL, VTs, Ops,
106 MVT::i64, MPI, Align(8),
108 break;
109 }
110 case ISD::FP_EXTEND: {
111 // We only have vector patterns for riscv_fpextend_vl in isel.
112 SDLoc DL(N);
113 MVT VT = N->getSimpleValueType(0);
114 if (!VT.isVector())
115 break;
116 SDValue VLMAX = CurDAG->getRegister(RISCV::X0, Subtarget->getXLenVT());
117 SDValue TrueMask = CurDAG->getNode(
118 RISCVISD::VMSET_VL, DL, VT.changeVectorElementType(MVT::i1), VLMAX);
119 Result = CurDAG->getNode(RISCVISD::FP_EXTEND_VL, DL, VT, N->getOperand(0),
120 TrueMask, VLMAX);
121 break;
122 }
123 case ISD::ADD: {
124 // Turn (add X, C) into (sub X, -C) when a constant node holding -C
125 // already exists in the DAG, so both share one materialization. Do this
126 // before selection, while both are still ConstantSDNodes: by selection
127 // time -C may already have been selected into instructions.
128 //
129 // ADD is commutative, but getNode canonicalizes constants to the RHS, so
130 // the constant is always operand 1.
131 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
132 if (!N1C)
133 break;
134 MVT VT = N->getSimpleValueType(0);
135 if (VT != Subtarget->getXLenVT())
136 break;
137 int64_t Imm = N1C->getSExtValue();
138 // Only worthwhile for wide constants: values that fit in 32 bits take at
139 // most two instructions to materialize, matching the threshold used by
140 // selectNegImm. Skip INT64_MIN too, whose negation is itself.
141 if (isInt<32>(Imm) || Imm == INT64_MIN)
142 break;
143 // A constant is anchored if it has a user other than an ADD, i.e. it is
144 // materialized regardless of this fold. N1C is the (unique) node for Imm,
145 // so the positive side needs no search.
146 auto IsAnchored = [](const SDNode *C) {
147 return any_of(C->users(), [](const SDNode *U) {
148 return U->getOpcode() != ISD::ADD;
149 });
150 };
151 // If Imm is materialized anyway, keep the ADD so it reuses Imm; an ADD is
152 // also more compressible than a SUB. This also lets us skip the search
153 // for -Imm below.
154 if (IsAnchored(N1C))
155 break;
156 // Find the (unique) constant node for -Imm, if any.
157 const SDNode *NegC = nullptr;
158 for (const SDNode &Node : CurDAG->allnodes()) {
160 if (C && C->getSimpleValueType(0) == VT && C->getSExtValue() == -Imm) {
161 NegC = &Node;
162 break;
163 }
164 }
165 // Reuse is only free if -Imm is already in the DAG.
166 if (!NegC)
167 break;
168 // dyn_cast<ConstantSDNode> also matches TargetConstant, which is encoded
169 // into the instruction rather than materialized, so reusing it would not
170 // remove a materialization. No TargetConstant is this wide (the largest
171 // are intrinsic IDs, which fit in 32 bits), so assert it is a Constant.
172 assert(NegC->getOpcode() == ISD::Constant &&
173 "Unexpected wide TargetConstant");
174 // Pick which of Imm/-Imm should be the surviving constant, so exactly
175 // one of the pair is materialized and any ADDs of the other reuse it:
176 // - if -Imm is materialized anyway, reuse it (rewrite to SUB);
177 // - else keep the cheaper constant, breaking ties towards the positive
178 // value so both ADDs of a C/-C pair agree on the survivor.
179 bool Rewrite;
180 if (IsAnchored(NegC)) {
181 Rewrite = true;
182 } else {
183 int PosCost = RISCVMatInt::getIntMatCost(APInt(64, Imm), 64, *Subtarget,
184 /*CompressionCost=*/true);
185 int NegCost =
186 RISCVMatInt::getIntMatCost(APInt(64, -Imm), 64, *Subtarget,
187 /*CompressionCost=*/true);
188 Rewrite = NegCost != PosCost ? NegCost < PosCost : Imm < 0;
189 }
190 if (!Rewrite)
191 break;
192 SDLoc DL(N);
193 // getConstant uniques onto the existing -C node, so it is shared.
194 Result = CurDAG->getNode(ISD::SUB, DL, VT, N->getOperand(0),
195 CurDAG->getConstant(-Imm, DL, VT));
196 break;
197 }
198 }
199
200 if (Result) {
201 LLVM_DEBUG(dbgs() << "RISC-V DAG preprocessing replacing:\nOld: ");
202 LLVM_DEBUG(N->dump(CurDAG));
203 LLVM_DEBUG(dbgs() << "\nNew: ");
204 LLVM_DEBUG(Result->dump(CurDAG));
205 LLVM_DEBUG(dbgs() << "\n");
206
207 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
208 MadeChange = true;
209 }
210 }
211
212 if (MadeChange)
213 CurDAG->RemoveDeadNodes();
214}
215
217 HandleSDNode Dummy(CurDAG->getRoot());
218 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
219
220 bool MadeChange = false;
221 while (Position != CurDAG->allnodes_begin()) {
222 SDNode *N = &*--Position;
223 // Skip dead nodes and any non-machine opcodes.
224 if (N->use_empty() || !N->isMachineOpcode())
225 continue;
226
227 MadeChange |= doPeepholeSExtW(N);
228
229 // FIXME: This is here only because the VMerge transform doesn't
230 // know how to handle masked true inputs. Once that has been moved
231 // to post-ISEL, this can be deleted as well.
232 MadeChange |= doPeepholeMaskedRVV(cast<MachineSDNode>(N));
233 }
234
235 CurDAG->setRoot(Dummy.getValue());
236
237 // After we're done with everything else, convert IMPLICIT_DEF
238 // passthru operands to NoRegister. This is required to workaround
239 // an optimization deficiency in MachineCSE. This really should
240 // be merged back into each of the patterns (i.e. there's no good
241 // reason not to go directly to NoReg), but is being done this way
242 // to allow easy backporting.
243 MadeChange |= doPeepholeNoRegPassThru();
244
245 if (MadeChange)
246 CurDAG->RemoveDeadNodes();
247}
248
249static SDValue selectImmSeq(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT,
251 SDValue SrcReg = CurDAG->getRegister(RISCV::X0, VT);
252 for (const RISCVMatInt::Inst &Inst : Seq) {
253 SDValue SDImm = CurDAG->getSignedTargetConstant(Inst.getImm(), DL, VT);
254 SDNode *Result = nullptr;
255 switch (Inst.getOpndKind()) {
256 case RISCVMatInt::Imm:
257 Result = CurDAG->getMachineNode(Inst.getOpcode(), DL, VT, SDImm);
258 break;
260 Result = CurDAG->getMachineNode(Inst.getOpcode(), DL, VT, SrcReg,
261 CurDAG->getRegister(RISCV::X0, VT));
262 break;
264 Result = CurDAG->getMachineNode(Inst.getOpcode(), DL, VT, SrcReg, SrcReg);
265 break;
267 Result = CurDAG->getMachineNode(Inst.getOpcode(), DL, VT, SrcReg, SDImm);
268 break;
269 }
270
271 // Only the first instruction has X0 as its source.
272 SrcReg = SDValue(Result, 0);
273 }
274
275 return SrcReg;
276}
277
278static SDValue selectImm(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT,
279 int64_t Imm, const RISCVSubtarget &Subtarget) {
281
282 // Use a rematerializable pseudo instruction for short sequences if enabled.
283 if (Seq.size() == 2 && Subtarget.getCLOpts().use_rematerializable_movimm)
284 return SDValue(
285 CurDAG->getMachineNode(RISCV::PseudoMovImm, DL, VT,
286 CurDAG->getSignedTargetConstant(Imm, DL, VT)),
287 0);
288
289 // See if we can create this constant as (ADD (SLLI X, C), X) where X is at
290 // worst an LUI+ADDIW. This will require an extra register, but avoids a
291 // constant pool.
292 // If we have Zba we can use (ADD_UW X, (SLLI X, 32)) to handle cases where
293 // low and high 32 bits are the same and bit 31 and 63 are set.
294 if (Seq.size() > 3) {
295 unsigned ShiftAmt, AddOpc;
298 if (!SeqLo.empty() && (SeqLo.size() + 2) < Seq.size()) {
299 SDValue Lo = selectImmSeq(CurDAG, DL, VT, SeqLo);
300
301 SDValue SLLI = SDValue(
302 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, Lo,
303 CurDAG->getTargetConstant(ShiftAmt, DL, VT)),
304 0);
305 return SDValue(CurDAG->getMachineNode(AddOpc, DL, VT, Lo, SLLI), 0);
306 }
307 }
308
309 // Otherwise, use the original sequence.
310 return selectImmSeq(CurDAG, DL, VT, Seq);
311}
312
314 SDNode *Node, unsigned Log2SEW, const SDLoc &DL, unsigned CurOp,
315 bool IsMasked, bool IsStridedOrIndexed, SmallVectorImpl<SDValue> &Operands,
316 bool IsLoad, MVT *IndexVT) {
317 SDValue Chain = Node->getOperand(0);
318
319 Operands.push_back(Node->getOperand(CurOp++)); // Base pointer.
320
321 if (IsStridedOrIndexed) {
322 Operands.push_back(Node->getOperand(CurOp++)); // Index.
323 if (IndexVT)
324 *IndexVT = Operands.back()->getSimpleValueType(0);
325 }
326
327 if (IsMasked) {
328 SDValue Mask = Node->getOperand(CurOp++);
329 Operands.push_back(Mask);
330 }
331 SDValue VL;
332 selectVLOp(Node->getOperand(CurOp++), VL);
333 Operands.push_back(VL);
334
335 MVT XLenVT = Subtarget->getXLenVT();
336 SDValue SEWOp = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
337 Operands.push_back(SEWOp);
338
339 // At the IR layer, all the masked load intrinsics have policy operands,
340 // none of the others do. All have passthru operands. For our pseudos,
341 // all loads have policy operands.
342 if (IsLoad) {
343 uint64_t Policy = RISCVVType::MASK_AGNOSTIC;
344 if (IsMasked)
345 Policy = Node->getConstantOperandVal(CurOp++);
346 SDValue PolicyOp = CurDAG->getTargetConstant(Policy, DL, XLenVT);
347 Operands.push_back(PolicyOp);
348 }
349
350 Operands.push_back(Chain); // Chain.
351}
352
353void RISCVDAGToDAGISel::selectVLSEG(SDNode *Node, unsigned NF, bool IsMasked,
354 bool IsStrided) {
355 SDLoc DL(Node);
356 MVT VT = Node->getSimpleValueType(0);
357 unsigned Log2SEW = Node->getConstantOperandVal(Node->getNumOperands() - 1);
359
360 unsigned CurOp = 2;
362
363 Operands.push_back(Node->getOperand(CurOp++));
364
365 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
366 Operands, /*IsLoad=*/true);
367
368 const RISCV::VLSEGPseudo *P =
369 RISCV::getVLSEGPseudo(NF, IsMasked, IsStrided, /*FF*/ false, Log2SEW,
370 static_cast<unsigned>(LMUL));
372 CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands);
373
374 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
375
378 CurDAG->RemoveDeadNode(Node);
379}
380
382 bool IsMasked) {
383 SDLoc DL(Node);
384 MVT VT = Node->getSimpleValueType(0);
385 MVT XLenVT = Subtarget->getXLenVT();
386 unsigned Log2SEW = Node->getConstantOperandVal(Node->getNumOperands() - 1);
388
389 unsigned CurOp = 2;
391
392 Operands.push_back(Node->getOperand(CurOp++));
393
394 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
395 /*IsStridedOrIndexed*/ false, Operands,
396 /*IsLoad=*/true);
397
398 const RISCV::VLSEGPseudo *P =
399 RISCV::getVLSEGPseudo(NF, IsMasked, /*Strided*/ false, /*FF*/ true,
400 Log2SEW, static_cast<unsigned>(LMUL));
401 MachineSDNode *Load = CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped,
402 XLenVT, MVT::Other, Operands);
403
404 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
405
406 ReplaceUses(SDValue(Node, 0), SDValue(Load, 0)); // Result
407 ReplaceUses(SDValue(Node, 1), SDValue(Load, 1)); // VL
408 ReplaceUses(SDValue(Node, 2), SDValue(Load, 2)); // Chain
409 CurDAG->RemoveDeadNode(Node);
410}
411
412void RISCVDAGToDAGISel::selectVLXSEG(SDNode *Node, unsigned NF, bool IsMasked,
413 bool IsOrdered) {
414 SDLoc DL(Node);
415 MVT VT = Node->getSimpleValueType(0);
416 unsigned Log2SEW = Node->getConstantOperandVal(Node->getNumOperands() - 1);
418
419 unsigned CurOp = 2;
421
422 Operands.push_back(Node->getOperand(CurOp++));
423
424 MVT IndexVT;
425 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
426 /*IsStridedOrIndexed*/ true, Operands,
427 /*IsLoad=*/true, &IndexVT);
428
429#ifndef NDEBUG
430 // Number of element = RVVBitsPerBlock * LMUL / SEW
431 unsigned ContainedTyNumElts = RISCV::RVVBitsPerBlock >> Log2SEW;
432 auto DecodedLMUL = RISCVVType::decodeVLMUL(LMUL);
433 if (DecodedLMUL.second)
434 ContainedTyNumElts /= DecodedLMUL.first;
435 else
436 ContainedTyNumElts *= DecodedLMUL.first;
437 assert(ContainedTyNumElts == IndexVT.getVectorMinNumElements() &&
438 "Element count mismatch");
439#endif
440
442 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
443 if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
444 reportFatalUsageError("The V extension does not support EEW=64 for index "
445 "values when XLEN=32");
446 }
447 const RISCV::VLXSEGPseudo *P = RISCV::getVLXSEGPseudo(
448 NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
449 static_cast<unsigned>(IndexLMUL));
451 CurDAG->getMachineNode(P->Pseudo, DL, MVT::Untyped, MVT::Other, Operands);
452
453 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
454
457 CurDAG->RemoveDeadNode(Node);
458}
459
460void RISCVDAGToDAGISel::selectVSSEG(SDNode *Node, unsigned NF, bool IsMasked,
461 bool IsStrided) {
462 SDLoc DL(Node);
463 MVT VT = Node->getOperand(2)->getSimpleValueType(0);
464 unsigned Log2SEW = Node->getConstantOperandVal(Node->getNumOperands() - 1);
466
467 unsigned CurOp = 2;
469
470 Operands.push_back(Node->getOperand(CurOp++));
471
472 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
473 Operands);
474
475 const RISCV::VSSEGPseudo *P = RISCV::getVSSEGPseudo(
476 NF, IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL));
478 CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands);
479
480 CurDAG->setNodeMemRefs(Store, {cast<MemSDNode>(Node)->getMemOperand()});
481
483}
484
485void RISCVDAGToDAGISel::selectVSXSEG(SDNode *Node, unsigned NF, bool IsMasked,
486 bool IsOrdered) {
487 SDLoc DL(Node);
488 MVT VT = Node->getOperand(2)->getSimpleValueType(0);
489 unsigned Log2SEW = Node->getConstantOperandVal(Node->getNumOperands() - 1);
491
492 unsigned CurOp = 2;
494
495 Operands.push_back(Node->getOperand(CurOp++));
496
497 MVT IndexVT;
498 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
499 /*IsStridedOrIndexed*/ true, Operands,
500 /*IsLoad=*/false, &IndexVT);
501
502#ifndef NDEBUG
503 // Number of element = RVVBitsPerBlock * LMUL / SEW
504 unsigned ContainedTyNumElts = RISCV::RVVBitsPerBlock >> Log2SEW;
505 auto DecodedLMUL = RISCVVType::decodeVLMUL(LMUL);
506 if (DecodedLMUL.second)
507 ContainedTyNumElts /= DecodedLMUL.first;
508 else
509 ContainedTyNumElts *= DecodedLMUL.first;
510 assert(ContainedTyNumElts == IndexVT.getVectorMinNumElements() &&
511 "Element count mismatch");
512#endif
513
515 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
516 if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
517 reportFatalUsageError("The V extension does not support EEW=64 for index "
518 "values when XLEN=32");
519 }
520 const RISCV::VSXSEGPseudo *P = RISCV::getVSXSEGPseudo(
521 NF, IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
522 static_cast<unsigned>(IndexLMUL));
524 CurDAG->getMachineNode(P->Pseudo, DL, Node->getValueType(0), Operands);
525
526 CurDAG->setNodeMemRefs(Store, {cast<MemSDNode>(Node)->getMemOperand()});
527
529}
530
532 if (!Subtarget->hasVInstructions())
533 return;
534
535 assert(Node->getOpcode() == ISD::INTRINSIC_WO_CHAIN && "Unexpected opcode");
536
537 SDLoc DL(Node);
538 MVT XLenVT = Subtarget->getXLenVT();
539
540 unsigned IntNo = Node->getConstantOperandVal(0);
541
542 assert((IntNo == Intrinsic::riscv_vsetvli ||
543 IntNo == Intrinsic::riscv_vsetvlimax) &&
544 "Unexpected vsetvli intrinsic");
545
546 bool VLMax = IntNo == Intrinsic::riscv_vsetvlimax;
547 unsigned Offset = (VLMax ? 1 : 2);
548
549 assert(Node->getNumOperands() == Offset + 2 &&
550 "Unexpected number of operands");
551
552 unsigned SEW =
553 RISCVVType::decodeVSEW(Node->getConstantOperandVal(Offset) & 0x7);
554 RISCVVType::VLMUL VLMul = static_cast<RISCVVType::VLMUL>(
555 Node->getConstantOperandVal(Offset + 1) & 0x7);
556
557 unsigned VTypeI = RISCVVType::encodeVTYPE(VLMul, SEW, /*TailAgnostic*/ true,
558 /*MaskAgnostic*/ true);
559 SDValue VTypeIOp = CurDAG->getTargetConstant(VTypeI, DL, XLenVT);
560
561 SDValue VLOperand;
562 unsigned Opcode = RISCV::PseudoVSETVLI;
563 if (auto *C = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
564 if (auto VLEN = Subtarget->getRealVLen())
565 if (*VLEN / RISCVVType::getSEWLMULRatio(SEW, VLMul) == C->getZExtValue())
566 VLMax = true;
567 }
568 if (VLMax || isAllOnesConstant(Node->getOperand(1))) {
569 VLOperand = CurDAG->getRegister(RISCV::X0, XLenVT);
570 Opcode = RISCV::PseudoVSETVLIX0;
571 } else {
572 VLOperand = Node->getOperand(1);
573
574 if (auto *C = dyn_cast<ConstantSDNode>(VLOperand)) {
575 uint64_t AVL = C->getZExtValue();
576 if (isUInt<5>(AVL)) {
577 SDValue VLImm = CurDAG->getTargetConstant(AVL, DL, XLenVT);
578 ReplaceNode(Node, CurDAG->getMachineNode(RISCV::PseudoVSETIVLI, DL,
579 XLenVT, VLImm, VTypeIOp));
580 return;
581 }
582 }
583 }
584
586 CurDAG->getMachineNode(Opcode, DL, XLenVT, VLOperand, VTypeIOp));
587}
588
590 if (!Subtarget->hasVendorXSfmmbase())
591 return;
592
593 assert(Node->getOpcode() == ISD::INTRINSIC_WO_CHAIN && "Unexpected opcode");
594
595 SDLoc DL(Node);
596 MVT XLenVT = Subtarget->getXLenVT();
597
598 unsigned IntNo = Node->getConstantOperandVal(0);
599
600 assert((IntNo == Intrinsic::riscv_sf_vsettnt ||
601 IntNo == Intrinsic::riscv_sf_vsettm ||
602 IntNo == Intrinsic::riscv_sf_vsettk) &&
603 "Unexpected XSfmm vset intrinsic");
604
605 unsigned SEW = RISCVVType::decodeVSEW(Node->getConstantOperandVal(2));
606 unsigned Widen = RISCVVType::decodeTWiden(Node->getConstantOperandVal(3));
607 unsigned PseudoOpCode =
608 IntNo == Intrinsic::riscv_sf_vsettnt ? RISCV::PseudoSF_VSETTNT
609 : IntNo == Intrinsic::riscv_sf_vsettm ? RISCV::PseudoSF_VSETTM
610 : RISCV::PseudoSF_VSETTK;
611
612 if (IntNo == Intrinsic::riscv_sf_vsettnt) {
613 unsigned VTypeI = RISCVVType::encodeXSfmmVType(SEW, Widen, 0);
614 SDValue VTypeIOp = CurDAG->getTargetConstant(VTypeI, DL, XLenVT);
615
616 ReplaceNode(Node, CurDAG->getMachineNode(PseudoOpCode, DL, XLenVT,
617 Node->getOperand(1), VTypeIOp));
618 } else {
619 SDValue Log2SEW = CurDAG->getTargetConstant(Log2_32(SEW), DL, XLenVT);
620 SDValue TWiden = CurDAG->getTargetConstant(Widen, DL, XLenVT);
622 CurDAG->getMachineNode(PseudoOpCode, DL, XLenVT,
623 Node->getOperand(1), Log2SEW, TWiden));
624 }
625}
626
628 MVT VT = Node->getSimpleValueType(0);
629 unsigned Opcode = Node->getOpcode();
630 assert((Opcode == ISD::AND || Opcode == ISD::OR || Opcode == ISD::XOR) &&
631 "Unexpected opcode");
632 SDLoc DL(Node);
633
634 // For operations of the form (x << C1) op C2, check if we can use
635 // ANDI/ORI/XORI by transforming it into (x op (C2>>C1)) << C1.
636 SDValue N0 = Node->getOperand(0);
637 SDValue N1 = Node->getOperand(1);
638
640 if (!Cst)
641 return false;
642
643 int64_t Val = Cst->getSExtValue();
644
645 // Check if immediate can already use ANDI/ORI/XORI.
646 if (isInt<12>(Val))
647 return false;
648
649 SDValue Shift = N0;
650
651 // If Val is simm32 and we have a sext_inreg from i32, then the binop
652 // produces at least 33 sign bits. We can peek through the sext_inreg and use
653 // a SLLIW at the end.
654 bool SignExt = false;
655 if (isInt<32>(Val) && N0.getOpcode() == ISD::SIGN_EXTEND_INREG &&
656 N0.hasOneUse() && cast<VTSDNode>(N0.getOperand(1))->getVT() == MVT::i32) {
657 SignExt = true;
658 Shift = N0.getOperand(0);
659 }
660
661 if (Shift.getOpcode() != ISD::SHL || !Shift.hasOneUse())
662 return false;
663
665 if (!ShlCst)
666 return false;
667
668 uint64_t ShAmt = ShlCst->getZExtValue();
669 assert(ShAmt > 0 && ShAmt < Subtarget->getXLen() &&
670 "Unexpected shift amount!");
671
672 // Make sure that we don't change the operation by removing bits.
673 // This only matters for OR and XOR, AND is unaffected.
674 uint64_t RemovedBitsMask = maskTrailingOnes<uint64_t>(ShAmt);
675 if (Opcode != ISD::AND && (Val & RemovedBitsMask) != 0)
676 return false;
677
678 int64_t ShiftedVal = Val >> ShAmt;
679 if (!isInt<12>(ShiftedVal))
680 return false;
681
682 // If we peeked through a sext_inreg, make sure the shift is valid for SLLIW.
683 if (SignExt && ShAmt >= 32)
684 return false;
685
686 // Ok, we can reorder to get a smaller immediate.
687 unsigned BinOpc;
688 switch (Opcode) {
689 default: llvm_unreachable("Unexpected opcode");
690 case ISD::AND: BinOpc = RISCV::ANDI; break;
691 case ISD::OR: BinOpc = RISCV::ORI; break;
692 case ISD::XOR: BinOpc = RISCV::XORI; break;
693 }
694
695 unsigned ShOpc = SignExt ? RISCV::SLLIW : RISCV::SLLI;
696
697 SDNode *BinOp = CurDAG->getMachineNode(
698 BinOpc, DL, VT, Shift.getOperand(0),
699 CurDAG->getSignedTargetConstant(ShiftedVal, DL, VT));
700 SDNode *SLLI =
701 CurDAG->getMachineNode(ShOpc, DL, VT, SDValue(BinOp, 0),
702 CurDAG->getTargetConstant(ShAmt, DL, VT));
703 ReplaceNode(Node, SLLI);
704 return true;
705}
706
708 unsigned Opc;
709
710 if (Subtarget->hasVendorXTHeadBb())
711 Opc = RISCV::TH_EXT;
712 else if (Subtarget->hasVendorXAndesPerf())
713 Opc = RISCV::NDS_BFOS;
714 else if (Subtarget->hasVendorXqcibm())
715 Opc = RISCV::QC_EXT;
716 else
717 // Only supported with XTHeadBb/XAndesPerf/Xqcibm at the moment.
718 return false;
719
720 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
721 if (!N1C)
722 return false;
723
724 SDValue N0 = Node->getOperand(0);
725 if (!N0.hasOneUse())
726 return false;
727
728 auto BitfieldExtract = [&](SDValue N0, unsigned Msb, unsigned Lsb,
729 const SDLoc &DL, MVT VT) {
730 if (Opc == RISCV::QC_EXT) {
731 // QC.EXT X, width, shamt
732 // shamt is the same as Lsb
733 // width is the number of bits to extract from the Lsb
734 Msb = Msb - Lsb + 1;
735 }
736 return CurDAG->getMachineNode(Opc, DL, VT, N0.getOperand(0),
737 CurDAG->getTargetConstant(Msb, DL, VT),
738 CurDAG->getTargetConstant(Lsb, DL, VT));
739 };
740
741 SDLoc DL(Node);
742 MVT VT = Node->getSimpleValueType(0);
743 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
744 const unsigned RightShAmt = N1C->getZExtValue();
745 assert(RightShAmt > 0 && RightShAmt < VT.getSizeInBits() &&
746 "Unexpected shift amount!");
747
748 // Transform (sra (shl X, C1) C2) with C1 < C2
749 // -> (SignedBitfieldExtract X, msb, lsb)
750 if (N0.getOpcode() == ISD::SHL) {
751 auto *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
752 if (!N01C)
753 return false;
754
755 const unsigned LeftShAmt = N01C->getZExtValue();
756 assert(LeftShAmt > 0 && LeftShAmt < VT.getSizeInBits() &&
757 "Unexpected shift amount!");
758 // Make sure that this is a bitfield extraction (i.e., the shift-right
759 // amount can not be less than the left-shift).
760 if (LeftShAmt > RightShAmt)
761 return false;
762
763 const unsigned MsbPlusOne = VT.getSizeInBits() - LeftShAmt;
764 const unsigned Msb = MsbPlusOne - 1;
765 const unsigned Lsb = RightShAmt - LeftShAmt;
766
767 SDNode *Sbe = BitfieldExtract(N0, Msb, Lsb, DL, VT);
768 ReplaceNode(Node, Sbe);
769 return true;
770 }
771
772 // Transform (sra (sext_inreg X, _), C) ->
773 // (SignedBitfieldExtract X, msb, lsb)
774 if (N0.getOpcode() == ISD::SIGN_EXTEND_INREG) {
775 unsigned ExtSize =
776 cast<VTSDNode>(N0.getOperand(1))->getVT().getSizeInBits();
777
778 // ExtSize of 32 should use sraiw via tablegen pattern.
779 if (ExtSize == 32)
780 return false;
781
782 const unsigned Msb = ExtSize - 1;
783 // If the shift-right amount is greater than Msb, it means that extracts
784 // the X[Msb] bit and sign-extend it.
785 const unsigned Lsb = RightShAmt > Msb ? Msb : RightShAmt;
786
787 SDNode *Sbe = BitfieldExtract(N0, Msb, Lsb, DL, VT);
788 ReplaceNode(Node, Sbe);
789 return true;
790 }
791
792 return false;
793}
794
796 // Only supported with XAndesPerf at the moment.
797 if (!Subtarget->hasVendorXAndesPerf())
798 return false;
799
800 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
801 if (!N1C)
802 return false;
803
804 SDValue N0 = Node->getOperand(0);
805 if (!N0.hasOneUse())
806 return false;
807
808 auto BitfieldInsert = [&](SDValue N0, unsigned Msb, unsigned Lsb,
809 const SDLoc &DL, MVT VT) {
810 unsigned Opc = RISCV::NDS_BFOS;
811 // If the Lsb is equal to the Msb, then the Lsb should be 0.
812 if (Lsb == Msb)
813 Lsb = 0;
814 return CurDAG->getMachineNode(Opc, DL, VT, N0.getOperand(0),
815 CurDAG->getTargetConstant(Lsb, DL, VT),
816 CurDAG->getTargetConstant(Msb, DL, VT));
817 };
818
819 SDLoc DL(Node);
820 MVT VT = Node->getSimpleValueType(0);
821 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
822 const unsigned RightShAmt = N1C->getZExtValue();
823 assert(RightShAmt > 0 && RightShAmt < VT.getSizeInBits() &&
824 "Unexpected shift amount!");
825
826 // Transform (sra (shl X, C1) C2) with C1 > C2
827 // -> (NDS.BFOS X, lsb, msb)
828 if (N0.getOpcode() == ISD::SHL) {
829 auto *N01C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
830 if (!N01C)
831 return false;
832
833 const unsigned LeftShAmt = N01C->getZExtValue();
834 assert(LeftShAmt > 0 && LeftShAmt < VT.getSizeInBits() &&
835 "Unexpected shift amount!");
836 // Make sure that this is a bitfield insertion (i.e., the shift-right
837 // amount should be less than the left-shift).
838 if (LeftShAmt <= RightShAmt)
839 return false;
840
841 const unsigned MsbPlusOne = VT.getSizeInBits() - RightShAmt;
842 const unsigned Msb = MsbPlusOne - 1;
843 const unsigned Lsb = LeftShAmt - RightShAmt;
844
845 SDNode *Sbi = BitfieldInsert(N0, Msb, Lsb, DL, VT);
846 ReplaceNode(Node, Sbi);
847 return true;
848 }
849
850 return false;
851}
852
854 const SDLoc &DL, MVT VT,
855 SDValue X, unsigned Msb,
856 unsigned Lsb) {
857 unsigned Opc;
858
859 if (Subtarget->hasVendorXTHeadBb()) {
860 Opc = RISCV::TH_EXTU;
861 } else if (Subtarget->hasVendorXAndesPerf()) {
862 Opc = RISCV::NDS_BFOZ;
863 } else if (Subtarget->hasVendorXqcibm()) {
864 Opc = RISCV::QC_EXTU;
865 // QC.EXTU X, width, shamt
866 // shamt is the same as Lsb
867 // width is the number of bits to extract from the Lsb
868 Msb = Msb - Lsb + 1;
869 } else {
870 // Only supported with XTHeadBb/XAndesPerf/Xqcibm at the moment.
871 return false;
872 }
873
874 SDNode *Ube = CurDAG->getMachineNode(Opc, DL, VT, X,
875 CurDAG->getTargetConstant(Msb, DL, VT),
876 CurDAG->getTargetConstant(Lsb, DL, VT));
877 ReplaceNode(Node, Ube);
878 return true;
879}
880
882 const SDLoc &DL, MVT VT,
883 SDValue X, unsigned Msb,
884 unsigned Lsb) {
885 // Only supported with XAndesPerf at the moment.
886 if (!Subtarget->hasVendorXAndesPerf())
887 return false;
888
889 unsigned Opc = RISCV::NDS_BFOZ;
890
891 // If the Lsb is equal to the Msb, then the Lsb should be 0.
892 if (Lsb == Msb)
893 Lsb = 0;
894 SDNode *Ubi = CurDAG->getMachineNode(Opc, DL, VT, X,
895 CurDAG->getTargetConstant(Lsb, DL, VT),
896 CurDAG->getTargetConstant(Msb, DL, VT));
897 ReplaceNode(Node, Ubi);
898 return true;
899}
900
902 // Target does not support indexed loads.
903 if (!Subtarget->hasVendorXTHeadMemIdx())
904 return false;
905
907 ISD::MemIndexedMode AM = Ld->getAddressingMode();
908 if (AM == ISD::UNINDEXED)
909 return false;
910
911 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Ld->getOffset());
912 if (!C)
913 return false;
914
915 EVT LoadVT = Ld->getMemoryVT();
916 assert((AM == ISD::PRE_INC || AM == ISD::POST_INC) &&
917 "Unexpected addressing mode");
918 bool IsPre = AM == ISD::PRE_INC;
919 bool IsPost = AM == ISD::POST_INC;
920 int64_t Offset = C->getSExtValue();
921
922 // The constants that can be encoded in the THeadMemIdx instructions
923 // are of the form (sign_extend(imm5) << imm2).
924 unsigned Shift;
925 for (Shift = 0; Shift < 4; Shift++)
926 if (isInt<5>(Offset >> Shift) && ((Offset % (1LL << Shift)) == 0))
927 break;
928
929 // Constant cannot be encoded.
930 if (Shift == 4)
931 return false;
932
933 bool IsZExt = (Ld->getExtensionType() == ISD::ZEXTLOAD);
934 unsigned Opcode;
935 if (LoadVT == MVT::i8 && IsPre)
936 Opcode = IsZExt ? RISCV::TH_LBUIB : RISCV::TH_LBIB;
937 else if (LoadVT == MVT::i8 && IsPost)
938 Opcode = IsZExt ? RISCV::TH_LBUIA : RISCV::TH_LBIA;
939 else if (LoadVT == MVT::i16 && IsPre)
940 Opcode = IsZExt ? RISCV::TH_LHUIB : RISCV::TH_LHIB;
941 else if (LoadVT == MVT::i16 && IsPost)
942 Opcode = IsZExt ? RISCV::TH_LHUIA : RISCV::TH_LHIA;
943 else if (LoadVT == MVT::i32 && IsPre)
944 Opcode = IsZExt ? RISCV::TH_LWUIB : RISCV::TH_LWIB;
945 else if (LoadVT == MVT::i32 && IsPost)
946 Opcode = IsZExt ? RISCV::TH_LWUIA : RISCV::TH_LWIA;
947 else if (LoadVT == MVT::i64 && IsPre)
948 Opcode = RISCV::TH_LDIB;
949 else if (LoadVT == MVT::i64 && IsPost)
950 Opcode = RISCV::TH_LDIA;
951 else
952 return false;
953
954 EVT Ty = Ld->getOffset().getValueType();
955 SDValue Ops[] = {
956 Ld->getBasePtr(),
957 CurDAG->getSignedTargetConstant(Offset >> Shift, SDLoc(Node), Ty),
958 CurDAG->getTargetConstant(Shift, SDLoc(Node), Ty), Ld->getChain()};
959 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(Node), Ld->getValueType(0),
960 Ld->getValueType(1), MVT::Other, Ops);
961
962 MachineMemOperand *MemOp = cast<MemSDNode>(Node)->getMemOperand();
963 CurDAG->setNodeMemRefs(cast<MachineSDNode>(New), {MemOp});
964
965 ReplaceNode(Node, New);
966
967 return true;
968}
969
970static SDValue buildGPRPair(SelectionDAG *CurDAG, const SDLoc &DL, MVT VT,
971 SDValue Lo, SDValue Hi) {
972 SDValue Ops[] = {
973 CurDAG->getTargetConstant(RISCV::GPRPairNoX0RegClassID, DL, MVT::i32), Lo,
974 CurDAG->getTargetConstant(RISCV::sub_gpr_even, DL, MVT::i32), Hi,
975 CurDAG->getTargetConstant(RISCV::sub_gpr_odd, DL, MVT::i32)};
976
977 return SDValue(
978 CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops), 0);
979}
980
981// Helper to extract Lo and Hi values from a GPR pair.
982static std::pair<SDValue, SDValue>
984 SDValue Lo =
985 CurDAG->getTargetExtractSubreg(RISCV::sub_gpr_even, DL, MVT::i32, Pair);
986 SDValue Hi =
987 CurDAG->getTargetExtractSubreg(RISCV::sub_gpr_odd, DL, MVT::i32, Pair);
988 return {Lo, Hi};
989}
990
991// Try to match WMACC pattern: ADDD where one operand pair comes from a
992// widening multiply (both results of UMUL_LOHI, SMUL_LOHI, or WMULSU).
994 assert(Node->getOpcode() == RISCVISD::ADDD && "Expected ADDD");
995
996 SDValue Op0Lo = Node->getOperand(0);
997 SDValue Op0Hi = Node->getOperand(1);
998 SDValue Op1Lo = Node->getOperand(2);
999 SDValue Op1Hi = Node->getOperand(3);
1000
1001 auto IsSupportedMulWithOneUse = [](SDValue Lo, SDValue Hi) {
1002 unsigned Opc = Lo.getOpcode();
1003 if (Opc != ISD::UMUL_LOHI && Opc != ISD::SMUL_LOHI &&
1004 Opc != RISCVISD::WMULSU)
1005 return false;
1006 return Lo.getNode() == Hi.getNode() && Lo.getResNo() == 0 &&
1007 Hi.getResNo() == 1 && Lo.hasOneUse() && Hi.hasOneUse();
1008 };
1009
1010 SDNode *MulNode = nullptr;
1011 SDValue AddLo, AddHi;
1012
1013 // Check if first operand pair is a supported multiply with single use.
1014 if (IsSupportedMulWithOneUse(Op0Lo, Op0Hi)) {
1015 MulNode = Op0Lo.getNode();
1016 AddLo = Op1Lo;
1017 AddHi = Op1Hi;
1018 }
1019 // ADDD is commutative. Check if second operand pair is a supported multiply
1020 // with single use.
1021 else if (IsSupportedMulWithOneUse(Op1Lo, Op1Hi)) {
1022 MulNode = Op1Lo.getNode();
1023 AddLo = Op0Lo;
1024 AddHi = Op0Hi;
1025 } else {
1026 return false;
1027 }
1028
1029 unsigned Opc;
1030 switch (MulNode->getOpcode()) {
1031 default:
1032 llvm_unreachable("Unexpected multiply opcode");
1033 case ISD::UMUL_LOHI:
1034 Opc = RISCV::WMACCU;
1035 break;
1036 case ISD::SMUL_LOHI:
1037 Opc = RISCV::WMACC;
1038 break;
1039 case RISCVISD::WMULSU:
1040 Opc = RISCV::WMACCSU;
1041 break;
1042 }
1043
1044 SDValue Acc = buildGPRPair(CurDAG, DL, MVT::Untyped, AddLo, AddHi);
1045
1046 // WMACC instruction format: rd, rs1, rs2 (rd is accumulator).
1047 SDValue M0 = MulNode->getOperand(0);
1048 SDValue M1 = MulNode->getOperand(1);
1049 MachineSDNode *New =
1050 CurDAG->getMachineNode(Opc, DL, MVT::Untyped, Acc, M0, M1);
1051
1052 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(New, 0));
1053 ReplaceUses(SDValue(Node, 0), Lo);
1054 ReplaceUses(SDValue(Node, 1), Hi);
1055 CurDAG->RemoveDeadNode(Node);
1056 return true;
1057}
1058
1060 assert(TileNum <= 15 && "Invalid tile number");
1061 return RISCV::T0 + TileNum;
1062}
1063
1065 if (!Subtarget->hasVInstructions())
1066 return;
1067
1068 assert(Node->getOpcode() == ISD::INTRINSIC_VOID && "Unexpected opcode");
1069
1070 SDLoc DL(Node);
1071 unsigned IntNo = Node->getConstantOperandVal(1);
1072
1073 assert((IntNo == Intrinsic::riscv_sf_vc_x_se ||
1074 IntNo == Intrinsic::riscv_sf_vc_i_se) &&
1075 "Unexpected vsetvli intrinsic");
1076
1077 // imm, imm, imm, simm5/scalar, sew, log2lmul, vl
1078 unsigned Log2SEW = Log2_32(Node->getConstantOperandVal(6));
1079 SDValue SEWOp =
1080 CurDAG->getTargetConstant(Log2SEW, DL, Subtarget->getXLenVT());
1081 SmallVector<SDValue, 8> Operands = {Node->getOperand(2), Node->getOperand(3),
1082 Node->getOperand(4), Node->getOperand(5),
1083 Node->getOperand(8), SEWOp,
1084 Node->getOperand(0)};
1085
1086 unsigned Opcode;
1087 auto *LMulSDNode = cast<ConstantSDNode>(Node->getOperand(7));
1088 switch (LMulSDNode->getSExtValue()) {
1089 case 5:
1090 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_MF8
1091 : RISCV::PseudoSF_VC_I_SE_MF8;
1092 break;
1093 case 6:
1094 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_MF4
1095 : RISCV::PseudoSF_VC_I_SE_MF4;
1096 break;
1097 case 7:
1098 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_MF2
1099 : RISCV::PseudoSF_VC_I_SE_MF2;
1100 break;
1101 case 0:
1102 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_M1
1103 : RISCV::PseudoSF_VC_I_SE_M1;
1104 break;
1105 case 1:
1106 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_M2
1107 : RISCV::PseudoSF_VC_I_SE_M2;
1108 break;
1109 case 2:
1110 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_M4
1111 : RISCV::PseudoSF_VC_I_SE_M4;
1112 break;
1113 case 3:
1114 Opcode = IntNo == Intrinsic::riscv_sf_vc_x_se ? RISCV::PseudoSF_VC_X_SE_M8
1115 : RISCV::PseudoSF_VC_I_SE_M8;
1116 break;
1117 }
1118
1119 ReplaceNode(Node, CurDAG->getMachineNode(
1120 Opcode, DL, Node->getSimpleValueType(0), Operands));
1121}
1122
1123static unsigned getSegInstNF(unsigned Intrinsic) {
1124#define INST_NF_CASE(NAME, NF) \
1125 case Intrinsic::riscv_##NAME##NF: \
1126 return NF;
1127#define INST_NF_CASE_MASK(NAME, NF) \
1128 case Intrinsic::riscv_##NAME##NF##_mask: \
1129 return NF;
1130#define INST_NF_CASE_FF(NAME, NF) \
1131 case Intrinsic::riscv_##NAME##NF##ff: \
1132 return NF;
1133#define INST_NF_CASE_FF_MASK(NAME, NF) \
1134 case Intrinsic::riscv_##NAME##NF##ff_mask: \
1135 return NF;
1136#define INST_ALL_NF_CASE_BASE(MACRO_NAME, NAME) \
1137 MACRO_NAME(NAME, 2) \
1138 MACRO_NAME(NAME, 3) \
1139 MACRO_NAME(NAME, 4) \
1140 MACRO_NAME(NAME, 5) \
1141 MACRO_NAME(NAME, 6) \
1142 MACRO_NAME(NAME, 7) \
1143 MACRO_NAME(NAME, 8)
1144#define INST_ALL_NF_CASE(NAME) \
1145 INST_ALL_NF_CASE_BASE(INST_NF_CASE, NAME) \
1146 INST_ALL_NF_CASE_BASE(INST_NF_CASE_MASK, NAME)
1147#define INST_ALL_NF_CASE_WITH_FF(NAME) \
1148 INST_ALL_NF_CASE(NAME) \
1149 INST_ALL_NF_CASE_BASE(INST_NF_CASE_FF, NAME) \
1150 INST_ALL_NF_CASE_BASE(INST_NF_CASE_FF_MASK, NAME)
1151 switch (Intrinsic) {
1152 default:
1153 llvm_unreachable("Unexpected segment load/store intrinsic");
1155 INST_ALL_NF_CASE(vlsseg)
1156 INST_ALL_NF_CASE(vloxseg)
1157 INST_ALL_NF_CASE(vluxseg)
1158 INST_ALL_NF_CASE(vsseg)
1159 INST_ALL_NF_CASE(vssseg)
1160 INST_ALL_NF_CASE(vsoxseg)
1161 INST_ALL_NF_CASE(vsuxseg)
1162 }
1163}
1164
1165static bool isApplicableToPLIOrPLUI(int Val) {
1166 // Check if the immediate is packed i8 or i10
1167 int16_t Bit31To16 = Val >> 16;
1168 int16_t Bit15To0 = Val;
1169 int8_t Bit15To8 = Bit15To0 >> 8;
1170 int8_t Bit7To0 = Val;
1171 if (Bit31To16 != Bit15To0)
1172 return false;
1173
1174 return isInt<10>(Bit15To0) || isShiftedInt<10, 6>(Bit15To0) ||
1175 Bit15To8 == Bit7To0;
1176}
1177
1179 // If we have a custom node, we have already selected.
1180 if (Node->isMachineOpcode()) {
1181 LLVM_DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
1182 Node->setNodeId(-1);
1183 return;
1184 }
1185
1186 // Instruction Selection not handled by the auto-generated tablegen selection
1187 // should be handled here.
1188 unsigned Opcode = Node->getOpcode();
1189 MVT XLenVT = Subtarget->getXLenVT();
1190 SDLoc DL(Node);
1191 MVT VT = Node->getSimpleValueType(0);
1192
1193 bool HasBitTest = Subtarget->hasBEXTILike();
1194
1195 switch (Opcode) {
1196 case ISD::Constant: {
1197 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
1198 auto *ConstNode = cast<ConstantSDNode>(Node);
1199 if (ConstNode->isZero()) {
1200 SDValue New =
1201 CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, RISCV::X0, VT);
1202 ReplaceNode(Node, New.getNode());
1203 return;
1204 }
1205 int64_t Imm = ConstNode->getSExtValue();
1206 // If only the lower 8 bits are used, try to convert this to a simm6 by
1207 // sign-extending bit 7. This is neutral without the C extension, and
1208 // allows C.LI to be used if C is present.
1212 // If the upper XLen-16 bits are not used, try to convert this to a simm12
1213 // by sign extending bit 15.
1214 else if (!isInt<16>(Imm) && isUInt<16>(Imm) &&
1217
1218 // If the upper XLen-16 bits are not used, the lower 2 bytes are the same,
1219 // and we can't use li, convert to an xlen splat so we can use pli.b.
1220 if (Subtarget->hasStdExtP() && !isInt<12>(Imm) &&
1221 (Imm & 0xff) == ((Imm >> 8) & 0xff) && hasAllHUsers(Node)) {
1222 // Splat the lower 16 bits to XLen. Sign extend for RV32.
1223 uint64_t Splat = Imm & 0xffff;
1224 Splat = (Splat << 16) | Splat;
1225 if (VT == MVT::i64)
1226 Imm = Splat << 32 | Splat;
1227 else
1229 } else {
1230 // If the upper 32-bits are not used try to convert this into a simm32 by
1231 // sign extending bit 32.
1234
1235 if (VT == MVT::i64 && !isInt<12>(Imm) && !isShiftedInt<20, 12>(Imm) &&
1236 Subtarget->hasStdExtP() && isApplicableToPLIOrPLUI(Imm) &&
1237 hasAllWUsers(Node)) {
1238 // If it's 4 packed 8-bit integers or 2 packed signed 16-bit integers,
1239 // we can simply copy lower 32 bits to higher 32 bits to make it able to
1240 // rematerialize to PLI_B or PLI_H
1241 Imm = ((uint64_t)Imm << 32) | (Imm & 0xFFFFFFFF);
1242 }
1243 }
1244
1245 ReplaceNode(Node, selectImm(CurDAG, DL, VT, Imm, *Subtarget).getNode());
1246 return;
1247 }
1248 case ISD::ConstantFP: {
1249 const APFloat &APF = cast<ConstantFPSDNode>(Node)->getValueAPF();
1250
1251 bool Is64Bit = Subtarget->is64Bit();
1252 bool HasZdinx = Subtarget->hasStdExtZdinx();
1253
1254 bool NegZeroF64 = APF.isNegZero() && VT == MVT::f64;
1255 SDValue Imm;
1256 // For +0.0 or f64 -0.0 we need to start from X0. For all others, we will
1257 // create an integer immediate.
1258 if (APF.isPosZero() || NegZeroF64) {
1259 if (VT == MVT::f64 && HasZdinx && !Is64Bit)
1260 Imm = CurDAG->getRegister(RISCV::X0_Pair, MVT::f64);
1261 else
1262 Imm = CurDAG->getRegister(RISCV::X0, XLenVT);
1263 } else {
1264 Imm = selectImm(CurDAG, DL, XLenVT, APF.bitcastToAPInt().getSExtValue(),
1265 *Subtarget);
1266 }
1267
1268 unsigned Opc;
1269 switch (VT.SimpleTy) {
1270 default:
1271 llvm_unreachable("Unexpected size");
1272 case MVT::bf16:
1273 assert(Subtarget->hasStdExtZfbfmin());
1274 Opc = RISCV::FMV_H_X;
1275 break;
1276 case MVT::f16:
1277 Opc = Subtarget->hasStdExtZhinxmin() ? RISCV::COPY : RISCV::FMV_H_X;
1278 break;
1279 case MVT::f32:
1280 Opc = Subtarget->hasStdExtZfinx() ? RISCV::COPY : RISCV::FMV_W_X;
1281 break;
1282 case MVT::f64:
1283 // For RV32, we can't move from a GPR, we need to convert instead. This
1284 // should only happen for +0.0 and -0.0.
1285 assert((Subtarget->is64Bit() || APF.isZero()) && "Unexpected constant");
1286 if (HasZdinx)
1287 Opc = RISCV::COPY;
1288 else
1289 Opc = Is64Bit ? RISCV::FMV_D_X : RISCV::FCVT_D_W;
1290 break;
1291 }
1292
1293 SDNode *Res;
1294 if (VT.SimpleTy == MVT::f16 && Opc == RISCV::COPY) {
1295 Res =
1296 CurDAG->getTargetExtractSubreg(RISCV::sub_16, DL, VT, Imm).getNode();
1297 } else if (VT.SimpleTy == MVT::f32 && Opc == RISCV::COPY) {
1298 Res =
1299 CurDAG->getTargetExtractSubreg(RISCV::sub_32, DL, VT, Imm).getNode();
1300 } else if (Opc == RISCV::FCVT_D_W_IN32X || Opc == RISCV::FCVT_D_W)
1301 Res = CurDAG->getMachineNode(
1302 Opc, DL, VT, Imm,
1303 CurDAG->getTargetConstant(RISCVFPRndMode::RNE, DL, XLenVT));
1304 else
1305 Res = CurDAG->getMachineNode(Opc, DL, VT, Imm);
1306
1307 // For f64 -0.0, we need to insert a fneg.d idiom.
1308 if (NegZeroF64) {
1309 Opc = RISCV::FSGNJN_D;
1310 if (HasZdinx)
1311 Opc = Is64Bit ? RISCV::FSGNJN_D_INX : RISCV::FSGNJN_D_IN32X;
1312 Res =
1313 CurDAG->getMachineNode(Opc, DL, VT, SDValue(Res, 0), SDValue(Res, 0));
1314 }
1315
1316 ReplaceNode(Node, Res);
1317 return;
1318 }
1319 case RISCVISD::BuildGPRPair:
1320 case RISCVISD::BuildPairF64:
1321 case RISCVISD::BuildPairGPRVec: {
1322 if (Opcode == RISCVISD::BuildPairF64 && !Subtarget->hasStdExtZdinx())
1323 break;
1324
1325 assert((!Subtarget->is64Bit() || Opcode != RISCVISD::BuildPairF64) &&
1326 "BuildPairF64 only handled here on rv32i_zdinx");
1327
1328 SDValue N =
1329 buildGPRPair(CurDAG, DL, VT, Node->getOperand(0), Node->getOperand(1));
1330 ReplaceNode(Node, N.getNode());
1331 return;
1332 }
1333 case RISCVISD::SplitGPRPair:
1334 case RISCVISD::SplitF64:
1335 case RISCVISD::SplitGPRVec: {
1336 if (Subtarget->hasStdExtZdinx() || Opcode != RISCVISD::SplitF64) {
1337 assert((!Subtarget->is64Bit() || Opcode != RISCVISD::SplitF64) &&
1338 "SplitF64 only handled here on rv32i_zdinx");
1339
1340 if (!SDValue(Node, 0).use_empty()) {
1341 SDValue Lo = CurDAG->getTargetExtractSubreg(RISCV::sub_gpr_even, DL,
1342 Node->getValueType(0),
1343 Node->getOperand(0));
1344 ReplaceUses(SDValue(Node, 0), Lo);
1345 }
1346
1347 if (!SDValue(Node, 1).use_empty()) {
1348 SDValue Hi = CurDAG->getTargetExtractSubreg(
1349 RISCV::sub_gpr_odd, DL, Node->getValueType(1), Node->getOperand(0));
1350 ReplaceUses(SDValue(Node, 1), Hi);
1351 }
1352
1353 CurDAG->RemoveDeadNode(Node);
1354 return;
1355 }
1356
1357 if (!Subtarget->hasStdExtZfa())
1358 break;
1359 assert(Subtarget->hasStdExtD() && !Subtarget->is64Bit() &&
1360 "Unexpected subtarget");
1361
1362 // With Zfa, lower to fmv.x.w and fmvh.x.d.
1363 if (!SDValue(Node, 0).use_empty()) {
1364 SDNode *Lo = CurDAG->getMachineNode(RISCV::FMV_X_W_FPR64, DL, VT,
1365 Node->getOperand(0));
1366 ReplaceUses(SDValue(Node, 0), SDValue(Lo, 0));
1367 }
1368 if (!SDValue(Node, 1).use_empty()) {
1369 SDNode *Hi = CurDAG->getMachineNode(RISCV::FMVH_X_D, DL, VT,
1370 Node->getOperand(0));
1371 ReplaceUses(SDValue(Node, 1), SDValue(Hi, 0));
1372 }
1373
1374 CurDAG->RemoveDeadNode(Node);
1375 return;
1376 }
1377 case ISD::SHL: {
1378 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1379 if (!N1C)
1380 break;
1381 SDValue N0 = Node->getOperand(0);
1382 if (N0.getOpcode() != ISD::AND || !N0.hasOneUse() ||
1384 break;
1385 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
1386 unsigned ShAmt = N1C->getZExtValue();
1387 assert(ShAmt > 0 && ShAmt < Subtarget->getXLen() &&
1388 "Unexpected shift amount!");
1389 uint64_t Mask = N0.getConstantOperandVal(1);
1390
1391 if (isShiftedMask_64(Mask)) {
1392 unsigned XLen = Subtarget->getXLen();
1393 unsigned LeadingZeros = XLen - llvm::bit_width(Mask);
1394 unsigned TrailingZeros = llvm::countr_zero(Mask);
1395 if (ShAmt <= 32 && TrailingZeros > 0 && LeadingZeros == 32) {
1396 // Optimize (shl (and X, C2), C) -> (slli (srliw X, C3), C3+C)
1397 // where C2 has 32 leading zeros and C3 trailing zeros.
1398 SDNode *SRLIW = CurDAG->getMachineNode(
1399 RISCV::SRLIW, DL, VT, N0.getOperand(0),
1400 CurDAG->getTargetConstant(TrailingZeros, DL, VT));
1401 SDNode *SLLI = CurDAG->getMachineNode(
1402 RISCV::SLLI, DL, VT, SDValue(SRLIW, 0),
1403 CurDAG->getTargetConstant(TrailingZeros + ShAmt, DL, VT));
1404 ReplaceNode(Node, SLLI);
1405 return;
1406 }
1407 if (TrailingZeros == 0 && LeadingZeros > ShAmt &&
1408 XLen - LeadingZeros > 11 && LeadingZeros != 32) {
1409 // Optimize (shl (and X, C2), C) -> (srli (slli X, C4), C4-C)
1410 // where C2 has C4 leading zeros and no trailing zeros.
1411 // This is profitable if the "and" was to be lowered to
1412 // (srli (slli X, C4), C4) and not (andi X, C2).
1413 // For "LeadingZeros == 32":
1414 // - with Zba it's just (slli.uw X, C)
1415 // - without Zba a tablegen pattern applies the very same
1416 // transform as we would have done here
1417 SDNode *SLLI = CurDAG->getMachineNode(
1418 RISCV::SLLI, DL, VT, N0.getOperand(0),
1419 CurDAG->getTargetConstant(LeadingZeros, DL, VT));
1420 SDNode *SRLI = CurDAG->getMachineNode(
1421 RISCV::SRLI, DL, VT, SDValue(SLLI, 0),
1422 CurDAG->getTargetConstant(LeadingZeros - ShAmt, DL, VT));
1423 ReplaceNode(Node, SRLI);
1424 return;
1425 }
1426 }
1427 break;
1428 }
1429 case ISD::SRL: {
1430 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1431 if (!N1C)
1432 break;
1433 SDValue N0 = Node->getOperand(0);
1434 if (N0.getOpcode() != ISD::AND || !isa<ConstantSDNode>(N0.getOperand(1)))
1435 break;
1436 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
1437 unsigned ShAmt = N1C->getZExtValue();
1438 assert(ShAmt > 0 && ShAmt < Subtarget->getXLen() &&
1439 "Unexpected shift amount!");
1440 uint64_t Mask = N0.getConstantOperandVal(1);
1441
1442 // Optimize (srl (and X, C2), C) -> (slli (srliw X, C3), C3-C) where C2 has
1443 // 32 leading zeros and C3 trailing zeros.
1444 if (isShiftedMask_64(Mask) && N0.hasOneUse()) {
1445 unsigned XLen = Subtarget->getXLen();
1446 unsigned LeadingZeros = XLen - llvm::bit_width(Mask);
1447 unsigned TrailingZeros = llvm::countr_zero(Mask);
1448 if (LeadingZeros == 32 && TrailingZeros > ShAmt) {
1449 SDNode *SRLIW = CurDAG->getMachineNode(
1450 RISCV::SRLIW, DL, VT, N0.getOperand(0),
1451 CurDAG->getTargetConstant(TrailingZeros, DL, VT));
1452 SDNode *SLLI = CurDAG->getMachineNode(
1453 RISCV::SLLI, DL, VT, SDValue(SRLIW, 0),
1454 CurDAG->getTargetConstant(TrailingZeros - ShAmt, DL, VT));
1455 ReplaceNode(Node, SLLI);
1456 return;
1457 }
1458 }
1459
1460 // Optimize (srl (and X, C2), C) ->
1461 // (srli (slli X, (XLen-C3), (XLen-C3) + C)
1462 // Where C2 is a mask with C3 trailing ones.
1463 // Taking into account that the C2 may have had lower bits unset by
1464 // SimplifyDemandedBits. This avoids materializing the C2 immediate.
1465 // This pattern occurs when type legalizing right shifts for types with
1466 // less than XLen bits.
1467 Mask |= maskTrailingOnes<uint64_t>(ShAmt);
1468 if (!isMask_64(Mask))
1469 break;
1470 unsigned TrailingOnes = llvm::countr_one(Mask);
1471 if (ShAmt >= TrailingOnes)
1472 break;
1473 // If the mask has 32 trailing ones, use SRLI on RV32 or SRLIW on RV64.
1474 if (TrailingOnes == 32) {
1475 SDNode *SRLI = CurDAG->getMachineNode(
1476 Subtarget->is64Bit() ? RISCV::SRLIW : RISCV::SRLI, DL, VT,
1477 N0.getOperand(0), CurDAG->getTargetConstant(ShAmt, DL, VT));
1478 ReplaceNode(Node, SRLI);
1479 return;
1480 }
1481
1482 // Only do the remaining transforms if the AND has one use.
1483 if (!N0.hasOneUse())
1484 break;
1485
1486 // If C2 is (1 << ShAmt) use bexti or th.tst if possible.
1487 if (HasBitTest && ShAmt + 1 == TrailingOnes) {
1488 SDNode *BEXTI = CurDAG->getMachineNode(
1489 Subtarget->hasStdExtZbs() ? RISCV::BEXTI : RISCV::TH_TST, DL, VT,
1490 N0.getOperand(0), CurDAG->getTargetConstant(ShAmt, DL, VT));
1491 ReplaceNode(Node, BEXTI);
1492 return;
1493 }
1494
1495 const unsigned Msb = TrailingOnes - 1;
1496 const unsigned Lsb = ShAmt;
1497 if (tryUnsignedBitfieldExtract(Node, DL, VT, N0.getOperand(0), Msb, Lsb))
1498 return;
1499
1500 unsigned LShAmt = Subtarget->getXLen() - TrailingOnes;
1501 SDNode *SLLI =
1502 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
1503 CurDAG->getTargetConstant(LShAmt, DL, VT));
1504 SDNode *SRLI = CurDAG->getMachineNode(
1505 RISCV::SRLI, DL, VT, SDValue(SLLI, 0),
1506 CurDAG->getTargetConstant(LShAmt + ShAmt, DL, VT));
1507 ReplaceNode(Node, SRLI);
1508 return;
1509 }
1510 case ISD::SRA: {
1512 return;
1513
1515 return;
1516
1517 // Optimize (sra (sext_inreg X, i16), C) ->
1518 // (srai (slli X, (XLen-16), (XLen-16) + C)
1519 // And (sra (sext_inreg X, i8), C) ->
1520 // (srai (slli X, (XLen-8), (XLen-8) + C)
1521 // This can occur when Zbb is enabled, which makes sext_inreg i16/i8 legal.
1522 // This transform matches the code we get without Zbb. The shifts are more
1523 // compressible, and this can help expose CSE opportunities in the sdiv by
1524 // constant optimization.
1525 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1526 if (!N1C)
1527 break;
1528 SDValue N0 = Node->getOperand(0);
1529 if (N0.getOpcode() != ISD::SIGN_EXTEND_INREG || !N0.hasOneUse())
1530 break;
1531 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
1532 unsigned ShAmt = N1C->getZExtValue();
1533 assert(ShAmt > 0 && ShAmt < Subtarget->getXLen() &&
1534 "Unexpected shift amount!");
1535 unsigned ExtSize =
1536 cast<VTSDNode>(N0.getOperand(1))->getVT().getSizeInBits();
1537 // ExtSize of 32 should use sraiw via tablegen pattern.
1538 if (ExtSize >= 32 || ShAmt >= ExtSize)
1539 break;
1540 unsigned LShAmt = Subtarget->getXLen() - ExtSize;
1541 SDNode *SLLI =
1542 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
1543 CurDAG->getTargetConstant(LShAmt, DL, VT));
1544 SDNode *SRAI = CurDAG->getMachineNode(
1545 RISCV::SRAI, DL, VT, SDValue(SLLI, 0),
1546 CurDAG->getTargetConstant(LShAmt + ShAmt, DL, VT));
1547 ReplaceNode(Node, SRAI);
1548 return;
1549 }
1551 // Optimize (sext_inreg (srl X, C), i8/i16) ->
1552 // (srai (slli X, XLen-ExtSize-C), XLen-ExtSize)
1553 // This is a bitfield extract pattern where we're extracting a signed
1554 // 8-bit or 16-bit field from position C.
1555 SDValue N0 = Node->getOperand(0);
1556 if (N0.getOpcode() != ISD::SRL || !N0.hasOneUse())
1557 break;
1558
1559 auto *ShAmtC = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1560 if (!ShAmtC)
1561 break;
1562
1563 unsigned ExtSize =
1564 cast<VTSDNode>(Node->getOperand(1))->getVT().getSizeInBits();
1565 unsigned ShAmt = ShAmtC->getZExtValue();
1566 unsigned XLen = Subtarget->getXLen();
1567
1568 // Only handle types less than 32, and make sure the shift amount is valid.
1569 if (ExtSize >= 32 || ShAmt >= XLen - ExtSize)
1570 break;
1571
1572 unsigned LShAmt = XLen - ExtSize - ShAmt;
1573 SDNode *SLLI =
1574 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
1575 CurDAG->getTargetConstant(LShAmt, DL, VT));
1576 SDNode *SRAI = CurDAG->getMachineNode(
1577 RISCV::SRAI, DL, VT, SDValue(SLLI, 0),
1578 CurDAG->getTargetConstant(XLen - ExtSize, DL, VT));
1579 ReplaceNode(Node, SRAI);
1580 return;
1581 }
1582 case ISD::OR: {
1584 return;
1585
1586 break;
1587 }
1588 case ISD::XOR:
1590 return;
1591
1592 break;
1593 case ISD::AND: {
1594 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1595 if (!N1C)
1596 break;
1597 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
1598
1599 SDValue N0 = Node->getOperand(0);
1600
1601 bool LeftShift = N0.getOpcode() == ISD::SHL;
1602 if (LeftShift || N0.getOpcode() == ISD::SRL) {
1603 auto *C = dyn_cast<ConstantSDNode>(N0.getOperand(1));
1604 if (!C)
1605 break;
1606 unsigned C2 = C->getZExtValue();
1607 unsigned XLen = Subtarget->getXLen();
1608 assert((C2 > 0 && C2 < XLen) && "Unexpected shift amount!");
1609
1610 // Keep track of whether this is a c.andi. If we can't use c.andi, the
1611 // shift pair might offer more compression opportunities.
1612 // TODO: We could check for C extension here, but we don't have many lit
1613 // tests with the C extension enabled so not checking gets better
1614 // coverage.
1615 // TODO: What if ANDI faster than shift?
1616 bool IsCANDI = isInt<6>(N1C->getSExtValue());
1617
1618 uint64_t C1 = N1C->getZExtValue();
1619
1620 // Clear irrelevant bits in the mask.
1621 if (LeftShift)
1623 else
1624 C1 &= maskTrailingOnes<uint64_t>(XLen - C2);
1625
1626 // Some transforms should only be done if the shift has a single use or
1627 // the AND would become (srli (slli X, 32), 32)
1628 bool OneUseOrZExtW = N0.hasOneUse() || C1 == UINT64_C(0xFFFFFFFF);
1629
1630 SDValue X = N0.getOperand(0);
1631
1632 // Turn (and (srl x, c2) c1) -> (srli (slli x, c3-c2), c3) if c1 is a mask
1633 // with c3 leading zeros.
1634 if (!LeftShift && isMask_64(C1)) {
1635 unsigned Leading = XLen - llvm::bit_width(C1);
1636 if (C2 < Leading) {
1637 // If the number of leading zeros is C2+32 this can be SRLIW.
1638 if (C2 + 32 == Leading) {
1639 SDNode *SRLIW = CurDAG->getMachineNode(
1640 RISCV::SRLIW, DL, VT, X, CurDAG->getTargetConstant(C2, DL, VT));
1641 ReplaceNode(Node, SRLIW);
1642 return;
1643 }
1644
1645 // (and (srl (sexti32 Y), c2), c1) -> (srliw (sraiw Y, 31), c3 - 32)
1646 // if c1 is a mask with c3 leading zeros and c2 >= 32 and c3-c2==1.
1647 //
1648 // This pattern occurs when (i32 (srl (sra 31), c3 - 32)) is type
1649 // legalized and goes through DAG combine.
1650 if (C2 >= 32 && (Leading - C2) == 1 && N0.hasOneUse() &&
1651 X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1652 cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32) {
1653 SDNode *SRAIW =
1654 CurDAG->getMachineNode(RISCV::SRAIW, DL, VT, X.getOperand(0),
1655 CurDAG->getTargetConstant(31, DL, VT));
1656 SDNode *SRLIW = CurDAG->getMachineNode(
1657 RISCV::SRLIW, DL, VT, SDValue(SRAIW, 0),
1658 CurDAG->getTargetConstant(Leading - 32, DL, VT));
1659 ReplaceNode(Node, SRLIW);
1660 return;
1661 }
1662
1663 // Try to use an unsigned bitfield extract (e.g., th.extu) if
1664 // available.
1665 // Transform (and (srl x, C2), C1)
1666 // -> (<bfextract> x, msb, lsb)
1667 //
1668 // Make sure to keep this below the SRLIW cases, as we always want to
1669 // prefer the more common instruction.
1670 const unsigned Msb = llvm::bit_width(C1) + C2 - 1;
1671 const unsigned Lsb = C2;
1672 if (tryUnsignedBitfieldExtract(Node, DL, VT, X, Msb, Lsb))
1673 return;
1674
1675 // (srli (slli x, c3-c2), c3).
1676 // Skip if we could use (zext.w (sraiw X, C2)).
1677 bool Skip = Subtarget->hasStdExtZba() && Leading == 32 &&
1678 X.getOpcode() == ISD::SIGN_EXTEND_INREG &&
1679 cast<VTSDNode>(X.getOperand(1))->getVT() == MVT::i32;
1680 // Also Skip if we can use bexti or th.tst.
1681 Skip |= HasBitTest && Leading == XLen - 1;
1682 if (OneUseOrZExtW && !Skip) {
1683 SDNode *SLLI = CurDAG->getMachineNode(
1684 RISCV::SLLI, DL, VT, X,
1685 CurDAG->getTargetConstant(Leading - C2, DL, VT));
1686 SDNode *SRLI = CurDAG->getMachineNode(
1687 RISCV::SRLI, DL, VT, SDValue(SLLI, 0),
1688 CurDAG->getTargetConstant(Leading, DL, VT));
1689 ReplaceNode(Node, SRLI);
1690 return;
1691 }
1692 }
1693 }
1694
1695 // Turn (and (shl x, c2), c1) -> (srli (slli c2+c3), c3) if c1 is a mask
1696 // shifted by c2 bits with c3 leading zeros.
1697 if (LeftShift && isShiftedMask_64(C1)) {
1698 unsigned Leading = XLen - llvm::bit_width(C1);
1699
1700 if (C2 + Leading < XLen &&
1701 C1 == (maskTrailingOnes<uint64_t>(XLen - (C2 + Leading)) << C2)) {
1702 // Use slli.uw when possible.
1703 if ((XLen - (C2 + Leading)) == 32 && Subtarget->hasStdExtZba()) {
1704 SDNode *SLLI_UW =
1705 CurDAG->getMachineNode(RISCV::SLLI_UW, DL, VT, X,
1706 CurDAG->getTargetConstant(C2, DL, VT));
1707 ReplaceNode(Node, SLLI_UW);
1708 return;
1709 }
1710
1711 // Try to use an unsigned bitfield insert (e.g., nds.bfoz) if
1712 // available.
1713 // Transform (and (shl x, c2), c1)
1714 // -> (<bfinsert> x, msb, lsb)
1715 // e.g.
1716 // (and (shl x, 12), 0x00fff000)
1717 // If XLen = 32 and C2 = 12, then
1718 // Msb = 32 - 8 - 1 = 23 and Lsb = 12
1719 const unsigned Msb = XLen - Leading - 1;
1720 const unsigned Lsb = C2;
1721 if (tryUnsignedBitfieldInsertInZero(Node, DL, VT, X, Msb, Lsb))
1722 return;
1723
1724 if (OneUseOrZExtW && !IsCANDI) {
1725 // (packh x0, X)
1726 if (Subtarget->hasStdExtZbkb() && C1 == 0xff00 && C2 == 8) {
1727 SDNode *PACKH = CurDAG->getMachineNode(
1728 RISCV::PACKH, DL, VT,
1729 CurDAG->getRegister(RISCV::X0, Subtarget->getXLenVT()), X);
1730 ReplaceNode(Node, PACKH);
1731 return;
1732 }
1733 // (srli (slli c2+c3), c3)
1734 SDNode *SLLI = CurDAG->getMachineNode(
1735 RISCV::SLLI, DL, VT, X,
1736 CurDAG->getTargetConstant(C2 + Leading, DL, VT));
1737 SDNode *SRLI = CurDAG->getMachineNode(
1738 RISCV::SRLI, DL, VT, SDValue(SLLI, 0),
1739 CurDAG->getTargetConstant(Leading, DL, VT));
1740 ReplaceNode(Node, SRLI);
1741 return;
1742 }
1743 }
1744 }
1745
1746 // Turn (and (shr x, c2), c1) -> (slli (srli x, c2+c3), c3) if c1 is a
1747 // shifted mask with c2 leading zeros and c3 trailing zeros.
1748 if (!LeftShift && isShiftedMask_64(C1)) {
1749 unsigned Leading = XLen - llvm::bit_width(C1);
1750 unsigned Trailing = llvm::countr_zero(C1);
1751 if (Leading == C2 && C2 + Trailing < XLen && OneUseOrZExtW &&
1752 !IsCANDI) {
1753 unsigned SrliOpc = RISCV::SRLI;
1754 // If the input is zexti32 we should use SRLIW.
1755 if (X.getOpcode() == ISD::AND &&
1756 isa<ConstantSDNode>(X.getOperand(1)) &&
1757 X.getConstantOperandVal(1) == UINT64_C(0xFFFFFFFF)) {
1758 SrliOpc = RISCV::SRLIW;
1759 X = X.getOperand(0);
1760 }
1761 SDNode *SRLI = CurDAG->getMachineNode(
1762 SrliOpc, DL, VT, X,
1763 CurDAG->getTargetConstant(C2 + Trailing, DL, VT));
1764 SDNode *SLLI = CurDAG->getMachineNode(
1765 RISCV::SLLI, DL, VT, SDValue(SRLI, 0),
1766 CurDAG->getTargetConstant(Trailing, DL, VT));
1767 ReplaceNode(Node, SLLI);
1768 return;
1769 }
1770 // If the leading zero count is C2+32, we can use SRLIW instead of SRLI.
1771 if (Leading > 32 && (Leading - 32) == C2 && C2 + Trailing < 32 &&
1772 OneUseOrZExtW && !IsCANDI) {
1773 SDNode *SRLIW = CurDAG->getMachineNode(
1774 RISCV::SRLIW, DL, VT, X,
1775 CurDAG->getTargetConstant(C2 + Trailing, DL, VT));
1776 SDNode *SLLI = CurDAG->getMachineNode(
1777 RISCV::SLLI, DL, VT, SDValue(SRLIW, 0),
1778 CurDAG->getTargetConstant(Trailing, DL, VT));
1779 ReplaceNode(Node, SLLI);
1780 return;
1781 }
1782 // If we have 32 bits in the mask, we can use SLLI_UW instead of SLLI.
1783 if (Trailing > 0 && Leading + Trailing == 32 && C2 + Trailing < XLen &&
1784 OneUseOrZExtW && Subtarget->hasStdExtZba()) {
1785 SDNode *SRLI = CurDAG->getMachineNode(
1786 RISCV::SRLI, DL, VT, X,
1787 CurDAG->getTargetConstant(C2 + Trailing, DL, VT));
1788 SDNode *SLLI_UW = CurDAG->getMachineNode(
1789 RISCV::SLLI_UW, DL, VT, SDValue(SRLI, 0),
1790 CurDAG->getTargetConstant(Trailing, DL, VT));
1791 ReplaceNode(Node, SLLI_UW);
1792 return;
1793 }
1794 }
1795
1796 // Turn (and (shl x, c2), c1) -> (slli (srli x, c3-c2), c3) if c1 is a
1797 // shifted mask with no leading zeros and c3 trailing zeros.
1798 if (LeftShift && isShiftedMask_64(C1)) {
1799 unsigned Leading = XLen - llvm::bit_width(C1);
1800 unsigned Trailing = llvm::countr_zero(C1);
1801 if (Leading == 0 && C2 < Trailing && OneUseOrZExtW && !IsCANDI) {
1802 SDNode *SRLI = CurDAG->getMachineNode(
1803 RISCV::SRLI, DL, VT, X,
1804 CurDAG->getTargetConstant(Trailing - C2, DL, VT));
1805 SDNode *SLLI = CurDAG->getMachineNode(
1806 RISCV::SLLI, DL, VT, SDValue(SRLI, 0),
1807 CurDAG->getTargetConstant(Trailing, DL, VT));
1808 ReplaceNode(Node, SLLI);
1809 return;
1810 }
1811 // If we have (32-C2) leading zeros, we can use SRLIW instead of SRLI.
1812 if (C2 < Trailing && Leading + C2 == 32 && OneUseOrZExtW && !IsCANDI) {
1813 SDNode *SRLIW = CurDAG->getMachineNode(
1814 RISCV::SRLIW, DL, VT, X,
1815 CurDAG->getTargetConstant(Trailing - C2, DL, VT));
1816 SDNode *SLLI = CurDAG->getMachineNode(
1817 RISCV::SLLI, DL, VT, SDValue(SRLIW, 0),
1818 CurDAG->getTargetConstant(Trailing, DL, VT));
1819 ReplaceNode(Node, SLLI);
1820 return;
1821 }
1822
1823 // If we have 32 bits in the mask, we can use SLLI_UW instead of SLLI.
1824 if (C2 < Trailing && Leading + Trailing == 32 && OneUseOrZExtW &&
1825 Subtarget->hasStdExtZba()) {
1826 SDNode *SRLI = CurDAG->getMachineNode(
1827 RISCV::SRLI, DL, VT, X,
1828 CurDAG->getTargetConstant(Trailing - C2, DL, VT));
1829 SDNode *SLLI_UW = CurDAG->getMachineNode(
1830 RISCV::SLLI_UW, DL, VT, SDValue(SRLI, 0),
1831 CurDAG->getTargetConstant(Trailing, DL, VT));
1832 ReplaceNode(Node, SLLI_UW);
1833 return;
1834 }
1835 }
1836 }
1837
1838 const uint64_t C1 = N1C->getZExtValue();
1839
1840 if (N0.getOpcode() == ISD::SRA && isa<ConstantSDNode>(N0.getOperand(1)) &&
1841 N0.hasOneUse()) {
1842 unsigned C2 = N0.getConstantOperandVal(1);
1843 unsigned XLen = Subtarget->getXLen();
1844 assert((C2 > 0 && C2 < XLen) && "Unexpected shift amount!");
1845
1846 SDValue X = N0.getOperand(0);
1847
1848 // Prefer SRAIW + ANDI when possible.
1849 bool Skip = C2 > 32 && isInt<12>(N1C->getSExtValue()) &&
1850 X.getOpcode() == ISD::SHL &&
1851 isa<ConstantSDNode>(X.getOperand(1)) &&
1852 X.getConstantOperandVal(1) == 32;
1853 // Turn (and (sra x, c2), c1) -> (srli (srai x, c2-c3), c3) if c1 is a
1854 // mask with c3 leading zeros and c2 is larger than c3.
1855 if (isMask_64(C1) && !Skip) {
1856 unsigned Leading = XLen - llvm::bit_width(C1);
1857 if (C2 > Leading) {
1858 SDNode *SRAI = CurDAG->getMachineNode(
1859 RISCV::SRAI, DL, VT, X,
1860 CurDAG->getTargetConstant(C2 - Leading, DL, VT));
1861 SDNode *SRLI = CurDAG->getMachineNode(
1862 RISCV::SRLI, DL, VT, SDValue(SRAI, 0),
1863 CurDAG->getTargetConstant(Leading, DL, VT));
1864 ReplaceNode(Node, SRLI);
1865 return;
1866 }
1867 }
1868
1869 // Look for (and (sra y, c2), c1) where c1 is a shifted mask with c3
1870 // leading zeros and c4 trailing zeros. If c2 is greater than c3, we can
1871 // use (slli (srli (srai y, c2 - c3), c3 + c4), c4).
1872 if (isShiftedMask_64(C1) && !Skip) {
1873 unsigned Leading = XLen - llvm::bit_width(C1);
1874 unsigned Trailing = llvm::countr_zero(C1);
1875 if (C2 > Leading && Leading > 0 && Trailing > 0) {
1876 SDNode *SRAI = CurDAG->getMachineNode(
1877 RISCV::SRAI, DL, VT, N0.getOperand(0),
1878 CurDAG->getTargetConstant(C2 - Leading, DL, VT));
1879 SDNode *SRLI = CurDAG->getMachineNode(
1880 RISCV::SRLI, DL, VT, SDValue(SRAI, 0),
1881 CurDAG->getTargetConstant(Leading + Trailing, DL, VT));
1882 SDNode *SLLI = CurDAG->getMachineNode(
1883 RISCV::SLLI, DL, VT, SDValue(SRLI, 0),
1884 CurDAG->getTargetConstant(Trailing, DL, VT));
1885 ReplaceNode(Node, SLLI);
1886 return;
1887 }
1888 }
1889 }
1890
1891 // If C1 masks off the upper bits only (but can't be formed as an
1892 // ANDI), use an unsigned bitfield extract (e.g., th.extu), if
1893 // available.
1894 // Transform (and x, C1)
1895 // -> (<bfextract> x, msb, lsb)
1896 if (isMask_64(C1) && !isInt<12>(N1C->getSExtValue()) &&
1897 !(C1 == 0xffff && Subtarget->hasStdExtZbb()) &&
1898 !(C1 == 0xffffffff && Subtarget->hasStdExtZba())) {
1899 const unsigned Msb = llvm::bit_width(C1) - 1;
1900 if (tryUnsignedBitfieldExtract(Node, DL, VT, N0, Msb, 0))
1901 return;
1902 }
1903
1905 return;
1906
1907 break;
1908 }
1909 case ISD::MUL: {
1910 // Special case for calculating (mul (and X, C2), C1) where the full product
1911 // fits in XLen bits. We can shift X left by the number of leading zeros in
1912 // C2 and shift C1 left by XLen-lzcnt(C2). This will ensure the final
1913 // product has XLen trailing zeros, putting it in the output of MULHU. This
1914 // can avoid materializing a constant in a register for C2.
1915
1916 // RHS should be a constant.
1917 auto *N1C = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1918 if (!N1C || !N1C->hasOneUse())
1919 break;
1920
1921 // LHS should be an AND with constant.
1922 SDValue N0 = Node->getOperand(0);
1923 if (N0.getOpcode() != ISD::AND || !isa<ConstantSDNode>(N0.getOperand(1)))
1924 break;
1925
1926 uint64_t C2 = N0.getConstantOperandVal(1);
1927
1928 // Constant should be a mask.
1929 if (!isMask_64(C2))
1930 break;
1931
1932 // If this can be an ANDI or ZEXT.H, don't do this if the ANDI/ZEXT has
1933 // multiple users or the constant is a simm12. This prevents inserting a
1934 // shift and still have uses of the AND/ZEXT. Shifting a simm12 will likely
1935 // make it more costly to materialize. Otherwise, using a SLLI might allow
1936 // it to be compressed.
1937 bool IsANDIOrZExt =
1938 isInt<12>(C2) ||
1939 (C2 == UINT64_C(0xFFFF) && Subtarget->hasStdExtZbb());
1940 // With XTHeadBb, we can use TH.EXTU.
1941 IsANDIOrZExt |= C2 == UINT64_C(0xFFFF) && Subtarget->hasVendorXTHeadBb();
1942 if (IsANDIOrZExt && (isInt<12>(N1C->getSExtValue()) || !N0.hasOneUse()))
1943 break;
1944 // If this can be a ZEXT.w, don't do this if the ZEXT has multiple users or
1945 // the constant is a simm32.
1946 bool IsZExtW = C2 == UINT64_C(0xFFFFFFFF) && Subtarget->hasStdExtZba();
1947 // With XTHeadBb, we can use TH.EXTU.
1948 IsZExtW |= C2 == UINT64_C(0xFFFFFFFF) && Subtarget->hasVendorXTHeadBb();
1949 if (IsZExtW && (isInt<32>(N1C->getSExtValue()) || !N0.hasOneUse()))
1950 break;
1951
1952 // We need to shift left the AND input and C1 by a total of XLen bits.
1953
1954 // How far left do we need to shift the AND input?
1955 unsigned XLen = Subtarget->getXLen();
1956 unsigned LeadingZeros = XLen - llvm::bit_width(C2);
1957
1958 // The constant gets shifted by the remaining amount unless that would
1959 // shift bits out.
1960 uint64_t C1 = N1C->getZExtValue();
1961 unsigned ConstantShift = XLen - LeadingZeros;
1962 if (ConstantShift > (XLen - llvm::bit_width(C1)))
1963 break;
1964
1965 uint64_t ShiftedC1 = C1 << ConstantShift;
1966 // If this RV32, we need to sign extend the constant.
1967 if (XLen == 32)
1968 ShiftedC1 = SignExtend64<32>(ShiftedC1);
1969
1970 // Create (mulhu (slli X, lzcnt(C2)), C1 << (XLen - lzcnt(C2))).
1971 SDNode *Imm = selectImm(CurDAG, DL, VT, ShiftedC1, *Subtarget).getNode();
1972 SDNode *SLLI =
1973 CurDAG->getMachineNode(RISCV::SLLI, DL, VT, N0.getOperand(0),
1974 CurDAG->getTargetConstant(LeadingZeros, DL, VT));
1975 SDNode *MULHU = CurDAG->getMachineNode(RISCV::MULHU, DL, VT,
1976 SDValue(SLLI, 0), SDValue(Imm, 0));
1977 ReplaceNode(Node, MULHU);
1978 return;
1979 }
1980 case ISD::SMUL_LOHI:
1981 case ISD::UMUL_LOHI:
1982 case RISCVISD::WMULSU:
1983 case RISCVISD::WADD:
1984 case RISCVISD::WSUB:
1985 case RISCVISD::WADDU:
1986 case RISCVISD::WSUBU: {
1987 assert(Subtarget->hasStdExtP() && !Subtarget->is64Bit() && VT == MVT::i32 &&
1988 "Unexpected opcode");
1989
1990 unsigned Opc;
1991 switch (Node->getOpcode()) {
1992 default:
1993 llvm_unreachable("Unexpected opcode");
1994 case ISD::SMUL_LOHI:
1995 Opc = RISCV::WMUL;
1996 break;
1997 case ISD::UMUL_LOHI:
1998 Opc = RISCV::WMULU;
1999 break;
2000 case RISCVISD::WMULSU:
2001 Opc = RISCV::WMULSU;
2002 break;
2003 case RISCVISD::WADD:
2004 Opc = RISCV::WADD;
2005 break;
2006 case RISCVISD::WSUB:
2007 Opc = RISCV::WSUB;
2008 break;
2009 case RISCVISD::WADDU:
2010 Opc = RISCV::WADDU;
2011 break;
2012 case RISCVISD::WSUBU:
2013 Opc = RISCV::WSUBU;
2014 break;
2015 }
2016
2017 SDNode *Result = CurDAG->getMachineNode(
2018 Opc, DL, MVT::Untyped, Node->getOperand(0), Node->getOperand(1));
2019
2020 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(Result, 0));
2021 ReplaceUses(SDValue(Node, 0), Lo);
2022 ReplaceUses(SDValue(Node, 1), Hi);
2023 CurDAG->RemoveDeadNode(Node);
2024 return;
2025 }
2026 case RISCVISD::WSLL:
2027 case RISCVISD::WSLA: {
2028 // Custom select WSLL/WSLA for RV32P.
2029 assert(Subtarget->hasStdExtP() && !Subtarget->is64Bit() && VT == MVT::i32 &&
2030 "Unexpected opcode");
2031
2032 bool IsSigned = Node->getOpcode() == RISCVISD::WSLA;
2033
2034 SDValue ShAmt = Node->getOperand(1);
2035
2036 unsigned Opc;
2037
2038 auto *ShAmtC = dyn_cast<ConstantSDNode>(ShAmt);
2039 if (ShAmtC && ShAmtC->getZExtValue() < 64) {
2040 Opc = IsSigned ? RISCV::WSLAI : RISCV::WSLLI;
2041 ShAmt = CurDAG->getTargetConstant(ShAmtC->getZExtValue(), DL, XLenVT);
2042 } else {
2043 Opc = IsSigned ? RISCV::WSLA : RISCV::WSLL;
2044 }
2045
2046 SDNode *WShift = CurDAG->getMachineNode(Opc, DL, MVT::Untyped,
2047 Node->getOperand(0), ShAmt);
2048
2049 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(WShift, 0));
2050 ReplaceUses(SDValue(Node, 0), Lo);
2051 ReplaceUses(SDValue(Node, 1), Hi);
2052 CurDAG->RemoveDeadNode(Node);
2053 return;
2054 }
2055 case ISD::LOAD: {
2056 if (tryIndexedLoad(Node))
2057 return;
2058
2059 if (Subtarget->hasVendorXCVmem() && !Subtarget->is64Bit()) {
2060 // We match post-incrementing load here
2062 if (Load->getAddressingMode() != ISD::POST_INC)
2063 break;
2064
2065 SDValue Chain = Node->getOperand(0);
2066 SDValue Base = Node->getOperand(1);
2067 SDValue Offset = Node->getOperand(2);
2068
2069 bool Simm12 = false;
2070 bool SignExtend = Load->getExtensionType() == ISD::SEXTLOAD;
2071
2072 if (auto ConstantOffset = dyn_cast<ConstantSDNode>(Offset)) {
2073 int ConstantVal = ConstantOffset->getSExtValue();
2074 Simm12 = isInt<12>(ConstantVal);
2075 if (Simm12)
2076 Offset = CurDAG->getSignedTargetConstant(ConstantVal, SDLoc(Offset),
2077 Offset.getValueType());
2078 }
2079
2080 unsigned Opcode = 0;
2081 switch (Load->getMemoryVT().getSimpleVT().SimpleTy) {
2082 case MVT::i8:
2083 if (Simm12 && SignExtend)
2084 Opcode = RISCV::CV_LB_ri_inc;
2085 else if (Simm12 && !SignExtend)
2086 Opcode = RISCV::CV_LBU_ri_inc;
2087 else if (!Simm12 && SignExtend)
2088 Opcode = RISCV::CV_LB_rr_inc;
2089 else
2090 Opcode = RISCV::CV_LBU_rr_inc;
2091 break;
2092 case MVT::i16:
2093 if (Simm12 && SignExtend)
2094 Opcode = RISCV::CV_LH_ri_inc;
2095 else if (Simm12 && !SignExtend)
2096 Opcode = RISCV::CV_LHU_ri_inc;
2097 else if (!Simm12 && SignExtend)
2098 Opcode = RISCV::CV_LH_rr_inc;
2099 else
2100 Opcode = RISCV::CV_LHU_rr_inc;
2101 break;
2102 case MVT::i32:
2103 if (Simm12)
2104 Opcode = RISCV::CV_LW_ri_inc;
2105 else
2106 Opcode = RISCV::CV_LW_rr_inc;
2107 break;
2108 default:
2109 break;
2110 }
2111 if (!Opcode)
2112 break;
2113
2114 ReplaceNode(Node, CurDAG->getMachineNode(Opcode, DL, XLenVT, XLenVT,
2115 Chain.getSimpleValueType(), Base,
2116 Offset, Chain));
2117 return;
2118 }
2119 break;
2120 }
2121 case RISCVISD::LD_RV32: {
2122 assert(Subtarget->hasStdExtZilsd() && "LD_RV32 is only used with Zilsd");
2123
2125 SDValue Chain = Node->getOperand(0);
2126 SDValue Addr = Node->getOperand(1);
2128
2129 SDValue Ops[] = {Base, Offset, Chain};
2130 MachineSDNode *New = CurDAG->getMachineNode(
2131 RISCV::LD_RV32, DL, {MVT::Untyped, MVT::Other}, Ops);
2132 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(New, 0));
2133 CurDAG->setNodeMemRefs(New, {cast<MemSDNode>(Node)->getMemOperand()});
2134 ReplaceUses(SDValue(Node, 0), Lo);
2135 ReplaceUses(SDValue(Node, 1), Hi);
2136 ReplaceUses(SDValue(Node, 2), SDValue(New, 1));
2137 CurDAG->RemoveDeadNode(Node);
2138 return;
2139 }
2140 case RISCVISD::SD_RV32: {
2142 SDValue Chain = Node->getOperand(0);
2143 SDValue Addr = Node->getOperand(3);
2145
2146 SDValue Lo = Node->getOperand(1);
2147 SDValue Hi = Node->getOperand(2);
2148
2149 SDValue RegPair;
2150 // Peephole to use X0_Pair for storing zero.
2152 RegPair = CurDAG->getRegister(RISCV::X0_Pair, MVT::Untyped);
2153 } else {
2154 RegPair = buildGPRPair(CurDAG, DL, MVT::Untyped, Lo, Hi);
2155 }
2156
2157 MachineSDNode *New = CurDAG->getMachineNode(RISCV::SD_RV32, DL, MVT::Other,
2158 {RegPair, Base, Offset, Chain});
2159 CurDAG->setNodeMemRefs(New, {cast<MemSDNode>(Node)->getMemOperand()});
2160 ReplaceUses(SDValue(Node, 0), SDValue(New, 0));
2161 CurDAG->RemoveDeadNode(Node);
2162 return;
2163 }
2164 case RISCVISD::MQWACC:
2165 case RISCVISD::MQRWACC:
2166 case RISCVISD::WMACC:
2167 case RISCVISD::WMACCU:
2168 case RISCVISD::WMACCSU:
2169 case RISCVISD::PM2WADDA:
2170 case RISCVISD::PM2WADDA_X:
2171 case RISCVISD::PM2WADDAU:
2172 case RISCVISD::PM2WSUBA:
2173 case RISCVISD::PM2WSUBA_X:
2174 case RISCVISD::PM2WADDASU:
2175 case RISCVISD::WADDAU:
2176 case RISCVISD::WSUBAU:
2177 case RISCVISD::WADDA:
2178 case RISCVISD::WSUBA: {
2179 assert(!Subtarget->is64Bit() && Subtarget->hasStdExtP() &&
2180 "Unexpected opcode");
2181
2182 SDValue Op0 = buildGPRPair(CurDAG, DL, MVT::Untyped, Node->getOperand(0),
2183 Node->getOperand(1));
2184 unsigned Opc;
2185 switch (Opcode) {
2186 default:
2187 llvm_unreachable("Unexpected opcode");
2188 case RISCVISD::MQWACC:
2189 Opc = RISCV::MQWACC;
2190 break;
2191 case RISCVISD::MQRWACC:
2192 Opc = RISCV::MQRWACC;
2193 break;
2194 case RISCVISD::WMACC:
2195 Opc = RISCV::WMACC;
2196 break;
2197 case RISCVISD::WMACCU:
2198 Opc = RISCV::WMACCU;
2199 break;
2200 case RISCVISD::WMACCSU:
2201 Opc = RISCV::WMACCSU;
2202 break;
2203 case RISCVISD::PM2WADDA:
2204 Opc = RISCV::PM2WADDA_H;
2205 break;
2206 case RISCVISD::PM2WADDA_X:
2207 Opc = RISCV::PM2WADDA_HX;
2208 break;
2209 case RISCVISD::PM2WADDAU:
2210 Opc = RISCV::PM2WADDAU_H;
2211 break;
2212 case RISCVISD::PM2WSUBA:
2213 Opc = RISCV::PM2WSUBA_H;
2214 break;
2215 case RISCVISD::PM2WSUBA_X:
2216 Opc = RISCV::PM2WSUBA_HX;
2217 break;
2218 case RISCVISD::PM2WADDASU:
2219 Opc = RISCV::PM2WADDASU_H;
2220 break;
2221 case RISCVISD::WADDAU:
2222 Opc = RISCV::WADDAU;
2223 break;
2224 case RISCVISD::WSUBAU:
2225 Opc = RISCV::WSUBAU;
2226 break;
2227 case RISCVISD::WADDA:
2228 Opc = RISCV::WADDA;
2229 break;
2230 case RISCVISD::WSUBA:
2231 Opc = RISCV::WSUBA;
2232 break;
2233 }
2234 MachineSDNode *New = CurDAG->getMachineNode(
2235 Opc, DL, MVT::Untyped, Op0, Node->getOperand(2), Node->getOperand(3));
2236 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(New, 0));
2237 ReplaceUses(SDValue(Node, 0), Lo);
2238 ReplaceUses(SDValue(Node, 1), Hi);
2239 CurDAG->RemoveDeadNode(Node);
2240 return;
2241 }
2242 case RISCVISD::PM2WADD:
2243 case RISCVISD::PM2WADDU:
2244 case RISCVISD::PM2WADDSU:
2245 case RISCVISD::PM2WADD_X:
2246 case RISCVISD::PM2WSUB:
2247 case RISCVISD::PM2WSUB_X: {
2248 assert(!Subtarget->is64Bit() && Subtarget->hasStdExtP() &&
2249 "Unexpected opcode");
2250 unsigned Opc;
2251 switch (Opcode) {
2252 default:
2253 llvm_unreachable("Unexpected opcode");
2254 case RISCVISD::PM2WADD:
2255 Opc = RISCV::PM2WADD_H;
2256 break;
2257 case RISCVISD::PM2WADDU:
2258 Opc = RISCV::PM2WADDU_H;
2259 break;
2260 case RISCVISD::PM2WADDSU:
2261 Opc = RISCV::PM2WADDSU_H;
2262 break;
2263 case RISCVISD::PM2WADD_X:
2264 Opc = RISCV::PM2WADD_HX;
2265 break;
2266 case RISCVISD::PM2WSUB:
2267 Opc = RISCV::PM2WSUB_H;
2268 break;
2269 case RISCVISD::PM2WSUB_X:
2270 Opc = RISCV::PM2WSUB_HX;
2271 break;
2272 }
2273 MachineSDNode *New = CurDAG->getMachineNode(
2274 Opc, DL, MVT::Untyped, Node->getOperand(0), Node->getOperand(1));
2275 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(New, 0));
2276 ReplaceUses(SDValue(Node, 0), Lo);
2277 ReplaceUses(SDValue(Node, 1), Hi);
2278 CurDAG->RemoveDeadNode(Node);
2279 return;
2280 }
2281 case RISCVISD::ADDD:
2282 // Try to match WMACC pattern: ADDD where one operand pair comes from a
2283 // widening multiply.
2285 return;
2286
2287 // Fall through to regular ADDD selection.
2288 [[fallthrough]];
2289 case RISCVISD::SUBD: {
2290 assert(!Subtarget->is64Bit() && Subtarget->hasStdExtP() &&
2291 "Unexpected opcode");
2292
2293 SDValue Op0Lo = Node->getOperand(0);
2294 SDValue Op0Hi = Node->getOperand(1);
2295
2296 SDValue Op0;
2297 if (isNullConstant(Op0Lo) && isNullConstant(Op0Hi)) {
2298 Op0 = CurDAG->getRegister(RISCV::X0_Pair, MVT::Untyped);
2299 } else {
2300 Op0 = buildGPRPair(CurDAG, DL, MVT::Untyped, Op0Lo, Op0Hi);
2301 }
2302
2303 SDValue Op1Lo = Node->getOperand(2);
2304 SDValue Op1Hi = Node->getOperand(3);
2305 SDValue Op1 = buildGPRPair(CurDAG, DL, MVT::Untyped, Op1Lo, Op1Hi);
2306
2307 unsigned Opc = Opcode == RISCVISD::ADDD ? RISCV::ADDD : RISCV::SUBD;
2308 MachineSDNode *New =
2309 CurDAG->getMachineNode(Opc, DL, MVT::Untyped, Op0, Op1);
2310
2311 auto [Lo, Hi] = extractGPRPair(CurDAG, DL, SDValue(New, 0));
2312 ReplaceUses(SDValue(Node, 0), Lo);
2313 ReplaceUses(SDValue(Node, 1), Hi);
2314 CurDAG->RemoveDeadNode(Node);
2315 return;
2316 }
2318 unsigned IntNo = Node->getConstantOperandVal(0);
2319 switch (IntNo) {
2320 // By default we do not custom select any intrinsic.
2321 default:
2322 break;
2323 case Intrinsic::riscv_vmsgeu:
2324 case Intrinsic::riscv_vmsge: {
2325 SDValue Src1 = Node->getOperand(1);
2326 SDValue Src2 = Node->getOperand(2);
2327 bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu;
2328 bool IsCmpConstant = false;
2329 bool IsCmpMinimum = false;
2330 // Only custom select scalar second operand.
2331 if (Src2.getValueType() != XLenVT)
2332 break;
2333 // Small constants are handled with patterns.
2334 int64_t CVal = 0;
2335 MVT Src1VT = Src1.getSimpleValueType();
2336 if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
2337 IsCmpConstant = true;
2338 CVal = C->getSExtValue();
2339 if (CVal >= -15 && CVal <= 16) {
2340 if (!IsUnsigned || CVal != 0)
2341 break;
2342 IsCmpMinimum = true;
2343 } else if (!IsUnsigned && CVal == APInt::getSignedMinValue(
2344 Src1VT.getScalarSizeInBits())
2345 .getSExtValue()) {
2346 IsCmpMinimum = true;
2347 }
2348 }
2349 unsigned VMSLTOpcode, VMNANDOpcode, VMSetOpcode, VMSGTOpcode;
2350 switch (RISCVTargetLowering::getLMUL(Src1VT)) {
2351 default:
2352 llvm_unreachable("Unexpected LMUL!");
2353#define CASE_VMSLT_OPCODES(lmulenum, suffix) \
2354 case RISCVVType::lmulenum: \
2355 VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix \
2356 : RISCV::PseudoVMSLT_VX_##suffix; \
2357 VMSGTOpcode = IsUnsigned ? RISCV::PseudoVMSGTU_VX_##suffix \
2358 : RISCV::PseudoVMSGT_VX_##suffix; \
2359 break;
2360 CASE_VMSLT_OPCODES(LMUL_F8, MF8)
2361 CASE_VMSLT_OPCODES(LMUL_F4, MF4)
2362 CASE_VMSLT_OPCODES(LMUL_F2, MF2)
2363 CASE_VMSLT_OPCODES(LMUL_1, M1)
2364 CASE_VMSLT_OPCODES(LMUL_2, M2)
2365 CASE_VMSLT_OPCODES(LMUL_4, M4)
2366 CASE_VMSLT_OPCODES(LMUL_8, M8)
2367#undef CASE_VMSLT_OPCODES
2368 }
2369 // Mask operations use the LMUL from the mask type.
2370 switch (RISCVTargetLowering::getLMUL(VT)) {
2371 default:
2372 llvm_unreachable("Unexpected LMUL!");
2373#define CASE_VMNAND_VMSET_OPCODES(lmulenum, suffix) \
2374 case RISCVVType::lmulenum: \
2375 VMNANDOpcode = RISCV::PseudoVMNAND_MM_##suffix; \
2376 VMSetOpcode = RISCV::PseudoVMSET_M_##suffix; \
2377 break;
2378 CASE_VMNAND_VMSET_OPCODES(LMUL_F8, B64)
2379 CASE_VMNAND_VMSET_OPCODES(LMUL_F4, B32)
2380 CASE_VMNAND_VMSET_OPCODES(LMUL_F2, B16)
2381 CASE_VMNAND_VMSET_OPCODES(LMUL_1, B8)
2382 CASE_VMNAND_VMSET_OPCODES(LMUL_2, B4)
2383 CASE_VMNAND_VMSET_OPCODES(LMUL_4, B2)
2384 CASE_VMNAND_VMSET_OPCODES(LMUL_8, B1)
2385#undef CASE_VMNAND_VMSET_OPCODES
2386 }
2387 SDValue SEW = CurDAG->getTargetConstant(
2388 Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
2389 SDValue MaskSEW = CurDAG->getTargetConstant(0, DL, XLenVT);
2390 SDValue VL;
2391 selectVLOp(Node->getOperand(3), VL);
2392
2393 // If vmsge(u) with minimum value, expand it to vmset.
2394 if (IsCmpMinimum) {
2396 CurDAG->getMachineNode(VMSetOpcode, DL, VT, VL, MaskSEW));
2397 return;
2398 }
2399
2400 if (IsCmpConstant) {
2401 SDValue Imm =
2402 selectImm(CurDAG, SDLoc(Src2), XLenVT, CVal - 1, *Subtarget);
2403
2404 ReplaceNode(Node, CurDAG->getMachineNode(VMSGTOpcode, DL, VT,
2405 {Src1, Imm, VL, SEW}));
2406 return;
2407 }
2408
2409 // Expand to
2410 // vmslt{u}.vx vd, va, x; vmnand.mm vd, vd, vd
2411 SDValue Cmp = SDValue(
2412 CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
2413 0);
2414 ReplaceNode(Node, CurDAG->getMachineNode(VMNANDOpcode, DL, VT,
2415 {Cmp, Cmp, VL, MaskSEW}));
2416 return;
2417 }
2418 case Intrinsic::riscv_vmsgeu_mask:
2419 case Intrinsic::riscv_vmsge_mask: {
2420 SDValue Src1 = Node->getOperand(2);
2421 SDValue Src2 = Node->getOperand(3);
2422 bool IsUnsigned = IntNo == Intrinsic::riscv_vmsgeu_mask;
2423 bool IsCmpConstant = false;
2424 bool IsCmpMinimum = false;
2425 // Only custom select scalar second operand.
2426 if (Src2.getValueType() != XLenVT)
2427 break;
2428 // Small constants are handled with patterns.
2429 MVT Src1VT = Src1.getSimpleValueType();
2430 int64_t CVal = 0;
2431 if (auto *C = dyn_cast<ConstantSDNode>(Src2)) {
2432 IsCmpConstant = true;
2433 CVal = C->getSExtValue();
2434 if (CVal >= -15 && CVal <= 16) {
2435 if (!IsUnsigned || CVal != 0)
2436 break;
2437 IsCmpMinimum = true;
2438 } else if (!IsUnsigned && CVal == APInt::getSignedMinValue(
2439 Src1VT.getScalarSizeInBits())
2440 .getSExtValue()) {
2441 IsCmpMinimum = true;
2442 }
2443 }
2444 unsigned VMSLTOpcode, VMSLTMaskOpcode, VMXOROpcode, VMANDNOpcode,
2445 VMOROpcode, VMSGTMaskOpcode;
2446 switch (RISCVTargetLowering::getLMUL(Src1VT)) {
2447 default:
2448 llvm_unreachable("Unexpected LMUL!");
2449#define CASE_VMSLT_OPCODES(lmulenum, suffix) \
2450 case RISCVVType::lmulenum: \
2451 VMSLTOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix \
2452 : RISCV::PseudoVMSLT_VX_##suffix; \
2453 VMSLTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSLTU_VX_##suffix##_MASK \
2454 : RISCV::PseudoVMSLT_VX_##suffix##_MASK; \
2455 VMSGTMaskOpcode = IsUnsigned ? RISCV::PseudoVMSGTU_VX_##suffix##_MASK \
2456 : RISCV::PseudoVMSGT_VX_##suffix##_MASK; \
2457 break;
2458 CASE_VMSLT_OPCODES(LMUL_F8, MF8)
2459 CASE_VMSLT_OPCODES(LMUL_F4, MF4)
2460 CASE_VMSLT_OPCODES(LMUL_F2, MF2)
2461 CASE_VMSLT_OPCODES(LMUL_1, M1)
2462 CASE_VMSLT_OPCODES(LMUL_2, M2)
2463 CASE_VMSLT_OPCODES(LMUL_4, M4)
2464 CASE_VMSLT_OPCODES(LMUL_8, M8)
2465#undef CASE_VMSLT_OPCODES
2466 }
2467 // Mask operations use the LMUL from the mask type.
2468 switch (RISCVTargetLowering::getLMUL(VT)) {
2469 default:
2470 llvm_unreachable("Unexpected LMUL!");
2471#define CASE_VMXOR_VMANDN_VMOR_OPCODES(lmulenum, suffix) \
2472 case RISCVVType::lmulenum: \
2473 VMXOROpcode = RISCV::PseudoVMXOR_MM_##suffix; \
2474 VMANDNOpcode = RISCV::PseudoVMANDN_MM_##suffix; \
2475 VMOROpcode = RISCV::PseudoVMOR_MM_##suffix; \
2476 break;
2477 CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F8, B64)
2478 CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F4, B32)
2479 CASE_VMXOR_VMANDN_VMOR_OPCODES(LMUL_F2, B16)
2484#undef CASE_VMXOR_VMANDN_VMOR_OPCODES
2485 }
2486 SDValue SEW = CurDAG->getTargetConstant(
2487 Log2_32(Src1VT.getScalarSizeInBits()), DL, XLenVT);
2488 SDValue MaskSEW = CurDAG->getTargetConstant(0, DL, XLenVT);
2489 SDValue VL;
2490 selectVLOp(Node->getOperand(5), VL);
2491 SDValue MaskedOff = Node->getOperand(1);
2492 SDValue Mask = Node->getOperand(4);
2493
2494 // If vmsge(u) with minimum value, expand it to vmor mask, maskedoff.
2495 if (IsCmpMinimum) {
2496 // We don't need vmor if the MaskedOff and the Mask are the same
2497 // value.
2498 if (Mask == MaskedOff) {
2499 ReplaceUses(Node, Mask.getNode());
2500 return;
2501 }
2503 CurDAG->getMachineNode(VMOROpcode, DL, VT,
2504 {Mask, MaskedOff, VL, MaskSEW}));
2505 return;
2506 }
2507
2508 // If the MaskedOff value and the Mask are the same value use
2509 // vmslt{u}.vx vt, va, x; vmandn.mm vd, vd, vt
2510 // This avoids needing to copy v0 to vd before starting the next sequence.
2511 if (Mask == MaskedOff) {
2512 SDValue Cmp = SDValue(
2513 CurDAG->getMachineNode(VMSLTOpcode, DL, VT, {Src1, Src2, VL, SEW}),
2514 0);
2515 ReplaceNode(Node, CurDAG->getMachineNode(VMANDNOpcode, DL, VT,
2516 {Mask, Cmp, VL, MaskSEW}));
2517 return;
2518 }
2519
2520 SDValue PolicyOp =
2521 CurDAG->getTargetConstant(RISCVVType::TAIL_AGNOSTIC, DL, XLenVT);
2522
2523 if (IsCmpConstant) {
2524 SDValue Imm =
2525 selectImm(CurDAG, SDLoc(Src2), XLenVT, CVal - 1, *Subtarget);
2526
2527 ReplaceNode(Node, CurDAG->getMachineNode(
2528 VMSGTMaskOpcode, DL, VT,
2529 {MaskedOff, Src1, Imm, Mask, VL, SEW, PolicyOp}));
2530 return;
2531 }
2532
2533 // Otherwise use
2534 // vmslt{u}.vx vd, va, x, v0.t; vmxor.mm vd, vd, v0
2535 // The result is mask undisturbed.
2536 // We use the same instructions to emulate mask agnostic behavior, because
2537 // the agnostic result can be either undisturbed or all 1.
2538 SDValue Cmp = SDValue(CurDAG->getMachineNode(VMSLTMaskOpcode, DL, VT,
2539 {MaskedOff, Src1, Src2, Mask,
2540 VL, SEW, PolicyOp}),
2541 0);
2542 // vmxor.mm vd, vd, v0 is used to update active value.
2543 ReplaceNode(Node, CurDAG->getMachineNode(VMXOROpcode, DL, VT,
2544 {Cmp, Mask, VL, MaskSEW}));
2545 return;
2546 }
2547 case Intrinsic::riscv_vsetvli:
2548 case Intrinsic::riscv_vsetvlimax:
2549 return selectVSETVLI(Node);
2550 case Intrinsic::riscv_sf_vsettnt:
2551 case Intrinsic::riscv_sf_vsettm:
2552 case Intrinsic::riscv_sf_vsettk:
2553 return selectXSfmmVSET(Node);
2554 }
2555 break;
2556 }
2558 unsigned IntNo = Node->getConstantOperandVal(1);
2559 switch (IntNo) {
2560 // By default we do not custom select any intrinsic.
2561 default:
2562 break;
2563 case Intrinsic::riscv_vlseg2:
2564 case Intrinsic::riscv_vlseg3:
2565 case Intrinsic::riscv_vlseg4:
2566 case Intrinsic::riscv_vlseg5:
2567 case Intrinsic::riscv_vlseg6:
2568 case Intrinsic::riscv_vlseg7:
2569 case Intrinsic::riscv_vlseg8: {
2570 selectVLSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2571 /*IsStrided*/ false);
2572 return;
2573 }
2574 case Intrinsic::riscv_vlseg2_mask:
2575 case Intrinsic::riscv_vlseg3_mask:
2576 case Intrinsic::riscv_vlseg4_mask:
2577 case Intrinsic::riscv_vlseg5_mask:
2578 case Intrinsic::riscv_vlseg6_mask:
2579 case Intrinsic::riscv_vlseg7_mask:
2580 case Intrinsic::riscv_vlseg8_mask: {
2581 selectVLSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2582 /*IsStrided*/ false);
2583 return;
2584 }
2585 case Intrinsic::riscv_vlsseg2:
2586 case Intrinsic::riscv_vlsseg3:
2587 case Intrinsic::riscv_vlsseg4:
2588 case Intrinsic::riscv_vlsseg5:
2589 case Intrinsic::riscv_vlsseg6:
2590 case Intrinsic::riscv_vlsseg7:
2591 case Intrinsic::riscv_vlsseg8: {
2592 selectVLSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2593 /*IsStrided*/ true);
2594 return;
2595 }
2596 case Intrinsic::riscv_vlsseg2_mask:
2597 case Intrinsic::riscv_vlsseg3_mask:
2598 case Intrinsic::riscv_vlsseg4_mask:
2599 case Intrinsic::riscv_vlsseg5_mask:
2600 case Intrinsic::riscv_vlsseg6_mask:
2601 case Intrinsic::riscv_vlsseg7_mask:
2602 case Intrinsic::riscv_vlsseg8_mask: {
2603 selectVLSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2604 /*IsStrided*/ true);
2605 return;
2606 }
2607 case Intrinsic::riscv_vloxseg2:
2608 case Intrinsic::riscv_vloxseg3:
2609 case Intrinsic::riscv_vloxseg4:
2610 case Intrinsic::riscv_vloxseg5:
2611 case Intrinsic::riscv_vloxseg6:
2612 case Intrinsic::riscv_vloxseg7:
2613 case Intrinsic::riscv_vloxseg8:
2614 selectVLXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2615 /*IsOrdered*/ true);
2616 return;
2617 case Intrinsic::riscv_vluxseg2:
2618 case Intrinsic::riscv_vluxseg3:
2619 case Intrinsic::riscv_vluxseg4:
2620 case Intrinsic::riscv_vluxseg5:
2621 case Intrinsic::riscv_vluxseg6:
2622 case Intrinsic::riscv_vluxseg7:
2623 case Intrinsic::riscv_vluxseg8:
2624 selectVLXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2625 /*IsOrdered*/ false);
2626 return;
2627 case Intrinsic::riscv_vloxseg2_mask:
2628 case Intrinsic::riscv_vloxseg3_mask:
2629 case Intrinsic::riscv_vloxseg4_mask:
2630 case Intrinsic::riscv_vloxseg5_mask:
2631 case Intrinsic::riscv_vloxseg6_mask:
2632 case Intrinsic::riscv_vloxseg7_mask:
2633 case Intrinsic::riscv_vloxseg8_mask:
2634 selectVLXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2635 /*IsOrdered*/ true);
2636 return;
2637 case Intrinsic::riscv_vluxseg2_mask:
2638 case Intrinsic::riscv_vluxseg3_mask:
2639 case Intrinsic::riscv_vluxseg4_mask:
2640 case Intrinsic::riscv_vluxseg5_mask:
2641 case Intrinsic::riscv_vluxseg6_mask:
2642 case Intrinsic::riscv_vluxseg7_mask:
2643 case Intrinsic::riscv_vluxseg8_mask:
2644 selectVLXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2645 /*IsOrdered*/ false);
2646 return;
2647 case Intrinsic::riscv_vlseg8ff:
2648 case Intrinsic::riscv_vlseg7ff:
2649 case Intrinsic::riscv_vlseg6ff:
2650 case Intrinsic::riscv_vlseg5ff:
2651 case Intrinsic::riscv_vlseg4ff:
2652 case Intrinsic::riscv_vlseg3ff:
2653 case Intrinsic::riscv_vlseg2ff: {
2654 selectVLSEGFF(Node, getSegInstNF(IntNo), /*IsMasked*/ false);
2655 return;
2656 }
2657 case Intrinsic::riscv_vlseg8ff_mask:
2658 case Intrinsic::riscv_vlseg7ff_mask:
2659 case Intrinsic::riscv_vlseg6ff_mask:
2660 case Intrinsic::riscv_vlseg5ff_mask:
2661 case Intrinsic::riscv_vlseg4ff_mask:
2662 case Intrinsic::riscv_vlseg3ff_mask:
2663 case Intrinsic::riscv_vlseg2ff_mask: {
2664 selectVLSEGFF(Node, getSegInstNF(IntNo), /*IsMasked*/ true);
2665 return;
2666 }
2667 case Intrinsic::riscv_vloxei:
2668 case Intrinsic::riscv_vloxei_mask:
2669 case Intrinsic::riscv_vluxei:
2670 case Intrinsic::riscv_vluxei_mask: {
2671 bool IsMasked = IntNo == Intrinsic::riscv_vloxei_mask ||
2672 IntNo == Intrinsic::riscv_vluxei_mask;
2673 bool IsOrdered = IntNo == Intrinsic::riscv_vloxei ||
2674 IntNo == Intrinsic::riscv_vloxei_mask;
2675
2676 MVT VT = Node->getSimpleValueType(0);
2677 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2678
2679 unsigned CurOp = 2;
2681 Operands.push_back(Node->getOperand(CurOp++));
2682
2683 MVT IndexVT;
2684 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
2685 /*IsStridedOrIndexed*/ true, Operands,
2686 /*IsLoad=*/true, &IndexVT);
2687
2689 "Element count mismatch");
2690
2693 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
2694 if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
2695 reportFatalUsageError("The V extension does not support EEW=64 for "
2696 "index values when XLEN=32");
2697 }
2698 const RISCV::VLX_VSXPseudo *P = RISCV::getVLXPseudo(
2699 IsMasked, IsOrdered, IndexLog2EEW, static_cast<unsigned>(LMUL),
2700 static_cast<unsigned>(IndexLMUL));
2702 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
2703
2704 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
2705
2707 return;
2708 }
2709 case Intrinsic::riscv_vlm:
2710 case Intrinsic::riscv_vle:
2711 case Intrinsic::riscv_vle_mask:
2712 case Intrinsic::riscv_vlse:
2713 case Intrinsic::riscv_vlse_mask: {
2714 bool IsMasked = IntNo == Intrinsic::riscv_vle_mask ||
2715 IntNo == Intrinsic::riscv_vlse_mask;
2716 bool IsStrided =
2717 IntNo == Intrinsic::riscv_vlse || IntNo == Intrinsic::riscv_vlse_mask;
2718
2719 MVT VT = Node->getSimpleValueType(0);
2720 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2721
2722 // The riscv_vlm intrinsic are always tail agnostic and no passthru
2723 // operand at the IR level. In pseudos, they have both policy and
2724 // passthru operand. The passthru operand is needed to track the
2725 // "tail undefined" state, and the policy is there just for
2726 // for consistency - it will always be "don't care" for the
2727 // unmasked form.
2728 bool HasPassthruOperand = IntNo != Intrinsic::riscv_vlm;
2729 unsigned CurOp = 2;
2731 if (HasPassthruOperand)
2732 Operands.push_back(Node->getOperand(CurOp++));
2733 else {
2734 // We eagerly lower to implicit_def (instead of undef), as we
2735 // otherwise fail to select nodes such as: nxv1i1 = undef
2736 SDNode *Passthru =
2737 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
2738 Operands.push_back(SDValue(Passthru, 0));
2739 }
2740 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
2741 Operands, /*IsLoad=*/true);
2742
2744 const RISCV::VLEPseudo *P =
2745 RISCV::getVLEPseudo(IsMasked, IsStrided, /*FF*/ false, Log2SEW,
2746 static_cast<unsigned>(LMUL));
2748 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
2749
2750 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
2751
2753 return;
2754 }
2755 case Intrinsic::riscv_vleff:
2756 case Intrinsic::riscv_vleff_mask: {
2757 bool IsMasked = IntNo == Intrinsic::riscv_vleff_mask;
2758
2759 MVT VT = Node->getSimpleValueType(0);
2760 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2761
2762 unsigned CurOp = 2;
2764 Operands.push_back(Node->getOperand(CurOp++));
2765 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
2766 /*IsStridedOrIndexed*/ false, Operands,
2767 /*IsLoad=*/true);
2768
2770 const RISCV::VLEPseudo *P =
2771 RISCV::getVLEPseudo(IsMasked, /*Strided*/ false, /*FF*/ true,
2772 Log2SEW, static_cast<unsigned>(LMUL));
2773 MachineSDNode *Load = CurDAG->getMachineNode(
2774 P->Pseudo, DL, Node->getVTList(), Operands);
2775 CurDAG->setNodeMemRefs(Load, {cast<MemSDNode>(Node)->getMemOperand()});
2776
2778 return;
2779 }
2780 case Intrinsic::riscv_nds_vln:
2781 case Intrinsic::riscv_nds_vln_mask:
2782 case Intrinsic::riscv_nds_vlnu:
2783 case Intrinsic::riscv_nds_vlnu_mask: {
2784 bool IsMasked = IntNo == Intrinsic::riscv_nds_vln_mask ||
2785 IntNo == Intrinsic::riscv_nds_vlnu_mask;
2786 bool IsUnsigned = IntNo == Intrinsic::riscv_nds_vlnu ||
2787 IntNo == Intrinsic::riscv_nds_vlnu_mask;
2788
2789 MVT VT = Node->getSimpleValueType(0);
2790 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2791 unsigned CurOp = 2;
2793
2794 Operands.push_back(Node->getOperand(CurOp++));
2795 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
2796 /*IsStridedOrIndexed=*/false, Operands,
2797 /*IsLoad=*/true);
2798
2800 const RISCV::NDSVLNPseudo *P = RISCV::getNDSVLNPseudo(
2801 IsMasked, IsUnsigned, Log2SEW, static_cast<unsigned>(LMUL));
2803 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
2804
2805 if (auto *MemOp = dyn_cast<MemSDNode>(Node))
2806 CurDAG->setNodeMemRefs(Load, {MemOp->getMemOperand()});
2807
2809 return;
2810 }
2811 }
2812 break;
2813 }
2814 case ISD::INTRINSIC_VOID: {
2815 unsigned IntNo = Node->getConstantOperandVal(1);
2816 switch (IntNo) {
2817 case Intrinsic::riscv_vsseg2:
2818 case Intrinsic::riscv_vsseg3:
2819 case Intrinsic::riscv_vsseg4:
2820 case Intrinsic::riscv_vsseg5:
2821 case Intrinsic::riscv_vsseg6:
2822 case Intrinsic::riscv_vsseg7:
2823 case Intrinsic::riscv_vsseg8: {
2824 selectVSSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2825 /*IsStrided*/ false);
2826 return;
2827 }
2828 case Intrinsic::riscv_vsseg2_mask:
2829 case Intrinsic::riscv_vsseg3_mask:
2830 case Intrinsic::riscv_vsseg4_mask:
2831 case Intrinsic::riscv_vsseg5_mask:
2832 case Intrinsic::riscv_vsseg6_mask:
2833 case Intrinsic::riscv_vsseg7_mask:
2834 case Intrinsic::riscv_vsseg8_mask: {
2835 selectVSSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2836 /*IsStrided*/ false);
2837 return;
2838 }
2839 case Intrinsic::riscv_vssseg2:
2840 case Intrinsic::riscv_vssseg3:
2841 case Intrinsic::riscv_vssseg4:
2842 case Intrinsic::riscv_vssseg5:
2843 case Intrinsic::riscv_vssseg6:
2844 case Intrinsic::riscv_vssseg7:
2845 case Intrinsic::riscv_vssseg8: {
2846 selectVSSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2847 /*IsStrided*/ true);
2848 return;
2849 }
2850 case Intrinsic::riscv_vssseg2_mask:
2851 case Intrinsic::riscv_vssseg3_mask:
2852 case Intrinsic::riscv_vssseg4_mask:
2853 case Intrinsic::riscv_vssseg5_mask:
2854 case Intrinsic::riscv_vssseg6_mask:
2855 case Intrinsic::riscv_vssseg7_mask:
2856 case Intrinsic::riscv_vssseg8_mask: {
2857 selectVSSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2858 /*IsStrided*/ true);
2859 return;
2860 }
2861 case Intrinsic::riscv_vsoxseg2:
2862 case Intrinsic::riscv_vsoxseg3:
2863 case Intrinsic::riscv_vsoxseg4:
2864 case Intrinsic::riscv_vsoxseg5:
2865 case Intrinsic::riscv_vsoxseg6:
2866 case Intrinsic::riscv_vsoxseg7:
2867 case Intrinsic::riscv_vsoxseg8:
2868 selectVSXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2869 /*IsOrdered*/ true);
2870 return;
2871 case Intrinsic::riscv_vsuxseg2:
2872 case Intrinsic::riscv_vsuxseg3:
2873 case Intrinsic::riscv_vsuxseg4:
2874 case Intrinsic::riscv_vsuxseg5:
2875 case Intrinsic::riscv_vsuxseg6:
2876 case Intrinsic::riscv_vsuxseg7:
2877 case Intrinsic::riscv_vsuxseg8:
2878 selectVSXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ false,
2879 /*IsOrdered*/ false);
2880 return;
2881 case Intrinsic::riscv_vsoxseg2_mask:
2882 case Intrinsic::riscv_vsoxseg3_mask:
2883 case Intrinsic::riscv_vsoxseg4_mask:
2884 case Intrinsic::riscv_vsoxseg5_mask:
2885 case Intrinsic::riscv_vsoxseg6_mask:
2886 case Intrinsic::riscv_vsoxseg7_mask:
2887 case Intrinsic::riscv_vsoxseg8_mask:
2888 selectVSXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2889 /*IsOrdered*/ true);
2890 return;
2891 case Intrinsic::riscv_vsuxseg2_mask:
2892 case Intrinsic::riscv_vsuxseg3_mask:
2893 case Intrinsic::riscv_vsuxseg4_mask:
2894 case Intrinsic::riscv_vsuxseg5_mask:
2895 case Intrinsic::riscv_vsuxseg6_mask:
2896 case Intrinsic::riscv_vsuxseg7_mask:
2897 case Intrinsic::riscv_vsuxseg8_mask:
2898 selectVSXSEG(Node, getSegInstNF(IntNo), /*IsMasked*/ true,
2899 /*IsOrdered*/ false);
2900 return;
2901 case Intrinsic::riscv_vsoxei:
2902 case Intrinsic::riscv_vsoxei_mask:
2903 case Intrinsic::riscv_vsuxei:
2904 case Intrinsic::riscv_vsuxei_mask: {
2905 bool IsMasked = IntNo == Intrinsic::riscv_vsoxei_mask ||
2906 IntNo == Intrinsic::riscv_vsuxei_mask;
2907 bool IsOrdered = IntNo == Intrinsic::riscv_vsoxei ||
2908 IntNo == Intrinsic::riscv_vsoxei_mask;
2909
2910 MVT VT = Node->getOperand(2)->getSimpleValueType(0);
2911 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2912
2913 unsigned CurOp = 2;
2915 Operands.push_back(Node->getOperand(CurOp++)); // Store value.
2916
2917 MVT IndexVT;
2918 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked,
2919 /*IsStridedOrIndexed*/ true, Operands,
2920 /*IsLoad=*/false, &IndexVT);
2921
2923 "Element count mismatch");
2924
2927 unsigned IndexLog2EEW = Log2_32(IndexVT.getScalarSizeInBits());
2928 if (IndexLog2EEW == 6 && !Subtarget->is64Bit()) {
2929 reportFatalUsageError("The V extension does not support EEW=64 for "
2930 "index values when XLEN=32");
2931 }
2932 const RISCV::VLX_VSXPseudo *P = RISCV::getVSXPseudo(
2933 IsMasked, IsOrdered, IndexLog2EEW,
2934 static_cast<unsigned>(LMUL), static_cast<unsigned>(IndexLMUL));
2936 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
2937
2938 CurDAG->setNodeMemRefs(Store, {cast<MemSDNode>(Node)->getMemOperand()});
2939
2941 return;
2942 }
2943 case Intrinsic::riscv_vsm:
2944 case Intrinsic::riscv_vse:
2945 case Intrinsic::riscv_vse_mask:
2946 case Intrinsic::riscv_vsse:
2947 case Intrinsic::riscv_vsse_mask: {
2948 bool IsMasked = IntNo == Intrinsic::riscv_vse_mask ||
2949 IntNo == Intrinsic::riscv_vsse_mask;
2950 bool IsStrided =
2951 IntNo == Intrinsic::riscv_vsse || IntNo == Intrinsic::riscv_vsse_mask;
2952
2953 MVT VT = Node->getOperand(2)->getSimpleValueType(0);
2954 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
2955
2956 unsigned CurOp = 2;
2958 Operands.push_back(Node->getOperand(CurOp++)); // Store value.
2959
2960 addVectorLoadStoreOperands(Node, Log2SEW, DL, CurOp, IsMasked, IsStrided,
2961 Operands);
2962
2964 const RISCV::VSEPseudo *P = RISCV::getVSEPseudo(
2965 IsMasked, IsStrided, Log2SEW, static_cast<unsigned>(LMUL));
2967 CurDAG->getMachineNode(P->Pseudo, DL, Node->getVTList(), Operands);
2968 CurDAG->setNodeMemRefs(Store, {cast<MemSDNode>(Node)->getMemOperand()});
2969
2971 return;
2972 }
2973 case Intrinsic::riscv_sf_vc_x_se:
2974 case Intrinsic::riscv_sf_vc_i_se:
2976 return;
2977 case Intrinsic::riscv_sf_vlte8:
2978 case Intrinsic::riscv_sf_vlte16:
2979 case Intrinsic::riscv_sf_vlte32:
2980 case Intrinsic::riscv_sf_vlte64: {
2981 unsigned Log2SEW;
2982 unsigned PseudoInst;
2983 switch (IntNo) {
2984 case Intrinsic::riscv_sf_vlte8:
2985 PseudoInst = RISCV::PseudoSF_VLTE8;
2986 Log2SEW = 3;
2987 break;
2988 case Intrinsic::riscv_sf_vlte16:
2989 PseudoInst = RISCV::PseudoSF_VLTE16;
2990 Log2SEW = 4;
2991 break;
2992 case Intrinsic::riscv_sf_vlte32:
2993 PseudoInst = RISCV::PseudoSF_VLTE32;
2994 Log2SEW = 5;
2995 break;
2996 case Intrinsic::riscv_sf_vlte64:
2997 PseudoInst = RISCV::PseudoSF_VLTE64;
2998 Log2SEW = 6;
2999 break;
3000 }
3001
3002 SDValue SEWOp = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
3003 SDValue TWidenOp = CurDAG->getTargetConstant(1, DL, XLenVT);
3004 SDValue Operands[] = {Node->getOperand(2),
3005 Node->getOperand(3),
3006 Node->getOperand(4),
3007 SEWOp,
3008 TWidenOp,
3009 Node->getOperand(0)};
3010
3011 MachineSDNode *TileLoad =
3012 CurDAG->getMachineNode(PseudoInst, DL, Node->getVTList(), Operands);
3013 CurDAG->setNodeMemRefs(TileLoad,
3014 {cast<MemSDNode>(Node)->getMemOperand()});
3015
3016 ReplaceNode(Node, TileLoad);
3017 return;
3018 }
3019 case Intrinsic::riscv_sf_mm_s_s:
3020 case Intrinsic::riscv_sf_mm_s_u:
3021 case Intrinsic::riscv_sf_mm_u_s:
3022 case Intrinsic::riscv_sf_mm_u_u:
3023 case Intrinsic::riscv_sf_mm_e5m2_e5m2:
3024 case Intrinsic::riscv_sf_mm_e5m2_e4m3:
3025 case Intrinsic::riscv_sf_mm_e4m3_e5m2:
3026 case Intrinsic::riscv_sf_mm_e4m3_e4m3:
3027 case Intrinsic::riscv_sf_mm_f_f: {
3028 bool HasFRM = false;
3029 unsigned PseudoInst;
3030 switch (IntNo) {
3031 case Intrinsic::riscv_sf_mm_s_s:
3032 PseudoInst = RISCV::PseudoSF_MM_S_S;
3033 break;
3034 case Intrinsic::riscv_sf_mm_s_u:
3035 PseudoInst = RISCV::PseudoSF_MM_S_U;
3036 break;
3037 case Intrinsic::riscv_sf_mm_u_s:
3038 PseudoInst = RISCV::PseudoSF_MM_U_S;
3039 break;
3040 case Intrinsic::riscv_sf_mm_u_u:
3041 PseudoInst = RISCV::PseudoSF_MM_U_U;
3042 break;
3043 case Intrinsic::riscv_sf_mm_e5m2_e5m2:
3044 PseudoInst = RISCV::PseudoSF_MM_E5M2_E5M2;
3045 HasFRM = true;
3046 break;
3047 case Intrinsic::riscv_sf_mm_e5m2_e4m3:
3048 PseudoInst = RISCV::PseudoSF_MM_E5M2_E4M3;
3049 HasFRM = true;
3050 break;
3051 case Intrinsic::riscv_sf_mm_e4m3_e5m2:
3052 PseudoInst = RISCV::PseudoSF_MM_E4M3_E5M2;
3053 HasFRM = true;
3054 break;
3055 case Intrinsic::riscv_sf_mm_e4m3_e4m3:
3056 PseudoInst = RISCV::PseudoSF_MM_E4M3_E4M3;
3057 HasFRM = true;
3058 break;
3059 case Intrinsic::riscv_sf_mm_f_f:
3060 if (Node->getOperand(3).getValueType().getScalarType() == MVT::bf16)
3061 PseudoInst = RISCV::PseudoSF_MM_F_F_ALT;
3062 else
3063 PseudoInst = RISCV::PseudoSF_MM_F_F;
3064 HasFRM = true;
3065 break;
3066 }
3067 uint64_t TileNum = Node->getConstantOperandVal(2);
3068 SDValue Op1 = Node->getOperand(3);
3069 SDValue Op2 = Node->getOperand(4);
3070 MVT VT = Op1->getSimpleValueType(0);
3071 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
3072
3073 // Both matrix operands must have the same type, which must be an LMUL=8
3074 // register group with the expected element type.
3075 if (!VT.isScalableVector() ||
3077 reportFatalUsageError("sf.mm operands must have LMUL=8");
3078 // sf.mm.f.f takes floating point elements. The others take 8-bit
3079 // elements.
3080 if (IntNo == Intrinsic::riscv_sf_mm_f_f
3081 ? !VT.isFloatingPoint()
3082 : VT.getVectorElementType() != MVT::i8)
3083 reportFatalUsageError("unsupported element type for sf.mm operand");
3084
3085 SDValue TmOp = Node->getOperand(5);
3086 SDValue TnOp = Node->getOperand(6);
3087 SDValue TkOp = Node->getOperand(7);
3088 SDValue TWidenOp = Node->getOperand(8);
3089 SDValue Chain = Node->getOperand(0);
3090
3091 // sf.mm.f.f with sew=32, twiden=2 is invalid
3092 if (IntNo == Intrinsic::riscv_sf_mm_f_f && Log2SEW == 5 &&
3093 TWidenOp->getAsZExtVal() == 2)
3094 reportFatalUsageError("sf.mm.f.f doesn't support (sew=32, twiden=2)");
3095
3097 {CurDAG->getRegister(getTileReg(TileNum), XLenVT), Op1, Op2});
3098 if (HasFRM)
3099 Operands.push_back(
3100 CurDAG->getTargetConstant(RISCVFPRndMode::DYN, DL, XLenVT));
3101 Operands.append({TmOp, TnOp, TkOp,
3102 CurDAG->getTargetConstant(Log2SEW, DL, XLenVT), TWidenOp,
3103 Chain});
3104
3105 auto *NewNode =
3106 CurDAG->getMachineNode(PseudoInst, DL, Node->getVTList(), Operands);
3107
3108 ReplaceNode(Node, NewNode);
3109 return;
3110 }
3111 case Intrinsic::riscv_sf_vtzero_t: {
3112 uint64_t TileNum = Node->getConstantOperandVal(2);
3113 SDValue Tm = Node->getOperand(3);
3114 SDValue Tn = Node->getOperand(4);
3115 SDValue Log2SEW = Node->getOperand(5);
3116 SDValue TWiden = Node->getOperand(6);
3117 SDValue Chain = Node->getOperand(0);
3118 auto *NewNode = CurDAG->getMachineNode(
3119 RISCV::PseudoSF_VTZERO_T, DL, Node->getVTList(),
3120 {CurDAG->getRegister(getTileReg(TileNum), XLenVT), Tm, Tn, Log2SEW,
3121 TWiden, Chain});
3122
3123 ReplaceNode(Node, NewNode);
3124 return;
3125 }
3126 }
3127 break;
3128 }
3129 case ISD::BITCAST: {
3130 MVT SrcVT = Node->getOperand(0).getSimpleValueType();
3131 // Just drop bitcasts between vectors if both are fixed or both are
3132 // scalable.
3133 if ((VT.isScalableVector() && SrcVT.isScalableVector()) ||
3134 (VT.isFixedLengthVector() && SrcVT.isFixedLengthVector())) {
3135 ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
3136 CurDAG->RemoveDeadNode(Node);
3137 return;
3138 }
3139 if (Subtarget->hasStdExtP()) {
3140 bool Is32BitCast =
3141 (VT == MVT::i32 && (SrcVT == MVT::v4i8 || SrcVT == MVT::v2i16)) ||
3142 (SrcVT == MVT::i32 && (VT == MVT::v4i8 || VT == MVT::v2i16));
3143 bool Is64BitCast =
3144 (VT == MVT::i64 && (SrcVT == MVT::v8i8 || SrcVT == MVT::v4i16 ||
3145 SrcVT == MVT::v2i32)) ||
3146 (SrcVT == MVT::i64 &&
3147 (VT == MVT::v8i8 || VT == MVT::v4i16 || VT == MVT::v2i32));
3148 if (Is32BitCast || Is64BitCast) {
3149 ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
3150 CurDAG->RemoveDeadNode(Node);
3151 return;
3152 }
3153 }
3154 break;
3155 }
3156 case ISD::SPLAT_VECTOR: {
3157 if (!Subtarget->hasStdExtP())
3158 break;
3159 if (auto *ConstNode = dyn_cast<ConstantSDNode>(Node->getOperand(0))) {
3160 bool IsDoubleWide = Subtarget->isPExtPackedDoubleType(VT);
3161
3162 if (ConstNode->isZero()) {
3163 MCPhysReg X0Reg = IsDoubleWide ? RISCV::X0_Pair : RISCV::X0;
3164 SDValue New =
3165 CurDAG->getCopyFromReg(CurDAG->getEntryNode(), DL, X0Reg, VT);
3166 ReplaceNode(Node, New.getNode());
3167 return;
3168 }
3169
3170 unsigned EltSize = VT.getVectorElementType().getSizeInBits();
3171 APInt Val = ConstNode->getAPIntValue().trunc(EltSize);
3172
3173 // Use LI for all ones since it can be compressed to c.li.
3174 if (Val.isAllOnes() && !IsDoubleWide) {
3175 SDNode *NewNode = CurDAG->getMachineNode(
3176 RISCV::ADDI, DL, VT, CurDAG->getRegister(RISCV::X0, VT),
3177 CurDAG->getAllOnesConstant(DL, XLenVT, /*IsTarget=*/true));
3178 ReplaceNode(Node, NewNode);
3179 return;
3180 }
3181
3182 // Find the smallest splat.
3183 if (Val.getBitWidth() > 16 && Val.isSplat(16))
3184 Val = Val.trunc(16);
3185 if (Val.getBitWidth() > 8 && Val.isSplat(8))
3186 Val = Val.trunc(8);
3187
3188 EltSize = Val.getBitWidth();
3189 int64_t Imm = Val.getSExtValue();
3190
3191 unsigned Opc = 0;
3192 if (EltSize == 8) {
3193 Opc = IsDoubleWide ? RISCV::PLI_DB : RISCV::PLI_B;
3194 } else if (EltSize == 16 && isInt<10>(Imm)) {
3195 Opc = IsDoubleWide ? RISCV::PLI_DH : RISCV::PLI_H;
3196 } else if (!IsDoubleWide && EltSize == 32 && isInt<10>(Imm)) {
3197 Opc = RISCV::PLI_W;
3198 } else if (EltSize == 16 && isShiftedInt<10, 6>(Imm)) {
3199 Opc = IsDoubleWide ? RISCV::PLUI_DH : RISCV::PLUI_H;
3200 Imm = Imm >> 6;
3201 } else if (!IsDoubleWide && EltSize == 32 && isShiftedInt<10, 22>(Imm)) {
3202 Opc = RISCV::PLUI_W;
3203 Imm = Imm >> 22;
3204 }
3205
3206 if (Opc) {
3207 SDNode *NewNode = CurDAG->getMachineNode(
3208 Opc, DL, VT, CurDAG->getSignedTargetConstant(Imm, DL, XLenVT));
3209 ReplaceNode(Node, NewNode);
3210 return;
3211 }
3212 }
3213
3214 break;
3215 }
3217 if (Subtarget->hasStdExtP()) {
3218 MVT SrcVT = Node->getOperand(0).getSimpleValueType();
3219 if (SrcVT == Subtarget->getXLenVT() &&
3220 VT.getSizeInBits() == SrcVT.getSizeInBits()) {
3221 ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
3222 CurDAG->RemoveDeadNode(Node);
3223 return;
3224 }
3225 }
3226 break;
3228 case RISCVISD::TUPLE_INSERT: {
3229 SDValue V = Node->getOperand(0);
3230 SDValue SubV = Node->getOperand(1);
3231 SDLoc DL(SubV);
3232 auto Idx = Node->getConstantOperandVal(2);
3233 MVT SubVecVT = SubV.getSimpleValueType();
3234
3235 const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
3236 MVT SubVecContainerVT = SubVecVT;
3237 // Establish the correct scalable-vector types for any fixed-length type.
3238 if (SubVecVT.isFixedLengthVector()) {
3239 SubVecContainerVT = TLI.getContainerForFixedLengthVector(SubVecVT);
3241 [[maybe_unused]] bool ExactlyVecRegSized =
3242 Subtarget->expandVScale(SubVecVT.getSizeInBits())
3243 .isKnownMultipleOf(Subtarget->expandVScale(VecRegSize));
3244 assert(isPowerOf2_64(Subtarget->expandVScale(SubVecVT.getSizeInBits())
3245 .getKnownMinValue()));
3246 assert(Idx == 0 && (ExactlyVecRegSized || V.isUndef()));
3247 }
3248 MVT ContainerVT = VT;
3249 if (VT.isFixedLengthVector())
3250 ContainerVT = TLI.getContainerForFixedLengthVector(VT);
3251
3252 const auto *TRI = Subtarget->getRegisterInfo();
3253 unsigned SubRegIdx;
3254 std::tie(SubRegIdx, Idx) =
3256 ContainerVT, SubVecContainerVT, Idx, TRI);
3257
3258 // If the Idx hasn't been completely eliminated then this is a subvector
3259 // insert which doesn't naturally align to a vector register. These must
3260 // be handled using instructions to manipulate the vector registers.
3261 if (Idx != 0)
3262 break;
3263
3264 RISCVVType::VLMUL SubVecLMUL =
3265 RISCVTargetLowering::getLMUL(SubVecContainerVT);
3266 [[maybe_unused]] bool IsSubVecPartReg =
3267 SubVecLMUL == RISCVVType::VLMUL::LMUL_F2 ||
3268 SubVecLMUL == RISCVVType::VLMUL::LMUL_F4 ||
3269 SubVecLMUL == RISCVVType::VLMUL::LMUL_F8;
3270 assert((V.getValueType().isRISCVVectorTuple() || !IsSubVecPartReg ||
3271 V.isUndef()) &&
3272 "Expecting lowering to have created legal INSERT_SUBVECTORs when "
3273 "the subvector is smaller than a full-sized register");
3274
3275 // If we haven't set a SubRegIdx, then we must be going between
3276 // equally-sized LMUL groups (e.g. VR -> VR). This can be done as a copy.
3277 if (SubRegIdx == RISCV::NoSubRegister) {
3278 unsigned InRegClassID =
3281 InRegClassID &&
3282 "Unexpected subvector extraction");
3283 SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
3284 SDNode *NewNode = CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
3285 DL, VT, SubV, RC);
3286 ReplaceNode(Node, NewNode);
3287 return;
3288 }
3289
3290 SDValue Insert = CurDAG->getTargetInsertSubreg(SubRegIdx, DL, VT, V, SubV);
3291 ReplaceNode(Node, Insert.getNode());
3292 return;
3293 }
3294 case RISCVISD::TUPLE_CAST: {
3295 // TUPLE_CAST reinterprets a vector tuple as a different tuple type with
3296 // the same register class (same NF and LMUL), just a different minimum
3297 // number of elements per field. The two types occupy identical
3298 // registers, so just forward the operand like we do for same-register-
3299 // class ISD::BITCAST, without emitting a copy.
3302 Node->getOperand(0).getSimpleValueType()) &&
3303 "Expected input and output of TUPLE_CAST to use the same "
3304 "register class");
3305 ReplaceUses(SDValue(Node, 0), Node->getOperand(0));
3306 CurDAG->RemoveDeadNode(Node);
3307 return;
3308 }
3310 case RISCVISD::TUPLE_EXTRACT: {
3311 if (Subtarget->hasStdExtP())
3312 break;
3313
3314 SDValue V = Node->getOperand(0);
3315 auto Idx = Node->getConstantOperandVal(1);
3316 MVT InVT = V.getSimpleValueType();
3317
3318 SDLoc DL(V);
3319
3320 const RISCVTargetLowering &TLI = *Subtarget->getTargetLowering();
3321 MVT SubVecContainerVT = VT;
3322 // Establish the correct scalable-vector types for any fixed-length type.
3323 if (VT.isFixedLengthVector()) {
3324 assert(Idx == 0);
3325 SubVecContainerVT = TLI.getContainerForFixedLengthVector(VT);
3326 }
3327 if (InVT.isFixedLengthVector())
3328 InVT = TLI.getContainerForFixedLengthVector(InVT);
3329
3330 const auto *TRI = Subtarget->getRegisterInfo();
3331 unsigned SubRegIdx;
3332 std::tie(SubRegIdx, Idx) =
3334 InVT, SubVecContainerVT, Idx, TRI);
3335
3336 // If the Idx hasn't been completely eliminated then this is a subvector
3337 // extract which doesn't naturally align to a vector register. These must
3338 // be handled using instructions to manipulate the vector registers.
3339 if (Idx != 0)
3340 break;
3341
3342 // If we haven't set a SubRegIdx, then we must be going between
3343 // equally-sized LMUL types (e.g. VR -> VR). This can be done as a copy.
3344 if (SubRegIdx == RISCV::NoSubRegister) {
3345 unsigned InRegClassID = RISCVTargetLowering::getRegClassIDForVecVT(InVT);
3347 InRegClassID &&
3348 "Unexpected subvector extraction");
3349 SDValue RC = CurDAG->getTargetConstant(InRegClassID, DL, XLenVT);
3350 SDNode *NewNode =
3351 CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, DL, VT, V, RC);
3352 ReplaceNode(Node, NewNode);
3353 return;
3354 }
3355
3356 SDValue Extract = CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, V);
3357 ReplaceNode(Node, Extract.getNode());
3358 return;
3359 }
3360 case RISCVISD::VMV_S_X_VL:
3361 case RISCVISD::VFMV_S_F_VL:
3362 case RISCVISD::VMV_V_X_VL:
3363 case RISCVISD::VFMV_V_F_VL: {
3364 // Try to match splat of a scalar load to a strided load with stride of x0.
3365 bool IsScalarMove = Node->getOpcode() == RISCVISD::VMV_S_X_VL ||
3366 Node->getOpcode() == RISCVISD::VFMV_S_F_VL;
3367 if (!Node->getOperand(0).isUndef())
3368 break;
3369 SDValue Src = Node->getOperand(1);
3370 auto *Ld = dyn_cast<LoadSDNode>(Src);
3371 // Can't fold load update node because the second
3372 // output is used so that load update node can't be removed.
3373 if (!Ld || Ld->isIndexed())
3374 break;
3375 EVT MemVT = Ld->getMemoryVT();
3376 // The memory VT should be the same size as the element type.
3377 if (MemVT.getStoreSize() != VT.getVectorElementType().getStoreSize())
3378 break;
3379 if (!IsProfitableToFold(Src, Node, Node) ||
3380 !IsLegalToFold(Src, Node, Node, TM.getOptLevel()))
3381 break;
3382
3383 SDValue VL;
3384 if (IsScalarMove) {
3385 // We could deal with more VL if we update the VSETVLI insert pass to
3386 // avoid introducing more VSETVLI.
3387 if (!isOneConstant(Node->getOperand(2)))
3388 break;
3389 selectVLOp(Node->getOperand(2), VL);
3390 } else
3391 selectVLOp(Node->getOperand(2), VL);
3392
3393 unsigned Log2SEW = Log2_32(VT.getScalarSizeInBits());
3394 SDValue SEW = CurDAG->getTargetConstant(Log2SEW, DL, XLenVT);
3395
3396 // If VL=1, then we don't need to do a strided load and can just do a
3397 // regular load.
3398 bool IsStrided = !isOneConstant(VL);
3399
3400 // Only do a strided load if we have optimized zero-stride vector load.
3401 if (IsStrided && !Subtarget->hasOptimizedZeroStrideLoad())
3402 break;
3403
3405 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT), 0),
3406 Ld->getBasePtr()};
3407 if (IsStrided)
3408 Operands.push_back(CurDAG->getRegister(RISCV::X0, XLenVT));
3410 SDValue PolicyOp = CurDAG->getTargetConstant(Policy, DL, XLenVT);
3411 Operands.append({VL, SEW, PolicyOp, Ld->getChain()});
3412
3414 const RISCV::VLEPseudo *P = RISCV::getVLEPseudo(
3415 /*IsMasked*/ false, IsStrided, /*FF*/ false,
3416 Log2SEW, static_cast<unsigned>(LMUL));
3418 CurDAG->getMachineNode(P->Pseudo, DL, {VT, MVT::Other}, Operands);
3419 // Update the chain.
3420 ReplaceUses(Src.getValue(1), SDValue(Load, 1));
3421 // Record the mem-refs
3422 CurDAG->setNodeMemRefs(Load, {Ld->getMemOperand()});
3423 // Replace the splat with the vlse.
3425 return;
3426 }
3427 case RISCVISD::LPAD_CALL:
3428 case RISCVISD::LPAD_CALL_INDIRECT: {
3429 bool IsIndirect = Opcode == RISCVISD::LPAD_CALL_INDIRECT;
3430 unsigned PseudoOpc = IsIndirect ? RISCV::PseudoCALLIndirectLpadAlign
3431 : RISCV::PseudoCALLLpadAlign;
3432
3433 uint32_t LpadLabel = 0;
3434 if (std::optional<uint32_t> Label =
3435 Subtarget->getCLOpts().landing_pad_label) {
3436 if (!isUInt<20>(*Label))
3437 report_fatal_error("riscv-landing-pad-label=<val>, <val> needs to fit "
3438 "in unsigned 20-bits");
3439 LpadLabel = *Label;
3440 }
3441
3442 // Preserve the argument-register and register-mask operands, between
3443 // Callee and the optional glue, so the pseudo call still reports its
3444 // call-preserved mask to the register allocator.
3446 Ops.push_back(Node->getOperand(1));
3447 Ops.push_back(CurDAG->getTargetConstant(LpadLabel, DL, XLenVT));
3448
3449 unsigned NumOps = Node->getNumOperands();
3450 bool HasGlue = Node->getGluedNode() != nullptr;
3451 unsigned RegOperandsEnd = HasGlue ? NumOps - 1 : NumOps;
3452 for (unsigned I = 2; I != RegOperandsEnd; ++I)
3453 Ops.push_back(Node->getOperand(I));
3454
3455 Ops.push_back(Node->getOperand(0));
3456 if (HasGlue)
3457 Ops.push_back(Node->getOperand(NumOps - 1));
3458
3460 CurDAG->getMachineNode(PseudoOpc, DL, Node->getVTList(), Ops));
3461 return;
3462 }
3463 case ISD::PREFETCH:
3464 // MIPS's prefetch instruction already encodes the hint within the
3465 // instruction itself, so no extra NTL hint is needed.
3466 if (Subtarget->hasVendorXMIPSCBOP())
3467 break;
3468
3469 unsigned Locality = Node->getConstantOperandVal(3);
3470 if (Locality > 2)
3471 break;
3472
3473 auto *LoadStoreMem = cast<MemSDNode>(Node);
3474 MachineMemOperand *MMO = LoadStoreMem->getMemOperand();
3476
3477 int NontemporalLevel = 0;
3478 switch (Locality) {
3479 case 0:
3480 NontemporalLevel = 3; // NTL.ALL
3481 break;
3482 case 1:
3483 NontemporalLevel = 1; // NTL.PALL
3484 break;
3485 case 2:
3486 NontemporalLevel = 0; // NTL.P1
3487 break;
3488 default:
3489 llvm_unreachable("unexpected locality value.");
3490 }
3491
3492 if (NontemporalLevel & 0b1)
3494 if (NontemporalLevel & 0b10)
3496 break;
3497 }
3498
3499 // Select the default instruction.
3500 SelectCode(Node);
3501}
3502
3504 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
3505 std::vector<SDValue> &OutOps) {
3506 // Always produce a register and immediate operand, as expected by
3507 // RISCVAsmPrinter::PrintAsmMemoryOperand.
3508 switch (ConstraintID) {
3511 SDValue Op0, Op1;
3512 [[maybe_unused]] bool Found = SelectAddrRegImm(Op, Op0, Op1);
3513 assert(Found && "SelectAddrRegImm should always succeed");
3514 OutOps.push_back(Op0);
3515 OutOps.push_back(Op1);
3516 return false;
3517 }
3519 OutOps.push_back(Op);
3520 OutOps.push_back(
3521 CurDAG->getTargetConstant(0, SDLoc(Op), Subtarget->getXLenVT()));
3522 return false;
3523 default:
3524 report_fatal_error("Unexpected asm memory constraint " +
3525 InlineAsm::getMemConstraintName(ConstraintID));
3526 }
3527
3528 return true;
3529}
3530
3532 SDValue &Offset) {
3533 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
3534 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), Subtarget->getXLenVT());
3535 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), Subtarget->getXLenVT());
3536 return true;
3537 }
3538
3539 return false;
3540}
3541
3542// Fold constant addresses.
3543static bool selectConstantAddr(SelectionDAG *CurDAG, const SDLoc &DL,
3544 const MVT VT, const RISCVSubtarget *Subtarget,
3546 bool IsPrefetch = false) {
3547 if (!isa<ConstantSDNode>(Addr))
3548 return false;
3549
3550 int64_t CVal = cast<ConstantSDNode>(Addr)->getSExtValue();
3551
3552 // If the constant is a simm12, we can fold the whole constant and use X0 as
3553 // the base. If the constant can be materialized with LUI+simm12, use LUI as
3554 // the base. We can't use generateInstSeq because it favors LUI+ADDIW.
3555 int64_t Lo12 = SignExtend64<12>(CVal);
3556 int64_t Hi = (uint64_t)CVal - (uint64_t)Lo12;
3557 if (!Subtarget->is64Bit() || isInt<32>(Hi)) {
3558 if (IsPrefetch && (Lo12 & 0b11111) != 0)
3559 return false;
3560 if (Hi) {
3561 int64_t Hi20 = (Hi >> 12) & 0xfffff;
3562 Base = SDValue(
3563 CurDAG->getMachineNode(RISCV::LUI, DL, VT,
3564 CurDAG->getTargetConstant(Hi20, DL, VT)),
3565 0);
3566 } else {
3567 Base = CurDAG->getRegister(RISCV::X0, VT);
3568 }
3569 Offset = CurDAG->getSignedTargetConstant(Lo12, DL, VT);
3570 return true;
3571 }
3572
3573 // Ask how constant materialization would handle this constant.
3575
3576 // If the last instruction would be an ADDI, we can fold its immediate and
3577 // emit the rest of the sequence as the base.
3578 if (Seq.back().getOpcode() != RISCV::ADDI)
3579 return false;
3580 Lo12 = Seq.back().getImm();
3581 if (IsPrefetch && (Lo12 & 0b11111) != 0)
3582 return false;
3583
3584 // Drop the last instruction.
3585 Seq.pop_back();
3586 assert(!Seq.empty() && "Expected more instructions in sequence");
3587
3588 Base = selectImmSeq(CurDAG, DL, VT, Seq);
3589 Offset = CurDAG->getSignedTargetConstant(Lo12, DL, VT);
3590 return true;
3591}
3592
3593// Is this ADD instruction only used as the base pointer of scalar loads and
3594// stores?
3596 for (auto *User : Add->users()) {
3597 if (User->getOpcode() != ISD::LOAD && User->getOpcode() != ISD::STORE &&
3598 User->getOpcode() != RISCVISD::LD_RV32 &&
3599 User->getOpcode() != RISCVISD::SD_RV32 &&
3600 User->getOpcode() != ISD::ATOMIC_LOAD &&
3601 User->getOpcode() != ISD::ATOMIC_STORE)
3602 return false;
3603 EVT VT = cast<MemSDNode>(User)->getMemoryVT();
3604 if (!VT.isScalarInteger() && VT != MVT::f16 && VT != MVT::f32 &&
3605 VT != MVT::f64)
3606 return false;
3607 // Don't allow stores of the value. It must be used as the address.
3608 if (User->getOpcode() == ISD::STORE &&
3609 cast<StoreSDNode>(User)->getValue() == Add)
3610 return false;
3611 if (User->getOpcode() == ISD::ATOMIC_STORE &&
3612 cast<AtomicSDNode>(User)->getVal() == Add)
3613 return false;
3614 if (User->getOpcode() == RISCVISD::SD_RV32 &&
3615 (User->getOperand(0) == Add || User->getOperand(1) == Add))
3616 return false;
3617 if (isStrongerThanMonotonic(cast<MemSDNode>(User)->getSuccessOrdering()))
3618 return false;
3619 }
3620
3621 return true;
3622}
3623
3625 switch (User->getOpcode()) {
3626 default:
3627 return false;
3628 case ISD::LOAD:
3629 case RISCVISD::LD_RV32:
3630 case ISD::ATOMIC_LOAD:
3631 break;
3632 case ISD::STORE:
3633 // Don't allow stores of Add. It must only be used as the address.
3635 return false;
3636 break;
3637 case RISCVISD::SD_RV32:
3638 // Don't allow stores of Add. It must only be used as the address.
3639 if (User->getOperand(0) == Add || User->getOperand(1) == Add)
3640 return false;
3641 break;
3642 case ISD::ATOMIC_STORE:
3643 // Don't allow stores of Add. It must only be used as the address.
3644 if (cast<AtomicSDNode>(User)->getVal() == Add)
3645 return false;
3646 break;
3647 }
3648
3649 return true;
3650}
3651
3652// To prevent SelectAddrRegImm from folding offsets that conflict with the
3653// fusion of PseudoMovAddr, check if the offset of every use of a given address
3654// is within the alignment.
3656 Align Alignment) {
3657 assert(Addr->getOpcode() == RISCVISD::ADD_LO);
3658 for (auto *User : Addr->users()) {
3659 // If the user is a load or store, then the offset is 0 which is always
3660 // within alignment.
3661 if (isRegImmLoadOrStore(User, Addr))
3662 continue;
3663
3664 if (CurDAG->isBaseWithConstantOffset(SDValue(User, 0))) {
3665 int64_t CVal = cast<ConstantSDNode>(User->getOperand(1))->getSExtValue();
3666 if (!isInt<12>(CVal) || Alignment <= CVal)
3667 return false;
3668
3669 // Make sure all uses are foldable load/stores.
3670 for (auto *AddUser : User->users())
3671 if (!isRegImmLoadOrStore(AddUser, SDValue(User, 0)))
3672 return false;
3673
3674 continue;
3675 }
3676
3677 return false;
3678 }
3679
3680 return true;
3681}
3682
3684 SDValue &Offset) {
3685 if (SelectAddrFrameIndex(Addr, Base, Offset))
3686 return true;
3687
3688 SDLoc DL(Addr);
3689 MVT VT = Addr.getSimpleValueType();
3690
3691 if (Addr.getOpcode() == RISCVISD::ADD_LO) {
3692 bool CanFold = true;
3693 // Unconditionally fold if operand 1 is not a global address (e.g.
3694 // externsymbol)
3695 if (auto *GA = dyn_cast<GlobalAddressSDNode>(Addr.getOperand(1))) {
3696 const DataLayout &DL = CurDAG->getDataLayout();
3697 Align Alignment = commonAlignment(
3698 GA->getGlobal()->getPointerAlignment(DL), GA->getOffset());
3699 if (!areOffsetsWithinAlignment(Addr, Alignment))
3700 CanFold = false;
3701 }
3702 if (CanFold) {
3703 Base = Addr.getOperand(0);
3704 Offset = Addr.getOperand(1);
3705 return true;
3706 }
3707 }
3708
3709 if (CurDAG->isBaseWithConstantOffset(Addr)) {
3710 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3711 if (isInt<12>(CVal)) {
3712 Base = Addr.getOperand(0);
3713 if (Base.getOpcode() == RISCVISD::ADD_LO) {
3714 SDValue LoOperand = Base.getOperand(1);
3715 if (auto *GA = dyn_cast<GlobalAddressSDNode>(LoOperand)) {
3716 // If the Lo in (ADD_LO hi, lo) is a global variable's address
3717 // (its low part, really), then we can rely on the alignment of that
3718 // variable to provide a margin of safety before low part can overflow
3719 // the 12 bits of the load/store offset. Check if CVal falls within
3720 // that margin; if so (low part + CVal) can't overflow.
3721 const DataLayout &DL = CurDAG->getDataLayout();
3722 Align Alignment = commonAlignment(
3723 GA->getGlobal()->getPointerAlignment(DL), GA->getOffset());
3724 if ((CVal == 0 || Alignment > CVal) &&
3725 areOffsetsWithinAlignment(Base, Alignment)) {
3726 int64_t CombinedOffset = CVal + GA->getOffset();
3727 Base = Base.getOperand(0);
3728 Offset = CurDAG->getTargetGlobalAddress(
3729 GA->getGlobal(), SDLoc(LoOperand), LoOperand.getValueType(),
3730 CombinedOffset, GA->getTargetFlags());
3731 return true;
3732 }
3733 }
3734 }
3735
3736 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Base))
3737 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), VT);
3738 Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
3739 return true;
3740 }
3741 }
3742
3743 // Handle ADD with large immediates.
3744 if (Addr.getOpcode() == ISD::ADD && isa<ConstantSDNode>(Addr.getOperand(1))) {
3745 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3746 assert(!isInt<12>(CVal) && "simm12 not already handled?");
3747
3748 // Handle immediates in the range [-4096,-2049] or [2048, 4094]. We can use
3749 // an ADDI for part of the offset and fold the rest into the load/store.
3750 // This mirrors the AddiPair PatFrag in RISCVInstrInfo.td.
3751 if (CVal >= -4096 && CVal <= 4094) {
3752 int64_t Adj = CVal < 0 ? -2048 : 2047;
3753 Base = SDValue(
3754 CurDAG->getMachineNode(RISCV::ADDI, DL, VT, Addr.getOperand(0),
3755 CurDAG->getSignedTargetConstant(Adj, DL, VT)),
3756 0);
3757 Offset = CurDAG->getSignedTargetConstant(CVal - Adj, DL, VT);
3758 return true;
3759 }
3760
3761 // For larger immediates, we might be able to save one instruction from
3762 // constant materialization by folding the Lo12 bits of the immediate into
3763 // the address. We should only do this if the ADD is only used by loads and
3764 // stores that can fold the lo12 bits. Otherwise, the ADD will get iseled
3765 // separately with the full materialized immediate creating extra
3766 // instructions.
3767 if (isWorthFoldingAdd(Addr) &&
3768 selectConstantAddr(CurDAG, DL, VT, Subtarget, Addr.getOperand(1), Base,
3769 Offset, /*IsPrefetch=*/false)) {
3770 // Insert an ADD instruction with the materialized Hi52 bits.
3771 Base = SDValue(
3772 CurDAG->getMachineNode(RISCV::ADD, DL, VT, Addr.getOperand(0), Base),
3773 0);
3774 return true;
3775 }
3776 }
3777
3778 if (selectConstantAddr(CurDAG, DL, VT, Subtarget, Addr, Base, Offset,
3779 /*IsPrefetch=*/false))
3780 return true;
3781
3782 Base = Addr;
3783 Offset = CurDAG->getTargetConstant(0, DL, VT);
3784 return true;
3785}
3786
3787/// Similar to SelectAddrRegImm, but only matches a register, or a register
3788/// plus a simm12 offset. Doesn't match a FrameIndex or global address, since
3789/// those aren't valid for the callers of this function (e.g. the target of
3790/// an indirect branch).
3792 SDValue &Offset) {
3793 SDLoc DL(Addr);
3794 MVT VT = Addr.getSimpleValueType();
3795
3796 if (CurDAG->isBaseWithConstantOffset(Addr)) {
3797 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3798 if (isInt<12>(CVal)) {
3799 Base = Addr.getOperand(0);
3800 Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
3801 return true;
3802 }
3803 }
3804
3805 Base = Addr;
3806 Offset = CurDAG->getTargetConstant(0, DL, VT);
3807 return true;
3808}
3809
3810/// Similar to SelectAddrRegImm, except that the offset is a 26-bit signed
3811/// immediate. This is used by the Qualcomm Xqcilo large offset load/store
3812/// instructions (qc.e.lw/qc.e.sw), whose offset field is 26 bits wide.
3813/// Only matches offsets that do not fit a 12-bit signed immediate, so that
3814/// offsets in the simm12 range keep using the shorter (and possibly
3815/// compressible) standard load/store instructions.
3817 SDValue &Offset) {
3818 SDLoc DL(Addr);
3819 MVT VT = Addr.getSimpleValueType();
3820
3821 if (CurDAG->isBaseWithConstantOffset(Addr)) {
3822 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3823 // Fold a 26-bit (but not 12-bit) signed offset directly into the
3824 // load/store.
3825 if (isInt<26>(CVal) && !isInt<12>(CVal)) {
3826 Base = Addr.getOperand(0);
3827 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Base))
3828 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), VT);
3829 Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
3830 return true;
3831 }
3832 }
3833
3834 // The offset is just outside the 26-bit range. Split off a small (simm12)
3835 // adjustment with a plain ADDI and fold the remaining 26-bit offset into the
3836 // load/store. A plain ADDI is used (rather than the wide
3837 // qc.e.addi/qc.e.addai) because the adjustment fits simm12: this keeps it a
3838 // short, compressible (c.addi) instruction and is available without Xqcilia.
3839 //
3840 // Skip the split if the address is used other than as a foldable load/store
3841 // base. `isWorthFoldingAdd()` returns true when every user of the add node is
3842 // a scalar load/store using it as an address operand. If it return false, it
3843 // means that some use consumes the add result as a value (e.g. it feeds
3844 // another add, is a stored value, is used in arithmetic) and that use forces
3845 // the add to be materialized into a register.
3846 if (Addr.getOpcode() == ISD::ADD && isa<ConstantSDNode>(Addr.getOperand(1)) &&
3847 isWorthFoldingAdd(Addr)) {
3848 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3849 if (!isInt<26>(CVal)) {
3850 // check if lw in lui + add + lw combination can be compressed.
3851 // The check here purely based on the immediate value and hopes that
3852 // register allocator would assign a register from a GPRC set so that the
3853 // instruction can get compressed.
3854 bool IsLwCompressable = isShiftedUInt<5, 2>(CVal & ((1 << 12) - 1));
3855
3856 int64_t Imm26 = CVal < 0 ? minIntN(26) : maxIntN(26);
3857 int64_t Adj = CVal - Imm26;
3858 // If Adj fits within 6-bits, then both combinations will take 8 bytes
3859 // however c.addi + qc.e.lw/sw will take 1 less cycle. Also, if lw is not
3860 // compressable then both combination would take 10 bytes but again
3861 // addi + qc.e.lw/sw will take 1 less cycle.
3862 if (isInt<6>(Adj) || (isInt<12>(Adj) && !IsLwCompressable)) {
3863 Base = SDValue(CurDAG->getMachineNode(
3864 RISCV::ADDI, DL, VT, Addr.getOperand(0),
3865 CurDAG->getSignedTargetConstant(Adj, DL, VT)),
3866 0);
3867 Offset = CurDAG->getSignedTargetConstant(Imm26, DL, VT);
3868 return true;
3869 }
3870 }
3871 }
3872
3873 // Don't match: let the standard addressing modes handle it.
3874 return false;
3875}
3876
3877/// Similar to SelectAddrRegImm, except that the offset is restricted to uimm9.
3879 SDValue &Offset) {
3880 if (SelectAddrFrameIndex(Addr, Base, Offset))
3881 return true;
3882
3883 SDLoc DL(Addr);
3884 MVT VT = Addr.getSimpleValueType();
3885
3886 if (CurDAG->isBaseWithConstantOffset(Addr)) {
3887 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3888 if (isUInt<9>(CVal)) {
3889 Base = Addr.getOperand(0);
3890
3891 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Base))
3892 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), VT);
3893 Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
3894 return true;
3895 }
3896 }
3897
3898 Base = Addr;
3899 Offset = CurDAG->getTargetConstant(0, DL, VT);
3900 return true;
3901}
3902
3903/// Similar to SelectAddrRegImm, except that the least significant 5 bits of
3904/// Offset should be all zeros.
3906 SDValue &Offset) {
3907 if (SelectAddrFrameIndex(Addr, Base, Offset))
3908 return true;
3909
3910 SDLoc DL(Addr);
3911 MVT VT = Addr.getSimpleValueType();
3912
3913 if (CurDAG->isBaseWithConstantOffset(Addr)) {
3914 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3915 if (isInt<12>(CVal)) {
3916 Base = Addr.getOperand(0);
3917
3918 // Early-out if not a valid offset.
3919 if ((CVal & 0b11111) != 0) {
3920 Base = Addr;
3921 Offset = CurDAG->getTargetConstant(0, DL, VT);
3922 return true;
3923 }
3924
3925 if (auto *FIN = dyn_cast<FrameIndexSDNode>(Base))
3926 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), VT);
3927 Offset = CurDAG->getSignedTargetConstant(CVal, DL, VT);
3928 return true;
3929 }
3930 }
3931
3932 // Handle ADD with large immediates.
3933 if (Addr.getOpcode() == ISD::ADD && isa<ConstantSDNode>(Addr.getOperand(1))) {
3934 int64_t CVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue();
3935 assert(!isInt<12>(CVal) && "simm12 not already handled?");
3936
3937 // Handle immediates in the range [-4096,-2049] or [2017, 4063]. We can save
3938 // one instruction by folding adjustment (-2048 or 2016) into the address.
3939 // The upper bound keeps CVal - 2016 within simm12 ([−2048, 2047]).
3940 if ((-2049 >= CVal && CVal >= -4096) || (4063 >= CVal && CVal >= 2017)) {
3941 int64_t Adj = CVal < 0 ? -2048 : 2016;
3942 int64_t AdjustedOffset = CVal - Adj;
3943 Base =
3944 SDValue(CurDAG->getMachineNode(
3945 RISCV::ADDI, DL, VT, Addr.getOperand(0),
3946 CurDAG->getSignedTargetConstant(AdjustedOffset, DL, VT)),
3947 0);
3948 Offset = CurDAG->getSignedTargetConstant(Adj, DL, VT);
3949 return true;
3950 }
3951
3952 if (selectConstantAddr(CurDAG, DL, VT, Subtarget, Addr.getOperand(1), Base,
3953 Offset, /*IsPrefetch=*/true)) {
3954 // Insert an ADD instruction with the materialized Hi52 bits.
3955 Base = SDValue(
3956 CurDAG->getMachineNode(RISCV::ADD, DL, VT, Addr.getOperand(0), Base),
3957 0);
3958 return true;
3959 }
3960 }
3961
3962 if (selectConstantAddr(CurDAG, DL, VT, Subtarget, Addr, Base, Offset,
3963 /*IsPrefetch=*/true))
3964 return true;
3965
3966 Base = Addr;
3967 Offset = CurDAG->getTargetConstant(0, DL, VT);
3968 return true;
3969}
3970
3971/// Return true if this a load/store that we have a RegRegScale instruction for.
3973 const RISCVSubtarget &Subtarget) {
3974 unsigned UserOpc = User->getOpcode();
3975 if (UserOpc != ISD::LOAD && UserOpc != ISD::STORE)
3976 return false;
3977 EVT VT = cast<MemSDNode>(User)->getMemoryVT();
3978 // Zilx only provides indexed loads, so it must not enable reg+reg-scale
3979 // address folding for stores. XTheadMemIdx and Xqcisls have scaled stores.
3980 bool HasScalarIntegerMemIdx =
3981 Subtarget.hasVendorXTHeadMemIdx() || Subtarget.hasVendorXqcisls() ||
3982 (Subtarget.hasStdExtZilx() && UserOpc == ISD::LOAD);
3983 if (!(VT.isScalarInteger() && HasScalarIntegerMemIdx) &&
3984 !((VT == MVT::f32 || VT == MVT::f64) &&
3985 Subtarget.hasVendorXTHeadFMemIdx()))
3986 return false;
3987 // Don't allow stores of the value. It must be used as the address.
3988 if (UserOpc == ISD::STORE && cast<StoreSDNode>(User)->getValue() == Add)
3989 return false;
3990
3991 return true;
3992}
3993
3994/// Is it profitable to fold this Add into RegRegScale load/store. If \p
3995/// Shift is non-null, then we have matched a shl+add. We allow reassociating
3996/// (add (add (shl A C2) B) C1) -> (add (add B C1) (shl A C2)) if there is a
3997/// single addi and we don't have a SHXADD instruction we could use.
3998/// FIXME: May still need to check how many and what kind of users the SHL has.
4000 SDValue Add,
4001 SDValue Shift = SDValue()) {
4002 bool FoundADDI = false;
4003 for (auto *User : Add->users()) {
4005 continue;
4006
4007 // Allow a single ADDI that is used by loads/stores if we matched a shift.
4008 if (!Shift || FoundADDI || User->getOpcode() != ISD::ADD ||
4010 !isInt<12>(cast<ConstantSDNode>(User->getOperand(1))->getSExtValue()))
4011 return false;
4012
4013 FoundADDI = true;
4014
4015 // If we have a SHXADD instruction, prefer that over reassociating an ADDI.
4016 assert(Shift.getOpcode() == ISD::SHL);
4017 unsigned ShiftAmt = Shift.getConstantOperandVal(1);
4018 if (Subtarget.hasShlAdd(ShiftAmt))
4019 return false;
4020
4021 // All users of the ADDI should be load/store.
4022 for (auto *ADDIUser : User->users())
4023 if (!isRegRegScaleLoadOrStore(ADDIUser, SDValue(User, 0), Subtarget))
4024 return false;
4025 }
4026
4027 return true;
4028}
4029
4031 ArrayRef<unsigned> Amounts,
4032 SDValue &Base, SDValue &Index,
4033 SDValue &Scale) {
4034 if (Addr.getOpcode() != ISD::ADD)
4035 return false;
4036 SDValue LHS = Addr.getOperand(0);
4037 SDValue RHS = Addr.getOperand(1);
4038
4039 EVT VT = Addr.getSimpleValueType();
4040 auto SelectShl = [this, VT, Amounts](SDValue N, SDValue &Index,
4041 SDValue &Shift) {
4042 if (N.getOpcode() != ISD::SHL || !isa<ConstantSDNode>(N.getOperand(1)))
4043 return false;
4044
4045 // Only match shifts by a value in range [0, MaxShiftAmount].
4046 unsigned ShiftAmt = N.getConstantOperandVal(1);
4047 if (!llvm::is_contained(Amounts, ShiftAmt))
4048 return false;
4049
4050 Index = N.getOperand(0);
4051 Shift = CurDAG->getTargetConstant(ShiftAmt, SDLoc(N), VT);
4052 return true;
4053 };
4054
4055 if (auto *C1 = dyn_cast<ConstantSDNode>(RHS)) {
4056 // (add (add (shl A C2) B) C1) -> (add (add B C1) (shl A C2))
4057 if (LHS.getOpcode() == ISD::ADD &&
4058 !isa<ConstantSDNode>(LHS.getOperand(1)) &&
4059 isInt<12>(C1->getSExtValue())) {
4060 if (SelectShl(LHS.getOperand(1), Index, Scale) &&
4061 isWorthFoldingIntoRegRegScale(*Subtarget, LHS, LHS.getOperand(1))) {
4062 SDValue C1Val = CurDAG->getTargetConstant(*C1->getConstantIntValue(),
4063 SDLoc(Addr), VT);
4064 Base = SDValue(CurDAG->getMachineNode(RISCV::ADDI, SDLoc(Addr), VT,
4065 LHS.getOperand(0), C1Val),
4066 0);
4067 return true;
4068 }
4069
4070 // Add is commutative so we need to check both operands.
4071 if (SelectShl(LHS.getOperand(0), Index, Scale) &&
4072 isWorthFoldingIntoRegRegScale(*Subtarget, LHS, LHS.getOperand(0))) {
4073 SDValue C1Val = CurDAG->getTargetConstant(*C1->getConstantIntValue(),
4074 SDLoc(Addr), VT);
4075 Base = SDValue(CurDAG->getMachineNode(RISCV::ADDI, SDLoc(Addr), VT,
4076 LHS.getOperand(1), C1Val),
4077 0);
4078 return true;
4079 }
4080 }
4081
4082 // Don't match add with constants.
4083 // FIXME: Is this profitable for large constants that have 0s in the lower
4084 // 12 bits that we can materialize with LUI?
4085 return false;
4086 }
4087
4088 // Try to match a shift on the RHS.
4089 if (SelectShl(RHS, Index, Scale)) {
4090 if (!isWorthFoldingIntoRegRegScale(*Subtarget, Addr, RHS))
4091 return false;
4092 Base = LHS;
4093 return true;
4094 }
4095
4096 // Try to match a shift on the LHS.
4097 if (SelectShl(LHS, Index, Scale)) {
4098 if (!isWorthFoldingIntoRegRegScale(*Subtarget, Addr, LHS))
4099 return false;
4100 Base = RHS;
4101 return true;
4102 }
4103
4104 if (!isWorthFoldingIntoRegRegScale(*Subtarget, Addr))
4105 return false;
4106
4107 // Bail out if 0 is not in candidate shift amounts.
4108 if (!llvm::is_contained(Amounts, 0))
4109 return false;
4110
4111 Base = LHS;
4112 Index = RHS;
4113 Scale = CurDAG->getTargetConstant(0, SDLoc(Addr), VT);
4114 return true;
4115}
4116
4118 ArrayRef<unsigned> Amounts,
4119 unsigned Bits, SDValue &Base,
4120 SDValue &Index,
4121 SDValue &Scale) {
4122 if (!SelectAddrRegRegScale(Addr, Amounts, Base, Index, Scale))
4123 return false;
4124
4125 if (Index.getOpcode() == ISD::AND) {
4126 auto *C = dyn_cast<ConstantSDNode>(Index.getOperand(1));
4127 if (C && C->getZExtValue() == maskTrailingOnes<uint64_t>(Bits)) {
4128 Index = Index.getOperand(0);
4129 return true;
4130 }
4131 }
4132
4133 return false;
4134}
4135
4137 SDValue &Offset) {
4138 if (Addr.getOpcode() != ISD::ADD)
4139 return false;
4140
4141 if (isa<ConstantSDNode>(Addr.getOperand(1)))
4142 return false;
4143
4144 Base = Addr.getOperand(0);
4145 Offset = Addr.getOperand(1);
4146 return true;
4147}
4148
4150 SDValue &ShAmt) {
4151 // Shift instructions on RISC-V only read the lower 5 or 6 bits of the shift
4152 // amount. Look through an AND that doesn't affect those bits.
4153 if (!selectShiftAmountMask(N, ShiftWidth, ShAmt))
4154 ShAmt = N;
4155
4156 if (ShAmt.getOpcode() == ISD::ADD &&
4157 isa<ConstantSDNode>(ShAmt.getOperand(1))) {
4158 uint64_t Imm = ShAmt.getConstantOperandVal(1);
4159 // If we are shifting by X+N where N == 0 mod Size, then just shift by X
4160 // to avoid the ADD.
4161 if (Imm != 0 && Imm % ShiftWidth == 0) {
4162 ShAmt = ShAmt.getOperand(0);
4163 return true;
4164 }
4165 } else if (ShAmt.getOpcode() == ISD::SUB &&
4166 isa<ConstantSDNode>(ShAmt.getOperand(0))) {
4167 uint64_t Imm = ShAmt.getConstantOperandVal(0);
4168 // If we are shifting by N-X where N == 0 mod Size, then just shift by -X to
4169 // generate a NEG instead of a SUB of a constant.
4170 if (Imm != 0 && Imm % ShiftWidth == 0) {
4171 SDLoc DL(ShAmt);
4172 EVT VT = ShAmt.getValueType();
4173 SDValue Zero = CurDAG->getRegister(RISCV::X0, VT);
4174 unsigned NegOpc = VT == MVT::i64 ? RISCV::SUBW : RISCV::SUB;
4175 MachineSDNode *Neg = CurDAG->getMachineNode(NegOpc, DL, VT, Zero,
4176 ShAmt.getOperand(1));
4177 ShAmt = SDValue(Neg, 0);
4178 return true;
4179 }
4180 // If we are shifting by N-X where N == -1 mod Size, then just shift by ~X
4181 // to generate a NOT instead of a SUB of a constant.
4182 if (Imm % ShiftWidth == ShiftWidth - 1) {
4183 SDLoc DL(ShAmt);
4184 EVT VT = ShAmt.getValueType();
4185 MachineSDNode *Not = CurDAG->getMachineNode(
4186 RISCV::XORI, DL, VT, ShAmt.getOperand(1),
4187 CurDAG->getAllOnesConstant(DL, VT, /*isTarget=*/true));
4188 ShAmt = SDValue(Not, 0);
4189 return true;
4190 }
4191 }
4192
4193 return true;
4194}
4195
4196/// RISC-V doesn't have general instructions for integer setne/seteq, but we can
4197/// check for equality with 0. This function emits instructions that convert the
4198/// seteq/setne into something that can be compared with 0.
4199/// \p ExpectedCCVal indicates the condition code to attempt to match (e.g.
4200/// ISD::SETNE).
4202 SDValue &Val, bool OneUse) {
4203 assert(ISD::isIntEqualitySetCC(ExpectedCCVal) &&
4204 "Unexpected condition code!");
4205
4206 // We're looking for a setcc.
4207 if (N->getOpcode() != ISD::SETCC)
4208 return false;
4209
4210 if (OneUse && !N->hasOneUse())
4211 return false;
4212
4213 // Must be an equality comparison.
4214 ISD::CondCode CCVal = N->getOperandCondCode(2);
4215 if (CCVal != ExpectedCCVal)
4216 return false;
4217
4218 SDValue LHS = N->getOperand(0);
4219 SDValue RHS = N->getOperand(1);
4220
4221 if (!LHS.getValueType().isScalarInteger())
4222 return false;
4223
4224 // If the RHS side is 0, we don't need any extra instructions, return the LHS.
4225 if (isNullConstant(RHS)) {
4226 Val = LHS;
4227 return true;
4228 }
4229
4230 SDLoc DL(N);
4231
4232 if (auto *C = dyn_cast<ConstantSDNode>(RHS)) {
4233 int64_t CVal = C->getSExtValue();
4234 // If the RHS is -2048, we can use xori to produce 0 if the LHS is -2048 and
4235 // non-zero otherwise.
4236 if (CVal == -2048) {
4237 Val = SDValue(
4238 CurDAG->getMachineNode(
4239 RISCV::XORI, DL, N->getValueType(0), LHS,
4240 CurDAG->getSignedTargetConstant(CVal, DL, N->getValueType(0))),
4241 0);
4242 return true;
4243 }
4244 // If the RHS is [-2047,2048], we can use addi/addiw with -RHS to produce 0
4245 // if the LHS is equal to the RHS and non-zero otherwise.
4246 if (isInt<12>(CVal) || CVal == 2048) {
4247 unsigned Opc = RISCV::ADDI;
4248 if (LHS.getOpcode() == ISD::SIGN_EXTEND_INREG &&
4249 cast<VTSDNode>(LHS.getOperand(1))->getVT() == MVT::i32) {
4250 Opc = RISCV::ADDIW;
4251 LHS = LHS.getOperand(0);
4252 }
4253
4254 Val = SDValue(CurDAG->getMachineNode(Opc, DL, N->getValueType(0), LHS,
4255 CurDAG->getSignedTargetConstant(
4256 -CVal, DL, N->getValueType(0))),
4257 0);
4258 return true;
4259 }
4260 if (isPowerOf2_64(CVal) && Subtarget->hasStdExtZbs()) {
4261 Val = SDValue(
4262 CurDAG->getMachineNode(
4263 RISCV::BINVI, DL, N->getValueType(0), LHS,
4264 CurDAG->getTargetConstant(Log2_64(CVal), DL, N->getValueType(0))),
4265 0);
4266 return true;
4267 }
4268 // Same as the addi case above but for larger immediates (signed 26-bit) use
4269 // the QC_E_ADDI instruction from the Xqcilia extension, if available. Avoid
4270 // anything which can be done with a single lui as it might be compressible.
4271 if (Subtarget->hasVendorXqcilia() && isInt<26>(CVal) &&
4272 (CVal & 0xFFF) != 0) {
4273 Val = SDValue(
4274 CurDAG->getMachineNode(
4275 RISCV::QC_E_ADDI, DL, N->getValueType(0), LHS,
4276 CurDAG->getSignedTargetConstant(-CVal, DL, N->getValueType(0))),
4277 0);
4278 return true;
4279 }
4280 }
4281
4282 // If nothing else we can XOR the LHS and RHS to produce zero if they are
4283 // equal and a non-zero value if they aren't.
4284 Val = SDValue(
4285 CurDAG->getMachineNode(RISCV::XOR, DL, N->getValueType(0), LHS, RHS), 0);
4286 return true;
4287}
4288
4290 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG &&
4291 cast<VTSDNode>(N.getOperand(1))->getVT().getSizeInBits() == Bits) {
4292 Val = N.getOperand(0);
4293 return true;
4294 }
4295
4296 auto UnwrapShlSra = [](SDValue N, unsigned ShiftAmt) {
4297 if (N.getOpcode() != ISD::SRA || !isa<ConstantSDNode>(N.getOperand(1)))
4298 return N;
4299
4300 SDValue N0 = N.getOperand(0);
4301 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
4302 N.getConstantOperandVal(1) == ShiftAmt &&
4303 N0.getConstantOperandVal(1) == ShiftAmt)
4304 return N0.getOperand(0);
4305
4306 return N;
4307 };
4308
4309 MVT VT = N.getSimpleValueType();
4310 if (CurDAG->ComputeNumSignBits(N) > (VT.getSizeInBits() - Bits)) {
4311 Val = UnwrapShlSra(N, VT.getSizeInBits() - Bits);
4312 return true;
4313 }
4314
4315 return false;
4316}
4317
4319 if (N.getOpcode() == ISD::AND) {
4320 auto *C = dyn_cast<ConstantSDNode>(N.getOperand(1));
4321 if (C && C->getZExtValue() == maskTrailingOnes<uint64_t>(Bits)) {
4322 Val = N.getOperand(0);
4323 return true;
4324 }
4325 }
4326 MVT VT = N.getSimpleValueType();
4327 APInt Mask = APInt::getBitsSetFrom(VT.getSizeInBits(), Bits);
4328 if (CurDAG->MaskedValueIsZero(N, Mask)) {
4329 Val = N;
4330 return true;
4331 }
4332
4333 return false;
4334}
4335
4336/// Look for various patterns that can be done with a SHL that can be folded
4337/// into a SHXADD. \p ShAmt contains 1, 2, or 3 and is set based on which
4338/// SHXADD we are trying to match.
4340 SDValue &Val) {
4341 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1))) {
4342 MVT VT = N.getSimpleValueType();
4343 assert(VT == Subtarget->getXLenVT());
4344 SDValue N0 = N.getOperand(0);
4345
4346 if (bool LeftShift = N0.getOpcode() == ISD::SHL;
4347 (LeftShift || N0.getOpcode() == ISD::SRL) &&
4349 uint64_t Mask = N.getConstantOperandVal(1);
4350 unsigned C2 = N0.getConstantOperandVal(1);
4351 assert(C2 > 0 && C2 < Subtarget->getXLen() && "Unexpected shift amount!");
4352
4353 unsigned XLen = Subtarget->getXLen();
4354 if (LeftShift)
4355 Mask &= maskTrailingZeros<uint64_t>(C2);
4356 else
4357 Mask &= maskTrailingOnes<uint64_t>(XLen - C2);
4358
4359 if (isShiftedMask_64(Mask)) {
4360 unsigned Leading = XLen - llvm::bit_width(Mask);
4361 unsigned Trailing = llvm::countr_zero(Mask);
4362 if (Trailing != ShAmt)
4363 return false;
4364
4365 unsigned Opcode;
4366 // Look for (and (shl y, c2), c1) where c1 is a shifted mask with no
4367 // leading zeros and c3 trailing zeros. We can use an SRLI by c3-c2
4368 // followed by a SHXADD with c3 for the X amount.
4369 if (LeftShift && Leading == 0 && C2 < Trailing)
4370 Opcode = RISCV::SRLI;
4371 // Look for (and (shl y, c2), c1) where c1 is a shifted mask with 32-c2
4372 // leading zeros and c3 trailing zeros. We can use an SRLIW by c3-c2
4373 // followed by a SHXADD with c3 for the X amount.
4374 else if (LeftShift && Leading == 32 - C2 && C2 < Trailing)
4375 Opcode = RISCV::SRLIW;
4376 // Look for (and (shr y, c2), c1) where c1 is a shifted mask with c2
4377 // leading zeros and c3 trailing zeros. We can use an SRLI by c2+c3
4378 // followed by a SHXADD using c3 for the X amount.
4379 else if (!LeftShift && Leading == C2)
4380 Opcode = RISCV::SRLI;
4381 // Look for (and (shr y, c2), c1) where c1 is a shifted mask with 32+c2
4382 // leading zeros and c3 trailing zeros. We can use an SRLIW by c2+c3
4383 // followed by a SHXADD using c3 for the X amount.
4384 else if (!LeftShift && Leading == 32 + C2)
4385 Opcode = RISCV::SRLIW;
4386 else
4387 return false;
4388
4389 SDLoc DL(N);
4390 ShAmt = LeftShift ? Trailing - C2 : Trailing + C2;
4391 Val = SDValue(
4392 CurDAG->getMachineNode(Opcode, DL, VT, N0.getOperand(0),
4393 CurDAG->getTargetConstant(ShAmt, DL, VT)),
4394 0);
4395 return true;
4396 }
4397 } else if (N0.getOpcode() == ISD::SRA && N0.hasOneUse() &&
4399 uint64_t Mask = N.getConstantOperandVal(1);
4400 unsigned C2 = N0.getConstantOperandVal(1);
4401
4402 // Look for (and (sra y, c2), c1) where c1 is a shifted mask with c3
4403 // leading zeros and c4 trailing zeros. If c2 is greater than c3, we can
4404 // use (srli (srai y, c2 - c3), c3 + c4) followed by a SHXADD with c4 as
4405 // the X amount.
4406 if (isShiftedMask_64(Mask)) {
4407 unsigned XLen = Subtarget->getXLen();
4408 unsigned Leading = XLen - llvm::bit_width(Mask);
4409 unsigned Trailing = llvm::countr_zero(Mask);
4410 if (C2 > Leading && Leading > 0 && Trailing == ShAmt) {
4411 SDLoc DL(N);
4412 EVT VT = N.getValueType();
4413 Val = SDValue(CurDAG->getMachineNode(
4414 RISCV::SRAI, DL, VT, N0.getOperand(0),
4415 CurDAG->getTargetConstant(C2 - Leading, DL, VT)),
4416 0);
4417 Val = SDValue(CurDAG->getMachineNode(
4418 RISCV::SRLI, DL, VT, Val,
4419 CurDAG->getTargetConstant(Leading + ShAmt, DL, VT)),
4420 0);
4421 return true;
4422 }
4423 }
4424 }
4425 } else if (bool LeftShift = N.getOpcode() == ISD::SHL;
4426 (LeftShift || N.getOpcode() == ISD::SRL) &&
4427 isa<ConstantSDNode>(N.getOperand(1))) {
4428 MVT VT = N.getSimpleValueType();
4429 assert(VT == Subtarget->getXLenVT() && "Unexpected VT!");
4430 SDValue N0 = N.getOperand(0);
4431 if (N0.getOpcode() == ISD::AND && N0.hasOneUse() &&
4433 uint64_t Mask = N0.getConstantOperandVal(1);
4434 if (isShiftedMask_64(Mask)) {
4435 unsigned C1 = N.getConstantOperandVal(1);
4436 assert(C1 > 0 && C1 < Subtarget->getXLen() &&
4437 "Unexpected shift amount!");
4438 unsigned XLen = Subtarget->getXLen();
4439 unsigned Leading = XLen - llvm::bit_width(Mask);
4440 unsigned Trailing = llvm::countr_zero(Mask);
4441 // Look for (shl (and X, Mask), C1) where Mask has 32 leading zeros and
4442 // C3 trailing zeros. If C1+C3==ShAmt we can use SRLIW+SHXADD.
4443 if (LeftShift && Leading == 32 && Trailing > 0 &&
4444 (Trailing + C1) == ShAmt) {
4445 SDLoc DL(N);
4446 EVT VT = N.getValueType();
4447 Val = SDValue(CurDAG->getMachineNode(
4448 RISCV::SRLIW, DL, VT, N0.getOperand(0),
4449 CurDAG->getTargetConstant(Trailing, DL, VT)),
4450 0);
4451 return true;
4452 }
4453 // Look for (srl (and X, Mask), C1) where Mask has 32 leading zeros and
4454 // C3 trailing zeros. If C3-C1==ShAmt we can use SRLIW+SHXADD.
4455 if (!LeftShift && Leading == 32 && Trailing > C1 &&
4456 (Trailing - C1) == ShAmt) {
4457 SDLoc DL(N);
4458 Val = SDValue(CurDAG->getMachineNode(
4459 RISCV::SRLIW, DL, VT, N0.getOperand(0),
4460 CurDAG->getTargetConstant(Trailing, DL, VT)),
4461 0);
4462 return true;
4463 }
4464 }
4465 }
4466 }
4467
4468 return false;
4469}
4470
4471/// Look for various patterns that can be done with a SHL that can be folded
4472/// into a SHXADD_UW. \p ShAmt contains 1, 2, or 3 and is set based on which
4473/// SHXADD_UW we are trying to match.
4475 SDValue &Val) {
4476 if (N.getOpcode() == ISD::AND && isa<ConstantSDNode>(N.getOperand(1)) &&
4477 N.hasOneUse()) {
4478 assert(N.getSimpleValueType() == Subtarget->getXLenVT() &&
4479 "Unexpected VT!");
4480 SDValue N0 = N.getOperand(0);
4481 if (N0.getOpcode() == ISD::SHL && isa<ConstantSDNode>(N0.getOperand(1)) &&
4482 N0.hasOneUse()) {
4483 uint64_t Mask = N.getConstantOperandVal(1);
4484 unsigned C2 = N0.getConstantOperandVal(1);
4485 assert(C2 > 0 && C2 < Subtarget->getXLen() && "Unexpected shift amount!");
4486
4487 Mask &= maskTrailingZeros<uint64_t>(C2);
4488
4489 // Look for (and (shl y, c2), c1) where c1 is a shifted mask with
4490 // 32-ShAmt leading zeros and c2 trailing zeros. We can use SLLI by
4491 // c2-ShAmt followed by SHXADD_UW with ShAmt for the X amount.
4492 if (isShiftedMask_64(Mask)) {
4493 unsigned Leading = llvm::countl_zero(Mask);
4494 unsigned Trailing = llvm::countr_zero(Mask);
4495 if (Leading == 32 - ShAmt && Trailing == C2 && Trailing > ShAmt) {
4496 SDLoc DL(N);
4497 EVT VT = N.getValueType();
4498 Val = SDValue(CurDAG->getMachineNode(
4499 RISCV::SLLI, DL, VT, N0.getOperand(0),
4500 CurDAG->getTargetConstant(C2 - ShAmt, DL, VT)),
4501 0);
4502 return true;
4503 }
4504 }
4505 }
4506 }
4507
4508 return false;
4509}
4510
4512 assert(N->getOpcode() == ISD::OR || N->getOpcode() == RISCVISD::OR_VL);
4513 if (N->getFlags().hasDisjoint())
4514 return true;
4515 return CurDAG->haveNoCommonBitsSet(N->getOperand(0), N->getOperand(1));
4516}
4517
4518bool RISCVDAGToDAGISel::selectImm64IfCheaper(int64_t Imm, int64_t OrigImm,
4519 SDValue N, SDValue &Val) {
4520 int OrigCost = RISCVMatInt::getIntMatCost(APInt(64, OrigImm), 64, *Subtarget,
4521 /*CompressionCost=*/true);
4523 /*CompressionCost=*/true);
4524 if (OrigCost <= Cost)
4525 return false;
4526
4527 Val = selectImm(CurDAG, SDLoc(N), N->getSimpleValueType(0), Imm, *Subtarget);
4528 return true;
4529}
4530
4532 if (!isa<ConstantSDNode>(N))
4533 return false;
4534 int64_t Imm = cast<ConstantSDNode>(N)->getSExtValue();
4535 if ((Imm >> 31) != 1)
4536 return false;
4537
4538 for (const SDNode *U : N->users()) {
4539 switch (U->getOpcode()) {
4540 case ISD::ADD:
4541 break;
4542 case ISD::OR:
4543 if (orDisjoint(U))
4544 break;
4545 return false;
4546 default:
4547 return false;
4548 }
4549 }
4550
4551 return selectImm64IfCheaper(0xffffffff00000000 | Imm, Imm, N, Val);
4552}
4553
4555 if (!isa<ConstantSDNode>(N))
4556 return false;
4557 int64_t Imm = cast<ConstantSDNode>(N)->getSExtValue();
4558 if (isInt<32>(Imm))
4559 return false;
4560 if (Imm == INT64_MIN)
4561 return false;
4562
4563 for (const SDNode *U : N->users()) {
4564 switch (U->getOpcode()) {
4565 case ISD::ADD:
4566 break;
4567 case RISCVISD::VMV_V_X_VL:
4568 if (!all_of(U->users(), [](const SDNode *V) {
4569 return V->getOpcode() == ISD::ADD ||
4570 V->getOpcode() == RISCVISD::ADD_VL;
4571 }))
4572 return false;
4573 break;
4574 default:
4575 return false;
4576 }
4577 }
4578
4579 return selectImm64IfCheaper(-Imm, Imm, N, Val);
4580}
4581
4583 if (!isa<ConstantSDNode>(N))
4584 return false;
4585 int64_t Imm = cast<ConstantSDNode>(N)->getSExtValue();
4586
4587 // For 32-bit signed constants, we can only substitute LUI+ADDI with LUI.
4588 if (isInt<32>(Imm) && ((Imm & 0xfff) != 0xfff || Imm == -1))
4589 return false;
4590
4591 // Abandon this transform if the constant is needed elsewhere.
4592 for (const SDNode *U : N->users()) {
4593 switch (U->getOpcode()) {
4594 case ISD::AND:
4595 case ISD::OR:
4596 case ISD::XOR:
4597 if (!(Subtarget->hasStdExtZbb() || Subtarget->hasStdExtZbkb()))
4598 return false;
4599 break;
4600 case RISCVISD::VMV_V_X_VL:
4601 if (!Subtarget->hasStdExtZvkb())
4602 return false;
4603 if (!all_of(U->users(), [](const SDNode *V) {
4604 return V->getOpcode() == ISD::AND ||
4605 V->getOpcode() == RISCVISD::AND_VL;
4606 }))
4607 return false;
4608 break;
4609 default:
4610 return false;
4611 }
4612 }
4613
4614 if (isInt<32>(Imm)) {
4615 Val =
4616 selectImm(CurDAG, SDLoc(N), N->getSimpleValueType(0), ~Imm, *Subtarget);
4617 return true;
4618 }
4619
4620 // For 64-bit constants, the instruction sequences get complex,
4621 // so we select inverted only if it's cheaper.
4622 return selectImm64IfCheaper(~Imm, Imm, N, Val);
4623}
4624
4625static bool vectorPseudoHasAllNBitUsers(SDNode *User, unsigned UserOpNo,
4626 unsigned Bits,
4627 const TargetInstrInfo *TII) {
4628 unsigned MCOpcode = RISCV::getRVVMCOpcode(User->getMachineOpcode());
4629
4630 if (!MCOpcode)
4631 return false;
4632
4633 const MCInstrDesc &MCID = TII->get(User->getMachineOpcode());
4634 const uint64_t TSFlags = MCID.TSFlags;
4635 if (!RISCVII::hasSEWOp(TSFlags))
4636 return false;
4637 assert(RISCVII::hasVLOp(TSFlags));
4638
4639 unsigned ChainOpIdx = User->getNumOperands() - 1;
4640 bool HasChainOp = User->getOperand(ChainOpIdx).getValueType() == MVT::Other;
4641 bool HasVecPolicyOp = RISCVII::hasVecPolicyOp(TSFlags);
4642 unsigned VLIdx = User->getNumOperands() - HasVecPolicyOp - HasChainOp - 2;
4643 const unsigned Log2SEW = User->getConstantOperandVal(VLIdx + 1);
4644
4645 if (UserOpNo == VLIdx)
4646 return false;
4647
4648 auto NumDemandedBits =
4649 RISCV::getVectorLowDemandedScalarBits(MCOpcode, Log2SEW);
4650 return NumDemandedBits && Bits >= *NumDemandedBits;
4651}
4652
4653// Return true if all users of this SDNode* only consume the lower \p Bits.
4654// This can be used to form W instructions for add/sub/mul/shl even when the
4655// root isn't a sext_inreg. This can allow the ADDW/SUBW/MULW/SLLIW to CSE if
4656// SimplifyDemandedBits has made it so some users see a sext_inreg and some
4657// don't. The sext_inreg+add/sub/mul/shl will get selected, but still leave
4658// the add/sub/mul/shl to become non-W instructions. By checking the users we
4659// may be able to use a W instruction and CSE with the other instruction if
4660// this has happened. We could try to detect that the CSE opportunity exists
4661// before doing this, but that would be more complicated.
4663 const unsigned Depth) const {
4664 assert((Node->getOpcode() == ISD::ADD || Node->getOpcode() == ISD::SUB ||
4665 Node->getOpcode() == ISD::MUL || Node->getOpcode() == ISD::SHL ||
4666 Node->getOpcode() == ISD::SRL || Node->getOpcode() == ISD::AND ||
4667 Node->getOpcode() == ISD::OR || Node->getOpcode() == ISD::XOR ||
4668 Node->getOpcode() == ISD::SIGN_EXTEND_INREG ||
4669 isa<ConstantSDNode>(Node) || Depth != 0) &&
4670 "Unexpected opcode");
4671
4673 return false;
4674
4675 // The PatFrags that call this may run before RISCVGenDAGISel.inc has checked
4676 // the VT. Ensure the type is scalar to avoid wasting time on vectors.
4677 if (Depth == 0 && !Node->getValueType(0).isScalarInteger())
4678 return false;
4679
4680 for (SDUse &Use : Node->uses()) {
4681 SDNode *User = Use.getUser();
4682 // Users of this node should have already been instruction selected
4683 if (!User->isMachineOpcode())
4684 return false;
4685
4686 // TODO: Add more opcodes?
4687 switch (User->getMachineOpcode()) {
4688 default:
4690 break;
4691 return false;
4692 case RISCV::ADDW:
4693 case RISCV::ADDIW:
4694 case RISCV::SUBW:
4695 case RISCV::MULW:
4696 case RISCV::SLLW:
4697 case RISCV::SLLIW:
4698 case RISCV::SRAW:
4699 case RISCV::SRAIW:
4700 case RISCV::SRLW:
4701 case RISCV::SRLIW:
4702 case RISCV::DIVW:
4703 case RISCV::DIVUW:
4704 case RISCV::REMW:
4705 case RISCV::REMUW:
4706 case RISCV::ROLW:
4707 case RISCV::RORW:
4708 case RISCV::RORIW:
4709 case RISCV::CLSW:
4710 case RISCV::CLZW:
4711 case RISCV::CTZW:
4712 case RISCV::CPOPW:
4713 case RISCV::SLLI_UW:
4714 case RISCV::ABSW:
4715 case RISCV::FMV_W_X:
4716 case RISCV::FCVT_H_W:
4717 case RISCV::FCVT_H_W_INX:
4718 case RISCV::FCVT_H_WU:
4719 case RISCV::FCVT_H_WU_INX:
4720 case RISCV::FCVT_S_W:
4721 case RISCV::FCVT_S_W_INX:
4722 case RISCV::FCVT_S_WU:
4723 case RISCV::FCVT_S_WU_INX:
4724 case RISCV::FCVT_D_W:
4725 case RISCV::FCVT_D_W_INX:
4726 case RISCV::FCVT_D_WU:
4727 case RISCV::FCVT_D_WU_INX:
4728 case RISCV::TH_REVW:
4729 case RISCV::TH_SRRIW:
4730 if (Bits >= 32)
4731 break;
4732 return false;
4733 case RISCV::SLL:
4734 case RISCV::SRA:
4735 case RISCV::SRL:
4736 case RISCV::ROL:
4737 case RISCV::ROR:
4738 case RISCV::BSET:
4739 case RISCV::BCLR:
4740 case RISCV::BINV:
4741 case RISCV::BEXT:
4742 // Shift amount and bit index operands only use log2(Xlen) bits.
4743 if (Use.getOperandNo() == 1 && Bits >= Log2_32(Subtarget->getXLen()))
4744 break;
4745 return false;
4746 case RISCV::SLLI:
4747 // SLLI only uses the lower (XLen - ShAmt) bits.
4748 if (Bits >= Subtarget->getXLen() - User->getConstantOperandVal(1))
4749 break;
4750 return false;
4751 case RISCV::ANDI:
4752 if (Bits >= (unsigned)llvm::bit_width(User->getConstantOperandVal(1)))
4753 break;
4754 goto RecCheck;
4755 case RISCV::ORI: {
4756 uint64_t Imm = cast<ConstantSDNode>(User->getOperand(1))->getSExtValue();
4757 if (Bits >= (unsigned)llvm::bit_width<uint64_t>(~Imm))
4758 break;
4759 [[fallthrough]];
4760 }
4761 case RISCV::AND:
4762 case RISCV::OR:
4763 case RISCV::XOR:
4764 case RISCV::XORI:
4765 case RISCV::ANDN:
4766 case RISCV::ORN:
4767 case RISCV::XNOR:
4768 case RISCV::SH1ADD:
4769 case RISCV::SH2ADD:
4770 case RISCV::SH3ADD:
4771 case RISCV::PseudoMERGE:
4772 RecCheck:
4773 if (hasAllNBitUsers(User, Bits, Depth + 1))
4774 break;
4775 return false;
4776 case RISCV::SRLI: {
4777 unsigned ShAmt = User->getConstantOperandVal(1);
4778 // If we are shifting right by less than Bits, and users don't demand any
4779 // bits that were shifted into [Bits-1:0], then we can consider this as an
4780 // N-Bit user.
4781 if (Bits > ShAmt && hasAllNBitUsers(User, Bits - ShAmt, Depth + 1))
4782 break;
4783 return false;
4784 }
4785 case RISCV::SEXT_B:
4786 case RISCV::PACKH:
4787 if (Bits >= 8)
4788 break;
4789 return false;
4790 case RISCV::SEXT_H:
4791 case RISCV::FMV_H_X:
4792 case RISCV::ZEXT_H_RV32:
4793 case RISCV::ZEXT_H_RV64:
4794 case RISCV::PACKW:
4795 if (Bits >= 16)
4796 break;
4797 return false;
4798 case RISCV::PACK:
4799 if (Bits >= (Subtarget->getXLen() / 2))
4800 break;
4801 return false;
4802 case RISCV::PPAIRE_H:
4803 // If only the lower 32-bits of the result are used, then only the
4804 // lower 16 bits of the inputs are used.
4805 if (Bits >= 16 && hasAllNBitUsers(User, 32, Depth + 1))
4806 break;
4807 return false;
4808 case RISCV::ADD_UW:
4809 case RISCV::SH1ADD_UW:
4810 case RISCV::SH2ADD_UW:
4811 case RISCV::SH3ADD_UW:
4812 // The first operand to add.uw/shXadd.uw is implicitly zero extended from
4813 // 32 bits.
4814 if (Use.getOperandNo() == 0 && Bits >= 32)
4815 break;
4816 return false;
4817 case RISCV::SB:
4818 if (Use.getOperandNo() == 0 && Bits >= 8)
4819 break;
4820 return false;
4821 case RISCV::SH:
4822 if (Use.getOperandNo() == 0 && Bits >= 16)
4823 break;
4824 return false;
4825 case RISCV::SW:
4826 if (Use.getOperandNo() == 0 && Bits >= 32)
4827 break;
4828 return false;
4829 case RISCV::TH_EXT:
4830 case RISCV::TH_EXTU: {
4831 unsigned Msb = User->getConstantOperandVal(1);
4832 unsigned Lsb = User->getConstantOperandVal(2);
4833 // Behavior of Msb < Lsb is not well documented.
4834 if (Msb >= Lsb && Bits > Msb)
4835 break;
4836 return false;
4837 }
4838 }
4839 }
4840
4841 return true;
4842}
4843
4844// Select a constant that can be represented as (sign_extend(imm5) << imm2).
4846 SDValue &Shl2) {
4847 auto *C = dyn_cast<ConstantSDNode>(N);
4848 if (!C)
4849 return false;
4850
4851 int64_t Offset = C->getSExtValue();
4852 for (unsigned Shift = 0; Shift < 4; Shift++) {
4853 if (isInt<5>(Offset >> Shift) && ((Offset % (1LL << Shift)) == 0)) {
4854 EVT VT = N->getValueType(0);
4855 Simm5 = CurDAG->getSignedTargetConstant(Offset >> Shift, SDLoc(N), VT);
4856 Shl2 = CurDAG->getTargetConstant(Shift, SDLoc(N), VT);
4857 return true;
4858 }
4859 }
4860
4861 return false;
4862}
4863
4864// Select VL as a 5 bit immediate or a value that will become a register. This
4865// allows us to choose between VSETIVLI or VSETVLI later.
4867 auto *C = dyn_cast<ConstantSDNode>(N);
4868 if (C && isUInt<5>(C->getZExtValue())) {
4869 VL = CurDAG->getTargetConstant(C->getZExtValue(), SDLoc(N),
4870 N->getValueType(0));
4871 } else if (C && C->isAllOnes()) {
4872 // Treat all ones as VLMax.
4873 VL = CurDAG->getSignedTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
4874 N->getValueType(0));
4875 } else if (isa<RegisterSDNode>(N) &&
4876 cast<RegisterSDNode>(N)->getReg() == RISCV::X0) {
4877 // All our VL operands use an operand that allows GPRNoX0 or an immediate
4878 // as the register class. Convert X0 to a special immediate to pass the
4879 // MachineVerifier. This is recognized specially by the vsetvli insertion
4880 // pass.
4881 VL = CurDAG->getSignedTargetConstant(RISCV::VLMaxSentinel, SDLoc(N),
4882 N->getValueType(0));
4883 } else {
4884 VL = N;
4885 }
4886
4887 return true;
4888}
4889
4891 if (N.getOpcode() == ISD::INSERT_SUBVECTOR) {
4892 if (!N.getOperand(0).isUndef())
4893 return SDValue();
4894 N = N.getOperand(1);
4895 }
4896 SDValue Splat = N;
4897 if ((Splat.getOpcode() != RISCVISD::VMV_V_X_VL &&
4898 Splat.getOpcode() != RISCVISD::VMV_S_X_VL) ||
4899 !Splat.getOperand(0).isUndef())
4900 return SDValue();
4901 assert(Splat.getNumOperands() == 3 && "Unexpected number of operands");
4902 return Splat;
4903}
4904
4907 if (!Splat)
4908 return false;
4909
4910 SplatVal = Splat.getOperand(1);
4911 return true;
4912}
4913
4915 SelectionDAG &DAG,
4917 std::function<bool(int64_t)> ValidateImm,
4918 bool Decrement = false) {
4920 if (!Splat || !isa<ConstantSDNode>(Splat.getOperand(1)))
4921 return false;
4922
4923 const unsigned SplatEltSize = Splat.getScalarValueSizeInBits();
4924 assert(Subtarget.getXLenVT() == Splat.getOperand(1).getSimpleValueType() &&
4925 "Unexpected splat operand type");
4926
4927 // The semantics of RISCVISD::VMV_V_X_VL is that when the operand
4928 // type is wider than the resulting vector element type: an implicit
4929 // truncation first takes place. Therefore, perform a manual
4930 // truncation/sign-extension in order to ignore any truncated bits and catch
4931 // any zero-extended immediate.
4932 // For example, we wish to match (i8 -1) -> (XLenVT 255) as a simm5 by first
4933 // sign-extending to (XLenVT -1).
4934 APInt SplatConst = Splat.getConstantOperandAPInt(1).sextOrTrunc(SplatEltSize);
4935
4936 int64_t SplatImm = SplatConst.getSExtValue();
4937
4938 if (!ValidateImm(SplatImm))
4939 return false;
4940
4941 if (Decrement)
4942 SplatImm -= 1;
4943
4944 SplatVal =
4945 DAG.getSignedTargetConstant(SplatImm, SDLoc(N), Subtarget.getXLenVT());
4946 return true;
4947}
4948
4950 return selectVSplatImmHelper(N, SplatVal, *CurDAG, *Subtarget,
4951 [](int64_t Imm) { return isInt<5>(Imm); });
4952}
4953
4955 return selectVSplatImmHelper(
4956 N, SplatVal, *CurDAG, *Subtarget,
4957 [](int64_t Imm) { return Imm >= -15 && Imm <= 16; },
4958 /*Decrement=*/true);
4959}
4960
4962 return selectVSplatImmHelper(
4963 N, SplatVal, *CurDAG, *Subtarget,
4964 [](int64_t Imm) { return Imm >= -15 && Imm <= 16; },
4965 /*Decrement=*/false);
4966}
4967
4969 SDValue &SplatVal) {
4970 return selectVSplatImmHelper(
4971 N, SplatVal, *CurDAG, *Subtarget,
4972 [](int64_t Imm) { return Imm != 0 && Imm >= -15 && Imm <= 16; },
4973 /*Decrement=*/true);
4974}
4975
4977 SDValue &SplatVal) {
4978 return selectVSplatImmHelper(
4979 N, SplatVal, *CurDAG, *Subtarget,
4980 [Bits](int64_t Imm) { return isUIntN(Bits, Imm); });
4981}
4982
4985 return Splat && selectNegImm(Splat.getOperand(1), SplatVal);
4986}
4987
4989 auto IsExtOrTrunc = [](SDValue N) {
4990 switch (N->getOpcode()) {
4991 case ISD::SIGN_EXTEND:
4992 case ISD::ZERO_EXTEND:
4993 // There's no passthru on these _VL nodes so any VL/mask is ok, since any
4994 // inactive elements will be undef.
4995 case RISCVISD::TRUNCATE_VECTOR_VL:
4996 case RISCVISD::VSEXT_VL:
4997 case RISCVISD::VZEXT_VL:
4998 return true;
4999 default:
5000 return false;
5001 }
5002 };
5003
5004 // We can have multiple nested nodes, so unravel them all if needed.
5005 while (IsExtOrTrunc(N)) {
5006 if (!N.hasOneUse() || N.getScalarValueSizeInBits() < 8)
5007 return false;
5008 N = N->getOperand(0);
5009 }
5010
5011 return selectVSplat(N, SplatVal);
5012}
5013
5015 // Allow bitcasts from XLenVT -> FP.
5016 if (N.getOpcode() == ISD::BITCAST &&
5017 N.getOperand(0).getValueType() == Subtarget->getXLenVT()) {
5018 Imm = N.getOperand(0);
5019 return true;
5020 }
5021 // Allow moves from XLenVT to FP.
5022 if (N.getOpcode() == RISCVISD::FMV_H_X ||
5023 N.getOpcode() == RISCVISD::FMV_W_X_RV64) {
5024 Imm = N.getOperand(0);
5025 return true;
5026 }
5027
5028 // Otherwise, look for FP constants that can materialized with scalar int.
5030 if (!CFP)
5031 return false;
5032 const APFloat &APF = CFP->getValueAPF();
5033 // td can handle +0.0 already.
5034 if (APF.isPosZero())
5035 return false;
5036
5037 MVT VT = CFP->getSimpleValueType(0);
5038
5039 MVT XLenVT = Subtarget->getXLenVT();
5040 if (VT == MVT::f64 && !Subtarget->is64Bit()) {
5041 assert(APF.isNegZero() && "Unexpected constant.");
5042 return false;
5043 }
5044 SDLoc DL(N);
5045 Imm = selectImm(CurDAG, DL, XLenVT, APF.bitcastToAPInt().getSExtValue(),
5046 *Subtarget);
5047 return true;
5048}
5049
5051 SDValue &Imm) {
5052 if (auto *C = dyn_cast<ConstantSDNode>(N)) {
5053 int64_t ImmVal = SignExtend64(C->getSExtValue(), Width);
5054
5055 if (!isInt<5>(ImmVal))
5056 return false;
5057
5058 Imm = CurDAG->getSignedTargetConstant(ImmVal, SDLoc(N),
5059 Subtarget->getXLenVT());
5060 return true;
5061 }
5062
5063 return false;
5064}
5065
5066// Match XOR with a VMSET_VL operand. Return the other operand.
5068 if (N.getOpcode() != ISD::XOR)
5069 return false;
5070
5071 if (N.getOperand(0).getOpcode() == RISCVISD::VMSET_VL) {
5072 Res = N.getOperand(1);
5073 return true;
5074 }
5075
5076 if (N.getOperand(1).getOpcode() == RISCVISD::VMSET_VL) {
5077 Res = N.getOperand(0);
5078 return true;
5079 }
5080
5081 return false;
5082}
5083
5084// Match VMXOR_VL with a VMSET_VL operand. Making sure that that VL operand
5085// matches the parent's VL. Return the other operand of the VMXOR_VL.
5087 SDValue &Res) {
5088 if (N.getOpcode() != RISCVISD::VMXOR_VL)
5089 return false;
5090
5091 assert(Parent &&
5092 (Parent->getOpcode() == RISCVISD::VMAND_VL ||
5093 Parent->getOpcode() == RISCVISD::VMOR_VL ||
5094 Parent->getOpcode() == RISCVISD::VMXOR_VL) &&
5095 "Unexpected parent");
5096
5097 // The VL should match the parent.
5098 if (Parent->getOperand(2) != N->getOperand(2))
5099 return false;
5100
5101 if (N.getOperand(0).getOpcode() == RISCVISD::VMSET_VL) {
5102 Res = N.getOperand(1);
5103 return true;
5104 }
5105
5106 if (N.getOperand(1).getOpcode() == RISCVISD::VMSET_VL) {
5107 Res = N.getOperand(0);
5108 return true;
5109 }
5110
5111 return false;
5112}
5113
5114// Try to remove sext.w if the input is a W instruction or can be made into
5115// a W instruction cheaply.
5116bool RISCVDAGToDAGISel::doPeepholeSExtW(SDNode *N) {
5117 // Look for the sext.w pattern, addiw rd, rs1, 0.
5118 if (N->getMachineOpcode() != RISCV::ADDIW ||
5119 !isNullConstant(N->getOperand(1)))
5120 return false;
5121
5122 SDValue N0 = N->getOperand(0);
5123 if (!N0.isMachineOpcode())
5124 return false;
5125
5126 switch (N0.getMachineOpcode()) {
5127 default:
5128 break;
5129 case RISCV::ADD:
5130 case RISCV::ADDI:
5131 case RISCV::SUB:
5132 case RISCV::MUL:
5133 case RISCV::SLLI: {
5134 // Convert sext.w+add/sub/mul to their W instructions. This will create
5135 // a new independent instruction. This improves latency.
5136 unsigned Opc;
5137 switch (N0.getMachineOpcode()) {
5138 default:
5139 llvm_unreachable("Unexpected opcode!");
5140 case RISCV::ADD: Opc = RISCV::ADDW; break;
5141 case RISCV::ADDI: Opc = RISCV::ADDIW; break;
5142 case RISCV::SUB: Opc = RISCV::SUBW; break;
5143 case RISCV::MUL: Opc = RISCV::MULW; break;
5144 case RISCV::SLLI: Opc = RISCV::SLLIW; break;
5145 }
5146
5147 SDValue N00 = N0.getOperand(0);
5148 SDValue N01 = N0.getOperand(1);
5149
5150 // Shift amount needs to be uimm5.
5151 if (N0.getMachineOpcode() == RISCV::SLLI &&
5152 !isUInt<5>(cast<ConstantSDNode>(N01)->getSExtValue()))
5153 break;
5154
5155 SDNode *Result =
5156 CurDAG->getMachineNode(Opc, SDLoc(N), N->getValueType(0),
5157 N00, N01);
5158 ReplaceUses(N, Result);
5159 return true;
5160 }
5161 case RISCV::ADDW:
5162 case RISCV::ADDIW:
5163 case RISCV::SUBW:
5164 case RISCV::MULW:
5165 case RISCV::SLLIW:
5166 case RISCV::PACKW:
5167 case RISCV::TH_MULAW:
5168 case RISCV::TH_MULAH:
5169 case RISCV::TH_MULSW:
5170 case RISCV::TH_MULSH:
5171 if (N0.getValueType() == MVT::i32)
5172 break;
5173
5174 // Result is already sign extended just remove the sext.w.
5175 // NOTE: We only handle the nodes that are selected with hasAllWUsers.
5176 ReplaceUses(N, N0.getNode());
5177 return true;
5178 }
5179
5180 return false;
5181}
5182
5183static bool usesAllOnesMask(SDValue MaskOp) {
5184 const auto IsVMSet = [](unsigned Opc) {
5185 return Opc == RISCV::PseudoVMSET_M_B1 || Opc == RISCV::PseudoVMSET_M_B16 ||
5186 Opc == RISCV::PseudoVMSET_M_B2 || Opc == RISCV::PseudoVMSET_M_B32 ||
5187 Opc == RISCV::PseudoVMSET_M_B4 || Opc == RISCV::PseudoVMSET_M_B64 ||
5188 Opc == RISCV::PseudoVMSET_M_B8;
5189 };
5190
5191 // TODO: Check that the VMSET is the expected bitwidth? The pseudo has
5192 // undefined behaviour if it's the wrong bitwidth, so we could choose to
5193 // assume that it's all-ones? Same applies to its VL.
5194 return MaskOp->isMachineOpcode() && IsVMSet(MaskOp.getMachineOpcode());
5195}
5196
5198 if (!V.isMachineOpcode())
5199 return false;
5200 if (V.getMachineOpcode() == TargetOpcode::REG_SEQUENCE) {
5201 for (unsigned I = 1; I < V.getNumOperands(); I += 2)
5202 if (!isImplicitDef(V.getOperand(I)))
5203 return false;
5204 return true;
5205 }
5206 return V.getMachineOpcode() == TargetOpcode::IMPLICIT_DEF;
5207}
5208
5209// Optimize masked RVV pseudo instructions with a known all-ones mask to their
5210// corresponding "unmasked" pseudo versions.
5211bool RISCVDAGToDAGISel::doPeepholeMaskedRVV(MachineSDNode *N) {
5212 const RISCV::RISCVMaskedPseudoInfo *I =
5213 RISCV::getMaskedPseudoInfo(N->getMachineOpcode());
5214 if (!I)
5215 return false;
5216
5217 unsigned MaskOpIdx = I->MaskOpIdx;
5218 if (!usesAllOnesMask(N->getOperand(MaskOpIdx)))
5219 return false;
5220
5221 // There are two classes of pseudos in the table - compares and
5222 // everything else. See the comment on RISCVMaskedPseudo for details.
5223 const unsigned Opc = I->UnmaskedPseudo;
5224 const MCInstrDesc &MCID = TII->get(Opc);
5225 const bool HasPassthru = RISCVII::isFirstDefTiedToFirstUse(MCID);
5226
5227 const MCInstrDesc &MaskedMCID = TII->get(N->getMachineOpcode());
5228 const bool MaskedHasPassthru = RISCVII::isFirstDefTiedToFirstUse(MaskedMCID);
5229
5230 assert((RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags) ||
5232 "Unmasked pseudo has policy but masked pseudo doesn't?");
5233 assert(RISCVII::hasVecPolicyOp(MCID.TSFlags) == HasPassthru &&
5234 "Unexpected pseudo structure");
5235 assert(!(HasPassthru && !MaskedHasPassthru) &&
5236 "Unmasked pseudo has passthru but masked pseudo doesn't?");
5237
5239 // Skip the passthru operand at index 0 if the unmasked don't have one.
5240 bool ShouldSkip = !HasPassthru && MaskedHasPassthru;
5241 bool DropPolicy = !RISCVII::hasVecPolicyOp(MCID.TSFlags) &&
5242 RISCVII::hasVecPolicyOp(MaskedMCID.TSFlags);
5243 bool HasChainOp =
5244 N->getOperand(N->getNumOperands() - 1).getValueType() == MVT::Other;
5245 unsigned LastOpNum = N->getNumOperands() - 1 - HasChainOp;
5246 for (unsigned I = ShouldSkip, E = N->getNumOperands(); I != E; I++) {
5247 // Skip the mask
5248 SDValue Op = N->getOperand(I);
5249 if (I == MaskOpIdx)
5250 continue;
5251 if (DropPolicy && I == LastOpNum)
5252 continue;
5253 Ops.push_back(Op);
5254 }
5255
5256 MachineSDNode *Result =
5257 CurDAG->getMachineNode(Opc, SDLoc(N), N->getVTList(), Ops);
5258
5259 if (!N->memoperands_empty())
5260 CurDAG->setNodeMemRefs(Result, N->memoperands());
5261
5262 Result->setFlags(N->getFlags());
5263 ReplaceUses(N, Result);
5264
5265 return true;
5266}
5267
5268/// If our passthru is an implicit_def, use noreg instead. This side
5269/// steps issues with MachineCSE not being able to CSE expressions with
5270/// IMPLICIT_DEF operands while preserving the semantic intent. See
5271/// pr64282 for context. Note that this transform is the last one
5272/// performed at ISEL DAG to DAG.
5273bool RISCVDAGToDAGISel::doPeepholeNoRegPassThru() {
5274 bool MadeChange = false;
5275 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end();
5276
5277 while (Position != CurDAG->allnodes_begin()) {
5278 SDNode *N = &*--Position;
5279 if (N->use_empty() || !N->isMachineOpcode())
5280 continue;
5281
5282 const unsigned Opc = N->getMachineOpcode();
5283 if (!RISCVVPseudosTable::getPseudoInfo(Opc) ||
5285 !isImplicitDef(N->getOperand(0)))
5286 continue;
5287
5289 Ops.push_back(CurDAG->getRegister(RISCV::NoRegister, N->getValueType(0)));
5290 for (unsigned I = 1, E = N->getNumOperands(); I != E; I++) {
5291 SDValue Op = N->getOperand(I);
5292 Ops.push_back(Op);
5293 }
5294
5295 MachineSDNode *Result =
5296 CurDAG->getMachineNode(Opc, SDLoc(N), N->getVTList(), Ops);
5297 Result->setFlags(N->getFlags());
5298 CurDAG->setNodeMemRefs(Result, cast<MachineSDNode>(N)->memoperands());
5299 ReplaceUses(N, Result);
5300 MadeChange = true;
5301 }
5302 return MadeChange;
5303}
5304
5305
5306// This pass converts a legalized DAG into a RISCV-specific DAG, ready
5307// for instruction scheduling.
5312
5316
5318
5323
static SDValue Widen(SelectionDAG *CurDAG, SDValue N)
return SDValue()
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static msgpack::DocNode getNode(msgpack::DocNode DN, msgpack::Type Type, MCValue Val)
uint64_t uint64_t
uint64_t Imm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
static constexpr Value * getValue(Ty &ValueOrUse)
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
static MCRegister getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define P(N)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static bool getVal(MDTuple *MD, const char *Key, uint64_t &Val)
static bool usesAllOnesMask(SDValue MaskOp)
static Register getTileReg(uint64_t TileNum)
static SDValue selectImm(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT, int64_t Imm, const RISCVSubtarget &Subtarget)
static bool isRegRegScaleLoadOrStore(SDNode *User, SDValue Add, const RISCVSubtarget &Subtarget)
Return true if this a load/store that we have a RegRegScale instruction for.
static std::pair< SDValue, SDValue > extractGPRPair(SelectionDAG *CurDAG, const SDLoc &DL, SDValue Pair)
#define CASE_VMNAND_VMSET_OPCODES(lmulenum, suffix)
static bool isWorthFoldingAdd(SDValue Add)
static SDValue selectImmSeq(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT, RISCVMatInt::InstSeq &Seq)
static bool isImplicitDef(SDValue V)
#define CASE_VMXOR_VMANDN_VMOR_OPCODES(lmulenum, suffix)
static bool selectVSplatImmHelper(SDValue N, SDValue &SplatVal, SelectionDAG &DAG, const RISCVSubtarget &Subtarget, std::function< bool(int64_t)> ValidateImm, bool Decrement=false)
static unsigned getSegInstNF(unsigned Intrinsic)
static bool isWorthFoldingIntoRegRegScale(const RISCVSubtarget &Subtarget, SDValue Add, SDValue Shift=SDValue())
Is it profitable to fold this Add into RegRegScale load/store.
static bool vectorPseudoHasAllNBitUsers(SDNode *User, unsigned UserOpNo, unsigned Bits, const TargetInstrInfo *TII)
static bool selectConstantAddr(SelectionDAG *CurDAG, const SDLoc &DL, const MVT VT, const RISCVSubtarget *Subtarget, SDValue Addr, SDValue &Base, SDValue &Offset, bool IsPrefetch=false)
#define INST_ALL_NF_CASE_WITH_FF(NAME)
#define CASE_VMSLT_OPCODES(lmulenum, suffix)
static SDValue buildGPRPair(SelectionDAG *CurDAG, const SDLoc &DL, MVT VT, SDValue Lo, SDValue Hi)
bool isRegImmLoadOrStore(SDNode *User, SDValue Add)
static SDValue findVSplat(SDValue N)
static bool isApplicableToPLIOrPLUI(int Val)
#define INST_ALL_NF_CASE(NAME)
int64_t C1
MachineInstr *& Lo
SI Fold Operands
#define LLVM_DEBUG(...)
Definition Debug.h:119
#define PASS_NAME
DEMANGLE_DUMP_METHOD void dump() const
bool isZero() const
Definition APFloat.h:1612
APInt bitcastToAPInt() const
Definition APFloat.h:1508
bool isPosZero() const
Definition APFloat.h:1627
bool isNegZero() const
Definition APFloat.h:1628
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:970
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:367
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
LLVM_ABI bool isSplat(unsigned SplatSizeInBits) const
Check if the APInt consists of a repeated bit pattern.
Definition APInt.cpp:627
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:215
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:282
int64_t getSExtValue() const
Get sign extended value.
Definition APInt.h:1582
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
const APFloat & getValueAPF() const
uint64_t getZExtValue() const
int64_t getSExtValue() const
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
This class is used to form a handle around another node that is persistent and is updated across invo...
const SDValue & getValue() const
static StringRef getMemConstraintName(ConstraintCode C)
Definition InlineAsm.h:501
This class is used to represent ISD::LOAD nodes.
Describe properties that are true of each instruction in the target description file.
Machine Value Type.
unsigned getVectorMinNumElements() const
Given a vector type, return the minimum number of elements it contains.
SimpleValueType SimpleTy
uint64_t getScalarSizeInBits() const
MVT changeVectorElementType(MVT EltVT) const
Return a VT for a vector type whose attributes match ourselves with the exception of the element type...
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
bool isScalableVector() const
Return true if this is a vector value type where the runtime length is machine dependent.
TypeSize getSizeInBits() const
Returns the size of the specified MVT in bits.
bool isFixedLengthVector() const
ElementCount getVectorElementCount() const
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
MVT getVectorElementType() const
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
A description of a memory reference used in the backend.
@ MOLoad
The memory access reads data.
@ MONonTemporal
The memory access is non-temporal.
void setFlags(Flags f)
Bitwise OR the current flags with the given flags.
An SDNode that represents everything that will be needed to construct a MachineInstr.
RISCVDAGToDAGISelLegacy(RISCVTargetMachine &TargetMachine, CodeGenOptLevel OptLevel)
bool selectSExtBits(SDValue N, unsigned Bits, SDValue &Val)
bool selectNegImm(SDValue N, SDValue &Val)
bool selectZExtBits(SDValue N, unsigned Bits, SDValue &Val)
bool selectSHXADD_UWOp(SDValue N, unsigned ShAmt, SDValue &Val)
Look for various patterns that can be done with a SHL that can be folded into a SHXADD_UW.
bool areOffsetsWithinAlignment(SDValue Addr, Align Alignment)
bool hasAllNBitUsers(SDNode *Node, unsigned Bits, const unsigned Depth=0) const
bool SelectAddrRegImmLsb00000(SDValue Addr, SDValue &Base, SDValue &Offset)
Similar to SelectAddrRegImm, except that the least significant 5 bits of Offset should be all zeros.
bool SelectBrindRegImm(SDValue Addr, SDValue &Base, SDValue &Offset)
Similar to SelectAddrRegImm, but only matches a register, or a register plus a simm12 offset.
bool selectZExtImm32(SDValue N, SDValue &Val)
bool SelectAddrRegReg(SDValue Addr, SDValue &Base, SDValue &Offset)
bool selectVMNOT_VLOp(SDNode *Parent, SDValue N, SDValue &Res)
void selectVSXSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsOrdered)
void selectVLSEGFF(SDNode *Node, unsigned NF, bool IsMasked)
bool selectVSplatSimm5Plus1NoDec(SDValue N, SDValue &SplatVal)
bool SelectAddrRegImm26(SDValue Addr, SDValue &Base, SDValue &Offset)
Similar to SelectAddrRegImm, except that the offset is a 26-bit signed immediate.
bool selectSimm5Shl2(SDValue N, SDValue &Simm5, SDValue &Shl2)
void selectSF_VC_X_SE(SDNode *Node)
bool orDisjoint(const SDNode *Node) const
bool tryWideningMulAcc(SDNode *Node, const SDLoc &DL)
bool selectLow8BitsVSplat(SDValue N, SDValue &SplatVal)
bool hasAllHUsers(SDNode *Node) const
bool SelectInlineAsmMemoryOperand(const SDValue &Op, InlineAsm::ConstraintCode ConstraintID, std::vector< SDValue > &OutOps) override
SelectInlineAsmMemoryOperand - Select the specified address as a target addressing mode,...
bool selectVSplatSimm5(SDValue N, SDValue &SplatVal)
bool selectSETCC(SDValue N, ISD::CondCode ExpectedCCVal, SDValue &Val, bool OneUse)
RISC-V doesn't have general instructions for integer setne/seteq, but we can check for equality with ...
bool selectRVVSimm5(SDValue N, unsigned Width, SDValue &Imm)
bool SelectAddrFrameIndex(SDValue Addr, SDValue &Base, SDValue &Offset)
bool tryUnsignedBitfieldInsertInZero(SDNode *Node, const SDLoc &DL, MVT VT, SDValue X, unsigned Msb, unsigned Lsb)
bool hasAllWUsers(SDNode *Node) const
void PreprocessISelDAG() override
PreprocessISelDAG - This hook allows targets to hack on the graph before instruction selection starts...
bool selectInvLogicImm(SDValue N, SDValue &Val)
bool SelectAddrRegImm(SDValue Addr, SDValue &Base, SDValue &Offset)
bool SelectAddrRegRegScale(SDValue Addr, ArrayRef< unsigned > Amounts, SDValue &Base, SDValue &Index, SDValue &Scale)
void Select(SDNode *Node) override
Main hook for targets to transform nodes into machine nodes.
void selectXSfmmVSET(SDNode *Node)
bool trySignedBitfieldInsertInSign(SDNode *Node)
bool selectVSplat(SDValue N, SDValue &SplatVal)
void addVectorLoadStoreOperands(SDNode *Node, unsigned SEWImm, const SDLoc &DL, unsigned CurOp, bool IsMasked, bool IsStridedOrIndexed, SmallVectorImpl< SDValue > &Operands, bool IsLoad=false, MVT *IndexVT=nullptr)
void PostprocessISelDAG() override
PostprocessISelDAG() - This hook allows the target to hack on the graph right after selection.
bool SelectAddrRegImm9(SDValue Addr, SDValue &Base, SDValue &Offset)
Similar to SelectAddrRegImm, except that the offset is restricted to uimm9.
bool selectScalarFPAsInt(SDValue N, SDValue &Imm)
bool hasAllBUsers(SDNode *Node) const
void selectVLSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsStrided)
bool tryShrinkShlLogicImm(SDNode *Node)
void selectVSETVLI(SDNode *Node)
bool selectVLOp(SDValue N, SDValue &VL)
bool trySignedBitfieldExtract(SDNode *Node)
bool selectVSplatSimm5Plus1(SDValue N, SDValue &SplatVal)
bool SelectAddrRegZextRegScale(SDValue Addr, ArrayRef< unsigned > Amounts, unsigned Bits, SDValue &Base, SDValue &Index, SDValue &Scale)
bool selectVMNOTOp(SDValue N, SDValue &Res)
void selectVSSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsStrided)
bool selectVSplatImm64Neg(SDValue N, SDValue &SplatVal)
bool selectVSplatSimm5Plus1NonZero(SDValue N, SDValue &SplatVal)
bool tryUnsignedBitfieldExtract(SDNode *Node, const SDLoc &DL, MVT VT, SDValue X, unsigned Msb, unsigned Lsb)
void selectVLXSEG(SDNode *Node, unsigned NF, bool IsMasked, bool IsOrdered)
bool selectShiftMask(SDValue N, unsigned ShiftWidth, SDValue &ShAmt)
bool selectSHXADDOp(SDValue N, unsigned ShAmt, SDValue &Val)
Look for various patterns that can be done with a SHL that can be folded into a SHXADD.
bool tryIndexedLoad(SDNode *Node)
bool selectVSplatUimm(SDValue N, unsigned Bits, SDValue &SplatVal)
RISCVISelDAGToDAGPass(RISCVTargetMachine &TM, CodeGenOptLevel OptLevel)
static std::pair< unsigned, unsigned > decomposeSubvectorInsertExtractToSubRegs(MVT VecVT, MVT SubVecVT, unsigned InsertExtractIdx, const RISCVRegisterInfo *TRI)
static unsigned getRegClassIDForVecVT(MVT VT)
static RISCVVType::VLMUL getLMUL(MVT VT)
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
bool isMachineOpcode() const
Test if this node has a post-isel opcode, directly corresponding to a MachineInstr opcode.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
MVT getSimpleValueType(unsigned ResNo) const
Return the type of a specified result as a simple type.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
const SDValue & getOperand(unsigned Num) const
iterator_range< user_iterator > users()
Represents a use of a SDNode.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node, in exactly one operand.
EVT getValueType() const
Return the ValueType of the referenced return value.
bool isMachineOpcode() const
const SDValue & getOperand(unsigned i) const
uint64_t getConstantOperandVal(unsigned i) const
MVT getSimpleValueType() const
Return the simple ValueType of the referenced return value.
unsigned getMachineOpcode() const
unsigned getOpcode() const
SelectionDAGISelLegacy(char &ID, std::unique_ptr< SelectionDAGISel > S)
SelectionDAGISelPass(std::unique_ptr< SelectionDAGISel > Selector)
const TargetLowering * TLI
const TargetInstrInfo * TII
void ReplaceUses(SDValue F, SDValue T)
ReplaceUses - replace all uses of the old node F with the use of the new node T.
virtual bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const
IsProfitableToFold - Returns true if it's profitable to fold the specific operand node N of U during ...
static bool IsLegalToFold(SDValue N, SDNode *U, SDNode *Root, CodeGenOptLevel OptLevel, bool IgnoreChains=false)
IsLegalToFold - Returns true if the specific operand node N of U can be folded during instruction sel...
bool selectShiftAmountMask(SDValue N, unsigned ShiftWidth, SDValue &ShAmt) const
Look through a mask on the amount N of a shift that reads only the low Log2(ShiftWidth) bits of its a...
void ReplaceNode(SDNode *F, SDNode *T)
Replace all uses of F with T, then remove F from the DAG.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
LLVM_ABI MachineSDNode * getMachineNode(unsigned Opcode, const SDLoc &dl, EVT VT)
These are used for target selectors to create a new node with specified return type(s),...
LLVM_ABI SDValue getRegister(Register Reg, EVT VT)
static constexpr unsigned MaxRecursionDepth
SDValue getSignedTargetConstant(int64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
LLVM_ABI SDValue getTargetExtractSubreg(int SRIdx, const SDLoc &DL, EVT VT, SDValue Operand)
A convenience function for creating TargetInstrInfo::EXTRACT_SUBREG nodes.
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
ilist< SDNode >::iterator allnodes_iterator
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
static constexpr TypeSize getFixed(ScalarTy ExactSize)
Definition TypeSize.h:352
static constexpr TypeSize getScalable(ScalarTy MinimumSize)
Definition TypeSize.h:355
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
Value * getOperand(unsigned i) const
Definition User.h:207
unsigned getNumOperands() const
Definition User.h:229
iterator_range< user_iterator > users()
Definition Value.h:428
#define INT64_MIN
Definition DataTypes.h:74
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ SETCC
SetCC operator - This evaluates to a true value iff the condition is true.
Definition ISDOpcodes.h:851
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:277
@ INSERT_SUBVECTOR
INSERT_SUBVECTOR(VECTOR1, VECTOR2, IDX) - Returns a vector with VECTOR2 inserted into VECTOR1.
Definition ISDOpcodes.h:605
@ ATOMIC_STORE
OUTCHAIN = ATOMIC_STORE(INCHAIN, val, ptr) This corresponds to "store atomic" instruction.
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ ANY_EXTEND
ANY_EXTEND - Used for integer types. The high bits are undefined.
Definition ISDOpcodes.h:885
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:222
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ SIGN_EXTEND
Conversion operators.
Definition ISDOpcodes.h:876
@ SCALAR_TO_VECTOR
SCALAR_TO_VECTOR(VAL) - This represents the operation of loading a scalar value into element 0 of the...
Definition ISDOpcodes.h:681
@ PREFETCH
PREFETCH - This corresponds to a prefetch intrinsic.
@ ATOMIC_LOAD
Val, OUTCHAIN = ATOMIC_LOAD(INCHAIN, ptr) This corresponds to "load atomic" instruction.
@ SPLAT_VECTOR
SPLAT_VECTOR(VAL) - Returns a vector with the scalar value VAL duplicated in all lanes.
Definition ISDOpcodes.h:688
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ EXTRACT_SUBVECTOR
EXTRACT_SUBVECTOR(VECTOR, IDX) - Returns a subvector from VECTOR.
Definition ISDOpcodes.h:619
@ ZERO_EXTEND
ZERO_EXTEND - Used for integer types, zeroing the new bits.
Definition ISDOpcodes.h:882
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:920
@ FP_EXTEND
X = FP_EXTEND(Y) - Extend a smaller FP type into a larger FP type.
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:207
@ TokenFactor
TokenFactor - This node takes multiple tokens as input and produces a single token result.
Definition ISDOpcodes.h:55
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:215
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
CondCode
ISD::CondCode enum - These are ordered carefully to make the bitfields below work out,...
bool isIntEqualitySetCC(CondCode Code)
Return true if this is a setcc instruction that performs an equality comparison when used with intege...
This namespace contains an enum with a value for every intrinsic/builtin function known by LLVM.
static bool hasVLOp(uint64_t TSFlags)
static bool hasVecPolicyOp(uint64_t TSFlags)
static bool hasSEWOp(uint64_t TSFlags)
static bool isFirstDefTiedToFirstUse(const MCInstrDesc &Desc)
InstSeq generateInstSeq(int64_t Val, const MCSubtargetInfo &STI)
int getIntMatCost(const APInt &Val, unsigned Size, const MCSubtargetInfo &STI, bool CompressionCost, bool FreeZeroes)
InstSeq generateTwoRegInstSeq(int64_t Val, const MCSubtargetInfo &STI, unsigned &ShiftAmt, unsigned &AddOpc)
SmallVector< Inst, 8 > InstSeq
Definition RISCVMatInt.h:43
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned encodeXSfmmVType(unsigned SEW, unsigned Widen, bool AltFmt)
LLVM_ABI std::pair< unsigned, bool > decodeVLMUL(VLMUL VLMul)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static unsigned decodeTWiden(unsigned TWiden)
LLVM_ABI unsigned encodeVTYPE(VLMUL VLMUL, unsigned SEW, bool TailAgnostic, bool MaskAgnostic, bool AltFmt=false)
unsigned getRVVMCOpcode(unsigned RVVPseudoOpcode)
std::optional< unsigned > getVectorLowDemandedScalarBits(unsigned Opcode, unsigned Log2SEW)
static constexpr unsigned RVVBitsPerBlock
static constexpr int64_t VLMaxSentinel
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
static const MachineMemOperand::Flags MONontemporalBit1
InstructionCost Cost
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:166
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool isStrongerThanMonotonic(AtomicOrdering AO)
FunctionPass * createRISCVISelDagLegacyPass(RISCVTargetMachine &TM, CodeGenOptLevel OptLevel)
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
@ Load
The value being inserted comes from a load (InsertElement only).
@ Store
The extracted value is stored (ExtractElement only).
constexpr int64_t minIntN(int64_t N)
Gets the minimum value for a N-bit signed integer.
Definition MathExtras.h:224
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:325
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
static const MachineMemOperand::Flags MONontemporalBit0
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:285
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
constexpr bool isShiftedMask_64(uint64_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (64 bit ver...
Definition MathExtras.h:274
unsigned M1(unsigned Val)
Definition VE.h:394
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition bit.h:263
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
constexpr bool isMask_64(uint64_t Value)
Return true if the argument is a non-empty sequence of ones starting at the least significant bit wit...
Definition MathExtras.h:262
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:190
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
constexpr T maskTrailingZeros(unsigned N)
Create a bitmask with the N right-most bits set to 0, and all other bits set to 1.
Definition MathExtras.h:95
@ Add
Sum of integers.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
Definition MCRegister.h:21
DWARFExpression::Operation Op
unsigned M0(unsigned Val)
Definition VE.h:393
constexpr bool isShiftedInt(int64_t x)
Checks if a signed integer is an N bit number shifted left by S.
Definition MathExtras.h:183
constexpr int64_t maxIntN(int64_t N)
Gets the maximum value for a N-bit signed integer.
Definition MathExtras.h:233
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI bool isOneConstant(SDValue V)
Returns true if V is a constant integer one.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition Alignment.h:206
constexpr int64_t SignExtend64(uint64_t x)
Sign-extend the number in the bottom B bits of X to a 64-bit integer.
Definition MathExtras.h:572
constexpr T maskTrailingOnes(unsigned N)
Create a bitmask with the N right-most bits set to 1, and all other bits set to 0.
Definition MathExtras.h:78
constexpr bool isShiftedUInt(uint64_t x)
Checks if a unsigned integer is an N bit number shifted left by S.
Definition MathExtras.h:199
LLVM_ABI bool isAllOnesConstant(SDValue V)
Returns true if V is an integer constant with all bits set.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:879
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Extended Value Type.
Definition ValueTypes.h:35
TypeSize getStoreSize() const
Return the number of bytes overwritten by a store of the specified value type.
Definition ValueTypes.h:433
bool isScalarInteger() const
Return true if this is an integer, but not a vector.
Definition ValueTypes.h:180
This class contains a discriminated union of information about pointers in memory operands,...
MachinePointerInfo getWithOffset(int64_t O) const
static LLVM_ABI MachinePointerInfo getFixedStack(MachineFunction &MF, int FI, int64_t Offset=0)
Return a MachinePointerInfo record that refers to the specified FrameIndex.
This represents a list of ValueType's that has been intern'd by a SelectionDAG.