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 TII(static_cast<const R600InstrInfo*>(TM.getInstrInfo())) {
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::ADD, MVT::v4i32, Expand);
43 setOperationAction(ISD::AND, MVT::v4i32, Expand);
44 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Expand);
45 setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Expand);
46 setOperationAction(ISD::MUL, MVT::v2i32, Expand);
47 setOperationAction(ISD::MUL, MVT::v4i32, Expand);
48 setOperationAction(ISD::OR, MVT::v4i32, Expand);
49 setOperationAction(ISD::OR, MVT::v2i32, Expand);
50 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Expand);
51 setOperationAction(ISD::SHL, MVT::v4i32, Expand);
52 setOperationAction(ISD::SHL, MVT::v2i32, Expand);
53 setOperationAction(ISD::SRL, MVT::v4i32, Expand);
54 setOperationAction(ISD::SRL, MVT::v2i32, Expand);
55 setOperationAction(ISD::SRA, MVT::v4i32, Expand);
56 setOperationAction(ISD::SRA, MVT::v2i32, Expand);
57 setOperationAction(ISD::SUB, MVT::v4i32, Expand);
58 setOperationAction(ISD::SUB, MVT::v2i32, Expand);
59 setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Expand);
60 setOperationAction(ISD::UDIV, MVT::v4i32, Expand);
61 setOperationAction(ISD::UREM, MVT::v4i32, Expand);
62 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
63 setOperationAction(ISD::XOR, MVT::v4i32, Expand);
64 setOperationAction(ISD::XOR, 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::ROTL, MVT::i32, Custom);
77 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
78 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
80 setOperationAction(ISD::SETCC, MVT::i32, Expand);
81 setOperationAction(ISD::SETCC, MVT::f32, Expand);
82 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
84 setOperationAction(ISD::SELECT, MVT::i32, Custom);
85 setOperationAction(ISD::SELECT, MVT::f32, Custom);
87 setOperationAction(ISD::VSELECT, MVT::v4i32, Expand);
88 setOperationAction(ISD::VSELECT, MVT::v2i32, Expand);
90 // Legalize loads and stores to the private address space.
91 setOperationAction(ISD::LOAD, MVT::i32, Custom);
92 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
93 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
94 setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Custom);
95 setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
96 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
97 setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Custom);
98 setOperationAction(ISD::STORE, MVT::i8, Custom);
99 setOperationAction(ISD::STORE, MVT::i32, Custom);
100 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
101 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
103 setOperationAction(ISD::LOAD, MVT::i32, Custom);
104 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
105 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
107 setTargetDAGCombine(ISD::FP_ROUND);
108 setTargetDAGCombine(ISD::FP_TO_SINT);
109 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
110 setTargetDAGCombine(ISD::SELECT_CC);
112 setBooleanContents(ZeroOrNegativeOneBooleanContent);
113 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
114 setSchedulingPreference(Sched::VLIW);
117 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
118 MachineInstr * MI, MachineBasicBlock * BB) const {
119 MachineFunction * MF = BB->getParent();
120 MachineRegisterInfo &MRI = MF->getRegInfo();
121 MachineBasicBlock::iterator I = *MI;
123 switch (MI->getOpcode()) {
124 default: return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
125 case AMDGPU::CLAMP_R600: {
126 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
128 MI->getOperand(0).getReg(),
129 MI->getOperand(1).getReg());
130 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
134 case AMDGPU::FABS_R600: {
135 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
137 MI->getOperand(0).getReg(),
138 MI->getOperand(1).getReg());
139 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
143 case AMDGPU::FNEG_R600: {
144 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
146 MI->getOperand(0).getReg(),
147 MI->getOperand(1).getReg());
148 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
152 case AMDGPU::MASK_WRITE: {
153 unsigned maskedRegister = MI->getOperand(0).getReg();
154 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
155 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
156 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
160 case AMDGPU::MOV_IMM_F32:
161 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
162 MI->getOperand(1).getFPImm()->getValueAPF()
163 .bitcastToAPInt().getZExtValue());
165 case AMDGPU::MOV_IMM_I32:
166 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
167 MI->getOperand(1).getImm());
169 case AMDGPU::CONST_COPY: {
170 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
171 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
172 TII->setImmOperand(NewMI, R600Operands::SRC0_SEL,
173 MI->getOperand(1).getImm());
177 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
178 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
179 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
181 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
182 .addOperand(MI->getOperand(0))
183 .addOperand(MI->getOperand(1))
184 .addImm(EOP); // Set End of program bit
189 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
190 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
191 MachineOperand &RID = MI->getOperand(4);
192 MachineOperand &SID = MI->getOperand(5);
193 unsigned TextureId = MI->getOperand(6).getImm();
194 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
195 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
207 case 8: // ShadowRect
218 case 11: // Shadow1DArray
222 case 12: // Shadow2DArray
226 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
227 .addOperand(MI->getOperand(3))
245 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
246 .addOperand(MI->getOperand(2))
264 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
265 .addOperand(MI->getOperand(0))
266 .addOperand(MI->getOperand(1))
284 .addReg(T0, RegState::Implicit)
285 .addReg(T1, RegState::Implicit);
289 case AMDGPU::TXD_SHADOW: {
290 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
291 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
292 MachineOperand &RID = MI->getOperand(4);
293 MachineOperand &SID = MI->getOperand(5);
294 unsigned TextureId = MI->getOperand(6).getImm();
295 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
296 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
308 case 8: // ShadowRect
319 case 11: // Shadow1DArray
323 case 12: // Shadow2DArray
328 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
329 .addOperand(MI->getOperand(3))
347 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
348 .addOperand(MI->getOperand(2))
366 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
367 .addOperand(MI->getOperand(0))
368 .addOperand(MI->getOperand(1))
386 .addReg(T0, RegState::Implicit)
387 .addReg(T1, RegState::Implicit);
392 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
393 .addOperand(MI->getOperand(0));
396 case AMDGPU::BRANCH_COND_f32: {
397 MachineInstr *NewMI =
398 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
399 AMDGPU::PREDICATE_BIT)
400 .addOperand(MI->getOperand(1))
401 .addImm(OPCODE_IS_NOT_ZERO)
403 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
404 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
405 .addOperand(MI->getOperand(0))
406 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
410 case AMDGPU::BRANCH_COND_i32: {
411 MachineInstr *NewMI =
412 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
413 AMDGPU::PREDICATE_BIT)
414 .addOperand(MI->getOperand(1))
415 .addImm(OPCODE_IS_NOT_ZERO_INT)
417 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
418 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
419 .addOperand(MI->getOperand(0))
420 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
424 case AMDGPU::EG_ExportSwz:
425 case AMDGPU::R600_ExportSwz: {
426 // Instruction is left unmodified if its not the last one of its type
427 bool isLastInstructionOfItsType = true;
428 unsigned InstExportType = MI->getOperand(1).getImm();
429 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
430 EndBlock = BB->end(); NextExportInst != EndBlock;
431 NextExportInst = llvm::next(NextExportInst)) {
432 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
433 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
434 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
436 if (CurrentInstExportType == InstExportType) {
437 isLastInstructionOfItsType = false;
442 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
443 if (!EOP && !isLastInstructionOfItsType)
445 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
446 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
447 .addOperand(MI->getOperand(0))
448 .addOperand(MI->getOperand(1))
449 .addOperand(MI->getOperand(2))
450 .addOperand(MI->getOperand(3))
451 .addOperand(MI->getOperand(4))
452 .addOperand(MI->getOperand(5))
453 .addOperand(MI->getOperand(6))
458 case AMDGPU::RETURN: {
459 // RETURN instructions must have the live-out registers as implicit uses,
460 // otherwise they appear dead.
461 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
462 MachineInstrBuilder MIB(*MF, MI);
463 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
464 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
469 MI->eraseFromParent();
473 //===----------------------------------------------------------------------===//
474 // Custom DAG Lowering Operations
475 //===----------------------------------------------------------------------===//
477 using namespace llvm::Intrinsic;
478 using namespace llvm::AMDGPUIntrinsic;
480 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
481 switch (Op.getOpcode()) {
482 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
483 case ISD::ROTL: return LowerROTL(Op, DAG);
484 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
485 case ISD::SELECT: return LowerSELECT(Op, DAG);
486 case ISD::STORE: return LowerSTORE(Op, DAG);
487 case ISD::LOAD: return LowerLOAD(Op, DAG);
488 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
489 case ISD::INTRINSIC_VOID: {
490 SDValue Chain = Op.getOperand(0);
491 unsigned IntrinsicID =
492 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
493 switch (IntrinsicID) {
494 case AMDGPUIntrinsic::AMDGPU_store_output: {
495 MachineFunction &MF = DAG.getMachineFunction();
496 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
497 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
498 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
499 MFI->LiveOuts.push_back(Reg);
500 return DAG.getCopyToReg(Chain, Op.getDebugLoc(), Reg, Op.getOperand(2));
502 case AMDGPUIntrinsic::R600_store_swizzle: {
503 const SDValue Args[8] = {
505 Op.getOperand(2), // Export Value
506 Op.getOperand(3), // ArrayBase
507 Op.getOperand(4), // Type
508 DAG.getConstant(0, MVT::i32), // SWZ_X
509 DAG.getConstant(1, MVT::i32), // SWZ_Y
510 DAG.getConstant(2, MVT::i32), // SWZ_Z
511 DAG.getConstant(3, MVT::i32) // SWZ_W
513 return DAG.getNode(AMDGPUISD::EXPORT, Op.getDebugLoc(), Op.getValueType(),
517 // default for switch(IntrinsicID)
520 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
523 case ISD::INTRINSIC_WO_CHAIN: {
524 unsigned IntrinsicID =
525 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
526 EVT VT = Op.getValueType();
527 DebugLoc DL = Op.getDebugLoc();
528 switch(IntrinsicID) {
529 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
530 case AMDGPUIntrinsic::R600_load_input: {
531 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
532 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
533 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass, 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 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
542 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
543 return DAG.getTargetExtractSubreg(
544 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
545 DL, MVT::f32, SDValue(interp, 0));
549 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
550 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
551 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
552 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
553 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
554 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
556 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
557 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
558 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
559 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1), MVT::f32),
560 CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
561 AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb), MVT::f32));
563 return SDValue(interp, slot % 2);
565 case AMDGPUIntrinsic::R600_tex:
566 case AMDGPUIntrinsic::R600_texc:
567 case AMDGPUIntrinsic::R600_txl:
568 case AMDGPUIntrinsic::R600_txlc:
569 case AMDGPUIntrinsic::R600_txb:
570 case AMDGPUIntrinsic::R600_txbc:
571 case AMDGPUIntrinsic::R600_txf:
572 case AMDGPUIntrinsic::R600_txq:
573 case AMDGPUIntrinsic::R600_ddx:
574 case AMDGPUIntrinsic::R600_ddy: {
576 switch (IntrinsicID) {
577 case AMDGPUIntrinsic::R600_tex:
580 case AMDGPUIntrinsic::R600_texc:
583 case AMDGPUIntrinsic::R600_txl:
586 case AMDGPUIntrinsic::R600_txlc:
589 case AMDGPUIntrinsic::R600_txb:
592 case AMDGPUIntrinsic::R600_txbc:
595 case AMDGPUIntrinsic::R600_txf:
598 case AMDGPUIntrinsic::R600_txq:
601 case AMDGPUIntrinsic::R600_ddx:
604 case AMDGPUIntrinsic::R600_ddy:
608 llvm_unreachable("Unknow Texture Operation");
611 SDValue TexArgs[19] = {
612 DAG.getConstant(TextureOp, MVT::i32),
614 DAG.getConstant(0, MVT::i32),
615 DAG.getConstant(1, MVT::i32),
616 DAG.getConstant(2, MVT::i32),
617 DAG.getConstant(3, 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),
632 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs, 19);
634 case AMDGPUIntrinsic::AMDGPU_dp4: {
636 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
637 DAG.getConstant(0, MVT::i32)),
638 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
639 DAG.getConstant(0, MVT::i32)),
640 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
641 DAG.getConstant(1, MVT::i32)),
642 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
643 DAG.getConstant(1, MVT::i32)),
644 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
645 DAG.getConstant(2, MVT::i32)),
646 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
647 DAG.getConstant(2, MVT::i32)),
648 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
649 DAG.getConstant(3, MVT::i32)),
650 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
651 DAG.getConstant(3, MVT::i32))
653 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args, 8);
656 case r600_read_ngroups_x:
657 return LowerImplicitParameter(DAG, VT, DL, 0);
658 case r600_read_ngroups_y:
659 return LowerImplicitParameter(DAG, VT, DL, 1);
660 case r600_read_ngroups_z:
661 return LowerImplicitParameter(DAG, VT, DL, 2);
662 case r600_read_global_size_x:
663 return LowerImplicitParameter(DAG, VT, DL, 3);
664 case r600_read_global_size_y:
665 return LowerImplicitParameter(DAG, VT, DL, 4);
666 case r600_read_global_size_z:
667 return LowerImplicitParameter(DAG, VT, DL, 5);
668 case r600_read_local_size_x:
669 return LowerImplicitParameter(DAG, VT, DL, 6);
670 case r600_read_local_size_y:
671 return LowerImplicitParameter(DAG, VT, DL, 7);
672 case r600_read_local_size_z:
673 return LowerImplicitParameter(DAG, VT, DL, 8);
675 case r600_read_tgid_x:
676 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
678 case r600_read_tgid_y:
679 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
681 case r600_read_tgid_z:
682 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
684 case r600_read_tidig_x:
685 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
687 case r600_read_tidig_y:
688 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
690 case r600_read_tidig_z:
691 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
694 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
697 } // end switch(Op.getOpcode())
701 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
702 SmallVectorImpl<SDValue> &Results,
703 SelectionDAG &DAG) const {
704 switch (N->getOpcode()) {
706 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
709 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
710 Results.push_back(SDValue(Node, 0));
711 Results.push_back(SDValue(Node, 1));
712 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
714 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
718 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
719 Results.push_back(SDValue(Node, 0));
724 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
729 Op, DAG.getConstantFP(0.0f, MVT::f32),
730 DAG.getCondCode(ISD::SETNE)
734 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
736 unsigned DwordOffset) const {
737 unsigned ByteOffset = DwordOffset * 4;
738 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
739 AMDGPUAS::PARAM_I_ADDRESS);
741 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
742 assert(isInt<16>(ByteOffset));
744 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
745 DAG.getConstant(ByteOffset, MVT::i32), // PTR
746 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
747 false, false, false, 0);
750 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
752 MachineFunction &MF = DAG.getMachineFunction();
753 const AMDGPUFrameLowering *TFL =
754 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
756 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
759 unsigned FrameIndex = FIN->getIndex();
760 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
761 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
764 SDValue R600TargetLowering::LowerROTL(SDValue Op, SelectionDAG &DAG) const {
765 DebugLoc DL = Op.getDebugLoc();
766 EVT VT = Op.getValueType();
768 return DAG.getNode(AMDGPUISD::BITALIGN, DL, VT,
771 DAG.getNode(ISD::SUB, DL, VT,
772 DAG.getConstant(32, MVT::i32),
776 bool R600TargetLowering::isZero(SDValue Op) const {
777 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
778 return Cst->isNullValue();
779 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
780 return CstFP->isZero();
786 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
787 DebugLoc DL = Op.getDebugLoc();
788 EVT VT = Op.getValueType();
790 SDValue LHS = Op.getOperand(0);
791 SDValue RHS = Op.getOperand(1);
792 SDValue True = Op.getOperand(2);
793 SDValue False = Op.getOperand(3);
794 SDValue CC = Op.getOperand(4);
797 // LHS and RHS are guaranteed to be the same value type
798 EVT CompareVT = LHS.getValueType();
800 // Check if we can lower this to a native operation.
802 // Try to lower to a SET* instruction:
804 // SET* can match the following patterns:
806 // select_cc f32, f32, -1, 0, cc_any
807 // select_cc f32, f32, 1.0f, 0.0f, cc_any
808 // select_cc i32, i32, -1, 0, cc_any
811 // Move hardware True/False values to the correct operand.
812 if (isHWTrueValue(False) && isHWFalseValue(True)) {
813 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
814 std::swap(False, True);
815 CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
818 if (isHWTrueValue(True) && isHWFalseValue(False) &&
819 (CompareVT == VT || VT == MVT::i32)) {
820 // This can be matched by a SET* instruction.
821 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
824 // Try to lower to a CND* instruction:
826 // CND* can match the following patterns:
828 // select_cc f32, 0.0, f32, f32, cc_any
829 // select_cc f32, 0.0, i32, i32, cc_any
830 // select_cc i32, 0, f32, f32, cc_any
831 // select_cc i32, 0, i32, i32, cc_any
833 if (isZero(LHS) || isZero(RHS)) {
834 SDValue Cond = (isZero(LHS) ? RHS : LHS);
835 SDValue Zero = (isZero(LHS) ? LHS : RHS);
836 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
837 if (CompareVT != VT) {
838 // Bitcast True / False to the correct types. This will end up being
839 // a nop, but it allows us to define only a single pattern in the
840 // .TD files for each CND* instruction rather than having to have
841 // one pattern for integer True/False and one for fp True/False
842 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
843 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
846 CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
859 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
867 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
870 DAG.getCondCode(CCOpcode));
871 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
875 // Possible Min/Max pattern
876 SDValue MinMax = LowerMinMax(Op, DAG);
877 if (MinMax.getNode()) {
881 // If we make it this for it means we have no native instructions to handle
882 // this SELECT_CC, so we must lower it.
883 SDValue HWTrue, HWFalse;
885 if (CompareVT == MVT::f32) {
886 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
887 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
888 } else if (CompareVT == MVT::i32) {
889 HWTrue = DAG.getConstant(-1, CompareVT);
890 HWFalse = DAG.getConstant(0, CompareVT);
893 assert(!"Unhandled value type in LowerSELECT_CC");
896 // Lower this unsupported SELECT_CC into a combination of two supported
897 // SELECT_CC operations.
898 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
900 return DAG.getNode(ISD::SELECT_CC, DL, VT,
903 DAG.getCondCode(ISD::SETNE));
906 SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
907 return DAG.getNode(ISD::SELECT_CC,
911 DAG.getConstant(0, MVT::i32),
914 DAG.getCondCode(ISD::SETNE));
917 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
918 /// convert these pointers to a register index. Each register holds
919 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
920 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
921 /// for indirect addressing.
922 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
924 SelectionDAG &DAG) const {
936 default: llvm_unreachable("Invalid stack width");
939 return DAG.getNode(ISD::SRL, Ptr.getDebugLoc(), Ptr.getValueType(), Ptr,
940 DAG.getConstant(SRLPad, MVT::i32));
943 void R600TargetLowering::getStackAddress(unsigned StackWidth,
946 unsigned &PtrIncr) const {
947 switch (StackWidth) {
958 Channel = ElemIdx % 2;
972 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
973 DebugLoc DL = Op.getDebugLoc();
974 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
975 SDValue Chain = Op.getOperand(0);
976 SDValue Value = Op.getOperand(1);
977 SDValue Ptr = Op.getOperand(2);
979 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
980 Ptr->getOpcode() != AMDGPUISD::DWORDADDR) {
981 // Convert pointer from byte address to dword address.
982 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
983 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
984 Ptr, DAG.getConstant(2, MVT::i32)));
986 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
987 assert(!"Truncated and indexed stores not supported yet");
989 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
994 EVT ValueVT = Value.getValueType();
996 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1000 // Lowering for indirect addressing
1002 const MachineFunction &MF = DAG.getMachineFunction();
1003 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1004 getTargetMachine().getFrameLowering());
1005 unsigned StackWidth = TFL->getStackWidth(MF);
1007 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1009 if (ValueVT.isVector()) {
1010 unsigned NumElemVT = ValueVT.getVectorNumElements();
1011 EVT ElemVT = ValueVT.getVectorElementType();
1014 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1015 "vector width in load");
1017 for (unsigned i = 0; i < NumElemVT; ++i) {
1018 unsigned Channel, PtrIncr;
1019 getStackAddress(StackWidth, i, Channel, PtrIncr);
1020 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1021 DAG.getConstant(PtrIncr, MVT::i32));
1022 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1023 Value, DAG.getConstant(i, MVT::i32));
1025 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1027 DAG.getTargetConstant(Channel, MVT::i32));
1029 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1031 if (ValueVT == MVT::i8) {
1032 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1034 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1035 DAG.getTargetConstant(0, MVT::i32)); // Channel
1041 // return (512 + (kc_bank << 12)
1043 ConstantAddressBlock(unsigned AddressSpace) {
1044 switch (AddressSpace) {
1045 case AMDGPUAS::CONSTANT_BUFFER_0:
1047 case AMDGPUAS::CONSTANT_BUFFER_1:
1049 case AMDGPUAS::CONSTANT_BUFFER_2:
1050 return 512 + 4096 * 2;
1051 case AMDGPUAS::CONSTANT_BUFFER_3:
1052 return 512 + 4096 * 3;
1053 case AMDGPUAS::CONSTANT_BUFFER_4:
1054 return 512 + 4096 * 4;
1055 case AMDGPUAS::CONSTANT_BUFFER_5:
1056 return 512 + 4096 * 5;
1057 case AMDGPUAS::CONSTANT_BUFFER_6:
1058 return 512 + 4096 * 6;
1059 case AMDGPUAS::CONSTANT_BUFFER_7:
1060 return 512 + 4096 * 7;
1061 case AMDGPUAS::CONSTANT_BUFFER_8:
1062 return 512 + 4096 * 8;
1063 case AMDGPUAS::CONSTANT_BUFFER_9:
1064 return 512 + 4096 * 9;
1065 case AMDGPUAS::CONSTANT_BUFFER_10:
1066 return 512 + 4096 * 10;
1067 case AMDGPUAS::CONSTANT_BUFFER_11:
1068 return 512 + 4096 * 11;
1069 case AMDGPUAS::CONSTANT_BUFFER_12:
1070 return 512 + 4096 * 12;
1071 case AMDGPUAS::CONSTANT_BUFFER_13:
1072 return 512 + 4096 * 13;
1073 case AMDGPUAS::CONSTANT_BUFFER_14:
1074 return 512 + 4096 * 14;
1075 case AMDGPUAS::CONSTANT_BUFFER_15:
1076 return 512 + 4096 * 15;
1082 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1084 EVT VT = Op.getValueType();
1085 DebugLoc DL = Op.getDebugLoc();
1086 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1087 SDValue Chain = Op.getOperand(0);
1088 SDValue Ptr = Op.getOperand(1);
1089 SDValue LoweredLoad;
1091 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1092 if (ConstantBlock > -1) {
1094 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1095 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1096 dyn_cast<ConstantSDNode>(Ptr)) {
1098 for (unsigned i = 0; i < 4; i++) {
1099 // We want Const position encoded with the following formula :
1100 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1101 // const_index is Ptr computed by llvm using an alignment of 16.
1102 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1103 // then div by 4 at the ISel step
1104 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1105 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1106 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1108 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Slots, 4);
1110 // non constant ptr cant be folded, keeps it as a v4f32 load
1111 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1112 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1113 DAG.getConstant(LoadNode->getAddressSpace() -
1114 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1118 if (!VT.isVector()) {
1119 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1120 DAG.getConstant(0, MVT::i32));
1123 SDValue MergedValues[2] = {
1127 return DAG.getMergeValues(MergedValues, 2, DL);
1130 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1134 // Lowering for indirect addressing
1135 const MachineFunction &MF = DAG.getMachineFunction();
1136 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1137 getTargetMachine().getFrameLowering());
1138 unsigned StackWidth = TFL->getStackWidth(MF);
1140 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1142 if (VT.isVector()) {
1143 unsigned NumElemVT = VT.getVectorNumElements();
1144 EVT ElemVT = VT.getVectorElementType();
1147 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1148 "vector width in load");
1150 for (unsigned i = 0; i < NumElemVT; ++i) {
1151 unsigned Channel, PtrIncr;
1152 getStackAddress(StackWidth, i, Channel, PtrIncr);
1153 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1154 DAG.getConstant(PtrIncr, MVT::i32));
1155 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1157 DAG.getTargetConstant(Channel, MVT::i32),
1160 for (unsigned i = NumElemVT; i < 4; ++i) {
1161 Loads[i] = DAG.getUNDEF(ElemVT);
1163 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1164 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1166 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1168 DAG.getTargetConstant(0, MVT::i32), // Channel
1173 Ops[0] = LoweredLoad;
1176 return DAG.getMergeValues(Ops, 2, DL);
1179 /// XXX Only kernel functions are supported, so we can assume for now that
1180 /// every function is a kernel function, but in the future we should use
1181 /// separate calling conventions for kernel and non-kernel functions.
1182 SDValue R600TargetLowering::LowerFormalArguments(
1184 CallingConv::ID CallConv,
1186 const SmallVectorImpl<ISD::InputArg> &Ins,
1187 DebugLoc DL, SelectionDAG &DAG,
1188 SmallVectorImpl<SDValue> &InVals) const {
1189 unsigned ParamOffsetBytes = 36;
1190 Function::const_arg_iterator FuncArg =
1191 DAG.getMachineFunction().getFunction()->arg_begin();
1192 for (unsigned i = 0, e = Ins.size(); i < e; ++i, ++FuncArg) {
1194 Type *ArgType = FuncArg->getType();
1195 unsigned ArgSizeInBits = ArgType->isPointerTy() ?
1196 32 : ArgType->getPrimitiveSizeInBits();
1197 unsigned ArgBytes = ArgSizeInBits >> 3;
1199 if (ArgSizeInBits < VT.getSizeInBits()) {
1200 assert(!ArgType->isFloatTy() &&
1201 "Extending floating point arguments not supported yet");
1202 ArgVT = MVT::getIntegerVT(ArgSizeInBits);
1206 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1207 AMDGPUAS::PARAM_I_ADDRESS);
1208 SDValue Arg = DAG.getExtLoad(ISD::ZEXTLOAD, DL, VT, DAG.getRoot(),
1209 DAG.getConstant(ParamOffsetBytes, MVT::i32),
1210 MachinePointerInfo(UndefValue::get(PtrTy)),
1211 ArgVT, false, false, ArgBytes);
1212 InVals.push_back(Arg);
1213 ParamOffsetBytes += ArgBytes;
1218 EVT R600TargetLowering::getSetCCResultType(EVT VT) const {
1219 if (!VT.isVector()) return MVT::i32;
1220 return VT.changeVectorElementTypeToInteger();
1223 //===----------------------------------------------------------------------===//
1224 // Custom DAG Optimizations
1225 //===----------------------------------------------------------------------===//
1227 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1228 DAGCombinerInfo &DCI) const {
1229 SelectionDAG &DAG = DCI.DAG;
1231 switch (N->getOpcode()) {
1232 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1233 case ISD::FP_ROUND: {
1234 SDValue Arg = N->getOperand(0);
1235 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1236 return DAG.getNode(ISD::UINT_TO_FP, N->getDebugLoc(), N->getValueType(0),
1242 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1243 // (i32 select_cc f32, f32, -1, 0 cc)
1245 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1246 // this to one of the SET*_DX10 instructions.
1247 case ISD::FP_TO_SINT: {
1248 SDValue FNeg = N->getOperand(0);
1249 if (FNeg.getOpcode() != ISD::FNEG) {
1252 SDValue SelectCC = FNeg.getOperand(0);
1253 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1254 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1255 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1256 !isHWTrueValue(SelectCC.getOperand(2)) ||
1257 !isHWFalseValue(SelectCC.getOperand(3))) {
1261 return DAG.getNode(ISD::SELECT_CC, N->getDebugLoc(), N->getValueType(0),
1262 SelectCC.getOperand(0), // LHS
1263 SelectCC.getOperand(1), // RHS
1264 DAG.getConstant(-1, MVT::i32), // True
1265 DAG.getConstant(0, MVT::i32), // Flase
1266 SelectCC.getOperand(4)); // CC
1270 // Extract_vec (Build_vector) generated by custom lowering
1271 // also needs to be customly combined
1272 case ISD::EXTRACT_VECTOR_ELT: {
1273 SDValue Arg = N->getOperand(0);
1274 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1275 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1276 unsigned Element = Const->getZExtValue();
1277 return Arg->getOperand(Element);
1280 if (Arg.getOpcode() == ISD::BITCAST &&
1281 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1282 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1283 unsigned Element = Const->getZExtValue();
1284 return DAG.getNode(ISD::BITCAST, N->getDebugLoc(), N->getVTList(),
1285 Arg->getOperand(0).getOperand(Element));
1290 case ISD::SELECT_CC: {
1291 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1292 // selectcc x, y, a, b, inv(cc)
1294 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1295 // selectcc x, y, a, b, cc
1296 SDValue LHS = N->getOperand(0);
1297 if (LHS.getOpcode() != ISD::SELECT_CC) {
1301 SDValue RHS = N->getOperand(1);
1302 SDValue True = N->getOperand(2);
1303 SDValue False = N->getOperand(3);
1304 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1306 if (LHS.getOperand(2).getNode() != True.getNode() ||
1307 LHS.getOperand(3).getNode() != False.getNode() ||
1308 RHS.getNode() != False.getNode()) {
1313 default: return SDValue();
1314 case ISD::SETNE: return LHS;
1316 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1317 LHSCC = ISD::getSetCCInverse(LHSCC,
1318 LHS.getOperand(0).getValueType().isInteger());
1319 return DAG.getSelectCC(N->getDebugLoc(),
1328 case AMDGPUISD::EXPORT: {
1329 SDValue Arg = N->getOperand(1);
1330 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1332 SDValue NewBldVec[4] = {
1333 DAG.getUNDEF(MVT::f32),
1334 DAG.getUNDEF(MVT::f32),
1335 DAG.getUNDEF(MVT::f32),
1336 DAG.getUNDEF(MVT::f32)
1338 SDValue NewArgs[8] = {
1339 N->getOperand(0), // Chain
1341 N->getOperand(2), // ArrayBase
1342 N->getOperand(3), // Type
1343 N->getOperand(4), // SWZ_X
1344 N->getOperand(5), // SWZ_Y
1345 N->getOperand(6), // SWZ_Z
1346 N->getOperand(7) // SWZ_W
1348 for (unsigned i = 0; i < Arg.getNumOperands(); i++) {
1349 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(Arg.getOperand(i))) {
1351 NewArgs[4 + i] = DAG.getConstant(4, MVT::i32); // SEL_0
1352 } else if (C->isExactlyValue(1.0)) {
1353 NewArgs[4 + i] = DAG.getConstant(5, MVT::i32); // SEL_0
1355 NewBldVec[i] = Arg.getOperand(i);
1358 NewBldVec[i] = Arg.getOperand(i);
1361 DebugLoc DL = N->getDebugLoc();
1362 NewArgs[1] = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4f32, NewBldVec, 4);
1363 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);