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"
28 const uint64_t RSRC_DATA_FORMAT = 0xf00000000000LL;
32 SITargetLowering::SITargetLowering(TargetMachine &TM) :
33 AMDGPUTargetLowering(TM) {
34 addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
35 addRegisterClass(MVT::i64, &AMDGPU::VSrc_64RegClass);
37 addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
38 addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
40 addRegisterClass(MVT::i32, &AMDGPU::VSrc_32RegClass);
41 addRegisterClass(MVT::f32, &AMDGPU::VSrc_32RegClass);
43 addRegisterClass(MVT::f64, &AMDGPU::VSrc_64RegClass);
44 addRegisterClass(MVT::v2i32, &AMDGPU::VSrc_64RegClass);
45 addRegisterClass(MVT::v2f32, &AMDGPU::VSrc_64RegClass);
47 addRegisterClass(MVT::v4i32, &AMDGPU::VReg_128RegClass);
48 addRegisterClass(MVT::v4f32, &AMDGPU::VReg_128RegClass);
49 addRegisterClass(MVT::i128, &AMDGPU::SReg_128RegClass);
51 addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
52 addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
54 addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
55 addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
57 computeRegisterProperties();
60 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
61 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
62 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
63 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
64 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
65 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
67 setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
68 setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
69 setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
70 setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
71 setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
72 setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
74 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
75 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
76 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
77 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
79 setOperationAction(ISD::ADD, MVT::i32, Legal);
80 setOperationAction(ISD::ADDC, MVT::i32, Legal);
81 setOperationAction(ISD::ADDE, MVT::i32, Legal);
83 setOperationAction(ISD::BITCAST, MVT::i128, Legal);
85 // We need to custom lower vector stores from local memory
86 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
87 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
88 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
89 setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
91 setOperationAction(ISD::STORE, MVT::v8i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v16i32, Custom);
94 // We need to custom lower loads/stores from private memory
95 setOperationAction(ISD::LOAD, MVT::i32, Custom);
96 setOperationAction(ISD::LOAD, MVT::i64, Custom);
97 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
98 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
99 setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
101 setOperationAction(ISD::STORE, MVT::i32, Custom);
102 setOperationAction(ISD::STORE, MVT::i64, Custom);
103 setOperationAction(ISD::STORE, MVT::i128, Custom);
104 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
105 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
107 setOperationAction(ISD::SELECT, MVT::i64, Custom);
109 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
110 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
112 setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
114 setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
115 setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
117 setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
118 setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
119 setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
121 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
122 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
123 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
124 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
126 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
128 setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
129 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
130 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
131 setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
132 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
133 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
134 setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
135 setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
137 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
138 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
139 setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
140 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
141 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
142 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
143 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
144 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
145 setTruncStoreAction(MVT::i128, MVT::i64, Expand);
146 setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
147 setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
149 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
150 setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
151 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
153 // We only support LOAD/STORE and vector manipulation ops for vectors
154 // with > 4 elements.
156 MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
159 const size_t NumVecTypes = array_lengthof(VecTypes);
160 for (unsigned Type = 0; Type < NumVecTypes; ++Type) {
161 for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
165 case ISD::BUILD_VECTOR:
167 case ISD::EXTRACT_VECTOR_ELT:
168 case ISD::INSERT_VECTOR_ELT:
169 case ISD::CONCAT_VECTORS:
170 case ISD::INSERT_SUBVECTOR:
171 case ISD::EXTRACT_SUBVECTOR:
174 setOperationAction(Op, VecTypes[Type], Expand);
180 for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
181 MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
182 setOperationAction(ISD::FTRUNC, VT, Expand);
183 setOperationAction(ISD::FCEIL, VT, Expand);
184 setOperationAction(ISD::FFLOOR, VT, Expand);
187 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
188 setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
189 setOperationAction(ISD::FCEIL, MVT::f64, Legal);
190 setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
193 setTargetDAGCombine(ISD::SELECT_CC);
194 setTargetDAGCombine(ISD::SETCC);
196 setSchedulingPreference(Sched::RegPressure);
199 //===----------------------------------------------------------------------===//
200 // TargetLowering queries
201 //===----------------------------------------------------------------------===//
203 bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
205 bool *IsFast) const {
206 // XXX: This depends on the address space and also we may want to revist
207 // the alignment values we specify in the DataLayout.
208 if (!VT.isSimple() || VT == MVT::Other)
210 return VT.bitsGT(MVT::i32);
213 bool SITargetLowering::shouldSplitVectorElementType(EVT VT) const {
214 return VT.bitsLE(MVT::i16);
217 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
218 SDLoc DL, SDValue Chain,
219 unsigned Offset) const {
220 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
221 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
222 AMDGPUAS::CONSTANT_ADDRESS);
223 SDValue BasePtr = DAG.getCopyFromReg(Chain, DL,
224 MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
225 SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
226 DAG.getConstant(Offset, MVT::i64));
227 return DAG.getExtLoad(ISD::SEXTLOAD, DL, VT, Chain, Ptr,
228 MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
229 false, false, MemVT.getSizeInBits() >> 3);
233 SDValue SITargetLowering::LowerFormalArguments(
235 CallingConv::ID CallConv,
237 const SmallVectorImpl<ISD::InputArg> &Ins,
238 SDLoc DL, SelectionDAG &DAG,
239 SmallVectorImpl<SDValue> &InVals) const {
241 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
243 MachineFunction &MF = DAG.getMachineFunction();
244 FunctionType *FType = MF.getFunction()->getFunctionType();
245 SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
247 assert(CallConv == CallingConv::C);
249 SmallVector<ISD::InputArg, 16> Splits;
250 uint32_t Skipped = 0;
252 for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
253 const ISD::InputArg &Arg = Ins[i];
255 // First check if it's a PS input addr
256 if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
257 !Arg.Flags.isByVal()) {
259 assert((PSInputNum <= 15) && "Too many PS inputs!");
262 // We can savely skip PS inputs
268 Info->PSInputAddr |= 1 << PSInputNum++;
271 // Second split vertices into their elements
272 if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
273 ISD::InputArg NewArg = Arg;
274 NewArg.Flags.setSplit();
275 NewArg.VT = Arg.VT.getVectorElementType();
277 // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
278 // three or five element vertex only needs three or five registers,
279 // NOT four or eigth.
280 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
281 unsigned NumElements = ParamType->getVectorNumElements();
283 for (unsigned j = 0; j != NumElements; ++j) {
284 Splits.push_back(NewArg);
285 NewArg.PartOffset += NewArg.VT.getStoreSize();
288 } else if (Info->ShaderType != ShaderType::COMPUTE) {
289 Splits.push_back(Arg);
293 SmallVector<CCValAssign, 16> ArgLocs;
294 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
295 getTargetMachine(), ArgLocs, *DAG.getContext());
297 // At least one interpolation mode must be enabled or else the GPU will hang.
298 if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
299 Info->PSInputAddr |= 1;
300 CCInfo.AllocateReg(AMDGPU::VGPR0);
301 CCInfo.AllocateReg(AMDGPU::VGPR1);
304 // The pointer to the list of arguments is stored in SGPR0, SGPR1
305 if (Info->ShaderType == ShaderType::COMPUTE) {
306 CCInfo.AllocateReg(AMDGPU::SGPR0);
307 CCInfo.AllocateReg(AMDGPU::SGPR1);
308 MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
311 if (Info->ShaderType == ShaderType::COMPUTE) {
312 getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
316 AnalyzeFormalArguments(CCInfo, Splits);
318 for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
320 const ISD::InputArg &Arg = Ins[i];
321 if (Skipped & (1 << i)) {
322 InVals.push_back(DAG.getUNDEF(Arg.VT));
326 CCValAssign &VA = ArgLocs[ArgIdx++];
327 EVT VT = VA.getLocVT();
331 EVT MemVT = Splits[i].VT;
332 // The first 36 bytes of the input buffer contains information about
333 // thread group and global sizes.
334 SDValue Arg = LowerParameter(DAG, VT, MemVT, DL, DAG.getRoot(),
335 36 + VA.getLocMemOffset());
336 InVals.push_back(Arg);
339 assert(VA.isRegLoc() && "Parameter must be in a register!");
341 unsigned Reg = VA.getLocReg();
343 if (VT == MVT::i64) {
344 // For now assume it is a pointer
345 Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
346 &AMDGPU::SReg_64RegClass);
347 Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
348 InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
352 const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
354 Reg = MF.addLiveIn(Reg, RC);
355 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
357 if (Arg.VT.isVector()) {
359 // Build a vector from the registers
360 Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
361 unsigned NumElements = ParamType->getVectorNumElements();
363 SmallVector<SDValue, 4> Regs;
365 for (unsigned j = 1; j != NumElements; ++j) {
366 Reg = ArgLocs[ArgIdx++].getLocReg();
367 Reg = MF.addLiveIn(Reg, RC);
368 Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
371 // Fill up the missing vector elements
372 NumElements = Arg.VT.getVectorNumElements() - NumElements;
373 for (unsigned j = 0; j != NumElements; ++j)
374 Regs.push_back(DAG.getUNDEF(VT));
376 InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT,
377 Regs.data(), Regs.size()));
381 InVals.push_back(Val);
386 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
387 MachineInstr * MI, MachineBasicBlock * BB) const {
389 MachineBasicBlock::iterator I = *MI;
391 switch (MI->getOpcode()) {
393 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
394 case AMDGPU::BRANCH: return BB;
395 case AMDGPU::SI_ADDR64_RSRC: {
396 const SIInstrInfo *TII =
397 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
398 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
399 unsigned SuperReg = MI->getOperand(0).getReg();
400 unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
401 unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
402 unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
403 unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SReg_32RegClass);
404 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
405 .addOperand(MI->getOperand(1));
406 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
408 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
409 .addImm(RSRC_DATA_FORMAT >> 32);
410 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
412 .addImm(AMDGPU::sub0)
414 .addImm(AMDGPU::sub1);
415 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
417 .addImm(AMDGPU::sub0_sub1)
419 .addImm(AMDGPU::sub2_sub3);
420 MI->eraseFromParent();
423 case AMDGPU::V_SUB_F64: {
424 const SIInstrInfo *TII =
425 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
426 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
427 MI->getOperand(0).getReg())
428 .addReg(MI->getOperand(1).getReg())
429 .addReg(MI->getOperand(2).getReg())
430 .addImm(0) /* src2 */
432 .addImm(0) /* CLAMP */
433 .addImm(0) /* OMOD */
434 .addImm(2); /* NEG */
435 MI->eraseFromParent();
438 case AMDGPU::SI_RegisterStorePseudo: {
439 MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
440 const SIInstrInfo *TII =
441 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
442 unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
443 MachineInstrBuilder MIB =
444 BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
446 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
447 MIB.addOperand(MI->getOperand(i));
449 MI->eraseFromParent();
455 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
456 if (!VT.isVector()) {
459 return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
462 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
466 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
467 VT = VT.getScalarType();
472 switch (VT.getSimpleVT().SimpleTy) {
474 return false; /* There is V_MAD_F32 for f32 */
484 //===----------------------------------------------------------------------===//
485 // Custom DAG Lowering Operations
486 //===----------------------------------------------------------------------===//
488 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
489 MachineFunction &MF = DAG.getMachineFunction();
490 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
491 switch (Op.getOpcode()) {
492 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
493 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
495 LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
496 if (Op.getValueType().isVector() &&
497 (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
498 Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
499 (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
500 Op.getValueType().getVectorNumElements() > 4))) {
501 SDValue MergedValues[2] = {
502 SplitVectorLoad(Op, DAG),
505 return DAG.getMergeValues(MergedValues, 2, SDLoc(Op));
507 return LowerLOAD(Op, DAG);
511 case ISD::SELECT: return LowerSELECT(Op, DAG);
512 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
513 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
514 case ISD::STORE: return LowerSTORE(Op, DAG);
515 case ISD::ANY_EXTEND: // Fall-through
516 case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
517 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
518 case ISD::INTRINSIC_WO_CHAIN: {
519 unsigned IntrinsicID =
520 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
521 EVT VT = Op.getValueType();
523 //XXX: Hardcoded we only use two to store the pointer to the parameters.
524 unsigned NumUserSGPRs = 2;
525 switch (IntrinsicID) {
526 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
527 case Intrinsic::r600_read_ngroups_x:
528 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0);
529 case Intrinsic::r600_read_ngroups_y:
530 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4);
531 case Intrinsic::r600_read_ngroups_z:
532 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8);
533 case Intrinsic::r600_read_global_size_x:
534 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12);
535 case Intrinsic::r600_read_global_size_y:
536 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16);
537 case Intrinsic::r600_read_global_size_z:
538 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20);
539 case Intrinsic::r600_read_local_size_x:
540 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24);
541 case Intrinsic::r600_read_local_size_y:
542 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28);
543 case Intrinsic::r600_read_local_size_z:
544 return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32);
545 case Intrinsic::r600_read_tgid_x:
546 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
547 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
548 case Intrinsic::r600_read_tgid_y:
549 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
550 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
551 case Intrinsic::r600_read_tgid_z:
552 return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
553 AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
554 case Intrinsic::r600_read_tidig_x:
555 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
557 case Intrinsic::r600_read_tidig_y:
558 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
560 case Intrinsic::r600_read_tidig_z:
561 return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
563 case AMDGPUIntrinsic::SI_load_const: {
565 ResourceDescriptorToi128(Op.getOperand(1), DAG),
569 MachineMemOperand *MMO = MF.getMachineMemOperand(
570 MachinePointerInfo(),
571 MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
572 VT.getSizeInBits() / 8, 4);
573 return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
574 Op->getVTList(), Ops, 2, VT, MMO);
576 case AMDGPUIntrinsic::SI_sample:
577 return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
578 case AMDGPUIntrinsic::SI_sampleb:
579 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
580 case AMDGPUIntrinsic::SI_sampled:
581 return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
582 case AMDGPUIntrinsic::SI_samplel:
583 return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
584 case AMDGPUIntrinsic::SI_vs_load_input:
585 return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
586 ResourceDescriptorToi128(Op.getOperand(1), DAG),
592 case ISD::INTRINSIC_VOID:
593 SDValue Chain = Op.getOperand(0);
594 unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
596 switch (IntrinsicID) {
597 case AMDGPUIntrinsic::SI_tbuffer_store: {
601 ResourceDescriptorToi128(Op.getOperand(2), DAG),
615 EVT VT = Op.getOperand(3).getValueType();
617 MachineMemOperand *MMO = MF.getMachineMemOperand(
618 MachinePointerInfo(),
619 MachineMemOperand::MOStore,
620 VT.getSizeInBits() / 8, 4);
621 return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
622 Op->getVTList(), Ops,
623 sizeof(Ops)/sizeof(Ops[0]), VT, MMO);
632 /// \brief Helper function for LowerBRCOND
633 static SDNode *findUser(SDValue Value, unsigned Opcode) {
635 SDNode *Parent = Value.getNode();
636 for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
639 if (I.getUse().get() != Value)
642 if (I->getOpcode() == Opcode)
648 /// This transforms the control flow intrinsics to get the branch destination as
649 /// last parameter, also switches branch target with BR if the need arise
650 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
651 SelectionDAG &DAG) const {
655 SDNode *Intr = BRCOND.getOperand(1).getNode();
656 SDValue Target = BRCOND.getOperand(2);
659 if (Intr->getOpcode() == ISD::SETCC) {
660 // As long as we negate the condition everything is fine
661 SDNode *SetCC = Intr;
662 assert(SetCC->getConstantOperandVal(1) == 1);
663 assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
665 Intr = SetCC->getOperand(0).getNode();
668 // Get the target from BR if we don't negate the condition
669 BR = findUser(BRCOND, ISD::BR);
670 Target = BR->getOperand(1);
673 assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
675 // Build the result and
676 SmallVector<EVT, 4> Res;
677 for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
678 Res.push_back(Intr->getValueType(i));
680 // operands of the new intrinsic call
681 SmallVector<SDValue, 4> Ops;
682 Ops.push_back(BRCOND.getOperand(0));
683 for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
684 Ops.push_back(Intr->getOperand(i));
685 Ops.push_back(Target);
687 // build the new intrinsic call
688 SDNode *Result = DAG.getNode(
689 Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
690 DAG.getVTList(Res.data(), Res.size()), Ops.data(), Ops.size()).getNode();
693 // Give the branch instruction our target
698 DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
701 SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
703 // Copy the intrinsic results to registers
704 for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
705 SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
709 Chain = DAG.getCopyToReg(
711 CopyToReg->getOperand(1),
712 SDValue(Result, i - 1),
715 DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
718 // Remove the old intrinsic from the chain
719 DAG.ReplaceAllUsesOfValueWith(
720 SDValue(Intr, Intr->getNumValues() - 1),
721 Intr->getOperand(0));
726 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
728 LoadSDNode *Load = cast<LoadSDNode>(Op);
729 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
730 SDValue MergedValues[2];
731 MergedValues[1] = Load->getChain();
733 MergedValues[0] = Ret;
734 return DAG.getMergeValues(MergedValues, 2, DL);
737 if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
741 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
742 DAG.getConstant(2, MVT::i32));
743 Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
744 Load->getChain(), Ptr,
745 DAG.getTargetConstant(0, MVT::i32),
748 MergedValues[0] = Ret;
749 return DAG.getMergeValues(MergedValues, 2, DL);
753 SDValue SITargetLowering::ResourceDescriptorToi128(SDValue Op,
754 SelectionDAG &DAG) const {
756 if (Op.getValueType() == MVT::i128) {
760 assert(Op.getOpcode() == ISD::UNDEF);
762 return DAG.getNode(ISD::BUILD_PAIR, SDLoc(Op), MVT::i128,
763 DAG.getConstant(0, MVT::i64),
764 DAG.getConstant(0, MVT::i64));
767 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
769 SelectionDAG &DAG) const {
770 return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
772 ResourceDescriptorToi128(Op.getOperand(3), DAG),
776 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
777 if (Op.getValueType() != MVT::i64)
781 SDValue Cond = Op.getOperand(0);
782 SDValue LHS = Op.getOperand(1);
783 SDValue RHS = Op.getOperand(2);
785 SDValue Zero = DAG.getConstant(0, MVT::i32);
786 SDValue One = DAG.getConstant(1, MVT::i32);
788 SDValue Lo0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS, Zero);
789 SDValue Lo1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS, Zero);
791 SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
793 SDValue Hi0 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, LHS, One);
794 SDValue Hi1 = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, RHS, One);
796 SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
798 return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Lo, Hi);
801 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
802 SDValue LHS = Op.getOperand(0);
803 SDValue RHS = Op.getOperand(1);
804 SDValue True = Op.getOperand(2);
805 SDValue False = Op.getOperand(3);
806 SDValue CC = Op.getOperand(4);
807 EVT VT = Op.getValueType();
810 // Possible Min/Max pattern
811 SDValue MinMax = LowerMinMax(Op, DAG);
812 if (MinMax.getNode()) {
816 SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
817 return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
820 SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
821 SelectionDAG &DAG) const {
822 EVT VT = Op.getValueType();
825 if (VT != MVT::i64) {
829 SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
830 DAG.getConstant(31, MVT::i32));
832 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
835 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
837 StoreSDNode *Store = cast<StoreSDNode>(Op);
838 EVT VT = Store->getMemoryVT();
840 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
844 if (VT.isVector() && VT.getVectorNumElements() >= 8)
845 return SplitVectorStore(Op, DAG);
847 if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
850 SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
851 DAG.getConstant(2, MVT::i32));
852 SDValue Chain = Store->getChain();
853 SmallVector<SDValue, 8> Values;
855 if (Store->isTruncatingStore()) {
857 if (Store->getMemoryVT() == MVT::i8) {
859 } else if (Store->getMemoryVT() == MVT::i16) {
862 SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
863 Chain, Store->getBasePtr(),
864 DAG.getConstant(0, MVT::i32));
865 SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
866 DAG.getConstant(0x3, MVT::i32));
867 SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
868 DAG.getConstant(3, MVT::i32));
869 SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
870 DAG.getConstant(Mask, MVT::i32));
871 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
872 MaskedValue, ShiftAmt);
873 SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
874 DAG.getConstant(32, MVT::i32), ShiftAmt);
875 SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
876 DAG.getConstant(Mask, MVT::i32),
878 Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
879 Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
881 Values.push_back(Dst);
882 } else if (VT == MVT::i64) {
883 for (unsigned i = 0; i < 2; ++i) {
884 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
885 Store->getValue(), DAG.getConstant(i, MVT::i32)));
887 } else if (VT == MVT::i128) {
888 for (unsigned i = 0; i < 2; ++i) {
889 for (unsigned j = 0; j < 2; ++j) {
890 Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
891 DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
892 Store->getValue(), DAG.getConstant(i, MVT::i32)),
893 DAG.getConstant(j, MVT::i32)));
897 Values.push_back(Store->getValue());
900 for (unsigned i = 0; i < Values.size(); ++i) {
901 SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
902 Ptr, DAG.getConstant(i, MVT::i32));
903 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
904 Chain, Values[i], PartPtr,
905 DAG.getTargetConstant(0, MVT::i32));
911 SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op,
912 SelectionDAG &DAG) const {
913 EVT VT = Op.getValueType();
916 if (VT != MVT::i64) {
920 return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0),
921 DAG.getConstant(0, MVT::i32));
924 //===----------------------------------------------------------------------===//
925 // Custom DAG optimizations
926 //===----------------------------------------------------------------------===//
928 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
929 DAGCombinerInfo &DCI) const {
930 SelectionDAG &DAG = DCI.DAG;
932 EVT VT = N->getValueType(0);
934 switch (N->getOpcode()) {
936 case ISD::SELECT_CC: {
937 ConstantSDNode *True, *False;
938 // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
939 if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
940 && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
941 && True->isAllOnesValue()
942 && False->isNullValue()
944 return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
945 N->getOperand(1), N->getOperand(4));
951 SDValue Arg0 = N->getOperand(0);
952 SDValue Arg1 = N->getOperand(1);
953 SDValue CC = N->getOperand(2);
954 ConstantSDNode * C = NULL;
955 ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
957 // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
959 && Arg0.getOpcode() == ISD::SIGN_EXTEND
960 && Arg0.getOperand(0).getValueType() == MVT::i1
961 && (C = dyn_cast<ConstantSDNode>(Arg1))
963 && CCOp == ISD::SETNE) {
964 return SimplifySetCC(VT, Arg0.getOperand(0),
965 DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
973 /// \brief Test if RegClass is one of the VSrc classes
974 static bool isVSrc(unsigned RegClass) {
975 return AMDGPU::VSrc_32RegClassID == RegClass ||
976 AMDGPU::VSrc_64RegClassID == RegClass;
979 /// \brief Test if RegClass is one of the SSrc classes
980 static bool isSSrc(unsigned RegClass) {
981 return AMDGPU::SSrc_32RegClassID == RegClass ||
982 AMDGPU::SSrc_64RegClassID == RegClass;
985 /// \brief Analyze the possible immediate value Op
987 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
988 /// and the immediate value if it's a literal immediate
989 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
996 if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
997 if (Node->getZExtValue() >> 32) {
1000 Imm.I = Node->getSExtValue();
1001 } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N))
1002 Imm.F = Node->getValueAPF().convertToFloat();
1004 return -1; // It isn't an immediate
1006 if ((Imm.I >= -16 && Imm.I <= 64) ||
1007 Imm.F == 0.5f || Imm.F == -0.5f ||
1008 Imm.F == 1.0f || Imm.F == -1.0f ||
1009 Imm.F == 2.0f || Imm.F == -2.0f ||
1010 Imm.F == 4.0f || Imm.F == -4.0f)
1011 return 0; // It's an inline immediate
1013 return Imm.I; // It's a literal immediate
1016 /// \brief Try to fold an immediate directly into an instruction
1017 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1018 bool &ScalarSlotUsed) const {
1020 MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1021 const SIInstrInfo *TII =
1022 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1023 if (Mov == 0 || !TII->isMov(Mov->getMachineOpcode()))
1026 const SDValue &Op = Mov->getOperand(0);
1027 int32_t Value = analyzeImmediate(Op.getNode());
1029 // Not an immediate at all
1032 } else if (Value == 0) {
1033 // Inline immediates can always be fold
1037 } else if (Value == Immediate) {
1038 // Already fold literal immediate
1042 } else if (!ScalarSlotUsed && !Immediate) {
1043 // Fold this literal immediate
1044 ScalarSlotUsed = true;
1054 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1055 SelectionDAG &DAG, const SDValue &Op) const {
1056 const SIInstrInfo *TII =
1057 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1058 const SIRegisterInfo &TRI = TII->getRegisterInfo();
1060 if (!Op->isMachineOpcode()) {
1061 switch(Op->getOpcode()) {
1062 case ISD::CopyFromReg: {
1063 MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1064 unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1065 if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1066 return MRI.getRegClass(Reg);
1068 return TRI.getPhysRegClass(Reg);
1070 default: return NULL;
1073 const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1074 int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1075 if (OpClassID != -1) {
1076 return TRI.getRegClass(OpClassID);
1078 switch(Op.getMachineOpcode()) {
1079 case AMDGPU::COPY_TO_REGCLASS:
1080 // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1081 OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1083 // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1084 // class, then the register class for the value could be either a
1085 // VReg or and SReg. In order to get a more accurate
1086 if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1087 OpClassID == AMDGPU::VSrc_64RegClassID) {
1088 return getRegClassForNode(DAG, Op.getOperand(0));
1090 return TRI.getRegClass(OpClassID);
1091 case AMDGPU::EXTRACT_SUBREG: {
1092 int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1093 const TargetRegisterClass *SuperClass =
1094 getRegClassForNode(DAG, Op.getOperand(0));
1095 return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1097 case AMDGPU::REG_SEQUENCE:
1098 // Operand 0 is the register class id for REG_SEQUENCE instructions.
1099 return TRI.getRegClass(
1100 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1102 return getRegClassFor(Op.getSimpleValueType());
1106 /// \brief Does "Op" fit into register class "RegClass" ?
1107 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1108 unsigned RegClass) const {
1109 const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1110 const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1114 return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1117 /// \brief Make sure that we don't exeed the number of allowed scalars
1118 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1120 bool &ScalarSlotUsed) const {
1122 // First map the operands register class to a destination class
1123 if (RegClass == AMDGPU::VSrc_32RegClassID)
1124 RegClass = AMDGPU::VReg_32RegClassID;
1125 else if (RegClass == AMDGPU::VSrc_64RegClassID)
1126 RegClass = AMDGPU::VReg_64RegClassID;
1130 // Nothing to do if they fit naturally
1131 if (fitsRegClass(DAG, Operand, RegClass))
1134 // If the scalar slot isn't used yet use it now
1135 if (!ScalarSlotUsed) {
1136 ScalarSlotUsed = true;
1140 // This is a conservative aproach. It is possible that we can't determine the
1141 // correct register class and copy too often, but better safe than sorry.
1142 SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1143 SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1144 Operand.getValueType(), Operand, RC);
1145 Operand = SDValue(Node, 0);
1148 /// \returns true if \p Node's operands are different from the SDValue list
1150 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1151 for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1152 if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1159 /// \brief Try to fold the Nodes operands into the Node
1160 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1161 SelectionDAG &DAG) const {
1163 // Original encoding (either e32 or e64)
1164 int Opcode = Node->getMachineOpcode();
1165 const SIInstrInfo *TII =
1166 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1167 const MCInstrDesc *Desc = &TII->get(Opcode);
1169 unsigned NumDefs = Desc->getNumDefs();
1170 unsigned NumOps = Desc->getNumOperands();
1172 // Commuted opcode if available
1173 int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1174 const MCInstrDesc *DescRev = OpcodeRev == -1 ? 0 : &TII->get(OpcodeRev);
1176 assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1177 assert(!DescRev || DescRev->getNumOperands() == NumOps);
1179 // e64 version if available, -1 otherwise
1180 int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1181 const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? 0 : &TII->get(OpcodeE64);
1183 assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1184 assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
1186 int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1187 bool HaveVSrc = false, HaveSSrc = false;
1189 // First figure out what we alread have in this instruction
1190 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1191 i != e && Op < NumOps; ++i, ++Op) {
1193 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1194 if (isVSrc(RegClass))
1196 else if (isSSrc(RegClass))
1201 int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1202 if (Imm != -1 && Imm != 0) {
1203 // Literal immediate
1208 // If we neither have VSrc nor SSrc it makes no sense to continue
1209 if (!HaveVSrc && !HaveSSrc)
1212 // No scalar allowed when we have both VSrc and SSrc
1213 bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1215 // Second go over the operands and try to fold them
1216 std::vector<SDValue> Ops;
1217 bool Promote2e64 = false;
1218 for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1219 i != e && Op < NumOps; ++i, ++Op) {
1221 const SDValue &Operand = Node->getOperand(i);
1222 Ops.push_back(Operand);
1224 // Already folded immediate ?
1225 if (isa<ConstantSDNode>(Operand.getNode()) ||
1226 isa<ConstantFPSDNode>(Operand.getNode()))
1229 // Is this a VSrc or SSrc operand ?
1230 unsigned RegClass = Desc->OpInfo[Op].RegClass;
1231 if (isVSrc(RegClass) || isSSrc(RegClass)) {
1232 // Try to fold the immediates
1233 if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1234 // Folding didn't worked, make sure we don't hit the SReg limit
1235 ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1240 if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1242 unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1243 assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1245 // Test if it makes sense to swap operands
1246 if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1247 (!fitsRegClass(DAG, Ops[1], RegClass) &&
1248 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1250 // Swap commutable operands
1251 SDValue Tmp = Ops[1];
1261 if (DescE64 && !Immediate) {
1263 // Test if it makes sense to switch to e64 encoding
1264 unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1265 if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1268 int32_t TmpImm = -1;
1269 if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1270 (!fitsRegClass(DAG, Ops[i], RegClass) &&
1271 fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1273 // Switch to e64 encoding
1283 // Add the modifier flags while promoting
1284 for (unsigned i = 0; i < 4; ++i)
1285 Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1288 // Add optional chain and glue
1289 for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1290 Ops.push_back(Node->getOperand(i));
1292 // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1293 // this case a brand new node is always be created, even if the operands
1294 // are the same as before. So, manually check if anything has been changed.
1295 if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1299 // Create a complete new instruction
1300 return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1303 /// \brief Helper function for adjustWritemask
1304 static unsigned SubIdx2Lane(unsigned Idx) {
1307 case AMDGPU::sub0: return 0;
1308 case AMDGPU::sub1: return 1;
1309 case AMDGPU::sub2: return 2;
1310 case AMDGPU::sub3: return 3;
1314 /// \brief Adjust the writemask of MIMG instructions
1315 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1316 SelectionDAG &DAG) const {
1317 SDNode *Users[4] = { };
1319 unsigned OldDmask = Node->getConstantOperandVal(0);
1320 unsigned NewDmask = 0;
1322 // Try to figure out the used register components
1323 for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1326 // Abort if we can't understand the usage
1327 if (!I->isMachineOpcode() ||
1328 I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1331 // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1332 // Note that subregs are packed, i.e. Lane==0 is the first bit set
1333 // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1335 Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1337 // Set which texture component corresponds to the lane.
1339 for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1341 Comp = countTrailingZeros(Dmask);
1342 Dmask &= ~(1 << Comp);
1345 // Abort if we have more than one user per component
1350 NewDmask |= 1 << Comp;
1353 // Abort if there's no change
1354 if (NewDmask == OldDmask)
1357 // Adjust the writemask in the node
1358 std::vector<SDValue> Ops;
1359 Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1360 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1361 Ops.push_back(Node->getOperand(i));
1362 Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
1364 // If we only got one lane, replace it with a copy
1365 // (if NewDmask has only one bit set...)
1366 if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1367 SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1368 SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1369 SDLoc(), Users[Lane]->getValueType(0),
1370 SDValue(Node, 0), RC);
1371 DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1375 // Update the users of the node with the new indices
1376 for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1378 SDNode *User = Users[i];
1382 SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1383 DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1387 case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1388 case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1389 case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1394 /// \brief Fold the instructions after slecting them
1395 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1396 SelectionDAG &DAG) const {
1397 const SIInstrInfo *TII =
1398 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1399 Node = AdjustRegClass(Node, DAG);
1401 if (TII->isMIMG(Node->getMachineOpcode()))
1402 adjustWritemask(Node, DAG);
1404 return foldOperands(Node, DAG);
1407 /// \brief Assign the register class depending on the number of
1408 /// bits set in the writemask
1409 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1410 SDNode *Node) const {
1411 const SIInstrInfo *TII =
1412 static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1413 if (!TII->isMIMG(MI->getOpcode()))
1416 unsigned VReg = MI->getOperand(0).getReg();
1417 unsigned Writemask = MI->getOperand(1).getImm();
1418 unsigned BitsSet = 0;
1419 for (unsigned i = 0; i < 4; ++i)
1420 BitsSet += Writemask & (1 << i) ? 1 : 0;
1422 const TargetRegisterClass *RC;
1425 case 1: RC = &AMDGPU::VReg_32RegClass; break;
1426 case 2: RC = &AMDGPU::VReg_64RegClass; break;
1427 case 3: RC = &AMDGPU::VReg_96RegClass; break;
1430 unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1431 MI->setDesc(TII->get(NewOpcode));
1432 MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1433 MRI.setRegClass(VReg, RC);
1436 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1437 SelectionDAG &DAG) const {
1440 unsigned NewOpcode = N->getMachineOpcode();
1442 switch (N->getMachineOpcode()) {
1444 case AMDGPU::S_LOAD_DWORD_IMM:
1445 NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1447 case AMDGPU::S_LOAD_DWORDX2_SGPR:
1448 if (NewOpcode == N->getMachineOpcode()) {
1449 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1452 case AMDGPU::S_LOAD_DWORDX4_IMM:
1453 case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1454 if (NewOpcode == N->getMachineOpcode()) {
1455 NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1457 if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1460 ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1462 SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1463 DAG.getConstant(0, MVT::i64)), 0),
1465 DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1467 return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1472 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1473 const TargetRegisterClass *RC,
1474 unsigned Reg, EVT VT) const {
1475 SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1477 return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1478 cast<RegisterSDNode>(VReg)->getReg(), VT);