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/Analysis/ValueTracking.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/IR/Argument.h"
29 #include "llvm/IR/Function.h"
33 R600TargetLowering::R600TargetLowering(TargetMachine &TM,
34 const AMDGPUSubtarget &STI)
35 : AMDGPUTargetLowering(TM, STI), Gen(STI.getGeneration()) {
36 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
37 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
38 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
39 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
40 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
41 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
43 computeRegisterProperties(STI.getRegisterInfo());
45 // Set condition code actions
46 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
47 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
49 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
50 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
51 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
52 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
53 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
54 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
55 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
56 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
57 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
59 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
60 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
61 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
62 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
64 setOperationAction(ISD::FCOS, MVT::f32, Custom);
65 setOperationAction(ISD::FSIN, MVT::f32, Custom);
67 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
68 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
70 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
71 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
72 setOperationAction(ISD::BRCOND, MVT::Other, Custom);
74 setOperationAction(ISD::FSUB, MVT::f32, Expand);
76 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
77 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
78 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
80 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
81 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
83 setOperationAction(ISD::SETCC, MVT::i32, Expand);
84 setOperationAction(ISD::SETCC, MVT::f32, Expand);
85 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
86 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
87 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
89 setOperationAction(ISD::SELECT, MVT::i32, Expand);
90 setOperationAction(ISD::SELECT, MVT::f32, Expand);
91 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
92 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
94 // ADD, SUB overflow. These need to be Custom because
95 // SelectionDAGLegalize::LegalizeOp (LegalizeDAG.cpp)
96 // turns Legal into expand
97 if (Subtarget->hasCARRY())
98 setOperationAction(ISD::UADDO, MVT::i32, Custom);
100 if (Subtarget->hasBORROW())
101 setOperationAction(ISD::USUBO, MVT::i32, Custom);
103 // Expand sign extension of vectors
104 if (!Subtarget->hasBFE())
105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
110 if (!Subtarget->hasBFE())
111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
112 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
113 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
115 if (!Subtarget->hasBFE())
116 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
117 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
118 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
120 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
121 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
127 // Legalize loads and stores to the private address space.
128 setOperationAction(ISD::LOAD, MVT::i32, Custom);
129 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
130 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
132 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
133 // spaces, so it is custom lowered to handle those where it isn't.
134 for (MVT VT : MVT::integer_valuetypes()) {
135 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
136 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
137 setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
139 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
140 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
141 setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
143 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
144 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
145 setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
148 setOperationAction(ISD::STORE, MVT::i8, Custom);
149 setOperationAction(ISD::STORE, MVT::i32, Custom);
150 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
151 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
152 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
153 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
155 setOperationAction(ISD::LOAD, MVT::i32, Custom);
156 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
157 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
159 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
160 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
161 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
162 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
164 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
165 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
166 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
167 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
169 setTargetDAGCombine(ISD::FP_ROUND);
170 setTargetDAGCombine(ISD::FP_TO_SINT);
171 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
172 setTargetDAGCombine(ISD::SELECT_CC);
173 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
175 // These should be replaced by UDVIREM, but it does not happen automatically
176 // during Type Legalization
177 setOperationAction(ISD::UDIV, MVT::i64, Custom);
178 setOperationAction(ISD::UREM, MVT::i64, Custom);
179 setOperationAction(ISD::SDIV, MVT::i64, Custom);
180 setOperationAction(ISD::SREM, MVT::i64, Custom);
182 // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
183 // to be Legal/Custom in order to avoid library calls.
184 setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
185 setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
186 setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
188 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
190 const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
191 for (MVT VT : ScalarIntVTs) {
192 setOperationAction(ISD::ADDC, VT, Expand);
193 setOperationAction(ISD::SUBC, VT, Expand);
194 setOperationAction(ISD::ADDE, VT, Expand);
195 setOperationAction(ISD::SUBE, VT, Expand);
198 setSchedulingPreference(Sched::Source);
201 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
202 MachineInstr * MI, MachineBasicBlock * BB) const {
203 MachineFunction * MF = BB->getParent();
204 MachineRegisterInfo &MRI = MF->getRegInfo();
205 MachineBasicBlock::iterator I = *MI;
206 const R600InstrInfo *TII =
207 static_cast<const R600InstrInfo *>(Subtarget->getInstrInfo());
209 switch (MI->getOpcode()) {
211 // Replace LDS_*_RET instruction that don't have any uses with the
212 // equivalent LDS_*_NORET instruction.
213 if (TII->isLDSRetInstr(MI->getOpcode())) {
214 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
215 assert(DstIdx != -1);
216 MachineInstrBuilder NewMI;
217 // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
218 // LDS_1A2D support and remove this special case.
219 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()) ||
220 MI->getOpcode() == AMDGPU::LDS_CMPST_RET)
223 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
224 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
225 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
226 NewMI.addOperand(MI->getOperand(i));
229 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
232 case AMDGPU::CLAMP_R600: {
233 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
235 MI->getOperand(0).getReg(),
236 MI->getOperand(1).getReg());
237 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
241 case AMDGPU::FABS_R600: {
242 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
244 MI->getOperand(0).getReg(),
245 MI->getOperand(1).getReg());
246 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
250 case AMDGPU::FNEG_R600: {
251 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
253 MI->getOperand(0).getReg(),
254 MI->getOperand(1).getReg());
255 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
259 case AMDGPU::MASK_WRITE: {
260 unsigned maskedRegister = MI->getOperand(0).getReg();
261 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
262 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
263 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
267 case AMDGPU::MOV_IMM_F32:
268 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
269 MI->getOperand(1).getFPImm()->getValueAPF()
270 .bitcastToAPInt().getZExtValue());
272 case AMDGPU::MOV_IMM_I32:
273 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
274 MI->getOperand(1).getImm());
276 case AMDGPU::CONST_COPY: {
277 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
278 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
279 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
280 MI->getOperand(1).getImm());
284 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
285 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
286 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
287 unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
289 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
290 .addOperand(MI->getOperand(0))
291 .addOperand(MI->getOperand(1))
292 .addImm(EOP); // Set End of program bit
297 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
298 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
299 MachineOperand &RID = MI->getOperand(4);
300 MachineOperand &SID = MI->getOperand(5);
301 unsigned TextureId = MI->getOperand(6).getImm();
302 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
303 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
315 case 8: // ShadowRect
326 case 11: // Shadow1DArray
330 case 12: // Shadow2DArray
334 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
335 .addOperand(MI->getOperand(3))
353 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
354 .addOperand(MI->getOperand(2))
372 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
373 .addOperand(MI->getOperand(0))
374 .addOperand(MI->getOperand(1))
392 .addReg(T0, RegState::Implicit)
393 .addReg(T1, RegState::Implicit);
397 case AMDGPU::TXD_SHADOW: {
398 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
399 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
400 MachineOperand &RID = MI->getOperand(4);
401 MachineOperand &SID = MI->getOperand(5);
402 unsigned TextureId = MI->getOperand(6).getImm();
403 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
404 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
416 case 8: // ShadowRect
427 case 11: // Shadow1DArray
431 case 12: // Shadow2DArray
436 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
437 .addOperand(MI->getOperand(3))
455 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
456 .addOperand(MI->getOperand(2))
474 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
475 .addOperand(MI->getOperand(0))
476 .addOperand(MI->getOperand(1))
494 .addReg(T0, RegState::Implicit)
495 .addReg(T1, RegState::Implicit);
500 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
501 .addOperand(MI->getOperand(0));
504 case AMDGPU::BRANCH_COND_f32: {
505 MachineInstr *NewMI =
506 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
507 AMDGPU::PREDICATE_BIT)
508 .addOperand(MI->getOperand(1))
509 .addImm(OPCODE_IS_NOT_ZERO)
511 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
512 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
513 .addOperand(MI->getOperand(0))
514 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
518 case AMDGPU::BRANCH_COND_i32: {
519 MachineInstr *NewMI =
520 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
521 AMDGPU::PREDICATE_BIT)
522 .addOperand(MI->getOperand(1))
523 .addImm(OPCODE_IS_NOT_ZERO_INT)
525 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
526 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
527 .addOperand(MI->getOperand(0))
528 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
532 case AMDGPU::EG_ExportSwz:
533 case AMDGPU::R600_ExportSwz: {
534 // Instruction is left unmodified if its not the last one of its type
535 bool isLastInstructionOfItsType = true;
536 unsigned InstExportType = MI->getOperand(1).getImm();
537 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
538 EndBlock = BB->end(); NextExportInst != EndBlock;
539 NextExportInst = std::next(NextExportInst)) {
540 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
541 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
542 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
544 if (CurrentInstExportType == InstExportType) {
545 isLastInstructionOfItsType = false;
550 bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
551 if (!EOP && !isLastInstructionOfItsType)
553 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
554 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
555 .addOperand(MI->getOperand(0))
556 .addOperand(MI->getOperand(1))
557 .addOperand(MI->getOperand(2))
558 .addOperand(MI->getOperand(3))
559 .addOperand(MI->getOperand(4))
560 .addOperand(MI->getOperand(5))
561 .addOperand(MI->getOperand(6))
566 case AMDGPU::RETURN: {
567 // RETURN instructions must have the live-out registers as implicit uses,
568 // otherwise they appear dead.
569 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
570 MachineInstrBuilder MIB(*MF, MI);
571 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
572 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
577 MI->eraseFromParent();
581 //===----------------------------------------------------------------------===//
582 // Custom DAG Lowering Operations
583 //===----------------------------------------------------------------------===//
585 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
586 MachineFunction &MF = DAG.getMachineFunction();
587 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
588 switch (Op.getOpcode()) {
589 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
590 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
591 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
592 case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
594 case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
595 case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
596 case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
598 case ISD::FSIN: return LowerTrig(Op, DAG);
599 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
600 case ISD::STORE: return LowerSTORE(Op, DAG);
602 SDValue Result = LowerLOAD(Op, DAG);
603 assert((!Result.getNode() ||
604 Result.getNode()->getNumValues() == 2) &&
605 "Load should return a value and a chain");
609 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
610 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
611 case ISD::INTRINSIC_VOID: {
612 SDValue Chain = Op.getOperand(0);
613 unsigned IntrinsicID =
614 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
615 switch (IntrinsicID) {
616 case AMDGPUIntrinsic::AMDGPU_store_output: {
617 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
618 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
619 MFI->LiveOuts.push_back(Reg);
620 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
622 case AMDGPUIntrinsic::R600_store_swizzle: {
623 const SDValue Args[8] = {
625 Op.getOperand(2), // Export Value
626 Op.getOperand(3), // ArrayBase
627 Op.getOperand(4), // Type
628 DAG.getConstant(0, MVT::i32), // SWZ_X
629 DAG.getConstant(1, MVT::i32), // SWZ_Y
630 DAG.getConstant(2, MVT::i32), // SWZ_Z
631 DAG.getConstant(3, MVT::i32) // SWZ_W
633 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(), Args);
636 // default for switch(IntrinsicID)
639 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
642 case ISD::INTRINSIC_WO_CHAIN: {
643 unsigned IntrinsicID =
644 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
645 EVT VT = Op.getValueType();
647 switch(IntrinsicID) {
648 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
649 case AMDGPUIntrinsic::R600_load_input: {
650 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
651 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
652 MachineFunction &MF = DAG.getMachineFunction();
653 MachineRegisterInfo &MRI = MF.getRegInfo();
655 return DAG.getCopyFromReg(DAG.getEntryNode(),
656 SDLoc(DAG.getEntryNode()), Reg, VT);
659 case AMDGPUIntrinsic::R600_interp_input: {
660 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
661 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
662 MachineSDNode *interp;
664 const R600InstrInfo *TII =
665 static_cast<const R600InstrInfo *>(Subtarget->getInstrInfo());
666 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
667 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
668 return DAG.getTargetExtractSubreg(
669 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
670 DL, MVT::f32, SDValue(interp, 0));
672 MachineFunction &MF = DAG.getMachineFunction();
673 MachineRegisterInfo &MRI = MF.getRegInfo();
674 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
675 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
676 MRI.addLiveIn(RegisterI);
677 MRI.addLiveIn(RegisterJ);
678 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
679 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
680 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
681 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
684 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
685 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
686 RegisterJNode, RegisterINode);
688 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
689 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
690 RegisterJNode, RegisterINode);
691 return SDValue(interp, slot % 2);
693 case AMDGPUIntrinsic::R600_interp_xy:
694 case AMDGPUIntrinsic::R600_interp_zw: {
695 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
696 MachineSDNode *interp;
697 SDValue RegisterINode = Op.getOperand(2);
698 SDValue RegisterJNode = Op.getOperand(3);
700 if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
701 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
702 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
703 RegisterJNode, RegisterINode);
705 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
706 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
707 RegisterJNode, RegisterINode);
708 return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
709 SDValue(interp, 0), SDValue(interp, 1));
711 case AMDGPUIntrinsic::R600_tex:
712 case AMDGPUIntrinsic::R600_texc:
713 case AMDGPUIntrinsic::R600_txl:
714 case AMDGPUIntrinsic::R600_txlc:
715 case AMDGPUIntrinsic::R600_txb:
716 case AMDGPUIntrinsic::R600_txbc:
717 case AMDGPUIntrinsic::R600_txf:
718 case AMDGPUIntrinsic::R600_txq:
719 case AMDGPUIntrinsic::R600_ddx:
720 case AMDGPUIntrinsic::R600_ddy:
721 case AMDGPUIntrinsic::R600_ldptr: {
723 switch (IntrinsicID) {
724 case AMDGPUIntrinsic::R600_tex:
727 case AMDGPUIntrinsic::R600_texc:
730 case AMDGPUIntrinsic::R600_txl:
733 case AMDGPUIntrinsic::R600_txlc:
736 case AMDGPUIntrinsic::R600_txb:
739 case AMDGPUIntrinsic::R600_txbc:
742 case AMDGPUIntrinsic::R600_txf:
745 case AMDGPUIntrinsic::R600_txq:
748 case AMDGPUIntrinsic::R600_ddx:
751 case AMDGPUIntrinsic::R600_ddy:
754 case AMDGPUIntrinsic::R600_ldptr:
758 llvm_unreachable("Unknow Texture Operation");
761 SDValue TexArgs[19] = {
762 DAG.getConstant(TextureOp, MVT::i32),
764 DAG.getConstant(0, MVT::i32),
765 DAG.getConstant(1, MVT::i32),
766 DAG.getConstant(2, MVT::i32),
767 DAG.getConstant(3, MVT::i32),
771 DAG.getConstant(0, MVT::i32),
772 DAG.getConstant(1, MVT::i32),
773 DAG.getConstant(2, MVT::i32),
774 DAG.getConstant(3, MVT::i32),
782 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
784 case AMDGPUIntrinsic::AMDGPU_dp4: {
786 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
787 DAG.getConstant(0, MVT::i32)),
788 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
789 DAG.getConstant(0, MVT::i32)),
790 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
791 DAG.getConstant(1, MVT::i32)),
792 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
793 DAG.getConstant(1, MVT::i32)),
794 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
795 DAG.getConstant(2, MVT::i32)),
796 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
797 DAG.getConstant(2, MVT::i32)),
798 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
799 DAG.getConstant(3, MVT::i32)),
800 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
801 DAG.getConstant(3, MVT::i32))
803 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
806 case Intrinsic::r600_read_ngroups_x:
807 return LowerImplicitParameter(DAG, VT, DL, 0);
808 case Intrinsic::r600_read_ngroups_y:
809 return LowerImplicitParameter(DAG, VT, DL, 1);
810 case Intrinsic::r600_read_ngroups_z:
811 return LowerImplicitParameter(DAG, VT, DL, 2);
812 case Intrinsic::r600_read_global_size_x:
813 return LowerImplicitParameter(DAG, VT, DL, 3);
814 case Intrinsic::r600_read_global_size_y:
815 return LowerImplicitParameter(DAG, VT, DL, 4);
816 case Intrinsic::r600_read_global_size_z:
817 return LowerImplicitParameter(DAG, VT, DL, 5);
818 case Intrinsic::r600_read_local_size_x:
819 return LowerImplicitParameter(DAG, VT, DL, 6);
820 case Intrinsic::r600_read_local_size_y:
821 return LowerImplicitParameter(DAG, VT, DL, 7);
822 case Intrinsic::r600_read_local_size_z:
823 return LowerImplicitParameter(DAG, VT, DL, 8);
825 case Intrinsic::AMDGPU_read_workdim:
826 return LowerImplicitParameter(DAG, VT, DL, MFI->ABIArgOffset / 4);
828 case Intrinsic::r600_read_tgid_x:
829 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
831 case Intrinsic::r600_read_tgid_y:
832 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
834 case Intrinsic::r600_read_tgid_z:
835 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
837 case Intrinsic::r600_read_tidig_x:
838 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
840 case Intrinsic::r600_read_tidig_y:
841 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
843 case Intrinsic::r600_read_tidig_z:
844 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
846 case Intrinsic::AMDGPU_rsq:
847 // XXX - I'm assuming SI's RSQ_LEGACY matches R600's behavior.
848 return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
850 case AMDGPUIntrinsic::AMDGPU_fract:
851 case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
852 return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
854 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
857 } // end switch(Op.getOpcode())
861 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
862 SmallVectorImpl<SDValue> &Results,
863 SelectionDAG &DAG) const {
864 switch (N->getOpcode()) {
866 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
868 case ISD::FP_TO_UINT:
869 if (N->getValueType(0) == MVT::i1) {
870 Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
873 // Fall-through. Since we don't care about out of bounds values
874 // we can use FP_TO_SINT for uints too. The DAGLegalizer code for uint
875 // considers some extra cases which are not necessary here.
876 case ISD::FP_TO_SINT: {
878 if (expandFP_TO_SINT(N, Result, DAG))
879 Results.push_back(Result);
883 SDValue Op = SDValue(N, 0);
885 EVT VT = Op.getValueType();
886 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
887 N->getOperand(0), N->getOperand(1));
888 Results.push_back(UDIVREM);
892 SDValue Op = SDValue(N, 0);
894 EVT VT = Op.getValueType();
895 SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
896 N->getOperand(0), N->getOperand(1));
897 Results.push_back(UDIVREM.getValue(1));
901 SDValue Op = SDValue(N, 0);
903 EVT VT = Op.getValueType();
904 SDValue SDIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(VT, VT),
905 N->getOperand(0), N->getOperand(1));
906 Results.push_back(SDIVREM);
910 SDValue Op = SDValue(N, 0);
912 EVT VT = Op.getValueType();
913 SDValue SDIVREM = DAG.getNode(ISD::SDIVREM, DL, DAG.getVTList(VT, VT),
914 N->getOperand(0), N->getOperand(1));
915 Results.push_back(SDIVREM.getValue(1));
919 SDValue Op = SDValue(N, 1);
920 SDValue RES = LowerSDIVREM(Op, DAG);
921 Results.push_back(RES);
922 Results.push_back(RES.getValue(1));
926 SDValue Op = SDValue(N, 0);
927 LowerUDIVREM64(Op, DAG, Results);
933 SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
934 SDValue Vector) const {
937 EVT VecVT = Vector.getValueType();
938 EVT EltVT = VecVT.getVectorElementType();
939 SmallVector<SDValue, 8> Args;
941 for (unsigned i = 0, e = VecVT.getVectorNumElements();
943 Args.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT,
944 Vector, DAG.getConstant(i, getVectorIdxTy())));
947 return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
950 SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
951 SelectionDAG &DAG) const {
954 SDValue Vector = Op.getOperand(0);
955 SDValue Index = Op.getOperand(1);
957 if (isa<ConstantSDNode>(Index) ||
958 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
961 Vector = vectorToVerticalVector(DAG, Vector);
962 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
966 SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
967 SelectionDAG &DAG) const {
969 SDValue Vector = Op.getOperand(0);
970 SDValue Value = Op.getOperand(1);
971 SDValue Index = Op.getOperand(2);
973 if (isa<ConstantSDNode>(Index) ||
974 Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
977 Vector = vectorToVerticalVector(DAG, Vector);
978 SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
979 Vector, Value, Index);
980 return vectorToVerticalVector(DAG, Insert);
983 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
984 // On hw >= R700, COS/SIN input must be between -1. and 1.
985 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
986 EVT VT = Op.getValueType();
987 SDValue Arg = Op.getOperand(0);
988 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
989 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
990 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
991 DAG.getConstantFP(0.15915494309, MVT::f32)),
992 DAG.getConstantFP(0.5, MVT::f32)));
994 switch (Op.getOpcode()) {
996 TrigNode = AMDGPUISD::COS_HW;
999 TrigNode = AMDGPUISD::SIN_HW;
1002 llvm_unreachable("Wrong trig opcode");
1004 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
1005 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
1006 DAG.getConstantFP(-0.5, MVT::f32)));
1007 if (Gen >= AMDGPUSubtarget::R700)
1009 // On R600 hw, COS/SIN input must be between -Pi and Pi.
1010 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
1011 DAG.getConstantFP(3.14159265359, MVT::f32));
1014 SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
1016 EVT VT = Op.getValueType();
1018 SDValue Lo = Op.getOperand(0);
1019 SDValue Hi = Op.getOperand(1);
1020 SDValue Shift = Op.getOperand(2);
1021 SDValue Zero = DAG.getConstant(0, VT);
1022 SDValue One = DAG.getConstant(1, VT);
1024 SDValue Width = DAG.getConstant(VT.getSizeInBits(), VT);
1025 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, VT);
1026 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
1027 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
1029 // The dance around Width1 is necessary for 0 special case.
1030 // Without it the CompShift might be 32, producing incorrect results in
1031 // Overflow. So we do the shift in two steps, the alternative is to
1032 // add a conditional to filter the special case.
1034 SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
1035 Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
1037 SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
1038 HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
1039 SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
1041 SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
1042 SDValue LoBig = Zero;
1044 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1045 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1047 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1050 SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
1052 EVT VT = Op.getValueType();
1054 SDValue Lo = Op.getOperand(0);
1055 SDValue Hi = Op.getOperand(1);
1056 SDValue Shift = Op.getOperand(2);
1057 SDValue Zero = DAG.getConstant(0, VT);
1058 SDValue One = DAG.getConstant(1, VT);
1060 const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
1062 SDValue Width = DAG.getConstant(VT.getSizeInBits(), VT);
1063 SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, VT);
1064 SDValue BigShift = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
1065 SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
1067 // The dance around Width1 is necessary for 0 special case.
1068 // Without it the CompShift might be 32, producing incorrect results in
1069 // Overflow. So we do the shift in two steps, the alternative is to
1070 // add a conditional to filter the special case.
1072 SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
1073 Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
1075 SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
1076 SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
1077 LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
1079 SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
1080 SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
1082 Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1083 Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1085 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1088 SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
1089 unsigned mainop, unsigned ovf) const {
1091 EVT VT = Op.getValueType();
1093 SDValue Lo = Op.getOperand(0);
1094 SDValue Hi = Op.getOperand(1);
1096 SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
1098 OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
1099 DAG.getValueType(MVT::i1));
1101 SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
1103 return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
1106 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
1111 Op, DAG.getConstantFP(0.0f, MVT::f32),
1112 DAG.getCondCode(ISD::SETNE)
1116 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
1118 unsigned DwordOffset) const {
1119 unsigned ByteOffset = DwordOffset * 4;
1120 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1121 AMDGPUAS::CONSTANT_BUFFER_0);
1123 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
1124 assert(isInt<16>(ByteOffset));
1126 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
1127 DAG.getConstant(ByteOffset, MVT::i32), // PTR
1128 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
1129 false, false, false, 0);
1132 bool R600TargetLowering::isZero(SDValue Op) const {
1133 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
1134 return Cst->isNullValue();
1135 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
1136 return CstFP->isZero();
1142 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
1144 EVT VT = Op.getValueType();
1146 SDValue LHS = Op.getOperand(0);
1147 SDValue RHS = Op.getOperand(1);
1148 SDValue True = Op.getOperand(2);
1149 SDValue False = Op.getOperand(3);
1150 SDValue CC = Op.getOperand(4);
1153 if (VT == MVT::f32) {
1154 DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
1155 SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
1160 // LHS and RHS are guaranteed to be the same value type
1161 EVT CompareVT = LHS.getValueType();
1163 // Check if we can lower this to a native operation.
1165 // Try to lower to a SET* instruction:
1167 // SET* can match the following patterns:
1169 // select_cc f32, f32, -1, 0, cc_supported
1170 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
1171 // select_cc i32, i32, -1, 0, cc_supported
1174 // Move hardware True/False values to the correct operand.
1175 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1176 ISD::CondCode InverseCC =
1177 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1178 if (isHWTrueValue(False) && isHWFalseValue(True)) {
1179 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
1180 std::swap(False, True);
1181 CC = DAG.getCondCode(InverseCC);
1183 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
1184 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
1185 std::swap(False, True);
1186 std::swap(LHS, RHS);
1187 CC = DAG.getCondCode(SwapInvCC);
1192 if (isHWTrueValue(True) && isHWFalseValue(False) &&
1193 (CompareVT == VT || VT == MVT::i32)) {
1194 // This can be matched by a SET* instruction.
1195 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
1198 // Try to lower to a CND* instruction:
1200 // CND* can match the following patterns:
1202 // select_cc f32, 0.0, f32, f32, cc_supported
1203 // select_cc f32, 0.0, i32, i32, cc_supported
1204 // select_cc i32, 0, f32, f32, cc_supported
1205 // select_cc i32, 0, i32, i32, cc_supported
1208 // Try to move the zero value to the RHS
1210 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1211 // Try swapping the operands
1212 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
1213 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1214 std::swap(LHS, RHS);
1215 CC = DAG.getCondCode(CCSwapped);
1217 // Try inverting the conditon and then swapping the operands
1218 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
1219 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
1220 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1221 std::swap(True, False);
1222 std::swap(LHS, RHS);
1223 CC = DAG.getCondCode(CCSwapped);
1230 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1231 if (CompareVT != VT) {
1232 // Bitcast True / False to the correct types. This will end up being
1233 // a nop, but it allows us to define only a single pattern in the
1234 // .TD files for each CND* instruction rather than having to have
1235 // one pattern for integer True/False and one for fp True/False
1236 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1237 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1244 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1252 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1255 DAG.getCondCode(CCOpcode));
1256 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1259 // If we make it this for it means we have no native instructions to handle
1260 // this SELECT_CC, so we must lower it.
1261 SDValue HWTrue, HWFalse;
1263 if (CompareVT == MVT::f32) {
1264 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
1265 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
1266 } else if (CompareVT == MVT::i32) {
1267 HWTrue = DAG.getConstant(-1, CompareVT);
1268 HWFalse = DAG.getConstant(0, CompareVT);
1271 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1274 // Lower this unsupported SELECT_CC into a combination of two supported
1275 // SELECT_CC operations.
1276 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1278 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1281 DAG.getCondCode(ISD::SETNE));
1284 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to
1285 /// convert these pointers to a register index. Each register holds
1286 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1287 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1288 /// for indirect addressing.
1289 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1290 unsigned StackWidth,
1291 SelectionDAG &DAG) const {
1293 switch(StackWidth) {
1303 default: llvm_unreachable("Invalid stack width");
1306 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1307 DAG.getConstant(SRLPad, MVT::i32));
1310 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1313 unsigned &PtrIncr) const {
1314 switch (StackWidth) {
1325 Channel = ElemIdx % 2;
1339 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1341 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1342 SDValue Chain = Op.getOperand(0);
1343 SDValue Value = Op.getOperand(1);
1344 SDValue Ptr = Op.getOperand(2);
1346 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1347 if (Result.getNode()) {
1351 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1352 if (StoreNode->isTruncatingStore()) {
1353 EVT VT = Value.getValueType();
1354 assert(VT.bitsLE(MVT::i32));
1355 EVT MemVT = StoreNode->getMemoryVT();
1356 SDValue MaskConstant;
1357 if (MemVT == MVT::i8) {
1358 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1360 assert(MemVT == MVT::i16);
1361 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1363 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1364 DAG.getConstant(2, MVT::i32));
1365 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1366 DAG.getConstant(0x00000003, VT));
1367 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1368 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1369 DAG.getConstant(3, VT));
1370 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1371 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1372 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1376 DAG.getConstant(0, MVT::i32),
1377 DAG.getConstant(0, MVT::i32),
1380 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1381 SDValue Args[3] = { Chain, Input, DWordAddr };
1382 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1383 Op->getVTList(), Args, MemVT,
1384 StoreNode->getMemOperand());
1385 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1386 Value.getValueType().bitsGE(MVT::i32)) {
1387 // Convert pointer from byte address to dword address.
1388 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1389 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1390 Ptr, DAG.getConstant(2, MVT::i32)));
1392 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1393 llvm_unreachable("Truncated and indexed stores not supported yet");
1395 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1401 EVT ValueVT = Value.getValueType();
1403 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1407 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1408 if (Ret.getNode()) {
1411 // Lowering for indirect addressing
1413 const MachineFunction &MF = DAG.getMachineFunction();
1414 const AMDGPUFrameLowering *TFL =
1415 static_cast<const AMDGPUFrameLowering *>(Subtarget->getFrameLowering());
1416 unsigned StackWidth = TFL->getStackWidth(MF);
1418 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1420 if (ValueVT.isVector()) {
1421 unsigned NumElemVT = ValueVT.getVectorNumElements();
1422 EVT ElemVT = ValueVT.getVectorElementType();
1423 SmallVector<SDValue, 4> Stores(NumElemVT);
1425 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1426 "vector width in load");
1428 for (unsigned i = 0; i < NumElemVT; ++i) {
1429 unsigned Channel, PtrIncr;
1430 getStackAddress(StackWidth, i, Channel, PtrIncr);
1431 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1432 DAG.getConstant(PtrIncr, MVT::i32));
1433 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1434 Value, DAG.getConstant(i, MVT::i32));
1436 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1438 DAG.getTargetConstant(Channel, MVT::i32));
1440 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1442 if (ValueVT == MVT::i8) {
1443 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1445 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1446 DAG.getTargetConstant(0, MVT::i32)); // Channel
1452 // return (512 + (kc_bank << 12)
1454 ConstantAddressBlock(unsigned AddressSpace) {
1455 switch (AddressSpace) {
1456 case AMDGPUAS::CONSTANT_BUFFER_0:
1458 case AMDGPUAS::CONSTANT_BUFFER_1:
1460 case AMDGPUAS::CONSTANT_BUFFER_2:
1461 return 512 + 4096 * 2;
1462 case AMDGPUAS::CONSTANT_BUFFER_3:
1463 return 512 + 4096 * 3;
1464 case AMDGPUAS::CONSTANT_BUFFER_4:
1465 return 512 + 4096 * 4;
1466 case AMDGPUAS::CONSTANT_BUFFER_5:
1467 return 512 + 4096 * 5;
1468 case AMDGPUAS::CONSTANT_BUFFER_6:
1469 return 512 + 4096 * 6;
1470 case AMDGPUAS::CONSTANT_BUFFER_7:
1471 return 512 + 4096 * 7;
1472 case AMDGPUAS::CONSTANT_BUFFER_8:
1473 return 512 + 4096 * 8;
1474 case AMDGPUAS::CONSTANT_BUFFER_9:
1475 return 512 + 4096 * 9;
1476 case AMDGPUAS::CONSTANT_BUFFER_10:
1477 return 512 + 4096 * 10;
1478 case AMDGPUAS::CONSTANT_BUFFER_11:
1479 return 512 + 4096 * 11;
1480 case AMDGPUAS::CONSTANT_BUFFER_12:
1481 return 512 + 4096 * 12;
1482 case AMDGPUAS::CONSTANT_BUFFER_13:
1483 return 512 + 4096 * 13;
1484 case AMDGPUAS::CONSTANT_BUFFER_14:
1485 return 512 + 4096 * 14;
1486 case AMDGPUAS::CONSTANT_BUFFER_15:
1487 return 512 + 4096 * 15;
1493 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1495 EVT VT = Op.getValueType();
1497 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1498 SDValue Chain = Op.getOperand(0);
1499 SDValue Ptr = Op.getOperand(1);
1500 SDValue LoweredLoad;
1502 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1503 if (Ret.getNode()) {
1508 return DAG.getMergeValues(Ops, DL);
1511 // Lower loads constant address space global variable loads
1512 if (LoadNode->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1513 isa<GlobalVariable>(GetUnderlyingObject(
1514 LoadNode->getMemOperand()->getValue(), *getDataLayout()))) {
1516 SDValue Ptr = DAG.getZExtOrTrunc(LoadNode->getBasePtr(), DL,
1517 getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
1518 Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1519 DAG.getConstant(2, MVT::i32));
1520 return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op->getVTList(),
1521 LoadNode->getChain(), Ptr,
1522 DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
1525 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1526 SDValue MergedValues[2] = {
1527 ScalarizeVectorLoad(Op, DAG),
1530 return DAG.getMergeValues(MergedValues, DL);
1533 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1534 if (ConstantBlock > -1 &&
1535 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1536 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1538 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1539 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1540 isa<ConstantSDNode>(Ptr)) {
1542 for (unsigned i = 0; i < 4; i++) {
1543 // We want Const position encoded with the following formula :
1544 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1545 // const_index is Ptr computed by llvm using an alignment of 16.
1546 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1547 // then div by 4 at the ISel step
1548 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1549 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1550 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1552 EVT NewVT = MVT::v4i32;
1553 unsigned NumElements = 4;
1554 if (VT.isVector()) {
1556 NumElements = VT.getVectorNumElements();
1558 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1559 makeArrayRef(Slots, NumElements));
1561 // non-constant ptr can't be folded, keeps it as a v4f32 load
1562 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1563 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1564 DAG.getConstant(LoadNode->getAddressSpace() -
1565 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1569 if (!VT.isVector()) {
1570 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1571 DAG.getConstant(0, MVT::i32));
1574 SDValue MergedValues[2] = {
1578 return DAG.getMergeValues(MergedValues, DL);
1581 // For most operations returning SDValue() will result in the node being
1582 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1583 // need to manually expand loads that may be legal in some address spaces and
1584 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1585 // compute shaders, since the data is sign extended when it is uploaded to the
1586 // buffer. However SEXT loads from other address spaces are not supported, so
1587 // we need to expand them here.
1588 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1589 EVT MemVT = LoadNode->getMemoryVT();
1590 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1591 SDValue ShiftAmount =
1592 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1593 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1594 LoadNode->getPointerInfo(), MemVT,
1595 LoadNode->isVolatile(),
1596 LoadNode->isNonTemporal(),
1597 LoadNode->isInvariant(),
1598 LoadNode->getAlignment());
1599 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1600 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1602 SDValue MergedValues[2] = { Sra, Chain };
1603 return DAG.getMergeValues(MergedValues, DL);
1606 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1610 // Lowering for indirect addressing
1611 const MachineFunction &MF = DAG.getMachineFunction();
1612 const AMDGPUFrameLowering *TFL =
1613 static_cast<const AMDGPUFrameLowering *>(Subtarget->getFrameLowering());
1614 unsigned StackWidth = TFL->getStackWidth(MF);
1616 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1618 if (VT.isVector()) {
1619 unsigned NumElemVT = VT.getVectorNumElements();
1620 EVT ElemVT = VT.getVectorElementType();
1623 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1624 "vector width in load");
1626 for (unsigned i = 0; i < NumElemVT; ++i) {
1627 unsigned Channel, PtrIncr;
1628 getStackAddress(StackWidth, i, Channel, PtrIncr);
1629 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1630 DAG.getConstant(PtrIncr, MVT::i32));
1631 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1633 DAG.getTargetConstant(Channel, MVT::i32),
1636 for (unsigned i = NumElemVT; i < 4; ++i) {
1637 Loads[i] = DAG.getUNDEF(ElemVT);
1639 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1640 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1642 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1644 DAG.getTargetConstant(0, MVT::i32), // Channel
1653 return DAG.getMergeValues(Ops, DL);
1656 SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1657 SDValue Chain = Op.getOperand(0);
1658 SDValue Cond = Op.getOperand(1);
1659 SDValue Jump = Op.getOperand(2);
1661 return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1665 /// XXX Only kernel functions are supported, so we can assume for now that
1666 /// every function is a kernel function, but in the future we should use
1667 /// separate calling conventions for kernel and non-kernel functions.
1668 SDValue R600TargetLowering::LowerFormalArguments(
1670 CallingConv::ID CallConv,
1672 const SmallVectorImpl<ISD::InputArg> &Ins,
1673 SDLoc DL, SelectionDAG &DAG,
1674 SmallVectorImpl<SDValue> &InVals) const {
1675 SmallVector<CCValAssign, 16> ArgLocs;
1676 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1678 MachineFunction &MF = DAG.getMachineFunction();
1679 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
1681 SmallVector<ISD::InputArg, 8> LocalIns;
1683 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1685 AnalyzeFormalArguments(CCInfo, LocalIns);
1687 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
1689 const ISD::InputArg &In = Ins[i];
1691 EVT MemVT = VA.getLocVT();
1692 if (!VT.isVector() && MemVT.isVector()) {
1693 // Get load source type if scalarized.
1694 MemVT = MemVT.getVectorElementType();
1697 if (MFI->getShaderType() != ShaderType::COMPUTE) {
1698 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1699 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1700 InVals.push_back(Register);
1704 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1705 AMDGPUAS::CONSTANT_BUFFER_0);
1707 // i64 isn't a legal type, so the register type used ends up as i32, which
1708 // isn't expected here. It attempts to create this sextload, but it ends up
1709 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1712 // The first 36 bytes of the input buffer contains information about
1713 // thread group and global sizes.
1714 ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1715 if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1716 // FIXME: This should really check the extload type, but the handling of
1717 // extload vector parameters seems to be broken.
1719 // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1720 Ext = ISD::SEXTLOAD;
1723 // Compute the offset from the value.
1724 // XXX - I think PartOffset should give you this, but it seems to give the
1725 // size of the register which isn't useful.
1727 unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
1728 unsigned PartOffset = VA.getLocMemOffset();
1729 unsigned Offset = 36 + VA.getLocMemOffset();
1731 MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
1732 SDValue Arg = DAG.getLoad(ISD::UNINDEXED, Ext, VT, DL, Chain,
1733 DAG.getConstant(Offset, MVT::i32),
1734 DAG.getUNDEF(MVT::i32),
1736 MemVT, false, true, true, 4);
1738 // 4 is the preferred alignment for the CONSTANT memory space.
1739 InVals.push_back(Arg);
1740 MFI->ABIArgOffset = Offset + MemVT.getStoreSize();
1745 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1748 return VT.changeVectorElementTypeToInteger();
1751 static SDValue CompactSwizzlableVector(
1752 SelectionDAG &DAG, SDValue VectorEntry,
1753 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1754 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1755 assert(RemapSwizzle.empty());
1756 SDValue NewBldVec[4] = {
1757 VectorEntry.getOperand(0),
1758 VectorEntry.getOperand(1),
1759 VectorEntry.getOperand(2),
1760 VectorEntry.getOperand(3)
1763 for (unsigned i = 0; i < 4; i++) {
1764 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1765 // We mask write here to teach later passes that the ith element of this
1766 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1767 // break false dependencies and additionnaly make assembly easier to read.
1768 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1769 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1771 RemapSwizzle[i] = 4; // SEL_0
1772 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1773 } else if (C->isExactlyValue(1.0)) {
1774 RemapSwizzle[i] = 5; // SEL_1
1775 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1779 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1781 for (unsigned j = 0; j < i; j++) {
1782 if (NewBldVec[i] == NewBldVec[j]) {
1783 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1784 RemapSwizzle[i] = j;
1790 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1791 VectorEntry.getValueType(), NewBldVec);
1794 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1795 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1796 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1797 assert(RemapSwizzle.empty());
1798 SDValue NewBldVec[4] = {
1799 VectorEntry.getOperand(0),
1800 VectorEntry.getOperand(1),
1801 VectorEntry.getOperand(2),
1802 VectorEntry.getOperand(3)
1804 bool isUnmovable[4] = { false, false, false, false };
1805 for (unsigned i = 0; i < 4; i++) {
1806 RemapSwizzle[i] = i;
1807 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1808 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1811 isUnmovable[Idx] = true;
1815 for (unsigned i = 0; i < 4; i++) {
1816 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1817 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1819 if (isUnmovable[Idx])
1822 std::swap(NewBldVec[Idx], NewBldVec[i]);
1823 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1828 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1829 VectorEntry.getValueType(), NewBldVec);
1833 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1834 SDValue Swz[4], SelectionDAG &DAG) const {
1835 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1836 // Old -> New swizzle values
1837 DenseMap<unsigned, unsigned> SwizzleRemap;
1839 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1840 for (unsigned i = 0; i < 4; i++) {
1841 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
1842 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1843 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1846 SwizzleRemap.clear();
1847 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1848 for (unsigned i = 0; i < 4; i++) {
1849 unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
1850 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1851 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1858 //===----------------------------------------------------------------------===//
1859 // Custom DAG Optimizations
1860 //===----------------------------------------------------------------------===//
1862 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1863 DAGCombinerInfo &DCI) const {
1864 SelectionDAG &DAG = DCI.DAG;
1866 switch (N->getOpcode()) {
1867 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1868 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1869 case ISD::FP_ROUND: {
1870 SDValue Arg = N->getOperand(0);
1871 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1872 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1878 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1879 // (i32 select_cc f32, f32, -1, 0 cc)
1881 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1882 // this to one of the SET*_DX10 instructions.
1883 case ISD::FP_TO_SINT: {
1884 SDValue FNeg = N->getOperand(0);
1885 if (FNeg.getOpcode() != ISD::FNEG) {
1888 SDValue SelectCC = FNeg.getOperand(0);
1889 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1890 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1891 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1892 !isHWTrueValue(SelectCC.getOperand(2)) ||
1893 !isHWFalseValue(SelectCC.getOperand(3))) {
1897 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1898 SelectCC.getOperand(0), // LHS
1899 SelectCC.getOperand(1), // RHS
1900 DAG.getConstant(-1, MVT::i32), // True
1901 DAG.getConstant(0, MVT::i32), // False
1902 SelectCC.getOperand(4)); // CC
1907 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1908 // => build_vector elt0, ... , NewEltIdx, ... , eltN
1909 case ISD::INSERT_VECTOR_ELT: {
1910 SDValue InVec = N->getOperand(0);
1911 SDValue InVal = N->getOperand(1);
1912 SDValue EltNo = N->getOperand(2);
1915 // If the inserted element is an UNDEF, just use the input vector.
1916 if (InVal.getOpcode() == ISD::UNDEF)
1919 EVT VT = InVec.getValueType();
1921 // If we can't generate a legal BUILD_VECTOR, exit
1922 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1925 // Check that we know which element is being inserted
1926 if (!isa<ConstantSDNode>(EltNo))
1928 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1930 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1931 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1933 SmallVector<SDValue, 8> Ops;
1934 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1935 Ops.append(InVec.getNode()->op_begin(),
1936 InVec.getNode()->op_end());
1937 } else if (InVec.getOpcode() == ISD::UNDEF) {
1938 unsigned NElts = VT.getVectorNumElements();
1939 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1944 // Insert the element
1945 if (Elt < Ops.size()) {
1946 // All the operands of BUILD_VECTOR must have the same type;
1947 // we enforce that here.
1948 EVT OpVT = Ops[0].getValueType();
1949 if (InVal.getValueType() != OpVT)
1950 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1951 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1952 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1956 // Return the new vector
1957 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1960 // Extract_vec (Build_vector) generated by custom lowering
1961 // also needs to be customly combined
1962 case ISD::EXTRACT_VECTOR_ELT: {
1963 SDValue Arg = N->getOperand(0);
1964 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1965 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1966 unsigned Element = Const->getZExtValue();
1967 return Arg->getOperand(Element);
1970 if (Arg.getOpcode() == ISD::BITCAST &&
1971 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1972 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1973 unsigned Element = Const->getZExtValue();
1974 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1975 Arg->getOperand(0).getOperand(Element));
1980 case ISD::SELECT_CC: {
1981 // Try common optimizations
1982 SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1986 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1987 // selectcc x, y, a, b, inv(cc)
1989 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1990 // selectcc x, y, a, b, cc
1991 SDValue LHS = N->getOperand(0);
1992 if (LHS.getOpcode() != ISD::SELECT_CC) {
1996 SDValue RHS = N->getOperand(1);
1997 SDValue True = N->getOperand(2);
1998 SDValue False = N->getOperand(3);
1999 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
2001 if (LHS.getOperand(2).getNode() != True.getNode() ||
2002 LHS.getOperand(3).getNode() != False.getNode() ||
2003 RHS.getNode() != False.getNode()) {
2008 default: return SDValue();
2009 case ISD::SETNE: return LHS;
2011 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
2012 LHSCC = ISD::getSetCCInverse(LHSCC,
2013 LHS.getOperand(0).getValueType().isInteger());
2014 if (DCI.isBeforeLegalizeOps() ||
2015 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
2016 return DAG.getSelectCC(SDLoc(N),
2028 case AMDGPUISD::EXPORT: {
2029 SDValue Arg = N->getOperand(1);
2030 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2033 SDValue NewArgs[8] = {
2034 N->getOperand(0), // Chain
2036 N->getOperand(2), // ArrayBase
2037 N->getOperand(3), // Type
2038 N->getOperand(4), // SWZ_X
2039 N->getOperand(5), // SWZ_Y
2040 N->getOperand(6), // SWZ_Z
2041 N->getOperand(7) // SWZ_W
2044 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
2045 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
2047 case AMDGPUISD::TEXTURE_FETCH: {
2048 SDValue Arg = N->getOperand(1);
2049 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2052 SDValue NewArgs[19] = {
2073 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
2074 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
2079 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
2083 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
2084 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
2085 const R600InstrInfo *TII =
2086 static_cast<const R600InstrInfo *>(DAG.getSubtarget().getInstrInfo());
2087 if (!Src.isMachineOpcode())
2089 switch (Src.getMachineOpcode()) {
2090 case AMDGPU::FNEG_R600:
2093 Src = Src.getOperand(0);
2094 Neg = DAG.getTargetConstant(1, MVT::i32);
2096 case AMDGPU::FABS_R600:
2099 Src = Src.getOperand(0);
2100 Abs = DAG.getTargetConstant(1, MVT::i32);
2102 case AMDGPU::CONST_COPY: {
2103 unsigned Opcode = ParentNode->getMachineOpcode();
2104 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2109 SDValue CstOffset = Src.getOperand(0);
2110 if (ParentNode->getValueType(0).isVector())
2113 // Gather constants values
2114 int SrcIndices[] = {
2115 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2116 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2117 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
2118 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2119 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2120 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2121 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2122 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2123 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2124 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2125 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2127 std::vector<unsigned> Consts;
2128 for (int OtherSrcIdx : SrcIndices) {
2129 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2130 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2136 if (RegisterSDNode *Reg =
2137 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
2138 if (Reg->getReg() == AMDGPU::ALU_CONST) {
2140 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
2141 Consts.push_back(Cst->getZExtValue());
2146 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
2147 Consts.push_back(Cst->getZExtValue());
2148 if (!TII->fitsConstReadLimitations(Consts)) {
2153 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2156 case AMDGPU::MOV_IMM_I32:
2157 case AMDGPU::MOV_IMM_F32: {
2158 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2159 uint64_t ImmValue = 0;
2162 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2163 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2164 float FloatValue = FPC->getValueAPF().convertToFloat();
2165 if (FloatValue == 0.0) {
2166 ImmReg = AMDGPU::ZERO;
2167 } else if (FloatValue == 0.5) {
2168 ImmReg = AMDGPU::HALF;
2169 } else if (FloatValue == 1.0) {
2170 ImmReg = AMDGPU::ONE;
2172 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2175 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2176 uint64_t Value = C->getZExtValue();
2178 ImmReg = AMDGPU::ZERO;
2179 } else if (Value == 1) {
2180 ImmReg = AMDGPU::ONE_INT;
2186 // Check that we aren't already using an immediate.
2187 // XXX: It's possible for an instruction to have more than one
2188 // immediate operand, but this is not supported yet.
2189 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2192 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2194 if (C->getZExtValue())
2196 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
2198 Src = DAG.getRegister(ImmReg, MVT::i32);
2207 /// \brief Fold the instructions after selecting them
2208 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2209 SelectionDAG &DAG) const {
2210 const R600InstrInfo *TII =
2211 static_cast<const R600InstrInfo *>(DAG.getSubtarget().getInstrInfo());
2212 if (!Node->isMachineOpcode())
2214 unsigned Opcode = Node->getMachineOpcode();
2217 std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
2219 if (Opcode == AMDGPU::DOT_4) {
2220 int OperandIdx[] = {
2221 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2222 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2223 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2224 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2225 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2226 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2227 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2228 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2231 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2232 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2233 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2234 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2235 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2236 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2237 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2238 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2241 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2242 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2243 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2244 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2245 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2246 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2247 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2248 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2250 for (unsigned i = 0; i < 8; i++) {
2251 if (OperandIdx[i] < 0)
2253 SDValue &Src = Ops[OperandIdx[i] - 1];
2254 SDValue &Neg = Ops[NegIdx[i] - 1];
2255 SDValue &Abs = Ops[AbsIdx[i] - 1];
2256 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2257 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2260 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2261 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2262 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2264 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2265 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2266 SDValue &Src = Ops[i];
2267 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
2268 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2270 } else if (Opcode == AMDGPU::CLAMP_R600) {
2271 SDValue Src = Node->getOperand(0);
2272 if (!Src.isMachineOpcode() ||
2273 !TII->hasInstrModifiers(Src.getMachineOpcode()))
2275 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2276 AMDGPU::OpName::clamp);
2279 std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
2280 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
2281 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
2282 Node->getVTList(), Ops);
2284 if (!TII->hasInstrModifiers(Opcode))
2286 int OperandIdx[] = {
2287 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2288 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2289 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2292 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2293 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2294 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2297 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2298 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2301 for (unsigned i = 0; i < 3; i++) {
2302 if (OperandIdx[i] < 0)
2304 SDValue &Src = Ops[OperandIdx[i] - 1];
2305 SDValue &Neg = Ops[NegIdx[i] - 1];
2307 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2308 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2309 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2310 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2315 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2316 SDValue &Imm = Ops[ImmIdx];
2317 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2318 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);