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::v4i32, Expand);
87 // Expand sign extension of vectors
88 if (!Subtarget->hasBFE())
89 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
91 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
92 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
94 if (!Subtarget->hasBFE())
95 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
96 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
97 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
99 if (!Subtarget->hasBFE())
100 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
101 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
102 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
104 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
105 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
106 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
108 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
111 // Legalize loads and stores to the private address space.
112 setOperationAction(ISD::LOAD, MVT::i32, Custom);
113 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
114 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
116 // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
117 // spaces, so it is custom lowered to handle those where it isn't.
118 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
119 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
120 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
121 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
122 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
123 setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
125 setOperationAction(ISD::STORE, MVT::i8, Custom);
126 setOperationAction(ISD::STORE, MVT::i32, Custom);
127 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
128 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
129 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
130 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
132 setOperationAction(ISD::LOAD, MVT::i32, Custom);
133 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
134 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
136 setTargetDAGCombine(ISD::FP_ROUND);
137 setTargetDAGCombine(ISD::FP_TO_SINT);
138 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
139 setTargetDAGCombine(ISD::SELECT_CC);
140 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
142 // These should be replaced by UDVIREM, but it does not happen automatically
143 // during Type Legalization
144 setOperationAction(ISD::UDIV, MVT::i64, Custom);
145 setOperationAction(ISD::UREM, MVT::i64, Custom);
147 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
149 setBooleanContents(ZeroOrNegativeOneBooleanContent);
150 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
151 setSchedulingPreference(Sched::Source);
154 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
155 MachineInstr * MI, MachineBasicBlock * BB) const {
156 MachineFunction * MF = BB->getParent();
157 MachineRegisterInfo &MRI = MF->getRegInfo();
158 MachineBasicBlock::iterator I = *MI;
159 const R600InstrInfo *TII =
160 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
162 switch (MI->getOpcode()) {
164 // Replace LDS_*_RET instruction that don't have any uses with the
165 // equivalent LDS_*_NORET instruction.
166 if (TII->isLDSRetInstr(MI->getOpcode())) {
167 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
168 assert(DstIdx != -1);
169 MachineInstrBuilder NewMI;
170 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()))
173 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
174 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
175 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
176 NewMI.addOperand(MI->getOperand(i));
179 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
182 case AMDGPU::CLAMP_R600: {
183 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
185 MI->getOperand(0).getReg(),
186 MI->getOperand(1).getReg());
187 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
191 case AMDGPU::FABS_R600: {
192 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
194 MI->getOperand(0).getReg(),
195 MI->getOperand(1).getReg());
196 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
200 case AMDGPU::FNEG_R600: {
201 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
203 MI->getOperand(0).getReg(),
204 MI->getOperand(1).getReg());
205 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
209 case AMDGPU::MASK_WRITE: {
210 unsigned maskedRegister = MI->getOperand(0).getReg();
211 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
212 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
213 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
217 case AMDGPU::MOV_IMM_F32:
218 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
219 MI->getOperand(1).getFPImm()->getValueAPF()
220 .bitcastToAPInt().getZExtValue());
222 case AMDGPU::MOV_IMM_I32:
223 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
224 MI->getOperand(1).getImm());
226 case AMDGPU::CONST_COPY: {
227 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
228 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
229 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
230 MI->getOperand(1).getImm());
234 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
235 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
236 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
237 unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
239 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
240 .addOperand(MI->getOperand(0))
241 .addOperand(MI->getOperand(1))
242 .addImm(EOP); // Set End of program bit
247 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
248 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
249 MachineOperand &RID = MI->getOperand(4);
250 MachineOperand &SID = MI->getOperand(5);
251 unsigned TextureId = MI->getOperand(6).getImm();
252 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
253 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
265 case 8: // ShadowRect
276 case 11: // Shadow1DArray
280 case 12: // Shadow2DArray
284 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
285 .addOperand(MI->getOperand(3))
303 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
304 .addOperand(MI->getOperand(2))
322 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
323 .addOperand(MI->getOperand(0))
324 .addOperand(MI->getOperand(1))
342 .addReg(T0, RegState::Implicit)
343 .addReg(T1, RegState::Implicit);
347 case AMDGPU::TXD_SHADOW: {
348 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
349 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
350 MachineOperand &RID = MI->getOperand(4);
351 MachineOperand &SID = MI->getOperand(5);
352 unsigned TextureId = MI->getOperand(6).getImm();
353 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
354 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
366 case 8: // ShadowRect
377 case 11: // Shadow1DArray
381 case 12: // Shadow2DArray
386 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
387 .addOperand(MI->getOperand(3))
405 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
406 .addOperand(MI->getOperand(2))
424 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
425 .addOperand(MI->getOperand(0))
426 .addOperand(MI->getOperand(1))
444 .addReg(T0, RegState::Implicit)
445 .addReg(T1, RegState::Implicit);
450 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
451 .addOperand(MI->getOperand(0));
454 case AMDGPU::BRANCH_COND_f32: {
455 MachineInstr *NewMI =
456 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
457 AMDGPU::PREDICATE_BIT)
458 .addOperand(MI->getOperand(1))
459 .addImm(OPCODE_IS_NOT_ZERO)
461 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
462 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
463 .addOperand(MI->getOperand(0))
464 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
468 case AMDGPU::BRANCH_COND_i32: {
469 MachineInstr *NewMI =
470 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
471 AMDGPU::PREDICATE_BIT)
472 .addOperand(MI->getOperand(1))
473 .addImm(OPCODE_IS_NOT_ZERO_INT)
475 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
476 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
477 .addOperand(MI->getOperand(0))
478 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
482 case AMDGPU::EG_ExportSwz:
483 case AMDGPU::R600_ExportSwz: {
484 // Instruction is left unmodified if its not the last one of its type
485 bool isLastInstructionOfItsType = true;
486 unsigned InstExportType = MI->getOperand(1).getImm();
487 for (MachineBasicBlock::iterator NextExportInst = std::next(I),
488 EndBlock = BB->end(); NextExportInst != EndBlock;
489 NextExportInst = std::next(NextExportInst)) {
490 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
491 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
492 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
494 if (CurrentInstExportType == InstExportType) {
495 isLastInstructionOfItsType = false;
500 bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
501 if (!EOP && !isLastInstructionOfItsType)
503 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
504 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
505 .addOperand(MI->getOperand(0))
506 .addOperand(MI->getOperand(1))
507 .addOperand(MI->getOperand(2))
508 .addOperand(MI->getOperand(3))
509 .addOperand(MI->getOperand(4))
510 .addOperand(MI->getOperand(5))
511 .addOperand(MI->getOperand(6))
516 case AMDGPU::RETURN: {
517 // RETURN instructions must have the live-out registers as implicit uses,
518 // otherwise they appear dead.
519 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
520 MachineInstrBuilder MIB(*MF, MI);
521 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
522 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
527 MI->eraseFromParent();
531 //===----------------------------------------------------------------------===//
532 // Custom DAG Lowering Operations
533 //===----------------------------------------------------------------------===//
535 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
536 MachineFunction &MF = DAG.getMachineFunction();
537 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
538 switch (Op.getOpcode()) {
539 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
541 case ISD::FSIN: return LowerTrig(Op, DAG);
542 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
543 case ISD::STORE: return LowerSTORE(Op, DAG);
544 case ISD::LOAD: return LowerLOAD(Op, DAG);
545 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
546 case ISD::INTRINSIC_VOID: {
547 SDValue Chain = Op.getOperand(0);
548 unsigned IntrinsicID =
549 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
550 switch (IntrinsicID) {
551 case AMDGPUIntrinsic::AMDGPU_store_output: {
552 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
553 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
554 MFI->LiveOuts.push_back(Reg);
555 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
557 case AMDGPUIntrinsic::R600_store_swizzle: {
558 const SDValue Args[8] = {
560 Op.getOperand(2), // Export Value
561 Op.getOperand(3), // ArrayBase
562 Op.getOperand(4), // Type
563 DAG.getConstant(0, MVT::i32), // SWZ_X
564 DAG.getConstant(1, MVT::i32), // SWZ_Y
565 DAG.getConstant(2, MVT::i32), // SWZ_Z
566 DAG.getConstant(3, MVT::i32) // SWZ_W
568 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(), Args);
571 // default for switch(IntrinsicID)
574 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
577 case ISD::INTRINSIC_WO_CHAIN: {
578 unsigned IntrinsicID =
579 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
580 EVT VT = Op.getValueType();
582 switch(IntrinsicID) {
583 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
584 case AMDGPUIntrinsic::R600_load_input: {
585 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
586 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
587 MachineFunction &MF = DAG.getMachineFunction();
588 MachineRegisterInfo &MRI = MF.getRegInfo();
590 return DAG.getCopyFromReg(DAG.getEntryNode(),
591 SDLoc(DAG.getEntryNode()), Reg, VT);
594 case AMDGPUIntrinsic::R600_interp_input: {
595 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
596 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
597 MachineSDNode *interp;
599 const MachineFunction &MF = DAG.getMachineFunction();
600 const R600InstrInfo *TII =
601 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
602 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
603 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
604 return DAG.getTargetExtractSubreg(
605 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
606 DL, MVT::f32, SDValue(interp, 0));
608 MachineFunction &MF = DAG.getMachineFunction();
609 MachineRegisterInfo &MRI = MF.getRegInfo();
610 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
611 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
612 MRI.addLiveIn(RegisterI);
613 MRI.addLiveIn(RegisterJ);
614 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
615 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
616 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
617 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
620 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
621 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
622 RegisterJNode, RegisterINode);
624 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
625 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
626 RegisterJNode, RegisterINode);
627 return SDValue(interp, slot % 2);
629 case AMDGPUIntrinsic::R600_interp_xy:
630 case AMDGPUIntrinsic::R600_interp_zw: {
631 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
632 MachineSDNode *interp;
633 SDValue RegisterINode = Op.getOperand(2);
634 SDValue RegisterJNode = Op.getOperand(3);
636 if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
637 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
638 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
639 RegisterJNode, RegisterINode);
641 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
642 MVT::f32, MVT::f32, DAG.getTargetConstant(slot, MVT::i32),
643 RegisterJNode, RegisterINode);
644 return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
645 SDValue(interp, 0), SDValue(interp, 1));
647 case AMDGPUIntrinsic::R600_tex:
648 case AMDGPUIntrinsic::R600_texc:
649 case AMDGPUIntrinsic::R600_txl:
650 case AMDGPUIntrinsic::R600_txlc:
651 case AMDGPUIntrinsic::R600_txb:
652 case AMDGPUIntrinsic::R600_txbc:
653 case AMDGPUIntrinsic::R600_txf:
654 case AMDGPUIntrinsic::R600_txq:
655 case AMDGPUIntrinsic::R600_ddx:
656 case AMDGPUIntrinsic::R600_ddy:
657 case AMDGPUIntrinsic::R600_ldptr: {
659 switch (IntrinsicID) {
660 case AMDGPUIntrinsic::R600_tex:
663 case AMDGPUIntrinsic::R600_texc:
666 case AMDGPUIntrinsic::R600_txl:
669 case AMDGPUIntrinsic::R600_txlc:
672 case AMDGPUIntrinsic::R600_txb:
675 case AMDGPUIntrinsic::R600_txbc:
678 case AMDGPUIntrinsic::R600_txf:
681 case AMDGPUIntrinsic::R600_txq:
684 case AMDGPUIntrinsic::R600_ddx:
687 case AMDGPUIntrinsic::R600_ddy:
690 case AMDGPUIntrinsic::R600_ldptr:
694 llvm_unreachable("Unknow Texture Operation");
697 SDValue TexArgs[19] = {
698 DAG.getConstant(TextureOp, MVT::i32),
700 DAG.getConstant(0, MVT::i32),
701 DAG.getConstant(1, MVT::i32),
702 DAG.getConstant(2, MVT::i32),
703 DAG.getConstant(3, MVT::i32),
707 DAG.getConstant(0, MVT::i32),
708 DAG.getConstant(1, MVT::i32),
709 DAG.getConstant(2, MVT::i32),
710 DAG.getConstant(3, MVT::i32),
718 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
720 case AMDGPUIntrinsic::AMDGPU_dp4: {
722 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
723 DAG.getConstant(0, MVT::i32)),
724 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
725 DAG.getConstant(0, MVT::i32)),
726 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
727 DAG.getConstant(1, MVT::i32)),
728 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
729 DAG.getConstant(1, MVT::i32)),
730 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
731 DAG.getConstant(2, MVT::i32)),
732 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
733 DAG.getConstant(2, MVT::i32)),
734 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
735 DAG.getConstant(3, MVT::i32)),
736 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
737 DAG.getConstant(3, MVT::i32))
739 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
742 case Intrinsic::r600_read_ngroups_x:
743 return LowerImplicitParameter(DAG, VT, DL, 0);
744 case Intrinsic::r600_read_ngroups_y:
745 return LowerImplicitParameter(DAG, VT, DL, 1);
746 case Intrinsic::r600_read_ngroups_z:
747 return LowerImplicitParameter(DAG, VT, DL, 2);
748 case Intrinsic::r600_read_global_size_x:
749 return LowerImplicitParameter(DAG, VT, DL, 3);
750 case Intrinsic::r600_read_global_size_y:
751 return LowerImplicitParameter(DAG, VT, DL, 4);
752 case Intrinsic::r600_read_global_size_z:
753 return LowerImplicitParameter(DAG, VT, DL, 5);
754 case Intrinsic::r600_read_local_size_x:
755 return LowerImplicitParameter(DAG, VT, DL, 6);
756 case Intrinsic::r600_read_local_size_y:
757 return LowerImplicitParameter(DAG, VT, DL, 7);
758 case Intrinsic::r600_read_local_size_z:
759 return LowerImplicitParameter(DAG, VT, DL, 8);
761 case Intrinsic::r600_read_tgid_x:
762 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
764 case Intrinsic::r600_read_tgid_y:
765 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
767 case Intrinsic::r600_read_tgid_z:
768 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
770 case Intrinsic::r600_read_tidig_x:
771 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
773 case Intrinsic::r600_read_tidig_y:
774 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
776 case Intrinsic::r600_read_tidig_z:
777 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
780 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
783 } // end switch(Op.getOpcode())
787 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
788 SmallVectorImpl<SDValue> &Results,
789 SelectionDAG &DAG) const {
790 switch (N->getOpcode()) {
792 AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
794 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
797 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
798 Results.push_back(SDValue(Node, 0));
799 Results.push_back(SDValue(Node, 1));
800 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
802 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
806 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
807 Results.push_back(SDValue(Node, 0));
812 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
813 // On hw >= R700, COS/SIN input must be between -1. and 1.
814 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
815 EVT VT = Op.getValueType();
816 SDValue Arg = Op.getOperand(0);
817 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
818 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
819 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
820 DAG.getConstantFP(0.15915494309, MVT::f32)),
821 DAG.getConstantFP(0.5, MVT::f32)));
823 switch (Op.getOpcode()) {
825 TrigNode = AMDGPUISD::COS_HW;
828 TrigNode = AMDGPUISD::SIN_HW;
831 llvm_unreachable("Wrong trig opcode");
833 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
834 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
835 DAG.getConstantFP(-0.5, MVT::f32)));
836 if (Gen >= AMDGPUSubtarget::R700)
838 // On R600 hw, COS/SIN input must be between -Pi and Pi.
839 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
840 DAG.getConstantFP(3.14159265359, MVT::f32));
843 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
848 Op, DAG.getConstantFP(0.0f, MVT::f32),
849 DAG.getCondCode(ISD::SETNE)
853 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
855 unsigned DwordOffset) const {
856 unsigned ByteOffset = DwordOffset * 4;
857 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
858 AMDGPUAS::CONSTANT_BUFFER_0);
860 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
861 assert(isInt<16>(ByteOffset));
863 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
864 DAG.getConstant(ByteOffset, MVT::i32), // PTR
865 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
866 false, false, false, 0);
869 bool R600TargetLowering::isZero(SDValue Op) const {
870 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
871 return Cst->isNullValue();
872 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
873 return CstFP->isZero();
879 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
881 EVT VT = Op.getValueType();
883 SDValue LHS = Op.getOperand(0);
884 SDValue RHS = Op.getOperand(1);
885 SDValue True = Op.getOperand(2);
886 SDValue False = Op.getOperand(3);
887 SDValue CC = Op.getOperand(4);
890 // LHS and RHS are guaranteed to be the same value type
891 EVT CompareVT = LHS.getValueType();
893 // Check if we can lower this to a native operation.
895 // Try to lower to a SET* instruction:
897 // SET* can match the following patterns:
899 // select_cc f32, f32, -1, 0, cc_supported
900 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
901 // select_cc i32, i32, -1, 0, cc_supported
904 // Move hardware True/False values to the correct operand.
905 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
906 ISD::CondCode InverseCC =
907 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
908 if (isHWTrueValue(False) && isHWFalseValue(True)) {
909 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
910 std::swap(False, True);
911 CC = DAG.getCondCode(InverseCC);
913 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
914 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
915 std::swap(False, True);
917 CC = DAG.getCondCode(SwapInvCC);
922 if (isHWTrueValue(True) && isHWFalseValue(False) &&
923 (CompareVT == VT || VT == MVT::i32)) {
924 // This can be matched by a SET* instruction.
925 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
928 // Try to lower to a CND* instruction:
930 // CND* can match the following patterns:
932 // select_cc f32, 0.0, f32, f32, cc_supported
933 // select_cc f32, 0.0, i32, i32, cc_supported
934 // select_cc i32, 0, f32, f32, cc_supported
935 // select_cc i32, 0, i32, i32, cc_supported
938 // Try to move the zero value to the RHS
940 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
941 // Try swapping the operands
942 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
943 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
945 CC = DAG.getCondCode(CCSwapped);
947 // Try inverting the conditon and then swapping the operands
948 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
949 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
950 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
951 std::swap(True, False);
953 CC = DAG.getCondCode(CCSwapped);
960 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
961 if (CompareVT != VT) {
962 // Bitcast True / False to the correct types. This will end up being
963 // a nop, but it allows us to define only a single pattern in the
964 // .TD files for each CND* instruction rather than having to have
965 // one pattern for integer True/False and one for fp True/False
966 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
967 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
974 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
982 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
985 DAG.getCondCode(CCOpcode));
986 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
989 // If we make it this for it means we have no native instructions to handle
990 // this SELECT_CC, so we must lower it.
991 SDValue HWTrue, HWFalse;
993 if (CompareVT == MVT::f32) {
994 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
995 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
996 } else if (CompareVT == MVT::i32) {
997 HWTrue = DAG.getConstant(-1, CompareVT);
998 HWFalse = DAG.getConstant(0, CompareVT);
1001 llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1004 // Lower this unsupported SELECT_CC into a combination of two supported
1005 // SELECT_CC operations.
1006 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1008 return DAG.getNode(ISD::SELECT_CC, DL, VT,
1011 DAG.getCondCode(ISD::SETNE));
1014 /// LLVM generates byte-addressed pointers. For indirect addressing, we need to
1015 /// convert these pointers to a register index. Each register holds
1016 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1017 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1018 /// for indirect addressing.
1019 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1020 unsigned StackWidth,
1021 SelectionDAG &DAG) const {
1023 switch(StackWidth) {
1033 default: llvm_unreachable("Invalid stack width");
1036 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1037 DAG.getConstant(SRLPad, MVT::i32));
1040 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1043 unsigned &PtrIncr) const {
1044 switch (StackWidth) {
1055 Channel = ElemIdx % 2;
1069 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1071 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1072 SDValue Chain = Op.getOperand(0);
1073 SDValue Value = Op.getOperand(1);
1074 SDValue Ptr = Op.getOperand(2);
1076 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1077 if (Result.getNode()) {
1081 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1082 if (StoreNode->isTruncatingStore()) {
1083 EVT VT = Value.getValueType();
1084 assert(VT.bitsLE(MVT::i32));
1085 EVT MemVT = StoreNode->getMemoryVT();
1086 SDValue MaskConstant;
1087 if (MemVT == MVT::i8) {
1088 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1090 assert(MemVT == MVT::i16);
1091 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1093 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1094 DAG.getConstant(2, MVT::i32));
1095 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1096 DAG.getConstant(0x00000003, VT));
1097 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1098 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1099 DAG.getConstant(3, VT));
1100 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1101 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1102 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1106 DAG.getConstant(0, MVT::i32),
1107 DAG.getConstant(0, MVT::i32),
1110 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1111 SDValue Args[3] = { Chain, Input, DWordAddr };
1112 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1113 Op->getVTList(), Args, MemVT,
1114 StoreNode->getMemOperand());
1115 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1116 Value.getValueType().bitsGE(MVT::i32)) {
1117 // Convert pointer from byte address to dword address.
1118 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1119 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1120 Ptr, DAG.getConstant(2, MVT::i32)));
1122 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1123 llvm_unreachable("Truncated and indexed stores not supported yet");
1125 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1131 EVT ValueVT = Value.getValueType();
1133 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1137 SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1138 if (Ret.getNode()) {
1141 // Lowering for indirect addressing
1143 const MachineFunction &MF = DAG.getMachineFunction();
1144 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1145 getTargetMachine().getFrameLowering());
1146 unsigned StackWidth = TFL->getStackWidth(MF);
1148 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1150 if (ValueVT.isVector()) {
1151 unsigned NumElemVT = ValueVT.getVectorNumElements();
1152 EVT ElemVT = ValueVT.getVectorElementType();
1153 SmallVector<SDValue, 4> Stores(NumElemVT);
1155 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1156 "vector width in load");
1158 for (unsigned i = 0; i < NumElemVT; ++i) {
1159 unsigned Channel, PtrIncr;
1160 getStackAddress(StackWidth, i, Channel, PtrIncr);
1161 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1162 DAG.getConstant(PtrIncr, MVT::i32));
1163 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1164 Value, DAG.getConstant(i, MVT::i32));
1166 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1168 DAG.getTargetConstant(Channel, MVT::i32));
1170 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1172 if (ValueVT == MVT::i8) {
1173 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1175 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1176 DAG.getTargetConstant(0, MVT::i32)); // Channel
1182 // return (512 + (kc_bank << 12)
1184 ConstantAddressBlock(unsigned AddressSpace) {
1185 switch (AddressSpace) {
1186 case AMDGPUAS::CONSTANT_BUFFER_0:
1188 case AMDGPUAS::CONSTANT_BUFFER_1:
1190 case AMDGPUAS::CONSTANT_BUFFER_2:
1191 return 512 + 4096 * 2;
1192 case AMDGPUAS::CONSTANT_BUFFER_3:
1193 return 512 + 4096 * 3;
1194 case AMDGPUAS::CONSTANT_BUFFER_4:
1195 return 512 + 4096 * 4;
1196 case AMDGPUAS::CONSTANT_BUFFER_5:
1197 return 512 + 4096 * 5;
1198 case AMDGPUAS::CONSTANT_BUFFER_6:
1199 return 512 + 4096 * 6;
1200 case AMDGPUAS::CONSTANT_BUFFER_7:
1201 return 512 + 4096 * 7;
1202 case AMDGPUAS::CONSTANT_BUFFER_8:
1203 return 512 + 4096 * 8;
1204 case AMDGPUAS::CONSTANT_BUFFER_9:
1205 return 512 + 4096 * 9;
1206 case AMDGPUAS::CONSTANT_BUFFER_10:
1207 return 512 + 4096 * 10;
1208 case AMDGPUAS::CONSTANT_BUFFER_11:
1209 return 512 + 4096 * 11;
1210 case AMDGPUAS::CONSTANT_BUFFER_12:
1211 return 512 + 4096 * 12;
1212 case AMDGPUAS::CONSTANT_BUFFER_13:
1213 return 512 + 4096 * 13;
1214 case AMDGPUAS::CONSTANT_BUFFER_14:
1215 return 512 + 4096 * 14;
1216 case AMDGPUAS::CONSTANT_BUFFER_15:
1217 return 512 + 4096 * 15;
1223 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1225 EVT VT = Op.getValueType();
1227 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1228 SDValue Chain = Op.getOperand(0);
1229 SDValue Ptr = Op.getOperand(1);
1230 SDValue LoweredLoad;
1232 SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
1233 if (Ret.getNode()) {
1238 return DAG.getMergeValues(Ops, DL);
1242 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1243 SDValue MergedValues[2] = {
1244 SplitVectorLoad(Op, DAG),
1247 return DAG.getMergeValues(MergedValues, DL);
1250 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1251 if (ConstantBlock > -1 &&
1252 ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1253 (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1255 if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1256 isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1257 isa<ConstantSDNode>(Ptr)) {
1259 for (unsigned i = 0; i < 4; i++) {
1260 // We want Const position encoded with the following formula :
1261 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1262 // const_index is Ptr computed by llvm using an alignment of 16.
1263 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1264 // then div by 4 at the ISel step
1265 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1266 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1267 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1269 EVT NewVT = MVT::v4i32;
1270 unsigned NumElements = 4;
1271 if (VT.isVector()) {
1273 NumElements = VT.getVectorNumElements();
1275 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1276 makeArrayRef(Slots, NumElements));
1278 // non-constant ptr can't be folded, keeps it as a v4f32 load
1279 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1280 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1281 DAG.getConstant(LoadNode->getAddressSpace() -
1282 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1286 if (!VT.isVector()) {
1287 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1288 DAG.getConstant(0, MVT::i32));
1291 SDValue MergedValues[2] = {
1295 return DAG.getMergeValues(MergedValues, DL);
1298 // For most operations returning SDValue() will result in the node being
1299 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1300 // need to manually expand loads that may be legal in some address spaces and
1301 // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1302 // compute shaders, since the data is sign extended when it is uploaded to the
1303 // buffer. However SEXT loads from other address spaces are not supported, so
1304 // we need to expand them here.
1305 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1306 EVT MemVT = LoadNode->getMemoryVT();
1307 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1308 SDValue ShiftAmount =
1309 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1310 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1311 LoadNode->getPointerInfo(), MemVT,
1312 LoadNode->isVolatile(),
1313 LoadNode->isNonTemporal(),
1314 LoadNode->getAlignment());
1315 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1316 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1318 SDValue MergedValues[2] = { Sra, Chain };
1319 return DAG.getMergeValues(MergedValues, DL);
1322 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1326 // Lowering for indirect addressing
1327 const MachineFunction &MF = DAG.getMachineFunction();
1328 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1329 getTargetMachine().getFrameLowering());
1330 unsigned StackWidth = TFL->getStackWidth(MF);
1332 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1334 if (VT.isVector()) {
1335 unsigned NumElemVT = VT.getVectorNumElements();
1336 EVT ElemVT = VT.getVectorElementType();
1339 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1340 "vector width in load");
1342 for (unsigned i = 0; i < NumElemVT; ++i) {
1343 unsigned Channel, PtrIncr;
1344 getStackAddress(StackWidth, i, Channel, PtrIncr);
1345 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1346 DAG.getConstant(PtrIncr, MVT::i32));
1347 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1349 DAG.getTargetConstant(Channel, MVT::i32),
1352 for (unsigned i = NumElemVT; i < 4; ++i) {
1353 Loads[i] = DAG.getUNDEF(ElemVT);
1355 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1356 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1358 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1360 DAG.getTargetConstant(0, MVT::i32), // Channel
1369 return DAG.getMergeValues(Ops, DL);
1372 /// XXX Only kernel functions are supported, so we can assume for now that
1373 /// every function is a kernel function, but in the future we should use
1374 /// separate calling conventions for kernel and non-kernel functions.
1375 SDValue R600TargetLowering::LowerFormalArguments(
1377 CallingConv::ID CallConv,
1379 const SmallVectorImpl<ISD::InputArg> &Ins,
1380 SDLoc DL, SelectionDAG &DAG,
1381 SmallVectorImpl<SDValue> &InVals) const {
1382 SmallVector<CCValAssign, 16> ArgLocs;
1383 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1384 getTargetMachine(), ArgLocs, *DAG.getContext());
1385 MachineFunction &MF = DAG.getMachineFunction();
1386 unsigned ShaderType = MF.getInfo<R600MachineFunctionInfo>()->ShaderType;
1388 SmallVector<ISD::InputArg, 8> LocalIns;
1390 getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1392 AnalyzeFormalArguments(CCInfo, LocalIns);
1394 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1395 CCValAssign &VA = ArgLocs[i];
1397 EVT MemVT = LocalIns[i].VT;
1399 if (ShaderType != ShaderType::COMPUTE) {
1400 unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1401 SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1402 InVals.push_back(Register);
1406 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1407 AMDGPUAS::CONSTANT_BUFFER_0);
1409 // i64 isn't a legal type, so the register type used ends up as i32, which
1410 // isn't expected here. It attempts to create this sextload, but it ends up
1411 // being invalid. Somehow this seems to work with i64 arguments, but breaks
1414 // The first 36 bytes of the input buffer contains information about
1415 // thread group and global sizes.
1417 // FIXME: This should really check the extload type, but the handling of
1418 // extload vecto parameters seems to be broken.
1419 //ISD::LoadExtType Ext = Ins[i].Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1420 ISD::LoadExtType Ext = ISD::SEXTLOAD;
1421 SDValue Arg = DAG.getExtLoad(Ext, DL, VT, Chain,
1422 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1423 MachinePointerInfo(UndefValue::get(PtrTy)),
1424 MemVT, false, false, 4);
1426 // 4 is the preferred alignment for the CONSTANT memory space.
1427 InVals.push_back(Arg);
1432 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1435 return VT.changeVectorElementTypeToInteger();
1438 static SDValue CompactSwizzlableVector(
1439 SelectionDAG &DAG, SDValue VectorEntry,
1440 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1441 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1442 assert(RemapSwizzle.empty());
1443 SDValue NewBldVec[4] = {
1444 VectorEntry.getOperand(0),
1445 VectorEntry.getOperand(1),
1446 VectorEntry.getOperand(2),
1447 VectorEntry.getOperand(3)
1450 for (unsigned i = 0; i < 4; i++) {
1451 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1452 // We mask write here to teach later passes that the ith element of this
1453 // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1454 // break false dependencies and additionnaly make assembly easier to read.
1455 RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1456 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1458 RemapSwizzle[i] = 4; // SEL_0
1459 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1460 } else if (C->isExactlyValue(1.0)) {
1461 RemapSwizzle[i] = 5; // SEL_1
1462 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1466 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1468 for (unsigned j = 0; j < i; j++) {
1469 if (NewBldVec[i] == NewBldVec[j]) {
1470 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1471 RemapSwizzle[i] = j;
1477 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1478 VectorEntry.getValueType(), NewBldVec);
1481 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1482 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1483 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1484 assert(RemapSwizzle.empty());
1485 SDValue NewBldVec[4] = {
1486 VectorEntry.getOperand(0),
1487 VectorEntry.getOperand(1),
1488 VectorEntry.getOperand(2),
1489 VectorEntry.getOperand(3)
1491 bool isUnmovable[4] = { false, false, false, false };
1492 for (unsigned i = 0; i < 4; i++) {
1493 RemapSwizzle[i] = i;
1494 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1495 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1498 isUnmovable[Idx] = true;
1502 for (unsigned i = 0; i < 4; i++) {
1503 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1504 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1506 if (isUnmovable[Idx])
1509 std::swap(NewBldVec[Idx], NewBldVec[i]);
1510 std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1515 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1516 VectorEntry.getValueType(), NewBldVec);
1520 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1521 SDValue Swz[4], SelectionDAG &DAG) const {
1522 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1523 // Old -> New swizzle values
1524 DenseMap<unsigned, unsigned> SwizzleRemap;
1526 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1527 for (unsigned i = 0; i < 4; i++) {
1528 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1529 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1530 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1533 SwizzleRemap.clear();
1534 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1535 for (unsigned i = 0; i < 4; i++) {
1536 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1537 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1538 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1545 //===----------------------------------------------------------------------===//
1546 // Custom DAG Optimizations
1547 //===----------------------------------------------------------------------===//
1549 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1550 DAGCombinerInfo &DCI) const {
1551 SelectionDAG &DAG = DCI.DAG;
1553 switch (N->getOpcode()) {
1554 default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1555 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1556 case ISD::FP_ROUND: {
1557 SDValue Arg = N->getOperand(0);
1558 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1559 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1565 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1566 // (i32 select_cc f32, f32, -1, 0 cc)
1568 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1569 // this to one of the SET*_DX10 instructions.
1570 case ISD::FP_TO_SINT: {
1571 SDValue FNeg = N->getOperand(0);
1572 if (FNeg.getOpcode() != ISD::FNEG) {
1575 SDValue SelectCC = FNeg.getOperand(0);
1576 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1577 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1578 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1579 !isHWTrueValue(SelectCC.getOperand(2)) ||
1580 !isHWFalseValue(SelectCC.getOperand(3))) {
1584 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1585 SelectCC.getOperand(0), // LHS
1586 SelectCC.getOperand(1), // RHS
1587 DAG.getConstant(-1, MVT::i32), // True
1588 DAG.getConstant(0, MVT::i32), // Flase
1589 SelectCC.getOperand(4)); // CC
1594 // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1595 // => build_vector elt0, ... , NewEltIdx, ... , eltN
1596 case ISD::INSERT_VECTOR_ELT: {
1597 SDValue InVec = N->getOperand(0);
1598 SDValue InVal = N->getOperand(1);
1599 SDValue EltNo = N->getOperand(2);
1602 // If the inserted element is an UNDEF, just use the input vector.
1603 if (InVal.getOpcode() == ISD::UNDEF)
1606 EVT VT = InVec.getValueType();
1608 // If we can't generate a legal BUILD_VECTOR, exit
1609 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1612 // Check that we know which element is being inserted
1613 if (!isa<ConstantSDNode>(EltNo))
1615 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1617 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1618 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1620 SmallVector<SDValue, 8> Ops;
1621 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1622 Ops.append(InVec.getNode()->op_begin(),
1623 InVec.getNode()->op_end());
1624 } else if (InVec.getOpcode() == ISD::UNDEF) {
1625 unsigned NElts = VT.getVectorNumElements();
1626 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1631 // Insert the element
1632 if (Elt < Ops.size()) {
1633 // All the operands of BUILD_VECTOR must have the same type;
1634 // we enforce that here.
1635 EVT OpVT = Ops[0].getValueType();
1636 if (InVal.getValueType() != OpVT)
1637 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1638 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1639 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1643 // Return the new vector
1644 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1647 // Extract_vec (Build_vector) generated by custom lowering
1648 // also needs to be customly combined
1649 case ISD::EXTRACT_VECTOR_ELT: {
1650 SDValue Arg = N->getOperand(0);
1651 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1652 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1653 unsigned Element = Const->getZExtValue();
1654 return Arg->getOperand(Element);
1657 if (Arg.getOpcode() == ISD::BITCAST &&
1658 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1659 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1660 unsigned Element = Const->getZExtValue();
1661 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1662 Arg->getOperand(0).getOperand(Element));
1667 case ISD::SELECT_CC: {
1668 // Try common optimizations
1669 SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1673 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1674 // selectcc x, y, a, b, inv(cc)
1676 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1677 // selectcc x, y, a, b, cc
1678 SDValue LHS = N->getOperand(0);
1679 if (LHS.getOpcode() != ISD::SELECT_CC) {
1683 SDValue RHS = N->getOperand(1);
1684 SDValue True = N->getOperand(2);
1685 SDValue False = N->getOperand(3);
1686 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1688 if (LHS.getOperand(2).getNode() != True.getNode() ||
1689 LHS.getOperand(3).getNode() != False.getNode() ||
1690 RHS.getNode() != False.getNode()) {
1695 default: return SDValue();
1696 case ISD::SETNE: return LHS;
1698 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1699 LHSCC = ISD::getSetCCInverse(LHSCC,
1700 LHS.getOperand(0).getValueType().isInteger());
1701 if (DCI.isBeforeLegalizeOps() ||
1702 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1703 return DAG.getSelectCC(SDLoc(N),
1715 case AMDGPUISD::EXPORT: {
1716 SDValue Arg = N->getOperand(1);
1717 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1720 SDValue NewArgs[8] = {
1721 N->getOperand(0), // Chain
1723 N->getOperand(2), // ArrayBase
1724 N->getOperand(3), // Type
1725 N->getOperand(4), // SWZ_X
1726 N->getOperand(5), // SWZ_Y
1727 N->getOperand(6), // SWZ_Z
1728 N->getOperand(7) // SWZ_W
1731 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1732 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
1734 case AMDGPUISD::TEXTURE_FETCH: {
1735 SDValue Arg = N->getOperand(1);
1736 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1739 SDValue NewArgs[19] = {
1760 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1761 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1769 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
1770 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
1771 const R600InstrInfo *TII =
1772 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1773 if (!Src.isMachineOpcode())
1775 switch (Src.getMachineOpcode()) {
1776 case AMDGPU::FNEG_R600:
1779 Src = Src.getOperand(0);
1780 Neg = DAG.getTargetConstant(1, MVT::i32);
1782 case AMDGPU::FABS_R600:
1785 Src = Src.getOperand(0);
1786 Abs = DAG.getTargetConstant(1, MVT::i32);
1788 case AMDGPU::CONST_COPY: {
1789 unsigned Opcode = ParentNode->getMachineOpcode();
1790 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1795 SDValue CstOffset = Src.getOperand(0);
1796 if (ParentNode->getValueType(0).isVector())
1799 // Gather constants values
1800 int SrcIndices[] = {
1801 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1802 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1803 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1804 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1805 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1806 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1807 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1808 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1809 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1810 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1811 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1813 std::vector<unsigned> Consts;
1814 for (int OtherSrcIdx : SrcIndices) {
1815 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1816 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1822 if (RegisterSDNode *Reg =
1823 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1824 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1826 = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
1827 Consts.push_back(Cst->getZExtValue());
1832 ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
1833 Consts.push_back(Cst->getZExtValue());
1834 if (!TII->fitsConstReadLimitations(Consts)) {
1839 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1842 case AMDGPU::MOV_IMM_I32:
1843 case AMDGPU::MOV_IMM_F32: {
1844 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1845 uint64_t ImmValue = 0;
1848 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1849 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1850 float FloatValue = FPC->getValueAPF().convertToFloat();
1851 if (FloatValue == 0.0) {
1852 ImmReg = AMDGPU::ZERO;
1853 } else if (FloatValue == 0.5) {
1854 ImmReg = AMDGPU::HALF;
1855 } else if (FloatValue == 1.0) {
1856 ImmReg = AMDGPU::ONE;
1858 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1861 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1862 uint64_t Value = C->getZExtValue();
1864 ImmReg = AMDGPU::ZERO;
1865 } else if (Value == 1) {
1866 ImmReg = AMDGPU::ONE_INT;
1872 // Check that we aren't already using an immediate.
1873 // XXX: It's possible for an instruction to have more than one
1874 // immediate operand, but this is not supported yet.
1875 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1878 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1880 if (C->getZExtValue())
1882 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1884 Src = DAG.getRegister(ImmReg, MVT::i32);
1893 /// \brief Fold the instructions after selecting them
1894 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1895 SelectionDAG &DAG) const {
1896 const R600InstrInfo *TII =
1897 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1898 if (!Node->isMachineOpcode())
1900 unsigned Opcode = Node->getMachineOpcode();
1903 std::vector<SDValue> Ops;
1904 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1908 if (Opcode == AMDGPU::DOT_4) {
1909 int OperandIdx[] = {
1910 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1911 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1912 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1913 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1914 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1915 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1916 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1917 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1920 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1921 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1922 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1923 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1924 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1925 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1926 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1927 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1930 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1931 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1932 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1933 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1934 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1935 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1936 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1937 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1939 for (unsigned i = 0; i < 8; i++) {
1940 if (OperandIdx[i] < 0)
1942 SDValue &Src = Ops[OperandIdx[i] - 1];
1943 SDValue &Neg = Ops[NegIdx[i] - 1];
1944 SDValue &Abs = Ops[AbsIdx[i] - 1];
1945 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1946 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1949 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1950 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1951 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1953 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1954 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1955 SDValue &Src = Ops[i];
1956 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1957 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1959 } else if (Opcode == AMDGPU::CLAMP_R600) {
1960 SDValue Src = Node->getOperand(0);
1961 if (!Src.isMachineOpcode() ||
1962 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1964 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1965 AMDGPU::OpName::clamp);
1968 std::vector<SDValue> Ops;
1969 unsigned NumOp = Src.getNumOperands();
1970 for(unsigned i = 0; i < NumOp; ++i)
1971 Ops.push_back(Src.getOperand(i));
1972 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1973 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1974 Node->getVTList(), Ops);
1976 if (!TII->hasInstrModifiers(Opcode))
1978 int OperandIdx[] = {
1979 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1980 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1981 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1984 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1985 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1986 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1989 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1990 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1993 for (unsigned i = 0; i < 3; i++) {
1994 if (OperandIdx[i] < 0)
1996 SDValue &Src = Ops[OperandIdx[i] - 1];
1997 SDValue &Neg = Ops[NegIdx[i] - 1];
1999 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2000 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2001 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2002 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2007 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2008 SDValue &Imm = Ops[ImmIdx];
2009 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2010 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);