1 //===-- R600ISelLowering.cpp - R600 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 R600
13 //===----------------------------------------------------------------------===//
15 #include "R600ISelLowering.h"
16 #include "R600Defines.h"
17 #include "R600InstrInfo.h"
18 #include "R600MachineFunctionInfo.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Function.h"
28 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
29 AMDGPUTargetLowering(TM),
30 TII(static_cast<const R600InstrInfo*>(TM.getInstrInfo())) {
31 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
32 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
33 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
34 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
35 computeRegisterProperties();
37 setOperationAction(ISD::FADD, MVT::v4f32, Expand);
38 setOperationAction(ISD::FMUL, MVT::v4f32, Expand);
39 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
40 setOperationAction(ISD::FSUB, MVT::v4f32, Expand);
42 setOperationAction(ISD::ADD, MVT::v4i32, Expand);
43 setOperationAction(ISD::AND, MVT::v4i32, Expand);
44 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Expand);
45 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Expand);
46 setOperationAction(ISD::MUL, MVT::v2i32, Expand);
47 setOperationAction(ISD::MUL, MVT::v4i32, Expand);
48 setOperationAction(ISD::OR, MVT::v4i32, Expand);
49 setOperationAction(ISD::OR, MVT::v2i32, Expand);
50 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Expand);
51 setOperationAction(ISD::SHL, MVT::v4i32, Expand);
52 setOperationAction(ISD::SHL, MVT::v2i32, Expand);
53 setOperationAction(ISD::SRL, MVT::v4i32, Expand);
54 setOperationAction(ISD::SRL, MVT::v2i32, Expand);
55 setOperationAction(ISD::SRA, MVT::v4i32, Expand);
56 setOperationAction(ISD::SRA, MVT::v2i32, Expand);
57 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Expand);
58 setOperationAction(ISD::UDIV, MVT::v4i32, Expand);
59 setOperationAction(ISD::UREM, MVT::v4i32, Expand);
60 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
61 setOperationAction(ISD::XOR, MVT::v4i32, Expand);
62 setOperationAction(ISD::XOR, MVT::v2i32, Expand);
64 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
65 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
67 setOperationAction(ISD::FSUB, MVT::f32, Expand);
69 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
70 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
71 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
73 setOperationAction(ISD::ROTL, MVT::i32, Custom);
75 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
76 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
78 setOperationAction(ISD::SETCC, MVT::i32, Expand);
79 setOperationAction(ISD::SETCC, MVT::f32, Expand);
80 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
82 setOperationAction(ISD::SELECT, MVT::i32, Custom);
83 setOperationAction(ISD::SELECT, MVT::f32, Custom);
85 setOperationAction(ISD::VSELECT, MVT::v4i32, Expand);
86 setOperationAction(ISD::VSELECT, MVT::v2i32, Expand);
88 // Legalize loads and stores to the private address space.
89 setOperationAction(ISD::LOAD, MVT::i32, Custom);
90 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
91 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
92 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Custom);
93 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
94 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
95 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Custom);
96 setOperationAction(ISD::STORE, MVT::i8, Custom);
97 setOperationAction(ISD::STORE, MVT::i32, Custom);
98 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
99 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
101 setOperationAction(ISD::LOAD, MVT::i32, Custom);
102 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
103 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
105 setTargetDAGCombine(ISD::FP_ROUND);
106 setTargetDAGCombine(ISD::FP_TO_SINT);
107 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
108 setTargetDAGCombine(ISD::SELECT_CC);
110 setBooleanContents(ZeroOrNegativeOneBooleanContent);
111 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
112 setSchedulingPreference(Sched::VLIW);
115 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
116 MachineInstr * MI, MachineBasicBlock * BB) const {
117 MachineFunction * MF = BB->getParent();
118 MachineRegisterInfo &MRI = MF->getRegInfo();
119 MachineBasicBlock::iterator I = *MI;
121 switch (MI->getOpcode()) {
122 default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
123 case AMDGPU::CLAMP_R600: {
124 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
126 MI->getOperand(0).getReg(),
127 MI->getOperand(1).getReg());
128 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
132 case AMDGPU::FABS_R600: {
133 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
135 MI->getOperand(0).getReg(),
136 MI->getOperand(1).getReg());
137 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
141 case AMDGPU::FNEG_R600: {
142 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
144 MI->getOperand(0).getReg(),
145 MI->getOperand(1).getReg());
146 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
150 case AMDGPU::MASK_WRITE: {
151 unsigned maskedRegister = MI->getOperand(0).getReg();
152 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
153 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
154 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
158 case AMDGPU::MOV_IMM_F32:
159 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
160 MI->getOperand(1).getFPImm()->getValueAPF()
161 .bitcastToAPInt().getZExtValue());
163 case AMDGPU::MOV_IMM_I32:
164 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
165 MI->getOperand(1).getImm());
167 case AMDGPU::CONST_COPY: {
168 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
169 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
170 TII->setImmOperand(NewMI, R600Operands::SRC0_SEL,
171 MI->getOperand(1).getImm());
175 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
176 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
177 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
179 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
180 .addOperand(MI->getOperand(0))
181 .addOperand(MI->getOperand(1))
182 .addImm(EOP); // Set End of program bit
187 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
188 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
190 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
191 .addOperand(MI->getOperand(3))
192 .addOperand(MI->getOperand(4))
193 .addOperand(MI->getOperand(5))
194 .addOperand(MI->getOperand(6));
195 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
196 .addOperand(MI->getOperand(2))
197 .addOperand(MI->getOperand(4))
198 .addOperand(MI->getOperand(5))
199 .addOperand(MI->getOperand(6));
200 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
201 .addOperand(MI->getOperand(0))
202 .addOperand(MI->getOperand(1))
203 .addOperand(MI->getOperand(4))
204 .addOperand(MI->getOperand(5))
205 .addOperand(MI->getOperand(6))
206 .addReg(T0, RegState::Implicit)
207 .addReg(T1, RegState::Implicit);
211 case AMDGPU::TXD_SHADOW: {
212 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
213 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
215 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
216 .addOperand(MI->getOperand(3))
217 .addOperand(MI->getOperand(4))
218 .addOperand(MI->getOperand(5))
219 .addOperand(MI->getOperand(6));
220 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
221 .addOperand(MI->getOperand(2))
222 .addOperand(MI->getOperand(4))
223 .addOperand(MI->getOperand(5))
224 .addOperand(MI->getOperand(6));
225 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
226 .addOperand(MI->getOperand(0))
227 .addOperand(MI->getOperand(1))
228 .addOperand(MI->getOperand(4))
229 .addOperand(MI->getOperand(5))
230 .addOperand(MI->getOperand(6))
231 .addReg(T0, RegState::Implicit)
232 .addReg(T1, RegState::Implicit);
237 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
238 .addOperand(MI->getOperand(0));
241 case AMDGPU::BRANCH_COND_f32: {
242 MachineInstr *NewMI =
243 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
244 AMDGPU::PREDICATE_BIT)
245 .addOperand(MI->getOperand(1))
246 .addImm(OPCODE_IS_NOT_ZERO)
248 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
249 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
250 .addOperand(MI->getOperand(0))
251 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
255 case AMDGPU::BRANCH_COND_i32: {
256 MachineInstr *NewMI =
257 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
258 AMDGPU::PREDICATE_BIT)
259 .addOperand(MI->getOperand(1))
260 .addImm(OPCODE_IS_NOT_ZERO_INT)
262 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
263 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
264 .addOperand(MI->getOperand(0))
265 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
269 case AMDGPU::EG_ExportSwz:
270 case AMDGPU::R600_ExportSwz: {
271 // Instruction is left unmodified if its not the last one of its type
272 bool isLastInstructionOfItsType = true;
273 unsigned InstExportType = MI->getOperand(1).getImm();
274 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
275 EndBlock = BB->end(); NextExportInst != EndBlock;
276 NextExportInst = llvm::next(NextExportInst)) {
277 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
278 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
279 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
281 if (CurrentInstExportType == InstExportType) {
282 isLastInstructionOfItsType = false;
287 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
288 if (!EOP && !isLastInstructionOfItsType)
290 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
291 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
292 .addOperand(MI->getOperand(0))
293 .addOperand(MI->getOperand(1))
294 .addOperand(MI->getOperand(2))
295 .addOperand(MI->getOperand(3))
296 .addOperand(MI->getOperand(4))
297 .addOperand(MI->getOperand(5))
298 .addOperand(MI->getOperand(6))
303 case AMDGPU::RETURN: {
304 // RETURN instructions must have the live-out registers as implicit uses,
305 // otherwise they appear dead.
306 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
307 MachineInstrBuilder MIB(*MF, MI);
308 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
309 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
314 MI->eraseFromParent();
318 //===----------------------------------------------------------------------===//
319 // Custom DAG Lowering Operations
320 //===----------------------------------------------------------------------===//
322 using namespace llvm::Intrinsic;
323 using namespace llvm::AMDGPUIntrinsic;
325 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
326 switch (Op.getOpcode()) {
327 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
328 case ISD::ROTL: return LowerROTL(Op, DAG);
329 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
330 case ISD::SELECT: return LowerSELECT(Op, DAG);
331 case ISD::STORE: return LowerSTORE(Op, DAG);
332 case ISD::LOAD: return LowerLOAD(Op, DAG);
333 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
334 case ISD::INTRINSIC_VOID: {
335 SDValue Chain = Op.getOperand(0);
336 unsigned IntrinsicID =
337 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
338 switch (IntrinsicID) {
339 case AMDGPUIntrinsic::AMDGPU_store_output: {
340 MachineFunction &MF = DAG.getMachineFunction();
341 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
342 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
343 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
344 MFI->LiveOuts.push_back(Reg);
345 return DAG.getCopyToReg(Chain, Op.getDebugLoc(), Reg, Op.getOperand(2));
347 case AMDGPUIntrinsic::R600_store_swizzle: {
348 const SDValue Args[8] = {
350 Op.getOperand(2), // Export Value
351 Op.getOperand(3), // ArrayBase
352 Op.getOperand(4), // Type
353 DAG.getConstant(0, MVT::i32), // SWZ_X
354 DAG.getConstant(1, MVT::i32), // SWZ_Y
355 DAG.getConstant(2, MVT::i32), // SWZ_Z
356 DAG.getConstant(3, MVT::i32) // SWZ_W
358 return DAG.getNode(AMDGPUISD::EXPORT, Op.getDebugLoc(), Op.getValueType(),
362 // default for switch(IntrinsicID)
365 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
368 case ISD::INTRINSIC_WO_CHAIN: {
369 unsigned IntrinsicID =
370 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
371 EVT VT = Op.getValueType();
372 DebugLoc DL = Op.getDebugLoc();
373 switch(IntrinsicID) {
374 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
375 case AMDGPUIntrinsic::R600_load_input: {
376 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
377 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
378 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass, Reg, VT);
381 case AMDGPUIntrinsic::R600_interp_input: {
382 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
383 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
384 MachineSDNode *interp;
386 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
387 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
388 return DAG.getTargetExtractSubreg(
389 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
390 DL, MVT::f32, SDValue(interp, 0));
394 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
395 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
396 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
397 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
398 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
399 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
401 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
402 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
403 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
404 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
405 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
406 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
408 return SDValue(interp, slot % 2);
411 case r600_read_ngroups_x:
412 return LowerImplicitParameter(DAG, VT, DL, 0);
413 case r600_read_ngroups_y:
414 return LowerImplicitParameter(DAG, VT, DL, 1);
415 case r600_read_ngroups_z:
416 return LowerImplicitParameter(DAG, VT, DL, 2);
417 case r600_read_global_size_x:
418 return LowerImplicitParameter(DAG, VT, DL, 3);
419 case r600_read_global_size_y:
420 return LowerImplicitParameter(DAG, VT, DL, 4);
421 case r600_read_global_size_z:
422 return LowerImplicitParameter(DAG, VT, DL, 5);
423 case r600_read_local_size_x:
424 return LowerImplicitParameter(DAG, VT, DL, 6);
425 case r600_read_local_size_y:
426 return LowerImplicitParameter(DAG, VT, DL, 7);
427 case r600_read_local_size_z:
428 return LowerImplicitParameter(DAG, VT, DL, 8);
430 case r600_read_tgid_x:
431 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
433 case r600_read_tgid_y:
434 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
436 case r600_read_tgid_z:
437 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
439 case r600_read_tidig_x:
440 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
442 case r600_read_tidig_y:
443 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
445 case r600_read_tidig_z:
446 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
449 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
452 } // end switch(Op.getOpcode())
456 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
457 SmallVectorImpl<SDValue> &Results,
458 SelectionDAG &DAG) const {
459 switch (N->getOpcode()) {
461 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
464 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
465 Results.push_back(SDValue(Node, 0));
466 Results.push_back(SDValue(Node, 1));
467 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
469 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
473 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
474 Results.push_back(SDValue(Node, 0));
479 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
484 Op, DAG.getConstantFP(0.0f, MVT::f32),
485 DAG.getCondCode(ISD::SETNE)
489 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
491 unsigned DwordOffset) const {
492 unsigned ByteOffset = DwordOffset * 4;
493 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
494 AMDGPUAS::PARAM_I_ADDRESS);
496 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
497 assert(isInt<16>(ByteOffset));
499 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
500 DAG.getConstant(ByteOffset, MVT::i32), // PTR
501 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
502 false, false, false, 0);
505 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
507 MachineFunction &MF = DAG.getMachineFunction();
508 const AMDGPUFrameLowering *TFL =
509 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
511 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
514 unsigned FrameIndex = FIN->getIndex();
515 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
516 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
519 SDValue R600TargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
520 DebugLoc DL = Op.getDebugLoc();
521 EVT VT = Op.getValueType();
523 return DAG.getNode(AMDGPUISD::BITALIGN, DL, VT,
526 DAG.getNode(ISD::SUB, DL, VT,
527 DAG.getConstant(32, MVT::i32),
531 bool R600TargetLowering::isZero(SDValue Op) const {
532 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
533 return Cst->isNullValue();
534 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
535 return CstFP->isZero();
541 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
542 DebugLoc DL = Op.getDebugLoc();
543 EVT VT = Op.getValueType();
545 SDValue LHS = Op.getOperand(0);
546 SDValue RHS = Op.getOperand(1);
547 SDValue True = Op.getOperand(2);
548 SDValue False = Op.getOperand(3);
549 SDValue CC = Op.getOperand(4);
552 // LHS and RHS are guaranteed to be the same value type
553 EVT CompareVT = LHS.getValueType();
555 // Check if we can lower this to a native operation.
557 // Try to lower to a SET* instruction:
559 // SET* can match the following patterns:
561 // select_cc f32, f32, -1, 0, cc_any
562 // select_cc f32, f32, 1.0f, 0.0f, cc_any
563 // select_cc i32, i32, -1, 0, cc_any
566 // Move hardware True/False values to the correct operand.
567 if (isHWTrueValue(False) && isHWFalseValue(True)) {
568 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
569 std::swap(False, True);
570 CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
573 if (isHWTrueValue(True) && isHWFalseValue(False) &&
574 (CompareVT == VT || VT == MVT::i32)) {
575 // This can be matched by a SET* instruction.
576 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
579 // Try to lower to a CND* instruction:
581 // CND* can match the following patterns:
583 // select_cc f32, 0.0, f32, f32, cc_any
584 // select_cc f32, 0.0, i32, i32, cc_any
585 // select_cc i32, 0, f32, f32, cc_any
586 // select_cc i32, 0, i32, i32, cc_any
588 if (isZero(LHS) || isZero(RHS)) {
589 SDValue Cond = (isZero(LHS) ? RHS : LHS);
590 SDValue Zero = (isZero(LHS) ? LHS : RHS);
591 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
592 if (CompareVT != VT) {
593 // Bitcast True / False to the correct types. This will end up being
594 // a nop, but it allows us to define only a single pattern in the
595 // .TD files for each CND* instruction rather than having to have
596 // one pattern for integer True/False and one for fp True/False
597 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
598 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
601 CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
614 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
622 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
625 DAG.getCondCode(CCOpcode));
626 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
630 // Possible Min/Max pattern
631 SDValue MinMax = LowerMinMax(Op, DAG);
632 if (MinMax.getNode()) {
636 // If we make it this for it means we have no native instructions to handle
637 // this SELECT_CC, so we must lower it.
638 SDValue HWTrue, HWFalse;
640 if (CompareVT == MVT::f32) {
641 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
642 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
643 } else if (CompareVT == MVT::i32) {
644 HWTrue = DAG.getConstant(-1, CompareVT);
645 HWFalse = DAG.getConstant(0, CompareVT);
648 assert(!"Unhandled value type in LowerSELECT_CC");
651 // Lower this unsupported SELECT_CC into a combination of two supported
652 // SELECT_CC operations.
653 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
655 return DAG.getNode(ISD::SELECT_CC, DL, VT,
658 DAG.getCondCode(ISD::SETNE));
661 SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
662 return DAG.getNode(ISD::SELECT_CC,
666 DAG.getConstant(0, MVT::i32),
669 DAG.getCondCode(ISD::SETNE));
672 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
673 /// convert these pointers to a register index. Each register holds
674 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
675 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
676 /// for indirect addressing.
677 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
679 SelectionDAG &DAG) const {
691 default: llvm_unreachable("Invalid stack width");
694 return DAG.getNode(ISD::SRL, Ptr.getDebugLoc(), Ptr.getValueType(), Ptr,
695 DAG.getConstant(SRLPad, MVT::i32));
698 void R600TargetLowering::getStackAddress(unsigned StackWidth,
701 unsigned &PtrIncr) const {
702 switch (StackWidth) {
713 Channel = ElemIdx % 2;
727 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
728 DebugLoc DL = Op.getDebugLoc();
729 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
730 SDValue Chain = Op.getOperand(0);
731 SDValue Value = Op.getOperand(1);
732 SDValue Ptr = Op.getOperand(2);
734 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
735 Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
736 // Convert pointer from byte address to dword address.
737 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
738 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
739 Ptr, DAG.getConstant(2, MVT::i32)));
741 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
742 assert(!"Truncated and indexed stores not supported yet");
744 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
749 EVT ValueVT = Value.getValueType();
751 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
755 // Lowering for indirect addressing
757 const MachineFunction &MF = DAG.getMachineFunction();
758 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
759 getTargetMachine().getFrameLowering());
760 unsigned StackWidth = TFL->getStackWidth(MF);
762 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
764 if (ValueVT.isVector()) {
765 unsigned NumElemVT = ValueVT.getVectorNumElements();
766 EVT ElemVT = ValueVT.getVectorElementType();
769 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
770 "vector width in load");
772 for (unsigned i = 0; i < NumElemVT; ++i) {
773 unsigned Channel, PtrIncr;
774 getStackAddress(StackWidth, i, Channel, PtrIncr);
775 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
776 DAG.getConstant(PtrIncr, MVT::i32));
777 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
778 Value, DAG.getConstant(i, MVT::i32));
780 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
782 DAG.getTargetConstant(Channel, MVT::i32));
784 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
786 if (ValueVT == MVT::i8) {
787 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
789 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
790 DAG.getTargetConstant(0, MVT::i32)); // Channel
796 // return (512 + (kc_bank << 12)
798 ConstantAddressBlock(unsigned AddressSpace) {
799 switch (AddressSpace) {
800 case AMDGPUAS::CONSTANT_BUFFER_0:
802 case AMDGPUAS::CONSTANT_BUFFER_1:
804 case AMDGPUAS::CONSTANT_BUFFER_2:
805 return 512 + 4096 * 2;
806 case AMDGPUAS::CONSTANT_BUFFER_3:
807 return 512 + 4096 * 3;
808 case AMDGPUAS::CONSTANT_BUFFER_4:
809 return 512 + 4096 * 4;
810 case AMDGPUAS::CONSTANT_BUFFER_5:
811 return 512 + 4096 * 5;
812 case AMDGPUAS::CONSTANT_BUFFER_6:
813 return 512 + 4096 * 6;
814 case AMDGPUAS::CONSTANT_BUFFER_7:
815 return 512 + 4096 * 7;
816 case AMDGPUAS::CONSTANT_BUFFER_8:
817 return 512 + 4096 * 8;
818 case AMDGPUAS::CONSTANT_BUFFER_9:
819 return 512 + 4096 * 9;
820 case AMDGPUAS::CONSTANT_BUFFER_10:
821 return 512 + 4096 * 10;
822 case AMDGPUAS::CONSTANT_BUFFER_11:
823 return 512 + 4096 * 11;
824 case AMDGPUAS::CONSTANT_BUFFER_12:
825 return 512 + 4096 * 12;
826 case AMDGPUAS::CONSTANT_BUFFER_13:
827 return 512 + 4096 * 13;
828 case AMDGPUAS::CONSTANT_BUFFER_14:
829 return 512 + 4096 * 14;
830 case AMDGPUAS::CONSTANT_BUFFER_15:
831 return 512 + 4096 * 15;
837 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
839 EVT VT = Op.getValueType();
840 DebugLoc DL = Op.getDebugLoc();
841 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
842 SDValue Chain = Op.getOperand(0);
843 SDValue Ptr = Op.getOperand(1);
846 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
847 if (ConstantBlock > -1) {
849 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
850 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
851 dyn_cast<ConstantSDNode>(Ptr)) {
853 for (unsigned i = 0; i < 4; i++) {
854 // We want Const position encoded with the following formula :
855 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
856 // const_index is Ptr computed by llvm using an alignment of 16.
857 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
858 // then div by 4 at the ISel step
859 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
860 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
861 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
863 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
865 // non constant ptr cant be folded, keeps it as a v4f32 load
866 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
867 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
868 DAG.getConstant(LoadNode->getAddressSpace() -
869 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
873 if (!VT.isVector()) {
874 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
875 DAG.getConstant(0, MVT::i32));
878 SDValue MergedValues[2] = {
882 return DAG.getMergeValues(MergedValues, 2, DL);
885 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
889 // Lowering for indirect addressing
890 const MachineFunction &MF = DAG.getMachineFunction();
891 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
892 getTargetMachine().getFrameLowering());
893 unsigned StackWidth = TFL->getStackWidth(MF);
895 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
898 unsigned NumElemVT = VT.getVectorNumElements();
899 EVT ElemVT = VT.getVectorElementType();
902 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
903 "vector width in load");
905 for (unsigned i = 0; i < NumElemVT; ++i) {
906 unsigned Channel, PtrIncr;
907 getStackAddress(StackWidth, i, Channel, PtrIncr);
908 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
909 DAG.getConstant(PtrIncr, MVT::i32));
910 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
912 DAG.getTargetConstant(Channel, MVT::i32),
915 for (unsigned i = NumElemVT; i < 4; ++i) {
916 Loads[i] = DAG.getUNDEF(ElemVT);
918 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
919 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
921 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
923 DAG.getTargetConstant(0, MVT::i32), // Channel
928 Ops[0] = LoweredLoad;
931 return DAG.getMergeValues(Ops, 2, DL);
934 /// XXX Only kernel functions are supported, so we can assume for now that
935 /// every function is a kernel function, but in the future we should use
936 /// separate calling conventions for kernel and non-kernel functions.
937 SDValue R600TargetLowering::LowerFormalArguments(
939 CallingConv::ID CallConv,
941 const SmallVectorImpl<ISD::InputArg> &Ins,
942 DebugLoc DL, SelectionDAG &DAG,
943 SmallVectorImpl<SDValue> &InVals) const {
944 unsigned ParamOffsetBytes = 36;
945 Function::const_arg_iterator FuncArg =
946 DAG.getMachineFunction().getFunction()->arg_begin();
947 for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
949 Type *ArgType = FuncArg->getType();
950 unsigned ArgSizeInBits = ArgType->isPointerTy() ?
951 32 : ArgType->getPrimitiveSizeInBits();
952 unsigned ArgBytes = ArgSizeInBits >> 3;
954 if (ArgSizeInBits < VT.getSizeInBits()) {
955 assert(!ArgType->isFloatTy() &&
956 "Extending floating point arguments not supported yet");
957 ArgVT = MVT::getIntegerVT(ArgSizeInBits);
961 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
962 AMDGPUAS::PARAM_I_ADDRESS);
963 SDValue Arg = DAG.getExtLoad(ISD::ZEXTLOAD, DL, VT, DAG.getRoot(),
964 DAG.getConstant(ParamOffsetBytes, MVT::i32),
965 MachinePointerInfo(UndefValue::get(PtrTy)),
966 ArgVT, false, false, ArgBytes);
967 InVals.push_back(Arg);
968 ParamOffsetBytes += ArgBytes;
973 EVT R600TargetLowering::getSetCCResultType(EVT VT) const {
974 if (!VT.isVector()) return MVT::i32;
975 return VT.changeVectorElementTypeToInteger();
978 //===----------------------------------------------------------------------===//
979 // Custom DAG Optimizations
980 //===----------------------------------------------------------------------===//
982 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
983 DAGCombinerInfo &DCI) const {
984 SelectionDAG &DAG = DCI.DAG;
986 switch (N->getOpcode()) {
987 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
988 case ISD::FP_ROUND: {
989 SDValue Arg = N->getOperand(0);
990 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
991 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), N->getValueType(0),
997 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
998 // (i32 select_cc f32, f32, -1, 0 cc)
1000 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1001 // this to one of the SET*_DX10 instructions.
1002 case ISD::FP_TO_SINT: {
1003 SDValue FNeg = N->getOperand(0);
1004 if (FNeg.getOpcode() != ISD::FNEG) {
1007 SDValue SelectCC = FNeg.getOperand(0);
1008 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1009 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1010 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1011 !isHWTrueValue(SelectCC.getOperand(2)) ||
1012 !isHWFalseValue(SelectCC.getOperand(3))) {
1016 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N->getValueType(0),
1017 SelectCC.getOperand(0), // LHS
1018 SelectCC.getOperand(1), // RHS
1019 DAG.getConstant(-1, MVT::i32), // True
1020 DAG.getConstant(0, MVT::i32), // Flase
1021 SelectCC.getOperand(4)); // CC
1025 // Extract_vec (Build_vector) generated by custom lowering
1026 // also needs to be customly combined
1027 case ISD::EXTRACT_VECTOR_ELT: {
1028 SDValue Arg = N->getOperand(0);
1029 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1030 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1031 unsigned Element = Const->getZExtValue();
1032 return Arg->getOperand(Element);
1035 if (Arg.getOpcode() == ISD::BITCAST &&
1036 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1037 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1038 unsigned Element = Const->getZExtValue();
1039 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getVTList(),
1040 Arg->getOperand(0).getOperand(Element));
1045 case ISD::SELECT_CC: {
1046 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1047 // selectcc x, y, a, b, inv(cc)
1049 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1050 // selectcc x, y, a, b, cc
1051 SDValue LHS = N->getOperand(0);
1052 if (LHS.getOpcode() != ISD::SELECT_CC) {
1056 SDValue RHS = N->getOperand(1);
1057 SDValue True = N->getOperand(2);
1058 SDValue False = N->getOperand(3);
1059 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1061 if (LHS.getOperand(2).getNode() != True.getNode() ||
1062 LHS.getOperand(3).getNode() != False.getNode() ||
1063 RHS.getNode() != False.getNode()) {
1068 default: return SDValue();
1069 case ISD::SETNE: return LHS;
1071 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1072 LHSCC = ISD::getSetCCInverse(LHSCC,
1073 LHS.getOperand(0).getValueType().isInteger());
1074 return DAG.getSelectCC(N->getDebugLoc(),
1083 case AMDGPUISD::EXPORT: {
1084 SDValue Arg = N->getOperand(1);
1085 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1087 SDValue NewBldVec[4] = {
1088 DAG.getUNDEF(MVT::f32),
1089 DAG.getUNDEF(MVT::f32),
1090 DAG.getUNDEF(MVT::f32),
1091 DAG.getUNDEF(MVT::f32)
1093 SDValue NewArgs[8] = {
1094 N->getOperand(0), // Chain
1096 N->getOperand(2), // ArrayBase
1097 N->getOperand(3), // Type
1098 N->getOperand(4), // SWZ_X
1099 N->getOperand(5), // SWZ_Y
1100 N->getOperand(6), // SWZ_Z
1101 N->getOperand(7) // SWZ_W
1103 for (unsigned i = 0; i < Arg.getNumOperands(); i++) {
1104 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg.getOperand(i))) {
1106 NewArgs[4 + i] = DAG.getConstant(4, MVT::i32); // SEL_0
1107 } else if (C->isExactlyValue(1.0)) {
1108 NewArgs[4 + i] = DAG.getConstant(5, MVT::i32); // SEL_0
1110 NewBldVec[i] = Arg.getOperand(i);
1113 NewBldVec[i] = Arg.getOperand(i);
1116 DebugLoc DL = N->getDebugLoc();
1117 NewArgs[1] = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4f32, NewBldVec, 4);
1118 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);