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