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 "AMDGPUFrameLowering.h"
17 #include "AMDGPUIntrinsicInfo.h"
18 #include "AMDGPUSubtarget.h"
19 #include "R600Defines.h"
20 #include "R600InstrInfo.h"
21 #include "R600MachineFunctionInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Function.h"
32 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
33 AMDGPUTargetLowering(TM),
34 Gen(TM.getSubtarget<AMDGPUSubtarget>().getGeneration()) {
35 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
36 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
37 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
38 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
39 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
40 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
42 computeRegisterProperties();
44 // Set condition code actions
45 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
46 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
47 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
49 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
50 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
51 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
52 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
53 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
54 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
55 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
56 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
58 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
59 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
60 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
61 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
63 setOperationAction(ISD::FCOS, MVT::f32, Custom);
64 setOperationAction(ISD::FSIN, MVT::f32, Custom);
66 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
67 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
69 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
70 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
71 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
73 setOperationAction(ISD::FSUB, MVT::f32, Expand);
75 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
76 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
77 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
79 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
80 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
82 setOperationAction(ISD::SETCC, MVT::i32, Expand);
83 setOperationAction(ISD::SETCC, MVT::f32, Expand);
84 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
85 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
86 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
88 setOperationAction(ISD::SELECT, MVT::i32, Expand);
89 setOperationAction(ISD::SELECT, MVT::f32, Expand);
90 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
91 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
93 // Expand sign extension of vectors
94 if (!Subtarget->hasBFE())
95 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
97 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
98 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
100 if (!Subtarget->hasBFE())
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
105 if (!Subtarget->hasBFE())
106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
110 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
114 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
117 // Legalize loads and stores to the private address space.
118 setOperationAction(ISD::LOAD, MVT::i32, Custom);
119 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
120 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
122 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
123 // spaces, so it is custom lowered to handle those where it isn't.
124 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
125 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
126 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
127 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
128 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
129 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
131 setOperationAction(ISD::STORE, MVT::i8, Custom);
132 setOperationAction(ISD::STORE, MVT::i32, Custom);
133 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
134 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
135 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
136 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
138 setOperationAction(ISD::LOAD, MVT::i32, Custom);
139 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
140 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
142 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
143 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
144 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
145 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
147 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
148 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
149 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
150 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
152 setTargetDAGCombine(ISD::FP_ROUND);
153 setTargetDAGCombine(ISD::FP_TO_SINT);
154 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
155 setTargetDAGCombine(ISD::SELECT_CC);
156 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
158 setOperationAction(ISD::SUB, MVT::i64, Expand);
160 // These should be replaced by UDVIREM, but it does not happen automatically
161 // during Type Legalization
162 setOperationAction(ISD::UDIV, MVT::i64, Custom);
163 setOperationAction(ISD::UREM, MVT::i64, Custom);
164 setOperationAction(ISD::SDIV, MVT::i64, Custom);
165 setOperationAction(ISD::SREM, MVT::i64, Custom);
167 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
168 // to be Legal/Custom in order to avoid library calls.
169 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
170 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
171 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
173 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
175 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
176 for (MVT VT : ScalarIntVTs) {
177 setOperationAction(ISD::ADDC, VT, Expand);
178 setOperationAction(ISD::SUBC, VT, Expand);
179 setOperationAction(ISD::ADDE, VT, Expand);
180 setOperationAction(ISD::SUBE, VT, Expand);
183 setBooleanContents(ZeroOrNegativeOneBooleanContent);
184 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
185 setSchedulingPreference(Sched::Source);
188 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
189 MachineInstr * MI, MachineBasicBlock * BB) const {
190 MachineFunction * MF = BB->getParent();
191 MachineRegisterInfo &MRI = MF->getRegInfo();
192 MachineBasicBlock::iterator I = *MI;
193 const R600InstrInfo *TII =
194 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
196 switch (MI->getOpcode()) {
198 // Replace LDS_*_RET instruction that don't have any uses with the
199 // equivalent LDS_*_NORET instruction.
200 if (TII->isLDSRetInstr(MI->getOpcode())) {
201 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
202 assert(DstIdx != -1);
203 MachineInstrBuilder NewMI;
204 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()))
207 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
208 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
209 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
210 NewMI.addOperand(MI->getOperand(i));
213 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
216 case AMDGPU::CLAMP_R600: {
217 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
219 MI->getOperand(0).getReg(),
220 MI->getOperand(1).getReg());
221 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
225 case AMDGPU::FABS_R600: {
226 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
228 MI->getOperand(0).getReg(),
229 MI->getOperand(1).getReg());
230 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
234 case AMDGPU::FNEG_R600: {
235 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
237 MI->getOperand(0).getReg(),
238 MI->getOperand(1).getReg());
239 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
243 case AMDGPU::MASK_WRITE: {
244 unsigned maskedRegister = MI->getOperand(0).getReg();
245 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
246 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
247 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
251 case AMDGPU::MOV_IMM_F32:
252 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
253 MI->getOperand(1).getFPImm()->getValueAPF()
254 .bitcastToAPInt().getZExtValue());
256 case AMDGPU::MOV_IMM_I32:
257 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
258 MI->getOperand(1).getImm());
260 case AMDGPU::CONST_COPY: {
261 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
262 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
263 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
264 MI->getOperand(1).getImm());
268 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
269 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
270 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
271 unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
273 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
274 .addOperand(MI->getOperand(0))
275 .addOperand(MI->getOperand(1))
276 .addImm(EOP); // Set End of program bit
281 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
282 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
283 MachineOperand &RID = MI->getOperand(4);
284 MachineOperand &SID = MI->getOperand(5);
285 unsigned TextureId = MI->getOperand(6).getImm();
286 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
287 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
299 case 8: // ShadowRect
310 case 11: // Shadow1DArray
314 case 12: // Shadow2DArray
318 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
319 .addOperand(MI->getOperand(3))
337 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
338 .addOperand(MI->getOperand(2))
356 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
357 .addOperand(MI->getOperand(0))
358 .addOperand(MI->getOperand(1))
376 .addReg(T0, RegState::Implicit)
377 .addReg(T1, RegState::Implicit);
381 case AMDGPU::TXD_SHADOW: {
382 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
383 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
384 MachineOperand &RID = MI->getOperand(4);
385 MachineOperand &SID = MI->getOperand(5);
386 unsigned TextureId = MI->getOperand(6).getImm();
387 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
388 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
400 case 8: // ShadowRect
411 case 11: // Shadow1DArray
415 case 12: // Shadow2DArray
420 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
421 .addOperand(MI->getOperand(3))
439 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
440 .addOperand(MI->getOperand(2))
458 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
459 .addOperand(MI->getOperand(0))
460 .addOperand(MI->getOperand(1))
478 .addReg(T0, RegState::Implicit)
479 .addReg(T1, RegState::Implicit);
484 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
485 .addOperand(MI->getOperand(0));
488 case AMDGPU::BRANCH_COND_f32: {
489 MachineInstr *NewMI =
490 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
491 AMDGPU::PREDICATE_BIT)
492 .addOperand(MI->getOperand(1))
493 .addImm(OPCODE_IS_NOT_ZERO)
495 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
496 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
497 .addOperand(MI->getOperand(0))
498 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
502 case AMDGPU::BRANCH_COND_i32: {
503 MachineInstr *NewMI =
504 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
505 AMDGPU::PREDICATE_BIT)
506 .addOperand(MI->getOperand(1))
507 .addImm(OPCODE_IS_NOT_ZERO_INT)
509 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
510 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
511 .addOperand(MI->getOperand(0))
512 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
516 case AMDGPU::EG_ExportSwz:
517 case AMDGPU::R600_ExportSwz: {
518 // Instruction is left unmodified if its not the last one of its type
519 bool isLastInstructionOfItsType = true;
520 unsigned InstExportType = MI->getOperand(1).getImm();
521 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
522 EndBlock = BB->end(); NextExportInst != EndBlock;
523 NextExportInst = std::next(NextExportInst)) {
524 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
525 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
526 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
528 if (CurrentInstExportType == InstExportType) {
529 isLastInstructionOfItsType = false;
534 bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
535 if (!EOP && !isLastInstructionOfItsType)
537 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
538 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
539 .addOperand(MI->getOperand(0))
540 .addOperand(MI->getOperand(1))
541 .addOperand(MI->getOperand(2))
542 .addOperand(MI->getOperand(3))
543 .addOperand(MI->getOperand(4))
544 .addOperand(MI->getOperand(5))
545 .addOperand(MI->getOperand(6))
550 case AMDGPU::RETURN: {
551 // RETURN instructions must have the live-out registers as implicit uses,
552 // otherwise they appear dead.
553 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
554 MachineInstrBuilder MIB(*MF, MI);
555 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
556 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
561 MI->eraseFromParent();
565 //===----------------------------------------------------------------------===//
566 // Custom DAG Lowering Operations
567 //===----------------------------------------------------------------------===//
569 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
570 MachineFunction &MF = DAG.getMachineFunction();
571 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
572 switch (Op.getOpcode()) {
573 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
574 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
575 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
576 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
578 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
580 case ISD::FSIN: return LowerTrig(Op, DAG);
581 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
582 case ISD::STORE: return LowerSTORE(Op, DAG);
584 SDValue Result = LowerLOAD(Op, DAG);
585 assert((!Result.getNode() ||
586 Result.getNode()->getNumValues() == 2) &&
587 "Load should return a value and a chain");
591 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
592 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
593 case ISD::INTRINSIC_VOID: {
594 SDValue Chain = Op.getOperand(0);
595 unsigned IntrinsicID =
596 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
597 switch (IntrinsicID) {
598 case AMDGPUIntrinsic::AMDGPU_store_output: {
599 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
600 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
601 MFI->LiveOuts.push_back(Reg);
602 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
604 case AMDGPUIntrinsic::R600_store_swizzle: {
605 const SDValue Args[8] = {
607 Op.getOperand(2), // Export Value
608 Op.getOperand(3), // ArrayBase
609 Op.getOperand(4), // Type
610 DAG.getConstant(0, MVT::i32), // SWZ_X
611 DAG.getConstant(1, MVT::i32), // SWZ_Y
612 DAG.getConstant(2, MVT::i32), // SWZ_Z
613 DAG.getConstant(3, MVT::i32) // SWZ_W
615 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(), Args);
618 // default for switch(IntrinsicID)
621 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
624 case ISD::INTRINSIC_WO_CHAIN: {
625 unsigned IntrinsicID =
626 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
627 EVT VT = Op.getValueType();
629 switch(IntrinsicID) {
630 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
631 case AMDGPUIntrinsic::R600_load_input: {
632 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
633 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
634 MachineFunction &MF = DAG.getMachineFunction();
635 MachineRegisterInfo &MRI = MF.getRegInfo();
637 return DAG.getCopyFromReg(DAG.getEntryNode(),
638 SDLoc(DAG.getEntryNode()), Reg, VT);
641 case AMDGPUIntrinsic::R600_interp_input: {
642 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
643 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
644 MachineSDNode *interp;
646 const MachineFunction &MF = DAG.getMachineFunction();
647 const R600InstrInfo *TII =
648 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
649 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
650 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
651 return DAG.getTargetExtractSubreg(
652 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
653 DL, MVT::f32, SDValue(interp, 0));
655 MachineFunction &MF = DAG.getMachineFunction();
656 MachineRegisterInfo &MRI = MF.getRegInfo();
657 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
658 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
659 MRI.addLiveIn(RegisterI);
660 MRI.addLiveIn(RegisterJ);
661 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
662 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
663 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
664 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
667 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
668 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
669 RegisterJNode, RegisterINode);
671 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
672 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
673 RegisterJNode, RegisterINode);
674 return SDValue(interp, slot % 2);
676 case AMDGPUIntrinsic::R600_interp_xy:
677 case AMDGPUIntrinsic::R600_interp_zw: {
678 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
679 MachineSDNode *interp;
680 SDValue RegisterINode = Op.getOperand(2);
681 SDValue RegisterJNode = Op.getOperand(3);
683 if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
684 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
685 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
686 RegisterJNode, RegisterINode);
688 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
689 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
690 RegisterJNode, RegisterINode);
691 return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
692 SDValue(interp, 0), SDValue(interp, 1));
694 case AMDGPUIntrinsic::R600_tex:
695 case AMDGPUIntrinsic::R600_texc:
696 case AMDGPUIntrinsic::R600_txl:
697 case AMDGPUIntrinsic::R600_txlc:
698 case AMDGPUIntrinsic::R600_txb:
699 case AMDGPUIntrinsic::R600_txbc:
700 case AMDGPUIntrinsic::R600_txf:
701 case AMDGPUIntrinsic::R600_txq:
702 case AMDGPUIntrinsic::R600_ddx:
703 case AMDGPUIntrinsic::R600_ddy:
704 case AMDGPUIntrinsic::R600_ldptr: {
706 switch (IntrinsicID) {
707 case AMDGPUIntrinsic::R600_tex:
710 case AMDGPUIntrinsic::R600_texc:
713 case AMDGPUIntrinsic::R600_txl:
716 case AMDGPUIntrinsic::R600_txlc:
719 case AMDGPUIntrinsic::R600_txb:
722 case AMDGPUIntrinsic::R600_txbc:
725 case AMDGPUIntrinsic::R600_txf:
728 case AMDGPUIntrinsic::R600_txq:
731 case AMDGPUIntrinsic::R600_ddx:
734 case AMDGPUIntrinsic::R600_ddy:
737 case AMDGPUIntrinsic::R600_ldptr:
741 llvm_unreachable("Unknow Texture Operation");
744 SDValue TexArgs[19] = {
745 DAG.getConstant(TextureOp, MVT::i32),
747 DAG.getConstant(0, MVT::i32),
748 DAG.getConstant(1, MVT::i32),
749 DAG.getConstant(2, MVT::i32),
750 DAG.getConstant(3, MVT::i32),
754 DAG.getConstant(0, MVT::i32),
755 DAG.getConstant(1, MVT::i32),
756 DAG.getConstant(2, MVT::i32),
757 DAG.getConstant(3, MVT::i32),
765 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
767 case AMDGPUIntrinsic::AMDGPU_dp4: {
769 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
770 DAG.getConstant(0, MVT::i32)),
771 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
772 DAG.getConstant(0, MVT::i32)),
773 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
774 DAG.getConstant(1, MVT::i32)),
775 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
776 DAG.getConstant(1, MVT::i32)),
777 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
778 DAG.getConstant(2, MVT::i32)),
779 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
780 DAG.getConstant(2, MVT::i32)),
781 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
782 DAG.getConstant(3, MVT::i32)),
783 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
784 DAG.getConstant(3, MVT::i32))
786 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
789 case Intrinsic::r600_read_ngroups_x:
790 return LowerImplicitParameter(DAG, VT, DL, 0);
791 case Intrinsic::r600_read_ngroups_y:
792 return LowerImplicitParameter(DAG, VT, DL, 1);
793 case Intrinsic::r600_read_ngroups_z:
794 return LowerImplicitParameter(DAG, VT, DL, 2);
795 case Intrinsic::r600_read_global_size_x:
796 return LowerImplicitParameter(DAG, VT, DL, 3);
797 case Intrinsic::r600_read_global_size_y:
798 return LowerImplicitParameter(DAG, VT, DL, 4);
799 case Intrinsic::r600_read_global_size_z:
800 return LowerImplicitParameter(DAG, VT, DL, 5);
801 case Intrinsic::r600_read_local_size_x:
802 return LowerImplicitParameter(DAG, VT, DL, 6);
803 case Intrinsic::r600_read_local_size_y:
804 return LowerImplicitParameter(DAG, VT, DL, 7);
805 case Intrinsic::r600_read_local_size_z:
806 return LowerImplicitParameter(DAG, VT, DL, 8);
808 case Intrinsic::r600_read_tgid_x:
809 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
811 case Intrinsic::r600_read_tgid_y:
812 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
814 case Intrinsic::r600_read_tgid_z:
815 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
817 case Intrinsic::r600_read_tidig_x:
818 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
820 case Intrinsic::r600_read_tidig_y:
821 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
823 case Intrinsic::r600_read_tidig_z:
824 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
826 case Intrinsic::AMDGPU_rsq:
827 // XXX - I'm assuming SI's RSQ_LEGACY matches R600's behavior.
828 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
830 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
833 } // end switch(Op.getOpcode())
837 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
838 SmallVectorImpl<SDValue> &Results,
839 SelectionDAG &DAG) const {
840 switch (N->getOpcode()) {
842 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
844 case ISD::FP_TO_UINT:
845 if (N->getValueType(0) == MVT::i1) {
846 Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
849 // Fall-through. Since we don't care about out of bounds values
850 // we can use FP_TO_SINT for uints too. The DAGLegalizer code for uint
851 // considers some extra cases which are not necessary here.
852 case ISD::FP_TO_SINT: {
854 if (expandFP_TO_SINT(N, Result, DAG))
855 Results.push_back(Result);
859 SDValue Op = SDValue(N, 0);
861 EVT VT = Op.getValueType();
862 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
863 N->getOperand(0), N->getOperand(1));
864 Results.push_back(UDIVREM);
868 SDValue Op = SDValue(N, 0);
870 EVT VT = Op.getValueType();
871 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
872 N->getOperand(0), N->getOperand(1));
873 Results.push_back(UDIVREM.getValue(1));
877 SDValue Op = SDValue(N, 0);
879 EVT VT = Op.getValueType();
880 SDValue SDIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(VT, VT),
881 N->getOperand(0), N->getOperand(1));
882 Results.push_back(SDIVREM);
886 SDValue Op = SDValue(N, 0);
888 EVT VT = Op.getValueType();
889 SDValue SDIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(VT, VT),
890 N->getOperand(0), N->getOperand(1));
891 Results.push_back(SDIVREM.getValue(1));
895 SDValue Op = SDValue(N, 1);
896 SDValue RES = LowerSDIVREM(Op, DAG);
897 Results.push_back(RES);
898 Results.push_back(RES.getValue(1));
902 SDValue Op = SDValue(N, 0);
904 EVT VT = Op.getValueType();
905 EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
907 SDValue one = DAG.getConstant(1, HalfVT);
908 SDValue zero = DAG.getConstant(0, HalfVT);
911 SDValue LHS = N->getOperand(0);
912 SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
913 SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
915 SDValue RHS = N->getOperand(1);
916 SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
917 SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
919 // Get Speculative values
920 SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
921 SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
923 SDValue REM_Hi = zero;
924 SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
926 SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
927 SDValue DIV_Lo = zero;
929 const unsigned halfBitWidth = HalfVT.getSizeInBits();
931 for (unsigned i = 0; i < halfBitWidth; ++i) {
932 SDValue POS = DAG.getConstant(halfBitWidth - i - 1, HalfVT);
933 // Get Value of high bit
935 if (halfBitWidth == 32 && Subtarget->hasBFE()) {
936 HBit = DAG.getNode(AMDGPUISD::BFE_U32, DL, HalfVT, LHS_Lo, POS, one);
938 HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
939 HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
942 SDValue Carry = DAG.getNode(ISD::SRL, DL, HalfVT, REM_Lo,
943 DAG.getConstant(halfBitWidth - 1, HalfVT));
944 REM_Hi = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Hi, one);
945 REM_Hi = DAG.getNode(ISD::OR, DL, HalfVT, REM_Hi, Carry);
947 REM_Lo = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Lo, one);
948 REM_Lo = DAG.getNode(ISD::OR, DL, HalfVT, REM_Lo, HBit);
951 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
953 SDValue BIT = DAG.getConstant(1 << (halfBitWidth - i - 1), HalfVT);
954 SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETGE);
956 DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
960 SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
962 REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETGE);
963 REM_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, zero);
964 REM_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, one);
967 SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
968 SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, DIV_Lo, DIV_Hi);
969 Results.push_back(DIV);
970 Results.push_back(REM);
976 SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
977 SDValue Vector) const {
980 EVT VecVT = Vector.getValueType();
981 EVT EltVT = VecVT.getVectorElementType();
982 SmallVector<SDValue, 8> Args;
984 for (unsigned i = 0, e = VecVT.getVectorNumElements();
986 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
987 Vector, DAG.getConstant(i, getVectorIdxTy())));
990 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
993 SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
994 SelectionDAG &DAG) const {
997 SDValue Vector = Op.getOperand(0);
998 SDValue Index = Op.getOperand(1);
1000 if (isa<ConstantSDNode>(Index) ||
1001 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
1004 Vector = vectorToVerticalVector(DAG, Vector);
1005 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
1009 SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
1010 SelectionDAG &DAG) const {
1012 SDValue Vector = Op.getOperand(0);
1013 SDValue Value = Op.getOperand(1);
1014 SDValue Index = Op.getOperand(2);
1016 if (isa<ConstantSDNode>(Index) ||
1017 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
1020 Vector = vectorToVerticalVector(DAG, Vector);
1021 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
1022 Vector, Value, Index);
1023 return vectorToVerticalVector(DAG, Insert);
1026 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
1027 // On hw >= R700, COS/SIN input must be between -1. and 1.
1028 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
1029 EVT VT = Op.getValueType();
1030 SDValue Arg = Op.getOperand(0);
1031 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
1032 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
1033 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
1034 DAG.getConstantFP(0.15915494309, MVT::f32)),
1035 DAG.getConstantFP(0.5, MVT::f32)));
1037 switch (Op.getOpcode()) {
1039 TrigNode = AMDGPUISD::COS_HW;
1042 TrigNode = AMDGPUISD::SIN_HW;
1045 llvm_unreachable("Wrong trig opcode");
1047 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
1048 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
1049 DAG.getConstantFP(-0.5, MVT::f32)));
1050 if (Gen >= AMDGPUSubtarget::R700)
1052 // On R600 hw, COS/SIN input must be between -Pi and Pi.
1053 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
1054 DAG.getConstantFP(3.14159265359, MVT::f32));
1057 SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
1059 EVT VT = Op.getValueType();
1061 SDValue Lo = Op.getOperand(0);
1062 SDValue Hi = Op.getOperand(1);
1063 SDValue Shift = Op.getOperand(2);
1064 SDValue Zero = DAG.getConstant(0, VT);
1065 SDValue One = DAG.getConstant(1, VT);
1067 SDValue Width = DAG.getConstant(VT.getSizeInBits(), VT);
1068 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, VT);
1069 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
1070 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
1072 // The dance around Width1 is necessary for 0 special case.
1073 // Without it the CompShift might be 32, producing incorrect results in
1074 // Overflow. So we do the shift in two steps, the alternative is to
1075 // add a conditional to filter the special case.
1077 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
1078 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
1080 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
1081 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
1082 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
1084 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
1085 SDValue LoBig = Zero;
1087 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1088 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1090 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1093 SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
1095 EVT VT = Op.getValueType();
1097 SDValue Lo = Op.getOperand(0);
1098 SDValue Hi = Op.getOperand(1);
1099 SDValue Shift = Op.getOperand(2);
1100 SDValue Zero = DAG.getConstant(0, VT);
1101 SDValue One = DAG.getConstant(1, VT);
1103 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
1105 SDValue Width = DAG.getConstant(VT.getSizeInBits(), VT);
1106 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, VT);
1107 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
1108 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
1110 // The dance around Width1 is necessary for 0 special case.
1111 // Without it the CompShift might be 32, producing incorrect results in
1112 // Overflow. So we do the shift in two steps, the alternative is to
1113 // add a conditional to filter the special case.
1115 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
1116 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
1118 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
1119 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
1120 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
1122 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
1123 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
1125 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1126 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1128 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1131 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
1136 Op, DAG.getConstantFP(0.0f, MVT::f32),
1137 DAG.getCondCode(ISD::SETNE)
1141 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
1143 unsigned DwordOffset) const {
1144 unsigned ByteOffset = DwordOffset * 4;
1145 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1146 AMDGPUAS::CONSTANT_BUFFER_0);
1148 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
1149 assert(isInt<16>(ByteOffset));
1151 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
1152 DAG.getConstant(ByteOffset, MVT::i32), // PTR
1153 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
1154 false, false, false, 0);
1157 bool R600TargetLowering::isZero(SDValue Op) const {
1158 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
1159 return Cst->isNullValue();
1160 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
1161 return CstFP->isZero();
1167 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
1169 EVT VT = Op.getValueType();
1171 SDValue LHS = Op.getOperand(0);
1172 SDValue RHS = Op.getOperand(1);
1173 SDValue True = Op.getOperand(2);
1174 SDValue False = Op.getOperand(3);
1175 SDValue CC = Op.getOperand(4);
1178 // LHS and RHS are guaranteed to be the same value type
1179 EVT CompareVT = LHS.getValueType();
1181 // Check if we can lower this to a native operation.
1183 // Try to lower to a SET* instruction:
1185 // SET* can match the following patterns:
1187 // select_cc f32, f32, -1, 0, cc_supported
1188 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
1189 // select_cc i32, i32, -1, 0, cc_supported
1192 // Move hardware True/False values to the correct operand.
1193 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1194 ISD::CondCode InverseCC =
1195 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1196 if (isHWTrueValue(False) && isHWFalseValue(True)) {
1197 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
1198 std::swap(False, True);
1199 CC = DAG.getCondCode(InverseCC);
1201 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
1202 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
1203 std::swap(False, True);
1204 std::swap(LHS, RHS);
1205 CC = DAG.getCondCode(SwapInvCC);
1210 if (isHWTrueValue(True) && isHWFalseValue(False) &&
1211 (CompareVT == VT || VT == MVT::i32)) {
1212 // This can be matched by a SET* instruction.
1213 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
1216 // Try to lower to a CND* instruction:
1218 // CND* can match the following patterns:
1220 // select_cc f32, 0.0, f32, f32, cc_supported
1221 // select_cc f32, 0.0, i32, i32, cc_supported
1222 // select_cc i32, 0, f32, f32, cc_supported
1223 // select_cc i32, 0, i32, i32, cc_supported
1226 // Try to move the zero value to the RHS
1228 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1229 // Try swapping the operands
1230 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
1231 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1232 std::swap(LHS, RHS);
1233 CC = DAG.getCondCode(CCSwapped);
1235 // Try inverting the conditon and then swapping the operands
1236 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
1237 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
1238 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1239 std::swap(True, False);
1240 std::swap(LHS, RHS);
1241 CC = DAG.getCondCode(CCSwapped);
1248 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1249 if (CompareVT != VT) {
1250 // Bitcast True / False to the correct types. This will end up being
1251 // a nop, but it allows us to define only a single pattern in the
1252 // .TD files for each CND* instruction rather than having to have
1253 // one pattern for integer True/False and one for fp True/False
1254 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1255 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1262 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1270 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1273 DAG.getCondCode(CCOpcode));
1274 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1277 // If we make it this for it means we have no native instructions to handle
1278 // this SELECT_CC, so we must lower it.
1279 SDValue HWTrue, HWFalse;
1281 if (CompareVT == MVT::f32) {
1282 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
1283 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
1284 } else if (CompareVT == MVT::i32) {
1285 HWTrue = DAG.getConstant(-1, CompareVT);
1286 HWFalse = DAG.getConstant(0, CompareVT);
1289 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1292 // Lower this unsupported SELECT_CC into a combination of two supported
1293 // SELECT_CC operations.
1294 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1296 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1299 DAG.getCondCode(ISD::SETNE));
1302 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to
1303 /// convert these pointers to a register index. Each register holds
1304 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1305 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1306 /// for indirect addressing.
1307 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1308 unsigned StackWidth,
1309 SelectionDAG &DAG) const {
1311 switch(StackWidth) {
1321 default: llvm_unreachable("Invalid stack width");
1324 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1325 DAG.getConstant(SRLPad, MVT::i32));
1328 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1331 unsigned &PtrIncr) const {
1332 switch (StackWidth) {
1343 Channel = ElemIdx % 2;
1357 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1359 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1360 SDValue Chain = Op.getOperand(0);
1361 SDValue Value = Op.getOperand(1);
1362 SDValue Ptr = Op.getOperand(2);
1364 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1365 if (Result.getNode()) {
1369 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1370 if (StoreNode->isTruncatingStore()) {
1371 EVT VT = Value.getValueType();
1372 assert(VT.bitsLE(MVT::i32));
1373 EVT MemVT = StoreNode->getMemoryVT();
1374 SDValue MaskConstant;
1375 if (MemVT == MVT::i8) {
1376 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1378 assert(MemVT == MVT::i16);
1379 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1381 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1382 DAG.getConstant(2, MVT::i32));
1383 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1384 DAG.getConstant(0x00000003, VT));
1385 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1386 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1387 DAG.getConstant(3, VT));
1388 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1389 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1390 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1394 DAG.getConstant(0, MVT::i32),
1395 DAG.getConstant(0, MVT::i32),
1398 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1399 SDValue Args[3] = { Chain, Input, DWordAddr };
1400 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1401 Op->getVTList(), Args, MemVT,
1402 StoreNode->getMemOperand());
1403 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1404 Value.getValueType().bitsGE(MVT::i32)) {
1405 // Convert pointer from byte address to dword address.
1406 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1407 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1408 Ptr, DAG.getConstant(2, MVT::i32)));
1410 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1411 llvm_unreachable("Truncated and indexed stores not supported yet");
1413 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1419 EVT ValueVT = Value.getValueType();
1421 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1425 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1426 if (Ret.getNode()) {
1429 // Lowering for indirect addressing
1431 const MachineFunction &MF = DAG.getMachineFunction();
1432 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1433 getTargetMachine().getFrameLowering());
1434 unsigned StackWidth = TFL->getStackWidth(MF);
1436 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1438 if (ValueVT.isVector()) {
1439 unsigned NumElemVT = ValueVT.getVectorNumElements();
1440 EVT ElemVT = ValueVT.getVectorElementType();
1441 SmallVector<SDValue, 4> Stores(NumElemVT);
1443 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1444 "vector width in load");
1446 for (unsigned i = 0; i < NumElemVT; ++i) {
1447 unsigned Channel, PtrIncr;
1448 getStackAddress(StackWidth, i, Channel, PtrIncr);
1449 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1450 DAG.getConstant(PtrIncr, MVT::i32));
1451 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1452 Value, DAG.getConstant(i, MVT::i32));
1454 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1456 DAG.getTargetConstant(Channel, MVT::i32));
1458 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1460 if (ValueVT == MVT::i8) {
1461 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1463 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1464 DAG.getTargetConstant(0, MVT::i32)); // Channel
1470 // return (512 + (kc_bank << 12)
1472 ConstantAddressBlock(unsigned AddressSpace) {
1473 switch (AddressSpace) {
1474 case AMDGPUAS::CONSTANT_BUFFER_0:
1476 case AMDGPUAS::CONSTANT_BUFFER_1:
1478 case AMDGPUAS::CONSTANT_BUFFER_2:
1479 return 512 + 4096 * 2;
1480 case AMDGPUAS::CONSTANT_BUFFER_3:
1481 return 512 + 4096 * 3;
1482 case AMDGPUAS::CONSTANT_BUFFER_4:
1483 return 512 + 4096 * 4;
1484 case AMDGPUAS::CONSTANT_BUFFER_5:
1485 return 512 + 4096 * 5;
1486 case AMDGPUAS::CONSTANT_BUFFER_6:
1487 return 512 + 4096 * 6;
1488 case AMDGPUAS::CONSTANT_BUFFER_7:
1489 return 512 + 4096 * 7;
1490 case AMDGPUAS::CONSTANT_BUFFER_8:
1491 return 512 + 4096 * 8;
1492 case AMDGPUAS::CONSTANT_BUFFER_9:
1493 return 512 + 4096 * 9;
1494 case AMDGPUAS::CONSTANT_BUFFER_10:
1495 return 512 + 4096 * 10;
1496 case AMDGPUAS::CONSTANT_BUFFER_11:
1497 return 512 + 4096 * 11;
1498 case AMDGPUAS::CONSTANT_BUFFER_12:
1499 return 512 + 4096 * 12;
1500 case AMDGPUAS::CONSTANT_BUFFER_13:
1501 return 512 + 4096 * 13;
1502 case AMDGPUAS::CONSTANT_BUFFER_14:
1503 return 512 + 4096 * 14;
1504 case AMDGPUAS::CONSTANT_BUFFER_15:
1505 return 512 + 4096 * 15;
1511 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1513 EVT VT = Op.getValueType();
1515 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1516 SDValue Chain = Op.getOperand(0);
1517 SDValue Ptr = Op.getOperand(1);
1518 SDValue LoweredLoad;
1520 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1521 if (Ret.getNode()) {
1526 return DAG.getMergeValues(Ops, DL);
1530 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1531 SDValue MergedValues[2] = {
1532 SplitVectorLoad(Op, DAG),
1535 return DAG.getMergeValues(MergedValues, DL);
1538 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1539 if (ConstantBlock > -1 &&
1540 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1541 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1543 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1544 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1545 isa<ConstantSDNode>(Ptr)) {
1547 for (unsigned i = 0; i < 4; i++) {
1548 // We want Const position encoded with the following formula :
1549 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1550 // const_index is Ptr computed by llvm using an alignment of 16.
1551 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1552 // then div by 4 at the ISel step
1553 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1554 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1555 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1557 EVT NewVT = MVT::v4i32;
1558 unsigned NumElements = 4;
1559 if (VT.isVector()) {
1561 NumElements = VT.getVectorNumElements();
1563 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1564 makeArrayRef(Slots, NumElements));
1566 // non-constant ptr can't be folded, keeps it as a v4f32 load
1567 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1568 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1569 DAG.getConstant(LoadNode->getAddressSpace() -
1570 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1574 if (!VT.isVector()) {
1575 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1576 DAG.getConstant(0, MVT::i32));
1579 SDValue MergedValues[2] = {
1583 return DAG.getMergeValues(MergedValues, DL);
1586 // For most operations returning SDValue() will result in the node being
1587 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1588 // need to manually expand loads that may be legal in some address spaces and
1589 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1590 // compute shaders, since the data is sign extended when it is uploaded to the
1591 // buffer. However SEXT loads from other address spaces are not supported, so
1592 // we need to expand them here.
1593 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1594 EVT MemVT = LoadNode->getMemoryVT();
1595 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1596 SDValue ShiftAmount =
1597 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1598 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1599 LoadNode->getPointerInfo(), MemVT,
1600 LoadNode->isVolatile(),
1601 LoadNode->isNonTemporal(),
1602 LoadNode->getAlignment());
1603 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1604 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1606 SDValue MergedValues[2] = { Sra, Chain };
1607 return DAG.getMergeValues(MergedValues, DL);
1610 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1614 // Lowering for indirect addressing
1615 const MachineFunction &MF = DAG.getMachineFunction();
1616 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1617 getTargetMachine().getFrameLowering());
1618 unsigned StackWidth = TFL->getStackWidth(MF);
1620 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1622 if (VT.isVector()) {
1623 unsigned NumElemVT = VT.getVectorNumElements();
1624 EVT ElemVT = VT.getVectorElementType();
1627 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1628 "vector width in load");
1630 for (unsigned i = 0; i < NumElemVT; ++i) {
1631 unsigned Channel, PtrIncr;
1632 getStackAddress(StackWidth, i, Channel, PtrIncr);
1633 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1634 DAG.getConstant(PtrIncr, MVT::i32));
1635 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1637 DAG.getTargetConstant(Channel, MVT::i32),
1640 for (unsigned i = NumElemVT; i < 4; ++i) {
1641 Loads[i] = DAG.getUNDEF(ElemVT);
1643 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1644 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1646 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1648 DAG.getTargetConstant(0, MVT::i32), // Channel
1657 return DAG.getMergeValues(Ops, DL);
1660 SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1661 SDValue Chain = Op.getOperand(0);
1662 SDValue Cond = Op.getOperand(1);
1663 SDValue Jump = Op.getOperand(2);
1665 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1669 /// XXX Only kernel functions are supported, so we can assume for now that
1670 /// every function is a kernel function, but in the future we should use
1671 /// separate calling conventions for kernel and non-kernel functions.
1672 SDValue R600TargetLowering::LowerFormalArguments(
1674 CallingConv::ID CallConv,
1676 const SmallVectorImpl<ISD::InputArg> &Ins,
1677 SDLoc DL, SelectionDAG &DAG,
1678 SmallVectorImpl<SDValue> &InVals) const {
1679 SmallVector<CCValAssign, 16> ArgLocs;
1680 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1681 getTargetMachine(), ArgLocs, *DAG.getContext());
1682 MachineFunction &MF = DAG.getMachineFunction();
1683 unsigned ShaderType = MF.getInfo<R600MachineFunctionInfo>()->getShaderType();
1685 SmallVector<ISD::InputArg, 8> LocalIns;
1687 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1689 AnalyzeFormalArguments(CCInfo, LocalIns);
1691 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1692 CCValAssign &VA = ArgLocs[i];
1694 EVT MemVT = LocalIns[i].VT;
1696 if (ShaderType != ShaderType::COMPUTE) {
1697 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1698 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1699 InVals.push_back(Register);
1703 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1704 AMDGPUAS::CONSTANT_BUFFER_0);
1706 // i64 isn't a legal type, so the register type used ends up as i32, which
1707 // isn't expected here. It attempts to create this sextload, but it ends up
1708 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1711 // The first 36 bytes of the input buffer contains information about
1712 // thread group and global sizes.
1714 // FIXME: This should really check the extload type, but the handling of
1715 // extload vecto parameters seems to be broken.
1716 //ISD::LoadExtType Ext = Ins[i].Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1717 ISD::LoadExtType Ext = ISD::SEXTLOAD;
1718 SDValue Arg = DAG.getExtLoad(Ext, DL, VT, Chain,
1719 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1720 MachinePointerInfo(UndefValue::get(PtrTy)),
1721 MemVT, false, false, 4);
1723 // 4 is the preferred alignment for the CONSTANT memory space.
1724 InVals.push_back(Arg);
1729 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1732 return VT.changeVectorElementTypeToInteger();
1735 static SDValue CompactSwizzlableVector(
1736 SelectionDAG &DAG, SDValue VectorEntry,
1737 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1738 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1739 assert(RemapSwizzle.empty());
1740 SDValue NewBldVec[4] = {
1741 VectorEntry.getOperand(0),
1742 VectorEntry.getOperand(1),
1743 VectorEntry.getOperand(2),
1744 VectorEntry.getOperand(3)
1747 for (unsigned i = 0; i < 4; i++) {
1748 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1749 // We mask write here to teach later passes that the ith element of this
1750 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1751 // break false dependencies and additionnaly make assembly easier to read.
1752 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1753 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1755 RemapSwizzle[i] = 4; // SEL_0
1756 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1757 } else if (C->isExactlyValue(1.0)) {
1758 RemapSwizzle[i] = 5; // SEL_1
1759 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1763 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1765 for (unsigned j = 0; j < i; j++) {
1766 if (NewBldVec[i] == NewBldVec[j]) {
1767 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1768 RemapSwizzle[i] = j;
1774 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1775 VectorEntry.getValueType(), NewBldVec);
1778 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1779 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1780 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1781 assert(RemapSwizzle.empty());
1782 SDValue NewBldVec[4] = {
1783 VectorEntry.getOperand(0),
1784 VectorEntry.getOperand(1),
1785 VectorEntry.getOperand(2),
1786 VectorEntry.getOperand(3)
1788 bool isUnmovable[4] = { false, false, false, false };
1789 for (unsigned i = 0; i < 4; i++) {
1790 RemapSwizzle[i] = i;
1791 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1792 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1795 isUnmovable[Idx] = true;
1799 for (unsigned i = 0; i < 4; i++) {
1800 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1801 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1803 if (isUnmovable[Idx])
1806 std::swap(NewBldVec[Idx], NewBldVec[i]);
1807 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1812 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1813 VectorEntry.getValueType(), NewBldVec);
1817 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1818 SDValue Swz[4], SelectionDAG &DAG) const {
1819 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1820 // Old -> New swizzle values
1821 DenseMap<unsigned, unsigned> SwizzleRemap;
1823 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1824 for (unsigned i = 0; i < 4; i++) {
1825 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1826 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1827 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1830 SwizzleRemap.clear();
1831 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1832 for (unsigned i = 0; i < 4; i++) {
1833 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1834 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1835 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1842 //===----------------------------------------------------------------------===//
1843 // Custom DAG Optimizations
1844 //===----------------------------------------------------------------------===//
1846 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1847 DAGCombinerInfo &DCI) const {
1848 SelectionDAG &DAG = DCI.DAG;
1850 switch (N->getOpcode()) {
1851 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1852 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1853 case ISD::FP_ROUND: {
1854 SDValue Arg = N->getOperand(0);
1855 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1856 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1862 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1863 // (i32 select_cc f32, f32, -1, 0 cc)
1865 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1866 // this to one of the SET*_DX10 instructions.
1867 case ISD::FP_TO_SINT: {
1868 SDValue FNeg = N->getOperand(0);
1869 if (FNeg.getOpcode() != ISD::FNEG) {
1872 SDValue SelectCC = FNeg.getOperand(0);
1873 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1874 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1875 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1876 !isHWTrueValue(SelectCC.getOperand(2)) ||
1877 !isHWFalseValue(SelectCC.getOperand(3))) {
1881 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1882 SelectCC.getOperand(0), // LHS
1883 SelectCC.getOperand(1), // RHS
1884 DAG.getConstant(-1, MVT::i32), // True
1885 DAG.getConstant(0, MVT::i32), // Flase
1886 SelectCC.getOperand(4)); // CC
1891 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1892 // => build_vector elt0, ... , NewEltIdx, ... , eltN
1893 case ISD::INSERT_VECTOR_ELT: {
1894 SDValue InVec = N->getOperand(0);
1895 SDValue InVal = N->getOperand(1);
1896 SDValue EltNo = N->getOperand(2);
1899 // If the inserted element is an UNDEF, just use the input vector.
1900 if (InVal.getOpcode() == ISD::UNDEF)
1903 EVT VT = InVec.getValueType();
1905 // If we can't generate a legal BUILD_VECTOR, exit
1906 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1909 // Check that we know which element is being inserted
1910 if (!isa<ConstantSDNode>(EltNo))
1912 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1914 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1915 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1917 SmallVector<SDValue, 8> Ops;
1918 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1919 Ops.append(InVec.getNode()->op_begin(),
1920 InVec.getNode()->op_end());
1921 } else if (InVec.getOpcode() == ISD::UNDEF) {
1922 unsigned NElts = VT.getVectorNumElements();
1923 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1928 // Insert the element
1929 if (Elt < Ops.size()) {
1930 // All the operands of BUILD_VECTOR must have the same type;
1931 // we enforce that here.
1932 EVT OpVT = Ops[0].getValueType();
1933 if (InVal.getValueType() != OpVT)
1934 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1935 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1936 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1940 // Return the new vector
1941 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1944 // Extract_vec (Build_vector) generated by custom lowering
1945 // also needs to be customly combined
1946 case ISD::EXTRACT_VECTOR_ELT: {
1947 SDValue Arg = N->getOperand(0);
1948 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1949 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1950 unsigned Element = Const->getZExtValue();
1951 return Arg->getOperand(Element);
1954 if (Arg.getOpcode() == ISD::BITCAST &&
1955 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1956 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1957 unsigned Element = Const->getZExtValue();
1958 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1959 Arg->getOperand(0).getOperand(Element));
1964 case ISD::SELECT_CC: {
1965 // Try common optimizations
1966 SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1970 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1971 // selectcc x, y, a, b, inv(cc)
1973 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1974 // selectcc x, y, a, b, cc
1975 SDValue LHS = N->getOperand(0);
1976 if (LHS.getOpcode() != ISD::SELECT_CC) {
1980 SDValue RHS = N->getOperand(1);
1981 SDValue True = N->getOperand(2);
1982 SDValue False = N->getOperand(3);
1983 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1985 if (LHS.getOperand(2).getNode() != True.getNode() ||
1986 LHS.getOperand(3).getNode() != False.getNode() ||
1987 RHS.getNode() != False.getNode()) {
1992 default: return SDValue();
1993 case ISD::SETNE: return LHS;
1995 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1996 LHSCC = ISD::getSetCCInverse(LHSCC,
1997 LHS.getOperand(0).getValueType().isInteger());
1998 if (DCI.isBeforeLegalizeOps() ||
1999 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
2000 return DAG.getSelectCC(SDLoc(N),
2012 case AMDGPUISD::EXPORT: {
2013 SDValue Arg = N->getOperand(1);
2014 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2017 SDValue NewArgs[8] = {
2018 N->getOperand(0), // Chain
2020 N->getOperand(2), // ArrayBase
2021 N->getOperand(3), // Type
2022 N->getOperand(4), // SWZ_X
2023 N->getOperand(5), // SWZ_Y
2024 N->getOperand(6), // SWZ_Z
2025 N->getOperand(7) // SWZ_W
2028 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
2029 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
2031 case AMDGPUISD::TEXTURE_FETCH: {
2032 SDValue Arg = N->getOperand(1);
2033 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2036 SDValue NewArgs[19] = {
2057 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
2058 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
2063 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
2067 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
2068 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
2069 const R600InstrInfo *TII =
2070 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
2071 if (!Src.isMachineOpcode())
2073 switch (Src.getMachineOpcode()) {
2074 case AMDGPU::FNEG_R600:
2077 Src = Src.getOperand(0);
2078 Neg = DAG.getTargetConstant(1, MVT::i32);
2080 case AMDGPU::FABS_R600:
2083 Src = Src.getOperand(0);
2084 Abs = DAG.getTargetConstant(1, MVT::i32);
2086 case AMDGPU::CONST_COPY: {
2087 unsigned Opcode = ParentNode->getMachineOpcode();
2088 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2093 SDValue CstOffset = Src.getOperand(0);
2094 if (ParentNode->getValueType(0).isVector())
2097 // Gather constants values
2098 int SrcIndices[] = {
2099 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2100 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2101 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
2102 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2103 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2104 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2105 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2106 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2107 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2108 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2109 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2111 std::vector<unsigned> Consts;
2112 for (int OtherSrcIdx : SrcIndices) {
2113 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2114 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2120 if (RegisterSDNode *Reg =
2121 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
2122 if (Reg->getReg() == AMDGPU::ALU_CONST) {
2124 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
2125 Consts.push_back(Cst->getZExtValue());
2130 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
2131 Consts.push_back(Cst->getZExtValue());
2132 if (!TII->fitsConstReadLimitations(Consts)) {
2137 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2140 case AMDGPU::MOV_IMM_I32:
2141 case AMDGPU::MOV_IMM_F32: {
2142 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2143 uint64_t ImmValue = 0;
2146 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2147 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2148 float FloatValue = FPC->getValueAPF().convertToFloat();
2149 if (FloatValue == 0.0) {
2150 ImmReg = AMDGPU::ZERO;
2151 } else if (FloatValue == 0.5) {
2152 ImmReg = AMDGPU::HALF;
2153 } else if (FloatValue == 1.0) {
2154 ImmReg = AMDGPU::ONE;
2156 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2159 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2160 uint64_t Value = C->getZExtValue();
2162 ImmReg = AMDGPU::ZERO;
2163 } else if (Value == 1) {
2164 ImmReg = AMDGPU::ONE_INT;
2170 // Check that we aren't already using an immediate.
2171 // XXX: It's possible for an instruction to have more than one
2172 // immediate operand, but this is not supported yet.
2173 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2176 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2178 if (C->getZExtValue())
2180 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
2182 Src = DAG.getRegister(ImmReg, MVT::i32);
2191 /// \brief Fold the instructions after selecting them
2192 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2193 SelectionDAG &DAG) const {
2194 const R600InstrInfo *TII =
2195 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
2196 if (!Node->isMachineOpcode())
2198 unsigned Opcode = Node->getMachineOpcode();
2201 std::vector<SDValue> Ops;
2202 for (const SDUse &I : Node->ops())
2205 if (Opcode == AMDGPU::DOT_4) {
2206 int OperandIdx[] = {
2207 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2208 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2209 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2210 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2211 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2212 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2213 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2214 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2217 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2218 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2219 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2220 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2221 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2222 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2223 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2224 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2227 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2228 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2229 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2230 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2231 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2232 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2233 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2234 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2236 for (unsigned i = 0; i < 8; i++) {
2237 if (OperandIdx[i] < 0)
2239 SDValue &Src = Ops[OperandIdx[i] - 1];
2240 SDValue &Neg = Ops[NegIdx[i] - 1];
2241 SDValue &Abs = Ops[AbsIdx[i] - 1];
2242 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2243 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2246 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2247 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2248 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2250 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2251 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2252 SDValue &Src = Ops[i];
2253 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
2254 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2256 } else if (Opcode == AMDGPU::CLAMP_R600) {
2257 SDValue Src = Node->getOperand(0);
2258 if (!Src.isMachineOpcode() ||
2259 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2261 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2262 AMDGPU::OpName::clamp);
2265 std::vector<SDValue> Ops;
2266 unsigned NumOp = Src.getNumOperands();
2267 for(unsigned i = 0; i < NumOp; ++i)
2268 Ops.push_back(Src.getOperand(i));
2269 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
2270 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
2271 Node->getVTList(), Ops);
2273 if (!TII->hasInstrModifiers(Opcode))
2275 int OperandIdx[] = {
2276 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2277 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2278 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2281 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2282 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2283 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2286 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2287 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2290 for (unsigned i = 0; i < 3; i++) {
2291 if (OperandIdx[i] < 0)
2293 SDValue &Src = Ops[OperandIdx[i] - 1];
2294 SDValue &Neg = Ops[NegIdx[i] - 1];
2296 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2297 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2298 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2299 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2304 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2305 SDValue &Imm = Ops[ImmIdx];
2306 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2307 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);