R600: Add support for i8 and i16 local memory loads
[oota-llvm.git] / lib / Target / R600 / R600ISelLowering.cpp
1 //===-- R600ISelLowering.cpp - R600 DAG Lowering Implementation -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Custom DAG lowering for R600
12 //
13 //===----------------------------------------------------------------------===//
14
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"
26
27 using namespace llvm;
28
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);
38
39   computeRegisterProperties();
40
41   setOperationAction(ISD::FCOS, MVT::f32, Custom);
42   setOperationAction(ISD::FSIN, MVT::f32, Custom);
43
44   setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
45   setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
46
47   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
48   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
49
50   setOperationAction(ISD::FSUB, MVT::f32, Expand);
51
52   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
53   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
54   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
55
56   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
57   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
58
59   setOperationAction(ISD::SETCC, MVT::i32, Expand);
60   setOperationAction(ISD::SETCC, MVT::f32, Expand);
61   setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
62
63   setOperationAction(ISD::SELECT, MVT::i32, Custom);
64   setOperationAction(ISD::SELECT, MVT::f32, Custom);
65
66   // Legalize loads and stores to the private address space.
67   setOperationAction(ISD::LOAD, MVT::i32, Custom);
68   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
69   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
70   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
71   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
72   setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
73   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
74   setOperationAction(ISD::STORE, MVT::i8, Custom);
75   setOperationAction(ISD::STORE, MVT::i32, Custom);
76   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
77   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
78   setTruncStoreAction(MVT::i32, MVT::i8, Custom);
79   setTruncStoreAction(MVT::i32, MVT::i16, Custom);
80
81   setOperationAction(ISD::LOAD, MVT::i32, Custom);
82   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
83   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
84
85   setTargetDAGCombine(ISD::FP_ROUND);
86   setTargetDAGCombine(ISD::FP_TO_SINT);
87   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
88   setTargetDAGCombine(ISD::SELECT_CC);
89   setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
90
91   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
92
93   setBooleanContents(ZeroOrNegativeOneBooleanContent);
94   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
95   setSchedulingPreference(Sched::Source);
96 }
97
98 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
99     MachineInstr * MI, MachineBasicBlock * BB) const {
100   MachineFunction * MF = BB->getParent();
101   MachineRegisterInfo &MRI = MF->getRegInfo();
102   MachineBasicBlock::iterator I = *MI;
103   const R600InstrInfo *TII =
104     static_cast<const R600InstrInfo*>(MF->getTarget().getInstrInfo());
105
106   switch (MI->getOpcode()) {
107   default:
108     if (TII->get(MI->getOpcode()).TSFlags & R600_InstFlag::LDS_1A) {
109       MachineInstrBuilder NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
110                                           TII->get(MI->getOpcode()),
111                                           AMDGPU::OQAP);
112       for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
113         NewMI.addOperand(MI->getOperand(i));
114       }
115       TII->buildDefaultInstruction(*BB, I, AMDGPU::MOV,
116                                    MI->getOperand(0).getReg(),
117                                    AMDGPU::OQAP);
118     } else {
119       return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
120     }
121     break;
122   case AMDGPU::CLAMP_R600: {
123     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
124                                                    AMDGPU::MOV,
125                                                    MI->getOperand(0).getReg(),
126                                                    MI->getOperand(1).getReg());
127     TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
128     break;
129   }
130
131   case AMDGPU::FABS_R600: {
132     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
133                                                     AMDGPU::MOV,
134                                                     MI->getOperand(0).getReg(),
135                                                     MI->getOperand(1).getReg());
136     TII->addFlag(NewMI, 0, MO_FLAG_ABS);
137     break;
138   }
139
140   case AMDGPU::FNEG_R600: {
141     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
142                                                     AMDGPU::MOV,
143                                                     MI->getOperand(0).getReg(),
144                                                     MI->getOperand(1).getReg());
145     TII->addFlag(NewMI, 0, MO_FLAG_NEG);
146     break;
147   }
148
149   case AMDGPU::MASK_WRITE: {
150     unsigned maskedRegister = MI->getOperand(0).getReg();
151     assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
152     MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
153     TII->addFlag(defInstr, 0, MO_FLAG_MASK);
154     break;
155   }
156
157   case AMDGPU::MOV_IMM_F32:
158     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
159                      MI->getOperand(1).getFPImm()->getValueAPF()
160                          .bitcastToAPInt().getZExtValue());
161     break;
162   case AMDGPU::MOV_IMM_I32:
163     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
164                      MI->getOperand(1).getImm());
165     break;
166   case AMDGPU::CONST_COPY: {
167     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
168         MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
169     TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
170         MI->getOperand(1).getImm());
171     break;
172   }
173
174   case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
175   case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
176   case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
177     unsigned EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
178
179     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
180             .addOperand(MI->getOperand(0))
181             .addOperand(MI->getOperand(1))
182             .addImm(EOP); // Set End of program bit
183     break;
184   }
185
186   case AMDGPU::TXD: {
187     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
188     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
189     MachineOperand &RID = MI->getOperand(4);
190     MachineOperand &SID = MI->getOperand(5);
191     unsigned TextureId = MI->getOperand(6).getImm();
192     unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
193     unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
194
195     switch (TextureId) {
196     case 5: // Rect
197       CTX = CTY = 0;
198       break;
199     case 6: // Shadow1D
200       SrcW = SrcZ;
201       break;
202     case 7: // Shadow2D
203       SrcW = SrcZ;
204       break;
205     case 8: // ShadowRect
206       CTX = CTY = 0;
207       SrcW = SrcZ;
208       break;
209     case 9: // 1DArray
210       SrcZ = SrcY;
211       CTZ = 0;
212       break;
213     case 10: // 2DArray
214       CTZ = 0;
215       break;
216     case 11: // Shadow1DArray
217       SrcZ = SrcY;
218       CTZ = 0;
219       break;
220     case 12: // Shadow2DArray
221       CTZ = 0;
222       break;
223     }
224     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
225             .addOperand(MI->getOperand(3))
226             .addImm(SrcX)
227             .addImm(SrcY)
228             .addImm(SrcZ)
229             .addImm(SrcW)
230             .addImm(0)
231             .addImm(0)
232             .addImm(0)
233             .addImm(0)
234             .addImm(1)
235             .addImm(2)
236             .addImm(3)
237             .addOperand(RID)
238             .addOperand(SID)
239             .addImm(CTX)
240             .addImm(CTY)
241             .addImm(CTZ)
242             .addImm(CTW);
243     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
244             .addOperand(MI->getOperand(2))
245             .addImm(SrcX)
246             .addImm(SrcY)
247             .addImm(SrcZ)
248             .addImm(SrcW)
249             .addImm(0)
250             .addImm(0)
251             .addImm(0)
252             .addImm(0)
253             .addImm(1)
254             .addImm(2)
255             .addImm(3)
256             .addOperand(RID)
257             .addOperand(SID)
258             .addImm(CTX)
259             .addImm(CTY)
260             .addImm(CTZ)
261             .addImm(CTW);
262     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_G))
263             .addOperand(MI->getOperand(0))
264             .addOperand(MI->getOperand(1))
265             .addImm(SrcX)
266             .addImm(SrcY)
267             .addImm(SrcZ)
268             .addImm(SrcW)
269             .addImm(0)
270             .addImm(0)
271             .addImm(0)
272             .addImm(0)
273             .addImm(1)
274             .addImm(2)
275             .addImm(3)
276             .addOperand(RID)
277             .addOperand(SID)
278             .addImm(CTX)
279             .addImm(CTY)
280             .addImm(CTZ)
281             .addImm(CTW)
282             .addReg(T0, RegState::Implicit)
283             .addReg(T1, RegState::Implicit);
284     break;
285   }
286
287   case AMDGPU::TXD_SHADOW: {
288     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
289     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
290     MachineOperand &RID = MI->getOperand(4);
291     MachineOperand &SID = MI->getOperand(5);
292     unsigned TextureId = MI->getOperand(6).getImm();
293     unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
294     unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
295
296     switch (TextureId) {
297     case 5: // Rect
298       CTX = CTY = 0;
299       break;
300     case 6: // Shadow1D
301       SrcW = SrcZ;
302       break;
303     case 7: // Shadow2D
304       SrcW = SrcZ;
305       break;
306     case 8: // ShadowRect
307       CTX = CTY = 0;
308       SrcW = SrcZ;
309       break;
310     case 9: // 1DArray
311       SrcZ = SrcY;
312       CTZ = 0;
313       break;
314     case 10: // 2DArray
315       CTZ = 0;
316       break;
317     case 11: // Shadow1DArray
318       SrcZ = SrcY;
319       CTZ = 0;
320       break;
321     case 12: // Shadow2DArray
322       CTZ = 0;
323       break;
324     }
325
326     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
327             .addOperand(MI->getOperand(3))
328             .addImm(SrcX)
329             .addImm(SrcY)
330             .addImm(SrcZ)
331             .addImm(SrcW)
332             .addImm(0)
333             .addImm(0)
334             .addImm(0)
335             .addImm(0)
336             .addImm(1)
337             .addImm(2)
338             .addImm(3)
339             .addOperand(RID)
340             .addOperand(SID)
341             .addImm(CTX)
342             .addImm(CTY)
343             .addImm(CTZ)
344             .addImm(CTW);
345     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
346             .addOperand(MI->getOperand(2))
347             .addImm(SrcX)
348             .addImm(SrcY)
349             .addImm(SrcZ)
350             .addImm(SrcW)
351             .addImm(0)
352             .addImm(0)
353             .addImm(0)
354             .addImm(0)
355             .addImm(1)
356             .addImm(2)
357             .addImm(3)
358             .addOperand(RID)
359             .addOperand(SID)
360             .addImm(CTX)
361             .addImm(CTY)
362             .addImm(CTZ)
363             .addImm(CTW);
364     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
365             .addOperand(MI->getOperand(0))
366             .addOperand(MI->getOperand(1))
367             .addImm(SrcX)
368             .addImm(SrcY)
369             .addImm(SrcZ)
370             .addImm(SrcW)
371             .addImm(0)
372             .addImm(0)
373             .addImm(0)
374             .addImm(0)
375             .addImm(1)
376             .addImm(2)
377             .addImm(3)
378             .addOperand(RID)
379             .addOperand(SID)
380             .addImm(CTX)
381             .addImm(CTY)
382             .addImm(CTZ)
383             .addImm(CTW)
384             .addReg(T0, RegState::Implicit)
385             .addReg(T1, RegState::Implicit);
386     break;
387   }
388
389   case AMDGPU::BRANCH:
390       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
391               .addOperand(MI->getOperand(0));
392       break;
393
394   case AMDGPU::BRANCH_COND_f32: {
395     MachineInstr *NewMI =
396       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
397               AMDGPU::PREDICATE_BIT)
398               .addOperand(MI->getOperand(1))
399               .addImm(OPCODE_IS_NOT_ZERO)
400               .addImm(0); // Flags
401     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
402     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
403             .addOperand(MI->getOperand(0))
404             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
405     break;
406   }
407
408   case AMDGPU::BRANCH_COND_i32: {
409     MachineInstr *NewMI =
410       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
411             AMDGPU::PREDICATE_BIT)
412             .addOperand(MI->getOperand(1))
413             .addImm(OPCODE_IS_NOT_ZERO_INT)
414             .addImm(0); // Flags
415     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
416     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
417            .addOperand(MI->getOperand(0))
418             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
419     break;
420   }
421
422   case AMDGPU::EG_ExportSwz:
423   case AMDGPU::R600_ExportSwz: {
424     // Instruction is left unmodified if its not the last one of its type
425     bool isLastInstructionOfItsType = true;
426     unsigned InstExportType = MI->getOperand(1).getImm();
427     for (MachineBasicBlock::iterator NextExportInst = llvm::next(I),
428          EndBlock = BB->end(); NextExportInst != EndBlock;
429          NextExportInst = llvm::next(NextExportInst)) {
430       if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
431           NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
432         unsigned CurrentInstExportType = NextExportInst->getOperand(1)
433             .getImm();
434         if (CurrentInstExportType == InstExportType) {
435           isLastInstructionOfItsType = false;
436           break;
437         }
438       }
439     }
440     bool EOP = (llvm::next(I)->getOpcode() == AMDGPU::RETURN)? 1 : 0;
441     if (!EOP && !isLastInstructionOfItsType)
442       return BB;
443     unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
444     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
445             .addOperand(MI->getOperand(0))
446             .addOperand(MI->getOperand(1))
447             .addOperand(MI->getOperand(2))
448             .addOperand(MI->getOperand(3))
449             .addOperand(MI->getOperand(4))
450             .addOperand(MI->getOperand(5))
451             .addOperand(MI->getOperand(6))
452             .addImm(CfInst)
453             .addImm(EOP);
454     break;
455   }
456   case AMDGPU::RETURN: {
457     // RETURN instructions must have the live-out registers as implicit uses,
458     // otherwise they appear dead.
459     R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
460     MachineInstrBuilder MIB(*MF, MI);
461     for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
462       MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
463     return BB;
464   }
465   }
466
467   MI->eraseFromParent();
468   return BB;
469 }
470
471 //===----------------------------------------------------------------------===//
472 // Custom DAG Lowering Operations
473 //===----------------------------------------------------------------------===//
474
475 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
476   MachineFunction &MF = DAG.getMachineFunction();
477   R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
478   switch (Op.getOpcode()) {
479   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
480   case ISD::FCOS:
481   case ISD::FSIN: return LowerTrig(Op, DAG);
482   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
483   case ISD::SELECT: return LowerSELECT(Op, DAG);
484   case ISD::STORE: return LowerSTORE(Op, DAG);
485   case ISD::LOAD: return LowerLOAD(Op, DAG);
486   case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
487   case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
488   case ISD::INTRINSIC_VOID: {
489     SDValue Chain = Op.getOperand(0);
490     unsigned IntrinsicID =
491                          cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
492     switch (IntrinsicID) {
493     case AMDGPUIntrinsic::AMDGPU_store_output: {
494       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
495       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
496       MFI->LiveOuts.push_back(Reg);
497       return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
498     }
499     case AMDGPUIntrinsic::R600_store_swizzle: {
500       const SDValue Args[8] = {
501         Chain,
502         Op.getOperand(2), // Export Value
503         Op.getOperand(3), // ArrayBase
504         Op.getOperand(4), // Type
505         DAG.getConstant(0, MVT::i32), // SWZ_X
506         DAG.getConstant(1, MVT::i32), // SWZ_Y
507         DAG.getConstant(2, MVT::i32), // SWZ_Z
508         DAG.getConstant(3, MVT::i32) // SWZ_W
509       };
510       return DAG.getNode(AMDGPUISD::EXPORT, SDLoc(Op), Op.getValueType(),
511           Args, 8);
512     }
513
514     // default for switch(IntrinsicID)
515     default: break;
516     }
517     // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
518     break;
519   }
520   case ISD::INTRINSIC_WO_CHAIN: {
521     unsigned IntrinsicID =
522                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
523     EVT VT = Op.getValueType();
524     SDLoc DL(Op);
525     switch(IntrinsicID) {
526     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
527     case AMDGPUIntrinsic::R600_load_input: {
528       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
529       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
530       MachineFunction &MF = DAG.getMachineFunction();
531       MachineRegisterInfo &MRI = MF.getRegInfo();
532       MRI.addLiveIn(Reg);
533       return DAG.getCopyFromReg(DAG.getEntryNode(),
534           SDLoc(DAG.getEntryNode()), Reg, VT);
535     }
536
537     case AMDGPUIntrinsic::R600_interp_input: {
538       int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
539       int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
540       MachineSDNode *interp;
541       if (ijb < 0) {
542         const MachineFunction &MF = DAG.getMachineFunction();
543         const R600InstrInfo *TII =
544           static_cast<const R600InstrInfo*>(MF.getTarget().getInstrInfo());
545         interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
546             MVT::v4f32, DAG.getTargetConstant(slot / 4 , MVT::i32));
547         return DAG.getTargetExtractSubreg(
548             TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
549             DL, MVT::f32, SDValue(interp, 0));
550       }
551
552       MachineFunction &MF = DAG.getMachineFunction();
553       MachineRegisterInfo &MRI = MF.getRegInfo();
554       unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
555       unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
556       MRI.addLiveIn(RegisterI);
557       MRI.addLiveIn(RegisterJ);
558       SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
559           SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
560       SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
561           SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
562
563       if (slot % 4 < 2)
564         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
565             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
566             RegisterJNode, RegisterINode);
567       else
568         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
569             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4 , MVT::i32),
570             RegisterJNode, RegisterINode);
571       return SDValue(interp, slot % 2);
572     }
573     case AMDGPUIntrinsic::R600_tex:
574     case AMDGPUIntrinsic::R600_texc:
575     case AMDGPUIntrinsic::R600_txl:
576     case AMDGPUIntrinsic::R600_txlc:
577     case AMDGPUIntrinsic::R600_txb:
578     case AMDGPUIntrinsic::R600_txbc:
579     case AMDGPUIntrinsic::R600_txf:
580     case AMDGPUIntrinsic::R600_txq:
581     case AMDGPUIntrinsic::R600_ddx:
582     case AMDGPUIntrinsic::R600_ddy: {
583       unsigned TextureOp;
584       switch (IntrinsicID) {
585       case AMDGPUIntrinsic::R600_tex:
586         TextureOp = 0;
587         break;
588       case AMDGPUIntrinsic::R600_texc:
589         TextureOp = 1;
590         break;
591       case AMDGPUIntrinsic::R600_txl:
592         TextureOp = 2;
593         break;
594       case AMDGPUIntrinsic::R600_txlc:
595         TextureOp = 3;
596         break;
597       case AMDGPUIntrinsic::R600_txb:
598         TextureOp = 4;
599         break;
600       case AMDGPUIntrinsic::R600_txbc:
601         TextureOp = 5;
602         break;
603       case AMDGPUIntrinsic::R600_txf:
604         TextureOp = 6;
605         break;
606       case AMDGPUIntrinsic::R600_txq:
607         TextureOp = 7;
608         break;
609       case AMDGPUIntrinsic::R600_ddx:
610         TextureOp = 8;
611         break;
612       case AMDGPUIntrinsic::R600_ddy:
613         TextureOp = 9;
614         break;
615       default:
616         llvm_unreachable("Unknow Texture Operation");
617       }
618
619       SDValue TexArgs[19] = {
620         DAG.getConstant(TextureOp, MVT::i32),
621         Op.getOperand(1),
622         DAG.getConstant(0, MVT::i32),
623         DAG.getConstant(1, MVT::i32),
624         DAG.getConstant(2, MVT::i32),
625         DAG.getConstant(3, MVT::i32),
626         Op.getOperand(2),
627         Op.getOperand(3),
628         Op.getOperand(4),
629         DAG.getConstant(0, MVT::i32),
630         DAG.getConstant(1, MVT::i32),
631         DAG.getConstant(2, MVT::i32),
632         DAG.getConstant(3, MVT::i32),
633         Op.getOperand(5),
634         Op.getOperand(6),
635         Op.getOperand(7),
636         Op.getOperand(8),
637         Op.getOperand(9),
638         Op.getOperand(10)
639       };
640       return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs, 19);
641     }
642     case AMDGPUIntrinsic::AMDGPU_dp4: {
643       SDValue Args[8] = {
644       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
645           DAG.getConstant(0, MVT::i32)),
646       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
647           DAG.getConstant(0, MVT::i32)),
648       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
649           DAG.getConstant(1, MVT::i32)),
650       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
651           DAG.getConstant(1, MVT::i32)),
652       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
653           DAG.getConstant(2, MVT::i32)),
654       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
655           DAG.getConstant(2, MVT::i32)),
656       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
657           DAG.getConstant(3, MVT::i32)),
658       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
659           DAG.getConstant(3, MVT::i32))
660       };
661       return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args, 8);
662     }
663
664     case Intrinsic::r600_read_ngroups_x:
665       return LowerImplicitParameter(DAG, VT, DL, 0);
666     case Intrinsic::r600_read_ngroups_y:
667       return LowerImplicitParameter(DAG, VT, DL, 1);
668     case Intrinsic::r600_read_ngroups_z:
669       return LowerImplicitParameter(DAG, VT, DL, 2);
670     case Intrinsic::r600_read_global_size_x:
671       return LowerImplicitParameter(DAG, VT, DL, 3);
672     case Intrinsic::r600_read_global_size_y:
673       return LowerImplicitParameter(DAG, VT, DL, 4);
674     case Intrinsic::r600_read_global_size_z:
675       return LowerImplicitParameter(DAG, VT, DL, 5);
676     case Intrinsic::r600_read_local_size_x:
677       return LowerImplicitParameter(DAG, VT, DL, 6);
678     case Intrinsic::r600_read_local_size_y:
679       return LowerImplicitParameter(DAG, VT, DL, 7);
680     case Intrinsic::r600_read_local_size_z:
681       return LowerImplicitParameter(DAG, VT, DL, 8);
682
683     case Intrinsic::r600_read_tgid_x:
684       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
685                                   AMDGPU::T1_X, VT);
686     case Intrinsic::r600_read_tgid_y:
687       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
688                                   AMDGPU::T1_Y, VT);
689     case Intrinsic::r600_read_tgid_z:
690       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
691                                   AMDGPU::T1_Z, VT);
692     case Intrinsic::r600_read_tidig_x:
693       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
694                                   AMDGPU::T0_X, VT);
695     case Intrinsic::r600_read_tidig_y:
696       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
697                                   AMDGPU::T0_Y, VT);
698     case Intrinsic::r600_read_tidig_z:
699       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
700                                   AMDGPU::T0_Z, VT);
701     }
702     // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
703     break;
704   }
705   } // end switch(Op.getOpcode())
706   return SDValue();
707 }
708
709 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
710                                             SmallVectorImpl<SDValue> &Results,
711                                             SelectionDAG &DAG) const {
712   switch (N->getOpcode()) {
713   default: return;
714   case ISD::FP_TO_UINT: Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
715     return;
716   case ISD::LOAD: {
717     SDNode *Node = LowerLOAD(SDValue(N, 0), DAG).getNode();
718     Results.push_back(SDValue(Node, 0));
719     Results.push_back(SDValue(Node, 1));
720     // XXX: LLVM seems not to replace Chain Value inside CustomWidenLowerNode
721     // function
722     DAG.ReplaceAllUsesOfValueWith(SDValue(N,1), SDValue(Node, 1));
723     return;
724   }
725   case ISD::STORE:
726     SDNode *Node = LowerSTORE(SDValue(N, 0), DAG).getNode();
727     Results.push_back(SDValue(Node, 0));
728     return;
729   }
730 }
731
732 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
733   // On hw >= R700, COS/SIN input must be between -1. and 1.
734   // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
735   EVT VT = Op.getValueType();
736   SDValue Arg = Op.getOperand(0);
737   SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, SDLoc(Op), VT,
738       DAG.getNode(ISD::FADD, SDLoc(Op), VT,
739         DAG.getNode(ISD::FMUL, SDLoc(Op), VT, Arg,
740           DAG.getConstantFP(0.15915494309, MVT::f32)),
741         DAG.getConstantFP(0.5, MVT::f32)));
742   unsigned TrigNode;
743   switch (Op.getOpcode()) {
744   case ISD::FCOS:
745     TrigNode = AMDGPUISD::COS_HW;
746     break;
747   case ISD::FSIN:
748     TrigNode = AMDGPUISD::SIN_HW;
749     break;
750   default:
751     llvm_unreachable("Wrong trig opcode");
752   }
753   SDValue TrigVal = DAG.getNode(TrigNode, SDLoc(Op), VT,
754       DAG.getNode(ISD::FADD, SDLoc(Op), VT, FractPart,
755         DAG.getConstantFP(-0.5, MVT::f32)));
756   if (Gen >= AMDGPUSubtarget::R700)
757     return TrigVal;
758   // On R600 hw, COS/SIN input must be between -Pi and Pi.
759   return DAG.getNode(ISD::FMUL, SDLoc(Op), VT, TrigVal,
760       DAG.getConstantFP(3.14159265359, MVT::f32));
761 }
762
763 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
764   return DAG.getNode(
765       ISD::SETCC,
766       SDLoc(Op),
767       MVT::i1,
768       Op, DAG.getConstantFP(0.0f, MVT::f32),
769       DAG.getCondCode(ISD::SETNE)
770       );
771 }
772
773 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
774                                                    SDLoc DL,
775                                                    unsigned DwordOffset) const {
776   unsigned ByteOffset = DwordOffset * 4;
777   PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
778                                       AMDGPUAS::CONSTANT_BUFFER_0);
779
780   // We shouldn't be using an offset wider than 16-bits for implicit parameters.
781   assert(isInt<16>(ByteOffset));
782
783   return DAG.getLoad(VT, DL, DAG.getEntryNode(),
784                      DAG.getConstant(ByteOffset, MVT::i32), // PTR
785                      MachinePointerInfo(ConstantPointerNull::get(PtrType)),
786                      false, false, false, 0);
787 }
788
789 SDValue R600TargetLowering::LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const {
790
791   MachineFunction &MF = DAG.getMachineFunction();
792   const AMDGPUFrameLowering *TFL =
793    static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
794
795   FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
796   assert(FIN);
797
798   unsigned FrameIndex = FIN->getIndex();
799   unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
800   return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF), MVT::i32);
801 }
802
803 bool R600TargetLowering::isZero(SDValue Op) const {
804   if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
805     return Cst->isNullValue();
806   } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
807     return CstFP->isZero();
808   } else {
809     return false;
810   }
811 }
812
813 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
814   SDLoc DL(Op);
815   EVT VT = Op.getValueType();
816
817   SDValue LHS = Op.getOperand(0);
818   SDValue RHS = Op.getOperand(1);
819   SDValue True = Op.getOperand(2);
820   SDValue False = Op.getOperand(3);
821   SDValue CC = Op.getOperand(4);
822   SDValue Temp;
823
824   // LHS and RHS are guaranteed to be the same value type
825   EVT CompareVT = LHS.getValueType();
826
827   // Check if we can lower this to a native operation.
828
829   // Try to lower to a SET* instruction:
830   //
831   // SET* can match the following patterns:
832   //
833   // select_cc f32, f32, -1,  0, cc_any
834   // select_cc f32, f32, 1.0f, 0.0f, cc_any
835   // select_cc i32, i32, -1,  0, cc_any
836   //
837
838   // Move hardware True/False values to the correct operand.
839   if (isHWTrueValue(False) && isHWFalseValue(True)) {
840     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
841     std::swap(False, True);
842     CC = DAG.getCondCode(ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32));
843   }
844
845   if (isHWTrueValue(True) && isHWFalseValue(False) &&
846       (CompareVT == VT || VT == MVT::i32)) {
847     // This can be matched by a SET* instruction.
848     return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
849   }
850
851   // Try to lower to a CND* instruction:
852   //
853   // CND* can match the following patterns:
854   //
855   // select_cc f32, 0.0, f32, f32, cc_any
856   // select_cc f32, 0.0, i32, i32, cc_any
857   // select_cc i32, 0,   f32, f32, cc_any
858   // select_cc i32, 0,   i32, i32, cc_any
859   //
860   if (isZero(LHS) || isZero(RHS)) {
861     SDValue Cond = (isZero(LHS) ? RHS : LHS);
862     SDValue Zero = (isZero(LHS) ? LHS : RHS);
863     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
864     if (CompareVT != VT) {
865       // Bitcast True / False to the correct types.  This will end up being
866       // a nop, but it allows us to define only a single pattern in the
867       // .TD files for each CND* instruction rather than having to have
868       // one pattern for integer True/False and one for fp True/False
869       True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
870       False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
871     }
872     if (isZero(LHS)) {
873       CCOpcode = ISD::getSetCCSwappedOperands(CCOpcode);
874     }
875
876     switch (CCOpcode) {
877     case ISD::SETONE:
878     case ISD::SETUNE:
879     case ISD::SETNE:
880     case ISD::SETULE:
881     case ISD::SETULT:
882     case ISD::SETOLE:
883     case ISD::SETOLT:
884     case ISD::SETLE:
885     case ISD::SETLT:
886       CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
887       Temp = True;
888       True = False;
889       False = Temp;
890       break;
891     default:
892       break;
893     }
894     SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
895         Cond, Zero,
896         True, False,
897         DAG.getCondCode(CCOpcode));
898     return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
899   }
900
901
902   // Possible Min/Max pattern
903   SDValue MinMax = LowerMinMax(Op, DAG);
904   if (MinMax.getNode()) {
905     return MinMax;
906   }
907
908   // If we make it this for it means we have no native instructions to handle
909   // this SELECT_CC, so we must lower it.
910   SDValue HWTrue, HWFalse;
911
912   if (CompareVT == MVT::f32) {
913     HWTrue = DAG.getConstantFP(1.0f, CompareVT);
914     HWFalse = DAG.getConstantFP(0.0f, CompareVT);
915   } else if (CompareVT == MVT::i32) {
916     HWTrue = DAG.getConstant(-1, CompareVT);
917     HWFalse = DAG.getConstant(0, CompareVT);
918   }
919   else {
920     assert(!"Unhandled value type in LowerSELECT_CC");
921   }
922
923   // Lower this unsupported SELECT_CC into a combination of two supported
924   // SELECT_CC operations.
925   SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
926
927   return DAG.getNode(ISD::SELECT_CC, DL, VT,
928       Cond, HWFalse,
929       True, False,
930       DAG.getCondCode(ISD::SETNE));
931 }
932
933 SDValue R600TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
934   return DAG.getNode(ISD::SELECT_CC,
935       SDLoc(Op),
936       Op.getValueType(),
937       Op.getOperand(0),
938       DAG.getConstant(0, MVT::i32),
939       Op.getOperand(1),
940       Op.getOperand(2),
941       DAG.getCondCode(ISD::SETNE));
942 }
943
944 /// LLVM generates byte-addresed pointers.  For indirect addressing, we need to
945 /// convert these pointers to a register index.  Each register holds
946 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
947 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
948 /// for indirect addressing.
949 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
950                                                unsigned StackWidth,
951                                                SelectionDAG &DAG) const {
952   unsigned SRLPad;
953   switch(StackWidth) {
954   case 1:
955     SRLPad = 2;
956     break;
957   case 2:
958     SRLPad = 3;
959     break;
960   case 4:
961     SRLPad = 4;
962     break;
963   default: llvm_unreachable("Invalid stack width");
964   }
965
966   return DAG.getNode(ISD::SRL, SDLoc(Ptr), Ptr.getValueType(), Ptr,
967                      DAG.getConstant(SRLPad, MVT::i32));
968 }
969
970 void R600TargetLowering::getStackAddress(unsigned StackWidth,
971                                          unsigned ElemIdx,
972                                          unsigned &Channel,
973                                          unsigned &PtrIncr) const {
974   switch (StackWidth) {
975   default:
976   case 1:
977     Channel = 0;
978     if (ElemIdx > 0) {
979       PtrIncr = 1;
980     } else {
981       PtrIncr = 0;
982     }
983     break;
984   case 2:
985     Channel = ElemIdx % 2;
986     if (ElemIdx == 2) {
987       PtrIncr = 1;
988     } else {
989       PtrIncr = 0;
990     }
991     break;
992   case 4:
993     Channel = ElemIdx;
994     PtrIncr = 0;
995     break;
996   }
997 }
998
999 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1000   SDLoc DL(Op);
1001   StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1002   SDValue Chain = Op.getOperand(0);
1003   SDValue Value = Op.getOperand(1);
1004   SDValue Ptr = Op.getOperand(2);
1005
1006   SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1007   if (Result.getNode()) {
1008     return Result;
1009   }
1010
1011   if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1012     if (StoreNode->isTruncatingStore()) {
1013       EVT VT = Value.getValueType();
1014       assert(VT.bitsLE(MVT::i32));
1015       EVT MemVT = StoreNode->getMemoryVT();
1016       SDValue MaskConstant;
1017       if (MemVT == MVT::i8) {
1018         MaskConstant = DAG.getConstant(0xFF, MVT::i32);
1019       } else {
1020         assert(MemVT == MVT::i16);
1021         MaskConstant = DAG.getConstant(0xFFFF, MVT::i32);
1022       }
1023       SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1024                                       DAG.getConstant(2, MVT::i32));
1025       SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1026                                       DAG.getConstant(0x00000003, VT));
1027       SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1028       SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1029                                    DAG.getConstant(3, VT));
1030       SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1031       SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1032       // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1033       // vector instead.
1034       SDValue Src[4] = {
1035         ShiftedValue,
1036         DAG.getConstant(0, MVT::i32),
1037         DAG.getConstant(0, MVT::i32),
1038         Mask
1039       };
1040       SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src, 4);
1041       SDValue Args[3] = { Chain, Input, DWordAddr };
1042       return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1043                                      Op->getVTList(), Args, 3, MemVT,
1044                                      StoreNode->getMemOperand());
1045     } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1046                Value.getValueType().bitsGE(MVT::i32)) {
1047       // Convert pointer from byte address to dword address.
1048       Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1049                         DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1050                                     Ptr, DAG.getConstant(2, MVT::i32)));
1051
1052       if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1053         assert(!"Truncated and indexed stores not supported yet");
1054       } else {
1055         Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1056       }
1057       return Chain;
1058     }
1059   }
1060
1061   EVT ValueVT = Value.getValueType();
1062
1063   if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1064     return SDValue();
1065   }
1066
1067   // Lowering for indirect addressing
1068
1069   const MachineFunction &MF = DAG.getMachineFunction();
1070   const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1071                                          getTargetMachine().getFrameLowering());
1072   unsigned StackWidth = TFL->getStackWidth(MF);
1073
1074   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1075
1076   if (ValueVT.isVector()) {
1077     unsigned NumElemVT = ValueVT.getVectorNumElements();
1078     EVT ElemVT = ValueVT.getVectorElementType();
1079     SDValue Stores[4];
1080
1081     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1082                                       "vector width in load");
1083
1084     for (unsigned i = 0; i < NumElemVT; ++i) {
1085       unsigned Channel, PtrIncr;
1086       getStackAddress(StackWidth, i, Channel, PtrIncr);
1087       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1088                         DAG.getConstant(PtrIncr, MVT::i32));
1089       SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1090                                  Value, DAG.getConstant(i, MVT::i32));
1091
1092       Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1093                               Chain, Elem, Ptr,
1094                               DAG.getTargetConstant(Channel, MVT::i32));
1095     }
1096      Chain =  DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores, NumElemVT);
1097    } else {
1098     if (ValueVT == MVT::i8) {
1099       Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1100     }
1101     Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1102     DAG.getTargetConstant(0, MVT::i32)); // Channel
1103   }
1104
1105   return Chain;
1106 }
1107
1108 // return (512 + (kc_bank << 12)
1109 static int
1110 ConstantAddressBlock(unsigned AddressSpace) {
1111   switch (AddressSpace) {
1112   case AMDGPUAS::CONSTANT_BUFFER_0:
1113     return 512;
1114   case AMDGPUAS::CONSTANT_BUFFER_1:
1115     return 512 + 4096;
1116   case AMDGPUAS::CONSTANT_BUFFER_2:
1117     return 512 + 4096 * 2;
1118   case AMDGPUAS::CONSTANT_BUFFER_3:
1119     return 512 + 4096 * 3;
1120   case AMDGPUAS::CONSTANT_BUFFER_4:
1121     return 512 + 4096 * 4;
1122   case AMDGPUAS::CONSTANT_BUFFER_5:
1123     return 512 + 4096 * 5;
1124   case AMDGPUAS::CONSTANT_BUFFER_6:
1125     return 512 + 4096 * 6;
1126   case AMDGPUAS::CONSTANT_BUFFER_7:
1127     return 512 + 4096 * 7;
1128   case AMDGPUAS::CONSTANT_BUFFER_8:
1129     return 512 + 4096 * 8;
1130   case AMDGPUAS::CONSTANT_BUFFER_9:
1131     return 512 + 4096 * 9;
1132   case AMDGPUAS::CONSTANT_BUFFER_10:
1133     return 512 + 4096 * 10;
1134   case AMDGPUAS::CONSTANT_BUFFER_11:
1135     return 512 + 4096 * 11;
1136   case AMDGPUAS::CONSTANT_BUFFER_12:
1137     return 512 + 4096 * 12;
1138   case AMDGPUAS::CONSTANT_BUFFER_13:
1139     return 512 + 4096 * 13;
1140   case AMDGPUAS::CONSTANT_BUFFER_14:
1141     return 512 + 4096 * 14;
1142   case AMDGPUAS::CONSTANT_BUFFER_15:
1143     return 512 + 4096 * 15;
1144   default:
1145     return -1;
1146   }
1147 }
1148
1149 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1150 {
1151   EVT VT = Op.getValueType();
1152   SDLoc DL(Op);
1153   LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1154   SDValue Chain = Op.getOperand(0);
1155   SDValue Ptr = Op.getOperand(1);
1156   SDValue LoweredLoad;
1157
1158   int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1159   if (ConstantBlock > -1) {
1160     SDValue Result;
1161     if (dyn_cast<ConstantExpr>(LoadNode->getSrcValue()) ||
1162         dyn_cast<Constant>(LoadNode->getSrcValue()) ||
1163         dyn_cast<ConstantSDNode>(Ptr)) {
1164       SDValue Slots[4];
1165       for (unsigned i = 0; i < 4; i++) {
1166         // We want Const position encoded with the following formula :
1167         // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1168         // const_index is Ptr computed by llvm using an alignment of 16.
1169         // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1170         // then div by 4 at the ISel step
1171         SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1172             DAG.getConstant(4 * i + ConstantBlock * 16, MVT::i32));
1173         Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1174       }
1175       EVT NewVT = MVT::v4i32;
1176       unsigned NumElements = 4;
1177       if (VT.isVector()) {
1178         NewVT = VT;
1179         NumElements = VT.getVectorNumElements();
1180       }
1181       Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT, Slots, NumElements);
1182     } else {
1183       // non constant ptr cant be folded, keeps it as a v4f32 load
1184       Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1185           DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr, DAG.getConstant(4, MVT::i32)),
1186           DAG.getConstant(LoadNode->getAddressSpace() -
1187                           AMDGPUAS::CONSTANT_BUFFER_0, MVT::i32)
1188           );
1189     }
1190
1191     if (!VT.isVector()) {
1192       Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1193           DAG.getConstant(0, MVT::i32));
1194     }
1195
1196     SDValue MergedValues[2] = {
1197         Result,
1198         Chain
1199     };
1200     return DAG.getMergeValues(MergedValues, 2, DL);
1201   }
1202
1203   // For most operations returning SDValue() will result int he node being
1204   // expanded by the DAG Legalizer.  This is not the case for ISD::LOAD, so
1205   // we need to manually expand loads that may be legal in some address spaces
1206   // and illegal in others.  SEXT loads from CONSTANT_BUFFER_0 are supported
1207   // for compute shaders, since the data is sign extended when it is uploaded
1208   // to the buffer.  Howerver SEXT loads from other addresspaces are not
1209   // supported, so we need to expand them here.
1210   if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1211     EVT MemVT = LoadNode->getMemoryVT();
1212     assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1213     SDValue ShiftAmount =
1214           DAG.getConstant(VT.getSizeInBits() - MemVT.getSizeInBits(), MVT::i32);
1215     SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1216                                   LoadNode->getPointerInfo(), MemVT,
1217                                   LoadNode->isVolatile(),
1218                                   LoadNode->isNonTemporal(),
1219                                   LoadNode->getAlignment());
1220     SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, NewLoad, ShiftAmount);
1221     SDValue Sra = DAG.getNode(ISD::SRA, DL, VT, Shl, ShiftAmount);
1222
1223     SDValue MergedValues[2] = { Sra, Chain };
1224     return DAG.getMergeValues(MergedValues, 2, DL);
1225   }
1226
1227   if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1228     return SDValue();
1229   }
1230
1231   // Lowering for indirect addressing
1232   const MachineFunction &MF = DAG.getMachineFunction();
1233   const AMDGPUFrameLowering *TFL = static_cast<const AMDGPUFrameLowering*>(
1234                                          getTargetMachine().getFrameLowering());
1235   unsigned StackWidth = TFL->getStackWidth(MF);
1236
1237   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1238
1239   if (VT.isVector()) {
1240     unsigned NumElemVT = VT.getVectorNumElements();
1241     EVT ElemVT = VT.getVectorElementType();
1242     SDValue Loads[4];
1243
1244     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1245                                       "vector width in load");
1246
1247     for (unsigned i = 0; i < NumElemVT; ++i) {
1248       unsigned Channel, PtrIncr;
1249       getStackAddress(StackWidth, i, Channel, PtrIncr);
1250       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1251                         DAG.getConstant(PtrIncr, MVT::i32));
1252       Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1253                              Chain, Ptr,
1254                              DAG.getTargetConstant(Channel, MVT::i32),
1255                              Op.getOperand(2));
1256     }
1257     for (unsigned i = NumElemVT; i < 4; ++i) {
1258       Loads[i] = DAG.getUNDEF(ElemVT);
1259     }
1260     EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1261     LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads, 4);
1262   } else {
1263     LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1264                               Chain, Ptr,
1265                               DAG.getTargetConstant(0, MVT::i32), // Channel
1266                               Op.getOperand(2));
1267   }
1268
1269   SDValue Ops[2];
1270   Ops[0] = LoweredLoad;
1271   Ops[1] = Chain;
1272
1273   return DAG.getMergeValues(Ops, 2, DL);
1274 }
1275
1276 /// XXX Only kernel functions are supported, so we can assume for now that
1277 /// every function is a kernel function, but in the future we should use
1278 /// separate calling conventions for kernel and non-kernel functions.
1279 SDValue R600TargetLowering::LowerFormalArguments(
1280                                       SDValue Chain,
1281                                       CallingConv::ID CallConv,
1282                                       bool isVarArg,
1283                                       const SmallVectorImpl<ISD::InputArg> &Ins,
1284                                       SDLoc DL, SelectionDAG &DAG,
1285                                       SmallVectorImpl<SDValue> &InVals) const {
1286   SmallVector<CCValAssign, 16> ArgLocs;
1287   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
1288                  getTargetMachine(), ArgLocs, *DAG.getContext());
1289
1290   AnalyzeFormalArguments(CCInfo, Ins);
1291
1292   for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1293     CCValAssign &VA = ArgLocs[i];
1294     EVT VT = VA.getLocVT();
1295
1296     PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1297                                                    AMDGPUAS::CONSTANT_BUFFER_0);
1298
1299     // The first 36 bytes of the input buffer contains information about
1300     // thread group and global sizes.
1301     SDValue Arg = DAG.getLoad(VT, DL, Chain,
1302                            DAG.getConstant(36 + VA.getLocMemOffset(), MVT::i32),
1303                            MachinePointerInfo(UndefValue::get(PtrTy)), false,
1304                            false, false, 4); // 4 is the prefered alignment for
1305                                              // the CONSTANT memory space.
1306     InVals.push_back(Arg);
1307   }
1308   return Chain;
1309 }
1310
1311 EVT R600TargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1312    if (!VT.isVector()) return MVT::i32;
1313    return VT.changeVectorElementTypeToInteger();
1314 }
1315
1316 static SDValue
1317 CompactSwizzlableVector(SelectionDAG &DAG, SDValue VectorEntry,
1318                         DenseMap<unsigned, unsigned> &RemapSwizzle) {
1319   assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1320   assert(RemapSwizzle.empty());
1321   SDValue NewBldVec[4] = {
1322       VectorEntry.getOperand(0),
1323       VectorEntry.getOperand(1),
1324       VectorEntry.getOperand(2),
1325       VectorEntry.getOperand(3)
1326   };
1327
1328   for (unsigned i = 0; i < 4; i++) {
1329     if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1330       if (C->isZero()) {
1331         RemapSwizzle[i] = 4; // SEL_0
1332         NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1333       } else if (C->isExactlyValue(1.0)) {
1334         RemapSwizzle[i] = 5; // SEL_1
1335         NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1336       }
1337     }
1338
1339     if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1340       continue;
1341     for (unsigned j = 0; j < i; j++) {
1342       if (NewBldVec[i] == NewBldVec[j]) {
1343         NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1344         RemapSwizzle[i] = j;
1345         break;
1346       }
1347     }
1348   }
1349
1350   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1351       VectorEntry.getValueType(), NewBldVec, 4);
1352 }
1353
1354 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1355                                 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1356   assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1357   assert(RemapSwizzle.empty());
1358   SDValue NewBldVec[4] = {
1359       VectorEntry.getOperand(0),
1360       VectorEntry.getOperand(1),
1361       VectorEntry.getOperand(2),
1362       VectorEntry.getOperand(3)
1363   };
1364   bool isUnmovable[4] = { false, false, false, false };
1365   for (unsigned i = 0; i < 4; i++)
1366     RemapSwizzle[i] = i;
1367
1368   for (unsigned i = 0; i < 4; i++) {
1369     if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1370       unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1371           ->getZExtValue();
1372       if (!isUnmovable[Idx]) {
1373         // Swap i and Idx
1374         std::swap(NewBldVec[Idx], NewBldVec[i]);
1375         std::swap(RemapSwizzle[RemapSwizzle[Idx]], RemapSwizzle[RemapSwizzle[i]]);
1376       }
1377       isUnmovable[Idx] = true;
1378     }
1379   }
1380
1381   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1382       VectorEntry.getValueType(), NewBldVec, 4);
1383 }
1384
1385
1386 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1387 SDValue Swz[4], SelectionDAG &DAG) const {
1388   assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1389   // Old -> New swizzle values
1390   DenseMap<unsigned, unsigned> SwizzleRemap;
1391
1392   BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1393   for (unsigned i = 0; i < 4; i++) {
1394     unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1395     if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1396       Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1397   }
1398
1399   SwizzleRemap.clear();
1400   BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1401   for (unsigned i = 0; i < 4; i++) {
1402     unsigned Idx = dyn_cast<ConstantSDNode>(Swz[i])->getZExtValue();
1403     if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1404       Swz[i] = DAG.getConstant(SwizzleRemap[Idx], MVT::i32);
1405   }
1406
1407   return BuildVector;
1408 }
1409
1410
1411 //===----------------------------------------------------------------------===//
1412 // Custom DAG Optimizations
1413 //===----------------------------------------------------------------------===//
1414
1415 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1416                                               DAGCombinerInfo &DCI) const {
1417   SelectionDAG &DAG = DCI.DAG;
1418
1419   switch (N->getOpcode()) {
1420   // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1421   case ISD::FP_ROUND: {
1422       SDValue Arg = N->getOperand(0);
1423       if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1424         return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1425                            Arg.getOperand(0));
1426       }
1427       break;
1428     }
1429
1430   // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1431   // (i32 select_cc f32, f32, -1, 0 cc)
1432   //
1433   // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1434   // this to one of the SET*_DX10 instructions.
1435   case ISD::FP_TO_SINT: {
1436     SDValue FNeg = N->getOperand(0);
1437     if (FNeg.getOpcode() != ISD::FNEG) {
1438       return SDValue();
1439     }
1440     SDValue SelectCC = FNeg.getOperand(0);
1441     if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1442         SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1443         SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1444         !isHWTrueValue(SelectCC.getOperand(2)) ||
1445         !isHWFalseValue(SelectCC.getOperand(3))) {
1446       return SDValue();
1447     }
1448
1449     return DAG.getNode(ISD::SELECT_CC, SDLoc(N), N->getValueType(0),
1450                            SelectCC.getOperand(0), // LHS
1451                            SelectCC.getOperand(1), // RHS
1452                            DAG.getConstant(-1, MVT::i32), // True
1453                            DAG.getConstant(0, MVT::i32),  // Flase
1454                            SelectCC.getOperand(4)); // CC
1455
1456     break;
1457   }
1458
1459   // insert_vector_elt (build_vector elt0, …, eltN), NewEltIdx, idx
1460   // => build_vector elt0, …, NewEltIdx, …, eltN
1461   case ISD::INSERT_VECTOR_ELT: {
1462     SDValue InVec = N->getOperand(0);
1463     SDValue InVal = N->getOperand(1);
1464     SDValue EltNo = N->getOperand(2);
1465     SDLoc dl(N);
1466
1467     // If the inserted element is an UNDEF, just use the input vector.
1468     if (InVal.getOpcode() == ISD::UNDEF)
1469       return InVec;
1470
1471     EVT VT = InVec.getValueType();
1472
1473     // If we can't generate a legal BUILD_VECTOR, exit
1474     if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1475       return SDValue();
1476
1477     // Check that we know which element is being inserted
1478     if (!isa<ConstantSDNode>(EltNo))
1479       return SDValue();
1480     unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1481
1482     // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1483     // be converted to a BUILD_VECTOR).  Fill in the Ops vector with the
1484     // vector elements.
1485     SmallVector<SDValue, 8> Ops;
1486     if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1487       Ops.append(InVec.getNode()->op_begin(),
1488                  InVec.getNode()->op_end());
1489     } else if (InVec.getOpcode() == ISD::UNDEF) {
1490       unsigned NElts = VT.getVectorNumElements();
1491       Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1492     } else {
1493       return SDValue();
1494     }
1495
1496     // Insert the element
1497     if (Elt < Ops.size()) {
1498       // All the operands of BUILD_VECTOR must have the same type;
1499       // we enforce that here.
1500       EVT OpVT = Ops[0].getValueType();
1501       if (InVal.getValueType() != OpVT)
1502         InVal = OpVT.bitsGT(InVal.getValueType()) ?
1503           DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1504           DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1505       Ops[Elt] = InVal;
1506     }
1507
1508     // Return the new vector
1509     return DAG.getNode(ISD::BUILD_VECTOR, dl,
1510                        VT, &Ops[0], Ops.size());
1511   }
1512
1513   // Extract_vec (Build_vector) generated by custom lowering
1514   // also needs to be customly combined
1515   case ISD::EXTRACT_VECTOR_ELT: {
1516     SDValue Arg = N->getOperand(0);
1517     if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1518       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1519         unsigned Element = Const->getZExtValue();
1520         return Arg->getOperand(Element);
1521       }
1522     }
1523     if (Arg.getOpcode() == ISD::BITCAST &&
1524         Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1525       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1526         unsigned Element = Const->getZExtValue();
1527         return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1528             Arg->getOperand(0).getOperand(Element));
1529       }
1530     }
1531   }
1532
1533   case ISD::SELECT_CC: {
1534     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1535     //      selectcc x, y, a, b, inv(cc)
1536     //
1537     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1538     //      selectcc x, y, a, b, cc
1539     SDValue LHS = N->getOperand(0);
1540     if (LHS.getOpcode() != ISD::SELECT_CC) {
1541       return SDValue();
1542     }
1543
1544     SDValue RHS = N->getOperand(1);
1545     SDValue True = N->getOperand(2);
1546     SDValue False = N->getOperand(3);
1547     ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1548
1549     if (LHS.getOperand(2).getNode() != True.getNode() ||
1550         LHS.getOperand(3).getNode() != False.getNode() ||
1551         RHS.getNode() != False.getNode()) {
1552       return SDValue();
1553     }
1554
1555     switch (NCC) {
1556     default: return SDValue();
1557     case ISD::SETNE: return LHS;
1558     case ISD::SETEQ: {
1559       ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1560       LHSCC = ISD::getSetCCInverse(LHSCC,
1561                                   LHS.getOperand(0).getValueType().isInteger());
1562       return DAG.getSelectCC(SDLoc(N),
1563                              LHS.getOperand(0),
1564                              LHS.getOperand(1),
1565                              LHS.getOperand(2),
1566                              LHS.getOperand(3),
1567                              LHSCC);
1568     }
1569     }
1570   }
1571
1572   case AMDGPUISD::EXPORT: {
1573     SDValue Arg = N->getOperand(1);
1574     if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1575       break;
1576
1577     SDValue NewArgs[8] = {
1578       N->getOperand(0), // Chain
1579       SDValue(),
1580       N->getOperand(2), // ArrayBase
1581       N->getOperand(3), // Type
1582       N->getOperand(4), // SWZ_X
1583       N->getOperand(5), // SWZ_Y
1584       N->getOperand(6), // SWZ_Z
1585       N->getOperand(7) // SWZ_W
1586     };
1587     SDLoc DL(N);
1588     NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG);
1589     return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs, 8);
1590   }
1591   case AMDGPUISD::TEXTURE_FETCH: {
1592     SDValue Arg = N->getOperand(1);
1593     if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1594       break;
1595
1596     SDValue NewArgs[19] = {
1597       N->getOperand(0),
1598       N->getOperand(1),
1599       N->getOperand(2),
1600       N->getOperand(3),
1601       N->getOperand(4),
1602       N->getOperand(5),
1603       N->getOperand(6),
1604       N->getOperand(7),
1605       N->getOperand(8),
1606       N->getOperand(9),
1607       N->getOperand(10),
1608       N->getOperand(11),
1609       N->getOperand(12),
1610       N->getOperand(13),
1611       N->getOperand(14),
1612       N->getOperand(15),
1613       N->getOperand(16),
1614       N->getOperand(17),
1615       N->getOperand(18),
1616     };
1617     NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG);
1618     return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, SDLoc(N), N->getVTList(),
1619         NewArgs, 19);
1620   }
1621   }
1622   return SDValue();
1623 }