1//===-- NVPTXISelLowering.h - NVPTX 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 NVPTX uses to lower LLVM code into a
10// selection DAG.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_NVPTX_NVPTXISELLOWERING_H
15#define LLVM_LIB_TARGET_NVPTX_NVPTXISELLOWERING_H
16
17#include "NVPTX.h"
18#include "llvm/CodeGen/SelectionDAG.h"
19#include "llvm/CodeGen/TargetLowering.h"
20#include "llvm/Support/AtomicOrdering.h"
21
22namespace llvm {
23
24class MCContext;
25class MCSymbol;
26class NVPTXSubtarget;
27
28//===--------------------------------------------------------------------===//
29// TargetLowering Implementation
30//===--------------------------------------------------------------------===//
31class NVPTXTargetLowering : public TargetLowering {
32public:
33 explicit NVPTXTargetLowering(const NVPTXTargetMachine &TM,
34 const NVPTXSubtarget &STI);
35 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
36
37 void getTgtMemIntrinsic(SmallVectorImpl<IntrinsicInfo> &Infos,
38 const CallBase &I, MachineFunction &MF,
39 unsigned Intrinsic) const override;
40
41 // Helper for getting a function parameter name. Name is composed from
42 // its index and the function name. Negative index corresponds to special
43 // parameter (unsized array) used for passing variable arguments.
44 MCSymbol *getParamSymbol(MCContext &Ctx, const Function *F, int Idx) const;
45
46 /// isLegalAddressingMode - Return true if the addressing mode represented
47 /// by AM is legal for this target, for a load/store of the specified type
48 /// Used to guide target specific optimizations, like loop strength
49 /// reduction (LoopStrengthReduce.cpp) and memory optimization for
50 /// address mode (CodeGenPrepare.cpp)
51 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty,
52 unsigned AS,
53 Instruction *I = nullptr) const override;
54
55 bool isTruncateFree(Type *SrcTy, Type *DstTy) const override {
56 // Truncating 64-bit to 32-bit is free in SASS.
57 if (!SrcTy->isIntegerTy() || !DstTy->isIntegerTy())
58 return false;
59 return SrcTy->getPrimitiveSizeInBits() == 64 &&
60 DstTy->getPrimitiveSizeInBits() == 32;
61 }
62
63 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Ctx,
64 EVT VT) const override {
65 if (VT.isVector())
66 return EVT::getVectorVT(Context&: Ctx, VT: MVT::i1, NumElements: VT.getVectorNumElements());
67 return MVT::i1;
68 }
69
70 ConstraintType getConstraintType(StringRef Constraint) const override;
71 std::pair<unsigned, const TargetRegisterClass *>
72 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
73 StringRef Constraint, MVT VT) const override;
74
75 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv,
76 bool isVarArg,
77 const SmallVectorImpl<ISD::InputArg> &Ins,
78 const SDLoc &dl, SelectionDAG &DAG,
79 SmallVectorImpl<SDValue> &InVals) const override;
80
81 SDValue LowerCall(CallLoweringInfo &CLI,
82 SmallVectorImpl<SDValue> &InVals) const override;
83
84 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
85 SDValue LowerSTACKSAVE(SDValue Op, SelectionDAG &DAG) const;
86 SDValue LowerSTACKRESTORE(SDValue Op, SelectionDAG &DAG) const;
87
88 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
89 const SmallVectorImpl<ISD::OutputArg> &Outs,
90 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &dl,
91 SelectionDAG &DAG) const override;
92
93 void LowerAsmOperandForConstraint(SDValue Op, StringRef Constraint,
94 std::vector<SDValue> &Ops,
95 SelectionDAG &DAG) const override;
96
97 // PTX always uses 32-bit shift amounts
98 MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override {
99 return MVT::i32;
100 }
101
102 TargetLoweringBase::LegalizeTypeAction
103 getPreferredVectorAction(MVT VT) const override;
104
105 // Get the degree of precision we want from 32-bit floating point division
106 // operations.
107 NVPTX::DivPrecisionLevel getDivF32Level(const MachineFunction &MF,
108 const SDNode &N) const;
109
110 // Get whether we should use a precise or approximate 32-bit floating point
111 // sqrt instruction.
112 bool usePrecSqrtF32(const SDNode *N = nullptr) const;
113
114 // Get whether we should use instructions that flush floating-point denormals
115 // to sign-preserving zero.
116 bool useF16FTZ(const MachineFunction &MF) const;
117 bool useF32FTZ(const MachineFunction &MF) const;
118
119 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
120 int &ExtraSteps, bool &UseOneConst,
121 bool Reciprocal) const override;
122
123 unsigned combineRepeatedFPDivisors() const override { return 2; }
124
125 bool allowFMA(MachineFunction &MF, CodeGenOptLevel OptLevel) const;
126
127 bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF,
128 EVT) const override {
129 return true;
130 }
131
132 // The default is the same as pointer type, but brx.idx only accepts i32
133 MVT getJumpTableRegTy(const DataLayout &) const override { return MVT::i32; }
134
135 unsigned getJumpTableEncoding() const override;
136
137 bool enableAggressiveFMAFusion(EVT VT) const override { return true; }
138
139 // The default is to transform llvm.ctlz(x, false) (where false indicates that
140 // x == 0 is not undefined behavior) into a branch that checks whether x is 0
141 // and avoids calling ctlz in that case. We have a dedicated ctlz
142 // instruction, so we say that ctlz is cheap to speculate.
143 bool isCheapToSpeculateCtlz(Type *Ty) const override { return true; }
144
145 AtomicExpansionKind shouldCastAtomicLoadInIR(LoadInst *LI) const override {
146 return AtomicExpansionKind::None;
147 }
148
149 AtomicExpansionKind shouldCastAtomicStoreInIR(StoreInst *SI) const override {
150 return AtomicExpansionKind::None;
151 }
152
153 AtomicExpansionKind
154 shouldExpandAtomicRMWInIR(const AtomicRMWInst *AI) const override;
155
156 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override {
157 // There's rarely any point of packing something into a vector type if we
158 // already have the source data.
159 return true;
160 }
161
162 bool isTypeDesirableForOp(unsigned Opc, EVT VT) const override {
163 // The DAGCombiner replaces a conversion from i1 with a select of the two
164 // exact results. A selp of the exact result replaces selp.b32 plus a
165 // conversion.
166 if (VT == MVT::i1 && (Opc == ISD::SINT_TO_FP || Opc == ISD::UINT_TO_FP))
167 return false;
168 return TargetLowering::isTypeDesirableForOp(Opc, VT);
169 }
170
171 bool shouldInsertFencesForAtomic(const Instruction *) const override;
172
173 AtomicOrdering
174 atomicOperationOrderAfterFenceSplit(const Instruction *I) const override;
175
176 Instruction *emitLeadingFence(IRBuilderBase &Builder, Instruction *Inst,
177 AtomicOrdering Ord) const override;
178 Instruction *emitTrailingFence(IRBuilderBase &Builder, Instruction *Inst,
179 AtomicOrdering Ord) const override;
180
181 unsigned getPreferredFPToIntOpcode(unsigned Op, EVT FromVT,
182 EVT ToVT) const override;
183
184 void computeKnownBitsForTargetNode(const SDValue Op, KnownBits &Known,
185 const APInt &DemandedElts,
186 const SelectionDAG &DAG,
187 unsigned Depth = 0) const override;
188 bool SimplifyDemandedBitsForTargetNode(SDValue Op, const APInt &DemandedBits,
189 const APInt &DemandedElts,
190 KnownBits &Known,
191 TargetLoweringOpt &TLO,
192 unsigned Depth = 0) const override;
193
194private:
195 const NVPTXSubtarget &STI; // cache the subtarget here
196 mutable unsigned GlobalUniqueCallSite;
197
198 SDValue getParamSymbolNode(SelectionDAG &DAG, int I, EVT T) const;
199 SDValue getCallParamSymbolNode(SelectionDAG &DAG, int I, EVT T) const;
200 SDValue LowerADDRSPACECAST(SDValue Op, SelectionDAG &DAG) const;
201 SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG) const;
202
203 SDValue LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const;
204 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
205 SDValue LowerVECREDUCE(SDValue Op, SelectionDAG &DAG) const;
206 SDValue LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
207 SDValue LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const;
208 SDValue LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const;
209
210 SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const;
211
212 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const;
213 SDValue LowerFROUND32(SDValue Op, SelectionDAG &DAG) const;
214 SDValue LowerFROUND64(SDValue Op, SelectionDAG &DAG) const;
215
216 SDValue PromoteBinOpIfF32FTZ(SDValue Op, SelectionDAG &DAG) const;
217
218 SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
219 SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
220
221 SDValue LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const;
222 SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const;
223
224 SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const;
225 SDValue LowerMLOAD(SDValue Op, SelectionDAG &DAG) const;
226 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
227 SDValue LowerSTOREi1(SDValue Op, SelectionDAG &DAG) const;
228
229 SDValue LowerShiftRightParts(SDValue Op, SelectionDAG &DAG) const;
230 SDValue LowerShiftLeftParts(SDValue Op, SelectionDAG &DAG) const;
231
232 SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) const;
233 SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
234
235 SDValue LowerCopyToReg_128(SDValue Op, SelectionDAG &DAG) const;
236 unsigned getNumRegisters(LLVMContext &Context, EVT VT,
237 std::optional<MVT> RegisterVT) const override;
238 bool
239 splitValueIntoRegisterParts(SelectionDAG &DAG, const SDLoc &DL, SDValue Val,
240 SDValue *Parts, unsigned NumParts, MVT PartVT,
241 std::optional<CallingConv::ID> CC) const override;
242
243 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results,
244 SelectionDAG &DAG) const override;
245 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
246
247 bool mayFoldFMULIntoFMA(SDNode *N, MachineFunction &MF,
248 CodeGenOptLevel OptLevel) const;
249 SDValue performScalarizeV2F32Op(SDNode *N, DAGCombinerInfo &DCI,
250 CodeGenOptLevel OptLevel) const;
251 SDValue performFADDCombineWithOperands(SDNode *N, SDValue N0, SDValue N1,
252 DAGCombinerInfo &DCI,
253 CodeGenOptLevel OptLevel) const;
254 SDValue performFADDCombine(SDNode *N, DAGCombinerInfo &DCI,
255 CodeGenOptLevel OptLevel) const;
256};
257
258} // namespace llvm
259
260#endif
261