1 //===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Custom DAG lowering for R600
13 //===----------------------------------------------------------------------===//
15 #include "R600ISelLowering.h"
16 #include "R600Defines.h"
17 #include "R600InstrInfo.h"
18 #include "R600MachineFunctionInfo.h"
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/IR/Argument.h"
25 #include "llvm/IR/Function.h"
29 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
30 AMDGPUTargetLowering(TM),
31 Gen(TM.getSubtarget<AMDGPUSubtarget>().getGeneration()) {
32 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
33 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
34 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
35 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
36 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
37 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
39 computeRegisterProperties();
41 // Set condition code actions
42 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
43 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
44 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
45 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
46 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
47 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
49 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
50 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
51 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
52 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
53 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
55 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
56 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
57 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
58 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
60 setOperationAction(ISD::FCOS, MVT::f32, Custom);
61 setOperationAction(ISD::FSIN, MVT::f32, Custom);
63 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
64 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
66 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
67 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
69 setOperationAction(ISD::FSUB, MVT::f32, Expand);
71 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
72 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
73 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
75 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
76 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
78 setOperationAction(ISD::SETCC, MVT::i32, Expand);
79 setOperationAction(ISD::SETCC, MVT::f32, Expand);
80 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
82 setOperationAction(ISD::SELECT, MVT::i32, Expand);
83 setOperationAction(ISD::SELECT, MVT::f32, Expand);
84 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
85 setOperationAction(ISD::SELECT, MVT::v2f32, Expand);
86 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
87 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
89 // Expand sign extension of vectors
90 if (!Subtarget->hasBFE())
91 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
93 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
94 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
96 if (!Subtarget->hasBFE())
97 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
98 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
99 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
101 if (!Subtarget->hasBFE())
102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
104 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
110 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
113 // Legalize loads and stores to the private address space.
114 setOperationAction(ISD::LOAD, MVT::i32, Custom);
115 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
116 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
118 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
119 // spaces, so it is custom lowered to handle those where it isn't.
120 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
121 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
122 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
123 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
124 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
125 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
127 setOperationAction(ISD::STORE, MVT::i8, Custom);
128 setOperationAction(ISD::STORE, MVT::i32, Custom);
129 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
130 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
131 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
132 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
134 setOperationAction(ISD::LOAD, MVT::i32, Custom);
135 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
136 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
138 setTargetDAGCombine(ISD::FP_ROUND);
139 setTargetDAGCombine(ISD::FP_TO_SINT);
140 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
141 setTargetDAGCombine(ISD::SELECT_CC);
142 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
144 // These should be replaced by UDVIREM, but it does not happen automatically
145 // during Type Legalization
146 setOperationAction(ISD::UDIV, MVT::i64, Custom);
147 setOperationAction(ISD::UREM, MVT::i64, Custom);
149 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
151 setBooleanContents(ZeroOrNegativeOneBooleanContent);
152 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
153 setSchedulingPreference(Sched::Source);
156 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
157 MachineInstr * MI, MachineBasicBlock * BB) const {
158 MachineFunction * MF = BB->getParent();
159 MachineRegisterInfo &MRI = MF->getRegInfo();
160 MachineBasicBlock::iterator I = *MI;
161 const R600InstrInfo *TII =
162 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
164 switch (MI->getOpcode()) {
166 // Replace LDS_*_RET instruction that don't have any uses with the
167 // equivalent LDS_*_NORET instruction.
168 if (TII->isLDSRetInstr(MI->getOpcode())) {
169 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
170 assert(DstIdx != -1);
171 MachineInstrBuilder NewMI;
172 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()))
175 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
176 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
177 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
178 NewMI.addOperand(MI->getOperand(i));
181 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
184 case AMDGPU::CLAMP_R600: {
185 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
187 MI->getOperand(0).getReg(),
188 MI->getOperand(1).getReg());
189 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
193 case AMDGPU::FABS_R600: {
194 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
196 MI->getOperand(0).getReg(),
197 MI->getOperand(1).getReg());
198 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
202 case AMDGPU::FNEG_R600: {
203 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
205 MI->getOperand(0).getReg(),
206 MI->getOperand(1).getReg());
207 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
211 case AMDGPU::MASK_WRITE: {
212 unsigned maskedRegister = MI->getOperand(0).getReg();
213 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
214 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
215 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
219 case AMDGPU::MOV_IMM_F32:
220 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
221 MI->getOperand(1).getFPImm()->getValueAPF()
222 .bitcastToAPInt().getZExtValue());
224 case AMDGPU::MOV_IMM_I32:
225 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
226 MI->getOperand(1).getImm());
228 case AMDGPU::CONST_COPY: {
229 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
230 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
231 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
232 MI->getOperand(1).getImm());
236 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
237 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
238 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
239 unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
241 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
242 .addOperand(MI->getOperand(0))
243 .addOperand(MI->getOperand(1))
244 .addImm(EOP); // Set End of program bit
249 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
250 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
251 MachineOperand &RID = MI->getOperand(4);
252 MachineOperand &SID = MI->getOperand(5);
253 unsigned TextureId = MI->getOperand(6).getImm();
254 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
255 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
267 case 8: // ShadowRect
278 case 11: // Shadow1DArray
282 case 12: // Shadow2DArray
286 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
287 .addOperand(MI->getOperand(3))
305 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
306 .addOperand(MI->getOperand(2))
324 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
325 .addOperand(MI->getOperand(0))
326 .addOperand(MI->getOperand(1))
344 .addReg(T0, RegState::Implicit)
345 .addReg(T1, RegState::Implicit);
349 case AMDGPU::TXD_SHADOW: {
350 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
351 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
352 MachineOperand &RID = MI->getOperand(4);
353 MachineOperand &SID = MI->getOperand(5);
354 unsigned TextureId = MI->getOperand(6).getImm();
355 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
356 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
368 case 8: // ShadowRect
379 case 11: // Shadow1DArray
383 case 12: // Shadow2DArray
388 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
389 .addOperand(MI->getOperand(3))
407 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
408 .addOperand(MI->getOperand(2))
426 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
427 .addOperand(MI->getOperand(0))
428 .addOperand(MI->getOperand(1))
446 .addReg(T0, RegState::Implicit)
447 .addReg(T1, RegState::Implicit);
452 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
453 .addOperand(MI->getOperand(0));
456 case AMDGPU::BRANCH_COND_f32: {
457 MachineInstr *NewMI =
458 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
459 AMDGPU::PREDICATE_BIT)
460 .addOperand(MI->getOperand(1))
461 .addImm(OPCODE_IS_NOT_ZERO)
463 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
464 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
465 .addOperand(MI->getOperand(0))
466 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
470 case AMDGPU::BRANCH_COND_i32: {
471 MachineInstr *NewMI =
472 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
473 AMDGPU::PREDICATE_BIT)
474 .addOperand(MI->getOperand(1))
475 .addImm(OPCODE_IS_NOT_ZERO_INT)
477 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
478 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
479 .addOperand(MI->getOperand(0))
480 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
484 case AMDGPU::EG_ExportSwz:
485 case AMDGPU::R600_ExportSwz: {
486 // Instruction is left unmodified if its not the last one of its type
487 bool isLastInstructionOfItsType = true;
488 unsigned InstExportType = MI->getOperand(1).getImm();
489 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
490 EndBlock = BB->end(); NextExportInst != EndBlock;
491 NextExportInst = std::next(NextExportInst)) {
492 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
493 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
494 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
496 if (CurrentInstExportType == InstExportType) {
497 isLastInstructionOfItsType = false;
502 bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
503 if (!EOP && !isLastInstructionOfItsType)
505 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
506 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
507 .addOperand(MI->getOperand(0))
508 .addOperand(MI->getOperand(1))
509 .addOperand(MI->getOperand(2))
510 .addOperand(MI->getOperand(3))
511 .addOperand(MI->getOperand(4))
512 .addOperand(MI->getOperand(5))
513 .addOperand(MI->getOperand(6))
518 case AMDGPU::RETURN: {
519 // RETURN instructions must have the live-out registers as implicit uses,
520 // otherwise they appear dead.
521 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
522 MachineInstrBuilder MIB(*MF, MI);
523 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
524 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
529 MI->eraseFromParent();
533 //===----------------------------------------------------------------------===//
534 // Custom DAG Lowering Operations
535 //===----------------------------------------------------------------------===//
537 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
538 MachineFunction &MF = DAG.getMachineFunction();
539 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
540 switch (Op.getOpcode()) {
541 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
543 case ISD::FSIN: return LowerTrig(Op, DAG);
544 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
545 case ISD::STORE: return LowerSTORE(Op, DAG);
546 case ISD::LOAD: return LowerLOAD(Op, DAG);
547 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
548 case ISD::INTRINSIC_VOID: {
549 SDValue Chain = Op.getOperand(0);
550 unsigned IntrinsicID =
551 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
552 switch (IntrinsicID) {
553 case AMDGPUIntrinsic::AMDGPU_store_output: {
554 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
555 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
556 MFI->LiveOuts.push_back(Reg);
557 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
559 case AMDGPUIntrinsic::R600_store_swizzle: {
560 const SDValue Args[8] = {
562 Op.getOperand(2), // Export Value
563 Op.getOperand(3), // ArrayBase
564 Op.getOperand(4), // Type
565 DAG.getConstant(0, MVT::i32), // SWZ_X
566 DAG.getConstant(1, MVT::i32), // SWZ_Y
567 DAG.getConstant(2, MVT::i32), // SWZ_Z
568 DAG.getConstant(3, MVT::i32) // SWZ_W
570 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(), Args);
573 // default for switch(IntrinsicID)
576 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
579 case ISD::INTRINSIC_WO_CHAIN: {
580 unsigned IntrinsicID =
581 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
582 EVT VT = Op.getValueType();
584 switch(IntrinsicID) {
585 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
586 case AMDGPUIntrinsic::R600_load_input: {
587 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
588 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
589 MachineFunction &MF = DAG.getMachineFunction();
590 MachineRegisterInfo &MRI = MF.getRegInfo();
592 return DAG.getCopyFromReg(DAG.getEntryNode(),
593 SDLoc(DAG.getEntryNode()), Reg, VT);
596 case AMDGPUIntrinsic::R600_interp_input: {
597 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
598 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
599 MachineSDNode *interp;
601 const MachineFunction &MF = DAG.getMachineFunction();
602 const R600InstrInfo *TII =
603 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
604 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
605 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
606 return DAG.getTargetExtractSubreg(
607 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
608 DL, MVT::f32, SDValue(interp, 0));
610 MachineFunction &MF = DAG.getMachineFunction();
611 MachineRegisterInfo &MRI = MF.getRegInfo();
612 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
613 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
614 MRI.addLiveIn(RegisterI);
615 MRI.addLiveIn(RegisterJ);
616 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
617 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
618 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
619 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
622 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
623 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
624 RegisterJNode, RegisterINode);
626 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
627 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
628 RegisterJNode, RegisterINode);
629 return SDValue(interp, slot % 2);
631 case AMDGPUIntrinsic::R600_interp_xy:
632 case AMDGPUIntrinsic::R600_interp_zw: {
633 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
634 MachineSDNode *interp;
635 SDValue RegisterINode = Op.getOperand(2);
636 SDValue RegisterJNode = Op.getOperand(3);
638 if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
639 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
640 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
641 RegisterJNode, RegisterINode);
643 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
644 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
645 RegisterJNode, RegisterINode);
646 return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
647 SDValue(interp, 0), SDValue(interp, 1));
649 case AMDGPUIntrinsic::R600_tex:
650 case AMDGPUIntrinsic::R600_texc:
651 case AMDGPUIntrinsic::R600_txl:
652 case AMDGPUIntrinsic::R600_txlc:
653 case AMDGPUIntrinsic::R600_txb:
654 case AMDGPUIntrinsic::R600_txbc:
655 case AMDGPUIntrinsic::R600_txf:
656 case AMDGPUIntrinsic::R600_txq:
657 case AMDGPUIntrinsic::R600_ddx:
658 case AMDGPUIntrinsic::R600_ddy:
659 case AMDGPUIntrinsic::R600_ldptr: {
661 switch (IntrinsicID) {
662 case AMDGPUIntrinsic::R600_tex:
665 case AMDGPUIntrinsic::R600_texc:
668 case AMDGPUIntrinsic::R600_txl:
671 case AMDGPUIntrinsic::R600_txlc:
674 case AMDGPUIntrinsic::R600_txb:
677 case AMDGPUIntrinsic::R600_txbc:
680 case AMDGPUIntrinsic::R600_txf:
683 case AMDGPUIntrinsic::R600_txq:
686 case AMDGPUIntrinsic::R600_ddx:
689 case AMDGPUIntrinsic::R600_ddy:
692 case AMDGPUIntrinsic::R600_ldptr:
696 llvm_unreachable("Unknow Texture Operation");
699 SDValue TexArgs[19] = {
700 DAG.getConstant(TextureOp, MVT::i32),
702 DAG.getConstant(0, MVT::i32),
703 DAG.getConstant(1, MVT::i32),
704 DAG.getConstant(2, MVT::i32),
705 DAG.getConstant(3, MVT::i32),
709 DAG.getConstant(0, MVT::i32),
710 DAG.getConstant(1, MVT::i32),
711 DAG.getConstant(2, MVT::i32),
712 DAG.getConstant(3, MVT::i32),
720 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
722 case AMDGPUIntrinsic::AMDGPU_dp4: {
724 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
725 DAG.getConstant(0, MVT::i32)),
726 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
727 DAG.getConstant(0, MVT::i32)),
728 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
729 DAG.getConstant(1, MVT::i32)),
730 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
731 DAG.getConstant(1, MVT::i32)),
732 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
733 DAG.getConstant(2, MVT::i32)),
734 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
735 DAG.getConstant(2, MVT::i32)),
736 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
737 DAG.getConstant(3, MVT::i32)),
738 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
739 DAG.getConstant(3, MVT::i32))
741 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
744 case Intrinsic::r600_read_ngroups_x:
745 return LowerImplicitParameter(DAG, VT, DL, 0);
746 case Intrinsic::r600_read_ngroups_y:
747 return LowerImplicitParameter(DAG, VT, DL, 1);
748 case Intrinsic::r600_read_ngroups_z:
749 return LowerImplicitParameter(DAG, VT, DL, 2);
750 case Intrinsic::r600_read_global_size_x:
751 return LowerImplicitParameter(DAG, VT, DL, 3);
752 case Intrinsic::r600_read_global_size_y:
753 return LowerImplicitParameter(DAG, VT, DL, 4);
754 case Intrinsic::r600_read_global_size_z:
755 return LowerImplicitParameter(DAG, VT, DL, 5);
756 case Intrinsic::r600_read_local_size_x:
757 return LowerImplicitParameter(DAG, VT, DL, 6);
758 case Intrinsic::r600_read_local_size_y:
759 return LowerImplicitParameter(DAG, VT, DL, 7);
760 case Intrinsic::r600_read_local_size_z:
761 return LowerImplicitParameter(DAG, VT, DL, 8);
763 case Intrinsic::r600_read_tgid_x:
764 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
766 case Intrinsic::r600_read_tgid_y:
767 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
769 case Intrinsic::r600_read_tgid_z:
770 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
772 case Intrinsic::r600_read_tidig_x:
773 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
775 case Intrinsic::r600_read_tidig_y:
776 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
778 case Intrinsic::r600_read_tidig_z:
779 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
782 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
785 } // end switch(Op.getOpcode())
789 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
790 SmallVectorImpl<SDValue> &Results,
791 SelectionDAG &DAG) const {
792 switch (N->getOpcode()) {
794 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
796 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
799 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
800 Results.push_back(SDValue(Node, 0));
801 Results.push_back(SDValue(Node, 1));
802 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
804 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
808 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
809 Results.push_back(SDValue(Node, 0));
814 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
815 // On hw >= R700, COS/SIN input must be between -1. and 1.
816 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
817 EVT VT = Op.getValueType();
818 SDValue Arg = Op.getOperand(0);
819 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
820 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
821 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
822 DAG.getConstantFP(0.15915494309, MVT::f32)),
823 DAG.getConstantFP(0.5, MVT::f32)));
825 switch (Op.getOpcode()) {
827 TrigNode = AMDGPUISD::COS_HW;
830 TrigNode = AMDGPUISD::SIN_HW;
833 llvm_unreachable("Wrong trig opcode");
835 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
836 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
837 DAG.getConstantFP(-0.5, MVT::f32)));
838 if (Gen >= AMDGPUSubtarget::R700)
840 // On R600 hw, COS/SIN input must be between -Pi and Pi.
841 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
842 DAG.getConstantFP(3.14159265359, MVT::f32));
845 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
850 Op, DAG.getConstantFP(0.0f, MVT::f32),
851 DAG.getCondCode(ISD::SETNE)
855 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
857 unsigned DwordOffset) const {
858 unsigned ByteOffset = DwordOffset * 4;
859 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
860 AMDGPUAS::CONSTANT_BUFFER_0);
862 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
863 assert(isInt<16>(ByteOffset));
865 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
866 DAG.getConstant(ByteOffset, MVT::i32), // PTR
867 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
868 false, false, false, 0);
871 bool R600TargetLowering::isZero(SDValue Op) const {
872 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
873 return Cst->isNullValue();
874 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
875 return CstFP->isZero();
881 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
883 EVT VT = Op.getValueType();
885 SDValue LHS = Op.getOperand(0);
886 SDValue RHS = Op.getOperand(1);
887 SDValue True = Op.getOperand(2);
888 SDValue False = Op.getOperand(3);
889 SDValue CC = Op.getOperand(4);
892 // LHS and RHS are guaranteed to be the same value type
893 EVT CompareVT = LHS.getValueType();
895 // Check if we can lower this to a native operation.
897 // Try to lower to a SET* instruction:
899 // SET* can match the following patterns:
901 // select_cc f32, f32, -1, 0, cc_supported
902 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
903 // select_cc i32, i32, -1, 0, cc_supported
906 // Move hardware True/False values to the correct operand.
907 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
908 ISD::CondCode InverseCC =
909 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
910 if (isHWTrueValue(False) && isHWFalseValue(True)) {
911 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
912 std::swap(False, True);
913 CC = DAG.getCondCode(InverseCC);
915 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
916 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
917 std::swap(False, True);
919 CC = DAG.getCondCode(SwapInvCC);
924 if (isHWTrueValue(True) && isHWFalseValue(False) &&
925 (CompareVT == VT || VT == MVT::i32)) {
926 // This can be matched by a SET* instruction.
927 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
930 // Try to lower to a CND* instruction:
932 // CND* can match the following patterns:
934 // select_cc f32, 0.0, f32, f32, cc_supported
935 // select_cc f32, 0.0, i32, i32, cc_supported
936 // select_cc i32, 0, f32, f32, cc_supported
937 // select_cc i32, 0, i32, i32, cc_supported
940 // Try to move the zero value to the RHS
942 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
943 // Try swapping the operands
944 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
945 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
947 CC = DAG.getCondCode(CCSwapped);
949 // Try inverting the conditon and then swapping the operands
950 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
951 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
952 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
953 std::swap(True, False);
955 CC = DAG.getCondCode(CCSwapped);
962 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
963 if (CompareVT != VT) {
964 // Bitcast True / False to the correct types. This will end up being
965 // a nop, but it allows us to define only a single pattern in the
966 // .TD files for each CND* instruction rather than having to have
967 // one pattern for integer True/False and one for fp True/False
968 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
969 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
976 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
984 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
987 DAG.getCondCode(CCOpcode));
988 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
992 // Possible Min/Max pattern
993 SDValue MinMax = LowerMinMax(Op, DAG);
994 if (MinMax.getNode()) {
998 // If we make it this for it means we have no native instructions to handle
999 // this SELECT_CC, so we must lower it.
1000 SDValue HWTrue, HWFalse;
1002 if (CompareVT == MVT::f32) {
1003 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
1004 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
1005 } else if (CompareVT == MVT::i32) {
1006 HWTrue = DAG.getConstant(-1, CompareVT);
1007 HWFalse = DAG.getConstant(0, CompareVT);
1010 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1013 // Lower this unsupported SELECT_CC into a combination of two supported
1014 // SELECT_CC operations.
1015 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1017 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1020 DAG.getCondCode(ISD::SETNE));
1023 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to
1024 /// convert these pointers to a register index. Each register holds
1025 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1026 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1027 /// for indirect addressing.
1028 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1029 unsigned StackWidth,
1030 SelectionDAG &DAG) const {
1032 switch(StackWidth) {
1042 default: llvm_unreachable("Invalid stack width");
1045 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1046 DAG.getConstant(SRLPad, MVT::i32));
1049 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1052 unsigned &PtrIncr) const {
1053 switch (StackWidth) {
1064 Channel = ElemIdx % 2;
1078 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1080 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1081 SDValue Chain = Op.getOperand(0);
1082 SDValue Value = Op.getOperand(1);
1083 SDValue Ptr = Op.getOperand(2);
1085 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1086 if (Result.getNode()) {
1090 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1091 if (StoreNode->isTruncatingStore()) {
1092 EVT VT = Value.getValueType();
1093 assert(VT.bitsLE(MVT::i32));
1094 EVT MemVT = StoreNode->getMemoryVT();
1095 SDValue MaskConstant;
1096 if (MemVT == MVT::i8) {
1097 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1099 assert(MemVT == MVT::i16);
1100 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1102 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1103 DAG.getConstant(2, MVT::i32));
1104 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1105 DAG.getConstant(0x00000003, VT));
1106 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1107 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1108 DAG.getConstant(3, VT));
1109 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1110 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1111 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1115 DAG.getConstant(0, MVT::i32),
1116 DAG.getConstant(0, MVT::i32),
1119 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1120 SDValue Args[3] = { Chain, Input, DWordAddr };
1121 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1122 Op->getVTList(), Args, MemVT,
1123 StoreNode->getMemOperand());
1124 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1125 Value.getValueType().bitsGE(MVT::i32)) {
1126 // Convert pointer from byte address to dword address.
1127 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1128 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1129 Ptr, DAG.getConstant(2, MVT::i32)));
1131 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1132 llvm_unreachable("Truncated and indexed stores not supported yet");
1134 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1140 EVT ValueVT = Value.getValueType();
1142 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1146 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1147 if (Ret.getNode()) {
1150 // Lowering for indirect addressing
1152 const MachineFunction &MF = DAG.getMachineFunction();
1153 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1154 getTargetMachine().getFrameLowering());
1155 unsigned StackWidth = TFL->getStackWidth(MF);
1157 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1159 if (ValueVT.isVector()) {
1160 unsigned NumElemVT = ValueVT.getVectorNumElements();
1161 EVT ElemVT = ValueVT.getVectorElementType();
1162 SmallVector<SDValue, 4> Stores(NumElemVT);
1164 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1165 "vector width in load");
1167 for (unsigned i = 0; i < NumElemVT; ++i) {
1168 unsigned Channel, PtrIncr;
1169 getStackAddress(StackWidth, i, Channel, PtrIncr);
1170 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1171 DAG.getConstant(PtrIncr, MVT::i32));
1172 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1173 Value, DAG.getConstant(i, MVT::i32));
1175 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1177 DAG.getTargetConstant(Channel, MVT::i32));
1179 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1181 if (ValueVT == MVT::i8) {
1182 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1184 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1185 DAG.getTargetConstant(0, MVT::i32)); // Channel
1191 // return (512 + (kc_bank << 12)
1193 ConstantAddressBlock(unsigned AddressSpace) {
1194 switch (AddressSpace) {
1195 case AMDGPUAS::CONSTANT_BUFFER_0:
1197 case AMDGPUAS::CONSTANT_BUFFER_1:
1199 case AMDGPUAS::CONSTANT_BUFFER_2:
1200 return 512 + 4096 * 2;
1201 case AMDGPUAS::CONSTANT_BUFFER_3:
1202 return 512 + 4096 * 3;
1203 case AMDGPUAS::CONSTANT_BUFFER_4:
1204 return 512 + 4096 * 4;
1205 case AMDGPUAS::CONSTANT_BUFFER_5:
1206 return 512 + 4096 * 5;
1207 case AMDGPUAS::CONSTANT_BUFFER_6:
1208 return 512 + 4096 * 6;
1209 case AMDGPUAS::CONSTANT_BUFFER_7:
1210 return 512 + 4096 * 7;
1211 case AMDGPUAS::CONSTANT_BUFFER_8:
1212 return 512 + 4096 * 8;
1213 case AMDGPUAS::CONSTANT_BUFFER_9:
1214 return 512 + 4096 * 9;
1215 case AMDGPUAS::CONSTANT_BUFFER_10:
1216 return 512 + 4096 * 10;
1217 case AMDGPUAS::CONSTANT_BUFFER_11:
1218 return 512 + 4096 * 11;
1219 case AMDGPUAS::CONSTANT_BUFFER_12:
1220 return 512 + 4096 * 12;
1221 case AMDGPUAS::CONSTANT_BUFFER_13:
1222 return 512 + 4096 * 13;
1223 case AMDGPUAS::CONSTANT_BUFFER_14:
1224 return 512 + 4096 * 14;
1225 case AMDGPUAS::CONSTANT_BUFFER_15:
1226 return 512 + 4096 * 15;
1232 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1234 EVT VT = Op.getValueType();
1236 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1237 SDValue Chain = Op.getOperand(0);
1238 SDValue Ptr = Op.getOperand(1);
1239 SDValue LoweredLoad;
1241 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1242 if (Ret.getNode()) {
1247 return DAG.getMergeValues(Ops, DL);
1251 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1252 SDValue MergedValues[2] = {
1253 SplitVectorLoad(Op, DAG),
1256 return DAG.getMergeValues(MergedValues, DL);
1259 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1260 if (ConstantBlock > -1 &&
1261 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1262 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1264 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1265 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1266 isa<ConstantSDNode>(Ptr)) {
1268 for (unsigned i = 0; i < 4; i++) {
1269 // We want Const position encoded with the following formula :
1270 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1271 // const_index is Ptr computed by llvm using an alignment of 16.
1272 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1273 // then div by 4 at the ISel step
1274 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1275 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1276 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1278 EVT NewVT = MVT::v4i32;
1279 unsigned NumElements = 4;
1280 if (VT.isVector()) {
1282 NumElements = VT.getVectorNumElements();
1284 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1285 ArrayRef<SDValue>(Slots, NumElements));
1287 // non-constant ptr can't be folded, keeps it as a v4f32 load
1288 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1289 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1290 DAG.getConstant(LoadNode->getAddressSpace() -
1291 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1295 if (!VT.isVector()) {
1296 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1297 DAG.getConstant(0, MVT::i32));
1300 SDValue MergedValues[2] = {
1304 return DAG.getMergeValues(MergedValues, DL);
1307 // For most operations returning SDValue() will result in the node being
1308 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1309 // need to manually expand loads that may be legal in some address spaces and
1310 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1311 // compute shaders, since the data is sign extended when it is uploaded to the
1312 // buffer. However SEXT loads from other address spaces are not supported, so
1313 // we need to expand them here.
1314 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1315 EVT MemVT = LoadNode->getMemoryVT();
1316 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1317 SDValue ShiftAmount =
1318 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1319 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1320 LoadNode->getPointerInfo(), MemVT,
1321 LoadNode->isVolatile(),
1322 LoadNode->isNonTemporal(),
1323 LoadNode->getAlignment());
1324 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1325 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1327 SDValue MergedValues[2] = { Sra, Chain };
1328 return DAG.getMergeValues(MergedValues, DL);
1331 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1335 // Lowering for indirect addressing
1336 const MachineFunction &MF = DAG.getMachineFunction();
1337 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1338 getTargetMachine().getFrameLowering());
1339 unsigned StackWidth = TFL->getStackWidth(MF);
1341 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1343 if (VT.isVector()) {
1344 unsigned NumElemVT = VT.getVectorNumElements();
1345 EVT ElemVT = VT.getVectorElementType();
1348 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1349 "vector width in load");
1351 for (unsigned i = 0; i < NumElemVT; ++i) {
1352 unsigned Channel, PtrIncr;
1353 getStackAddress(StackWidth, i, Channel, PtrIncr);
1354 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1355 DAG.getConstant(PtrIncr, MVT::i32));
1356 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1358 DAG.getTargetConstant(Channel, MVT::i32),
1361 for (unsigned i = NumElemVT; i < 4; ++i) {
1362 Loads[i] = DAG.getUNDEF(ElemVT);
1364 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1365 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1367 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1369 DAG.getTargetConstant(0, MVT::i32), // Channel
1378 return DAG.getMergeValues(Ops, DL);
1381 /// XXX Only kernel functions are supported, so we can assume for now that
1382 /// every function is a kernel function, but in the future we should use
1383 /// separate calling conventions for kernel and non-kernel functions.
1384 SDValue R600TargetLowering::LowerFormalArguments(
1386 CallingConv::ID CallConv,
1388 const SmallVectorImpl<ISD::InputArg> &Ins,
1389 SDLoc DL, SelectionDAG &DAG,
1390 SmallVectorImpl<SDValue> &InVals) const {
1391 SmallVector<CCValAssign, 16> ArgLocs;
1392 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1393 getTargetMachine(), ArgLocs, *DAG.getContext());
1394 MachineFunction &MF = DAG.getMachineFunction();
1395 unsigned ShaderType = MF.getInfo<R600MachineFunctionInfo>()->ShaderType;
1397 SmallVector<ISD::InputArg, 8> LocalIns;
1399 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1401 AnalyzeFormalArguments(CCInfo, LocalIns);
1403 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1404 CCValAssign &VA = ArgLocs[i];
1406 EVT MemVT = LocalIns[i].VT;
1408 if (ShaderType != ShaderType::COMPUTE) {
1409 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1410 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1411 InVals.push_back(Register);
1415 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1416 AMDGPUAS::CONSTANT_BUFFER_0);
1418 // i64 isn't a legal type, so the register type used ends up as i32, which
1419 // isn't expected here. It attempts to create this sextload, but it ends up
1420 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1423 // The first 36 bytes of the input buffer contains information about
1424 // thread group and global sizes.
1426 // FIXME: This should really check the extload type, but the handling of
1427 // extload vecto parameters seems to be broken.
1428 //ISD::LoadExtType Ext = Ins[i].Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1429 ISD::LoadExtType Ext = ISD::SEXTLOAD;
1430 SDValue Arg = DAG.getExtLoad(Ext, DL, VT, Chain,
1431 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1432 MachinePointerInfo(UndefValue::get(PtrTy)),
1433 MemVT, false, false, 4);
1435 // 4 is the preferred alignment for the CONSTANT memory space.
1436 InVals.push_back(Arg);
1441 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1444 return VT.changeVectorElementTypeToInteger();
1447 static SDValue CompactSwizzlableVector(
1448 SelectionDAG &DAG, SDValue VectorEntry,
1449 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1450 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1451 assert(RemapSwizzle.empty());
1452 SDValue NewBldVec[4] = {
1453 VectorEntry.getOperand(0),
1454 VectorEntry.getOperand(1),
1455 VectorEntry.getOperand(2),
1456 VectorEntry.getOperand(3)
1459 for (unsigned i = 0; i < 4; i++) {
1460 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1461 // We mask write here to teach later passes that the ith element of this
1462 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1463 // break false dependencies and additionnaly make assembly easier to read.
1464 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1465 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1467 RemapSwizzle[i] = 4; // SEL_0
1468 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1469 } else if (C->isExactlyValue(1.0)) {
1470 RemapSwizzle[i] = 5; // SEL_1
1471 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1475 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1477 for (unsigned j = 0; j < i; j++) {
1478 if (NewBldVec[i] == NewBldVec[j]) {
1479 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1480 RemapSwizzle[i] = j;
1486 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1487 VectorEntry.getValueType(), NewBldVec);
1490 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1491 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1492 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1493 assert(RemapSwizzle.empty());
1494 SDValue NewBldVec[4] = {
1495 VectorEntry.getOperand(0),
1496 VectorEntry.getOperand(1),
1497 VectorEntry.getOperand(2),
1498 VectorEntry.getOperand(3)
1500 bool isUnmovable[4] = { false, false, false, false };
1501 for (unsigned i = 0; i < 4; i++) {
1502 RemapSwizzle[i] = i;
1503 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1504 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1507 isUnmovable[Idx] = true;
1511 for (unsigned i = 0; i < 4; i++) {
1512 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1513 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1515 if (isUnmovable[Idx])
1518 std::swap(NewBldVec[Idx], NewBldVec[i]);
1519 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1524 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1525 VectorEntry.getValueType(), NewBldVec);
1529 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1530 SDValue Swz[4], SelectionDAG &DAG) const {
1531 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1532 // Old -> New swizzle values
1533 DenseMap<unsigned, unsigned> SwizzleRemap;
1535 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1536 for (unsigned i = 0; i < 4; i++) {
1537 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1538 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1539 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1542 SwizzleRemap.clear();
1543 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1544 for (unsigned i = 0; i < 4; i++) {
1545 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1546 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1547 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1554 //===----------------------------------------------------------------------===//
1555 // Custom DAG Optimizations
1556 //===----------------------------------------------------------------------===//
1558 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1559 DAGCombinerInfo &DCI) const {
1560 SelectionDAG &DAG = DCI.DAG;
1562 switch (N->getOpcode()) {
1563 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1564 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1565 case ISD::FP_ROUND: {
1566 SDValue Arg = N->getOperand(0);
1567 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1568 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1574 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1575 // (i32 select_cc f32, f32, -1, 0 cc)
1577 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1578 // this to one of the SET*_DX10 instructions.
1579 case ISD::FP_TO_SINT: {
1580 SDValue FNeg = N->getOperand(0);
1581 if (FNeg.getOpcode() != ISD::FNEG) {
1584 SDValue SelectCC = FNeg.getOperand(0);
1585 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1586 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1587 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1588 !isHWTrueValue(SelectCC.getOperand(2)) ||
1589 !isHWFalseValue(SelectCC.getOperand(3))) {
1593 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1594 SelectCC.getOperand(0), // LHS
1595 SelectCC.getOperand(1), // RHS
1596 DAG.getConstant(-1, MVT::i32), // True
1597 DAG.getConstant(0, MVT::i32), // Flase
1598 SelectCC.getOperand(4)); // CC
1603 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1604 // => build_vector elt0, ... , NewEltIdx, ... , eltN
1605 case ISD::INSERT_VECTOR_ELT: {
1606 SDValue InVec = N->getOperand(0);
1607 SDValue InVal = N->getOperand(1);
1608 SDValue EltNo = N->getOperand(2);
1611 // If the inserted element is an UNDEF, just use the input vector.
1612 if (InVal.getOpcode() == ISD::UNDEF)
1615 EVT VT = InVec.getValueType();
1617 // If we can't generate a legal BUILD_VECTOR, exit
1618 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1621 // Check that we know which element is being inserted
1622 if (!isa<ConstantSDNode>(EltNo))
1624 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1626 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1627 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1629 SmallVector<SDValue, 8> Ops;
1630 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1631 Ops.append(InVec.getNode()->op_begin(),
1632 InVec.getNode()->op_end());
1633 } else if (InVec.getOpcode() == ISD::UNDEF) {
1634 unsigned NElts = VT.getVectorNumElements();
1635 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1640 // Insert the element
1641 if (Elt < Ops.size()) {
1642 // All the operands of BUILD_VECTOR must have the same type;
1643 // we enforce that here.
1644 EVT OpVT = Ops[0].getValueType();
1645 if (InVal.getValueType() != OpVT)
1646 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1647 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1648 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1652 // Return the new vector
1653 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1656 // Extract_vec (Build_vector) generated by custom lowering
1657 // also needs to be customly combined
1658 case ISD::EXTRACT_VECTOR_ELT: {
1659 SDValue Arg = N->getOperand(0);
1660 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1661 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1662 unsigned Element = Const->getZExtValue();
1663 return Arg->getOperand(Element);
1666 if (Arg.getOpcode() == ISD::BITCAST &&
1667 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1668 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1669 unsigned Element = Const->getZExtValue();
1670 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1671 Arg->getOperand(0).getOperand(Element));
1676 case ISD::SELECT_CC: {
1677 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1678 // selectcc x, y, a, b, inv(cc)
1680 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1681 // selectcc x, y, a, b, cc
1682 SDValue LHS = N->getOperand(0);
1683 if (LHS.getOpcode() != ISD::SELECT_CC) {
1687 SDValue RHS = N->getOperand(1);
1688 SDValue True = N->getOperand(2);
1689 SDValue False = N->getOperand(3);
1690 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1692 if (LHS.getOperand(2).getNode() != True.getNode() ||
1693 LHS.getOperand(3).getNode() != False.getNode() ||
1694 RHS.getNode() != False.getNode()) {
1699 default: return SDValue();
1700 case ISD::SETNE: return LHS;
1702 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1703 LHSCC = ISD::getSetCCInverse(LHSCC,
1704 LHS.getOperand(0).getValueType().isInteger());
1705 if (DCI.isBeforeLegalizeOps() ||
1706 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1707 return DAG.getSelectCC(SDLoc(N),
1719 case AMDGPUISD::EXPORT: {
1720 SDValue Arg = N->getOperand(1);
1721 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1724 SDValue NewArgs[8] = {
1725 N->getOperand(0), // Chain
1727 N->getOperand(2), // ArrayBase
1728 N->getOperand(3), // Type
1729 N->getOperand(4), // SWZ_X
1730 N->getOperand(5), // SWZ_Y
1731 N->getOperand(6), // SWZ_Z
1732 N->getOperand(7) // SWZ_W
1735 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1736 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
1738 case AMDGPUISD::TEXTURE_FETCH: {
1739 SDValue Arg = N->getOperand(1);
1740 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1743 SDValue NewArgs[19] = {
1764 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1765 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1773 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
1774 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
1775 const R600InstrInfo *TII =
1776 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1777 if (!Src.isMachineOpcode())
1779 switch (Src.getMachineOpcode()) {
1780 case AMDGPU::FNEG_R600:
1783 Src = Src.getOperand(0);
1784 Neg = DAG.getTargetConstant(1, MVT::i32);
1786 case AMDGPU::FABS_R600:
1789 Src = Src.getOperand(0);
1790 Abs = DAG.getTargetConstant(1, MVT::i32);
1792 case AMDGPU::CONST_COPY: {
1793 unsigned Opcode = ParentNode->getMachineOpcode();
1794 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1799 SDValue CstOffset = Src.getOperand(0);
1800 if (ParentNode->getValueType(0).isVector())
1803 // Gather constants values
1804 int SrcIndices[] = {
1805 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1806 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1807 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1808 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1809 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1810 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1811 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1812 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1813 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1814 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1815 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1817 std::vector<unsigned> Consts;
1818 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
1819 int OtherSrcIdx = SrcIndices[i];
1820 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1821 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1827 if (RegisterSDNode *Reg =
1828 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1829 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1830 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(
1831 ParentNode->getOperand(OtherSelIdx));
1832 Consts.push_back(Cst->getZExtValue());
1837 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
1838 Consts.push_back(Cst->getZExtValue());
1839 if (!TII->fitsConstReadLimitations(Consts)) {
1844 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1847 case AMDGPU::MOV_IMM_I32:
1848 case AMDGPU::MOV_IMM_F32: {
1849 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1850 uint64_t ImmValue = 0;
1853 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1854 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1855 float FloatValue = FPC->getValueAPF().convertToFloat();
1856 if (FloatValue == 0.0) {
1857 ImmReg = AMDGPU::ZERO;
1858 } else if (FloatValue == 0.5) {
1859 ImmReg = AMDGPU::HALF;
1860 } else if (FloatValue == 1.0) {
1861 ImmReg = AMDGPU::ONE;
1863 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1866 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1867 uint64_t Value = C->getZExtValue();
1869 ImmReg = AMDGPU::ZERO;
1870 } else if (Value == 1) {
1871 ImmReg = AMDGPU::ONE_INT;
1877 // Check that we aren't already using an immediate.
1878 // XXX: It's possible for an instruction to have more than one
1879 // immediate operand, but this is not supported yet.
1880 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1883 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1885 if (C->getZExtValue())
1887 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1889 Src = DAG.getRegister(ImmReg, MVT::i32);
1898 /// \brief Fold the instructions after selecting them
1899 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1900 SelectionDAG &DAG) const {
1901 const R600InstrInfo *TII =
1902 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1903 if (!Node->isMachineOpcode())
1905 unsigned Opcode = Node->getMachineOpcode();
1908 std::vector<SDValue> Ops;
1909 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1913 if (Opcode == AMDGPU::DOT_4) {
1914 int OperandIdx[] = {
1915 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1916 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1917 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1918 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1919 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1920 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1921 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1922 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1925 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1926 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1927 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1928 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1929 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1930 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1931 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1932 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1935 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1936 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1937 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1938 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1939 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1940 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1941 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1942 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1944 for (unsigned i = 0; i < 8; i++) {
1945 if (OperandIdx[i] < 0)
1947 SDValue &Src = Ops[OperandIdx[i] - 1];
1948 SDValue &Neg = Ops[NegIdx[i] - 1];
1949 SDValue &Abs = Ops[AbsIdx[i] - 1];
1950 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1951 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1954 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1955 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1956 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1958 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1959 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1960 SDValue &Src = Ops[i];
1961 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1962 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1964 } else if (Opcode == AMDGPU::CLAMP_R600) {
1965 SDValue Src = Node->getOperand(0);
1966 if (!Src.isMachineOpcode() ||
1967 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1969 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1970 AMDGPU::OpName::clamp);
1973 std::vector<SDValue> Ops;
1974 unsigned NumOp = Src.getNumOperands();
1975 for(unsigned i = 0; i < NumOp; ++i)
1976 Ops.push_back(Src.getOperand(i));
1977 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1978 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1979 Node->getVTList(), Ops);
1981 if (!TII->hasInstrModifiers(Opcode))
1983 int OperandIdx[] = {
1984 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1985 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1986 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1989 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1990 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1991 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1994 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1995 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1998 for (unsigned i = 0; i < 3; i++) {
1999 if (OperandIdx[i] < 0)
2001 SDValue &Src = Ops[OperandIdx[i] - 1];
2002 SDValue &Neg = Ops[NegIdx[i] - 1];
2004 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2005 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2006 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2007 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2012 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2013 SDValue &Imm = Ops[ImmIdx];
2014 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2015 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);