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 setCondCodeAction(ISD::SETLE, MVT::f32, Expand);
42 setCondCodeAction(ISD::SETLT, MVT::f32, Expand);
43 setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
44 setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
45 setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
46 setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
48 setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
49 setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
50 setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
51 setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
53 setOperationAction(ISD::FCOS, MVT::f32, Custom);
54 setOperationAction(ISD::FSIN, MVT::f32, Custom);
56 setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
57 setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
59 setOperationAction(ISD::BR_CC, MVT::i32, Expand);
60 setOperationAction(ISD::BR_CC, MVT::f32, Expand);
62 setOperationAction(ISD::FSUB, MVT::f32, Expand);
64 setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
65 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
66 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
68 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
69 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
71 setOperationAction(ISD::SETCC, MVT::i32, Expand);
72 setOperationAction(ISD::SETCC, MVT::f32, Expand);
73 setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
75 setOperationAction(ISD::SELECT, MVT::i32, Expand);
76 setOperationAction(ISD::SELECT, MVT::f32, Expand);
77 setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
78 setOperationAction(ISD::SELECT, MVT::v2f32, Expand);
79 setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
80 setOperationAction(ISD::SELECT, MVT::v4f32, Expand);
82 // Legalize loads and stores to the private address space.
83 setOperationAction(ISD::LOAD, MVT::i32, Custom);
84 setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
85 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
86 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
87 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
88 setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
89 setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
90 setOperationAction(ISD::STORE, MVT::i8, Custom);
91 setOperationAction(ISD::STORE, MVT::i32, Custom);
92 setOperationAction(ISD::STORE, MVT::v2i32, Custom);
93 setOperationAction(ISD::STORE, MVT::v4i32, Custom);
94 setTruncStoreAction(MVT::i32, MVT::i8, Custom);
95 setTruncStoreAction(MVT::i32, MVT::i16, Custom);
97 setOperationAction(ISD::LOAD, MVT::i32, Custom);
98 setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
99 setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
101 setTargetDAGCombine(ISD::FP_ROUND);
102 setTargetDAGCombine(ISD::FP_TO_SINT);
103 setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
104 setTargetDAGCombine(ISD::SELECT_CC);
105 setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
107 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
109 setBooleanContents(ZeroOrNegativeOneBooleanContent);
110 setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
111 setSchedulingPreference(Sched::Source);
114 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
115 MachineInstr * MI, MachineBasicBlock * BB) const {
116 MachineFunction * MF = BB->getParent();
117 MachineRegisterInfo &MRI = MF->getRegInfo();
118 MachineBasicBlock::iterator I = *MI;
119 const R600InstrInfo *TII =
120 static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
122 switch (MI->getOpcode()) {
124 if (TII->isLDSInstr(MI->getOpcode()) &&
125 TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst) != -1) {
126 int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
127 assert(DstIdx != -1);
128 MachineInstrBuilder NewMI;
129 if (!MRI.use_empty(MI->getOperand(DstIdx).getReg())) {
130 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()),
132 TII->buildDefaultInstruction(*BB, I, AMDGPU::MOV,
133 MI->getOperand(0).getReg(),
136 NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
137 TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
139 for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
140 NewMI.addOperand(MI->getOperand(i));
143 return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
146 case AMDGPU::CLAMP_R600: {
147 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
149 MI->getOperand(0).getReg(),
150 MI->getOperand(1).getReg());
151 TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
155 case AMDGPU::FABS_R600: {
156 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
158 MI->getOperand(0).getReg(),
159 MI->getOperand(1).getReg());
160 TII->addFlag(NewMI, 0, MO_FLAG_ABS);
164 case AMDGPU::FNEG_R600: {
165 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
167 MI->getOperand(0).getReg(),
168 MI->getOperand(1).getReg());
169 TII->addFlag(NewMI, 0, MO_FLAG_NEG);
173 case AMDGPU::MASK_WRITE: {
174 unsigned maskedRegister = MI->getOperand(0).getReg();
175 assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
176 MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
177 TII->addFlag(defInstr, 0, MO_FLAG_MASK);
181 case AMDGPU::MOV_IMM_F32:
182 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
183 MI->getOperand(1).getFPImm()->getValueAPF()
184 .bitcastToAPInt().getZExtValue());
186 case AMDGPU::MOV_IMM_I32:
187 TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
188 MI->getOperand(1).getImm());
190 case AMDGPU::CONST_COPY: {
191 MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
192 MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
193 TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
194 MI->getOperand(1).getImm());
198 case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
199 case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
200 case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
201 unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
203 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
204 .addOperand(MI->getOperand(0))
205 .addOperand(MI->getOperand(1))
206 .addImm(EOP); // Set End of program bit
211 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
212 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
213 MachineOperand &RID = MI->getOperand(4);
214 MachineOperand &SID = MI->getOperand(5);
215 unsigned TextureId = MI->getOperand(6).getImm();
216 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
217 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
229 case 8: // ShadowRect
240 case 11: // Shadow1DArray
244 case 12: // Shadow2DArray
248 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
249 .addOperand(MI->getOperand(3))
267 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
268 .addOperand(MI->getOperand(2))
286 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
287 .addOperand(MI->getOperand(0))
288 .addOperand(MI->getOperand(1))
306 .addReg(T0, RegState::Implicit)
307 .addReg(T1, RegState::Implicit);
311 case AMDGPU::TXD_SHADOW: {
312 unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
313 unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
314 MachineOperand &RID = MI->getOperand(4);
315 MachineOperand &SID = MI->getOperand(5);
316 unsigned TextureId = MI->getOperand(6).getImm();
317 unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
318 unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
330 case 8: // ShadowRect
341 case 11: // Shadow1DArray
345 case 12: // Shadow2DArray
350 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
351 .addOperand(MI->getOperand(3))
369 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
370 .addOperand(MI->getOperand(2))
388 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
389 .addOperand(MI->getOperand(0))
390 .addOperand(MI->getOperand(1))
408 .addReg(T0, RegState::Implicit)
409 .addReg(T1, RegState::Implicit);
414 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
415 .addOperand(MI->getOperand(0));
418 case AMDGPU::BRANCH_COND_f32: {
419 MachineInstr *NewMI =
420 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
421 AMDGPU::PREDICATE_BIT)
422 .addOperand(MI->getOperand(1))
423 .addImm(OPCODE_IS_NOT_ZERO)
425 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
426 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
427 .addOperand(MI->getOperand(0))
428 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
432 case AMDGPU::BRANCH_COND_i32: {
433 MachineInstr *NewMI =
434 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
435 AMDGPU::PREDICATE_BIT)
436 .addOperand(MI->getOperand(1))
437 .addImm(OPCODE_IS_NOT_ZERO_INT)
439 TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
440 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
441 .addOperand(MI->getOperand(0))
442 .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
446 case AMDGPU::EG_ExportSwz:
447 case AMDGPU::R600_ExportSwz: {
448 // Instruction is left unmodified if its not the last one of its type
449 bool isLastInstructionOfItsType = true;
450 unsigned InstExportType = MI->getOperand(1).getImm();
451 for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
452 EndBlock = BB->end(); NextExportInst != EndBlock;
453 NextExportInst = llvm::next(NextExportInst)) {
454 if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
455 NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
456 unsigned CurrentInstExportType = NextExportInst->getOperand(1)
458 if (CurrentInstExportType == InstExportType) {
459 isLastInstructionOfItsType = false;
464 bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
465 if (!EOP && !isLastInstructionOfItsType)
467 unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
468 BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
469 .addOperand(MI->getOperand(0))
470 .addOperand(MI->getOperand(1))
471 .addOperand(MI->getOperand(2))
472 .addOperand(MI->getOperand(3))
473 .addOperand(MI->getOperand(4))
474 .addOperand(MI->getOperand(5))
475 .addOperand(MI->getOperand(6))
480 case AMDGPU::RETURN: {
481 // RETURN instructions must have the live-out registers as implicit uses,
482 // otherwise they appear dead.
483 R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
484 MachineInstrBuilder MIB(*MF, MI);
485 for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
486 MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
491 MI->eraseFromParent();
495 //===----------------------------------------------------------------------===//
496 // Custom DAG Lowering Operations
497 //===----------------------------------------------------------------------===//
499 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
500 MachineFunction &MF = DAG.getMachineFunction();
501 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
502 switch (Op.getOpcode()) {
503 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
505 case ISD::FSIN: return LowerTrig(Op, DAG);
506 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
507 case ISD::STORE: return LowerSTORE(Op, DAG);
508 case ISD::LOAD: return LowerLOAD(Op, DAG);
509 case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
510 case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
511 case ISD::INTRINSIC_VOID: {
512 SDValue Chain = Op.getOperand(0);
513 unsigned IntrinsicID =
514 cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
515 switch (IntrinsicID) {
516 case AMDGPUIntrinsic::AMDGPU_store_output: {
517 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
518 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
519 MFI->LiveOuts.push_back(Reg);
520 return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
522 case AMDGPUIntrinsic::R600_store_swizzle: {
523 const SDValue Args[8] = {
525 Op.getOperand(2), // Export Value
526 Op.getOperand(3), // ArrayBase
527 Op.getOperand(4), // Type
528 DAG.getConstant(0, MVT::i32), // SWZ_X
529 DAG.getConstant(1, MVT::i32), // SWZ_Y
530 DAG.getConstant(2, MVT::i32), // SWZ_Z
531 DAG.getConstant(3, MVT::i32) // SWZ_W
533 return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(),
537 // default for switch(IntrinsicID)
540 // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
543 case ISD::INTRINSIC_WO_CHAIN: {
544 unsigned IntrinsicID =
545 cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
546 EVT VT = Op.getValueType();
548 switch(IntrinsicID) {
549 default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
550 case AMDGPUIntrinsic::R600_load_input: {
551 int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
552 unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
553 MachineFunction &MF = DAG.getMachineFunction();
554 MachineRegisterInfo &MRI = MF.getRegInfo();
556 return DAG.getCopyFromReg(DAG.getEntryNode(),
557 SDLoc(DAG.getEntryNode()), Reg, VT);
560 case AMDGPUIntrinsic::R600_interp_input: {
561 int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
562 int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
563 MachineSDNode *interp;
565 const MachineFunction &MF = DAG.getMachineFunction();
566 const R600InstrInfo *TII =
567 static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
568 interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
569 MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
570 return DAG.getTargetExtractSubreg(
571 TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
572 DL, MVT::f32, SDValue(interp, 0));
575 MachineFunction &MF = DAG.getMachineFunction();
576 MachineRegisterInfo &MRI = MF.getRegInfo();
577 unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
578 unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
579 MRI.addLiveIn(RegisterI);
580 MRI.addLiveIn(RegisterJ);
581 SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
582 SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
583 SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
584 SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
587 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
588 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
589 RegisterJNode, RegisterINode);
591 interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
592 MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
593 RegisterJNode, RegisterINode);
594 return SDValue(interp, slot % 2);
596 case AMDGPUIntrinsic::R600_tex:
597 case AMDGPUIntrinsic::R600_texc:
598 case AMDGPUIntrinsic::R600_txl:
599 case AMDGPUIntrinsic::R600_txlc:
600 case AMDGPUIntrinsic::R600_txb:
601 case AMDGPUIntrinsic::R600_txbc:
602 case AMDGPUIntrinsic::R600_txf:
603 case AMDGPUIntrinsic::R600_txq:
604 case AMDGPUIntrinsic::R600_ddx:
605 case AMDGPUIntrinsic::R600_ddy: {
607 switch (IntrinsicID) {
608 case AMDGPUIntrinsic::R600_tex:
611 case AMDGPUIntrinsic::R600_texc:
614 case AMDGPUIntrinsic::R600_txl:
617 case AMDGPUIntrinsic::R600_txlc:
620 case AMDGPUIntrinsic::R600_txb:
623 case AMDGPUIntrinsic::R600_txbc:
626 case AMDGPUIntrinsic::R600_txf:
629 case AMDGPUIntrinsic::R600_txq:
632 case AMDGPUIntrinsic::R600_ddx:
635 case AMDGPUIntrinsic::R600_ddy:
639 llvm_unreachable("Unknow Texture Operation");
642 SDValue TexArgs[19] = {
643 DAG.getConstant(TextureOp, MVT::i32),
645 DAG.getConstant(0, MVT::i32),
646 DAG.getConstant(1, MVT::i32),
647 DAG.getConstant(2, MVT::i32),
648 DAG.getConstant(3, 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),
663 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs, 19);
665 case AMDGPUIntrinsic::AMDGPU_dp4: {
667 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
668 DAG.getConstant(0, MVT::i32)),
669 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
670 DAG.getConstant(0, MVT::i32)),
671 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
672 DAG.getConstant(1, MVT::i32)),
673 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
674 DAG.getConstant(1, MVT::i32)),
675 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
676 DAG.getConstant(2, MVT::i32)),
677 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
678 DAG.getConstant(2, MVT::i32)),
679 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
680 DAG.getConstant(3, MVT::i32)),
681 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
682 DAG.getConstant(3, MVT::i32))
684 return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args, 8);
687 case Intrinsic::r600_read_ngroups_x:
688 return LowerImplicitParameter(DAG, VT, DL, 0);
689 case Intrinsic::r600_read_ngroups_y:
690 return LowerImplicitParameter(DAG, VT, DL, 1);
691 case Intrinsic::r600_read_ngroups_z:
692 return LowerImplicitParameter(DAG, VT, DL, 2);
693 case Intrinsic::r600_read_global_size_x:
694 return LowerImplicitParameter(DAG, VT, DL, 3);
695 case Intrinsic::r600_read_global_size_y:
696 return LowerImplicitParameter(DAG, VT, DL, 4);
697 case Intrinsic::r600_read_global_size_z:
698 return LowerImplicitParameter(DAG, VT, DL, 5);
699 case Intrinsic::r600_read_local_size_x:
700 return LowerImplicitParameter(DAG, VT, DL, 6);
701 case Intrinsic::r600_read_local_size_y:
702 return LowerImplicitParameter(DAG, VT, DL, 7);
703 case Intrinsic::r600_read_local_size_z:
704 return LowerImplicitParameter(DAG, VT, DL, 8);
706 case Intrinsic::r600_read_tgid_x:
707 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
709 case Intrinsic::r600_read_tgid_y:
710 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
712 case Intrinsic::r600_read_tgid_z:
713 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
715 case Intrinsic::r600_read_tidig_x:
716 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
718 case Intrinsic::r600_read_tidig_y:
719 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
721 case Intrinsic::r600_read_tidig_z:
722 return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
725 // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
728 } // end switch(Op.getOpcode())
732 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
733 SmallVectorImpl<SDValue> &Results,
734 SelectionDAG &DAG) const {
735 switch (N->getOpcode()) {
737 case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
740 SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
741 Results.push_back(SDValue(Node, 0));
742 Results.push_back(SDValue(Node, 1));
743 // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
745 DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
749 SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
750 Results.push_back(SDValue(Node, 0));
755 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
756 // On hw >= R700, COS/SIN input must be between -1. and 1.
757 // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
758 EVT VT = Op.getValueType();
759 SDValue Arg = Op.getOperand(0);
760 SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
761 DAG.getNode(ISD::FADD, SDLoc(Op), VT,
762 DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
763 DAG.getConstantFP(0.15915494309, MVT::f32)),
764 DAG.getConstantFP(0.5, MVT::f32)));
766 switch (Op.getOpcode()) {
768 TrigNode = AMDGPUISD::COS_HW;
771 TrigNode = AMDGPUISD::SIN_HW;
774 llvm_unreachable("Wrong trig opcode");
776 SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
777 DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
778 DAG.getConstantFP(-0.5, MVT::f32)));
779 if (Gen >= AMDGPUSubtarget::R700)
781 // On R600 hw, COS/SIN input must be between -Pi and Pi.
782 return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
783 DAG.getConstantFP(3.14159265359, MVT::f32));
786 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
791 Op, DAG.getConstantFP(0.0f, MVT::f32),
792 DAG.getCondCode(ISD::SETNE)
796 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
798 unsigned DwordOffset) const {
799 unsigned ByteOffset = DwordOffset * 4;
800 PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
801 AMDGPUAS::CONSTANT_BUFFER_0);
803 // We shouldn't be using an offset wider than 16-bits for implicit parameters.
804 assert(isInt<16>(ByteOffset));
806 return DAG.getLoad(VT, DL, DAG.getEntryNode(),
807 DAG.getConstant(ByteOffset, MVT::i32), // PTR
808 MachinePointerInfo(ConstantPointerNull::get(PtrType)),
809 false, false, false, 0);
812 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
814 MachineFunction &MF = DAG.getMachineFunction();
815 const AMDGPUFrameLowering *TFL =
816 static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
818 FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
821 unsigned FrameIndex = FIN->getIndex();
822 unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
823 return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
826 bool R600TargetLowering::isZero(SDValue Op) const {
827 if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
828 return Cst->isNullValue();
829 } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
830 return CstFP->isZero();
836 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
838 EVT VT = Op.getValueType();
840 SDValue LHS = Op.getOperand(0);
841 SDValue RHS = Op.getOperand(1);
842 SDValue True = Op.getOperand(2);
843 SDValue False = Op.getOperand(3);
844 SDValue CC = Op.getOperand(4);
847 // LHS and RHS are guaranteed to be the same value type
848 EVT CompareVT = LHS.getValueType();
850 // Check if we can lower this to a native operation.
852 // Try to lower to a SET* instruction:
854 // SET* can match the following patterns:
856 // select_cc f32, f32, -1, 0, cc_supported
857 // select_cc f32, f32, 1.0f, 0.0f, cc_supported
858 // select_cc i32, i32, -1, 0, cc_supported
861 // Move hardware True/False values to the correct operand.
862 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
863 ISD::CondCode InverseCC =
864 ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
865 if (isHWTrueValue(False) && isHWFalseValue(True)) {
866 if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
867 std::swap(False, True);
868 CC = DAG.getCondCode(InverseCC);
870 ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
871 if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
872 std::swap(False, True);
874 CC = DAG.getCondCode(SwapInvCC);
879 if (isHWTrueValue(True) && isHWFalseValue(False) &&
880 (CompareVT == VT || VT == MVT::i32)) {
881 // This can be matched by a SET* instruction.
882 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
885 // Try to lower to a CND* instruction:
887 // CND* can match the following patterns:
889 // select_cc f32, 0.0, f32, f32, cc_supported
890 // select_cc f32, 0.0, i32, i32, cc_supported
891 // select_cc i32, 0, f32, f32, cc_supported
892 // select_cc i32, 0, i32, i32, cc_supported
895 // Try to move the zero value to the RHS
897 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
898 // Try swapping the operands
899 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
900 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
902 CC = DAG.getCondCode(CCSwapped);
904 // Try inverting the conditon and then swapping the operands
905 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
906 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
907 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
908 std::swap(True, False);
910 CC = DAG.getCondCode(CCSwapped);
917 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
918 if (CompareVT != VT) {
919 // Bitcast True / False to the correct types. This will end up being
920 // a nop, but it allows us to define only a single pattern in the
921 // .TD files for each CND* instruction rather than having to have
922 // one pattern for integer True/False and one for fp True/False
923 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
924 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
931 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
939 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
942 DAG.getCondCode(CCOpcode));
943 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
947 // Possible Min/Max pattern
948 SDValue MinMax = LowerMinMax(Op, DAG);
949 if (MinMax.getNode()) {
953 // If we make it this for it means we have no native instructions to handle
954 // this SELECT_CC, so we must lower it.
955 SDValue HWTrue, HWFalse;
957 if (CompareVT == MVT::f32) {
958 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
959 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
960 } else if (CompareVT == MVT::i32) {
961 HWTrue = DAG.getConstant(-1, CompareVT);
962 HWFalse = DAG.getConstant(0, CompareVT);
965 assert(!"Unhandled value type in LowerSELECT_CC");
968 // Lower this unsupported SELECT_CC into a combination of two supported
969 // SELECT_CC operations.
970 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
972 return DAG.getNode(ISD::SELECT_CC, DL, VT,
975 DAG.getCondCode(ISD::SETNE));
978 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
979 /// convert these pointers to a register index. Each register holds
980 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
981 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
982 /// for indirect addressing.
983 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
985 SelectionDAG &DAG) const {
997 default: llvm_unreachable("Invalid stack width");
1000 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
1001 DAG.getConstant(SRLPad, MVT::i32));
1004 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1007 unsigned &PtrIncr) const {
1008 switch (StackWidth) {
1019 Channel = ElemIdx % 2;
1033 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1035 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1036 SDValue Chain = Op.getOperand(0);
1037 SDValue Value = Op.getOperand(1);
1038 SDValue Ptr = Op.getOperand(2);
1040 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1041 if (Result.getNode()) {
1045 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1046 if (StoreNode->isTruncatingStore()) {
1047 EVT VT = Value.getValueType();
1048 assert(VT.bitsLE(MVT::i32));
1049 EVT MemVT = StoreNode->getMemoryVT();
1050 SDValue MaskConstant;
1051 if (MemVT == MVT::i8) {
1052 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1054 assert(MemVT == MVT::i16);
1055 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1057 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1058 DAG.getConstant(2, MVT::i32));
1059 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1060 DAG.getConstant(0x00000003, VT));
1061 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1062 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1063 DAG.getConstant(3, VT));
1064 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1065 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1066 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1070 DAG.getConstant(0, MVT::i32),
1071 DAG.getConstant(0, MVT::i32),
1074 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src, 4);
1075 SDValue Args[3] = { Chain, Input, DWordAddr };
1076 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1077 Op->getVTList(), Args, 3, MemVT,
1078 StoreNode->getMemOperand());
1079 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1080 Value.getValueType().bitsGE(MVT::i32)) {
1081 // Convert pointer from byte address to dword address.
1082 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1083 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1084 Ptr, DAG.getConstant(2, MVT::i32)));
1086 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1087 assert(!"Truncated and indexed stores not supported yet");
1089 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1095 EVT ValueVT = Value.getValueType();
1097 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1101 // Lowering for indirect addressing
1103 const MachineFunction &MF = DAG.getMachineFunction();
1104 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1105 getTargetMachine().getFrameLowering());
1106 unsigned StackWidth = TFL->getStackWidth(MF);
1108 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1110 if (ValueVT.isVector()) {
1111 unsigned NumElemVT = ValueVT.getVectorNumElements();
1112 EVT ElemVT = ValueVT.getVectorElementType();
1115 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1116 "vector width in load");
1118 for (unsigned i = 0; i < NumElemVT; ++i) {
1119 unsigned Channel, PtrIncr;
1120 getStackAddress(StackWidth, i, Channel, PtrIncr);
1121 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1122 DAG.getConstant(PtrIncr, MVT::i32));
1123 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1124 Value, DAG.getConstant(i, MVT::i32));
1126 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1128 DAG.getTargetConstant(Channel, MVT::i32));
1130 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1132 if (ValueVT == MVT::i8) {
1133 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1135 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1136 DAG.getTargetConstant(0, MVT::i32)); // Channel
1142 // return (512 + (kc_bank << 12)
1144 ConstantAddressBlock(unsigned AddressSpace) {
1145 switch (AddressSpace) {
1146 case AMDGPUAS::CONSTANT_BUFFER_0:
1148 case AMDGPUAS::CONSTANT_BUFFER_1:
1150 case AMDGPUAS::CONSTANT_BUFFER_2:
1151 return 512 + 4096 * 2;
1152 case AMDGPUAS::CONSTANT_BUFFER_3:
1153 return 512 + 4096 * 3;
1154 case AMDGPUAS::CONSTANT_BUFFER_4:
1155 return 512 + 4096 * 4;
1156 case AMDGPUAS::CONSTANT_BUFFER_5:
1157 return 512 + 4096 * 5;
1158 case AMDGPUAS::CONSTANT_BUFFER_6:
1159 return 512 + 4096 * 6;
1160 case AMDGPUAS::CONSTANT_BUFFER_7:
1161 return 512 + 4096 * 7;
1162 case AMDGPUAS::CONSTANT_BUFFER_8:
1163 return 512 + 4096 * 8;
1164 case AMDGPUAS::CONSTANT_BUFFER_9:
1165 return 512 + 4096 * 9;
1166 case AMDGPUAS::CONSTANT_BUFFER_10:
1167 return 512 + 4096 * 10;
1168 case AMDGPUAS::CONSTANT_BUFFER_11:
1169 return 512 + 4096 * 11;
1170 case AMDGPUAS::CONSTANT_BUFFER_12:
1171 return 512 + 4096 * 12;
1172 case AMDGPUAS::CONSTANT_BUFFER_13:
1173 return 512 + 4096 * 13;
1174 case AMDGPUAS::CONSTANT_BUFFER_14:
1175 return 512 + 4096 * 14;
1176 case AMDGPUAS::CONSTANT_BUFFER_15:
1177 return 512 + 4096 * 15;
1183 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1185 EVT VT = Op.getValueType();
1187 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1188 SDValue Chain = Op.getOperand(0);
1189 SDValue Ptr = Op.getOperand(1);
1190 SDValue LoweredLoad;
1192 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1193 SDValue MergedValues[2] = {
1194 SplitVectorLoad(Op, DAG),
1197 return DAG.getMergeValues(MergedValues, 2, DL);
1200 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1201 if (ConstantBlock > -1) {
1203 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1204 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1205 dyn_cast<ConstantSDNode>(Ptr)) {
1207 for (unsigned i = 0; i < 4; i++) {
1208 // We want Const position encoded with the following formula :
1209 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1210 // const_index is Ptr computed by llvm using an alignment of 16.
1211 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1212 // then div by 4 at the ISel step
1213 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1214 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1215 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1217 EVT NewVT = MVT::v4i32;
1218 unsigned NumElements = 4;
1219 if (VT.isVector()) {
1221 NumElements = VT.getVectorNumElements();
1223 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT, Slots, NumElements);
1225 // non constant ptr cant be folded, keeps it as a v4f32 load
1226 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1227 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1228 DAG.getConstant(LoadNode->getAddressSpace() -
1229 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1233 if (!VT.isVector()) {
1234 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1235 DAG.getConstant(0, MVT::i32));
1238 SDValue MergedValues[2] = {
1242 return DAG.getMergeValues(MergedValues, 2, DL);
1245 // For most operations returning SDValue() will result int he node being
1246 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so
1247 // we need to manually expand loads that may be legal in some address spaces
1248 // and illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported
1249 // for compute shaders, since the data is sign extended when it is uploaded
1250 // to the buffer. Howerver SEXT loads from other addresspaces are not
1251 // supported, so we need to expand them here.
1252 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1253 EVT MemVT = LoadNode->getMemoryVT();
1254 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1255 SDValue ShiftAmount =
1256 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1257 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1258 LoadNode->getPointerInfo(), MemVT,
1259 LoadNode->isVolatile(),
1260 LoadNode->isNonTemporal(),
1261 LoadNode->getAlignment());
1262 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1263 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1265 SDValue MergedValues[2] = { Sra, Chain };
1266 return DAG.getMergeValues(MergedValues, 2, DL);
1269 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1273 // Lowering for indirect addressing
1274 const MachineFunction &MF = DAG.getMachineFunction();
1275 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1276 getTargetMachine().getFrameLowering());
1277 unsigned StackWidth = TFL->getStackWidth(MF);
1279 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1281 if (VT.isVector()) {
1282 unsigned NumElemVT = VT.getVectorNumElements();
1283 EVT ElemVT = VT.getVectorElementType();
1286 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1287 "vector width in load");
1289 for (unsigned i = 0; i < NumElemVT; ++i) {
1290 unsigned Channel, PtrIncr;
1291 getStackAddress(StackWidth, i, Channel, PtrIncr);
1292 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1293 DAG.getConstant(PtrIncr, MVT::i32));
1294 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1296 DAG.getTargetConstant(Channel, MVT::i32),
1299 for (unsigned i = NumElemVT; i < 4; ++i) {
1300 Loads[i] = DAG.getUNDEF(ElemVT);
1302 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1303 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1305 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1307 DAG.getTargetConstant(0, MVT::i32), // Channel
1312 Ops[0] = LoweredLoad;
1315 return DAG.getMergeValues(Ops, 2, DL);
1318 /// XXX Only kernel functions are supported, so we can assume for now that
1319 /// every function is a kernel function, but in the future we should use
1320 /// separate calling conventions for kernel and non-kernel functions.
1321 SDValue R600TargetLowering::LowerFormalArguments(
1323 CallingConv::ID CallConv,
1325 const SmallVectorImpl<ISD::InputArg> &Ins,
1326 SDLoc DL, SelectionDAG &DAG,
1327 SmallVectorImpl<SDValue> &InVals) const {
1328 SmallVector<CCValAssign, 16> ArgLocs;
1329 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1330 getTargetMachine(), ArgLocs, *DAG.getContext());
1332 AnalyzeFormalArguments(CCInfo, Ins);
1334 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1335 CCValAssign &VA = ArgLocs[i];
1336 EVT VT = VA.getLocVT();
1338 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1339 AMDGPUAS::CONSTANT_BUFFER_0);
1341 // The first 36 bytes of the input buffer contains information about
1342 // thread group and global sizes.
1343 SDValue Arg = DAG.getLoad(VT, DL, Chain,
1344 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1345 MachinePointerInfo(UndefValue::get(PtrTy)), false,
1346 false, false, 4); // 4 is the prefered alignment for
1347 // the CONSTANT memory space.
1348 InVals.push_back(Arg);
1353 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1354 if (!VT.isVector()) return MVT::i32;
1355 return VT.changeVectorElementTypeToInteger();
1359 CompactSwizzlableVector(SelectionDAG &DAG, SDValue VectorEntry,
1360 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1361 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1362 assert(RemapSwizzle.empty());
1363 SDValue NewBldVec[4] = {
1364 VectorEntry.getOperand(0),
1365 VectorEntry.getOperand(1),
1366 VectorEntry.getOperand(2),
1367 VectorEntry.getOperand(3)
1370 for (unsigned i = 0; i < 4; i++) {
1371 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1373 RemapSwizzle[i] = 4; // SEL_0
1374 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1375 } else if (C->isExactlyValue(1.0)) {
1376 RemapSwizzle[i] = 5; // SEL_1
1377 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1381 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1383 for (unsigned j = 0; j < i; j++) {
1384 if (NewBldVec[i] == NewBldVec[j]) {
1385 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1386 RemapSwizzle[i] = j;
1392 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1393 VectorEntry.getValueType(), NewBldVec, 4);
1396 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1397 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1398 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1399 assert(RemapSwizzle.empty());
1400 SDValue NewBldVec[4] = {
1401 VectorEntry.getOperand(0),
1402 VectorEntry.getOperand(1),
1403 VectorEntry.getOperand(2),
1404 VectorEntry.getOperand(3)
1406 bool isUnmovable[4] = { false, false, false, false };
1407 for (unsigned i = 0; i < 4; i++)
1408 RemapSwizzle[i] = i;
1410 for (unsigned i = 0; i < 4; i++) {
1411 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1412 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1414 if (!isUnmovable[Idx]) {
1416 std::swap(NewBldVec[Idx], NewBldVec[i]);
1417 std::swap(RemapSwizzle[RemapSwizzle[Idx]], RemapSwizzle[RemapSwizzle[i]]);
1419 isUnmovable[Idx] = true;
1423 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1424 VectorEntry.getValueType(), NewBldVec, 4);
1428 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1429 SDValue Swz[4], SelectionDAG &DAG) const {
1430 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1431 // Old -> New swizzle values
1432 DenseMap<unsigned, unsigned> SwizzleRemap;
1434 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1435 for (unsigned i = 0; i < 4; i++) {
1436 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1437 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1438 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1441 SwizzleRemap.clear();
1442 BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1443 for (unsigned i = 0; i < 4; i++) {
1444 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1445 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1446 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1453 //===----------------------------------------------------------------------===//
1454 // Custom DAG Optimizations
1455 //===----------------------------------------------------------------------===//
1457 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1458 DAGCombinerInfo &DCI) const {
1459 SelectionDAG &DAG = DCI.DAG;
1461 switch (N->getOpcode()) {
1462 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1463 case ISD::FP_ROUND: {
1464 SDValue Arg = N->getOperand(0);
1465 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1466 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1472 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1473 // (i32 select_cc f32, f32, -1, 0 cc)
1475 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1476 // this to one of the SET*_DX10 instructions.
1477 case ISD::FP_TO_SINT: {
1478 SDValue FNeg = N->getOperand(0);
1479 if (FNeg.getOpcode() != ISD::FNEG) {
1482 SDValue SelectCC = FNeg.getOperand(0);
1483 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1484 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1485 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1486 !isHWTrueValue(SelectCC.getOperand(2)) ||
1487 !isHWFalseValue(SelectCC.getOperand(3))) {
1491 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1492 SelectCC.getOperand(0), // LHS
1493 SelectCC.getOperand(1), // RHS
1494 DAG.getConstant(-1, MVT::i32), // True
1495 DAG.getConstant(0, MVT::i32), // Flase
1496 SelectCC.getOperand(4)); // CC
1501 // insert_vector_elt (build_vector elt0, …, eltN), NewEltIdx, idx
1502 // => build_vector elt0, …, NewEltIdx, …, eltN
1503 case ISD::INSERT_VECTOR_ELT: {
1504 SDValue InVec = N->getOperand(0);
1505 SDValue InVal = N->getOperand(1);
1506 SDValue EltNo = N->getOperand(2);
1509 // If the inserted element is an UNDEF, just use the input vector.
1510 if (InVal.getOpcode() == ISD::UNDEF)
1513 EVT VT = InVec.getValueType();
1515 // If we can't generate a legal BUILD_VECTOR, exit
1516 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1519 // Check that we know which element is being inserted
1520 if (!isa<ConstantSDNode>(EltNo))
1522 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1524 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1525 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1527 SmallVector<SDValue, 8> Ops;
1528 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1529 Ops.append(InVec.getNode()->op_begin(),
1530 InVec.getNode()->op_end());
1531 } else if (InVec.getOpcode() == ISD::UNDEF) {
1532 unsigned NElts = VT.getVectorNumElements();
1533 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1538 // Insert the element
1539 if (Elt < Ops.size()) {
1540 // All the operands of BUILD_VECTOR must have the same type;
1541 // we enforce that here.
1542 EVT OpVT = Ops[0].getValueType();
1543 if (InVal.getValueType() != OpVT)
1544 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1545 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1546 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1550 // Return the new vector
1551 return DAG.getNode(ISD::BUILD_VECTOR, dl,
1552 VT, &Ops[0], Ops.size());
1555 // Extract_vec (Build_vector) generated by custom lowering
1556 // also needs to be customly combined
1557 case ISD::EXTRACT_VECTOR_ELT: {
1558 SDValue Arg = N->getOperand(0);
1559 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1560 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1561 unsigned Element = Const->getZExtValue();
1562 return Arg->getOperand(Element);
1565 if (Arg.getOpcode() == ISD::BITCAST &&
1566 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1567 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1568 unsigned Element = Const->getZExtValue();
1569 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1570 Arg->getOperand(0).getOperand(Element));
1575 case ISD::SELECT_CC: {
1576 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1577 // selectcc x, y, a, b, inv(cc)
1579 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1580 // selectcc x, y, a, b, cc
1581 SDValue LHS = N->getOperand(0);
1582 if (LHS.getOpcode() != ISD::SELECT_CC) {
1586 SDValue RHS = N->getOperand(1);
1587 SDValue True = N->getOperand(2);
1588 SDValue False = N->getOperand(3);
1589 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1591 if (LHS.getOperand(2).getNode() != True.getNode() ||
1592 LHS.getOperand(3).getNode() != False.getNode() ||
1593 RHS.getNode() != False.getNode()) {
1598 default: return SDValue();
1599 case ISD::SETNE: return LHS;
1601 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1602 LHSCC = ISD::getSetCCInverse(LHSCC,
1603 LHS.getOperand(0).getValueType().isInteger());
1604 if (DCI.isBeforeLegalizeOps() ||
1605 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1606 return DAG.getSelectCC(SDLoc(N),
1618 case AMDGPUISD::EXPORT: {
1619 SDValue Arg = N->getOperand(1);
1620 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1623 SDValue NewArgs[8] = {
1624 N->getOperand(0), // Chain
1626 N->getOperand(2), // ArrayBase
1627 N->getOperand(3), // Type
1628 N->getOperand(4), // SWZ_X
1629 N->getOperand(5), // SWZ_Y
1630 N->getOperand(6), // SWZ_Z
1631 N->getOperand(7) // SWZ_W
1634 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1635 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1637 case AMDGPUISD::TEXTURE_FETCH: {
1638 SDValue Arg = N->getOperand(1);
1639 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1642 SDValue NewArgs[19] = {
1663 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1664 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1672 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
1673 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
1674 const R600InstrInfo *TII =
1675 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1676 if (!Src.isMachineOpcode())
1678 switch (Src.getMachineOpcode()) {
1679 case AMDGPU::FNEG_R600:
1682 Src = Src.getOperand(0);
1683 Neg = DAG.getTargetConstant(1, MVT::i32);
1685 case AMDGPU::FABS_R600:
1688 Src = Src.getOperand(0);
1689 Abs = DAG.getTargetConstant(1, MVT::i32);
1691 case AMDGPU::CONST_COPY: {
1692 unsigned Opcode = ParentNode->getMachineOpcode();
1693 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1698 SDValue CstOffset = Src.getOperand(0);
1699 if (ParentNode->getValueType(0).isVector())
1702 // Gather constants values
1703 int SrcIndices[] = {
1704 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1705 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1706 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1707 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1708 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1709 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1710 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1711 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1712 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1713 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1714 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1716 std::vector<unsigned> Consts;
1717 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
1718 int OtherSrcIdx = SrcIndices[i];
1719 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1720 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1726 if (RegisterSDNode *Reg =
1727 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1728 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1729 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(
1730 ParentNode->getOperand(OtherSelIdx));
1731 Consts.push_back(Cst->getZExtValue());
1736 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
1737 Consts.push_back(Cst->getZExtValue());
1738 if (!TII->fitsConstReadLimitations(Consts)) {
1743 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1746 case AMDGPU::MOV_IMM_I32:
1747 case AMDGPU::MOV_IMM_F32: {
1748 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1749 uint64_t ImmValue = 0;
1752 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1753 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1754 float FloatValue = FPC->getValueAPF().convertToFloat();
1755 if (FloatValue == 0.0) {
1756 ImmReg = AMDGPU::ZERO;
1757 } else if (FloatValue == 0.5) {
1758 ImmReg = AMDGPU::HALF;
1759 } else if (FloatValue == 1.0) {
1760 ImmReg = AMDGPU::ONE;
1762 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1765 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1766 uint64_t Value = C->getZExtValue();
1768 ImmReg = AMDGPU::ZERO;
1769 } else if (Value == 1) {
1770 ImmReg = AMDGPU::ONE_INT;
1776 // Check that we aren't already using an immediate.
1777 // XXX: It's possible for an instruction to have more than one
1778 // immediate operand, but this is not supported yet.
1779 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1782 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1784 if (C->getZExtValue())
1786 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1788 Src = DAG.getRegister(ImmReg, MVT::i32);
1797 /// \brief Fold the instructions after selecting them
1798 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1799 SelectionDAG &DAG) const {
1800 const R600InstrInfo *TII =
1801 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1802 if (!Node->isMachineOpcode())
1804 unsigned Opcode = Node->getMachineOpcode();
1807 std::vector<SDValue> Ops;
1808 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1812 if (Opcode == AMDGPU::DOT_4) {
1813 int OperandIdx[] = {
1814 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1815 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1816 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1817 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1818 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1819 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1820 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1821 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1824 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1825 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1826 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1827 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1828 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1829 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1830 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1831 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1834 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1835 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1836 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1837 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1838 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1839 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1840 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1841 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1843 for (unsigned i = 0; i < 8; i++) {
1844 if (OperandIdx[i] < 0)
1846 SDValue &Src = Ops[OperandIdx[i] - 1];
1847 SDValue &Neg = Ops[NegIdx[i] - 1];
1848 SDValue &Abs = Ops[AbsIdx[i] - 1];
1849 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1850 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1853 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1854 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1855 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1857 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1858 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1859 SDValue &Src = Ops[i];
1860 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1861 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1863 } else if (Opcode == AMDGPU::CLAMP_R600) {
1864 SDValue Src = Node->getOperand(0);
1865 if (!Src.isMachineOpcode() ||
1866 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1868 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1869 AMDGPU::OpName::clamp);
1872 std::vector<SDValue> Ops;
1873 unsigned NumOp = Src.getNumOperands();
1874 for(unsigned i = 0; i < NumOp; ++i)
1875 Ops.push_back(Src.getOperand(i));
1876 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1877 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1878 Node->getVTList(), Ops);
1880 if (!TII->hasInstrModifiers(Opcode))
1882 int OperandIdx[] = {
1883 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1884 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1885 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1888 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1889 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1890 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1893 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1894 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1897 for (unsigned i = 0; i < 3; i++) {
1898 if (OperandIdx[i] < 0)
1900 SDValue &Src = Ops[OperandIdx[i] - 1];
1901 SDValue &Neg = Ops[NegIdx[i] - 1];
1903 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
1904 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1905 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1906 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
1911 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1912 SDValue &Imm = Ops[ImmIdx];
1913 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
1914 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);