1//===-- RISCVISelLowering.h - RISC-V DAG Lowering Interface -----*- C++ -*-===//
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 the interfaces that RISC-V uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_RISCV_RISCVISELLOWERING_H
15#define LLVM_LIB_TARGET_RISCV_RISCVISELLOWERING_H
16
17#include "RISCV.h"
18#include "RISCVCallingConv.h"
19#include "llvm/CodeGen/CallingConvLower.h"
20#include "llvm/CodeGen/SelectionDAG.h"
21#include "llvm/CodeGen/TargetLowering.h"
22#include <optional>
23
24namespace llvm {
25class InstructionCost;
26class RISCVSubtarget;
27struct RISCVRegisterInfo;
28
29class RISCVTargetLowering : public TargetLowering {
30 const RISCVSubtarget &Subtarget;
31
32public:
33 explicit RISCVTargetLowering(const TargetMachine &TM,
34 const RISCVSubtarget &STI);
35
36 const RISCVSubtarget &getSubtarget() const { return Subtarget; }
37
38 void getTgtMemIntrinsic(SmallVectorImpl<IntrinsicInfo> &Infos,
39 const CallBase &I, MachineFunction &MF,
40 unsigned Intrinsic) const override;
41 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
42 unsigned AS,
43 Instruction *I = nullptr) const override;
44 bool isLegalICmpImmediate(int64_t Imm) const override;
45 bool isLegalAddImmediate(int64_t Imm) const override;
46 bool isTruncateFree(Type *SrcTy, Type *DstTy) const override;
47 bool isTruncateFree(EVT SrcVT, EVT DstVT) const override;
48 bool isTruncateFree(SDValue Val, EVT VT2) const override;
49 bool isZExtFree(SDValue Val, EVT VT2) const override;
50 bool isSExtCheaperThanZExt(EVT SrcVT, EVT DstVT) const override;
51 bool signExtendConstant(const ConstantInt *CI) const override;
52 bool isCheapToSpeculateCttz(Type *Ty) const override;
53 bool isCheapToSpeculateCtlz(Type *Ty) const override;
54 bool isMaskAndCmp0FoldingBeneficial(const Instruction &AndI) const override;
55 bool hasAndNotCompare(SDValue Y) const override;
56 bool hasAndNot(SDValue Y) const override;
57 bool hasBitTest(SDValue X, SDValue Y) const override;
58 bool shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd(
59 SDValue X, ConstantSDNode *XC, ConstantSDNode *CC, SDValue Y,
60 unsigned OldShiftOpcode, unsigned NewShiftOpcode,
61 SelectionDAG &DAG) const override;
62 bool shouldScalarizeBinop(SDValue VecOp) const override;
63 bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override;
64 int getLegalZfaFPImm(const APFloat &Imm, EVT VT) const;
65 FPImmAction getFPImmAction(const APFloat &Imm, EVT VT,
66 bool ForCodeSize) const override;
67 FPImmAction getFPImmAction(const APFloat &Imm, LLT Ty,
68 bool ForCodeSize) const override;
69 ExtractSubvectorCost getExtractSubvectorCost(EVT ResVT, EVT SrcVT,
70 unsigned Index) const override;
71
72 bool isIntDivCheap(EVT VT, AttributeList Attr) const override;
73
74 bool preferScalarizeSplat(SDNode *N) const override;
75
76 void finalizeLowering(MachineFunction &MF) const override;
77
78 /// Customize the preferred legalization strategy for certain types.
79 LegalizeTypeAction getPreferredVectorAction(MVT VT) const override;
80
81 /// Return the register type for a given MVT, ensuring vectors are treated
82 /// as a series of gpr sized integers.
83 MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC,
84 EVT VT) const override;
85
86 /// Return the number of registers for a given MVT, for inline assembly
87 unsigned
88 getNumRegisters(LLVMContext &Context, EVT VT,
89 std::optional<MVT> RegisterVT = std::nullopt) const override;
90
91 /// Return the number of registers for a given MVT, ensuring vectors are
92 /// treated as a series of gpr sized integers.
93 unsigned getNumRegistersForCallingConv(LLVMContext &Context,
94 CallingConv::ID CC,
95 EVT VT) const override;
96
97 bool shouldFoldSelectWithIdentityConstant(unsigned BinOpcode, EVT VT,
98 unsigned SelectOpcode, SDValue X,
99 SDValue Y) const override;
100
101 /// Return true if the given shuffle mask can be codegen'd directly, or if it
102 /// should be stack expanded.
103 bool isShuffleMaskLegal(ArrayRef<int> M, EVT VT) const override;
104
105 bool isVectorClearMaskLegal(ArrayRef<int> M, EVT VT) const override;
106
107 bool isMultiStoresCheaperThanBitsMerge(EVT LTy, EVT HTy) const override {
108 // If the pair to store is a mixture of float and int values, we will
109 // save two bitwise instructions and one float-to-int instruction and
110 // increase one store instruction. There is potentially a more
111 // significant benefit because it avoids the float->int domain switch
112 // for input value. So It is more likely a win.
113 if ((LTy.isFloatingPoint() && HTy.isInteger()) ||
114 (LTy.isInteger() && HTy.isFloatingPoint()))
115 return true;
116 // If the pair only contains int values, we will save two bitwise
117 // instructions and increase one store instruction (costing one more
118 // store buffer). Since the benefit is more blurred we leave such a pair
119 // out until we get testcase to prove it is a win.
120 return false;
121 }
122
123 bool
124 shouldExpandBuildVectorWithShuffles(EVT VT,
125 unsigned DefinedValues) const override;
126
127 /// Return the cost of LMUL for linear operations.
128 InstructionCost getLMULCost(MVT VT) const;
129
130 InstructionCost getVRGatherVVCost(MVT VT) const;
131 InstructionCost getVRGatherVICost(MVT VT) const;
132 InstructionCost getVSlideVXCost(MVT VT) const;
133 InstructionCost getVSlideVICost(MVT VT) const;
134
135 // Provide custom lowering hooks for some operations.
136 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
137 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
138 SelectionDAG &DAG) const override;
139
140 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
141
142 bool targetShrinkDemandedConstant(SDValue Op, const APInt &DemandedBits,
143 const APInt &DemandedElts,
144 TargetLoweringOpt &TLO) const override;
145
146 void computeKnownBitsForTargetNode(const SDValue Op,
147 KnownBits &Known,
148 const APInt &DemandedElts,
149 const SelectionDAG &DAG,
150 unsigned Depth) const override;
151
152 void computeKnownBitsForTargetInstr(GISelValueTracking &Analysis, Register R,
153 KnownBits &Known,
154 const APInt &DemandedElts,
155 const MachineRegisterInfo &MRI,
156 unsigned Depth = 0) const override;
157 unsigned ComputeNumSignBitsForTargetNode(SDValue Op,
158 const APInt &DemandedElts,
159 const SelectionDAG &DAG,
160 unsigned Depth) const override;
161
162 bool SimplifyDemandedBitsForTargetNode(SDValue Op, const APInt &DemandedBits,
163 const APInt &DemandedElts,
164 KnownBits &Known,
165 TargetLoweringOpt &TLO,
166 unsigned Depth) const override;
167
168 bool canCreateUndefOrPoisonForTargetNode(
169 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
170 UndefPoisonKind Kind, bool ConsiderFlags, unsigned Depth) const override;
171
172 const Constant *getTargetConstantFromLoad(LoadSDNode *LD) const override;
173
174 MachineMemOperand::Flags
175 getTargetMMOFlags(const Instruction &I) const override;
176
177 MachineMemOperand::Flags
178 getTargetMMOFlags(const MemSDNode &Node) const override;
179
180 bool
181 areTwoSDNodeTargetMMOFlagsMergeable(const MemSDNode &NodeX,
182 const MemSDNode &NodeY) const override;
183
184 ConstraintType getConstraintType(StringRef Constraint) const override;
185
186 InlineAsm::ConstraintCode
187 getInlineAsmMemConstraint(StringRef ConstraintCode) const override;
188
189 std::pair<unsigned, const TargetRegisterClass *>
190 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
191 StringRef Constraint, MVT VT) const override;
192
193 void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint,
194 std::vector<SDValue> &Ops,
195 SelectionDAG &DAG) const override;
196
197 MachineBasicBlock *
198 EmitInstrWithCustomInserter(MachineInstr &MI,
199 MachineBasicBlock *BB) const override;
200
201 void AdjustInstrPostInstrSelection(MachineInstr &MI,
202 SDNode *Node) const override;
203
204 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context,
205 EVT VT) const override;
206
207 CondMergingParams
208 getJumpConditionMergingParams(Instruction::BinaryOps Opc, const Value *LHS,
209 const Value *RHS,
210 const Function *F) const override;
211
212 bool shouldFormOverflowOp(unsigned Opcode, EVT VT,
213 bool MathUsed) const override {
214 if (VT == MVT::i8 || VT == MVT::i16)
215 return false;
216
217 return TargetLowering::shouldFormOverflowOp(Opcode, VT, MathUsed);
218 }
219
220 bool storeOfVectorConstantIsCheap(bool IsZero, EVT MemVT, unsigned NumElem,
221 unsigned AddrSpace) const override {
222 // Replacing 4 or more element stores is a net reduction in instructions
223 // even after accounting for the vmv.v.i needed to materialize the zero
224 // vector. MemVT may already be a vector, since memset lowers to LMUL1
225 // stores that we rely on this merge to widen, so use the total element
226 // count.
227 unsigned NumMemElts = MemVT.isVector() ? MemVT.getVectorNumElements() : 1;
228 if (IsZero)
229 return NumElem * NumMemElts >= 4;
230
231 // Materializing a non-zero vector constant isn't free (it may need
232 // several instructions like li + vmv.v.x or a constant-pool load), so stay
233 // conservative and only merge when replacing 4 or more stores.
234 return NumElem >= 4;
235 }
236
237 bool convertSetCCLogicToBitwiseLogic(EVT VT) const override {
238 return VT.isScalarInteger();
239 }
240 bool convertSelectOfConstantsToMath(EVT VT) const override { return true; }
241
242 bool isCtpopFast(EVT VT) const override;
243
244 unsigned getCustomCtpopCost(EVT VT, ISD::CondCode Cond) const override;
245
246 bool preferZeroCompareBranch() const override { return true; }
247
248 bool hasMultipleConditionRegisters(EVT VT) const override;
249
250 // Note that one specific case requires fence insertion for an
251 // AtomicCmpXchgInst but is handled via the RISCVZacasABIFix pass rather
252 // than this hook due to limitations in the interface here.
253 bool shouldInsertFencesForAtomic(const Instruction *I) const override;
254
255 Instruction *emitLeadingFence(IRBuilderBase &Builder, Instruction *Inst,
256 AtomicOrdering Ord) const override;
257 Instruction *emitTrailingFence(IRBuilderBase &Builder, Instruction *Inst,
258 AtomicOrdering Ord) const override;
259
260 bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
261 EVT VT) const override;
262
263 ISD::NodeType getExtendForAtomicOps() const override {
264 return ISD::SIGN_EXTEND;
265 }
266
267 ISD::NodeType getExtendForAtomicCmpSwapArg() const override;
268 ISD::NodeType getExtendForAtomicRMWArg(unsigned Op) const override;
269
270 bool shouldTransformSignedTruncationCheck(EVT XVT,
271 unsigned KeptBits) const override;
272
273 TargetLowering::ShiftLegalizationStrategy
274 preferredShiftLegalizationStrategy(SelectionDAG &DAG, SDNode *N,
275 unsigned ExpansionFactor) const override {
276 if (DAG.getMachineFunction().getFunction().hasMinSize())
277 return ShiftLegalizationStrategy::LowerToLibcall;
278 return TargetLowering::preferredShiftLegalizationStrategy(DAG, N,
279 ExpansionFactor);
280 }
281
282 bool isDesirableToCommuteWithShift(const SDNode *N,
283 CombineLevel Level) const override;
284
285 /// If a physical register, this returns the register that receives the
286 /// exception address on entry to an EH pad.
287 Register
288 getExceptionPointerRegister(ExceptionHandling EH,
289 const Constant *PersonalityFn) const override;
290
291 /// If a physical register, this returns the register that receives the
292 /// exception typeid on entry to a landing pad.
293 Register
294 getExceptionSelectorRegister(ExceptionHandling EH,
295 const Constant *PersonalityFn) const override;
296
297 bool shouldExtendTypeInLibCall(EVT Type) const override;
298 bool shouldSignExtendTypeInLibCall(Type *Ty, bool IsSigned) const override;
299
300 /// Returns the register with the specified architectural or ABI name. This
301 /// method is necessary to lower the llvm.read_register.* and
302 /// llvm.write_register.* intrinsics. Allocatable registers must be reserved
303 /// with the clang -ffixed-xX flag for access to be allowed.
304 Register getRegisterByName(const char *RegName, LLT VT,
305 const MachineFunction &MF) const override;
306
307 // Lower incoming arguments, copy physregs into vregs
308 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
309 bool IsVarArg,
310 const SmallVectorImpl<ISD::InputArg> &Ins,
311 const SDLoc &DL, SelectionDAG &DAG,
312 SmallVectorImpl<SDValue> &InVals) const override;
313 bool CanLowerReturn(CallingConv::ID CallConv, MachineFunction &MF,
314 bool IsVarArg,
315 const SmallVectorImpl<ISD::OutputArg> &Outs,
316 LLVMContext &Context, const Type *RetTy) const override;
317 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
318 const SmallVectorImpl<ISD::OutputArg> &Outs,
319 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
320 SelectionDAG &DAG) const override;
321 SDValue LowerCall(TargetLowering::CallLoweringInfo &CLI,
322 SmallVectorImpl<SDValue> &InVals) const override;
323
324 bool shouldConvertConstantLoadToIntImm(const APInt &Imm,
325 Type *Ty) const override;
326 bool isUsedByReturnOnly(SDNode *N, SDValue &Chain) const override;
327 bool mayBeEmittedAsTailCall(const CallInst *CI) const override;
328 bool shouldConsiderGEPOffsetSplit() const override { return true; }
329
330 bool decomposeMulByConstant(LLVMContext &Context, EVT VT,
331 SDValue C) const override;
332
333 bool isMulAddWithConstProfitable(SDValue AddNode,
334 SDValue ConstNode) const override;
335
336 TargetLowering::AtomicExpansionKind
337 shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const override;
338 Value *emitMaskedAtomicRMWIntrinsic(IRBuilderBase &Builder, AtomicRMWInst *AI,
339 Value *AlignedAddr, Value *Incr,
340 Value *Mask, Value *ShiftAmt,
341 AtomicOrdering Ord) const override;
342 TargetLowering::AtomicExpansionKind
343 shouldExpandAtomicCmpXchgInIR(const AtomicCmpXchgInst *CI) const override;
344 Value *emitMaskedAtomicCmpXchgIntrinsic(IRBuilderBase &Builder,
345 AtomicCmpXchgInst *CI,
346 Value *AlignedAddr, Value *CmpVal,
347 Value *NewVal, Value *Mask,
348 AtomicOrdering Ord) const override;
349
350 /// Returns true if the target allows unaligned memory accesses of the
351 /// specified type.
352 bool allowsMisalignedMemoryAccesses(
353 EVT VT, unsigned AddrSpace = 0, Align Alignment = Align(1),
354 MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
355 unsigned *Fast = nullptr) const override;
356
357 bool allowsMisalignedMemoryAccesses(
358 LLT Ty, unsigned AddrSpace = 0, Align Alignment = Align(1),
359 MachineMemOperand::Flags Flags = MachineMemOperand::MONone,
360 unsigned *Fast = nullptr) const override;
361
362 EVT getOptimalMemOpType(LLVMContext &Context, const MemOp &Op,
363 const AttributeList &FuncAttributes) const override;
364
365 bool splitValueIntoRegisterParts(
366 SelectionDAG & DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
367 unsigned NumParts, MVT PartVT, std::optional<CallingConv::ID> CC)
368 const override;
369
370 SDValue joinRegisterPartsIntoValue(
371 SelectionDAG & DAG, const SDLoc &DL, const SDValue *Parts,
372 unsigned NumParts, MVT PartVT, EVT ValueVT,
373 std::optional<CallingConv::ID> CC) const override;
374
375 // Return the value of VLMax for the given vector type (i.e. SEW and LMUL)
376 SDValue computeVLMax(MVT VecVT, const SDLoc &DL, SelectionDAG &DAG) const;
377
378 static RISCVVType::VLMUL getLMUL(MVT VT);
379 inline static unsigned computeVLMAX(unsigned VectorBits, unsigned EltSize,
380 unsigned MinSize) {
381 // Original equation:
382 // VLMAX = (VectorBits / EltSize) * LMUL
383 // where LMUL = MinSize / RISCV::RVVBitsPerBlock
384 // The following equations have been reordered to prevent loss of precision
385 // when calculating fractional LMUL.
386 return ((VectorBits / EltSize) * MinSize) / RISCV::RVVBitsPerBlock;
387 }
388
389 // Return inclusive (low, high) bounds on the value of VLMAX for the
390 // given scalable container type given known bounds on VLEN.
391 static std::pair<unsigned, unsigned>
392 computeVLMAXBounds(MVT ContainerVT, const RISCVSubtarget &Subtarget);
393
394 /// Given a vector (either fixed or scalable), return the scalable vector
395 /// corresponding to a vector register (i.e. an m1 register group).
396 static MVT getM1VT(MVT VT) {
397 unsigned EltSizeInBits = VT.getVectorElementType().getSizeInBits();
398 assert(EltSizeInBits <= RISCV::RVVBitsPerBlock && "Unexpected vector MVT");
399 return MVT::getScalableVectorVT(VT: VT.getVectorElementType(),
400 NumElements: RISCV::RVVBitsPerBlock / EltSizeInBits);
401 }
402
403 static unsigned getRegClassIDForLMUL(RISCVVType::VLMUL LMul);
404 static unsigned getSubregIndexByMVT(MVT VT, unsigned Index);
405 static unsigned getRegClassIDForVecVT(MVT VT);
406 static std::pair<unsigned, unsigned>
407 decomposeSubvectorInsertExtractToSubRegs(MVT VecVT, MVT SubVecVT,
408 unsigned InsertExtractIdx,
409 const RISCVRegisterInfo *TRI);
410 MVT getContainerForFixedLengthVector(MVT VT) const;
411
412 bool shouldRemoveExtendFromGSIndex(SDValue Extend, EVT DataVT) const override;
413
414 bool isLegalElementTypeForRVV(EVT ScalarTy) const;
415
416 bool shouldConvertFpToSat(unsigned Op, EVT FPVT, EVT VT) const override;
417
418 unsigned getJumpTableEncoding() const override;
419
420 const MCExpr *LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
421 const MachineBasicBlock *MBB,
422 unsigned uid,
423 MCContext &Ctx) const override;
424
425 bool getIndexedAddressParts(SDNode *Op, SDValue &Base, SDValue &Offset,
426 ISD::MemIndexedMode &AM, SelectionDAG &DAG) const;
427 bool getPreIndexedAddressParts(SDNode *N, SDValue &Base, SDValue &Offset,
428 ISD::MemIndexedMode &AM,
429 SelectionDAG &DAG) const override;
430 bool getPostIndexedAddressParts(SDNode *N, SDNode *Op, SDValue &Base,
431 SDValue &Offset, ISD::MemIndexedMode &AM,
432 SelectionDAG &DAG) const override;
433
434 bool isLegalScaleForGatherScatter(uint64_t Scale,
435 uint64_t ElemSize) const override {
436 // Scaled addressing not supported on indexed load/stores
437 return Scale == 1;
438 }
439
440 /// If the target has a standard location for the stack protector cookie,
441 /// returns the address of that location. Otherwise, returns nullptr.
442 Value *getIRStackGuard(IRBuilderBase &IRB,
443 const LibcallLoweringInfo &Libcalls) const override;
444
445 /// Returns whether or not generating a interleaved load/store intrinsic for
446 /// this type will be legal.
447 bool isLegalInterleavedAccessType(VectorType *VTy, unsigned Factor,
448 Align Alignment, unsigned AddrSpace,
449 const DataLayout &) const;
450
451 /// Return true if a stride load store of the given result type and
452 /// alignment is legal.
453 bool isLegalStridedLoadStore(EVT DataType, Align Alignment) const;
454
455 /// Return true if a fault-only-first load of the given result type and
456 /// alignment is legal.
457 bool isLegalFirstFaultLoad(EVT DataType, Align Alignment) const;
458
459 unsigned getMaxSupportedInterleaveFactor() const override { return 8; }
460
461 bool fallBackToDAGISel(const Instruction &Inst) const override;
462
463 bool lowerInterleavedLoad(Instruction *Load, Value *Mask,
464 ArrayRef<ShuffleVectorInst *> Shuffles,
465 ArrayRef<unsigned> Indices, unsigned Factor,
466 const APInt &GapMask) const override;
467
468 bool lowerInterleavedStore(Instruction *Store, Value *Mask,
469 ShuffleVectorInst *SVI, unsigned Factor,
470 const APInt &GapMask) const override;
471
472 bool lowerDeinterleaveIntrinsicToLoad(Instruction *Load, Value *Mask,
473 IntrinsicInst *DI,
474 const APInt &GapMask) const override;
475
476 bool lowerInterleaveIntrinsicToStore(
477 Instruction *Store, Value *Mask,
478 ArrayRef<Value *> InterleaveValues) const override;
479
480 bool supportKCFIBundles() const override { return true; }
481
482 SDValue expandIndirectJTBranch(const SDLoc &dl, SDValue Value, SDValue Addr,
483 int JTI, SelectionDAG &DAG) const override;
484
485 MachineInstr *EmitKCFICheck(MachineBasicBlock &MBB,
486 MachineBasicBlock::instr_iterator &MBBI,
487 const TargetInstrInfo *TII) const override;
488
489 /// True if stack clash protection is enabled for this functions.
490 bool hasInlineStackProbe(const MachineFunction &MF) const override;
491
492 unsigned getStackProbeSize(const MachineFunction &MF, Align StackAlign) const;
493
494 MachineBasicBlock *emitDynamicProbedAlloc(MachineInstr &MI,
495 MachineBasicBlock *MBB) const;
496
497 ArrayRef<MCPhysReg> getRoundingControlRegisters() const override;
498
499 bool shouldFoldMaskToVariableShiftPair(SDValue Y) const override;
500
501 /// Control the following reassociation of operands: (op (op x, c1), y) -> (op
502 /// (op x, y), c1) where N0 is (op x, c1) and N1 is y.
503 bool isReassocProfitable(SelectionDAG &DAG, SDValue N0,
504 SDValue N1) const override;
505
506 /// Match a mask which "spreads" the leading elements of a vector evenly
507 /// across the result. Factor is the spread amount, and Index is the
508 /// offset applied.
509 static bool isSpreadMask(ArrayRef<int> Mask, unsigned Factor,
510 unsigned &Index);
511
512private:
513 template <class NodeTy>
514 SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true,
515 bool IsExternWeak = false) const;
516 SDValue getStaticTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG,
517 bool UseGOT) const;
518 SDValue getDynamicTLSAddr(GlobalAddressSDNode *N, SelectionDAG &DAG) const;
519 SDValue getTLSDescAddr(GlobalAddressSDNode *N, SelectionDAG &DAG) const;
520
521 SDValue lowerConstantFP(SDValue Op, SelectionDAG &DAG) const;
522 SDValue lowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
523 SDValue lowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
524 SDValue lowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
525 SDValue lowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
526 SDValue lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
527 SDValue lowerSELECT(SDValue Op, SelectionDAG &DAG) const;
528 SDValue lowerBRCOND(SDValue Op, SelectionDAG &DAG) const;
529 SDValue lowerVASTART(SDValue Op, SelectionDAG &DAG) const;
530 SDValue lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
531 SDValue lowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
532 SDValue lowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
533 SDValue lowerShiftRightParts(SDValue Op, SelectionDAG &DAG, bool IsSRA) const;
534 SDValue lowerSPLAT_VECTOR_PARTS(SDValue Op, SelectionDAG &DAG) const;
535 SDValue lowerVectorMaskSplat(SDValue Op, SelectionDAG &DAG) const;
536 SDValue lowerVectorMaskExt(SDValue Op, SelectionDAG &DAG,
537 int64_t ExtTrueVal) const;
538 SDValue lowerVectorMaskTrunc(SDValue Op, SelectionDAG &DAG) const;
539 SDValue lowerVectorTrunc(SDValue Op, SelectionDAG &DAG) const;
540 SDValue lowerVectorFPExtendOrRound(SDValue Op, SelectionDAG &DAG) const;
541 SDValue lowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
542 SDValue lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
543 SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
544 SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const;
545 SDValue LowerINTRINSIC_VOID(SDValue Op, SelectionDAG &DAG) const;
546 SDValue lowerVPREDUCE(SDValue Op, SelectionDAG &DAG) const;
547 SDValue lowerVECREDUCE(SDValue Op, SelectionDAG &DAG) const;
548 SDValue lowerVectorMaskVecReduction(SDValue Op, SelectionDAG &DAG,
549 bool IsVP) const;
550 SDValue lowerFPVECREDUCE(SDValue Op, SelectionDAG &DAG) const;
551 SDValue lowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
552 SDValue lowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
553 SDValue lowerVECTOR_DEINTERLEAVE(SDValue Op, SelectionDAG &DAG) const;
554 SDValue lowerVECTOR_INTERLEAVE(SDValue Op, SelectionDAG &DAG) const;
555 SDValue lowerSTEP_VECTOR(SDValue Op, SelectionDAG &DAG) const;
556 SDValue lowerVECTOR_REVERSE(SDValue Op, SelectionDAG &DAG) const;
557 SDValue lowerVECTOR_SPLICE(SDValue Op, SelectionDAG &DAG) const;
558 SDValue lowerABS(SDValue Op, SelectionDAG &DAG) const;
559 SDValue lowerMaskedLoad(SDValue Op, SelectionDAG &DAG) const;
560 SDValue lowerLoadFF(SDValue Op, SelectionDAG &DAG) const;
561 SDValue lowerMaskedStore(SDValue Op, SelectionDAG &DAG) const;
562 SDValue lowerVectorCompress(SDValue Op, SelectionDAG &DAG) const;
563 SDValue lowerMaskedGather(SDValue Op, SelectionDAG &DAG) const;
564 SDValue lowerMaskedScatter(SDValue Op, SelectionDAG &DAG) const;
565 SDValue lowerFixedLengthVectorLoadToRVV(SDValue Op, SelectionDAG &DAG) const;
566 SDValue lowerFixedLengthVectorStoreToRVV(SDValue Op, SelectionDAG &DAG) const;
567 SDValue lowerToScalableOp(SDValue Op, SelectionDAG &DAG) const;
568 SDValue LowerIS_FPCLASS(SDValue Op, SelectionDAG &DAG) const;
569 SDValue lowerVPOp(SDValue Op, SelectionDAG &DAG) const;
570 SDValue lowerVPMergeMask(SDValue Op, SelectionDAG &DAG) const;
571 SDValue lowerVPSpliceExperimental(SDValue Op, SelectionDAG &DAG) const;
572 SDValue lowerVPReverseExperimental(SDValue Op, SelectionDAG &DAG) const;
573 SDValue lowerVPStridedLoad(SDValue Op, SelectionDAG &DAG) const;
574 SDValue lowerVPStridedStore(SDValue Op, SelectionDAG &DAG) const;
575 SDValue lowerVPCttzElements(SDValue Op, SelectionDAG &DAG) const;
576 SDValue lowerGET_ROUNDING(SDValue Op, SelectionDAG &DAG) const;
577 SDValue lowerSET_ROUNDING(SDValue Op, SelectionDAG &DAG) const;
578 SDValue lowerGET_FPENV(SDValue Op, SelectionDAG &DAG) const;
579 SDValue lowerSET_FPENV(SDValue Op, SelectionDAG &DAG) const;
580 SDValue lowerRESET_FPENV(SDValue Op, SelectionDAG &DAG) const;
581 SDValue lowerGET_FPMODE(SDValue Op, SelectionDAG &DAG) const;
582 SDValue lowerSET_FPMODE(SDValue Op, SelectionDAG &DAG) const;
583 SDValue lowerRESET_FPMODE(SDValue Op, SelectionDAG &DAG) const;
584
585 SDValue lowerEH_DWARF_CFA(SDValue Op, SelectionDAG &DAG) const;
586 SDValue lowerCTLZ_CTTZ_ZERO_POISON(SDValue Op, SelectionDAG &DAG) const;
587
588 SDValue lowerStrictFPExtendOrRoundLike(SDValue Op, SelectionDAG &DAG) const;
589
590 SDValue lowerVectorStrictFSetcc(SDValue Op, SelectionDAG &DAG) const;
591
592 SDValue lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
593
594 SDValue expandUnalignedRVVLoad(SDValue Op, SelectionDAG &DAG) const;
595 SDValue expandUnalignedRVVStore(SDValue Op, SelectionDAG &DAG) const;
596
597 SDValue expandUnalignedVPLoad(SDValue Op, SelectionDAG &DAG) const;
598 SDValue expandUnalignedVPStore(SDValue Op, SelectionDAG &DAG) const;
599
600 SDValue lowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
601 SDValue lowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
602 SDValue lowerPARTIAL_REDUCE_MLA(SDValue Op, SelectionDAG &DAG) const;
603 SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
604
605 SDValue lowerXAndesBfHCvtBFloat16Load(SDValue Op, SelectionDAG &DAG) const;
606 SDValue lowerXAndesBfHCvtBFloat16Store(SDValue Op, SelectionDAG &DAG) const;
607
608 bool isEligibleForTailCallOptimization(
609 CCState &CCInfo, CallLoweringInfo &CLI, MachineFunction &MF,
610 const SmallVector<CCValAssign, 16> &ArgLocs) const;
611
612 /// Generate error diagnostics if any register used by CC has been marked
613 /// reserved.
614 void validateCCReservedRegs(
615 const SmallVectorImpl<std::pair<llvm::Register, llvm::SDValue>> &Regs,
616 MachineFunction &MF) const;
617
618 bool useRVVForFixedLengthVectorVT(MVT VT) const;
619
620 MVT getVPExplicitVectorLengthTy() const override;
621
622 bool shouldExpandGetVectorLength(EVT TripCountVT, unsigned VF,
623 bool IsScalable) const override;
624
625 bool isProfitableToFoldVScaleAdd(SDValue N) const override;
626
627 /// RVV code generation for fixed length vectors does not lower all
628 /// BUILD_VECTORs. This makes BUILD_VECTOR legalisation a source of stores to
629 /// merge. However, merging them creates a BUILD_VECTOR that is just as
630 /// illegal as the original, thus leading to an infinite legalisation loop.
631 /// NOTE: Once BUILD_VECTOR can be custom lowered for all legal vector types,
632 /// this override can be removed.
633 bool mergeStoresAfterLegalization(EVT VT) const override;
634
635 /// Disable normalizing
636 /// select(N0&N1, X, Y) => select(N0, select(N1, X, Y), Y) and
637 /// select(N0|N1, X, Y) => select(N0, select(N1, X, Y, Y))
638 /// RISC-V doesn't have flags so it's better to perform the and/or in a GPR.
639 bool shouldNormalizeToSelectSequence(LLVMContext &, EVT, EVT) const override {
640 return false;
641 }
642
643 /// Disables storing and loading vectors by default when there are function
644 /// calls between the load and store, since these are more expensive than just
645 /// using scalars
646 bool shouldMergeStoreOfLoadsOverCall(EVT SrcVT, EVT MergedVT) const override {
647 return !MergedVT.isVector() || SrcVT.isVector();
648 }
649
650 /// For available scheduling models FDIV + two independent FMULs are much
651 /// faster than two FDIVs.
652 unsigned combineRepeatedFPDivisors() const override;
653
654 SDValue BuildSDIVPow2(SDNode *N, const APInt &Divisor, SelectionDAG &DAG,
655 SmallVectorImpl<SDNode *> &Created) const override;
656
657 bool shouldFoldSelectWithSingleBitTest(EVT VT,
658 const APInt &AndMask) const override;
659
660 unsigned getMinimumJumpTableEntries() const override;
661
662 SDValue emitFlushICache(SelectionDAG &DAG, SDValue InChain, SDValue Start,
663 SDValue End, SDValue Flags, SDLoc DL) const;
664
665 std::pair<const TargetRegisterClass *, uint8_t>
666 findRepresentativeClass(const TargetRegisterInfo *TRI, MVT VT) const override;
667};
668
669namespace RISCVVIntrinsicsTable {
670
671struct RISCVVIntrinsicInfo {
672 unsigned IntrinsicID;
673 uint8_t ScalarOperand;
674 uint8_t VLOperand;
675 bool IsFPIntrinsic;
676 bool hasScalarOperand() const {
677 // 0xF is not valid. See NoScalarOperand in IntrinsicsRISCV.td.
678 return ScalarOperand != 0xF;
679 }
680 bool hasVLOperand() const {
681 // 0x1F is not valid. See NoVLOperand in IntrinsicsRISCV.td.
682 return VLOperand != 0x1F;
683 }
684};
685
686using namespace RISCV;
687
688#define GET_RISCVVIntrinsicsTable_DECL
689#include "RISCVGenSearchableTables.inc"
690#undef GET_RISCVVIntrinsicsTable_DECL
691
692} // end namespace RISCVVIntrinsicsTable
693
694} // end namespace llvm
695
696#endif
697