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/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/IR/Argument.h"
24 #include "llvm/IR/Function.h"
28 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
29 AMDGPUTargetLowering(TM),
30 Gen(TM.getSubtarget<AMDGPUSubtarget>().getGeneration()) {
31 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
32 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
33 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
34 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
35 computeRegisterProperties();
37 setOperationAction(ISD::FADD, MVT::v4f32, Expand);
38 setOperationAction(ISD::FMUL, MVT::v4f32, Expand);
39 setOperationAction(ISD::FDIV, MVT::v4f32, Expand);
40 setOperationAction(ISD::FSUB, MVT::v4f32, Expand);
42 setOperationAction(ISD::FCOS, MVT::f32, Custom);
43 setOperationAction(ISD::FSIN, MVT::f32, Custom);
45 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Expand);
46 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Expand);
47 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Expand);
48 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Expand);
49 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
51 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
52 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
54 setOperationAction(ISD::FSUB, MVT::f32, Expand);
56 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
57 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
58 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
60 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
61 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
63 setOperationAction(ISD::SETCC, MVT::i32, Expand);
64 setOperationAction(ISD::SETCC, MVT::f32, Expand);
65 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
67 setOperationAction(ISD::SELECT, MVT::i32, Custom);
68 setOperationAction(ISD::SELECT, MVT::f32, Custom);
70 // Legalize loads and stores to the private address space.
71 setOperationAction(ISD::LOAD, MVT::i32, Custom);
72 setOperationAction(ISD::LOAD, MVT::v2i32, Expand);
73 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
74 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
75 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
76 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Expand);
77 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Expand);
78 setOperationAction(ISD::STORE, MVT::i8, Custom);
79 setOperationAction(ISD::STORE, MVT::i32, Custom);
80 setOperationAction(ISD::STORE, MVT::v2i32, Expand);
81 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
83 setOperationAction(ISD::LOAD, MVT::i32, Custom);
84 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
85 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
87 setTargetDAGCombine(ISD::FP_ROUND);
88 setTargetDAGCombine(ISD::FP_TO_SINT);
89 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
90 setTargetDAGCombine(ISD::SELECT_CC);
92 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
94 setBooleanContents(ZeroOrNegativeOneBooleanContent);
95 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
96 setSchedulingPreference(Sched::VLIW);
99 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
100 MachineInstr * MI, MachineBasicBlock * BB) const {
101 MachineFunction * MF = BB->getParent();
102 MachineRegisterInfo &MRI = MF->getRegInfo();
103 MachineBasicBlock::iterator I = *MI;
104 const R600InstrInfo *TII =
105 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
107 switch (MI->getOpcode()) {
108 default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
109 case AMDGPU::CLAMP_R600: {
110 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
112 MI->getOperand(0).getReg(),
113 MI->getOperand(1).getReg());
114 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
118 case AMDGPU::FABS_R600: {
119 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
121 MI->getOperand(0).getReg(),
122 MI->getOperand(1).getReg());
123 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
127 case AMDGPU::FNEG_R600: {
128 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
130 MI->getOperand(0).getReg(),
131 MI->getOperand(1).getReg());
132 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
136 case AMDGPU::MASK_WRITE: {
137 unsigned maskedRegister = MI->getOperand(0).getReg();
138 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
139 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
140 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
144 case AMDGPU::LDS_READ_RET: {
145 MachineInstrBuilder NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
146 TII->get(MI->getOpcode()),
148 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
149 NewMI.addOperand(MI->getOperand(i));
151 TII->buildDefaultInstruction(*BB, I, AMDGPU::MOV,
152 MI->getOperand(0).getReg(),
157 case AMDGPU::MOV_IMM_F32:
158 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
159 MI->getOperand(1).getFPImm()->getValueAPF()
160 .bitcastToAPInt().getZExtValue());
162 case AMDGPU::MOV_IMM_I32:
163 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
164 MI->getOperand(1).getImm());
166 case AMDGPU::CONST_COPY: {
167 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
168 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
169 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
170 MI->getOperand(1).getImm());
174 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
175 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
176 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
178 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
179 .addOperand(MI->getOperand(0))
180 .addOperand(MI->getOperand(1))
181 .addImm(EOP); // Set End of program bit
186 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
187 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
188 MachineOperand &RID = MI->getOperand(4);
189 MachineOperand &SID = MI->getOperand(5);
190 unsigned TextureId = MI->getOperand(6).getImm();
191 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
192 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
204 case 8: // ShadowRect
215 case 11: // Shadow1DArray
219 case 12: // Shadow2DArray
223 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
224 .addOperand(MI->getOperand(3))
242 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
243 .addOperand(MI->getOperand(2))
261 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
262 .addOperand(MI->getOperand(0))
263 .addOperand(MI->getOperand(1))
281 .addReg(T0, RegState::Implicit)
282 .addReg(T1, RegState::Implicit);
286 case AMDGPU::TXD_SHADOW: {
287 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
288 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
289 MachineOperand &RID = MI->getOperand(4);
290 MachineOperand &SID = MI->getOperand(5);
291 unsigned TextureId = MI->getOperand(6).getImm();
292 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
293 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
305 case 8: // ShadowRect
316 case 11: // Shadow1DArray
320 case 12: // Shadow2DArray
325 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
326 .addOperand(MI->getOperand(3))
344 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
345 .addOperand(MI->getOperand(2))
363 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
364 .addOperand(MI->getOperand(0))
365 .addOperand(MI->getOperand(1))
383 .addReg(T0, RegState::Implicit)
384 .addReg(T1, RegState::Implicit);
389 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
390 .addOperand(MI->getOperand(0));
393 case AMDGPU::BRANCH_COND_f32: {
394 MachineInstr *NewMI =
395 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
396 AMDGPU::PREDICATE_BIT)
397 .addOperand(MI->getOperand(1))
398 .addImm(OPCODE_IS_NOT_ZERO)
400 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
401 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
402 .addOperand(MI->getOperand(0))
403 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
407 case AMDGPU::BRANCH_COND_i32: {
408 MachineInstr *NewMI =
409 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
410 AMDGPU::PREDICATE_BIT)
411 .addOperand(MI->getOperand(1))
412 .addImm(OPCODE_IS_NOT_ZERO_INT)
414 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
415 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
416 .addOperand(MI->getOperand(0))
417 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
421 case AMDGPU::EG_ExportSwz:
422 case AMDGPU::R600_ExportSwz: {
423 // Instruction is left unmodified if its not the last one of its type
424 bool isLastInstructionOfItsType = true;
425 unsigned InstExportType = MI->getOperand(1).getImm();
426 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
427 EndBlock = BB->end(); NextExportInst != EndBlock;
428 NextExportInst = llvm::next(NextExportInst)) {
429 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
430 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
431 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
433 if (CurrentInstExportType == InstExportType) {
434 isLastInstructionOfItsType = false;
439 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
440 if (!EOP && !isLastInstructionOfItsType)
442 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
443 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
444 .addOperand(MI->getOperand(0))
445 .addOperand(MI->getOperand(1))
446 .addOperand(MI->getOperand(2))
447 .addOperand(MI->getOperand(3))
448 .addOperand(MI->getOperand(4))
449 .addOperand(MI->getOperand(5))
450 .addOperand(MI->getOperand(6))
455 case AMDGPU::RETURN: {
456 // RETURN instructions must have the live-out registers as implicit uses,
457 // otherwise they appear dead.
458 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
459 MachineInstrBuilder MIB(*MF, MI);
460 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
461 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
466 MI->eraseFromParent();
470 //===----------------------------------------------------------------------===//
471 // Custom DAG Lowering Operations
472 //===----------------------------------------------------------------------===//
474 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
475 MachineFunction &MF = DAG.getMachineFunction();
476 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
477 switch (Op.getOpcode()) {
478 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
480 case ISD::FSIN: return LowerTrig(Op, DAG);
481 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
482 case ISD::SELECT: return LowerSELECT(Op, DAG);
483 case ISD::STORE: return LowerSTORE(Op, DAG);
484 case ISD::LOAD: return LowerLOAD(Op, DAG);
485 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
486 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
487 case ISD::INTRINSIC_VOID: {
488 SDValue Chain = Op.getOperand(0);
489 unsigned IntrinsicID =
490 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
491 switch (IntrinsicID) {
492 case AMDGPUIntrinsic::AMDGPU_store_output: {
493 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
494 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
495 MFI->LiveOuts.push_back(Reg);
496 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
498 case AMDGPUIntrinsic::R600_store_swizzle: {
499 const SDValue Args[8] = {
501 Op.getOperand(2), // Export Value
502 Op.getOperand(3), // ArrayBase
503 Op.getOperand(4), // Type
504 DAG.getConstant(0, MVT::i32), // SWZ_X
505 DAG.getConstant(1, MVT::i32), // SWZ_Y
506 DAG.getConstant(2, MVT::i32), // SWZ_Z
507 DAG.getConstant(3, MVT::i32) // SWZ_W
509 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(),
513 // default for switch(IntrinsicID)
516 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
519 case ISD::INTRINSIC_WO_CHAIN: {
520 unsigned IntrinsicID =
521 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
522 EVT VT = Op.getValueType();
524 switch(IntrinsicID) {
525 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
526 case AMDGPUIntrinsic::R600_load_input: {
527 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
528 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
529 MachineFunction &MF = DAG.getMachineFunction();
530 MachineRegisterInfo &MRI = MF.getRegInfo();
532 return DAG.getCopyFromReg(DAG.getEntryNode(),
533 SDLoc(DAG.getEntryNode()), Reg, VT);
536 case AMDGPUIntrinsic::R600_interp_input: {
537 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
538 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
539 MachineSDNode *interp;
541 const MachineFunction &MF = DAG.getMachineFunction();
542 const R600InstrInfo *TII =
543 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
544 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
545 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
546 return DAG.getTargetExtractSubreg(
547 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
548 DL, MVT::f32, SDValue(interp, 0));
551 MachineFunction &MF = DAG.getMachineFunction();
552 MachineRegisterInfo &MRI = MF.getRegInfo();
553 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
554 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
555 MRI.addLiveIn(RegisterI);
556 MRI.addLiveIn(RegisterJ);
557 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
558 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
559 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
560 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
563 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
564 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
565 RegisterJNode, RegisterINode);
567 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
568 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
569 RegisterJNode, RegisterINode);
570 return SDValue(interp, slot % 2);
572 case AMDGPUIntrinsic::R600_tex:
573 case AMDGPUIntrinsic::R600_texc:
574 case AMDGPUIntrinsic::R600_txl:
575 case AMDGPUIntrinsic::R600_txlc:
576 case AMDGPUIntrinsic::R600_txb:
577 case AMDGPUIntrinsic::R600_txbc:
578 case AMDGPUIntrinsic::R600_txf:
579 case AMDGPUIntrinsic::R600_txq:
580 case AMDGPUIntrinsic::R600_ddx:
581 case AMDGPUIntrinsic::R600_ddy: {
583 switch (IntrinsicID) {
584 case AMDGPUIntrinsic::R600_tex:
587 case AMDGPUIntrinsic::R600_texc:
590 case AMDGPUIntrinsic::R600_txl:
593 case AMDGPUIntrinsic::R600_txlc:
596 case AMDGPUIntrinsic::R600_txb:
599 case AMDGPUIntrinsic::R600_txbc:
602 case AMDGPUIntrinsic::R600_txf:
605 case AMDGPUIntrinsic::R600_txq:
608 case AMDGPUIntrinsic::R600_ddx:
611 case AMDGPUIntrinsic::R600_ddy:
615 llvm_unreachable("Unknow Texture Operation");
618 SDValue TexArgs[19] = {
619 DAG.getConstant(TextureOp, MVT::i32),
621 DAG.getConstant(0, MVT::i32),
622 DAG.getConstant(1, MVT::i32),
623 DAG.getConstant(2, MVT::i32),
624 DAG.getConstant(3, MVT::i32),
628 DAG.getConstant(0, MVT::i32),
629 DAG.getConstant(1, MVT::i32),
630 DAG.getConstant(2, MVT::i32),
631 DAG.getConstant(3, MVT::i32),
639 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs, 19);
641 case AMDGPUIntrinsic::AMDGPU_dp4: {
643 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
644 DAG.getConstant(0, MVT::i32)),
645 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
646 DAG.getConstant(0, MVT::i32)),
647 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
648 DAG.getConstant(1, MVT::i32)),
649 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
650 DAG.getConstant(1, MVT::i32)),
651 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
652 DAG.getConstant(2, MVT::i32)),
653 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
654 DAG.getConstant(2, MVT::i32)),
655 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
656 DAG.getConstant(3, MVT::i32)),
657 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
658 DAG.getConstant(3, MVT::i32))
660 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args, 8);
663 case Intrinsic::r600_read_ngroups_x:
664 return LowerImplicitParameter(DAG, VT, DL, 0);
665 case Intrinsic::r600_read_ngroups_y:
666 return LowerImplicitParameter(DAG, VT, DL, 1);
667 case Intrinsic::r600_read_ngroups_z:
668 return LowerImplicitParameter(DAG, VT, DL, 2);
669 case Intrinsic::r600_read_global_size_x:
670 return LowerImplicitParameter(DAG, VT, DL, 3);
671 case Intrinsic::r600_read_global_size_y:
672 return LowerImplicitParameter(DAG, VT, DL, 4);
673 case Intrinsic::r600_read_global_size_z:
674 return LowerImplicitParameter(DAG, VT, DL, 5);
675 case Intrinsic::r600_read_local_size_x:
676 return LowerImplicitParameter(DAG, VT, DL, 6);
677 case Intrinsic::r600_read_local_size_y:
678 return LowerImplicitParameter(DAG, VT, DL, 7);
679 case Intrinsic::r600_read_local_size_z:
680 return LowerImplicitParameter(DAG, VT, DL, 8);
682 case Intrinsic::r600_read_tgid_x:
683 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
685 case Intrinsic::r600_read_tgid_y:
686 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
688 case Intrinsic::r600_read_tgid_z:
689 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
691 case Intrinsic::r600_read_tidig_x:
692 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
694 case Intrinsic::r600_read_tidig_y:
695 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
697 case Intrinsic::r600_read_tidig_z:
698 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
701 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
704 } // end switch(Op.getOpcode())
708 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
709 SmallVectorImpl<SDValue> &Results,
710 SelectionDAG &DAG) const {
711 switch (N->getOpcode()) {
713 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
716 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
717 Results.push_back(SDValue(Node, 0));
718 Results.push_back(SDValue(Node, 1));
719 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
721 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
725 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
726 Results.push_back(SDValue(Node, 0));
731 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
732 // On hw >= R700, COS/SIN input must be between -1. and 1.
733 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
734 EVT VT = Op.getValueType();
735 SDValue Arg = Op.getOperand(0);
736 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
737 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
738 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
739 DAG.getConstantFP(0.15915494309, MVT::f32)),
740 DAG.getConstantFP(0.5, MVT::f32)));
742 switch (Op.getOpcode()) {
744 TrigNode = AMDGPUISD::COS_HW;
747 TrigNode = AMDGPUISD::SIN_HW;
750 llvm_unreachable("Wrong trig opcode");
752 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
753 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
754 DAG.getConstantFP(-0.5, MVT::f32)));
755 if (Gen >= AMDGPUSubtarget::R700)
757 // On R600 hw, COS/SIN input must be between -Pi and Pi.
758 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
759 DAG.getConstantFP(3.14159265359, MVT::f32));
762 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
767 Op, DAG.getConstantFP(0.0f, MVT::f32),
768 DAG.getCondCode(ISD::SETNE)
772 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
774 unsigned DwordOffset) const {
775 unsigned ByteOffset = DwordOffset * 4;
776 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
777 AMDGPUAS::PARAM_I_ADDRESS);
779 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
780 assert(isInt<16>(ByteOffset));
782 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
783 DAG.getConstant(ByteOffset, MVT::i32), // PTR
784 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
785 false, false, false, 0);
788 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
790 MachineFunction &MF = DAG.getMachineFunction();
791 const AMDGPUFrameLowering *TFL =
792 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
794 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
797 unsigned FrameIndex = FIN->getIndex();
798 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
799 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
802 bool R600TargetLowering::isZero(SDValue Op) const {
803 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
804 return Cst->isNullValue();
805 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
806 return CstFP->isZero();
812 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
814 EVT VT = Op.getValueType();
816 SDValue LHS = Op.getOperand(0);
817 SDValue RHS = Op.getOperand(1);
818 SDValue True = Op.getOperand(2);
819 SDValue False = Op.getOperand(3);
820 SDValue CC = Op.getOperand(4);
823 // LHS and RHS are guaranteed to be the same value type
824 EVT CompareVT = LHS.getValueType();
826 // Check if we can lower this to a native operation.
828 // Try to lower to a SET* instruction:
830 // SET* can match the following patterns:
832 // select_cc f32, f32, -1, 0, cc_any
833 // select_cc f32, f32, 1.0f, 0.0f, cc_any
834 // select_cc i32, i32, -1, 0, cc_any
837 // Move hardware True/False values to the correct operand.
838 if (isHWTrueValue(False) && isHWFalseValue(True)) {
839 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
840 std::swap(False, True);
841 CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
844 if (isHWTrueValue(True) && isHWFalseValue(False) &&
845 (CompareVT == VT || VT == MVT::i32)) {
846 // This can be matched by a SET* instruction.
847 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
850 // Try to lower to a CND* instruction:
852 // CND* can match the following patterns:
854 // select_cc f32, 0.0, f32, f32, cc_any
855 // select_cc f32, 0.0, i32, i32, cc_any
856 // select_cc i32, 0, f32, f32, cc_any
857 // select_cc i32, 0, i32, i32, cc_any
859 if (isZero(LHS) || isZero(RHS)) {
860 SDValue Cond = (isZero(LHS) ? RHS : LHS);
861 SDValue Zero = (isZero(LHS) ? LHS : RHS);
862 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
863 if (CompareVT != VT) {
864 // Bitcast True / False to the correct types. This will end up being
865 // a nop, but it allows us to define only a single pattern in the
866 // .TD files for each CND* instruction rather than having to have
867 // one pattern for integer True/False and one for fp True/False
868 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
869 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
872 CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
885 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
893 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
896 DAG.getCondCode(CCOpcode));
897 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
901 // Possible Min/Max pattern
902 SDValue MinMax = LowerMinMax(Op, DAG);
903 if (MinMax.getNode()) {
907 // If we make it this for it means we have no native instructions to handle
908 // this SELECT_CC, so we must lower it.
909 SDValue HWTrue, HWFalse;
911 if (CompareVT == MVT::f32) {
912 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
913 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
914 } else if (CompareVT == MVT::i32) {
915 HWTrue = DAG.getConstant(-1, CompareVT);
916 HWFalse = DAG.getConstant(0, CompareVT);
919 assert(!"Unhandled value type in LowerSELECT_CC");
922 // Lower this unsupported SELECT_CC into a combination of two supported
923 // SELECT_CC operations.
924 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
926 return DAG.getNode(ISD::SELECT_CC, DL, VT,
929 DAG.getCondCode(ISD::SETNE));
932 SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
933 return DAG.getNode(ISD::SELECT_CC,
937 DAG.getConstant(0, MVT::i32),
940 DAG.getCondCode(ISD::SETNE));
943 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
944 /// convert these pointers to a register index. Each register holds
945 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
946 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
947 /// for indirect addressing.
948 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
950 SelectionDAG &DAG) const {
962 default: llvm_unreachable("Invalid stack width");
965 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
966 DAG.getConstant(SRLPad, MVT::i32));
969 void R600TargetLowering::getStackAddress(unsigned StackWidth,
972 unsigned &PtrIncr) const {
973 switch (StackWidth) {
984 Channel = ElemIdx % 2;
998 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1000 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1001 SDValue Chain = Op.getOperand(0);
1002 SDValue Value = Op.getOperand(1);
1003 SDValue Ptr = Op.getOperand(2);
1005 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
1006 Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
1007 // Convert pointer from byte address to dword address.
1008 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1009 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1010 Ptr, DAG.getConstant(2, MVT::i32)));
1012 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1013 assert(!"Truncated and indexed stores not supported yet");
1015 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1020 EVT ValueVT = Value.getValueType();
1022 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1026 // Lowering for indirect addressing
1028 const MachineFunction &MF = DAG.getMachineFunction();
1029 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1030 getTargetMachine().getFrameLowering());
1031 unsigned StackWidth = TFL->getStackWidth(MF);
1033 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1035 if (ValueVT.isVector()) {
1036 unsigned NumElemVT = ValueVT.getVectorNumElements();
1037 EVT ElemVT = ValueVT.getVectorElementType();
1040 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1041 "vector width in load");
1043 for (unsigned i = 0; i < NumElemVT; ++i) {
1044 unsigned Channel, PtrIncr;
1045 getStackAddress(StackWidth, i, Channel, PtrIncr);
1046 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1047 DAG.getConstant(PtrIncr, MVT::i32));
1048 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1049 Value, DAG.getConstant(i, MVT::i32));
1051 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1053 DAG.getTargetConstant(Channel, MVT::i32));
1055 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1057 if (ValueVT == MVT::i8) {
1058 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1060 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1061 DAG.getTargetConstant(0, MVT::i32)); // Channel
1067 // return (512 + (kc_bank << 12)
1069 ConstantAddressBlock(unsigned AddressSpace) {
1070 switch (AddressSpace) {
1071 case AMDGPUAS::CONSTANT_BUFFER_0:
1073 case AMDGPUAS::CONSTANT_BUFFER_1:
1075 case AMDGPUAS::CONSTANT_BUFFER_2:
1076 return 512 + 4096 * 2;
1077 case AMDGPUAS::CONSTANT_BUFFER_3:
1078 return 512 + 4096 * 3;
1079 case AMDGPUAS::CONSTANT_BUFFER_4:
1080 return 512 + 4096 * 4;
1081 case AMDGPUAS::CONSTANT_BUFFER_5:
1082 return 512 + 4096 * 5;
1083 case AMDGPUAS::CONSTANT_BUFFER_6:
1084 return 512 + 4096 * 6;
1085 case AMDGPUAS::CONSTANT_BUFFER_7:
1086 return 512 + 4096 * 7;
1087 case AMDGPUAS::CONSTANT_BUFFER_8:
1088 return 512 + 4096 * 8;
1089 case AMDGPUAS::CONSTANT_BUFFER_9:
1090 return 512 + 4096 * 9;
1091 case AMDGPUAS::CONSTANT_BUFFER_10:
1092 return 512 + 4096 * 10;
1093 case AMDGPUAS::CONSTANT_BUFFER_11:
1094 return 512 + 4096 * 11;
1095 case AMDGPUAS::CONSTANT_BUFFER_12:
1096 return 512 + 4096 * 12;
1097 case AMDGPUAS::CONSTANT_BUFFER_13:
1098 return 512 + 4096 * 13;
1099 case AMDGPUAS::CONSTANT_BUFFER_14:
1100 return 512 + 4096 * 14;
1101 case AMDGPUAS::CONSTANT_BUFFER_15:
1102 return 512 + 4096 * 15;
1108 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1110 EVT VT = Op.getValueType();
1112 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1113 SDValue Chain = Op.getOperand(0);
1114 SDValue Ptr = Op.getOperand(1);
1115 SDValue LoweredLoad;
1117 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1118 if (ConstantBlock > -1) {
1120 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1121 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1122 dyn_cast<ConstantSDNode>(Ptr)) {
1124 for (unsigned i = 0; i < 4; i++) {
1125 // We want Const position encoded with the following formula :
1126 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1127 // const_index is Ptr computed by llvm using an alignment of 16.
1128 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1129 // then div by 4 at the ISel step
1130 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1131 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1132 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1134 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
1136 // non constant ptr cant be folded, keeps it as a v4f32 load
1137 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1138 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1139 DAG.getConstant(LoadNode->getAddressSpace() -
1140 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1144 if (!VT.isVector()) {
1145 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1146 DAG.getConstant(0, MVT::i32));
1149 SDValue MergedValues[2] = {
1153 return DAG.getMergeValues(MergedValues, 2, DL);
1156 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1160 // Lowering for indirect addressing
1161 const MachineFunction &MF = DAG.getMachineFunction();
1162 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1163 getTargetMachine().getFrameLowering());
1164 unsigned StackWidth = TFL->getStackWidth(MF);
1166 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1168 if (VT.isVector()) {
1169 unsigned NumElemVT = VT.getVectorNumElements();
1170 EVT ElemVT = VT.getVectorElementType();
1173 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1174 "vector width in load");
1176 for (unsigned i = 0; i < NumElemVT; ++i) {
1177 unsigned Channel, PtrIncr;
1178 getStackAddress(StackWidth, i, Channel, PtrIncr);
1179 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1180 DAG.getConstant(PtrIncr, MVT::i32));
1181 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1183 DAG.getTargetConstant(Channel, MVT::i32),
1186 for (unsigned i = NumElemVT; i < 4; ++i) {
1187 Loads[i] = DAG.getUNDEF(ElemVT);
1189 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1190 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1192 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1194 DAG.getTargetConstant(0, MVT::i32), // Channel
1199 Ops[0] = LoweredLoad;
1202 return DAG.getMergeValues(Ops, 2, DL);
1205 /// XXX Only kernel functions are supported, so we can assume for now that
1206 /// every function is a kernel function, but in the future we should use
1207 /// separate calling conventions for kernel and non-kernel functions.
1208 SDValue R600TargetLowering::LowerFormalArguments(
1210 CallingConv::ID CallConv,
1212 const SmallVectorImpl<ISD::InputArg> &Ins,
1213 SDLoc DL, SelectionDAG &DAG,
1214 SmallVectorImpl<SDValue> &InVals) const {
1215 unsigned ParamOffsetBytes = 36;
1216 Function::const_arg_iterator FuncArg =
1217 DAG.getMachineFunction().getFunction()->arg_begin();
1218 for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
1220 Type *ArgType = FuncArg->getType();
1221 unsigned ArgSizeInBits = ArgType->isPointerTy() ?
1222 32 : ArgType->getPrimitiveSizeInBits();
1223 unsigned ArgBytes = ArgSizeInBits >> 3;
1225 if (ArgSizeInBits < VT.getSizeInBits()) {
1226 assert(!ArgType->isFloatTy() &&
1227 "Extending floating point arguments not supported yet");
1228 ArgVT = MVT::getIntegerVT(ArgSizeInBits);
1233 ISD::LoadExtType LoadType = ISD::EXTLOAD;
1234 if (Ins[i].Flags.isZExt()) {
1235 LoadType = ISD::ZEXTLOAD;
1236 } else if (Ins[i].Flags.isSExt()) {
1237 LoadType = ISD::SEXTLOAD;
1240 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1241 AMDGPUAS::PARAM_I_ADDRESS);
1242 SDValue Arg = DAG.getExtLoad(LoadType, DL, VT, DAG.getRoot(),
1243 DAG.getConstant(ParamOffsetBytes, MVT::i32),
1244 MachinePointerInfo(UndefValue::get(PtrTy)),
1245 ArgVT, false, false, ArgBytes);
1246 InVals.push_back(Arg);
1247 ParamOffsetBytes += ArgBytes;
1252 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1253 if (!VT.isVector()) return MVT::i32;
1254 return VT.changeVectorElementTypeToInteger();
1258 CompactSwizzlableVector(SelectionDAG &DAG, SDValue VectorEntry,
1259 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1260 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1261 assert(RemapSwizzle.empty());
1262 SDValue NewBldVec[4] = {
1263 VectorEntry.getOperand(0),
1264 VectorEntry.getOperand(1),
1265 VectorEntry.getOperand(2),
1266 VectorEntry.getOperand(3)
1269 for (unsigned i = 0; i < 4; i++) {
1270 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1272 RemapSwizzle[i] = 4; // SEL_0
1273 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1274 } else if (C->isExactlyValue(1.0)) {
1275 RemapSwizzle[i] = 5; // SEL_1
1276 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1280 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1282 for (unsigned j = 0; j < i; j++) {
1283 if (NewBldVec[i] == NewBldVec[j]) {
1284 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1285 RemapSwizzle[i] = j;
1291 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1292 VectorEntry.getValueType(), NewBldVec, 4);
1295 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1296 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1297 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1298 assert(RemapSwizzle.empty());
1299 SDValue NewBldVec[4] = {
1300 VectorEntry.getOperand(0),
1301 VectorEntry.getOperand(1),
1302 VectorEntry.getOperand(2),
1303 VectorEntry.getOperand(3)
1305 bool isUnmovable[4] = { false, false, false, false };
1306 for (unsigned i = 0; i < 4; i++)
1307 RemapSwizzle[i] = i;
1309 for (unsigned i = 0; i < 4; i++) {
1310 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1311 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1313 if (!isUnmovable[Idx]) {
1315 std::swap(NewBldVec[Idx], NewBldVec[i]);
1316 std::swap(RemapSwizzle[RemapSwizzle[Idx]], RemapSwizzle[RemapSwizzle[i]]);
1318 isUnmovable[Idx] = true;
1322 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1323 VectorEntry.getValueType(), NewBldVec, 4);
1327 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1328 SDValue Swz[4], SelectionDAG &DAG) const {
1329 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1330 // Old -> New swizzle values
1331 DenseMap<unsigned, unsigned> SwizzleRemap;
1333 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1334 for (unsigned i = 0; i < 4; i++) {
1335 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1336 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1337 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1340 SwizzleRemap.clear();
1341 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1342 for (unsigned i = 0; i < 4; i++) {
1343 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1344 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1345 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1352 //===----------------------------------------------------------------------===//
1353 // Custom DAG Optimizations
1354 //===----------------------------------------------------------------------===//
1356 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1357 DAGCombinerInfo &DCI) const {
1358 SelectionDAG &DAG = DCI.DAG;
1360 switch (N->getOpcode()) {
1361 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1362 case ISD::FP_ROUND: {
1363 SDValue Arg = N->getOperand(0);
1364 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1365 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1371 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1372 // (i32 select_cc f32, f32, -1, 0 cc)
1374 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1375 // this to one of the SET*_DX10 instructions.
1376 case ISD::FP_TO_SINT: {
1377 SDValue FNeg = N->getOperand(0);
1378 if (FNeg.getOpcode() != ISD::FNEG) {
1381 SDValue SelectCC = FNeg.getOperand(0);
1382 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1383 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1384 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1385 !isHWTrueValue(SelectCC.getOperand(2)) ||
1386 !isHWFalseValue(SelectCC.getOperand(3))) {
1390 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1391 SelectCC.getOperand(0), // LHS
1392 SelectCC.getOperand(1), // RHS
1393 DAG.getConstant(-1, MVT::i32), // True
1394 DAG.getConstant(0, MVT::i32), // Flase
1395 SelectCC.getOperand(4)); // CC
1399 // Extract_vec (Build_vector) generated by custom lowering
1400 // also needs to be customly combined
1401 case ISD::EXTRACT_VECTOR_ELT: {
1402 SDValue Arg = N->getOperand(0);
1403 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1404 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1405 unsigned Element = Const->getZExtValue();
1406 return Arg->getOperand(Element);
1409 if (Arg.getOpcode() == ISD::BITCAST &&
1410 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1411 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1412 unsigned Element = Const->getZExtValue();
1413 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1414 Arg->getOperand(0).getOperand(Element));
1419 case ISD::SELECT_CC: {
1420 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1421 // selectcc x, y, a, b, inv(cc)
1423 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1424 // selectcc x, y, a, b, cc
1425 SDValue LHS = N->getOperand(0);
1426 if (LHS.getOpcode() != ISD::SELECT_CC) {
1430 SDValue RHS = N->getOperand(1);
1431 SDValue True = N->getOperand(2);
1432 SDValue False = N->getOperand(3);
1433 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1435 if (LHS.getOperand(2).getNode() != True.getNode() ||
1436 LHS.getOperand(3).getNode() != False.getNode() ||
1437 RHS.getNode() != False.getNode()) {
1442 default: return SDValue();
1443 case ISD::SETNE: return LHS;
1445 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1446 LHSCC = ISD::getSetCCInverse(LHSCC,
1447 LHS.getOperand(0).getValueType().isInteger());
1448 return DAG.getSelectCC(SDLoc(N),
1457 case AMDGPUISD::EXPORT: {
1458 SDValue Arg = N->getOperand(1);
1459 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1462 SDValue NewArgs[8] = {
1463 N->getOperand(0), // Chain
1465 N->getOperand(2), // ArrayBase
1466 N->getOperand(3), // Type
1467 N->getOperand(4), // SWZ_X
1468 N->getOperand(5), // SWZ_Y
1469 N->getOperand(6), // SWZ_Z
1470 N->getOperand(7) // SWZ_W
1473 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1474 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1476 case AMDGPUISD::TEXTURE_FETCH: {
1477 SDValue Arg = N->getOperand(1);
1478 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1481 SDValue NewArgs[19] = {
1502 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1503 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),