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::SReg_64RegClass);
33 addRegisterClass(MVT::i64, &AMDGPU::VSrc_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::VSrc_32RegClass);
41 addRegisterClass(MVT::f64, &AMDGPU::VSrc_64RegClass);
42 addRegisterClass(MVT::v2i32, &AMDGPU::VSrc_64RegClass);
43 addRegisterClass(MVT::v2f32, &AMDGPU::VSrc_64RegClass);
45 addRegisterClass(MVT::v4i32, &AMDGPU::VSrc_128RegClass);
46 addRegisterClass(MVT::v4f32, &AMDGPU::VSrc_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::i64, Custom);
103 setOperationAction(ISD::SELECT, MVT::f64, Promote);
104 AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
106 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
107 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
109 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
111 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
112 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
114 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
115 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
116 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
119 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
126 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
127 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
128 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
130 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
134 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
135 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
136 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
137 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
139 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
141 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
142 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
143 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
144 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
145 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
146 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
148 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
149 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
150 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
151 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
153 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
154 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
155 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
156 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
157 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
159 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
160 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
161 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
162 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
163 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
164 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
166 setOperationAction(ISD::LOAD, MVT::i1, Custom);
168 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
169 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
170 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
172 // We only support LOAD/STORE and vector manipulation ops for vectors
173 // with > 4 elements.
175 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
178 const size_t NumVecTypes = array_lengthof(VecTypes);
179 for (unsigned Type = 0; Type < NumVecTypes; ++Type) {
180 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
184 case ISD::BUILD_VECTOR:
186 case ISD::EXTRACT_VECTOR_ELT:
187 case ISD::INSERT_VECTOR_ELT:
188 case ISD::CONCAT_VECTORS:
189 case ISD::INSERT_SUBVECTOR:
190 case ISD::EXTRACT_SUBVECTOR:
193 setOperationAction(Op, VecTypes[Type], Expand);
199 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
200 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
201 setOperationAction(ISD::FTRUNC, VT, Expand);
202 setOperationAction(ISD::FCEIL, VT, Expand);
203 setOperationAction(ISD::FFLOOR, VT, Expand);
206 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
207 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
208 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
209 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
210 setOperationAction(ISD::FRINT, MVT::f64, Legal);
213 setTargetDAGCombine(ISD::SELECT_CC);
214 setTargetDAGCombine(ISD::SETCC);
216 setSchedulingPreference(Sched::RegPressure);
219 //===----------------------------------------------------------------------===//
220 // TargetLowering queries
221 //===----------------------------------------------------------------------===//
223 bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
225 bool *IsFast) const {
229 // XXX: This depends on the address space and also we may want to revist
230 // the alignment values we specify in the DataLayout.
232 // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
233 // which isn't a simple VT.
234 if (!VT.isSimple() || VT == MVT::Other)
237 // XXX - CI changes say "Support for unaligned memory accesses" but I don't
238 // see what for specifically. The wording everywhere else seems to be the
241 // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
242 // no alignment restrictions.
243 if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
244 // Using any pair of GPRs should be the same as any other pair.
247 return VT.bitsGE(MVT::i64);
250 // XXX - The only mention I see of this in the ISA manual is for LDS direct
251 // reads the "byte address and must be dword aligned". Is it also true for the
252 // normal loads and stores?
253 if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
256 // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
257 // byte-address are ignored, thus forcing Dword alignment.
260 return VT.bitsGT(MVT::i32);
263 bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
264 return VT.getScalarType().bitsLE(MVT::i16);
267 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
269 const SIInstrInfo *TII =
270 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
271 return TII->isInlineConstant(Imm);
274 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
275 SDLoc DL, SDValue Chain,
276 unsigned Offset, bool Signed) const {
277 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
278 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
279 AMDGPUAS::CONSTANT_ADDRESS);
280 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
281 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
282 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
283 DAG.getConstant(Offset, MVT::i64));
284 return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
285 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
286 false, false, MemVT.getSizeInBits() >> 3);
290 SDValue SITargetLowering::LowerFormalArguments(
292 CallingConv::ID CallConv,
294 const SmallVectorImpl<ISD::InputArg> &Ins,
295 SDLoc DL, SelectionDAG &DAG,
296 SmallVectorImpl<SDValue> &InVals) const {
298 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
300 MachineFunction &MF = DAG.getMachineFunction();
301 FunctionType *FType = MF.getFunction()->getFunctionType();
302 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
304 assert(CallConv == CallingConv::C);
306 SmallVector<ISD::InputArg, 16> Splits;
307 uint32_t Skipped = 0;
309 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
310 const ISD::InputArg &Arg = Ins[i];
312 // First check if it's a PS input addr
313 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
314 !Arg.Flags.isByVal()) {
316 assert((PSInputNum <= 15) && "Too many PS inputs!");
319 // We can savely skip PS inputs
325 Info->PSInputAddr |= 1 << PSInputNum++;
328 // Second split vertices into their elements
329 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
330 ISD::InputArg NewArg = Arg;
331 NewArg.Flags.setSplit();
332 NewArg.VT = Arg.VT.getVectorElementType();
334 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
335 // three or five element vertex only needs three or five registers,
336 // NOT four or eigth.
337 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
338 unsigned NumElements = ParamType->getVectorNumElements();
340 for (unsigned j = 0; j != NumElements; ++j) {
341 Splits.push_back(NewArg);
342 NewArg.PartOffset += NewArg.VT.getStoreSize();
345 } else if (Info->ShaderType != ShaderType::COMPUTE) {
346 Splits.push_back(Arg);
350 SmallVector<CCValAssign, 16> ArgLocs;
351 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
352 getTargetMachine(), ArgLocs, *DAG.getContext());
354 // At least one interpolation mode must be enabled or else the GPU will hang.
355 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
356 Info->PSInputAddr |= 1;
357 CCInfo.AllocateReg(AMDGPU::VGPR0);
358 CCInfo.AllocateReg(AMDGPU::VGPR1);
361 // The pointer to the list of arguments is stored in SGPR0, SGPR1
362 if (Info->ShaderType == ShaderType::COMPUTE) {
363 CCInfo.AllocateReg(AMDGPU::SGPR0);
364 CCInfo.AllocateReg(AMDGPU::SGPR1);
365 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
368 if (Info->ShaderType == ShaderType::COMPUTE) {
369 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
373 AnalyzeFormalArguments(CCInfo, Splits);
375 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
377 const ISD::InputArg &Arg = Ins[i];
378 if (Skipped & (1 << i)) {
379 InVals.push_back(DAG.getUNDEF(Arg.VT));
383 CCValAssign &VA = ArgLocs[ArgIdx++];
384 EVT VT = VA.getLocVT();
388 EVT MemVT = Splits[i].VT;
389 // The first 36 bytes of the input buffer contains information about
390 // thread group and global sizes.
391 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
392 36 + VA.getLocMemOffset(),
393 Ins[i].Flags.isSExt());
394 InVals.push_back(Arg);
397 assert(VA.isRegLoc() && "Parameter must be in a register!");
399 unsigned Reg = VA.getLocReg();
401 if (VT == MVT::i64) {
402 // For now assume it is a pointer
403 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
404 &AMDGPU::SReg_64RegClass);
405 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
406 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
410 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
412 Reg = MF.addLiveIn(Reg, RC);
413 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
415 if (Arg.VT.isVector()) {
417 // Build a vector from the registers
418 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
419 unsigned NumElements = ParamType->getVectorNumElements();
421 SmallVector<SDValue, 4> Regs;
423 for (unsigned j = 1; j != NumElements; ++j) {
424 Reg = ArgLocs[ArgIdx++].getLocReg();
425 Reg = MF.addLiveIn(Reg, RC);
426 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
429 // Fill up the missing vector elements
430 NumElements = Arg.VT.getVectorNumElements() - NumElements;
431 for (unsigned j = 0; j != NumElements; ++j)
432 Regs.push_back(DAG.getUNDEF(VT));
434 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT,
435 Regs.data(), Regs.size()));
439 InVals.push_back(Val);
444 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
445 MachineInstr * MI, MachineBasicBlock * BB) const {
447 MachineBasicBlock::iterator I = *MI;
449 switch (MI->getOpcode()) {
451 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
452 case AMDGPU::BRANCH: return BB;
453 case AMDGPU::SI_ADDR64_RSRC: {
454 const SIInstrInfo *TII =
455 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
456 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
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 const SIInstrInfo *TII =
483 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
484 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
485 MI->getOperand(0).getReg())
486 .addReg(MI->getOperand(1).getReg())
487 .addReg(MI->getOperand(2).getReg())
488 .addImm(0) /* src2 */
490 .addImm(0) /* CLAMP */
491 .addImm(0) /* OMOD */
492 .addImm(2); /* NEG */
493 MI->eraseFromParent();
496 case AMDGPU::SI_RegisterStorePseudo: {
497 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
498 const SIInstrInfo *TII =
499 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
500 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
501 MachineInstrBuilder MIB =
502 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
504 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
505 MIB.addOperand(MI->getOperand(i));
507 MI->eraseFromParent();
513 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
514 if (!VT.isVector()) {
517 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
520 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
524 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
525 VT = VT.getScalarType();
530 switch (VT.getSimpleVT().SimpleTy) {
532 return false; /* There is V_MAD_F32 for f32 */
542 //===----------------------------------------------------------------------===//
543 // Custom DAG Lowering Operations
544 //===----------------------------------------------------------------------===//
546 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
547 MachineFunction &MF = DAG.getMachineFunction();
548 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
549 switch (Op.getOpcode()) {
550 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
551 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
553 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
554 if (Op.getValueType().isVector() &&
555 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
556 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
557 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
558 Op.getValueType().getVectorNumElements() > 4))) {
559 SDValue MergedValues[2] = {
560 SplitVectorLoad(Op, DAG),
563 return DAG.getMergeValues(MergedValues, 2, SDLoc(Op));
565 return LowerLOAD(Op, DAG);
569 case ISD::SELECT: return LowerSELECT(Op, DAG);
570 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
571 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
572 case ISD::STORE: return LowerSTORE(Op, DAG);
573 case ISD::ANY_EXTEND: // Fall-through
574 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
575 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
576 case ISD::INTRINSIC_WO_CHAIN: {
577 unsigned IntrinsicID =
578 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
579 EVT VT = Op.getValueType();
581 //XXX: Hardcoded we only use two to store the pointer to the parameters.
582 unsigned NumUserSGPRs = 2;
583 switch (IntrinsicID) {
584 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
585 case Intrinsic::r600_read_ngroups_x:
586 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
587 case Intrinsic::r600_read_ngroups_y:
588 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
589 case Intrinsic::r600_read_ngroups_z:
590 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
591 case Intrinsic::r600_read_global_size_x:
592 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
593 case Intrinsic::r600_read_global_size_y:
594 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
595 case Intrinsic::r600_read_global_size_z:
596 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
597 case Intrinsic::r600_read_local_size_x:
598 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
599 case Intrinsic::r600_read_local_size_y:
600 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
601 case Intrinsic::r600_read_local_size_z:
602 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
603 case Intrinsic::r600_read_tgid_x:
604 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
605 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
606 case Intrinsic::r600_read_tgid_y:
607 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
608 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
609 case Intrinsic::r600_read_tgid_z:
610 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
611 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
612 case Intrinsic::r600_read_tidig_x:
613 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
615 case Intrinsic::r600_read_tidig_y:
616 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
618 case Intrinsic::r600_read_tidig_z:
619 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
621 case AMDGPUIntrinsic::SI_load_const: {
627 MachineMemOperand *MMO = MF.getMachineMemOperand(
628 MachinePointerInfo(),
629 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
630 VT.getSizeInBits() / 8, 4);
631 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
632 Op->getVTList(), Ops, 2, VT, MMO);
634 case AMDGPUIntrinsic::SI_sample:
635 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
636 case AMDGPUIntrinsic::SI_sampleb:
637 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
638 case AMDGPUIntrinsic::SI_sampled:
639 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
640 case AMDGPUIntrinsic::SI_samplel:
641 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
642 case AMDGPUIntrinsic::SI_vs_load_input:
643 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
650 case ISD::INTRINSIC_VOID:
651 SDValue Chain = Op.getOperand(0);
652 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
654 switch (IntrinsicID) {
655 case AMDGPUIntrinsic::SI_tbuffer_store: {
673 EVT VT = Op.getOperand(3).getValueType();
675 MachineMemOperand *MMO = MF.getMachineMemOperand(
676 MachinePointerInfo(),
677 MachineMemOperand::MOStore,
678 VT.getSizeInBits() / 8, 4);
679 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
680 Op->getVTList(), Ops,
681 sizeof(Ops)/sizeof(Ops[0]), VT, MMO);
690 /// \brief Helper function for LowerBRCOND
691 static SDNode *findUser(SDValue Value, unsigned Opcode) {
693 SDNode *Parent = Value.getNode();
694 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
697 if (I.getUse().get() != Value)
700 if (I->getOpcode() == Opcode)
706 /// This transforms the control flow intrinsics to get the branch destination as
707 /// last parameter, also switches branch target with BR if the need arise
708 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
709 SelectionDAG &DAG) const {
713 SDNode *Intr = BRCOND.getOperand(1).getNode();
714 SDValue Target = BRCOND.getOperand(2);
715 SDNode *BR = nullptr;
717 if (Intr->getOpcode() == ISD::SETCC) {
718 // As long as we negate the condition everything is fine
719 SDNode *SetCC = Intr;
720 assert(SetCC->getConstantOperandVal(1) == 1);
721 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
723 Intr = SetCC->getOperand(0).getNode();
726 // Get the target from BR if we don't negate the condition
727 BR = findUser(BRCOND, ISD::BR);
728 Target = BR->getOperand(1);
731 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
733 // Build the result and
734 SmallVector<EVT, 4> Res;
735 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
736 Res.push_back(Intr->getValueType(i));
738 // operands of the new intrinsic call
739 SmallVector<SDValue, 4> Ops;
740 Ops.push_back(BRCOND.getOperand(0));
741 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
742 Ops.push_back(Intr->getOperand(i));
743 Ops.push_back(Target);
745 // build the new intrinsic call
746 SDNode *Result = DAG.getNode(
747 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
748 DAG.getVTList(Res), Ops.data(), Ops.size()).getNode();
751 // Give the branch instruction our target
756 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
759 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
761 // Copy the intrinsic results to registers
762 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
763 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
767 Chain = DAG.getCopyToReg(
769 CopyToReg->getOperand(1),
770 SDValue(Result, i - 1),
773 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
776 // Remove the old intrinsic from the chain
777 DAG.ReplaceAllUsesOfValueWith(
778 SDValue(Intr, Intr->getNumValues() - 1),
779 Intr->getOperand(0));
784 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
786 LoadSDNode *Load = cast<LoadSDNode>(Op);
787 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
788 SDValue MergedValues[2];
789 MergedValues[1] = Load->getChain();
791 MergedValues[0] = Ret;
792 return DAG.getMergeValues(MergedValues, 2, DL);
795 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
799 EVT MemVT = Load->getMemoryVT();
801 assert(!MemVT.isVector() && "Private loads should be scalarized");
802 assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
804 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
805 DAG.getConstant(2, MVT::i32));
806 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
807 Load->getChain(), Ptr,
808 DAG.getTargetConstant(0, MVT::i32),
810 if (MemVT.getSizeInBits() == 64) {
811 SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
812 DAG.getConstant(1, MVT::i32));
814 SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
815 Load->getChain(), IncPtr,
816 DAG.getTargetConstant(0, MVT::i32),
819 Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
822 MergedValues[0] = Ret;
823 return DAG.getMergeValues(MergedValues, 2, DL);
827 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
829 SelectionDAG &DAG) const {
830 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
836 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
837 if (Op.getValueType() != MVT::i64)
841 SDValue Cond = Op.getOperand(0);
843 SDValue Zero = DAG.getConstant(0, MVT::i32);
844 SDValue One = DAG.getConstant(1, MVT::i32);
846 SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
847 SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
849 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
850 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
852 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
854 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
855 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
857 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
859 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
860 return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
863 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
864 SDValue LHS = Op.getOperand(0);
865 SDValue RHS = Op.getOperand(1);
866 SDValue True = Op.getOperand(2);
867 SDValue False = Op.getOperand(3);
868 SDValue CC = Op.getOperand(4);
869 EVT VT = Op.getValueType();
872 // Possible Min/Max pattern
873 SDValue MinMax = LowerMinMax(Op, DAG);
874 if (MinMax.getNode()) {
878 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
879 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
882 SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
883 SelectionDAG &DAG) const {
884 EVT VT = Op.getValueType();
887 if (VT != MVT::i64) {
891 SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
892 DAG.getConstant(31, MVT::i32));
894 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
897 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
899 StoreSDNode *Store = cast<StoreSDNode>(Op);
900 EVT VT = Store->getMemoryVT();
902 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
906 if (VT.isVector() && VT.getVectorNumElements() >= 8)
907 return SplitVectorStore(Op, DAG);
910 return DAG.getTruncStore(Store->getChain(), DL,
911 DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
912 Store->getBasePtr(), MVT::i1, Store->getMemOperand());
914 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
917 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
918 DAG.getConstant(2, MVT::i32));
919 SDValue Chain = Store->getChain();
920 SmallVector<SDValue, 8> Values;
922 if (Store->isTruncatingStore()) {
924 if (Store->getMemoryVT() == MVT::i8) {
926 } else if (Store->getMemoryVT() == MVT::i16) {
929 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
930 Chain, Store->getBasePtr(),
931 DAG.getConstant(0, MVT::i32));
932 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
933 DAG.getConstant(0x3, MVT::i32));
934 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
935 DAG.getConstant(3, MVT::i32));
936 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
937 DAG.getConstant(Mask, MVT::i32));
938 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
939 MaskedValue, ShiftAmt);
940 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
941 DAG.getConstant(32, MVT::i32), ShiftAmt);
942 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
943 DAG.getConstant(Mask, MVT::i32),
945 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
946 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
948 Values.push_back(Dst);
949 } else if (VT == MVT::i64) {
950 for (unsigned i = 0; i < 2; ++i) {
951 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
952 Store->getValue(), DAG.getConstant(i, MVT::i32)));
954 } else if (VT == MVT::i128) {
955 for (unsigned i = 0; i < 2; ++i) {
956 for (unsigned j = 0; j < 2; ++j) {
957 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
958 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
959 Store->getValue(), DAG.getConstant(i, MVT::i32)),
960 DAG.getConstant(j, MVT::i32)));
964 Values.push_back(Store->getValue());
967 for (unsigned i = 0; i < Values.size(); ++i) {
968 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
969 Ptr, DAG.getConstant(i, MVT::i32));
970 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
971 Chain, Values[i], PartPtr,
972 DAG.getTargetConstant(0, MVT::i32));
978 SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op,
979 SelectionDAG &DAG) const {
980 EVT VT = Op.getValueType();
983 if (VT != MVT::i64) {
987 SDValue Src = Op.getOperand(0);
988 if (Src.getValueType() != MVT::i32)
989 Src = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Src);
991 SDValue Zero = DAG.getConstant(0, MVT::i32);
992 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Src, Zero);
995 //===----------------------------------------------------------------------===//
996 // Custom DAG optimizations
997 //===----------------------------------------------------------------------===//
999 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
1000 DAGCombinerInfo &DCI) const {
1001 SelectionDAG &DAG = DCI.DAG;
1003 EVT VT = N->getValueType(0);
1005 switch (N->getOpcode()) {
1006 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1007 case ISD::SELECT_CC: {
1008 ConstantSDNode *True, *False;
1009 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1010 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1011 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1012 && True->isAllOnesValue()
1013 && False->isNullValue()
1015 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1016 N->getOperand(1), N->getOperand(4));
1022 SDValue Arg0 = N->getOperand(0);
1023 SDValue Arg1 = N->getOperand(1);
1024 SDValue CC = N->getOperand(2);
1025 ConstantSDNode * C = nullptr;
1026 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1028 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1030 && Arg0.getOpcode() == ISD::SIGN_EXTEND
1031 && Arg0.getOperand(0).getValueType() == MVT::i1
1032 && (C = dyn_cast<ConstantSDNode>(Arg1))
1034 && CCOp == ISD::SETNE) {
1035 return SimplifySetCC(VT, Arg0.getOperand(0),
1036 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1044 /// \brief Test if RegClass is one of the VSrc classes
1045 static bool isVSrc(unsigned RegClass) {
1046 return AMDGPU::VSrc_32RegClassID == RegClass ||
1047 AMDGPU::VSrc_64RegClassID == RegClass;
1050 /// \brief Test if RegClass is one of the SSrc classes
1051 static bool isSSrc(unsigned RegClass) {
1052 return AMDGPU::SSrc_32RegClassID == RegClass ||
1053 AMDGPU::SSrc_64RegClassID == RegClass;
1056 /// \brief Analyze the possible immediate value Op
1058 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1059 /// and the immediate value if it's a literal immediate
1060 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1067 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1068 if (Node->getZExtValue() >> 32) {
1071 Imm.I = Node->getSExtValue();
1072 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1073 if (N->getValueType(0) != MVT::f32)
1075 Imm.F = Node->getValueAPF().convertToFloat();
1077 return -1; // It isn't an immediate
1079 if ((Imm.I >= -16 && Imm.I <= 64) ||
1080 Imm.F == 0.5f || Imm.F == -0.5f ||
1081 Imm.F == 1.0f || Imm.F == -1.0f ||
1082 Imm.F == 2.0f || Imm.F == -2.0f ||
1083 Imm.F == 4.0f || Imm.F == -4.0f)
1084 return 0; // It's an inline immediate
1086 return Imm.I; // It's a literal immediate
1089 /// \brief Try to fold an immediate directly into an instruction
1090 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1091 bool &ScalarSlotUsed) const {
1093 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1094 const SIInstrInfo *TII =
1095 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1096 if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
1099 const SDValue &Op = Mov->getOperand(0);
1100 int32_t Value = analyzeImmediate(Op.getNode());
1102 // Not an immediate at all
1105 } else if (Value == 0) {
1106 // Inline immediates can always be fold
1110 } else if (Value == Immediate) {
1111 // Already fold literal immediate
1115 } else if (!ScalarSlotUsed && !Immediate) {
1116 // Fold this literal immediate
1117 ScalarSlotUsed = true;
1127 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1128 SelectionDAG &DAG, const SDValue &Op) const {
1129 const SIInstrInfo *TII =
1130 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1131 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1133 if (!Op->isMachineOpcode()) {
1134 switch(Op->getOpcode()) {
1135 case ISD::CopyFromReg: {
1136 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1137 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1138 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1139 return MRI.getRegClass(Reg);
1141 return TRI.getPhysRegClass(Reg);
1143 default: return nullptr;
1146 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1147 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1148 if (OpClassID != -1) {
1149 return TRI.getRegClass(OpClassID);
1151 switch(Op.getMachineOpcode()) {
1152 case AMDGPU::COPY_TO_REGCLASS:
1153 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1154 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1156 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1157 // class, then the register class for the value could be either a
1158 // VReg or and SReg. In order to get a more accurate
1159 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1160 OpClassID == AMDGPU::VSrc_64RegClassID) {
1161 return getRegClassForNode(DAG, Op.getOperand(0));
1163 return TRI.getRegClass(OpClassID);
1164 case AMDGPU::EXTRACT_SUBREG: {
1165 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1166 const TargetRegisterClass *SuperClass =
1167 getRegClassForNode(DAG, Op.getOperand(0));
1168 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1170 case AMDGPU::REG_SEQUENCE:
1171 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1172 return TRI.getRegClass(
1173 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1175 return getRegClassFor(Op.getSimpleValueType());
1179 /// \brief Does "Op" fit into register class "RegClass" ?
1180 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1181 unsigned RegClass) const {
1182 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1183 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1187 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1190 /// \brief Make sure that we don't exeed the number of allowed scalars
1191 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1193 bool &ScalarSlotUsed) const {
1195 // First map the operands register class to a destination class
1196 if (RegClass == AMDGPU::VSrc_32RegClassID)
1197 RegClass = AMDGPU::VReg_32RegClassID;
1198 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1199 RegClass = AMDGPU::VReg_64RegClassID;
1203 // Nothing to do if they fit naturally
1204 if (fitsRegClass(DAG, Operand, RegClass))
1207 // If the scalar slot isn't used yet use it now
1208 if (!ScalarSlotUsed) {
1209 ScalarSlotUsed = true;
1213 // This is a conservative aproach. It is possible that we can't determine the
1214 // correct register class and copy too often, but better safe than sorry.
1215 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1216 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1217 Operand.getValueType(), Operand, RC);
1218 Operand = SDValue(Node, 0);
1221 /// \returns true if \p Node's operands are different from the SDValue list
1223 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1224 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1225 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1232 /// \brief Try to fold the Nodes operands into the Node
1233 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1234 SelectionDAG &DAG) const {
1236 // Original encoding (either e32 or e64)
1237 int Opcode = Node->getMachineOpcode();
1238 const SIInstrInfo *TII =
1239 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1240 const MCInstrDesc *Desc = &TII->get(Opcode);
1242 unsigned NumDefs = Desc->getNumDefs();
1243 unsigned NumOps = Desc->getNumOperands();
1245 // Commuted opcode if available
1246 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1247 const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
1249 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1250 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1252 // e64 version if available, -1 otherwise
1253 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1254 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
1256 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1257 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
1259 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1260 bool HaveVSrc = false, HaveSSrc = false;
1262 // First figure out what we alread have in this instruction
1263 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1264 i != e && Op < NumOps; ++i, ++Op) {
1266 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1267 if (isVSrc(RegClass))
1269 else if (isSSrc(RegClass))
1274 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1275 if (Imm != -1 && Imm != 0) {
1276 // Literal immediate
1281 // If we neither have VSrc nor SSrc it makes no sense to continue
1282 if (!HaveVSrc && !HaveSSrc)
1285 // No scalar allowed when we have both VSrc and SSrc
1286 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1288 // Second go over the operands and try to fold them
1289 std::vector<SDValue> Ops;
1290 bool Promote2e64 = false;
1291 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1292 i != e && Op < NumOps; ++i, ++Op) {
1294 const SDValue &Operand = Node->getOperand(i);
1295 Ops.push_back(Operand);
1297 // Already folded immediate ?
1298 if (isa<ConstantSDNode>(Operand.getNode()) ||
1299 isa<ConstantFPSDNode>(Operand.getNode()))
1302 // Is this a VSrc or SSrc operand ?
1303 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1304 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1305 // Try to fold the immediates
1306 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1307 // Folding didn't worked, make sure we don't hit the SReg limit
1308 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1313 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1315 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1316 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1318 // Test if it makes sense to swap operands
1319 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1320 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1321 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1323 // Swap commutable operands
1324 std::swap(Ops[0], Ops[1]);
1332 if (DescE64 && !Immediate) {
1334 // Test if it makes sense to switch to e64 encoding
1335 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1336 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1339 int32_t TmpImm = -1;
1340 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1341 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1342 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1344 // Switch to e64 encoding
1354 // Add the modifier flags while promoting
1355 for (unsigned i = 0; i < 4; ++i)
1356 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1359 // Add optional chain and glue
1360 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1361 Ops.push_back(Node->getOperand(i));
1363 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1364 // this case a brand new node is always be created, even if the operands
1365 // are the same as before. So, manually check if anything has been changed.
1366 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1370 // Create a complete new instruction
1371 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1374 /// \brief Helper function for adjustWritemask
1375 static unsigned SubIdx2Lane(unsigned Idx) {
1378 case AMDGPU::sub0: return 0;
1379 case AMDGPU::sub1: return 1;
1380 case AMDGPU::sub2: return 2;
1381 case AMDGPU::sub3: return 3;
1385 /// \brief Adjust the writemask of MIMG instructions
1386 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1387 SelectionDAG &DAG) const {
1388 SDNode *Users[4] = { };
1390 unsigned OldDmask = Node->getConstantOperandVal(0);
1391 unsigned NewDmask = 0;
1393 // Try to figure out the used register components
1394 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1397 // Abort if we can't understand the usage
1398 if (!I->isMachineOpcode() ||
1399 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1402 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1403 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1404 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1406 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1408 // Set which texture component corresponds to the lane.
1410 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1412 Comp = countTrailingZeros(Dmask);
1413 Dmask &= ~(1 << Comp);
1416 // Abort if we have more than one user per component
1421 NewDmask |= 1 << Comp;
1424 // Abort if there's no change
1425 if (NewDmask == OldDmask)
1428 // Adjust the writemask in the node
1429 std::vector<SDValue> Ops;
1430 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1431 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1432 Ops.push_back(Node->getOperand(i));
1433 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
1435 // If we only got one lane, replace it with a copy
1436 // (if NewDmask has only one bit set...)
1437 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1438 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1439 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1440 SDLoc(), Users[Lane]->getValueType(0),
1441 SDValue(Node, 0), RC);
1442 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1446 // Update the users of the node with the new indices
1447 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1449 SDNode *User = Users[i];
1453 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1454 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1458 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1459 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1460 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1465 /// \brief Fold the instructions after slecting them
1466 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1467 SelectionDAG &DAG) const {
1468 const SIInstrInfo *TII =
1469 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1470 Node = AdjustRegClass(Node, DAG);
1472 if (TII->isMIMG(Node->getMachineOpcode()))
1473 adjustWritemask(Node, DAG);
1475 return foldOperands(Node, DAG);
1478 /// \brief Assign the register class depending on the number of
1479 /// bits set in the writemask
1480 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1481 SDNode *Node) const {
1482 const SIInstrInfo *TII =
1483 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1484 if (!TII->isMIMG(MI->getOpcode()))
1487 unsigned VReg = MI->getOperand(0).getReg();
1488 unsigned Writemask = MI->getOperand(1).getImm();
1489 unsigned BitsSet = 0;
1490 for (unsigned i = 0; i < 4; ++i)
1491 BitsSet += Writemask & (1 << i) ? 1 : 0;
1493 const TargetRegisterClass *RC;
1496 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1497 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1498 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1501 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1502 MI->setDesc(TII->get(NewOpcode));
1503 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1504 MRI.setRegClass(VReg, RC);
1507 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1508 SelectionDAG &DAG) const {
1511 unsigned NewOpcode = N->getMachineOpcode();
1513 switch (N->getMachineOpcode()) {
1515 case AMDGPU::S_LOAD_DWORD_IMM:
1516 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1518 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1519 if (NewOpcode == N->getMachineOpcode()) {
1520 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1523 case AMDGPU::S_LOAD_DWORDX4_IMM:
1524 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1525 if (NewOpcode == N->getMachineOpcode()) {
1526 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1528 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1531 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1533 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1534 DAG.getConstant(0, MVT::i64)), 0),
1536 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1538 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1543 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1544 const TargetRegisterClass *RC,
1545 unsigned Reg, EVT VT) const {
1546 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1548 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1549 cast<RegisterSDNode>(VReg)->getReg(), VT);