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 isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
867 std::swap(False, True);
868 CC = DAG.getCondCode(InverseCC);
871 if (isHWTrueValue(True) && isHWFalseValue(False) &&
872 (CompareVT == VT || VT == MVT::i32)) {
873 // This can be matched by a SET* instruction.
874 return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
877 // Try to lower to a CND* instruction:
879 // CND* can match the following patterns:
881 // select_cc f32, 0.0, f32, f32, cc_supported
882 // select_cc f32, 0.0, i32, i32, cc_supported
883 // select_cc i32, 0, f32, f32, cc_supported
884 // select_cc i32, 0, i32, i32, cc_supported
887 // Try to move the zero value to the RHS
889 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
890 // Try swapping the operands
891 ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
892 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
894 CC = DAG.getCondCode(CCSwapped);
896 // Try inverting the conditon and then swapping the operands
897 ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
898 CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
899 if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
900 std::swap(True, False);
902 CC = DAG.getCondCode(CCSwapped);
909 ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
910 if (CompareVT != VT) {
911 // Bitcast True / False to the correct types. This will end up being
912 // a nop, but it allows us to define only a single pattern in the
913 // .TD files for each CND* instruction rather than having to have
914 // one pattern for integer True/False and one for fp True/False
915 True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
916 False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
923 CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
931 SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
934 DAG.getCondCode(CCOpcode));
935 return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
939 // Possible Min/Max pattern
940 SDValue MinMax = LowerMinMax(Op, DAG);
941 if (MinMax.getNode()) {
945 // If we make it this for it means we have no native instructions to handle
946 // this SELECT_CC, so we must lower it.
947 SDValue HWTrue, HWFalse;
949 if (CompareVT == MVT::f32) {
950 HWTrue = DAG.getConstantFP(1.0f, CompareVT);
951 HWFalse = DAG.getConstantFP(0.0f, CompareVT);
952 } else if (CompareVT == MVT::i32) {
953 HWTrue = DAG.getConstant(-1, CompareVT);
954 HWFalse = DAG.getConstant(0, CompareVT);
957 assert(!"Unhandled value type in LowerSELECT_CC");
960 // Lower this unsupported SELECT_CC into a combination of two supported
961 // SELECT_CC operations.
962 SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
964 return DAG.getNode(ISD::SELECT_CC, DL, VT,
967 DAG.getCondCode(ISD::SETNE));
970 /// LLVM generates byte-addresed pointers. For indirect addressing, we need to
971 /// convert these pointers to a register index. Each register holds
972 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
973 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
974 /// for indirect addressing.
975 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
977 SelectionDAG &DAG) const {
989 default: llvm_unreachable("Invalid stack width");
992 return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
993 DAG.getConstant(SRLPad, MVT::i32));
996 void R600TargetLowering::getStackAddress(unsigned StackWidth,
999 unsigned &PtrIncr) const {
1000 switch (StackWidth) {
1011 Channel = ElemIdx % 2;
1025 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1027 StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1028 SDValue Chain = Op.getOperand(0);
1029 SDValue Value = Op.getOperand(1);
1030 SDValue Ptr = Op.getOperand(2);
1032 SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1033 if (Result.getNode()) {
1037 if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1038 if (StoreNode->isTruncatingStore()) {
1039 EVT VT = Value.getValueType();
1040 assert(VT.bitsLE(MVT::i32));
1041 EVT MemVT = StoreNode->getMemoryVT();
1042 SDValue MaskConstant;
1043 if (MemVT == MVT::i8) {
1044 MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1046 assert(MemVT == MVT::i16);
1047 MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1049 SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1050 DAG.getConstant(2, MVT::i32));
1051 SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1052 DAG.getConstant(0x00000003, VT));
1053 SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1054 SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1055 DAG.getConstant(3, VT));
1056 SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1057 SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1058 // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1062 DAG.getConstant(0, MVT::i32),
1063 DAG.getConstant(0, MVT::i32),
1066 SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src, 4);
1067 SDValue Args[3] = { Chain, Input, DWordAddr };
1068 return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1069 Op->getVTList(), Args, 3, MemVT,
1070 StoreNode->getMemOperand());
1071 } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1072 Value.getValueType().bitsGE(MVT::i32)) {
1073 // Convert pointer from byte address to dword address.
1074 Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1075 DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1076 Ptr, DAG.getConstant(2, MVT::i32)));
1078 if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1079 assert(!"Truncated and indexed stores not supported yet");
1081 Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1087 EVT ValueVT = Value.getValueType();
1089 if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1093 // Lowering for indirect addressing
1095 const MachineFunction &MF = DAG.getMachineFunction();
1096 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1097 getTargetMachine().getFrameLowering());
1098 unsigned StackWidth = TFL->getStackWidth(MF);
1100 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1102 if (ValueVT.isVector()) {
1103 unsigned NumElemVT = ValueVT.getVectorNumElements();
1104 EVT ElemVT = ValueVT.getVectorElementType();
1107 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1108 "vector width in load");
1110 for (unsigned i = 0; i < NumElemVT; ++i) {
1111 unsigned Channel, PtrIncr;
1112 getStackAddress(StackWidth, i, Channel, PtrIncr);
1113 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1114 DAG.getConstant(PtrIncr, MVT::i32));
1115 SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1116 Value, DAG.getConstant(i, MVT::i32));
1118 Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1120 DAG.getTargetConstant(Channel, MVT::i32));
1122 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1124 if (ValueVT == MVT::i8) {
1125 Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1127 Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1128 DAG.getTargetConstant(0, MVT::i32)); // Channel
1134 // return (512 + (kc_bank << 12)
1136 ConstantAddressBlock(unsigned AddressSpace) {
1137 switch (AddressSpace) {
1138 case AMDGPUAS::CONSTANT_BUFFER_0:
1140 case AMDGPUAS::CONSTANT_BUFFER_1:
1142 case AMDGPUAS::CONSTANT_BUFFER_2:
1143 return 512 + 4096 * 2;
1144 case AMDGPUAS::CONSTANT_BUFFER_3:
1145 return 512 + 4096 * 3;
1146 case AMDGPUAS::CONSTANT_BUFFER_4:
1147 return 512 + 4096 * 4;
1148 case AMDGPUAS::CONSTANT_BUFFER_5:
1149 return 512 + 4096 * 5;
1150 case AMDGPUAS::CONSTANT_BUFFER_6:
1151 return 512 + 4096 * 6;
1152 case AMDGPUAS::CONSTANT_BUFFER_7:
1153 return 512 + 4096 * 7;
1154 case AMDGPUAS::CONSTANT_BUFFER_8:
1155 return 512 + 4096 * 8;
1156 case AMDGPUAS::CONSTANT_BUFFER_9:
1157 return 512 + 4096 * 9;
1158 case AMDGPUAS::CONSTANT_BUFFER_10:
1159 return 512 + 4096 * 10;
1160 case AMDGPUAS::CONSTANT_BUFFER_11:
1161 return 512 + 4096 * 11;
1162 case AMDGPUAS::CONSTANT_BUFFER_12:
1163 return 512 + 4096 * 12;
1164 case AMDGPUAS::CONSTANT_BUFFER_13:
1165 return 512 + 4096 * 13;
1166 case AMDGPUAS::CONSTANT_BUFFER_14:
1167 return 512 + 4096 * 14;
1168 case AMDGPUAS::CONSTANT_BUFFER_15:
1169 return 512 + 4096 * 15;
1175 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1177 EVT VT = Op.getValueType();
1179 LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1180 SDValue Chain = Op.getOperand(0);
1181 SDValue Ptr = Op.getOperand(1);
1182 SDValue LoweredLoad;
1184 if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1185 SDValue MergedValues[2] = {
1186 SplitVectorLoad(Op, DAG),
1189 return DAG.getMergeValues(MergedValues, 2, DL);
1192 int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1193 if (ConstantBlock > -1) {
1195 if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1196 dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1197 dyn_cast<ConstantSDNode>(Ptr)) {
1199 for (unsigned i = 0; i < 4; i++) {
1200 // We want Const position encoded with the following formula :
1201 // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1202 // const_index is Ptr computed by llvm using an alignment of 16.
1203 // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1204 // then div by 4 at the ISel step
1205 SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1206 DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1207 Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1209 EVT NewVT = MVT::v4i32;
1210 unsigned NumElements = 4;
1211 if (VT.isVector()) {
1213 NumElements = VT.getVectorNumElements();
1215 Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT, Slots, NumElements);
1217 // non constant ptr cant be folded, keeps it as a v4f32 load
1218 Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1219 DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1220 DAG.getConstant(LoadNode->getAddressSpace() -
1221 AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1225 if (!VT.isVector()) {
1226 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1227 DAG.getConstant(0, MVT::i32));
1230 SDValue MergedValues[2] = {
1234 return DAG.getMergeValues(MergedValues, 2, DL);
1237 // For most operations returning SDValue() will result int he node being
1238 // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so
1239 // we need to manually expand loads that may be legal in some address spaces
1240 // and illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported
1241 // for compute shaders, since the data is sign extended when it is uploaded
1242 // to the buffer. Howerver SEXT loads from other addresspaces are not
1243 // supported, so we need to expand them here.
1244 if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1245 EVT MemVT = LoadNode->getMemoryVT();
1246 assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1247 SDValue ShiftAmount =
1248 DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1249 SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1250 LoadNode->getPointerInfo(), MemVT,
1251 LoadNode->isVolatile(),
1252 LoadNode->isNonTemporal(),
1253 LoadNode->getAlignment());
1254 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1255 SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1257 SDValue MergedValues[2] = { Sra, Chain };
1258 return DAG.getMergeValues(MergedValues, 2, DL);
1261 if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1265 // Lowering for indirect addressing
1266 const MachineFunction &MF = DAG.getMachineFunction();
1267 const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1268 getTargetMachine().getFrameLowering());
1269 unsigned StackWidth = TFL->getStackWidth(MF);
1271 Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1273 if (VT.isVector()) {
1274 unsigned NumElemVT = VT.getVectorNumElements();
1275 EVT ElemVT = VT.getVectorElementType();
1278 assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1279 "vector width in load");
1281 for (unsigned i = 0; i < NumElemVT; ++i) {
1282 unsigned Channel, PtrIncr;
1283 getStackAddress(StackWidth, i, Channel, PtrIncr);
1284 Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1285 DAG.getConstant(PtrIncr, MVT::i32));
1286 Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1288 DAG.getTargetConstant(Channel, MVT::i32),
1291 for (unsigned i = NumElemVT; i < 4; ++i) {
1292 Loads[i] = DAG.getUNDEF(ElemVT);
1294 EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1295 LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1297 LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1299 DAG.getTargetConstant(0, MVT::i32), // Channel
1304 Ops[0] = LoweredLoad;
1307 return DAG.getMergeValues(Ops, 2, DL);
1310 /// XXX Only kernel functions are supported, so we can assume for now that
1311 /// every function is a kernel function, but in the future we should use
1312 /// separate calling conventions for kernel and non-kernel functions.
1313 SDValue R600TargetLowering::LowerFormalArguments(
1315 CallingConv::ID CallConv,
1317 const SmallVectorImpl<ISD::InputArg> &Ins,
1318 SDLoc DL, SelectionDAG &DAG,
1319 SmallVectorImpl<SDValue> &InVals) const {
1320 SmallVector<CCValAssign, 16> ArgLocs;
1321 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1322 getTargetMachine(), ArgLocs, *DAG.getContext());
1324 AnalyzeFormalArguments(CCInfo, Ins);
1326 for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1327 CCValAssign &VA = ArgLocs[i];
1328 EVT VT = VA.getLocVT();
1330 PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1331 AMDGPUAS::CONSTANT_BUFFER_0);
1333 // The first 36 bytes of the input buffer contains information about
1334 // thread group and global sizes.
1335 SDValue Arg = DAG.getLoad(VT, DL, Chain,
1336 DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1337 MachinePointerInfo(UndefValue::get(PtrTy)), false,
1338 false, false, 4); // 4 is the prefered alignment for
1339 // the CONSTANT memory space.
1340 InVals.push_back(Arg);
1345 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1346 if (!VT.isVector()) return MVT::i32;
1347 return VT.changeVectorElementTypeToInteger();
1351 CompactSwizzlableVector(SelectionDAG &DAG, SDValue VectorEntry,
1352 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1353 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1354 assert(RemapSwizzle.empty());
1355 SDValue NewBldVec[4] = {
1356 VectorEntry.getOperand(0),
1357 VectorEntry.getOperand(1),
1358 VectorEntry.getOperand(2),
1359 VectorEntry.getOperand(3)
1362 for (unsigned i = 0; i < 4; i++) {
1363 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1365 RemapSwizzle[i] = 4; // SEL_0
1366 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1367 } else if (C->isExactlyValue(1.0)) {
1368 RemapSwizzle[i] = 5; // SEL_1
1369 NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1373 if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1375 for (unsigned j = 0; j < i; j++) {
1376 if (NewBldVec[i] == NewBldVec[j]) {
1377 NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1378 RemapSwizzle[i] = j;
1384 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1385 VectorEntry.getValueType(), NewBldVec, 4);
1388 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1389 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1390 assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1391 assert(RemapSwizzle.empty());
1392 SDValue NewBldVec[4] = {
1393 VectorEntry.getOperand(0),
1394 VectorEntry.getOperand(1),
1395 VectorEntry.getOperand(2),
1396 VectorEntry.getOperand(3)
1398 bool isUnmovable[4] = { false, false, false, false };
1399 for (unsigned i = 0; i < 4; i++)
1400 RemapSwizzle[i] = i;
1402 for (unsigned i = 0; i < 4; i++) {
1403 if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1404 unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1406 if (!isUnmovable[Idx]) {
1408 std::swap(NewBldVec[Idx], NewBldVec[i]);
1409 std::swap(RemapSwizzle[RemapSwizzle[Idx]], RemapSwizzle[RemapSwizzle[i]]);
1411 isUnmovable[Idx] = true;
1415 return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1416 VectorEntry.getValueType(), NewBldVec, 4);
1420 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1421 SDValue Swz[4], SelectionDAG &DAG) const {
1422 assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1423 // Old -> New swizzle values
1424 DenseMap<unsigned, unsigned> SwizzleRemap;
1426 BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1427 for (unsigned i = 0; i < 4; i++) {
1428 unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1429 if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1430 Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1433 SwizzleRemap.clear();
1434 BuildVector = ReorganizeVector(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);
1445 //===----------------------------------------------------------------------===//
1446 // Custom DAG Optimizations
1447 //===----------------------------------------------------------------------===//
1449 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1450 DAGCombinerInfo &DCI) const {
1451 SelectionDAG &DAG = DCI.DAG;
1453 switch (N->getOpcode()) {
1454 // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1455 case ISD::FP_ROUND: {
1456 SDValue Arg = N->getOperand(0);
1457 if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1458 return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1464 // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1465 // (i32 select_cc f32, f32, -1, 0 cc)
1467 // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1468 // this to one of the SET*_DX10 instructions.
1469 case ISD::FP_TO_SINT: {
1470 SDValue FNeg = N->getOperand(0);
1471 if (FNeg.getOpcode() != ISD::FNEG) {
1474 SDValue SelectCC = FNeg.getOperand(0);
1475 if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1476 SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1477 SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1478 !isHWTrueValue(SelectCC.getOperand(2)) ||
1479 !isHWFalseValue(SelectCC.getOperand(3))) {
1483 return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1484 SelectCC.getOperand(0), // LHS
1485 SelectCC.getOperand(1), // RHS
1486 DAG.getConstant(-1, MVT::i32), // True
1487 DAG.getConstant(0, MVT::i32), // Flase
1488 SelectCC.getOperand(4)); // CC
1493 // insert_vector_elt (build_vector elt0, …, eltN), NewEltIdx, idx
1494 // => build_vector elt0, …, NewEltIdx, …, eltN
1495 case ISD::INSERT_VECTOR_ELT: {
1496 SDValue InVec = N->getOperand(0);
1497 SDValue InVal = N->getOperand(1);
1498 SDValue EltNo = N->getOperand(2);
1501 // If the inserted element is an UNDEF, just use the input vector.
1502 if (InVal.getOpcode() == ISD::UNDEF)
1505 EVT VT = InVec.getValueType();
1507 // If we can't generate a legal BUILD_VECTOR, exit
1508 if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1511 // Check that we know which element is being inserted
1512 if (!isa<ConstantSDNode>(EltNo))
1514 unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1516 // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1517 // be converted to a BUILD_VECTOR). Fill in the Ops vector with the
1519 SmallVector<SDValue, 8> Ops;
1520 if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1521 Ops.append(InVec.getNode()->op_begin(),
1522 InVec.getNode()->op_end());
1523 } else if (InVec.getOpcode() == ISD::UNDEF) {
1524 unsigned NElts = VT.getVectorNumElements();
1525 Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1530 // Insert the element
1531 if (Elt < Ops.size()) {
1532 // All the operands of BUILD_VECTOR must have the same type;
1533 // we enforce that here.
1534 EVT OpVT = Ops[0].getValueType();
1535 if (InVal.getValueType() != OpVT)
1536 InVal = OpVT.bitsGT(InVal.getValueType()) ?
1537 DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1538 DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1542 // Return the new vector
1543 return DAG.getNode(ISD::BUILD_VECTOR, dl,
1544 VT, &Ops[0], Ops.size());
1547 // Extract_vec (Build_vector) generated by custom lowering
1548 // also needs to be customly combined
1549 case ISD::EXTRACT_VECTOR_ELT: {
1550 SDValue Arg = N->getOperand(0);
1551 if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1552 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1553 unsigned Element = Const->getZExtValue();
1554 return Arg->getOperand(Element);
1557 if (Arg.getOpcode() == ISD::BITCAST &&
1558 Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1559 if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1560 unsigned Element = Const->getZExtValue();
1561 return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1562 Arg->getOperand(0).getOperand(Element));
1567 case ISD::SELECT_CC: {
1568 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1569 // selectcc x, y, a, b, inv(cc)
1571 // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1572 // selectcc x, y, a, b, cc
1573 SDValue LHS = N->getOperand(0);
1574 if (LHS.getOpcode() != ISD::SELECT_CC) {
1578 SDValue RHS = N->getOperand(1);
1579 SDValue True = N->getOperand(2);
1580 SDValue False = N->getOperand(3);
1581 ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1583 if (LHS.getOperand(2).getNode() != True.getNode() ||
1584 LHS.getOperand(3).getNode() != False.getNode() ||
1585 RHS.getNode() != False.getNode()) {
1590 default: return SDValue();
1591 case ISD::SETNE: return LHS;
1593 ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1594 LHSCC = ISD::getSetCCInverse(LHSCC,
1595 LHS.getOperand(0).getValueType().isInteger());
1596 if (DCI.isBeforeLegalizeOps() ||
1597 isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1598 return DAG.getSelectCC(SDLoc(N),
1610 case AMDGPUISD::EXPORT: {
1611 SDValue Arg = N->getOperand(1);
1612 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1615 SDValue NewArgs[8] = {
1616 N->getOperand(0), // Chain
1618 N->getOperand(2), // ArrayBase
1619 N->getOperand(3), // Type
1620 N->getOperand(4), // SWZ_X
1621 N->getOperand(5), // SWZ_Y
1622 N->getOperand(6), // SWZ_Z
1623 N->getOperand(7) // SWZ_W
1626 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1627 return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1629 case AMDGPUISD::TEXTURE_FETCH: {
1630 SDValue Arg = N->getOperand(1);
1631 if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1634 SDValue NewArgs[19] = {
1655 NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1656 return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1664 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
1665 SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
1666 const R600InstrInfo *TII =
1667 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1668 if (!Src.isMachineOpcode())
1670 switch (Src.getMachineOpcode()) {
1671 case AMDGPU::FNEG_R600:
1674 Src = Src.getOperand(0);
1675 Neg = DAG.getTargetConstant(1, MVT::i32);
1677 case AMDGPU::FABS_R600:
1680 Src = Src.getOperand(0);
1681 Abs = DAG.getTargetConstant(1, MVT::i32);
1683 case AMDGPU::CONST_COPY: {
1684 unsigned Opcode = ParentNode->getMachineOpcode();
1685 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1690 SDValue CstOffset = Src.getOperand(0);
1691 if (ParentNode->getValueType(0).isVector())
1694 // Gather constants values
1695 int SrcIndices[] = {
1696 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1697 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1698 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
1699 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1700 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1701 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1702 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1703 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1704 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1705 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1706 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1708 std::vector<unsigned> Consts;
1709 for (unsigned i = 0; i < sizeof(SrcIndices) / sizeof(int); i++) {
1710 int OtherSrcIdx = SrcIndices[i];
1711 int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
1712 if (OtherSrcIdx < 0 || OtherSelIdx < 0)
1718 if (RegisterSDNode *Reg =
1719 dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
1720 if (Reg->getReg() == AMDGPU::ALU_CONST) {
1721 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(
1722 ParentNode->getOperand(OtherSelIdx));
1723 Consts.push_back(Cst->getZExtValue());
1728 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
1729 Consts.push_back(Cst->getZExtValue());
1730 if (!TII->fitsConstReadLimitations(Consts)) {
1735 Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
1738 case AMDGPU::MOV_IMM_I32:
1739 case AMDGPU::MOV_IMM_F32: {
1740 unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
1741 uint64_t ImmValue = 0;
1744 if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
1745 ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
1746 float FloatValue = FPC->getValueAPF().convertToFloat();
1747 if (FloatValue == 0.0) {
1748 ImmReg = AMDGPU::ZERO;
1749 } else if (FloatValue == 0.5) {
1750 ImmReg = AMDGPU::HALF;
1751 } else if (FloatValue == 1.0) {
1752 ImmReg = AMDGPU::ONE;
1754 ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
1757 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
1758 uint64_t Value = C->getZExtValue();
1760 ImmReg = AMDGPU::ZERO;
1761 } else if (Value == 1) {
1762 ImmReg = AMDGPU::ONE_INT;
1768 // Check that we aren't already using an immediate.
1769 // XXX: It's possible for an instruction to have more than one
1770 // immediate operand, but this is not supported yet.
1771 if (ImmReg == AMDGPU::ALU_LITERAL_X) {
1774 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
1776 if (C->getZExtValue())
1778 Imm = DAG.getTargetConstant(ImmValue, MVT::i32);
1780 Src = DAG.getRegister(ImmReg, MVT::i32);
1789 /// \brief Fold the instructions after selecting them
1790 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
1791 SelectionDAG &DAG) const {
1792 const R600InstrInfo *TII =
1793 static_cast<const R600InstrInfo *>(DAG.getTarget().getInstrInfo());
1794 if (!Node->isMachineOpcode())
1796 unsigned Opcode = Node->getMachineOpcode();
1799 std::vector<SDValue> Ops;
1800 for(SDNode::op_iterator I = Node->op_begin(), E = Node->op_end();
1804 if (Opcode == AMDGPU::DOT_4) {
1805 int OperandIdx[] = {
1806 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
1807 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
1808 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
1809 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
1810 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
1811 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
1812 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
1813 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
1816 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
1817 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
1818 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
1819 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
1820 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
1821 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
1822 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
1823 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
1826 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
1827 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
1828 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
1829 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
1830 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
1831 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
1832 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
1833 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
1835 for (unsigned i = 0; i < 8; i++) {
1836 if (OperandIdx[i] < 0)
1838 SDValue &Src = Ops[OperandIdx[i] - 1];
1839 SDValue &Neg = Ops[NegIdx[i] - 1];
1840 SDValue &Abs = Ops[AbsIdx[i] - 1];
1841 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1842 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1845 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1846 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
1847 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1849 } else if (Opcode == AMDGPU::REG_SEQUENCE) {
1850 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
1851 SDValue &Src = Ops[i];
1852 if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
1853 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
1855 } else if (Opcode == AMDGPU::CLAMP_R600) {
1856 SDValue Src = Node->getOperand(0);
1857 if (!Src.isMachineOpcode() ||
1858 !TII->hasInstrModifiers(Src.getMachineOpcode()))
1860 int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
1861 AMDGPU::OpName::clamp);
1864 std::vector<SDValue> Ops;
1865 unsigned NumOp = Src.getNumOperands();
1866 for(unsigned i = 0; i < NumOp; ++i)
1867 Ops.push_back(Src.getOperand(i));
1868 Ops[ClampIdx - 1] = DAG.getTargetConstant(1, MVT::i32);
1869 return DAG.getMachineNode(Src.getMachineOpcode(), SDLoc(Node),
1870 Node->getVTList(), Ops);
1872 if (!TII->hasInstrModifiers(Opcode))
1874 int OperandIdx[] = {
1875 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
1876 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
1877 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
1880 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
1881 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
1882 TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
1885 TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
1886 TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
1889 for (unsigned i = 0; i < 3; i++) {
1890 if (OperandIdx[i] < 0)
1892 SDValue &Src = Ops[OperandIdx[i] - 1];
1893 SDValue &Neg = Ops[NegIdx[i] - 1];
1895 SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
1896 bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
1897 int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
1898 int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
1903 SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
1904 SDValue &Imm = Ops[ImmIdx];
1905 if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
1906 return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);