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 "AMDILIntrinsicInfo.h"
17 #include "AMDGPUFrameLowering.h"
18 #include "AMDGPUSubtarget.h"
19 #include "R600Defines.h"
20 #include "R600InstrInfo.h"
21 #include "R600MachineFunctionInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/IR/Argument.h"
28 #include "llvm/IR/Function.h"
32 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
33 AMDGPUTargetLowering(TM),
34 Gen(TM.getSubtarget<AMDGPUSubtarget>().getGeneration()) {
35 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
36 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
37 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
38 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
39 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
40 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
42 computeRegisterProperties();
44 // Set condition code actions
45 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
46 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
47 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
49 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
50 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
51 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
52 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
53 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
54 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
55 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
56 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
58 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
59 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
60 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
61 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
63 setOperationAction(ISD::FCOS, MVT::f32, Custom);
64 setOperationAction(ISD::FSIN, MVT::f32, Custom);
66 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
67 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
69 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
70 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
72 setOperationAction(ISD::FSUB, MVT::f32, Expand);
74 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
75 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
76 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
78 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
79 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
81 setOperationAction(ISD::SETCC, MVT::i32, Expand);
82 setOperationAction(ISD::SETCC, MVT::f32, Expand);
83 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
85 setOperationAction(ISD::SELECT, MVT::i32, Expand);
86 setOperationAction(ISD::SELECT, MVT::f32, Expand);
87 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
88 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
90 // Expand sign extension of vectors
91 if (!Subtarget->hasBFE())
92 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
94 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
95 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
97 if (!Subtarget->hasBFE())
98 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
99 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
100 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
102 if (!Subtarget->hasBFE())
103 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
104 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
107 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
109 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
111 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
114 // Legalize loads and stores to the private address space.
115 setOperationAction(ISD::LOAD, MVT::i32, Custom);
116 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
117 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
119 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
120 // spaces, so it is custom lowered to handle those where it isn't.
121 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
122 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
123 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
124 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
125 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
126 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
128 setOperationAction(ISD::STORE, MVT::i8, Custom);
129 setOperationAction(ISD::STORE, MVT::i32, Custom);
130 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
131 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
132 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
133 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
135 setOperationAction(ISD::LOAD, MVT::i32, Custom);
136 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
137 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
139 setTargetDAGCombine(ISD::FP_ROUND);
140 setTargetDAGCombine(ISD::FP_TO_SINT);
141 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
142 setTargetDAGCombine(ISD::SELECT_CC);
143 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
145 // These should be replaced by UDVIREM, but it does not happen automatically
146 // during Type Legalization
147 setOperationAction(ISD::UDIV, MVT::i64, Custom);
148 setOperationAction(ISD::UREM, MVT::i64, Custom);
150 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
152 setBooleanContents(ZeroOrNegativeOneBooleanContent);
153 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
154 setSchedulingPreference(Sched::Source);
157 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
158 MachineInstr * MI, MachineBasicBlock * BB) const {
159 MachineFunction * MF = BB->getParent();
160 MachineRegisterInfo &MRI = MF->getRegInfo();
161 MachineBasicBlock::iterator I = *MI;
162 const R600InstrInfo *TII =
163 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
165 switch (MI->getOpcode()) {
167 // Replace LDS_*_RET instruction that don't have any uses with the
168 // equivalent LDS_*_NORET instruction.
169 if (TII->isLDSRetInstr(MI->getOpcode())) {
170 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
171 assert(DstIdx != -1);
172 MachineInstrBuilder NewMI;
173 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()))
176 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
177 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
178 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
179 NewMI.addOperand(MI->getOperand(i));
182 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
185 case AMDGPU::CLAMP_R600: {
186 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
188 MI->getOperand(0).getReg(),
189 MI->getOperand(1).getReg());
190 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
194 case AMDGPU::FABS_R600: {
195 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
197 MI->getOperand(0).getReg(),
198 MI->getOperand(1).getReg());
199 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
203 case AMDGPU::FNEG_R600: {
204 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
206 MI->getOperand(0).getReg(),
207 MI->getOperand(1).getReg());
208 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
212 case AMDGPU::MASK_WRITE: {
213 unsigned maskedRegister = MI->getOperand(0).getReg();
214 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
215 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
216 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
220 case AMDGPU::MOV_IMM_F32:
221 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
222 MI->getOperand(1).getFPImm()->getValueAPF()
223 .bitcastToAPInt().getZExtValue());
225 case AMDGPU::MOV_IMM_I32:
226 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
227 MI->getOperand(1).getImm());
229 case AMDGPU::CONST_COPY: {
230 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
231 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
232 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
233 MI->getOperand(1).getImm());
237 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
238 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
239 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
240 unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
242 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
243 .addOperand(MI->getOperand(0))
244 .addOperand(MI->getOperand(1))
245 .addImm(EOP); // Set End of program bit
250 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
251 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
252 MachineOperand &RID = MI->getOperand(4);
253 MachineOperand &SID = MI->getOperand(5);
254 unsigned TextureId = MI->getOperand(6).getImm();
255 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
256 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
268 case 8: // ShadowRect
279 case 11: // Shadow1DArray
283 case 12: // Shadow2DArray
287 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
288 .addOperand(MI->getOperand(3))
306 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
307 .addOperand(MI->getOperand(2))
325 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
326 .addOperand(MI->getOperand(0))
327 .addOperand(MI->getOperand(1))
345 .addReg(T0, RegState::Implicit)
346 .addReg(T1, RegState::Implicit);
350 case AMDGPU::TXD_SHADOW: {
351 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
352 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
353 MachineOperand &RID = MI->getOperand(4);
354 MachineOperand &SID = MI->getOperand(5);
355 unsigned TextureId = MI->getOperand(6).getImm();
356 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
357 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
369 case 8: // ShadowRect
380 case 11: // Shadow1DArray
384 case 12: // Shadow2DArray
389 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
390 .addOperand(MI->getOperand(3))
408 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
409 .addOperand(MI->getOperand(2))
427 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
428 .addOperand(MI->getOperand(0))
429 .addOperand(MI->getOperand(1))
447 .addReg(T0, RegState::Implicit)
448 .addReg(T1, RegState::Implicit);
453 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
454 .addOperand(MI->getOperand(0));
457 case AMDGPU::BRANCH_COND_f32: {
458 MachineInstr *NewMI =
459 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
460 AMDGPU::PREDICATE_BIT)
461 .addOperand(MI->getOperand(1))
462 .addImm(OPCODE_IS_NOT_ZERO)
464 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
465 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
466 .addOperand(MI->getOperand(0))
467 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
471 case AMDGPU::BRANCH_COND_i32: {
472 MachineInstr *NewMI =
473 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
474 AMDGPU::PREDICATE_BIT)
475 .addOperand(MI->getOperand(1))
476 .addImm(OPCODE_IS_NOT_ZERO_INT)
478 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
479 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
480 .addOperand(MI->getOperand(0))
481 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
485 case AMDGPU::EG_ExportSwz:
486 case AMDGPU::R600_ExportSwz: {
487 // Instruction is left unmodified if its not the last one of its type
488 bool isLastInstructionOfItsType = true;
489 unsigned InstExportType = MI->getOperand(1).getImm();
490 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
491 EndBlock = BB->end(); NextExportInst != EndBlock;
492 NextExportInst = std::next(NextExportInst)) {
493 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
494 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
495 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
497 if (CurrentInstExportType == InstExportType) {
498 isLastInstructionOfItsType = false;
503 bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
504 if (!EOP && !isLastInstructionOfItsType)
506 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
507 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
508 .addOperand(MI->getOperand(0))
509 .addOperand(MI->getOperand(1))
510 .addOperand(MI->getOperand(2))
511 .addOperand(MI->getOperand(3))
512 .addOperand(MI->getOperand(4))
513 .addOperand(MI->getOperand(5))
514 .addOperand(MI->getOperand(6))
519 case AMDGPU::RETURN: {
520 // RETURN instructions must have the live-out registers as implicit uses,
521 // otherwise they appear dead.
522 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
523 MachineInstrBuilder MIB(*MF, MI);
524 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
525 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
530 MI->eraseFromParent();
534 //===----------------------------------------------------------------------===//
535 // Custom DAG Lowering Operations
536 //===----------------------------------------------------------------------===//
538 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
539 MachineFunction &MF = DAG.getMachineFunction();
540 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
541 switch (Op.getOpcode()) {
542 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
544 case ISD::FSIN: return LowerTrig(Op, DAG);
545 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
546 case ISD::STORE: return LowerSTORE(Op, DAG);
547 case ISD::LOAD: return LowerLOAD(Op, DAG);
548 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
549 case ISD::INTRINSIC_VOID: {
550 SDValue Chain = Op.getOperand(0);
551 unsigned IntrinsicID =
552 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
553 switch (IntrinsicID) {
554 case AMDGPUIntrinsic::AMDGPU_store_output: {
555 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
556 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
557 MFI->LiveOuts.push_back(Reg);
558 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
560 case AMDGPUIntrinsic::R600_store_swizzle: {
561 const SDValue Args[8] = {
563 Op.getOperand(2), // Export Value
564 Op.getOperand(3), // ArrayBase
565 Op.getOperand(4), // Type
566 DAG.getConstant(0, MVT::i32), // SWZ_X
567 DAG.getConstant(1, MVT::i32), // SWZ_Y
568 DAG.getConstant(2, MVT::i32), // SWZ_Z
569 DAG.getConstant(3, MVT::i32) // SWZ_W
571 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(), Args);
574 // default for switch(IntrinsicID)
577 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
580 case ISD::INTRINSIC_WO_CHAIN: {
581 unsigned IntrinsicID =
582 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
583 EVT VT = Op.getValueType();
585 switch(IntrinsicID) {
586 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
587 case AMDGPUIntrinsic::R600_load_input: {
588 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
589 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
590 MachineFunction &MF = DAG.getMachineFunction();
591 MachineRegisterInfo &MRI = MF.getRegInfo();
593 return DAG.getCopyFromReg(DAG.getEntryNode(),
594 SDLoc(DAG.getEntryNode()), Reg, VT);
597 case AMDGPUIntrinsic::R600_interp_input: {
598 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
599 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
600 MachineSDNode *interp;
602 const MachineFunction &MF = DAG.getMachineFunction();
603 const R600InstrInfo *TII =
604 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
605 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
606 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
607 return DAG.getTargetExtractSubreg(
608 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
609 DL, MVT::f32, SDValue(interp, 0));
611 MachineFunction &MF = DAG.getMachineFunction();
612 MachineRegisterInfo &MRI = MF.getRegInfo();
613 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
614 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
615 MRI.addLiveIn(RegisterI);
616 MRI.addLiveIn(RegisterJ);
617 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
618 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
619 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
620 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
623 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
624 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
625 RegisterJNode, RegisterINode);
627 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
628 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
629 RegisterJNode, RegisterINode);
630 return SDValue(interp, slot % 2);
632 case AMDGPUIntrinsic::R600_interp_xy:
633 case AMDGPUIntrinsic::R600_interp_zw: {
634 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
635 MachineSDNode *interp;
636 SDValue RegisterINode = Op.getOperand(2);
637 SDValue RegisterJNode = Op.getOperand(3);
639 if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
640 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
641 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
642 RegisterJNode, RegisterINode);
644 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
645 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
646 RegisterJNode, RegisterINode);
647 return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
648 SDValue(interp, 0), SDValue(interp, 1));
650 case AMDGPUIntrinsic::R600_tex:
651 case AMDGPUIntrinsic::R600_texc:
652 case AMDGPUIntrinsic::R600_txl:
653 case AMDGPUIntrinsic::R600_txlc:
654 case AMDGPUIntrinsic::R600_txb:
655 case AMDGPUIntrinsic::R600_txbc:
656 case AMDGPUIntrinsic::R600_txf:
657 case AMDGPUIntrinsic::R600_txq:
658 case AMDGPUIntrinsic::R600_ddx:
659 case AMDGPUIntrinsic::R600_ddy:
660 case AMDGPUIntrinsic::R600_ldptr: {
662 switch (IntrinsicID) {
663 case AMDGPUIntrinsic::R600_tex:
666 case AMDGPUIntrinsic::R600_texc:
669 case AMDGPUIntrinsic::R600_txl:
672 case AMDGPUIntrinsic::R600_txlc:
675 case AMDGPUIntrinsic::R600_txb:
678 case AMDGPUIntrinsic::R600_txbc:
681 case AMDGPUIntrinsic::R600_txf:
684 case AMDGPUIntrinsic::R600_txq:
687 case AMDGPUIntrinsic::R600_ddx:
690 case AMDGPUIntrinsic::R600_ddy:
693 case AMDGPUIntrinsic::R600_ldptr:
697 llvm_unreachable("Unknow Texture Operation");
700 SDValue TexArgs[19] = {
701 DAG.getConstant(TextureOp, MVT::i32),
703 DAG.getConstant(0, MVT::i32),
704 DAG.getConstant(1, MVT::i32),
705 DAG.getConstant(2, MVT::i32),
706 DAG.getConstant(3, MVT::i32),
710 DAG.getConstant(0, MVT::i32),
711 DAG.getConstant(1, MVT::i32),
712 DAG.getConstant(2, MVT::i32),
713 DAG.getConstant(3, MVT::i32),
721 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
723 case AMDGPUIntrinsic::AMDGPU_dp4: {
725 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
726 DAG.getConstant(0, MVT::i32)),
727 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
728 DAG.getConstant(0, MVT::i32)),
729 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
730 DAG.getConstant(1, MVT::i32)),
731 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
732 DAG.getConstant(1, MVT::i32)),
733 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
734 DAG.getConstant(2, MVT::i32)),
735 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
736 DAG.getConstant(2, MVT::i32)),
737 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
738 DAG.getConstant(3, MVT::i32)),
739 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
740 DAG.getConstant(3, MVT::i32))
742 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
745 case Intrinsic::r600_read_ngroups_x:
746 return LowerImplicitParameter(DAG, VT, DL, 0);
747 case Intrinsic::r600_read_ngroups_y:
748 return LowerImplicitParameter(DAG, VT, DL, 1);
749 case Intrinsic::r600_read_ngroups_z:
750 return LowerImplicitParameter(DAG, VT, DL, 2);
751 case Intrinsic::r600_read_global_size_x:
752 return LowerImplicitParameter(DAG, VT, DL, 3);
753 case Intrinsic::r600_read_global_size_y:
754 return LowerImplicitParameter(DAG, VT, DL, 4);
755 case Intrinsic::r600_read_global_size_z:
756 return LowerImplicitParameter(DAG, VT, DL, 5);
757 case Intrinsic::r600_read_local_size_x:
758 return LowerImplicitParameter(DAG, VT, DL, 6);
759 case Intrinsic::r600_read_local_size_y:
760 return LowerImplicitParameter(DAG, VT, DL, 7);
761 case Intrinsic::r600_read_local_size_z:
762 return LowerImplicitParameter(DAG, VT, DL, 8);
764 case Intrinsic::r600_read_tgid_x:
765 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
767 case Intrinsic::r600_read_tgid_y:
768 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
770 case Intrinsic::r600_read_tgid_z:
771 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
773 case Intrinsic::r600_read_tidig_x:
774 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
776 case Intrinsic::r600_read_tidig_y:
777 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
779 case Intrinsic::r600_read_tidig_z:
780 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
783 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
786 } // end switch(Op.getOpcode())
790 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
791 SmallVectorImpl<SDValue> &Results,
792 SelectionDAG &DAG) const {
793 switch (N->getOpcode()) {
795 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
797 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
800 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
801 Results.push_back(SDValue(Node, 0));
802 Results.push_back(SDValue(Node, 1));
803 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
805 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
809 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
810 Results.push_back(SDValue(Node, 0));
815 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
816 // On hw >= R700, COS/SIN input must be between -1. and 1.
817 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
818 EVT VT = Op.getValueType();
819 SDValue Arg = Op.getOperand(0);
820 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
821 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
822 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
823 DAG.getConstantFP(0.15915494309, MVT::f32)),
824 DAG.getConstantFP(0.5, MVT::f32)));
826 switch (Op.getOpcode()) {
828 TrigNode = AMDGPUISD::COS_HW;
831 TrigNode = AMDGPUISD::SIN_HW;
834 llvm_unreachable("Wrong trig opcode");
836 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
837 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
838 DAG.getConstantFP(-0.5, MVT::f32)));
839 if (Gen >= AMDGPUSubtarget::R700)
841 // On R600 hw, COS/SIN input must be between -Pi and Pi.
842 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
843 DAG.getConstantFP(3.14159265359, MVT::f32));
846 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
851 Op, DAG.getConstantFP(0.0f, MVT::f32),
852 DAG.getCondCode(ISD::SETNE)
856 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
858 unsigned DwordOffset) const {
859 unsigned ByteOffset = DwordOffset * 4;
860 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
861 AMDGPUAS::CONSTANT_BUFFER_0);
863 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
864 assert(isInt<16>(ByteOffset));
866 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
867 DAG.getConstant(ByteOffset, MVT::i32), // PTR
868 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
869 false, false, false, 0);
872 bool R600TargetLowering::isZero(SDValue Op) const {
873 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
874 return Cst->isNullValue();
875 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
876 return CstFP->isZero();
882 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
884 EVT VT = Op.getValueType();
886 SDValue LHS = Op.getOperand(0);
887 SDValue RHS = Op.getOperand(1);
888 SDValue True = Op.getOperand(2);
889 SDValue False = Op.getOperand(3);
890 SDValue CC = Op.getOperand(4);
893 // LHS and RHS are guaranteed to be the same value type
894 EVT CompareVT = LHS.getValueType();
896 // Check if we can lower this to a native operation.
898 // Try to lower to a SET* instruction:
900 // SET* can match the following patterns:
902 // select_cc f32, f32, -1, 0, cc_supported
903 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
904 // select_cc i32, i32, -1, 0, cc_supported
907 // Move hardware True/False values to the correct operand.
908 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
909 ISD::CondCode InverseCC =
910 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
911 if (isHWTrueValue(False) && isHWFalseValue(True)) {
912 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
913 std::swap(False, True);
914 CC = DAG.getCondCode(InverseCC);
916 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
917 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
918 std::swap(False, True);
920 CC = DAG.getCondCode(SwapInvCC);
925 if (isHWTrueValue(True) && isHWFalseValue(False) &&
926 (CompareVT == VT || VT == MVT::i32)) {
927 // This can be matched by a SET* instruction.
928 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
931 // Try to lower to a CND* instruction:
933 // CND* can match the following patterns:
935 // select_cc f32, 0.0, f32, f32, cc_supported
936 // select_cc f32, 0.0, i32, i32, cc_supported
937 // select_cc i32, 0, f32, f32, cc_supported
938 // select_cc i32, 0, i32, i32, cc_supported
941 // Try to move the zero value to the RHS
943 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
944 // Try swapping the operands
945 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
946 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
948 CC = DAG.getCondCode(CCSwapped);
950 // Try inverting the conditon and then swapping the operands
951 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
952 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
953 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
954 std::swap(True, False);
956 CC = DAG.getCondCode(CCSwapped);
963 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
964 if (CompareVT != VT) {
965 // Bitcast True / False to the correct types. This will end up being
966 // a nop, but it allows us to define only a single pattern in the
967 // .TD files for each CND* instruction rather than having to have
968 // one pattern for integer True/False and one for fp True/False
969 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
970 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
977 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
985 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
988 DAG.getCondCode(CCOpcode));
989 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
992 // If we make it this for it means we have no native instructions to handle
993 // this SELECT_CC, so we must lower it.
994 SDValue HWTrue, HWFalse;
996 if (CompareVT == MVT::f32) {
997 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
998 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
999 } else if (CompareVT == MVT::i32) {
1000 HWTrue = DAG.getConstant(-1, CompareVT);
1001 HWFalse = DAG.getConstant(0, CompareVT);
1004 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1007 // Lower this unsupported SELECT_CC into a combination of two supported
1008 // SELECT_CC operations.
1009 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1011 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1014 DAG.getCondCode(ISD::SETNE));
1017 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to
1018 /// convert these pointers to a register index. Each register holds
1019 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1020 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1021 /// for indirect addressing.
1022 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1023 unsigned StackWidth,
1024 SelectionDAG &DAG) const {
1026 switch(StackWidth) {
1036 default: llvm_unreachable("Invalid stack width");
1039 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1040 DAG.getConstant(SRLPad, MVT::i32));
1043 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1046 unsigned &PtrIncr) const {
1047 switch (StackWidth) {
1058 Channel = ElemIdx % 2;
1072 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1074 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1075 SDValue Chain = Op.getOperand(0);
1076 SDValue Value = Op.getOperand(1);
1077 SDValue Ptr = Op.getOperand(2);
1079 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1080 if (Result.getNode()) {
1084 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1085 if (StoreNode->isTruncatingStore()) {
1086 EVT VT = Value.getValueType();
1087 assert(VT.bitsLE(MVT::i32));
1088 EVT MemVT = StoreNode->getMemoryVT();
1089 SDValue MaskConstant;
1090 if (MemVT == MVT::i8) {
1091 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1093 assert(MemVT == MVT::i16);
1094 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1096 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1097 DAG.getConstant(2, MVT::i32));
1098 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1099 DAG.getConstant(0x00000003, VT));
1100 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1101 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1102 DAG.getConstant(3, VT));
1103 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1104 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1105 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1109 DAG.getConstant(0, MVT::i32),
1110 DAG.getConstant(0, MVT::i32),
1113 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1114 SDValue Args[3] = { Chain, Input, DWordAddr };
1115 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1116 Op->getVTList(), Args, MemVT,
1117 StoreNode->getMemOperand());
1118 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1119 Value.getValueType().bitsGE(MVT::i32)) {
1120 // Convert pointer from byte address to dword address.
1121 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1122 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1123 Ptr, DAG.getConstant(2, MVT::i32)));
1125 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1126 llvm_unreachable("Truncated and indexed stores not supported yet");
1128 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1134 EVT ValueVT = Value.getValueType();
1136 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1140 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1141 if (Ret.getNode()) {
1144 // Lowering for indirect addressing
1146 const MachineFunction &MF = DAG.getMachineFunction();
1147 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1148 getTargetMachine().getFrameLowering());
1149 unsigned StackWidth = TFL->getStackWidth(MF);
1151 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1153 if (ValueVT.isVector()) {
1154 unsigned NumElemVT = ValueVT.getVectorNumElements();
1155 EVT ElemVT = ValueVT.getVectorElementType();
1156 SmallVector<SDValue, 4> Stores(NumElemVT);
1158 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1159 "vector width in load");
1161 for (unsigned i = 0; i < NumElemVT; ++i) {
1162 unsigned Channel, PtrIncr;
1163 getStackAddress(StackWidth, i, Channel, PtrIncr);
1164 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1165 DAG.getConstant(PtrIncr, MVT::i32));
1166 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1167 Value, DAG.getConstant(i, MVT::i32));
1169 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1171 DAG.getTargetConstant(Channel, MVT::i32));
1173 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1175 if (ValueVT == MVT::i8) {
1176 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1178 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1179 DAG.getTargetConstant(0, MVT::i32)); // Channel
1185 // return (512 + (kc_bank << 12)
1187 ConstantAddressBlock(unsigned AddressSpace) {
1188 switch (AddressSpace) {
1189 case AMDGPUAS::CONSTANT_BUFFER_0:
1191 case AMDGPUAS::CONSTANT_BUFFER_1:
1193 case AMDGPUAS::CONSTANT_BUFFER_2:
1194 return 512 + 4096 * 2;
1195 case AMDGPUAS::CONSTANT_BUFFER_3:
1196 return 512 + 4096 * 3;
1197 case AMDGPUAS::CONSTANT_BUFFER_4:
1198 return 512 + 4096 * 4;
1199 case AMDGPUAS::CONSTANT_BUFFER_5:
1200 return 512 + 4096 * 5;
1201 case AMDGPUAS::CONSTANT_BUFFER_6:
1202 return 512 + 4096 * 6;
1203 case AMDGPUAS::CONSTANT_BUFFER_7:
1204 return 512 + 4096 * 7;
1205 case AMDGPUAS::CONSTANT_BUFFER_8:
1206 return 512 + 4096 * 8;
1207 case AMDGPUAS::CONSTANT_BUFFER_9:
1208 return 512 + 4096 * 9;
1209 case AMDGPUAS::CONSTANT_BUFFER_10:
1210 return 512 + 4096 * 10;
1211 case AMDGPUAS::CONSTANT_BUFFER_11:
1212 return 512 + 4096 * 11;
1213 case AMDGPUAS::CONSTANT_BUFFER_12:
1214 return 512 + 4096 * 12;
1215 case AMDGPUAS::CONSTANT_BUFFER_13:
1216 return 512 + 4096 * 13;
1217 case AMDGPUAS::CONSTANT_BUFFER_14:
1218 return 512 + 4096 * 14;
1219 case AMDGPUAS::CONSTANT_BUFFER_15:
1220 return 512 + 4096 * 15;
1226 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1228 EVT VT = Op.getValueType();
1230 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1231 SDValue Chain = Op.getOperand(0);
1232 SDValue Ptr = Op.getOperand(1);
1233 SDValue LoweredLoad;
1235 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1236 if (Ret.getNode()) {
1241 return DAG.getMergeValues(Ops, DL);
1245 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1246 SDValue MergedValues[2] = {
1247 SplitVectorLoad(Op, DAG),
1250 return DAG.getMergeValues(MergedValues, DL);
1253 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1254 if (ConstantBlock > -1 &&
1255 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1256 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1258 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1259 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1260 isa<ConstantSDNode>(Ptr)) {
1262 for (unsigned i = 0; i < 4; i++) {
1263 // We want Const position encoded with the following formula :
1264 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1265 // const_index is Ptr computed by llvm using an alignment of 16.
1266 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1267 // then div by 4 at the ISel step
1268 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1269 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1270 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1272 EVT NewVT = MVT::v4i32;
1273 unsigned NumElements = 4;
1274 if (VT.isVector()) {
1276 NumElements = VT.getVectorNumElements();
1278 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1279 makeArrayRef(Slots, NumElements));
1281 // non-constant ptr can't be folded, keeps it as a v4f32 load
1282 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1283 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1284 DAG.getConstant(LoadNode->getAddressSpace() -
1285 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1289 if (!VT.isVector()) {
1290 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1291 DAG.getConstant(0, MVT::i32));
1294 SDValue MergedValues[2] = {
1298 return DAG.getMergeValues(MergedValues, DL);
1301 // For most operations returning SDValue() will result in the node being
1302 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1303 // need to manually expand loads that may be legal in some address spaces and
1304 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1305 // compute shaders, since the data is sign extended when it is uploaded to the
1306 // buffer. However SEXT loads from other address spaces are not supported, so
1307 // we need to expand them here.
1308 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1309 EVT MemVT = LoadNode->getMemoryVT();
1310 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1311 SDValue ShiftAmount =
1312 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1313 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1314 LoadNode->getPointerInfo(), MemVT,
1315 LoadNode->isVolatile(),
1316 LoadNode->isNonTemporal(),
1317 LoadNode->getAlignment());
1318 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1319 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1321 SDValue MergedValues[2] = { Sra, Chain };
1322 return DAG.getMergeValues(MergedValues, DL);
1325 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1329 // Lowering for indirect addressing
1330 const MachineFunction &MF = DAG.getMachineFunction();
1331 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1332 getTargetMachine().getFrameLowering());
1333 unsigned StackWidth = TFL->getStackWidth(MF);
1335 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1337 if (VT.isVector()) {
1338 unsigned NumElemVT = VT.getVectorNumElements();
1339 EVT ElemVT = VT.getVectorElementType();
1342 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1343 "vector width in load");
1345 for (unsigned i = 0; i < NumElemVT; ++i) {
1346 unsigned Channel, PtrIncr;
1347 getStackAddress(StackWidth, i, Channel, PtrIncr);
1348 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1349 DAG.getConstant(PtrIncr, MVT::i32));
1350 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1352 DAG.getTargetConstant(Channel, MVT::i32),
1355 for (unsigned i = NumElemVT; i < 4; ++i) {
1356 Loads[i] = DAG.getUNDEF(ElemVT);
1358 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1359 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1361 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1363 DAG.getTargetConstant(0, MVT::i32), // Channel
1372 return DAG.getMergeValues(Ops, DL);
1375 /// XXX Only kernel functions are supported, so we can assume for now that
1376 /// every function is a kernel function, but in the future we should use
1377 /// separate calling conventions for kernel and non-kernel functions.
1378 SDValue R600TargetLowering::LowerFormalArguments(
1380 CallingConv::ID CallConv,
1382 const SmallVectorImpl<ISD::InputArg> &Ins,
1383 SDLoc DL, SelectionDAG &DAG,
1384 SmallVectorImpl<SDValue> &InVals) const {
1385 SmallVector<CCValAssign, 16> ArgLocs;
1386 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1387 getTargetMachine(), ArgLocs, *DAG.getContext());
1388 MachineFunction &MF = DAG.getMachineFunction();
1389 unsigned ShaderType = MF.getInfo<R600MachineFunctionInfo>()->ShaderType;
1391 SmallVector<ISD::InputArg, 8> LocalIns;
1393 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1395 AnalyzeFormalArguments(CCInfo, LocalIns);
1397 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1398 CCValAssign &VA = ArgLocs[i];
1400 EVT MemVT = LocalIns[i].VT;
1402 if (ShaderType != ShaderType::COMPUTE) {
1403 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1404 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1405 InVals.push_back(Register);
1409 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1410 AMDGPUAS::CONSTANT_BUFFER_0);
1412 // i64 isn't a legal type, so the register type used ends up as i32, which
1413 // isn't expected here. It attempts to create this sextload, but it ends up
1414 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1417 // The first 36 bytes of the input buffer contains information about
1418 // thread group and global sizes.
1420 // FIXME: This should really check the extload type, but the handling of
1421 // extload vecto parameters seems to be broken.
1422 //ISD::LoadExtType Ext = Ins[i].Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1423 ISD::LoadExtType Ext = ISD::SEXTLOAD;
1424 SDValue Arg = DAG.getExtLoad(Ext, DL, VT, Chain,
1425 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1426 MachinePointerInfo(UndefValue::get(PtrTy)),
1427 MemVT, false, false, 4);
1429 // 4 is the preferred alignment for the CONSTANT memory space.
1430 InVals.push_back(Arg);
1435 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1438 return VT.changeVectorElementTypeToInteger();
1441 static SDValue CompactSwizzlableVector(
1442 SelectionDAG &DAG, SDValue VectorEntry,
1443 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1444 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1445 assert(RemapSwizzle.empty());
1446 SDValue NewBldVec[4] = {
1447 VectorEntry.getOperand(0),
1448 VectorEntry.getOperand(1),
1449 VectorEntry.getOperand(2),
1450 VectorEntry.getOperand(3)
1453 for (unsigned i = 0; i < 4; i++) {
1454 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1455 // We mask write here to teach later passes that the ith element of this
1456 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1457 // break false dependencies and additionnaly make assembly easier to read.
1458 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1459 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1461 RemapSwizzle[i] = 4; // SEL_0
1462 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1463 } else if (C->isExactlyValue(1.0)) {
1464 RemapSwizzle[i] = 5; // SEL_1
1465 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1469 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1471 for (unsigned j = 0; j < i; j++) {
1472 if (NewBldVec[i] == NewBldVec[j]) {
1473 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1474 RemapSwizzle[i] = j;
1480 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1481 VectorEntry.getValueType(), NewBldVec);
1484 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1485 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1486 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1487 assert(RemapSwizzle.empty());
1488 SDValue NewBldVec[4] = {
1489 VectorEntry.getOperand(0),
1490 VectorEntry.getOperand(1),
1491 VectorEntry.getOperand(2),
1492 VectorEntry.getOperand(3)
1494 bool isUnmovable[4] = { false, false, false, false };
1495 for (unsigned i = 0; i < 4; i++) {
1496 RemapSwizzle[i] = i;
1497 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1498 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1501 isUnmovable[Idx] = true;
1505 for (unsigned i = 0; i < 4; i++) {
1506 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1507 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1509 if (isUnmovable[Idx])
1512 std::swap(NewBldVec[Idx], NewBldVec[i]);
1513 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1518 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1519 VectorEntry.getValueType(), NewBldVec);
1523 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1524 SDValue Swz[4], SelectionDAG &DAG) const {
1525 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1526 // Old -> New swizzle values
1527 DenseMap<unsigned, unsigned> SwizzleRemap;
1529 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1530 for (unsigned i = 0; i < 4; i++) {
1531 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1532 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1533 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1536 SwizzleRemap.clear();
1537 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1538 for (unsigned i = 0; i < 4; i++) {
1539 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1540 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1541 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1548 //===----------------------------------------------------------------------===//
1549 // Custom DAG Optimizations
1550 //===----------------------------------------------------------------------===//
1552 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1553 DAGCombinerInfo &DCI) const {
1554 SelectionDAG &DAG = DCI.DAG;
1556 switch (N->getOpcode()) {
1557 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1558 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1559 case ISD::FP_ROUND: {
1560 SDValue Arg = N->getOperand(0);
1561 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1562 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1568 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1569 // (i32 select_cc f32, f32, -1, 0 cc)
1571 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1572 // this to one of the SET*_DX10 instructions.
1573 case ISD::FP_TO_SINT: {
1574 SDValue FNeg = N->getOperand(0);
1575 if (FNeg.getOpcode() != ISD::FNEG) {
1578 SDValue SelectCC = FNeg.getOperand(0);
1579 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1580 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1581 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1582 !isHWTrueValue(SelectCC.getOperand(2)) ||
1583 !isHWFalseValue(SelectCC.getOperand(3))) {
1587 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1588 SelectCC.getOperand(0), // LHS
1589 SelectCC.getOperand(1), // RHS
1590 DAG.getConstant(-1, MVT::i32), // True
1591 DAG.getConstant(0, MVT::i32), // Flase
1592 SelectCC.getOperand(4)); // CC
1597 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1598 // => build_vector elt0, ... , NewEltIdx, ... , eltN
1599 case ISD::INSERT_VECTOR_ELT: {
1600 SDValue InVec = N->getOperand(0);
1601 SDValue InVal = N->getOperand(1);
1602 SDValue EltNo = N->getOperand(2);
1605 // If the inserted element is an UNDEF, just use the input vector.
1606 if (InVal.getOpcode() == ISD::UNDEF)
1609 EVT VT = InVec.getValueType();
1611 // If we can't generate a legal BUILD_VECTOR, exit
1612 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1615 // Check that we know which element is being inserted
1616 if (!isa<ConstantSDNode>(EltNo))
1618 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1620 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1621 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1623 SmallVector<SDValue, 8> Ops;
1624 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1625 Ops.append(InVec.getNode()->op_begin(),
1626 InVec.getNode()->op_end());
1627 } else if (InVec.getOpcode() == ISD::UNDEF) {
1628 unsigned NElts = VT.getVectorNumElements();
1629 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1634 // Insert the element
1635 if (Elt < Ops.size()) {
1636 // All the operands of BUILD_VECTOR must have the same type;
1637 // we enforce that here.
1638 EVT OpVT = Ops[0].getValueType();
1639 if (InVal.getValueType() != OpVT)
1640 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1641 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1642 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1646 // Return the new vector
1647 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1650 // Extract_vec (Build_vector) generated by custom lowering
1651 // also needs to be customly combined
1652 case ISD::EXTRACT_VECTOR_ELT: {
1653 SDValue Arg = N->getOperand(0);
1654 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1655 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1656 unsigned Element = Const->getZExtValue();
1657 return Arg->getOperand(Element);
1660 if (Arg.getOpcode() == ISD::BITCAST &&
1661 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1662 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1663 unsigned Element = Const->getZExtValue();
1664 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1665 Arg->getOperand(0).getOperand(Element));
1670 case ISD::SELECT_CC: {
1671 // Try common optimizations
1672 SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1676 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1677 // selectcc x, y, a, b, inv(cc)
1679 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1680 // selectcc x, y, a, b, cc
1681 SDValue LHS = N->getOperand(0);
1682 if (LHS.getOpcode() != ISD::SELECT_CC) {
1686 SDValue RHS = N->getOperand(1);
1687 SDValue True = N->getOperand(2);
1688 SDValue False = N->getOperand(3);
1689 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1691 if (LHS.getOperand(2).getNode() != True.getNode() ||
1692 LHS.getOperand(3).getNode() != False.getNode() ||
1693 RHS.getNode() != False.getNode()) {
1698 default: return SDValue();
1699 case ISD::SETNE: return LHS;
1701 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1702 LHSCC = ISD::getSetCCInverse(LHSCC,
1703 LHS.getOperand(0).getValueType().isInteger());
1704 if (DCI.isBeforeLegalizeOps() ||
1705 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1706 return DAG.getSelectCC(SDLoc(N),
1718 case AMDGPUISD::EXPORT: {
1719 SDValue Arg = N->getOperand(1);
1720 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1723 SDValue NewArgs[8] = {
1724 N->getOperand(0), // Chain
1726 N->getOperand(2), // ArrayBase
1727 N->getOperand(3), // Type
1728 N->getOperand(4), // SWZ_X
1729 N->getOperand(5), // SWZ_Y
1730 N->getOperand(6), // SWZ_Z
1731 N->getOperand(7) // SWZ_W
1734 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1735 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
1737 case AMDGPUISD::TEXTURE_FETCH: {
1738 SDValue Arg = N->getOperand(1);
1739 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1742 SDValue NewArgs[19] = {
1763 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1764 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1769 return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
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 (int OtherSrcIdx : SrcIndices) {
1819 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1820 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1826 if (RegisterSDNode *Reg =
1827 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1828 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1830 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
1831 Consts.push_back(Cst->getZExtValue());
1836 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
1837 Consts.push_back(Cst->getZExtValue());
1838 if (!TII->fitsConstReadLimitations(Consts)) {
1843 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1846 case AMDGPU::MOV_IMM_I32:
1847 case AMDGPU::MOV_IMM_F32: {
1848 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1849 uint64_t ImmValue = 0;
1852 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1853 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1854 float FloatValue = FPC->getValueAPF().convertToFloat();
1855 if (FloatValue == 0.0) {
1856 ImmReg = AMDGPU::ZERO;
1857 } else if (FloatValue == 0.5) {
1858 ImmReg = AMDGPU::HALF;
1859 } else if (FloatValue == 1.0) {
1860 ImmReg = AMDGPU::ONE;
1862 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1865 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1866 uint64_t Value = C->getZExtValue();
1868 ImmReg = AMDGPU::ZERO;
1869 } else if (Value == 1) {
1870 ImmReg = AMDGPU::ONE_INT;
1876 // Check that we aren't already using an immediate.
1877 // XXX: It's possible for an instruction to have more than one
1878 // immediate operand, but this is not supported yet.
1879 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1882 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1884 if (C->getZExtValue())
1886 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1888 Src = DAG.getRegister(ImmReg, MVT::i32);
1897 /// \brief Fold the instructions after selecting them
1898 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1899 SelectionDAG &DAG) const {
1900 const R600InstrInfo *TII =
1901 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1902 if (!Node->isMachineOpcode())
1904 unsigned Opcode = Node->getMachineOpcode();
1907 std::vector<SDValue> Ops;
1908 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1912 if (Opcode == AMDGPU::DOT_4) {
1913 int OperandIdx[] = {
1914 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1915 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1916 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1917 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1918 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1919 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1920 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1921 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1924 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1925 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1926 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1927 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1928 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1929 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1930 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1931 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1934 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1935 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1936 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1937 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1938 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1939 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1940 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1941 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1943 for (unsigned i = 0; i < 8; i++) {
1944 if (OperandIdx[i] < 0)
1946 SDValue &Src = Ops[OperandIdx[i] - 1];
1947 SDValue &Neg = Ops[NegIdx[i] - 1];
1948 SDValue &Abs = Ops[AbsIdx[i] - 1];
1949 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1950 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1953 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1954 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1955 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1957 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1958 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1959 SDValue &Src = Ops[i];
1960 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1961 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1963 } else if (Opcode == AMDGPU::CLAMP_R600) {
1964 SDValue Src = Node->getOperand(0);
1965 if (!Src.isMachineOpcode() ||
1966 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1968 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1969 AMDGPU::OpName::clamp);
1972 std::vector<SDValue> Ops;
1973 unsigned NumOp = Src.getNumOperands();
1974 for(unsigned i = 0; i < NumOp; ++i)
1975 Ops.push_back(Src.getOperand(i));
1976 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1977 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1978 Node->getVTList(), Ops);
1980 if (!TII->hasInstrModifiers(Opcode))
1982 int OperandIdx[] = {
1983 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1984 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1985 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1988 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1989 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1990 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1993 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1994 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1997 for (unsigned i = 0; i < 3; i++) {
1998 if (OperandIdx[i] < 0)
2000 SDValue &Src = Ops[OperandIdx[i] - 1];
2001 SDValue &Neg = Ops[NegIdx[i] - 1];
2003 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2004 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2005 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2006 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2011 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2012 SDValue &Imm = Ops[ImmIdx];
2013 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2014 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);