1//===-- AMDGPUISelLowering.h - AMDGPU 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/// \file
10/// Interface definition of the TargetLowering class that is common
11/// to all AMD GPUs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
16#define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
17
18#include "llvm/CodeGen/CallingConvLower.h"
19#include "llvm/CodeGen/TargetLowering.h"
20
21namespace llvm {
22
23class AMDGPUMachineFunctionInfo;
24class AMDGPUSubtarget;
25struct ArgDescriptor;
26
27class AMDGPUTargetLowering : public TargetLowering {
28private:
29 const AMDGPUSubtarget *Subtarget;
30
31 /// \returns AMDGPUISD::FFBH_U32 node if the incoming \p Op may have been
32 /// legalized from a smaller type VT. Need to match pre-legalized type because
33 /// the generic legalization inserts the add/sub between the select and
34 /// compare.
35 SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const;
36
37public:
38 /// \returns The minimum number of bits needed to store the value of \Op as an
39 /// unsigned integer. Truncating to this size and then zero-extending to the
40 /// original size will not change the value.
41 static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG);
42
43 /// \returns The minimum number of bits needed to store the value of \Op as a
44 /// signed integer. Truncating to this size and then sign-extending to the
45 /// original size will not change the value.
46 static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG);
47
48protected:
49 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
50 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
51 /// Split a vector store into multiple scalar stores.
52 /// \returns The resulting chain.
53
54 SDValue LowerCTLS(SDValue Op, SelectionDAG &DAG) const;
55 SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const;
56 SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const;
57 SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const;
58 SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const;
59
60 SDValue LowerFROUNDEVEN(SDValue Op, SelectionDAG &DAG) const;
61 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const;
62 SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const;
63
64 static bool allowApproxFunc(const SelectionDAG &DAG, SDNodeFlags Flags);
65 static bool needsDenormHandlingF32(const SelectionDAG &DAG, SDValue Src,
66 SDNodeFlags Flags);
67 SDValue getIsLtSmallestNormal(SelectionDAG &DAG, SDValue Op,
68 SDNodeFlags Flags) const;
69 SDValue getIsFinite(SelectionDAG &DAG, SDValue Op, SDNodeFlags Flags) const;
70 std::pair<SDValue, SDValue> getScaledLogInput(SelectionDAG &DAG,
71 const SDLoc SL, SDValue Op,
72 SDNodeFlags Flags) const;
73
74 SDValue LowerFLOG2(SDValue Op, SelectionDAG &DAG) const;
75 SDValue LowerFLOGCommon(SDValue Op, SelectionDAG &DAG) const;
76 SDValue LowerFLOG10(SDValue Op, SelectionDAG &DAG) const;
77 SDValue LowerFLOGUnsafe(SDValue Op, const SDLoc &SL, SelectionDAG &DAG,
78 bool IsLog10, SDNodeFlags Flags) const;
79 SDValue lowerFEXP2(SDValue Op, SelectionDAG &DAG) const;
80
81 SDValue lowerFEXPUnsafeImpl(SDValue Op, const SDLoc &SL, SelectionDAG &DAG,
82 SDNodeFlags Flags, bool IsExp10) const;
83
84 SDValue lowerFEXPUnsafe(SDValue Op, const SDLoc &SL, SelectionDAG &DAG,
85 SDNodeFlags Flags) const;
86 SDValue lowerFEXP10Unsafe(SDValue Op, const SDLoc &SL, SelectionDAG &DAG,
87 SDNodeFlags Flags) const;
88 SDValue lowerFEXP(SDValue Op, SelectionDAG &DAG) const;
89 SDValue lowerFEXPF64(SDValue Op, SelectionDAG &DAG) const;
90 SDValue lowerFPOW(SDValue Op, SelectionDAG &DAG) const;
91
92 SDValue lowerCTLZResults(SDValue Op, SelectionDAG &DAG) const;
93
94 SDValue LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const;
95
96 SDValue LowerINT_TO_FP16(SDValue Op, SelectionDAG &DAG, EVT FP16Ty) const;
97 SDValue LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, bool Signed) const;
98 SDValue LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, bool Signed) const;
99 SDValue lowerINT_TO_FPImpl(SDValue Op, SelectionDAG &DAG, bool Signed) const;
100 SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
101 SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
102
103 SDValue LowerFP_TO_INT64(SDValue Op, SelectionDAG &DAG, bool Signed) const;
104 SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const;
105 SDValue LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const;
106 SDValue LowerFP_TO_INT_SAT(SDValue Op, SelectionDAG &DAG) const;
107
108 SDValue LowerF64ToF16Safe(SDValue Src, const SDLoc &DL,
109 SelectionDAG &DAG) const;
110
111 SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
112
113protected:
114 /// Check whether value Val can be supported by v_mov_b64, for the current
115 /// target.
116 bool isInt64ImmLegal(SDNode *Val, SelectionDAG &DAG) const;
117 bool shouldCombineMemoryType(EVT VT) const;
118 SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
119 SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
120 SDValue performAssertSZExtCombine(SDNode *N, DAGCombinerInfo &DCI) const;
121 SDValue performIntrinsicWOChainCombine(SDNode *N, DAGCombinerInfo &DCI) const;
122
123 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL,
124 unsigned Opc, SDValue LHS,
125 uint32_t ValLo, uint32_t ValHi) const;
126 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
127 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const;
128 SDValue performSrlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
129 SDValue performTruncateCombine(SDNode *N, DAGCombinerInfo &DCI) const;
130 SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const;
131 SDValue performMulLoHiCombine(SDNode *N, DAGCombinerInfo &DCI) const;
132 SDValue performMulhsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
133 SDValue performMulhuCombine(SDNode *N, DAGCombinerInfo &DCI) const;
134 SDValue performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS,
135 SDValue RHS, DAGCombinerInfo &DCI) const;
136
137 SDValue foldFreeOpFromSelect(TargetLowering::DAGCombinerInfo &DCI,
138 SDValue N) const;
139 SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const;
140
141 TargetLowering::NegatibleCost
142 getConstantNegateCost(const ConstantFPSDNode *C) const;
143
144 bool isConstantCostlierToNegate(SDValue N) const;
145 bool isConstantCheaperToNegate(SDValue N) const;
146 SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const;
147 SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
148 SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const;
149
150 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
151
152 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunctionInfo *MFI, SDValue Op,
153 SelectionDAG &DAG) const;
154
155 SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
156
157 /// Return 64-bit value Op as two 32-bit integers.
158 std::pair<SDValue, SDValue> split64BitValue(SDValue Op,
159 SelectionDAG &DAG) const;
160 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const;
161 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const;
162
163 /// Split a vector type into two parts. The first part is a power of two
164 /// vector. The second part is whatever is left over, and is a scalar if it
165 /// would otherwise be a 1-vector.
166 std::pair<EVT, EVT> getSplitDestVTs(const EVT &VT, SelectionDAG &DAG) const;
167
168 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be
169 /// scalar.
170 std::pair<SDValue, SDValue> splitVector(const SDValue &N, const SDLoc &DL,
171 const EVT &LoVT, const EVT &HighVT,
172 SelectionDAG &DAG) const;
173
174 /// Split a vector load into 2 loads of half the vector.
175 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
176
177 /// Widen a suitably aligned v3 load. For all other cases, split the input
178 /// vector load.
179 SDValue WidenOrSplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
180
181 /// Split a vector store into 2 stores of half the vector.
182 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
183
184 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
185 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
186 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
187 SDValue LowerDIVREMToFloat(SDValue Op, SelectionDAG &DAG, bool sign) const;
188 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG,
189 SmallVectorImpl<SDValue> &Results) const;
190
191 void analyzeFormalArgumentsCompute(
192 CCState &State,
193 const SmallVectorImpl<ISD::InputArg> &Ins) const;
194
195public:
196 AMDGPUTargetLowering(const TargetMachine &TM, const TargetSubtargetInfo &STI,
197 const AMDGPUSubtarget &AMDGPUSTI);
198
199 static inline SDValue stripBitcast(SDValue Val) {
200 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(i: 0) : Val;
201 }
202
203 static bool shouldFoldFNegIntoSrc(SDNode *FNeg, SDValue FNegSrc);
204 static bool allUsesHaveSourceMods(const SDNode *N,
205 unsigned CostThreshold = 4);
206 bool isFAbsFree(EVT VT) const override;
207 bool isFNegFree(EVT VT) const override;
208 bool isTruncateFree(EVT Src, EVT Dest) const override;
209 bool isTruncateFree(Type *Src, Type *Dest) const override;
210
211 bool isZExtFree(Type *Src, Type *Dest) const override;
212 bool isZExtFree(EVT Src, EVT Dest) const override;
213
214 SDValue getNegatedExpression(SDValue Op, SelectionDAG &DAG,
215 bool LegalOperations, bool ForCodeSize,
216 NegatibleCost &Cost,
217 unsigned Depth) const override;
218
219 bool isNarrowingProfitable(SDNode *N, EVT SrcVT, EVT DestVT) const override;
220
221 bool isDesirableToCommuteWithShift(const SDNode *N,
222 CombineLevel Level) const override;
223
224 EVT getTypeForExtReturn(LLVMContext &Context, EVT VT,
225 ISD::NodeType ExtendKind) const override;
226
227 unsigned getVectorIdxWidth(const DataLayout &) const override;
228 bool isSelectSupported(SelectSupportKind) const override;
229
230 bool isFPImmLegal(const APFloat &Imm, EVT VT,
231 bool ForCodeSize) const override;
232 bool ShouldShrinkFPConstant(EVT VT) const override;
233 bool shouldReduceLoadWidth(SDNode *Load, ISD::LoadExtType ExtType, EVT ExtVT,
234 std::optional<unsigned> ByteOffset) const override;
235
236 bool isLoadBitCastBeneficial(EVT, EVT, const SelectionDAG &DAG,
237 const MachineMemOperand &MMO) const final;
238
239 bool storeOfVectorConstantIsCheap(bool IsZero, EVT MemVT,
240 unsigned NumElem,
241 unsigned AS) const override;
242 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override;
243 bool isCheapToSpeculateCttz(Type *Ty) const override;
244 bool isCheapToSpeculateCtlz(Type *Ty) const override;
245
246 bool isSDNodeAlwaysUniform(const SDNode *N) const override;
247
248 // FIXME: This hook should not exist
249 AtomicExpansionKind shouldCastAtomicLoadInIR(LoadInst *LI) const override {
250 return AtomicExpansionKind::None;
251 }
252
253 AtomicExpansionKind shouldCastAtomicStoreInIR(StoreInst *SI) const override {
254 return AtomicExpansionKind::None;
255 }
256
257 AtomicExpansionKind shouldCastAtomicRMWIInIR(AtomicRMWInst *) const override {
258 return AtomicExpansionKind::None;
259 }
260
261 bool shouldIssueAtomicLoadForAtomicEmulationLoop() const override {
262 return false;
263 }
264
265 static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg);
266 static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg);
267
268 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
269 const SmallVectorImpl<ISD::OutputArg> &Outs,
270 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
271 SelectionDAG &DAG) const override;
272
273 SDValue addTokenForArgument(SDValue Chain,
274 SelectionDAG &DAG,
275 MachineFrameInfo &MFI,
276 int ClobberedFI) const;
277
278 SDValue lowerUnhandledCall(CallLoweringInfo &CLI,
279 SmallVectorImpl<SDValue> &InVals,
280 StringRef Reason) const;
281 SDValue LowerCall(CallLoweringInfo &CLI,
282 SmallVectorImpl<SDValue> &InVals) const override;
283
284 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const;
285 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
286 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
287
288 bool SimplifyDemandedBitsForTargetNode(SDValue Op,
289 const APInt &OriginalDemandedBits,
290 const APInt &OriginalDemandedElts,
291 KnownBits &Known,
292 TargetLoweringOpt &TLO,
293 unsigned Depth) const override;
294
295 void ReplaceNodeResults(SDNode * N,
296 SmallVectorImpl<SDValue> &Results,
297 SelectionDAG &DAG) const override;
298
299 SDValue combineFMinMaxLegacyImpl(const SDLoc &DL, EVT VT, SDValue LHS,
300 SDValue RHS, SDValue True, SDValue False,
301 SDValue CC, SDNodeFlags Flags,
302 DAGCombinerInfo &DCI) const;
303
304 /// \p Flags must be the select flags, not the compare (SELECT_CC
305 /// flags come from the fcmp and say nothing about the selected value).
306 SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS,
307 SDValue RHS, SDValue True, SDValue False,
308 SDValue CC, SDNodeFlags Flags,
309 DAGCombinerInfo &DCI) const;
310
311 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for
312 // AMDGPU. Commit r319036,
313 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6)
314 // turned on MergeConsecutiveStores() before Instruction Selection for all
315 // targets. Enough AMDGPU compiles go into an infinite loop (
316 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges;
317 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for
318 // now.
319 bool mergeStoresAfterLegalization(EVT) const override { return false; }
320
321 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override {
322 return true;
323 }
324 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
325 int &RefinementSteps, bool &UseOneConstNR,
326 bool Reciprocal) const override;
327 SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
328 int &RefinementSteps) const override;
329
330 virtual SDNode *PostISelFolding(MachineSDNode *N,
331 SelectionDAG &DAG) const = 0;
332
333 /// Determine which of the bits specified in \p Mask are known to be
334 /// either zero or one and return them in the \p KnownZero and \p KnownOne
335 /// bitsets.
336 void computeKnownBitsForTargetNode(const SDValue Op,
337 KnownBits &Known,
338 const APInt &DemandedElts,
339 const SelectionDAG &DAG,
340 unsigned Depth = 0) const override;
341
342 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts,
343 const SelectionDAG &DAG,
344 unsigned Depth = 0) const override;
345
346 unsigned computeNumSignBitsForTargetInstr(GISelValueTracking &Analysis,
347 Register R,
348 const APInt &DemandedElts,
349 const MachineRegisterInfo &MRI,
350 unsigned Depth = 0) const override;
351
352 bool canCreateUndefOrPoisonForTargetNode(
353 SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
354 UndefPoisonKind Kind, bool ConsiderFlags, unsigned Depth) const override;
355
356 bool isKnownNeverNaNForTargetNode(SDValue Op, const APInt &DemandedElts,
357 const SelectionDAG &DAG, bool SNaN = false,
358 unsigned Depth = 0) const override;
359
360 bool isReassocProfitable(MachineRegisterInfo &MRI, Register N0,
361 Register N1) const override;
362
363 /// Helper function that adds Reg to the LiveIn list of the DAG's
364 /// MachineFunction.
365 ///
366 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
367 /// a copy from the register.
368 SDValue CreateLiveInRegister(SelectionDAG &DAG,
369 const TargetRegisterClass *RC,
370 Register Reg, EVT VT,
371 const SDLoc &SL,
372 bool RawReg = false) const;
373 SDValue CreateLiveInRegister(SelectionDAG &DAG,
374 const TargetRegisterClass *RC,
375 Register Reg, EVT VT) const {
376 return CreateLiveInRegister(DAG, RC, Reg, VT, SL: SDLoc(DAG.getEntryNode()));
377 }
378
379 // Returns the raw live in register rather than a copy from it.
380 SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG,
381 const TargetRegisterClass *RC,
382 Register Reg, EVT VT) const {
383 return CreateLiveInRegister(DAG, RC, Reg, VT, SL: SDLoc(DAG.getEntryNode()), RawReg: true);
384 }
385
386 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
387 /// slot rather than passed in a register.
388 SDValue loadStackInputValue(SelectionDAG &DAG,
389 EVT VT,
390 const SDLoc &SL,
391 int64_t Offset) const;
392
393 SDValue storeStackInputValue(SelectionDAG &DAG,
394 const SDLoc &SL,
395 SDValue Chain,
396 SDValue ArgVal,
397 int64_t Offset) const;
398
399 SDValue loadInputValue(SelectionDAG &DAG,
400 const TargetRegisterClass *RC,
401 EVT VT, const SDLoc &SL,
402 const ArgDescriptor &Arg) const;
403
404 enum ImplicitParameter {
405 FIRST_IMPLICIT,
406 PRIVATE_BASE,
407 SHARED_BASE,
408 QUEUE_PTR,
409 };
410
411 /// Helper function that returns the byte offset of the given
412 /// type of implicit parameter.
413 uint32_t getImplicitParameterOffset(const MachineFunction &MF,
414 const ImplicitParameter Param) const;
415 uint32_t getImplicitParameterOffset(const uint64_t ExplicitKernArgSize,
416 const ImplicitParameter Param) const;
417
418 MVT getFenceOperandTy(const DataLayout &DL) const override {
419 return MVT::i32;
420 }
421
422 bool hasMultipleConditionRegisters(EVT VT) const override {
423 // FIXME: This is only partially true. If we have to do vector compares, any
424 // SGPR pair can be a condition register. If we have a uniform condition, we
425 // are better off doing SALU operations, where there is only one SCC. For
426 // now, we don't have a way of knowing during instruction selection if a
427 // condition will be uniform and we always use vector compares. Assume we
428 // are using vector compares until that is fixed.
429 return true;
430 }
431};
432
433/// Strip fabs/fneg/fcopysign from a value to get the underlying source.
434/// Useful for comparing values where sign doesn't matter (e.g., frexp).
435inline SDValue peekFPSignOps(SDValue Val) {
436 while (Val.getOpcode() == ISD::FNEG || Val.getOpcode() == ISD::FABS ||
437 Val.getOpcode() == ISD::FCOPYSIGN)
438 Val = Val.getOperand(i: 0);
439 return Val;
440}
441
442} // End namespace llvm
443
444#endif
445