1 //===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Custom DAG lowering for R600
13 //===----------------------------------------------------------------------===//
15 #include "R600ISelLowering.h"
16 #include "R600Defines.h"
17 #include "R600InstrInfo.h"
18 #include "R600MachineFunctionInfo.h"
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/IR/Argument.h"
25 #include "llvm/IR/Function.h"
29 R600TargetLowering::R600TargetLowering(TargetMachine &TM) :
30 AMDGPUTargetLowering(TM),
31 Gen(TM.getSubtarget<AMDGPUSubtarget>().getGeneration()) {
32 addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
33 addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
34 addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
35 addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
36 addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
37 addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
39 computeRegisterProperties();
41 // Set condition code actions
42 setCondCodeAction(ISD::SETO, MVT::f32, Expand);
43 setCondCodeAction(ISD::SETUO, MVT::f32, Expand);
44 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
45 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
46 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
47 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
49 setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
50 setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
51 setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
52 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
53 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
55 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
56 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
57 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
58 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
60 setOperationAction(ISD::FCOS, MVT::f32, Custom);
61 setOperationAction(ISD::FSIN, MVT::f32, Custom);
63 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
64 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
66 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
67 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
69 setOperationAction(ISD::FSUB, MVT::f32, Expand);
71 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
72 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
73 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
75 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
76 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
78 setOperationAction(ISD::SETCC, MVT::i32, Expand);
79 setOperationAction(ISD::SETCC, MVT::f32, Expand);
80 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
82 setOperationAction(ISD::SELECT, MVT::i32, Expand);
83 setOperationAction(ISD::SELECT, MVT::f32, Expand);
84 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
85 setOperationAction(ISD::SELECT, MVT::v2f32, Expand);
86 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
87 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
89 // Legalize loads and stores to the private address space.
90 setOperationAction(ISD::LOAD, MVT::i32, Custom);
91 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
92 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
93 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
94 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
95 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
96 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
97 setOperationAction(ISD::STORE, MVT::i8, Custom);
98 setOperationAction(ISD::STORE, MVT::i32, Custom);
99 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
100 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
101 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
102 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
104 setOperationAction(ISD::LOAD, MVT::i32, Custom);
105 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
106 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
108 setTargetDAGCombine(ISD::FP_ROUND);
109 setTargetDAGCombine(ISD::FP_TO_SINT);
110 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
111 setTargetDAGCombine(ISD::SELECT_CC);
112 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
114 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
116 setBooleanContents(ZeroOrNegativeOneBooleanContent);
117 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
118 setSchedulingPreference(Sched::Source);
121 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
122 MachineInstr * MI, MachineBasicBlock * BB) const {
123 MachineFunction * MF = BB->getParent();
124 MachineRegisterInfo &MRI = MF->getRegInfo();
125 MachineBasicBlock::iterator I = *MI;
126 const R600InstrInfo *TII =
127 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
129 switch (MI->getOpcode()) {
131 if (TII->isLDSInstr(MI->getOpcode()) &&
132 TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst) != -1) {
133 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
134 assert(DstIdx != -1);
135 MachineInstrBuilder NewMI;
136 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg())) {
137 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()),
139 TII->buildDefaultInstruction(*BB, I, AMDGPU::MOV,
140 MI->getOperand(0).getReg(),
143 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
144 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
146 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
147 NewMI.addOperand(MI->getOperand(i));
150 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
153 case AMDGPU::CLAMP_R600: {
154 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
156 MI->getOperand(0).getReg(),
157 MI->getOperand(1).getReg());
158 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
162 case AMDGPU::FABS_R600: {
163 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
165 MI->getOperand(0).getReg(),
166 MI->getOperand(1).getReg());
167 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
171 case AMDGPU::FNEG_R600: {
172 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
174 MI->getOperand(0).getReg(),
175 MI->getOperand(1).getReg());
176 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
180 case AMDGPU::MASK_WRITE: {
181 unsigned maskedRegister = MI->getOperand(0).getReg();
182 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
183 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
184 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
188 case AMDGPU::MOV_IMM_F32:
189 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
190 MI->getOperand(1).getFPImm()->getValueAPF()
191 .bitcastToAPInt().getZExtValue());
193 case AMDGPU::MOV_IMM_I32:
194 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
195 MI->getOperand(1).getImm());
197 case AMDGPU::CONST_COPY: {
198 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
199 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
200 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
201 MI->getOperand(1).getImm());
205 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
206 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
207 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
208 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
210 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
211 .addOperand(MI->getOperand(0))
212 .addOperand(MI->getOperand(1))
213 .addImm(EOP); // Set End of program bit
218 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
219 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
220 MachineOperand &RID = MI->getOperand(4);
221 MachineOperand &SID = MI->getOperand(5);
222 unsigned TextureId = MI->getOperand(6).getImm();
223 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
224 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
236 case 8: // ShadowRect
247 case 11: // Shadow1DArray
251 case 12: // Shadow2DArray
255 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
256 .addOperand(MI->getOperand(3))
274 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
275 .addOperand(MI->getOperand(2))
293 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
294 .addOperand(MI->getOperand(0))
295 .addOperand(MI->getOperand(1))
313 .addReg(T0, RegState::Implicit)
314 .addReg(T1, RegState::Implicit);
318 case AMDGPU::TXD_SHADOW: {
319 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
320 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
321 MachineOperand &RID = MI->getOperand(4);
322 MachineOperand &SID = MI->getOperand(5);
323 unsigned TextureId = MI->getOperand(6).getImm();
324 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
325 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
337 case 8: // ShadowRect
348 case 11: // Shadow1DArray
352 case 12: // Shadow2DArray
357 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
358 .addOperand(MI->getOperand(3))
376 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
377 .addOperand(MI->getOperand(2))
395 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
396 .addOperand(MI->getOperand(0))
397 .addOperand(MI->getOperand(1))
415 .addReg(T0, RegState::Implicit)
416 .addReg(T1, RegState::Implicit);
421 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
422 .addOperand(MI->getOperand(0));
425 case AMDGPU::BRANCH_COND_f32: {
426 MachineInstr *NewMI =
427 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
428 AMDGPU::PREDICATE_BIT)
429 .addOperand(MI->getOperand(1))
430 .addImm(OPCODE_IS_NOT_ZERO)
432 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
433 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
434 .addOperand(MI->getOperand(0))
435 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
439 case AMDGPU::BRANCH_COND_i32: {
440 MachineInstr *NewMI =
441 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
442 AMDGPU::PREDICATE_BIT)
443 .addOperand(MI->getOperand(1))
444 .addImm(OPCODE_IS_NOT_ZERO_INT)
446 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
447 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
448 .addOperand(MI->getOperand(0))
449 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
453 case AMDGPU::EG_ExportSwz:
454 case AMDGPU::R600_ExportSwz: {
455 // Instruction is left unmodified if its not the last one of its type
456 bool isLastInstructionOfItsType = true;
457 unsigned InstExportType = MI->getOperand(1).getImm();
458 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
459 EndBlock = BB->end(); NextExportInst != EndBlock;
460 NextExportInst = llvm::next(NextExportInst)) {
461 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
462 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
463 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
465 if (CurrentInstExportType == InstExportType) {
466 isLastInstructionOfItsType = false;
471 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
472 if (!EOP && !isLastInstructionOfItsType)
474 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
475 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
476 .addOperand(MI->getOperand(0))
477 .addOperand(MI->getOperand(1))
478 .addOperand(MI->getOperand(2))
479 .addOperand(MI->getOperand(3))
480 .addOperand(MI->getOperand(4))
481 .addOperand(MI->getOperand(5))
482 .addOperand(MI->getOperand(6))
487 case AMDGPU::RETURN: {
488 // RETURN instructions must have the live-out registers as implicit uses,
489 // otherwise they appear dead.
490 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
491 MachineInstrBuilder MIB(*MF, MI);
492 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
493 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
498 MI->eraseFromParent();
502 //===----------------------------------------------------------------------===//
503 // Custom DAG Lowering Operations
504 //===----------------------------------------------------------------------===//
506 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
507 MachineFunction &MF = DAG.getMachineFunction();
508 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
509 switch (Op.getOpcode()) {
510 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
512 case ISD::FSIN: return LowerTrig(Op, DAG);
513 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
514 case ISD::STORE: return LowerSTORE(Op, DAG);
515 case ISD::LOAD: return LowerLOAD(Op, DAG);
516 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
517 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
518 case ISD::INTRINSIC_VOID: {
519 SDValue Chain = Op.getOperand(0);
520 unsigned IntrinsicID =
521 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
522 switch (IntrinsicID) {
523 case AMDGPUIntrinsic::AMDGPU_store_output: {
524 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
525 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
526 MFI->LiveOuts.push_back(Reg);
527 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
529 case AMDGPUIntrinsic::R600_store_swizzle: {
530 const SDValue Args[8] = {
532 Op.getOperand(2), // Export Value
533 Op.getOperand(3), // ArrayBase
534 Op.getOperand(4), // Type
535 DAG.getConstant(0, MVT::i32), // SWZ_X
536 DAG.getConstant(1, MVT::i32), // SWZ_Y
537 DAG.getConstant(2, MVT::i32), // SWZ_Z
538 DAG.getConstant(3, MVT::i32) // SWZ_W
540 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(),
544 // default for switch(IntrinsicID)
547 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
550 case ISD::INTRINSIC_WO_CHAIN: {
551 unsigned IntrinsicID =
552 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
553 EVT VT = Op.getValueType();
555 switch(IntrinsicID) {
556 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
557 case AMDGPUIntrinsic::R600_load_input: {
558 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
559 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
560 MachineFunction &MF = DAG.getMachineFunction();
561 MachineRegisterInfo &MRI = MF.getRegInfo();
563 return DAG.getCopyFromReg(DAG.getEntryNode(),
564 SDLoc(DAG.getEntryNode()), Reg, VT);
567 case AMDGPUIntrinsic::R600_interp_input: {
568 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
569 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
570 MachineSDNode *interp;
572 const MachineFunction &MF = DAG.getMachineFunction();
573 const R600InstrInfo *TII =
574 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
575 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
576 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
577 return DAG.getTargetExtractSubreg(
578 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
579 DL, MVT::f32, SDValue(interp, 0));
582 MachineFunction &MF = DAG.getMachineFunction();
583 MachineRegisterInfo &MRI = MF.getRegInfo();
584 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
585 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
586 MRI.addLiveIn(RegisterI);
587 MRI.addLiveIn(RegisterJ);
588 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
589 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
590 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
591 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
594 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
595 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
596 RegisterJNode, RegisterINode);
598 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
599 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
600 RegisterJNode, RegisterINode);
601 return SDValue(interp, slot % 2);
603 case AMDGPUIntrinsic::R600_tex:
604 case AMDGPUIntrinsic::R600_texc:
605 case AMDGPUIntrinsic::R600_txl:
606 case AMDGPUIntrinsic::R600_txlc:
607 case AMDGPUIntrinsic::R600_txb:
608 case AMDGPUIntrinsic::R600_txbc:
609 case AMDGPUIntrinsic::R600_txf:
610 case AMDGPUIntrinsic::R600_txq:
611 case AMDGPUIntrinsic::R600_ddx:
612 case AMDGPUIntrinsic::R600_ddy: {
614 switch (IntrinsicID) {
615 case AMDGPUIntrinsic::R600_tex:
618 case AMDGPUIntrinsic::R600_texc:
621 case AMDGPUIntrinsic::R600_txl:
624 case AMDGPUIntrinsic::R600_txlc:
627 case AMDGPUIntrinsic::R600_txb:
630 case AMDGPUIntrinsic::R600_txbc:
633 case AMDGPUIntrinsic::R600_txf:
636 case AMDGPUIntrinsic::R600_txq:
639 case AMDGPUIntrinsic::R600_ddx:
642 case AMDGPUIntrinsic::R600_ddy:
646 llvm_unreachable("Unknow Texture Operation");
649 SDValue TexArgs[19] = {
650 DAG.getConstant(TextureOp, MVT::i32),
652 DAG.getConstant(0, MVT::i32),
653 DAG.getConstant(1, MVT::i32),
654 DAG.getConstant(2, MVT::i32),
655 DAG.getConstant(3, MVT::i32),
659 DAG.getConstant(0, MVT::i32),
660 DAG.getConstant(1, MVT::i32),
661 DAG.getConstant(2, MVT::i32),
662 DAG.getConstant(3, MVT::i32),
670 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs, 19);
672 case AMDGPUIntrinsic::AMDGPU_dp4: {
674 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
675 DAG.getConstant(0, MVT::i32)),
676 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
677 DAG.getConstant(0, MVT::i32)),
678 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
679 DAG.getConstant(1, MVT::i32)),
680 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
681 DAG.getConstant(1, MVT::i32)),
682 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
683 DAG.getConstant(2, MVT::i32)),
684 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
685 DAG.getConstant(2, MVT::i32)),
686 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
687 DAG.getConstant(3, MVT::i32)),
688 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
689 DAG.getConstant(3, MVT::i32))
691 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args, 8);
694 case Intrinsic::r600_read_ngroups_x:
695 return LowerImplicitParameter(DAG, VT, DL, 0);
696 case Intrinsic::r600_read_ngroups_y:
697 return LowerImplicitParameter(DAG, VT, DL, 1);
698 case Intrinsic::r600_read_ngroups_z:
699 return LowerImplicitParameter(DAG, VT, DL, 2);
700 case Intrinsic::r600_read_global_size_x:
701 return LowerImplicitParameter(DAG, VT, DL, 3);
702 case Intrinsic::r600_read_global_size_y:
703 return LowerImplicitParameter(DAG, VT, DL, 4);
704 case Intrinsic::r600_read_global_size_z:
705 return LowerImplicitParameter(DAG, VT, DL, 5);
706 case Intrinsic::r600_read_local_size_x:
707 return LowerImplicitParameter(DAG, VT, DL, 6);
708 case Intrinsic::r600_read_local_size_y:
709 return LowerImplicitParameter(DAG, VT, DL, 7);
710 case Intrinsic::r600_read_local_size_z:
711 return LowerImplicitParameter(DAG, VT, DL, 8);
713 case Intrinsic::r600_read_tgid_x:
714 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
716 case Intrinsic::r600_read_tgid_y:
717 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
719 case Intrinsic::r600_read_tgid_z:
720 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
722 case Intrinsic::r600_read_tidig_x:
723 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
725 case Intrinsic::r600_read_tidig_y:
726 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
728 case Intrinsic::r600_read_tidig_z:
729 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
732 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
735 } // end switch(Op.getOpcode())
739 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
740 SmallVectorImpl<SDValue> &Results,
741 SelectionDAG &DAG) const {
742 switch (N->getOpcode()) {
744 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
747 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
748 Results.push_back(SDValue(Node, 0));
749 Results.push_back(SDValue(Node, 1));
750 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
752 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
756 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
757 Results.push_back(SDValue(Node, 0));
762 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
763 // On hw >= R700, COS/SIN input must be between -1. and 1.
764 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
765 EVT VT = Op.getValueType();
766 SDValue Arg = Op.getOperand(0);
767 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
768 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
769 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
770 DAG.getConstantFP(0.15915494309, MVT::f32)),
771 DAG.getConstantFP(0.5, MVT::f32)));
773 switch (Op.getOpcode()) {
775 TrigNode = AMDGPUISD::COS_HW;
778 TrigNode = AMDGPUISD::SIN_HW;
781 llvm_unreachable("Wrong trig opcode");
783 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
784 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
785 DAG.getConstantFP(-0.5, MVT::f32)));
786 if (Gen >= AMDGPUSubtarget::R700)
788 // On R600 hw, COS/SIN input must be between -Pi and Pi.
789 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
790 DAG.getConstantFP(3.14159265359, MVT::f32));
793 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
798 Op, DAG.getConstantFP(0.0f, MVT::f32),
799 DAG.getCondCode(ISD::SETNE)
803 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
805 unsigned DwordOffset) const {
806 unsigned ByteOffset = DwordOffset * 4;
807 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
808 AMDGPUAS::CONSTANT_BUFFER_0);
810 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
811 assert(isInt<16>(ByteOffset));
813 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
814 DAG.getConstant(ByteOffset, MVT::i32), // PTR
815 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
816 false, false, false, 0);
819 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
821 MachineFunction &MF = DAG.getMachineFunction();
822 const AMDGPUFrameLowering *TFL =
823 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
825 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
828 unsigned FrameIndex = FIN->getIndex();
829 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
830 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
833 bool R600TargetLowering::isZero(SDValue Op) const {
834 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
835 return Cst->isNullValue();
836 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
837 return CstFP->isZero();
843 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
845 EVT VT = Op.getValueType();
847 SDValue LHS = Op.getOperand(0);
848 SDValue RHS = Op.getOperand(1);
849 SDValue True = Op.getOperand(2);
850 SDValue False = Op.getOperand(3);
851 SDValue CC = Op.getOperand(4);
854 // LHS and RHS are guaranteed to be the same value type
855 EVT CompareVT = LHS.getValueType();
857 // Check if we can lower this to a native operation.
859 // Try to lower to a SET* instruction:
861 // SET* can match the following patterns:
863 // select_cc f32, f32, -1, 0, cc_supported
864 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
865 // select_cc i32, i32, -1, 0, cc_supported
868 // Move hardware True/False values to the correct operand.
869 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
870 ISD::CondCode InverseCC =
871 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
872 if (isHWTrueValue(False) && isHWFalseValue(True)) {
873 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
874 std::swap(False, True);
875 CC = DAG.getCondCode(InverseCC);
877 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
878 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
879 std::swap(False, True);
881 CC = DAG.getCondCode(SwapInvCC);
886 if (isHWTrueValue(True) && isHWFalseValue(False) &&
887 (CompareVT == VT || VT == MVT::i32)) {
888 // This can be matched by a SET* instruction.
889 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
892 // Try to lower to a CND* instruction:
894 // CND* can match the following patterns:
896 // select_cc f32, 0.0, f32, f32, cc_supported
897 // select_cc f32, 0.0, i32, i32, cc_supported
898 // select_cc i32, 0, f32, f32, cc_supported
899 // select_cc i32, 0, i32, i32, cc_supported
902 // Try to move the zero value to the RHS
904 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
905 // Try swapping the operands
906 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
907 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
909 CC = DAG.getCondCode(CCSwapped);
911 // Try inverting the conditon and then swapping the operands
912 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
913 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
914 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
915 std::swap(True, False);
917 CC = DAG.getCondCode(CCSwapped);
924 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
925 if (CompareVT != VT) {
926 // Bitcast True / False to the correct types. This will end up being
927 // a nop, but it allows us to define only a single pattern in the
928 // .TD files for each CND* instruction rather than having to have
929 // one pattern for integer True/False and one for fp True/False
930 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
931 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
938 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
946 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
949 DAG.getCondCode(CCOpcode));
950 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
954 // Possible Min/Max pattern
955 SDValue MinMax = LowerMinMax(Op, DAG);
956 if (MinMax.getNode()) {
960 // If we make it this for it means we have no native instructions to handle
961 // this SELECT_CC, so we must lower it.
962 SDValue HWTrue, HWFalse;
964 if (CompareVT == MVT::f32) {
965 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
966 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
967 } else if (CompareVT == MVT::i32) {
968 HWTrue = DAG.getConstant(-1, CompareVT);
969 HWFalse = DAG.getConstant(0, CompareVT);
972 assert(!"Unhandled value type in LowerSELECT_CC");
975 // Lower this unsupported SELECT_CC into a combination of two supported
976 // SELECT_CC operations.
977 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
979 return DAG.getNode(ISD::SELECT_CC, DL, VT,
982 DAG.getCondCode(ISD::SETNE));
985 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
986 /// convert these pointers to a register index. Each register holds
987 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
988 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
989 /// for indirect addressing.
990 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
992 SelectionDAG &DAG) const {
1004 default: llvm_unreachable("Invalid stack width");
1007 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1008 DAG.getConstant(SRLPad, MVT::i32));
1011 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1014 unsigned &PtrIncr) const {
1015 switch (StackWidth) {
1026 Channel = ElemIdx % 2;
1040 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1042 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1043 SDValue Chain = Op.getOperand(0);
1044 SDValue Value = Op.getOperand(1);
1045 SDValue Ptr = Op.getOperand(2);
1047 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1048 if (Result.getNode()) {
1052 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1053 if (StoreNode->isTruncatingStore()) {
1054 EVT VT = Value.getValueType();
1055 assert(VT.bitsLE(MVT::i32));
1056 EVT MemVT = StoreNode->getMemoryVT();
1057 SDValue MaskConstant;
1058 if (MemVT == MVT::i8) {
1059 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1061 assert(MemVT == MVT::i16);
1062 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1064 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1065 DAG.getConstant(2, MVT::i32));
1066 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1067 DAG.getConstant(0x00000003, VT));
1068 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1069 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1070 DAG.getConstant(3, VT));
1071 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1072 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1073 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1077 DAG.getConstant(0, MVT::i32),
1078 DAG.getConstant(0, MVT::i32),
1081 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src, 4);
1082 SDValue Args[3] = { Chain, Input, DWordAddr };
1083 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1084 Op->getVTList(), Args, 3, MemVT,
1085 StoreNode->getMemOperand());
1086 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1087 Value.getValueType().bitsGE(MVT::i32)) {
1088 // Convert pointer from byte address to dword address.
1089 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1090 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1091 Ptr, DAG.getConstant(2, MVT::i32)));
1093 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1094 assert(!"Truncated and indexed stores not supported yet");
1096 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1102 EVT ValueVT = Value.getValueType();
1104 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1108 // Lowering for indirect addressing
1110 const MachineFunction &MF = DAG.getMachineFunction();
1111 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1112 getTargetMachine().getFrameLowering());
1113 unsigned StackWidth = TFL->getStackWidth(MF);
1115 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1117 if (ValueVT.isVector()) {
1118 unsigned NumElemVT = ValueVT.getVectorNumElements();
1119 EVT ElemVT = ValueVT.getVectorElementType();
1122 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1123 "vector width in load");
1125 for (unsigned i = 0; i < NumElemVT; ++i) {
1126 unsigned Channel, PtrIncr;
1127 getStackAddress(StackWidth, i, Channel, PtrIncr);
1128 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1129 DAG.getConstant(PtrIncr, MVT::i32));
1130 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1131 Value, DAG.getConstant(i, MVT::i32));
1133 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1135 DAG.getTargetConstant(Channel, MVT::i32));
1137 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1139 if (ValueVT == MVT::i8) {
1140 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1142 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1143 DAG.getTargetConstant(0, MVT::i32)); // Channel
1149 // return (512 + (kc_bank << 12)
1151 ConstantAddressBlock(unsigned AddressSpace) {
1152 switch (AddressSpace) {
1153 case AMDGPUAS::CONSTANT_BUFFER_0:
1155 case AMDGPUAS::CONSTANT_BUFFER_1:
1157 case AMDGPUAS::CONSTANT_BUFFER_2:
1158 return 512 + 4096 * 2;
1159 case AMDGPUAS::CONSTANT_BUFFER_3:
1160 return 512 + 4096 * 3;
1161 case AMDGPUAS::CONSTANT_BUFFER_4:
1162 return 512 + 4096 * 4;
1163 case AMDGPUAS::CONSTANT_BUFFER_5:
1164 return 512 + 4096 * 5;
1165 case AMDGPUAS::CONSTANT_BUFFER_6:
1166 return 512 + 4096 * 6;
1167 case AMDGPUAS::CONSTANT_BUFFER_7:
1168 return 512 + 4096 * 7;
1169 case AMDGPUAS::CONSTANT_BUFFER_8:
1170 return 512 + 4096 * 8;
1171 case AMDGPUAS::CONSTANT_BUFFER_9:
1172 return 512 + 4096 * 9;
1173 case AMDGPUAS::CONSTANT_BUFFER_10:
1174 return 512 + 4096 * 10;
1175 case AMDGPUAS::CONSTANT_BUFFER_11:
1176 return 512 + 4096 * 11;
1177 case AMDGPUAS::CONSTANT_BUFFER_12:
1178 return 512 + 4096 * 12;
1179 case AMDGPUAS::CONSTANT_BUFFER_13:
1180 return 512 + 4096 * 13;
1181 case AMDGPUAS::CONSTANT_BUFFER_14:
1182 return 512 + 4096 * 14;
1183 case AMDGPUAS::CONSTANT_BUFFER_15:
1184 return 512 + 4096 * 15;
1190 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1192 EVT VT = Op.getValueType();
1194 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1195 SDValue Chain = Op.getOperand(0);
1196 SDValue Ptr = Op.getOperand(1);
1197 SDValue LoweredLoad;
1199 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1200 SDValue MergedValues[2] = {
1201 SplitVectorLoad(Op, DAG),
1204 return DAG.getMergeValues(MergedValues, 2, DL);
1207 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1208 if (ConstantBlock > -1) {
1210 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1211 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1212 dyn_cast<ConstantSDNode>(Ptr)) {
1214 for (unsigned i = 0; i < 4; i++) {
1215 // We want Const position encoded with the following formula :
1216 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1217 // const_index is Ptr computed by llvm using an alignment of 16.
1218 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1219 // then div by 4 at the ISel step
1220 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1221 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1222 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1224 EVT NewVT = MVT::v4i32;
1225 unsigned NumElements = 4;
1226 if (VT.isVector()) {
1228 NumElements = VT.getVectorNumElements();
1230 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT, Slots, NumElements);
1232 // non constant ptr cant be folded, keeps it as a v4f32 load
1233 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1234 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1235 DAG.getConstant(LoadNode->getAddressSpace() -
1236 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1240 if (!VT.isVector()) {
1241 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1242 DAG.getConstant(0, MVT::i32));
1245 SDValue MergedValues[2] = {
1249 return DAG.getMergeValues(MergedValues, 2, DL);
1252 // For most operations returning SDValue() will result int he node being
1253 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so
1254 // we need to manually expand loads that may be legal in some address spaces
1255 // and illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported
1256 // for compute shaders, since the data is sign extended when it is uploaded
1257 // to the buffer. Howerver SEXT loads from other addresspaces are not
1258 // supported, so we need to expand them here.
1259 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1260 EVT MemVT = LoadNode->getMemoryVT();
1261 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1262 SDValue ShiftAmount =
1263 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1264 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1265 LoadNode->getPointerInfo(), MemVT,
1266 LoadNode->isVolatile(),
1267 LoadNode->isNonTemporal(),
1268 LoadNode->getAlignment());
1269 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1270 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1272 SDValue MergedValues[2] = { Sra, Chain };
1273 return DAG.getMergeValues(MergedValues, 2, DL);
1276 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1280 // Lowering for indirect addressing
1281 const MachineFunction &MF = DAG.getMachineFunction();
1282 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1283 getTargetMachine().getFrameLowering());
1284 unsigned StackWidth = TFL->getStackWidth(MF);
1286 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1288 if (VT.isVector()) {
1289 unsigned NumElemVT = VT.getVectorNumElements();
1290 EVT ElemVT = VT.getVectorElementType();
1293 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1294 "vector width in load");
1296 for (unsigned i = 0; i < NumElemVT; ++i) {
1297 unsigned Channel, PtrIncr;
1298 getStackAddress(StackWidth, i, Channel, PtrIncr);
1299 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1300 DAG.getConstant(PtrIncr, MVT::i32));
1301 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1303 DAG.getTargetConstant(Channel, MVT::i32),
1306 for (unsigned i = NumElemVT; i < 4; ++i) {
1307 Loads[i] = DAG.getUNDEF(ElemVT);
1309 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1310 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1312 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1314 DAG.getTargetConstant(0, MVT::i32), // Channel
1319 Ops[0] = LoweredLoad;
1322 return DAG.getMergeValues(Ops, 2, DL);
1325 /// XXX Only kernel functions are supported, so we can assume for now that
1326 /// every function is a kernel function, but in the future we should use
1327 /// separate calling conventions for kernel and non-kernel functions.
1328 SDValue R600TargetLowering::LowerFormalArguments(
1330 CallingConv::ID CallConv,
1332 const SmallVectorImpl<ISD::InputArg> &Ins,
1333 SDLoc DL, SelectionDAG &DAG,
1334 SmallVectorImpl<SDValue> &InVals) const {
1335 SmallVector<CCValAssign, 16> ArgLocs;
1336 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1337 getTargetMachine(), ArgLocs, *DAG.getContext());
1339 AnalyzeFormalArguments(CCInfo, Ins);
1341 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1342 CCValAssign &VA = ArgLocs[i];
1343 EVT VT = VA.getLocVT();
1345 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1346 AMDGPUAS::CONSTANT_BUFFER_0);
1348 // The first 36 bytes of the input buffer contains information about
1349 // thread group and global sizes.
1350 SDValue Arg = DAG.getLoad(VT, DL, Chain,
1351 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1352 MachinePointerInfo(UndefValue::get(PtrTy)), false,
1353 false, false, 4); // 4 is the prefered alignment for
1354 // the CONSTANT memory space.
1355 InVals.push_back(Arg);
1360 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1361 if (!VT.isVector()) return MVT::i32;
1362 return VT.changeVectorElementTypeToInteger();
1366 CompactSwizzlableVector(SelectionDAG &DAG, SDValue VectorEntry,
1367 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1368 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1369 assert(RemapSwizzle.empty());
1370 SDValue NewBldVec[4] = {
1371 VectorEntry.getOperand(0),
1372 VectorEntry.getOperand(1),
1373 VectorEntry.getOperand(2),
1374 VectorEntry.getOperand(3)
1377 for (unsigned i = 0; i < 4; i++) {
1378 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1380 RemapSwizzle[i] = 4; // SEL_0
1381 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1382 } else if (C->isExactlyValue(1.0)) {
1383 RemapSwizzle[i] = 5; // SEL_1
1384 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1388 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1390 for (unsigned j = 0; j < i; j++) {
1391 if (NewBldVec[i] == NewBldVec[j]) {
1392 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1393 RemapSwizzle[i] = j;
1399 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1400 VectorEntry.getValueType(), NewBldVec, 4);
1403 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1404 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1405 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1406 assert(RemapSwizzle.empty());
1407 SDValue NewBldVec[4] = {
1408 VectorEntry.getOperand(0),
1409 VectorEntry.getOperand(1),
1410 VectorEntry.getOperand(2),
1411 VectorEntry.getOperand(3)
1413 bool isUnmovable[4] = { false, false, false, false };
1414 for (unsigned i = 0; i < 4; i++)
1415 RemapSwizzle[i] = i;
1417 for (unsigned i = 0; i < 4; i++) {
1418 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1419 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1421 if (!isUnmovable[Idx]) {
1423 std::swap(NewBldVec[Idx], NewBldVec[i]);
1424 std::swap(RemapSwizzle[RemapSwizzle[Idx]], RemapSwizzle[RemapSwizzle[i]]);
1426 isUnmovable[Idx] = true;
1430 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1431 VectorEntry.getValueType(), NewBldVec, 4);
1435 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1436 SDValue Swz[4], SelectionDAG &DAG) const {
1437 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1438 // Old -> New swizzle values
1439 DenseMap<unsigned, unsigned> SwizzleRemap;
1441 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1442 for (unsigned i = 0; i < 4; i++) {
1443 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1444 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1445 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1448 SwizzleRemap.clear();
1449 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1450 for (unsigned i = 0; i < 4; i++) {
1451 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1452 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1453 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1460 //===----------------------------------------------------------------------===//
1461 // Custom DAG Optimizations
1462 //===----------------------------------------------------------------------===//
1464 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1465 DAGCombinerInfo &DCI) const {
1466 SelectionDAG &DAG = DCI.DAG;
1468 switch (N->getOpcode()) {
1469 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1470 case ISD::FP_ROUND: {
1471 SDValue Arg = N->getOperand(0);
1472 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1473 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1479 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1480 // (i32 select_cc f32, f32, -1, 0 cc)
1482 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1483 // this to one of the SET*_DX10 instructions.
1484 case ISD::FP_TO_SINT: {
1485 SDValue FNeg = N->getOperand(0);
1486 if (FNeg.getOpcode() != ISD::FNEG) {
1489 SDValue SelectCC = FNeg.getOperand(0);
1490 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1491 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1492 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1493 !isHWTrueValue(SelectCC.getOperand(2)) ||
1494 !isHWFalseValue(SelectCC.getOperand(3))) {
1498 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1499 SelectCC.getOperand(0), // LHS
1500 SelectCC.getOperand(1), // RHS
1501 DAG.getConstant(-1, MVT::i32), // True
1502 DAG.getConstant(0, MVT::i32), // Flase
1503 SelectCC.getOperand(4)); // CC
1508 // insert_vector_elt (build_vector elt0, …, eltN), NewEltIdx, idx
1509 // => build_vector elt0, …, NewEltIdx, …, eltN
1510 case ISD::INSERT_VECTOR_ELT: {
1511 SDValue InVec = N->getOperand(0);
1512 SDValue InVal = N->getOperand(1);
1513 SDValue EltNo = N->getOperand(2);
1516 // If the inserted element is an UNDEF, just use the input vector.
1517 if (InVal.getOpcode() == ISD::UNDEF)
1520 EVT VT = InVec.getValueType();
1522 // If we can't generate a legal BUILD_VECTOR, exit
1523 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1526 // Check that we know which element is being inserted
1527 if (!isa<ConstantSDNode>(EltNo))
1529 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1531 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1532 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1534 SmallVector<SDValue, 8> Ops;
1535 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1536 Ops.append(InVec.getNode()->op_begin(),
1537 InVec.getNode()->op_end());
1538 } else if (InVec.getOpcode() == ISD::UNDEF) {
1539 unsigned NElts = VT.getVectorNumElements();
1540 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1545 // Insert the element
1546 if (Elt < Ops.size()) {
1547 // All the operands of BUILD_VECTOR must have the same type;
1548 // we enforce that here.
1549 EVT OpVT = Ops[0].getValueType();
1550 if (InVal.getValueType() != OpVT)
1551 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1552 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1553 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1557 // Return the new vector
1558 return DAG.getNode(ISD::BUILD_VECTOR, dl,
1559 VT, &Ops[0], Ops.size());
1562 // Extract_vec (Build_vector) generated by custom lowering
1563 // also needs to be customly combined
1564 case ISD::EXTRACT_VECTOR_ELT: {
1565 SDValue Arg = N->getOperand(0);
1566 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1567 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1568 unsigned Element = Const->getZExtValue();
1569 return Arg->getOperand(Element);
1572 if (Arg.getOpcode() == ISD::BITCAST &&
1573 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1574 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1575 unsigned Element = Const->getZExtValue();
1576 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1577 Arg->getOperand(0).getOperand(Element));
1582 case ISD::SELECT_CC: {
1583 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1584 // selectcc x, y, a, b, inv(cc)
1586 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1587 // selectcc x, y, a, b, cc
1588 SDValue LHS = N->getOperand(0);
1589 if (LHS.getOpcode() != ISD::SELECT_CC) {
1593 SDValue RHS = N->getOperand(1);
1594 SDValue True = N->getOperand(2);
1595 SDValue False = N->getOperand(3);
1596 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1598 if (LHS.getOperand(2).getNode() != True.getNode() ||
1599 LHS.getOperand(3).getNode() != False.getNode() ||
1600 RHS.getNode() != False.getNode()) {
1605 default: return SDValue();
1606 case ISD::SETNE: return LHS;
1608 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1609 LHSCC = ISD::getSetCCInverse(LHSCC,
1610 LHS.getOperand(0).getValueType().isInteger());
1611 if (DCI.isBeforeLegalizeOps() ||
1612 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1613 return DAG.getSelectCC(SDLoc(N),
1625 case AMDGPUISD::EXPORT: {
1626 SDValue Arg = N->getOperand(1);
1627 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1630 SDValue NewArgs[8] = {
1631 N->getOperand(0), // Chain
1633 N->getOperand(2), // ArrayBase
1634 N->getOperand(3), // Type
1635 N->getOperand(4), // SWZ_X
1636 N->getOperand(5), // SWZ_Y
1637 N->getOperand(6), // SWZ_Z
1638 N->getOperand(7) // SWZ_W
1641 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1642 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1644 case AMDGPUISD::TEXTURE_FETCH: {
1645 SDValue Arg = N->getOperand(1);
1646 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1649 SDValue NewArgs[19] = {
1670 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1671 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1679 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
1680 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
1681 const R600InstrInfo *TII =
1682 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1683 if (!Src.isMachineOpcode())
1685 switch (Src.getMachineOpcode()) {
1686 case AMDGPU::FNEG_R600:
1689 Src = Src.getOperand(0);
1690 Neg = DAG.getTargetConstant(1, MVT::i32);
1692 case AMDGPU::FABS_R600:
1695 Src = Src.getOperand(0);
1696 Abs = DAG.getTargetConstant(1, MVT::i32);
1698 case AMDGPU::CONST_COPY: {
1699 unsigned Opcode = ParentNode->getMachineOpcode();
1700 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1705 SDValue CstOffset = Src.getOperand(0);
1706 if (ParentNode->getValueType(0).isVector())
1709 // Gather constants values
1710 int SrcIndices[] = {
1711 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1712 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1713 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1714 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1715 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1716 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1717 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1718 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1719 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1720 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1721 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1723 std::vector<unsigned> Consts;
1724 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
1725 int OtherSrcIdx = SrcIndices[i];
1726 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1727 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1733 if (RegisterSDNode *Reg =
1734 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1735 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1736 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(
1737 ParentNode->getOperand(OtherSelIdx));
1738 Consts.push_back(Cst->getZExtValue());
1743 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
1744 Consts.push_back(Cst->getZExtValue());
1745 if (!TII->fitsConstReadLimitations(Consts)) {
1750 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1753 case AMDGPU::MOV_IMM_I32:
1754 case AMDGPU::MOV_IMM_F32: {
1755 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1756 uint64_t ImmValue = 0;
1759 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1760 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1761 float FloatValue = FPC->getValueAPF().convertToFloat();
1762 if (FloatValue == 0.0) {
1763 ImmReg = AMDGPU::ZERO;
1764 } else if (FloatValue == 0.5) {
1765 ImmReg = AMDGPU::HALF;
1766 } else if (FloatValue == 1.0) {
1767 ImmReg = AMDGPU::ONE;
1769 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1772 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1773 uint64_t Value = C->getZExtValue();
1775 ImmReg = AMDGPU::ZERO;
1776 } else if (Value == 1) {
1777 ImmReg = AMDGPU::ONE_INT;
1783 // Check that we aren't already using an immediate.
1784 // XXX: It's possible for an instruction to have more than one
1785 // immediate operand, but this is not supported yet.
1786 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1789 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1791 if (C->getZExtValue())
1793 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1795 Src = DAG.getRegister(ImmReg, MVT::i32);
1804 /// \brief Fold the instructions after selecting them
1805 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1806 SelectionDAG &DAG) const {
1807 const R600InstrInfo *TII =
1808 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1809 if (!Node->isMachineOpcode())
1811 unsigned Opcode = Node->getMachineOpcode();
1814 std::vector<SDValue> Ops;
1815 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1819 if (Opcode == AMDGPU::DOT_4) {
1820 int OperandIdx[] = {
1821 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1822 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1823 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1824 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1825 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1826 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1827 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1828 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1831 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1832 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1833 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1834 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1835 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1836 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1837 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1838 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1841 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1842 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1843 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1844 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1845 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1846 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1847 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1848 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1850 for (unsigned i = 0; i < 8; i++) {
1851 if (OperandIdx[i] < 0)
1853 SDValue &Src = Ops[OperandIdx[i] - 1];
1854 SDValue &Neg = Ops[NegIdx[i] - 1];
1855 SDValue &Abs = Ops[AbsIdx[i] - 1];
1856 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1857 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1860 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1861 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1862 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1864 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1865 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1866 SDValue &Src = Ops[i];
1867 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1868 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1870 } else if (Opcode == AMDGPU::CLAMP_R600) {
1871 SDValue Src = Node->getOperand(0);
1872 if (!Src.isMachineOpcode() ||
1873 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1875 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1876 AMDGPU::OpName::clamp);
1879 std::vector<SDValue> Ops;
1880 unsigned NumOp = Src.getNumOperands();
1881 for(unsigned i = 0; i < NumOp; ++i)
1882 Ops.push_back(Src.getOperand(i));
1883 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1884 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1885 Node->getVTList(), Ops);
1887 if (!TII->hasInstrModifiers(Opcode))
1889 int OperandIdx[] = {
1890 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1891 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1892 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1895 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1896 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1897 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1900 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1901 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1904 for (unsigned i = 0; i < 3; i++) {
1905 if (OperandIdx[i] < 0)
1907 SDValue &Src = Ops[OperandIdx[i] - 1];
1908 SDValue &Neg = Ops[NegIdx[i] - 1];
1910 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
1911 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1912 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1913 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
1918 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1919 SDValue &Imm = Ops[ImmIdx];
1920 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
1921 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);