1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Custom DAG lowering for SI
13 //===----------------------------------------------------------------------===//
15 #include "SIISelLowering.h"
17 #include "AMDGPUSubtarget.h"
18 #include "AMDILIntrinsicInfo.h"
19 #include "SIInstrInfo.h"
20 #include "SIMachineFunctionInfo.h"
21 #include "SIRegisterInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/IR/Function.h"
30 SITargetLowering::SITargetLowering(TargetMachine &TM) :
31 AMDGPUTargetLowering(TM) {
32 addRegisterClass(MVT::i1, &AMDGPU::VReg_1RegClass);
33 addRegisterClass(MVT::i64, &AMDGPU::SReg_64RegClass);
35 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
36 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
38 addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
39 addRegisterClass(MVT::f32, &AMDGPU::VReg_32RegClass);
41 addRegisterClass(MVT::f64, &AMDGPU::VReg_64RegClass);
42 addRegisterClass(MVT::v2i32, &AMDGPU::SReg_64RegClass);
43 addRegisterClass(MVT::v2f32, &AMDGPU::VReg_64RegClass);
45 addRegisterClass(MVT::v4i32, &AMDGPU::SReg_128RegClass);
46 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
48 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
49 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
51 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
52 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
54 computeRegisterProperties();
57 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
58 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
59 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
60 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
61 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
62 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
64 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
65 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
66 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
67 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
68 setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
69 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
71 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
72 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
73 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
74 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
76 setOperationAction(ISD::ADD, MVT::i32, Legal);
77 setOperationAction(ISD::ADDC, MVT::i32, Legal);
78 setOperationAction(ISD::ADDE, MVT::i32, Legal);
80 // We need to custom lower vector stores from local memory
81 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
82 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
83 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
84 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
86 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
89 // We need to custom lower loads/stores from private memory
90 setOperationAction(ISD::LOAD, MVT::i32, Custom);
91 setOperationAction(ISD::LOAD, MVT::i64, Custom);
92 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
93 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
94 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
96 setOperationAction(ISD::STORE, MVT::i1, Custom);
97 setOperationAction(ISD::STORE, MVT::i32, Custom);
98 setOperationAction(ISD::STORE, MVT::i64, Custom);
99 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
100 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
102 setOperationAction(ISD::SELECT, MVT::f32, Promote);
103 AddPromotedToType(ISD::SELECT, MVT::f32, MVT::i32);
104 setOperationAction(ISD::SELECT, MVT::i64, Custom);
105 setOperationAction(ISD::SELECT, MVT::f64, Promote);
106 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
108 setOperationAction(ISD::SELECT_CC, MVT::f32, Expand);
109 setOperationAction(ISD::SELECT_CC, MVT::i32, Expand);
110 setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
111 setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
113 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
114 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
125 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
132 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
133 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
134 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
135 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
137 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
139 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
140 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
141 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
142 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
143 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
144 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
146 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
147 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
148 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
149 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
151 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
152 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
153 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
154 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
155 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
157 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
158 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
159 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
160 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
161 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
162 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
164 setOperationAction(ISD::LOAD, MVT::i1, Custom);
166 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
167 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
168 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
170 // These should use UDIVREM, so set them to expand
171 setOperationAction(ISD::UDIV, MVT::i64, Expand);
172 setOperationAction(ISD::UREM, MVT::i64, Expand);
174 // We only support LOAD/STORE and vector manipulation ops for vectors
175 // with > 4 elements.
177 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
180 for (MVT VT : VecTypes) {
181 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
185 case ISD::BUILD_VECTOR:
187 case ISD::EXTRACT_VECTOR_ELT:
188 case ISD::INSERT_VECTOR_ELT:
189 case ISD::CONCAT_VECTORS:
190 case ISD::INSERT_SUBVECTOR:
191 case ISD::EXTRACT_SUBVECTOR:
194 setOperationAction(Op, VT, Expand);
200 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
201 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
202 setOperationAction(ISD::FTRUNC, VT, Expand);
203 setOperationAction(ISD::FCEIL, VT, Expand);
204 setOperationAction(ISD::FFLOOR, VT, Expand);
207 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
208 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
209 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
210 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
211 setOperationAction(ISD::FRINT, MVT::f64, Legal);
214 setTargetDAGCombine(ISD::SELECT_CC);
215 setTargetDAGCombine(ISD::SETCC);
217 setSchedulingPreference(Sched::RegPressure);
220 //===----------------------------------------------------------------------===//
221 // TargetLowering queries
222 //===----------------------------------------------------------------------===//
224 bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
226 bool *IsFast) const {
230 // XXX: This depends on the address space and also we may want to revist
231 // the alignment values we specify in the DataLayout.
233 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
234 // which isn't a simple VT.
235 if (!VT.isSimple() || VT == MVT::Other)
238 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
239 // see what for specifically. The wording everywhere else seems to be the
242 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
243 // no alignment restrictions.
244 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
245 // Using any pair of GPRs should be the same as any other pair.
248 return VT.bitsGE(MVT::i64);
251 // XXX - The only mention I see of this in the ISA manual is for LDS direct
252 // reads the "byte address and must be dword aligned". Is it also true for the
253 // normal loads and stores?
254 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
257 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
258 // byte-address are ignored, thus forcing Dword alignment.
261 return VT.bitsGT(MVT::i32);
264 bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
265 return VT.getScalarType().bitsLE(MVT::i16);
268 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
270 const SIInstrInfo *TII =
271 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
272 return TII->isInlineConstant(Imm);
275 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
276 SDLoc DL, SDValue Chain,
277 unsigned Offset, bool Signed) const {
278 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
279 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
280 AMDGPUAS::CONSTANT_ADDRESS);
281 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
282 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
283 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
284 DAG.getConstant(Offset, MVT::i64));
285 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
286 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
287 false, false, MemVT.getSizeInBits() >> 3);
291 SDValue SITargetLowering::LowerFormalArguments(
293 CallingConv::ID CallConv,
295 const SmallVectorImpl<ISD::InputArg> &Ins,
296 SDLoc DL, SelectionDAG &DAG,
297 SmallVectorImpl<SDValue> &InVals) const {
299 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
301 MachineFunction &MF = DAG.getMachineFunction();
302 FunctionType *FType = MF.getFunction()->getFunctionType();
303 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
305 assert(CallConv == CallingConv::C);
307 SmallVector<ISD::InputArg, 16> Splits;
308 uint32_t Skipped = 0;
310 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
311 const ISD::InputArg &Arg = Ins[i];
313 // First check if it's a PS input addr
314 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
315 !Arg.Flags.isByVal()) {
317 assert((PSInputNum <= 15) && "Too many PS inputs!");
320 // We can savely skip PS inputs
326 Info->PSInputAddr |= 1 << PSInputNum++;
329 // Second split vertices into their elements
330 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
331 ISD::InputArg NewArg = Arg;
332 NewArg.Flags.setSplit();
333 NewArg.VT = Arg.VT.getVectorElementType();
335 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
336 // three or five element vertex only needs three or five registers,
337 // NOT four or eigth.
338 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
339 unsigned NumElements = ParamType->getVectorNumElements();
341 for (unsigned j = 0; j != NumElements; ++j) {
342 Splits.push_back(NewArg);
343 NewArg.PartOffset += NewArg.VT.getStoreSize();
346 } else if (Info->ShaderType != ShaderType::COMPUTE) {
347 Splits.push_back(Arg);
351 SmallVector<CCValAssign, 16> ArgLocs;
352 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
353 getTargetMachine(), ArgLocs, *DAG.getContext());
355 // At least one interpolation mode must be enabled or else the GPU will hang.
356 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
357 Info->PSInputAddr |= 1;
358 CCInfo.AllocateReg(AMDGPU::VGPR0);
359 CCInfo.AllocateReg(AMDGPU::VGPR1);
362 // The pointer to the list of arguments is stored in SGPR0, SGPR1
363 if (Info->ShaderType == ShaderType::COMPUTE) {
364 CCInfo.AllocateReg(AMDGPU::SGPR0);
365 CCInfo.AllocateReg(AMDGPU::SGPR1);
366 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
369 if (Info->ShaderType == ShaderType::COMPUTE) {
370 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
374 AnalyzeFormalArguments(CCInfo, Splits);
376 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
378 const ISD::InputArg &Arg = Ins[i];
379 if (Skipped & (1 << i)) {
380 InVals.push_back(DAG.getUNDEF(Arg.VT));
384 CCValAssign &VA = ArgLocs[ArgIdx++];
385 EVT VT = VA.getLocVT();
389 EVT MemVT = Splits[i].VT;
390 // The first 36 bytes of the input buffer contains information about
391 // thread group and global sizes.
392 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
393 36 + VA.getLocMemOffset(),
394 Ins[i].Flags.isSExt());
395 InVals.push_back(Arg);
398 assert(VA.isRegLoc() && "Parameter must be in a register!");
400 unsigned Reg = VA.getLocReg();
402 if (VT == MVT::i64) {
403 // For now assume it is a pointer
404 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
405 &AMDGPU::SReg_64RegClass);
406 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
407 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
411 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
413 Reg = MF.addLiveIn(Reg, RC);
414 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
416 if (Arg.VT.isVector()) {
418 // Build a vector from the registers
419 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
420 unsigned NumElements = ParamType->getVectorNumElements();
422 SmallVector<SDValue, 4> Regs;
424 for (unsigned j = 1; j != NumElements; ++j) {
425 Reg = ArgLocs[ArgIdx++].getLocReg();
426 Reg = MF.addLiveIn(Reg, RC);
427 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
430 // Fill up the missing vector elements
431 NumElements = Arg.VT.getVectorNumElements() - NumElements;
432 for (unsigned j = 0; j != NumElements; ++j)
433 Regs.push_back(DAG.getUNDEF(VT));
435 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
439 InVals.push_back(Val);
444 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
445 MachineInstr * MI, MachineBasicBlock * BB) const {
447 MachineBasicBlock::iterator I = *MI;
448 const SIInstrInfo *TII =
449 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
450 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
452 switch (MI->getOpcode()) {
454 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
455 case AMDGPU::BRANCH: return BB;
456 case AMDGPU::SI_ADDR64_RSRC: {
457 unsigned SuperReg = MI->getOperand(0).getReg();
458 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
459 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
460 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
461 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
462 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
463 .addOperand(MI->getOperand(1));
464 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
466 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
467 .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
468 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
470 .addImm(AMDGPU::sub0)
472 .addImm(AMDGPU::sub1);
473 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
475 .addImm(AMDGPU::sub0_sub1)
477 .addImm(AMDGPU::sub2_sub3);
478 MI->eraseFromParent();
481 case AMDGPU::V_SUB_F64:
482 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
483 MI->getOperand(0).getReg())
484 .addReg(MI->getOperand(1).getReg())
485 .addReg(MI->getOperand(2).getReg())
486 .addImm(0) /* src2 */
488 .addImm(0) /* CLAMP */
489 .addImm(0) /* OMOD */
490 .addImm(2); /* NEG */
491 MI->eraseFromParent();
494 case AMDGPU::SI_RegisterStorePseudo: {
495 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
496 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
497 MachineInstrBuilder MIB =
498 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
500 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
501 MIB.addOperand(MI->getOperand(i));
503 MI->eraseFromParent();
506 case AMDGPU::FABS_SI: {
507 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
508 const SIInstrInfo *TII =
509 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
510 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
511 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
514 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_AND_B32_e32),
515 MI->getOperand(0).getReg())
516 .addReg(MI->getOperand(1).getReg())
518 MI->eraseFromParent();
521 case AMDGPU::FNEG_SI: {
522 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
523 const SIInstrInfo *TII =
524 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
525 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass);
526 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_MOV_B32_e32),
529 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_XOR_B32_e32),
530 MI->getOperand(0).getReg())
531 .addReg(MI->getOperand(1).getReg())
533 MI->eraseFromParent();
536 case AMDGPU::FCLAMP_SI: {
537 const SIInstrInfo *TII =
538 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
539 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F32_e64),
540 MI->getOperand(0).getReg())
541 .addImm(0) // SRC0 modifiers
542 .addOperand(MI->getOperand(1))
543 .addImm(0) // SRC1 modifiers
547 MI->eraseFromParent();
553 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
554 if (!VT.isVector()) {
557 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
560 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
564 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
565 VT = VT.getScalarType();
570 switch (VT.getSimpleVT().SimpleTy) {
572 return false; /* There is V_MAD_F32 for f32 */
582 //===----------------------------------------------------------------------===//
583 // Custom DAG Lowering Operations
584 //===----------------------------------------------------------------------===//
586 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
587 MachineFunction &MF = DAG.getMachineFunction();
588 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
589 switch (Op.getOpcode()) {
590 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
591 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
593 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
594 if (Op.getValueType().isVector() &&
595 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
596 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
597 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
598 Op.getValueType().getVectorNumElements() > 4))) {
599 SDValue MergedValues[2] = {
600 SplitVectorLoad(Op, DAG),
603 return DAG.getMergeValues(MergedValues, SDLoc(Op));
605 return LowerLOAD(Op, DAG);
609 case ISD::SELECT: return LowerSELECT(Op, DAG);
610 case ISD::STORE: return LowerSTORE(Op, DAG);
611 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
612 case ISD::INTRINSIC_WO_CHAIN: {
613 unsigned IntrinsicID =
614 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
615 EVT VT = Op.getValueType();
617 //XXX: Hardcoded we only use two to store the pointer to the parameters.
618 unsigned NumUserSGPRs = 2;
619 switch (IntrinsicID) {
620 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
621 case Intrinsic::r600_read_ngroups_x:
622 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
623 case Intrinsic::r600_read_ngroups_y:
624 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
625 case Intrinsic::r600_read_ngroups_z:
626 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
627 case Intrinsic::r600_read_global_size_x:
628 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
629 case Intrinsic::r600_read_global_size_y:
630 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
631 case Intrinsic::r600_read_global_size_z:
632 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
633 case Intrinsic::r600_read_local_size_x:
634 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
635 case Intrinsic::r600_read_local_size_y:
636 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
637 case Intrinsic::r600_read_local_size_z:
638 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
639 case Intrinsic::r600_read_tgid_x:
640 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
641 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
642 case Intrinsic::r600_read_tgid_y:
643 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
644 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
645 case Intrinsic::r600_read_tgid_z:
646 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
647 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
648 case Intrinsic::r600_read_tidig_x:
649 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
651 case Intrinsic::r600_read_tidig_y:
652 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
654 case Intrinsic::r600_read_tidig_z:
655 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
657 case AMDGPUIntrinsic::SI_load_const: {
663 MachineMemOperand *MMO = MF.getMachineMemOperand(
664 MachinePointerInfo(),
665 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
666 VT.getSizeInBits() / 8, 4);
667 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
668 Op->getVTList(), Ops, VT, MMO);
670 case AMDGPUIntrinsic::SI_sample:
671 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
672 case AMDGPUIntrinsic::SI_sampleb:
673 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
674 case AMDGPUIntrinsic::SI_sampled:
675 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
676 case AMDGPUIntrinsic::SI_samplel:
677 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
678 case AMDGPUIntrinsic::SI_vs_load_input:
679 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
686 case ISD::INTRINSIC_VOID:
687 SDValue Chain = Op.getOperand(0);
688 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
690 switch (IntrinsicID) {
691 case AMDGPUIntrinsic::SI_tbuffer_store: {
709 EVT VT = Op.getOperand(3).getValueType();
711 MachineMemOperand *MMO = MF.getMachineMemOperand(
712 MachinePointerInfo(),
713 MachineMemOperand::MOStore,
714 VT.getSizeInBits() / 8, 4);
715 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
716 Op->getVTList(), Ops, VT, MMO);
725 /// \brief Helper function for LowerBRCOND
726 static SDNode *findUser(SDValue Value, unsigned Opcode) {
728 SDNode *Parent = Value.getNode();
729 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
732 if (I.getUse().get() != Value)
735 if (I->getOpcode() == Opcode)
741 /// This transforms the control flow intrinsics to get the branch destination as
742 /// last parameter, also switches branch target with BR if the need arise
743 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
744 SelectionDAG &DAG) const {
748 SDNode *Intr = BRCOND.getOperand(1).getNode();
749 SDValue Target = BRCOND.getOperand(2);
750 SDNode *BR = nullptr;
752 if (Intr->getOpcode() == ISD::SETCC) {
753 // As long as we negate the condition everything is fine
754 SDNode *SetCC = Intr;
755 assert(SetCC->getConstantOperandVal(1) == 1);
756 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
758 Intr = SetCC->getOperand(0).getNode();
761 // Get the target from BR if we don't negate the condition
762 BR = findUser(BRCOND, ISD::BR);
763 Target = BR->getOperand(1);
766 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
768 // Build the result and
769 SmallVector<EVT, 4> Res;
770 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
771 Res.push_back(Intr->getValueType(i));
773 // operands of the new intrinsic call
774 SmallVector<SDValue, 4> Ops;
775 Ops.push_back(BRCOND.getOperand(0));
776 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
777 Ops.push_back(Intr->getOperand(i));
778 Ops.push_back(Target);
780 // build the new intrinsic call
781 SDNode *Result = DAG.getNode(
782 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
783 DAG.getVTList(Res), Ops).getNode();
786 // Give the branch instruction our target
791 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops);
794 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
796 // Copy the intrinsic results to registers
797 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
798 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
802 Chain = DAG.getCopyToReg(
804 CopyToReg->getOperand(1),
805 SDValue(Result, i - 1),
808 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
811 // Remove the old intrinsic from the chain
812 DAG.ReplaceAllUsesOfValueWith(
813 SDValue(Intr, Intr->getNumValues() - 1),
814 Intr->getOperand(0));
819 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
821 LoadSDNode *Load = cast<LoadSDNode>(Op);
822 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
823 SDValue MergedValues[2];
824 MergedValues[1] = Load->getChain();
826 MergedValues[0] = Ret;
827 return DAG.getMergeValues(MergedValues, DL);
830 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
834 EVT MemVT = Load->getMemoryVT();
836 assert(!MemVT.isVector() && "Private loads should be scalarized");
837 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
839 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
840 DAG.getConstant(2, MVT::i32));
841 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
842 Load->getChain(), Ptr,
843 DAG.getTargetConstant(0, MVT::i32),
845 if (MemVT.getSizeInBits() == 64) {
846 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
847 DAG.getConstant(1, MVT::i32));
849 SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
850 Load->getChain(), IncPtr,
851 DAG.getTargetConstant(0, MVT::i32),
854 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
857 MergedValues[0] = Ret;
858 return DAG.getMergeValues(MergedValues, DL);
862 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
864 SelectionDAG &DAG) const {
865 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
871 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
872 if (Op.getValueType() != MVT::i64)
876 SDValue Cond = Op.getOperand(0);
878 SDValue Zero = DAG.getConstant(0, MVT::i32);
879 SDValue One = DAG.getConstant(1, MVT::i32);
881 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
882 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
884 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
885 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
887 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
889 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
890 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
892 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
894 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
895 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
898 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
900 StoreSDNode *Store = cast<StoreSDNode>(Op);
901 EVT VT = Store->getMemoryVT();
903 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
907 if (VT.isVector() && VT.getVectorNumElements() >= 8)
908 return SplitVectorStore(Op, DAG);
911 return DAG.getTruncStore(Store->getChain(), DL,
912 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
913 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
915 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
918 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
919 DAG.getConstant(2, MVT::i32));
920 SDValue Chain = Store->getChain();
921 SmallVector<SDValue, 8> Values;
923 if (Store->isTruncatingStore()) {
925 if (Store->getMemoryVT() == MVT::i8) {
927 } else if (Store->getMemoryVT() == MVT::i16) {
930 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
931 Chain, Store->getBasePtr(),
932 DAG.getConstant(0, MVT::i32));
933 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
934 DAG.getConstant(0x3, MVT::i32));
935 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
936 DAG.getConstant(3, MVT::i32));
937 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
938 DAG.getConstant(Mask, MVT::i32));
939 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
940 MaskedValue, ShiftAmt);
941 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
942 DAG.getConstant(32, MVT::i32), ShiftAmt);
943 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
944 DAG.getConstant(Mask, MVT::i32),
946 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
947 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
949 Values.push_back(Dst);
950 } else if (VT == MVT::i64) {
951 for (unsigned i = 0; i < 2; ++i) {
952 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
953 Store->getValue(), DAG.getConstant(i, MVT::i32)));
955 } else if (VT == MVT::i128) {
956 for (unsigned i = 0; i < 2; ++i) {
957 for (unsigned j = 0; j < 2; ++j) {
958 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
959 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
960 Store->getValue(), DAG.getConstant(i, MVT::i32)),
961 DAG.getConstant(j, MVT::i32)));
965 Values.push_back(Store->getValue());
968 for (unsigned i = 0; i < Values.size(); ++i) {
969 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
970 Ptr, DAG.getConstant(i, MVT::i32));
971 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
972 Chain, Values[i], PartPtr,
973 DAG.getTargetConstant(0, MVT::i32));
978 //===----------------------------------------------------------------------===//
979 // Custom DAG optimizations
980 //===----------------------------------------------------------------------===//
982 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
983 DAGCombinerInfo &DCI) const {
984 SelectionDAG &DAG = DCI.DAG;
986 EVT VT = N->getValueType(0);
988 switch (N->getOpcode()) {
989 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
990 case ISD::SELECT_CC: {
991 ConstantSDNode *True, *False;
992 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
993 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
994 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
995 && True->isAllOnesValue()
996 && False->isNullValue()
998 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
999 N->getOperand(1), N->getOperand(4));
1005 SDValue Arg0 = N->getOperand(0);
1006 SDValue Arg1 = N->getOperand(1);
1007 SDValue CC = N->getOperand(2);
1008 ConstantSDNode * C = nullptr;
1009 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1011 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1013 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1014 && Arg0.getOperand(0).getValueType() == MVT::i1
1015 && (C = dyn_cast<ConstantSDNode>(Arg1))
1017 && CCOp == ISD::SETNE) {
1018 return SimplifySetCC(VT, Arg0.getOperand(0),
1019 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1025 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1028 /// \brief Test if RegClass is one of the VSrc classes
1029 static bool isVSrc(unsigned RegClass) {
1030 return AMDGPU::VSrc_32RegClassID == RegClass ||
1031 AMDGPU::VSrc_64RegClassID == RegClass;
1034 /// \brief Test if RegClass is one of the SSrc classes
1035 static bool isSSrc(unsigned RegClass) {
1036 return AMDGPU::SSrc_32RegClassID == RegClass ||
1037 AMDGPU::SSrc_64RegClassID == RegClass;
1040 /// \brief Analyze the possible immediate value Op
1042 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1043 /// and the immediate value if it's a literal immediate
1044 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1051 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1052 if (Node->getZExtValue() >> 32) {
1055 Imm.I = Node->getSExtValue();
1056 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1057 if (N->getValueType(0) != MVT::f32)
1059 Imm.F = Node->getValueAPF().convertToFloat();
1061 return -1; // It isn't an immediate
1063 if ((Imm.I >= -16 && Imm.I <= 64) ||
1064 Imm.F == 0.5f || Imm.F == -0.5f ||
1065 Imm.F == 1.0f || Imm.F == -1.0f ||
1066 Imm.F == 2.0f || Imm.F == -2.0f ||
1067 Imm.F == 4.0f || Imm.F == -4.0f)
1068 return 0; // It's an inline immediate
1070 return Imm.I; // It's a literal immediate
1073 /// \brief Try to fold an immediate directly into an instruction
1074 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1075 bool &ScalarSlotUsed) const {
1077 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1078 const SIInstrInfo *TII =
1079 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1080 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
1083 const SDValue &Op = Mov->getOperand(0);
1084 int32_t Value = analyzeImmediate(Op.getNode());
1086 // Not an immediate at all
1089 } else if (Value == 0) {
1090 // Inline immediates can always be fold
1094 } else if (Value == Immediate) {
1095 // Already fold literal immediate
1099 } else if (!ScalarSlotUsed && !Immediate) {
1100 // Fold this literal immediate
1101 ScalarSlotUsed = true;
1111 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1112 SelectionDAG &DAG, const SDValue &Op) const {
1113 const SIInstrInfo *TII =
1114 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1115 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1117 if (!Op->isMachineOpcode()) {
1118 switch(Op->getOpcode()) {
1119 case ISD::CopyFromReg: {
1120 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1121 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1122 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1123 return MRI.getRegClass(Reg);
1125 return TRI.getPhysRegClass(Reg);
1127 default: return nullptr;
1130 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1131 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1132 if (OpClassID != -1) {
1133 return TRI.getRegClass(OpClassID);
1135 switch(Op.getMachineOpcode()) {
1136 case AMDGPU::COPY_TO_REGCLASS:
1137 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1138 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1140 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1141 // class, then the register class for the value could be either a
1142 // VReg or and SReg. In order to get a more accurate
1143 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1144 OpClassID == AMDGPU::VSrc_64RegClassID) {
1145 return getRegClassForNode(DAG, Op.getOperand(0));
1147 return TRI.getRegClass(OpClassID);
1148 case AMDGPU::EXTRACT_SUBREG: {
1149 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1150 const TargetRegisterClass *SuperClass =
1151 getRegClassForNode(DAG, Op.getOperand(0));
1152 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1154 case AMDGPU::REG_SEQUENCE:
1155 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1156 return TRI.getRegClass(
1157 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1159 return getRegClassFor(Op.getSimpleValueType());
1163 /// \brief Does "Op" fit into register class "RegClass" ?
1164 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1165 unsigned RegClass) const {
1166 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1167 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1171 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1174 /// \brief Make sure that we don't exeed the number of allowed scalars
1175 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1177 bool &ScalarSlotUsed) const {
1179 // First map the operands register class to a destination class
1180 if (RegClass == AMDGPU::VSrc_32RegClassID)
1181 RegClass = AMDGPU::VReg_32RegClassID;
1182 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1183 RegClass = AMDGPU::VReg_64RegClassID;
1187 // Nothing to do if they fit naturally
1188 if (fitsRegClass(DAG, Operand, RegClass))
1191 // If the scalar slot isn't used yet use it now
1192 if (!ScalarSlotUsed) {
1193 ScalarSlotUsed = true;
1197 // This is a conservative aproach. It is possible that we can't determine the
1198 // correct register class and copy too often, but better safe than sorry.
1199 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1200 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1201 Operand.getValueType(), Operand, RC);
1202 Operand = SDValue(Node, 0);
1205 /// \returns true if \p Node's operands are different from the SDValue list
1207 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1208 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1209 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1216 /// \brief Try to fold the Nodes operands into the Node
1217 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1218 SelectionDAG &DAG) const {
1220 // Original encoding (either e32 or e64)
1221 int Opcode = Node->getMachineOpcode();
1222 const SIInstrInfo *TII =
1223 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1224 const MCInstrDesc *Desc = &TII->get(Opcode);
1226 unsigned NumDefs = Desc->getNumDefs();
1227 unsigned NumOps = Desc->getNumOperands();
1229 // Commuted opcode if available
1230 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1231 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
1233 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1234 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1236 // e64 version if available, -1 otherwise
1237 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1238 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
1239 int InputModifiers[3] = {0};
1241 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1243 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1244 bool HaveVSrc = false, HaveSSrc = false;
1246 // First figure out what we already have in this instruction.
1247 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1248 i != e && Op < NumOps; ++i, ++Op) {
1250 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1251 if (isVSrc(RegClass))
1253 else if (isSSrc(RegClass))
1258 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1259 if (Imm != -1 && Imm != 0) {
1260 // Literal immediate
1265 // If we neither have VSrc nor SSrc, it makes no sense to continue.
1266 if (!HaveVSrc && !HaveSSrc)
1269 // No scalar allowed when we have both VSrc and SSrc
1270 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1272 // Second go over the operands and try to fold them
1273 std::vector<SDValue> Ops;
1274 bool Promote2e64 = false;
1275 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1276 i != e && Op < NumOps; ++i, ++Op) {
1278 const SDValue &Operand = Node->getOperand(i);
1279 Ops.push_back(Operand);
1281 // Already folded immediate?
1282 if (isa<ConstantSDNode>(Operand.getNode()) ||
1283 isa<ConstantFPSDNode>(Operand.getNode()))
1286 // Is this a VSrc or SSrc operand?
1287 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1288 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1289 // Try to fold the immediates
1290 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1291 // Folding didn't work, make sure we don't hit the SReg limit.
1292 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1297 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1299 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1300 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1302 // Test if it makes sense to swap operands
1303 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1304 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1305 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1307 // Swap commutable operands
1308 std::swap(Ops[0], Ops[1]);
1320 // Test if it makes sense to switch to e64 encoding
1321 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1322 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1325 int32_t TmpImm = -1;
1326 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1327 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1328 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1330 // Switch to e64 encoding
1338 if (!DescE64 && !Promote2e64)
1340 if (!Operand.isMachineOpcode())
1342 if (Operand.getMachineOpcode() == AMDGPU::FNEG_SI) {
1344 Ops.push_back(Operand.getOperand(0));
1345 InputModifiers[i] = 1;
1352 else if (Operand.getMachineOpcode() == AMDGPU::FABS_SI) {
1354 Ops.push_back(Operand.getOperand(0));
1355 InputModifiers[i] = 2;
1365 std::vector<SDValue> OldOps(Ops);
1367 for (unsigned i = 0; i < OldOps.size(); ++i) {
1369 Ops.push_back(DAG.getTargetConstant(InputModifiers[i], MVT::i32));
1370 Ops.push_back(OldOps[i]);
1372 // Add the modifier flags while promoting
1373 for (unsigned i = 0; i < 2; ++i)
1374 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1377 // Add optional chain and glue
1378 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1379 Ops.push_back(Node->getOperand(i));
1381 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1382 // this case a brand new node is always be created, even if the operands
1383 // are the same as before. So, manually check if anything has been changed.
1384 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1388 // Create a complete new instruction
1389 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1392 /// \brief Helper function for adjustWritemask
1393 static unsigned SubIdx2Lane(unsigned Idx) {
1396 case AMDGPU::sub0: return 0;
1397 case AMDGPU::sub1: return 1;
1398 case AMDGPU::sub2: return 2;
1399 case AMDGPU::sub3: return 3;
1403 /// \brief Adjust the writemask of MIMG instructions
1404 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1405 SelectionDAG &DAG) const {
1406 SDNode *Users[4] = { };
1408 unsigned OldDmask = Node->getConstantOperandVal(0);
1409 unsigned NewDmask = 0;
1411 // Try to figure out the used register components
1412 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1415 // Abort if we can't understand the usage
1416 if (!I->isMachineOpcode() ||
1417 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1420 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1421 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1422 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1424 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1426 // Set which texture component corresponds to the lane.
1428 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1430 Comp = countTrailingZeros(Dmask);
1431 Dmask &= ~(1 << Comp);
1434 // Abort if we have more than one user per component
1439 NewDmask |= 1 << Comp;
1442 // Abort if there's no change
1443 if (NewDmask == OldDmask)
1446 // Adjust the writemask in the node
1447 std::vector<SDValue> Ops;
1448 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1449 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1450 Ops.push_back(Node->getOperand(i));
1451 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops);
1453 // If we only got one lane, replace it with a copy
1454 // (if NewDmask has only one bit set...)
1455 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1456 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1457 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1458 SDLoc(), Users[Lane]->getValueType(0),
1459 SDValue(Node, 0), RC);
1460 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1464 // Update the users of the node with the new indices
1465 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1467 SDNode *User = Users[i];
1471 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1472 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1476 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1477 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1478 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1483 /// \brief Fold the instructions after selecting them.
1484 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1485 SelectionDAG &DAG) const {
1486 const SIInstrInfo *TII =
1487 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1488 Node = AdjustRegClass(Node, DAG);
1490 if (TII->isMIMG(Node->getMachineOpcode()))
1491 adjustWritemask(Node, DAG);
1493 return foldOperands(Node, DAG);
1496 /// \brief Assign the register class depending on the number of
1497 /// bits set in the writemask
1498 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1499 SDNode *Node) const {
1500 const SIInstrInfo *TII =
1501 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1502 if (!TII->isMIMG(MI->getOpcode()))
1505 unsigned VReg = MI->getOperand(0).getReg();
1506 unsigned Writemask = MI->getOperand(1).getImm();
1507 unsigned BitsSet = 0;
1508 for (unsigned i = 0; i < 4; ++i)
1509 BitsSet += Writemask & (1 << i) ? 1 : 0;
1511 const TargetRegisterClass *RC;
1514 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1515 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1516 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1519 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1520 MI->setDesc(TII->get(NewOpcode));
1521 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1522 MRI.setRegClass(VReg, RC);
1525 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1526 SelectionDAG &DAG) const {
1529 unsigned NewOpcode = N->getMachineOpcode();
1531 switch (N->getMachineOpcode()) {
1533 case AMDGPU::S_LOAD_DWORD_IMM:
1534 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1536 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1537 if (NewOpcode == N->getMachineOpcode()) {
1538 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1541 case AMDGPU::S_LOAD_DWORDX4_IMM:
1542 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1543 if (NewOpcode == N->getMachineOpcode()) {
1544 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1546 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1549 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1551 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1552 DAG.getConstant(0, MVT::i64)), 0),
1554 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1556 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1561 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1562 const TargetRegisterClass *RC,
1563 unsigned Reg, EVT VT) const {
1564 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1566 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1567 cast<RegisterSDNode>(VReg)->getReg(), VT);