5699941d735e292c8b6a2e338622aa42f5956c6a
[oota-llvm.git] / lib / Target / AMDGPU / 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 "AMDGPUFrameLowering.h"
17 #include "AMDGPUIntrinsicInfo.h"
18 #include "AMDGPUSubtarget.h"
19 #include "R600Defines.h"
20 #include "R600InstrInfo.h"
21 #include "R600MachineFunctionInfo.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/IR/Argument.h"
29 #include "llvm/IR/Function.h"
30
31 using namespace llvm;
32
33 R600TargetLowering::R600TargetLowering(TargetMachine &TM,
34                                        const AMDGPUSubtarget &STI)
35     : AMDGPUTargetLowering(TM, STI), Gen(STI.getGeneration()) {
36   addRegisterClass(MVT::v4f32, &AMDGPU::R600_Reg128RegClass);
37   addRegisterClass(MVT::f32, &AMDGPU::R600_Reg32RegClass);
38   addRegisterClass(MVT::v4i32, &AMDGPU::R600_Reg128RegClass);
39   addRegisterClass(MVT::i32, &AMDGPU::R600_Reg32RegClass);
40   addRegisterClass(MVT::v2f32, &AMDGPU::R600_Reg64RegClass);
41   addRegisterClass(MVT::v2i32, &AMDGPU::R600_Reg64RegClass);
42
43   computeRegisterProperties(STI.getRegisterInfo());
44
45   // Set condition code actions
46   setCondCodeAction(ISD::SETO,   MVT::f32, Expand);
47   setCondCodeAction(ISD::SETUO,  MVT::f32, Expand);
48   setCondCodeAction(ISD::SETLT,  MVT::f32, Expand);
49   setCondCodeAction(ISD::SETLE,  MVT::f32, Expand);
50   setCondCodeAction(ISD::SETOLT, MVT::f32, Expand);
51   setCondCodeAction(ISD::SETOLE, MVT::f32, Expand);
52   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
53   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
54   setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
55   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
56   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
57   setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
58
59   setCondCodeAction(ISD::SETLE, MVT::i32, Expand);
60   setCondCodeAction(ISD::SETLT, MVT::i32, Expand);
61   setCondCodeAction(ISD::SETULE, MVT::i32, Expand);
62   setCondCodeAction(ISD::SETULT, MVT::i32, Expand);
63
64   setOperationAction(ISD::FCOS, MVT::f32, Custom);
65   setOperationAction(ISD::FSIN, MVT::f32, Custom);
66
67   setOperationAction(ISD::SETCC, MVT::v4i32, Expand);
68   setOperationAction(ISD::SETCC, MVT::v2i32, Expand);
69
70   setOperationAction(ISD::BR_CC, MVT::i32, Expand);
71   setOperationAction(ISD::BR_CC, MVT::f32, Expand);
72   setOperationAction(ISD::BRCOND, MVT::Other, Custom);
73
74   setOperationAction(ISD::FSUB, MVT::f32, Expand);
75
76   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
77   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
78   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i1, Custom);
79
80   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
81   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
82
83   setOperationAction(ISD::SETCC, MVT::i32, Expand);
84   setOperationAction(ISD::SETCC, MVT::f32, Expand);
85   setOperationAction(ISD::FP_TO_UINT, MVT::i1, Custom);
86   setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
87   setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
88
89   setOperationAction(ISD::SELECT, MVT::i32, Expand);
90   setOperationAction(ISD::SELECT, MVT::f32, Expand);
91   setOperationAction(ISD::SELECT, MVT::v2i32, Expand);
92   setOperationAction(ISD::SELECT, MVT::v4i32, Expand);
93
94   // ADD, SUB overflow.
95   // TODO: turn these into Legal?
96   if (Subtarget->hasCARRY())
97     setOperationAction(ISD::UADDO, MVT::i32, Custom);
98
99   if (Subtarget->hasBORROW())
100     setOperationAction(ISD::USUBO, MVT::i32, Custom);
101
102   // Expand sign extension of vectors
103   if (!Subtarget->hasBFE())
104     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
105
106   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Expand);
107   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Expand);
108
109   if (!Subtarget->hasBFE())
110     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Expand);
111   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Expand);
112   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Expand);
113
114   if (!Subtarget->hasBFE())
115     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
116   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Expand);
117   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Expand);
118
119   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
120   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i32, Expand);
121   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i32, Expand);
122
123   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Expand);
124
125
126   // Legalize loads and stores to the private address space.
127   setOperationAction(ISD::LOAD, MVT::i32, Custom);
128   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
129   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
130
131   // EXTLOAD should be the same as ZEXTLOAD. It is legal for some address
132   // spaces, so it is custom lowered to handle those where it isn't.
133   for (MVT VT : MVT::integer_valuetypes()) {
134     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
135     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i8, Custom);
136     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i16, Custom);
137
138     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
139     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i8, Custom);
140     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i16, Custom);
141
142     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i1, Promote);
143     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i8, Custom);
144     setLoadExtAction(ISD::EXTLOAD, VT, MVT::i16, Custom);
145   }
146
147   setOperationAction(ISD::STORE, MVT::i8, Custom);
148   setOperationAction(ISD::STORE, MVT::i32, Custom);
149   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
150   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
151   setTruncStoreAction(MVT::i32, MVT::i8, Custom);
152   setTruncStoreAction(MVT::i32, MVT::i16, Custom);
153
154   setOperationAction(ISD::LOAD, MVT::i32, Custom);
155   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
156   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
157
158   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i32, Custom);
159   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f32, Custom);
160   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
161   setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
162
163   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i32, Custom);
164   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f32, Custom);
165   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
166   setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
167
168   setTargetDAGCombine(ISD::FP_ROUND);
169   setTargetDAGCombine(ISD::FP_TO_SINT);
170   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
171   setTargetDAGCombine(ISD::SELECT_CC);
172   setTargetDAGCombine(ISD::INSERT_VECTOR_ELT);
173
174   // We don't have 64-bit shifts. Thus we need either SHX i64 or SHX_PARTS i32
175   //  to be Legal/Custom in order to avoid library calls.
176   setOperationAction(ISD::SHL_PARTS, MVT::i32, Custom);
177   setOperationAction(ISD::SRL_PARTS, MVT::i32, Custom);
178   setOperationAction(ISD::SRA_PARTS, MVT::i32, Custom);
179
180   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
181
182   const MVT ScalarIntVTs[] = { MVT::i32, MVT::i64 };
183   for (MVT VT : ScalarIntVTs) {
184     setOperationAction(ISD::ADDC, VT, Expand);
185     setOperationAction(ISD::SUBC, VT, Expand);
186     setOperationAction(ISD::ADDE, VT, Expand);
187     setOperationAction(ISD::SUBE, VT, Expand);
188   }
189
190   setSchedulingPreference(Sched::Source);
191 }
192
193 MachineBasicBlock * R600TargetLowering::EmitInstrWithCustomInserter(
194     MachineInstr * MI, MachineBasicBlock * BB) const {
195   MachineFunction * MF = BB->getParent();
196   MachineRegisterInfo &MRI = MF->getRegInfo();
197   MachineBasicBlock::iterator I = *MI;
198   const R600InstrInfo *TII =
199       static_cast<const R600InstrInfo *>(Subtarget->getInstrInfo());
200
201   switch (MI->getOpcode()) {
202   default:
203     // Replace LDS_*_RET instruction that don't have any uses with the
204     // equivalent LDS_*_NORET instruction.
205     if (TII->isLDSRetInstr(MI->getOpcode())) {
206       int DstIdx = TII->getOperandIdx(MI->getOpcode(), AMDGPU::OpName::dst);
207       assert(DstIdx != -1);
208       MachineInstrBuilder NewMI;
209       // FIXME: getLDSNoRetOp method only handles LDS_1A1D LDS ops. Add
210       //        LDS_1A2D support and remove this special case.
211       if (!MRI.use_empty(MI->getOperand(DstIdx).getReg()) ||
212            MI->getOpcode() == AMDGPU::LDS_CMPST_RET)
213         return BB;
214
215       NewMI = BuildMI(*BB, I, BB->findDebugLoc(I),
216                       TII->get(AMDGPU::getLDSNoRetOp(MI->getOpcode())));
217       for (unsigned i = 1, e = MI->getNumOperands(); i < e; ++i) {
218         NewMI.addOperand(MI->getOperand(i));
219       }
220     } else {
221       return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
222     }
223     break;
224   case AMDGPU::CLAMP_R600: {
225     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
226                                                    AMDGPU::MOV,
227                                                    MI->getOperand(0).getReg(),
228                                                    MI->getOperand(1).getReg());
229     TII->addFlag(NewMI, 0, MO_FLAG_CLAMP);
230     break;
231   }
232
233   case AMDGPU::FABS_R600: {
234     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
235                                                     AMDGPU::MOV,
236                                                     MI->getOperand(0).getReg(),
237                                                     MI->getOperand(1).getReg());
238     TII->addFlag(NewMI, 0, MO_FLAG_ABS);
239     break;
240   }
241
242   case AMDGPU::FNEG_R600: {
243     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, I,
244                                                     AMDGPU::MOV,
245                                                     MI->getOperand(0).getReg(),
246                                                     MI->getOperand(1).getReg());
247     TII->addFlag(NewMI, 0, MO_FLAG_NEG);
248     break;
249   }
250
251   case AMDGPU::MASK_WRITE: {
252     unsigned maskedRegister = MI->getOperand(0).getReg();
253     assert(TargetRegisterInfo::isVirtualRegister(maskedRegister));
254     MachineInstr * defInstr = MRI.getVRegDef(maskedRegister);
255     TII->addFlag(defInstr, 0, MO_FLAG_MASK);
256     break;
257   }
258
259   case AMDGPU::MOV_IMM_F32:
260     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
261                      MI->getOperand(1).getFPImm()->getValueAPF()
262                          .bitcastToAPInt().getZExtValue());
263     break;
264   case AMDGPU::MOV_IMM_I32:
265     TII->buildMovImm(*BB, I, MI->getOperand(0).getReg(),
266                      MI->getOperand(1).getImm());
267     break;
268   case AMDGPU::CONST_COPY: {
269     MachineInstr *NewMI = TII->buildDefaultInstruction(*BB, MI, AMDGPU::MOV,
270         MI->getOperand(0).getReg(), AMDGPU::ALU_CONST);
271     TII->setImmOperand(NewMI, AMDGPU::OpName::src0_sel,
272         MI->getOperand(1).getImm());
273     break;
274   }
275
276   case AMDGPU::RAT_WRITE_CACHELESS_32_eg:
277   case AMDGPU::RAT_WRITE_CACHELESS_64_eg:
278   case AMDGPU::RAT_WRITE_CACHELESS_128_eg: {
279     unsigned EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
280
281     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
282             .addOperand(MI->getOperand(0))
283             .addOperand(MI->getOperand(1))
284             .addImm(EOP); // Set End of program bit
285     break;
286   }
287
288   case AMDGPU::TXD: {
289     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
290     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
291     MachineOperand &RID = MI->getOperand(4);
292     MachineOperand &SID = MI->getOperand(5);
293     unsigned TextureId = MI->getOperand(6).getImm();
294     unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
295     unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
296
297     switch (TextureId) {
298     case 5: // Rect
299       CTX = CTY = 0;
300       break;
301     case 6: // Shadow1D
302       SrcW = SrcZ;
303       break;
304     case 7: // Shadow2D
305       SrcW = SrcZ;
306       break;
307     case 8: // ShadowRect
308       CTX = CTY = 0;
309       SrcW = SrcZ;
310       break;
311     case 9: // 1DArray
312       SrcZ = SrcY;
313       CTZ = 0;
314       break;
315     case 10: // 2DArray
316       CTZ = 0;
317       break;
318     case 11: // Shadow1DArray
319       SrcZ = SrcY;
320       CTZ = 0;
321       break;
322     case 12: // Shadow2DArray
323       CTZ = 0;
324       break;
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_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::TXD_SHADOW: {
390     unsigned T0 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
391     unsigned T1 = MRI.createVirtualRegister(&AMDGPU::R600_Reg128RegClass);
392     MachineOperand &RID = MI->getOperand(4);
393     MachineOperand &SID = MI->getOperand(5);
394     unsigned TextureId = MI->getOperand(6).getImm();
395     unsigned SrcX = 0, SrcY = 1, SrcZ = 2, SrcW = 3;
396     unsigned CTX = 1, CTY = 1, CTZ = 1, CTW = 1;
397
398     switch (TextureId) {
399     case 5: // Rect
400       CTX = CTY = 0;
401       break;
402     case 6: // Shadow1D
403       SrcW = SrcZ;
404       break;
405     case 7: // Shadow2D
406       SrcW = SrcZ;
407       break;
408     case 8: // ShadowRect
409       CTX = CTY = 0;
410       SrcW = SrcZ;
411       break;
412     case 9: // 1DArray
413       SrcZ = SrcY;
414       CTZ = 0;
415       break;
416     case 10: // 2DArray
417       CTZ = 0;
418       break;
419     case 11: // Shadow1DArray
420       SrcZ = SrcY;
421       CTZ = 0;
422       break;
423     case 12: // Shadow2DArray
424       CTZ = 0;
425       break;
426     }
427
428     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_H), T0)
429             .addOperand(MI->getOperand(3))
430             .addImm(SrcX)
431             .addImm(SrcY)
432             .addImm(SrcZ)
433             .addImm(SrcW)
434             .addImm(0)
435             .addImm(0)
436             .addImm(0)
437             .addImm(0)
438             .addImm(1)
439             .addImm(2)
440             .addImm(3)
441             .addOperand(RID)
442             .addOperand(SID)
443             .addImm(CTX)
444             .addImm(CTY)
445             .addImm(CTZ)
446             .addImm(CTW);
447     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SET_GRADIENTS_V), T1)
448             .addOperand(MI->getOperand(2))
449             .addImm(SrcX)
450             .addImm(SrcY)
451             .addImm(SrcZ)
452             .addImm(SrcW)
453             .addImm(0)
454             .addImm(0)
455             .addImm(0)
456             .addImm(0)
457             .addImm(1)
458             .addImm(2)
459             .addImm(3)
460             .addOperand(RID)
461             .addOperand(SID)
462             .addImm(CTX)
463             .addImm(CTY)
464             .addImm(CTZ)
465             .addImm(CTW);
466     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::TEX_SAMPLE_C_G))
467             .addOperand(MI->getOperand(0))
468             .addOperand(MI->getOperand(1))
469             .addImm(SrcX)
470             .addImm(SrcY)
471             .addImm(SrcZ)
472             .addImm(SrcW)
473             .addImm(0)
474             .addImm(0)
475             .addImm(0)
476             .addImm(0)
477             .addImm(1)
478             .addImm(2)
479             .addImm(3)
480             .addOperand(RID)
481             .addOperand(SID)
482             .addImm(CTX)
483             .addImm(CTY)
484             .addImm(CTZ)
485             .addImm(CTW)
486             .addReg(T0, RegState::Implicit)
487             .addReg(T1, RegState::Implicit);
488     break;
489   }
490
491   case AMDGPU::BRANCH:
492       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP))
493               .addOperand(MI->getOperand(0));
494       break;
495
496   case AMDGPU::BRANCH_COND_f32: {
497     MachineInstr *NewMI =
498       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
499               AMDGPU::PREDICATE_BIT)
500               .addOperand(MI->getOperand(1))
501               .addImm(OPCODE_IS_NOT_ZERO)
502               .addImm(0); // Flags
503     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
504     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
505             .addOperand(MI->getOperand(0))
506             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
507     break;
508   }
509
510   case AMDGPU::BRANCH_COND_i32: {
511     MachineInstr *NewMI =
512       BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::PRED_X),
513             AMDGPU::PREDICATE_BIT)
514             .addOperand(MI->getOperand(1))
515             .addImm(OPCODE_IS_NOT_ZERO_INT)
516             .addImm(0); // Flags
517     TII->addFlag(NewMI, 0, MO_FLAG_PUSH);
518     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(AMDGPU::JUMP_COND))
519            .addOperand(MI->getOperand(0))
520             .addReg(AMDGPU::PREDICATE_BIT, RegState::Kill);
521     break;
522   }
523
524   case AMDGPU::EG_ExportSwz:
525   case AMDGPU::R600_ExportSwz: {
526     // Instruction is left unmodified if its not the last one of its type
527     bool isLastInstructionOfItsType = true;
528     unsigned InstExportType = MI->getOperand(1).getImm();
529     for (MachineBasicBlock::iterator NextExportInst = std::next(I),
530          EndBlock = BB->end(); NextExportInst != EndBlock;
531          NextExportInst = std::next(NextExportInst)) {
532       if (NextExportInst->getOpcode() == AMDGPU::EG_ExportSwz ||
533           NextExportInst->getOpcode() == AMDGPU::R600_ExportSwz) {
534         unsigned CurrentInstExportType = NextExportInst->getOperand(1)
535             .getImm();
536         if (CurrentInstExportType == InstExportType) {
537           isLastInstructionOfItsType = false;
538           break;
539         }
540       }
541     }
542     bool EOP = (std::next(I)->getOpcode() == AMDGPU::RETURN) ? 1 : 0;
543     if (!EOP && !isLastInstructionOfItsType)
544       return BB;
545     unsigned CfInst = (MI->getOpcode() == AMDGPU::EG_ExportSwz)? 84 : 40;
546     BuildMI(*BB, I, BB->findDebugLoc(I), TII->get(MI->getOpcode()))
547             .addOperand(MI->getOperand(0))
548             .addOperand(MI->getOperand(1))
549             .addOperand(MI->getOperand(2))
550             .addOperand(MI->getOperand(3))
551             .addOperand(MI->getOperand(4))
552             .addOperand(MI->getOperand(5))
553             .addOperand(MI->getOperand(6))
554             .addImm(CfInst)
555             .addImm(EOP);
556     break;
557   }
558   case AMDGPU::RETURN: {
559     // RETURN instructions must have the live-out registers as implicit uses,
560     // otherwise they appear dead.
561     R600MachineFunctionInfo *MFI = MF->getInfo<R600MachineFunctionInfo>();
562     MachineInstrBuilder MIB(*MF, MI);
563     for (unsigned i = 0, e = MFI->LiveOuts.size(); i != e; ++i)
564       MIB.addReg(MFI->LiveOuts[i], RegState::Implicit);
565     return BB;
566   }
567   }
568
569   MI->eraseFromParent();
570   return BB;
571 }
572
573 //===----------------------------------------------------------------------===//
574 // Custom DAG Lowering Operations
575 //===----------------------------------------------------------------------===//
576
577 SDValue R600TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
578   MachineFunction &MF = DAG.getMachineFunction();
579   R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
580   switch (Op.getOpcode()) {
581   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
582   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
583   case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
584   case ISD::SHL_PARTS: return LowerSHLParts(Op, DAG);
585   case ISD::SRA_PARTS:
586   case ISD::SRL_PARTS: return LowerSRXParts(Op, DAG);
587   case ISD::UADDO: return LowerUADDSUBO(Op, DAG, ISD::ADD, AMDGPUISD::CARRY);
588   case ISD::USUBO: return LowerUADDSUBO(Op, DAG, ISD::SUB, AMDGPUISD::BORROW);
589   case ISD::FCOS:
590   case ISD::FSIN: return LowerTrig(Op, DAG);
591   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
592   case ISD::STORE: return LowerSTORE(Op, DAG);
593   case ISD::LOAD: {
594     SDValue Result = LowerLOAD(Op, DAG);
595     assert((!Result.getNode() ||
596             Result.getNode()->getNumValues() == 2) &&
597            "Load should return a value and a chain");
598     return Result;
599   }
600
601   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
602   case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
603   case ISD::INTRINSIC_VOID: {
604     SDValue Chain = Op.getOperand(0);
605     unsigned IntrinsicID =
606                          cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
607     switch (IntrinsicID) {
608     case AMDGPUIntrinsic::AMDGPU_store_output: {
609       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
610       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
611       MFI->LiveOuts.push_back(Reg);
612       return DAG.getCopyToReg(Chain, SDLoc(Op), Reg, Op.getOperand(2));
613     }
614     case AMDGPUIntrinsic::R600_store_swizzle: {
615       SDLoc DL(Op);
616       const SDValue Args[8] = {
617         Chain,
618         Op.getOperand(2), // Export Value
619         Op.getOperand(3), // ArrayBase
620         Op.getOperand(4), // Type
621         DAG.getConstant(0, DL, MVT::i32), // SWZ_X
622         DAG.getConstant(1, DL, MVT::i32), // SWZ_Y
623         DAG.getConstant(2, DL, MVT::i32), // SWZ_Z
624         DAG.getConstant(3, DL, MVT::i32) // SWZ_W
625       };
626       return DAG.getNode(AMDGPUISD::EXPORT, DL, Op.getValueType(), Args);
627     }
628
629     // default for switch(IntrinsicID)
630     default: break;
631     }
632     // break out of case ISD::INTRINSIC_VOID in switch(Op.getOpcode())
633     break;
634   }
635   case ISD::INTRINSIC_WO_CHAIN: {
636     unsigned IntrinsicID =
637                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
638     EVT VT = Op.getValueType();
639     SDLoc DL(Op);
640     switch(IntrinsicID) {
641     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
642     case AMDGPUIntrinsic::R600_load_input: {
643       int64_t RegIndex = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
644       unsigned Reg = AMDGPU::R600_TReg32RegClass.getRegister(RegIndex);
645       MachineFunction &MF = DAG.getMachineFunction();
646       MachineRegisterInfo &MRI = MF.getRegInfo();
647       MRI.addLiveIn(Reg);
648       return DAG.getCopyFromReg(DAG.getEntryNode(),
649           SDLoc(DAG.getEntryNode()), Reg, VT);
650     }
651
652     case AMDGPUIntrinsic::R600_interp_input: {
653       int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
654       int ijb = cast<ConstantSDNode>(Op.getOperand(2))->getSExtValue();
655       MachineSDNode *interp;
656       if (ijb < 0) {
657         const R600InstrInfo *TII =
658             static_cast<const R600InstrInfo *>(Subtarget->getInstrInfo());
659         interp = DAG.getMachineNode(AMDGPU::INTERP_VEC_LOAD, DL,
660             MVT::v4f32, DAG.getTargetConstant(slot / 4, DL, MVT::i32));
661         return DAG.getTargetExtractSubreg(
662             TII->getRegisterInfo().getSubRegFromChannel(slot % 4),
663             DL, MVT::f32, SDValue(interp, 0));
664       }
665       MachineFunction &MF = DAG.getMachineFunction();
666       MachineRegisterInfo &MRI = MF.getRegInfo();
667       unsigned RegisterI = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb);
668       unsigned RegisterJ = AMDGPU::R600_TReg32RegClass.getRegister(2 * ijb + 1);
669       MRI.addLiveIn(RegisterI);
670       MRI.addLiveIn(RegisterJ);
671       SDValue RegisterINode = DAG.getCopyFromReg(DAG.getEntryNode(),
672           SDLoc(DAG.getEntryNode()), RegisterI, MVT::f32);
673       SDValue RegisterJNode = DAG.getCopyFromReg(DAG.getEntryNode(),
674           SDLoc(DAG.getEntryNode()), RegisterJ, MVT::f32);
675
676       if (slot % 4 < 2)
677         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
678             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4, DL, MVT::i32),
679             RegisterJNode, RegisterINode);
680       else
681         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
682             MVT::f32, MVT::f32, DAG.getTargetConstant(slot / 4, DL, MVT::i32),
683             RegisterJNode, RegisterINode);
684       return SDValue(interp, slot % 2);
685     }
686     case AMDGPUIntrinsic::R600_interp_xy:
687     case AMDGPUIntrinsic::R600_interp_zw: {
688       int slot = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
689       MachineSDNode *interp;
690       SDValue RegisterINode = Op.getOperand(2);
691       SDValue RegisterJNode = Op.getOperand(3);
692
693       if (IntrinsicID == AMDGPUIntrinsic::R600_interp_xy)
694         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_XY, DL,
695             MVT::f32, MVT::f32, DAG.getTargetConstant(slot, DL, MVT::i32),
696             RegisterJNode, RegisterINode);
697       else
698         interp = DAG.getMachineNode(AMDGPU::INTERP_PAIR_ZW, DL,
699             MVT::f32, MVT::f32, DAG.getTargetConstant(slot, DL, MVT::i32),
700             RegisterJNode, RegisterINode);
701       return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2f32,
702           SDValue(interp, 0), SDValue(interp, 1));
703     }
704     case AMDGPUIntrinsic::R600_tex:
705     case AMDGPUIntrinsic::R600_texc:
706     case AMDGPUIntrinsic::R600_txl:
707     case AMDGPUIntrinsic::R600_txlc:
708     case AMDGPUIntrinsic::R600_txb:
709     case AMDGPUIntrinsic::R600_txbc:
710     case AMDGPUIntrinsic::R600_txf:
711     case AMDGPUIntrinsic::R600_txq:
712     case AMDGPUIntrinsic::R600_ddx:
713     case AMDGPUIntrinsic::R600_ddy:
714     case AMDGPUIntrinsic::R600_ldptr: {
715       unsigned TextureOp;
716       switch (IntrinsicID) {
717       case AMDGPUIntrinsic::R600_tex:
718         TextureOp = 0;
719         break;
720       case AMDGPUIntrinsic::R600_texc:
721         TextureOp = 1;
722         break;
723       case AMDGPUIntrinsic::R600_txl:
724         TextureOp = 2;
725         break;
726       case AMDGPUIntrinsic::R600_txlc:
727         TextureOp = 3;
728         break;
729       case AMDGPUIntrinsic::R600_txb:
730         TextureOp = 4;
731         break;
732       case AMDGPUIntrinsic::R600_txbc:
733         TextureOp = 5;
734         break;
735       case AMDGPUIntrinsic::R600_txf:
736         TextureOp = 6;
737         break;
738       case AMDGPUIntrinsic::R600_txq:
739         TextureOp = 7;
740         break;
741       case AMDGPUIntrinsic::R600_ddx:
742         TextureOp = 8;
743         break;
744       case AMDGPUIntrinsic::R600_ddy:
745         TextureOp = 9;
746         break;
747       case AMDGPUIntrinsic::R600_ldptr:
748         TextureOp = 10;
749         break;
750       default:
751         llvm_unreachable("Unknow Texture Operation");
752       }
753
754       SDValue TexArgs[19] = {
755         DAG.getConstant(TextureOp, DL, MVT::i32),
756         Op.getOperand(1),
757         DAG.getConstant(0, DL, MVT::i32),
758         DAG.getConstant(1, DL, MVT::i32),
759         DAG.getConstant(2, DL, MVT::i32),
760         DAG.getConstant(3, DL, MVT::i32),
761         Op.getOperand(2),
762         Op.getOperand(3),
763         Op.getOperand(4),
764         DAG.getConstant(0, DL, MVT::i32),
765         DAG.getConstant(1, DL, MVT::i32),
766         DAG.getConstant(2, DL, MVT::i32),
767         DAG.getConstant(3, DL, MVT::i32),
768         Op.getOperand(5),
769         Op.getOperand(6),
770         Op.getOperand(7),
771         Op.getOperand(8),
772         Op.getOperand(9),
773         Op.getOperand(10)
774       };
775       return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, MVT::v4f32, TexArgs);
776     }
777     case AMDGPUIntrinsic::AMDGPU_dp4: {
778       SDValue Args[8] = {
779       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
780           DAG.getConstant(0, DL, MVT::i32)),
781       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
782           DAG.getConstant(0, DL, MVT::i32)),
783       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
784           DAG.getConstant(1, DL, MVT::i32)),
785       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
786           DAG.getConstant(1, DL, MVT::i32)),
787       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
788           DAG.getConstant(2, DL, MVT::i32)),
789       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
790           DAG.getConstant(2, DL, MVT::i32)),
791       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(1),
792           DAG.getConstant(3, DL, MVT::i32)),
793       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, Op.getOperand(2),
794           DAG.getConstant(3, DL, MVT::i32))
795       };
796       return DAG.getNode(AMDGPUISD::DOT4, DL, MVT::f32, Args);
797     }
798
799     case Intrinsic::r600_read_ngroups_x:
800       return LowerImplicitParameter(DAG, VT, DL, 0);
801     case Intrinsic::r600_read_ngroups_y:
802       return LowerImplicitParameter(DAG, VT, DL, 1);
803     case Intrinsic::r600_read_ngroups_z:
804       return LowerImplicitParameter(DAG, VT, DL, 2);
805     case Intrinsic::r600_read_global_size_x:
806       return LowerImplicitParameter(DAG, VT, DL, 3);
807     case Intrinsic::r600_read_global_size_y:
808       return LowerImplicitParameter(DAG, VT, DL, 4);
809     case Intrinsic::r600_read_global_size_z:
810       return LowerImplicitParameter(DAG, VT, DL, 5);
811     case Intrinsic::r600_read_local_size_x:
812       return LowerImplicitParameter(DAG, VT, DL, 6);
813     case Intrinsic::r600_read_local_size_y:
814       return LowerImplicitParameter(DAG, VT, DL, 7);
815     case Intrinsic::r600_read_local_size_z:
816       return LowerImplicitParameter(DAG, VT, DL, 8);
817
818     case Intrinsic::AMDGPU_read_workdim: {
819       uint32_t ByteOffset = getImplicitParameterOffset(MFI, GRID_DIM);
820       return LowerImplicitParameter(DAG, VT, DL, ByteOffset / 4);
821     }
822
823     case Intrinsic::r600_read_tgid_x:
824       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
825                                   AMDGPU::T1_X, VT);
826     case Intrinsic::r600_read_tgid_y:
827       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
828                                   AMDGPU::T1_Y, VT);
829     case Intrinsic::r600_read_tgid_z:
830       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
831                                   AMDGPU::T1_Z, VT);
832     case Intrinsic::r600_read_tidig_x:
833       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
834                                   AMDGPU::T0_X, VT);
835     case Intrinsic::r600_read_tidig_y:
836       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
837                                   AMDGPU::T0_Y, VT);
838     case Intrinsic::r600_read_tidig_z:
839       return CreateLiveInRegister(DAG, &AMDGPU::R600_TReg32RegClass,
840                                   AMDGPU::T0_Z, VT);
841     case Intrinsic::AMDGPU_rsq:
842       // XXX - I'm assuming SI's RSQ_LEGACY matches R600's behavior.
843       return DAG.getNode(AMDGPUISD::RSQ_LEGACY, DL, VT, Op.getOperand(1));
844
845     case AMDGPUIntrinsic::AMDGPU_fract:
846     case AMDGPUIntrinsic::AMDIL_fraction: // Legacy name.
847       return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
848     }
849     // break out of case ISD::INTRINSIC_WO_CHAIN in switch(Op.getOpcode())
850     break;
851   }
852   } // end switch(Op.getOpcode())
853   return SDValue();
854 }
855
856 void R600TargetLowering::ReplaceNodeResults(SDNode *N,
857                                             SmallVectorImpl<SDValue> &Results,
858                                             SelectionDAG &DAG) const {
859   switch (N->getOpcode()) {
860   default:
861     AMDGPUTargetLowering::ReplaceNodeResults(N, Results, DAG);
862     return;
863   case ISD::FP_TO_UINT:
864     if (N->getValueType(0) == MVT::i1) {
865       Results.push_back(LowerFPTOUINT(N->getOperand(0), DAG));
866       return;
867     }
868     // Fall-through. Since we don't care about out of bounds values
869     // we can use FP_TO_SINT for uints too. The DAGLegalizer code for uint
870     // considers some extra cases which are not necessary here.
871   case ISD::FP_TO_SINT: {
872     SDValue Result;
873     if (expandFP_TO_SINT(N, Result, DAG))
874       Results.push_back(Result);
875     return;
876   }
877   case ISD::SDIVREM: {
878     SDValue Op = SDValue(N, 1);
879     SDValue RES = LowerSDIVREM(Op, DAG);
880     Results.push_back(RES);
881     Results.push_back(RES.getValue(1));
882     break;
883   }
884   case ISD::UDIVREM: {
885     SDValue Op = SDValue(N, 0);
886     LowerUDIVREM64(Op, DAG, Results);
887     break;
888   }
889   }
890 }
891
892 SDValue R600TargetLowering::vectorToVerticalVector(SelectionDAG &DAG,
893                                                    SDValue Vector) const {
894
895   SDLoc DL(Vector);
896   EVT VecVT = Vector.getValueType();
897   EVT EltVT = VecVT.getVectorElementType();
898   SmallVector<SDValue, 8> Args;
899
900   for (unsigned i = 0, e = VecVT.getVectorNumElements();
901                                                            i != e; ++i) {
902     Args.push_back(DAG.getNode(
903         ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Vector,
904         DAG.getConstant(i, DL, getVectorIdxTy(DAG.getDataLayout()))));
905   }
906
907   return DAG.getNode(AMDGPUISD::BUILD_VERTICAL_VECTOR, DL, VecVT, Args);
908 }
909
910 SDValue R600TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
911                                                     SelectionDAG &DAG) const {
912
913   SDLoc DL(Op);
914   SDValue Vector = Op.getOperand(0);
915   SDValue Index = Op.getOperand(1);
916
917   if (isa<ConstantSDNode>(Index) ||
918       Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
919     return Op;
920
921   Vector = vectorToVerticalVector(DAG, Vector);
922   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getValueType(),
923                      Vector, Index);
924 }
925
926 SDValue R600TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
927                                                    SelectionDAG &DAG) const {
928   SDLoc DL(Op);
929   SDValue Vector = Op.getOperand(0);
930   SDValue Value = Op.getOperand(1);
931   SDValue Index = Op.getOperand(2);
932
933   if (isa<ConstantSDNode>(Index) ||
934       Vector.getOpcode() == AMDGPUISD::BUILD_VERTICAL_VECTOR)
935     return Op;
936
937   Vector = vectorToVerticalVector(DAG, Vector);
938   SDValue Insert = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, Op.getValueType(),
939                                Vector, Value, Index);
940   return vectorToVerticalVector(DAG, Insert);
941 }
942
943 SDValue R600TargetLowering::LowerTrig(SDValue Op, SelectionDAG &DAG) const {
944   // On hw >= R700, COS/SIN input must be between -1. and 1.
945   // Thus we lower them to TRIG ( FRACT ( x / 2Pi + 0.5) - 0.5)
946   EVT VT = Op.getValueType();
947   SDValue Arg = Op.getOperand(0);
948   SDLoc DL(Op);
949
950   // TODO: Should this propagate fast-math-flags?
951   SDValue FractPart = DAG.getNode(AMDGPUISD::FRACT, DL, VT,
952       DAG.getNode(ISD::FADD, DL, VT,
953         DAG.getNode(ISD::FMUL, DL, VT, Arg,
954           DAG.getConstantFP(0.15915494309, DL, MVT::f32)),
955         DAG.getConstantFP(0.5, DL, MVT::f32)));
956   unsigned TrigNode;
957   switch (Op.getOpcode()) {
958   case ISD::FCOS:
959     TrigNode = AMDGPUISD::COS_HW;
960     break;
961   case ISD::FSIN:
962     TrigNode = AMDGPUISD::SIN_HW;
963     break;
964   default:
965     llvm_unreachable("Wrong trig opcode");
966   }
967   SDValue TrigVal = DAG.getNode(TrigNode, DL, VT,
968       DAG.getNode(ISD::FADD, DL, VT, FractPart,
969         DAG.getConstantFP(-0.5, DL, MVT::f32)));
970   if (Gen >= AMDGPUSubtarget::R700)
971     return TrigVal;
972   // On R600 hw, COS/SIN input must be between -Pi and Pi.
973   return DAG.getNode(ISD::FMUL, DL, VT, TrigVal,
974       DAG.getConstantFP(3.14159265359, DL, MVT::f32));
975 }
976
977 SDValue R600TargetLowering::LowerSHLParts(SDValue Op, SelectionDAG &DAG) const {
978   SDLoc DL(Op);
979   EVT VT = Op.getValueType();
980
981   SDValue Lo = Op.getOperand(0);
982   SDValue Hi = Op.getOperand(1);
983   SDValue Shift = Op.getOperand(2);
984   SDValue Zero = DAG.getConstant(0, DL, VT);
985   SDValue One  = DAG.getConstant(1, DL, VT);
986
987   SDValue Width  = DAG.getConstant(VT.getSizeInBits(), DL, VT);
988   SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
989   SDValue BigShift  = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
990   SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
991
992   // The dance around Width1 is necessary for 0 special case.
993   // Without it the CompShift might be 32, producing incorrect results in
994   // Overflow. So we do the shift in two steps, the alternative is to
995   // add a conditional to filter the special case.
996
997   SDValue Overflow = DAG.getNode(ISD::SRL, DL, VT, Lo, CompShift);
998   Overflow = DAG.getNode(ISD::SRL, DL, VT, Overflow, One);
999
1000   SDValue HiSmall = DAG.getNode(ISD::SHL, DL, VT, Hi, Shift);
1001   HiSmall = DAG.getNode(ISD::OR, DL, VT, HiSmall, Overflow);
1002   SDValue LoSmall = DAG.getNode(ISD::SHL, DL, VT, Lo, Shift);
1003
1004   SDValue HiBig = DAG.getNode(ISD::SHL, DL, VT, Lo, BigShift);
1005   SDValue LoBig = Zero;
1006
1007   Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1008   Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1009
1010   return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1011 }
1012
1013 SDValue R600TargetLowering::LowerSRXParts(SDValue Op, SelectionDAG &DAG) const {
1014   SDLoc DL(Op);
1015   EVT VT = Op.getValueType();
1016
1017   SDValue Lo = Op.getOperand(0);
1018   SDValue Hi = Op.getOperand(1);
1019   SDValue Shift = Op.getOperand(2);
1020   SDValue Zero = DAG.getConstant(0, DL, VT);
1021   SDValue One  = DAG.getConstant(1, DL, VT);
1022
1023   const bool SRA = Op.getOpcode() == ISD::SRA_PARTS;
1024
1025   SDValue Width  = DAG.getConstant(VT.getSizeInBits(), DL, VT);
1026   SDValue Width1 = DAG.getConstant(VT.getSizeInBits() - 1, DL, VT);
1027   SDValue BigShift  = DAG.getNode(ISD::SUB, DL, VT, Shift, Width);
1028   SDValue CompShift = DAG.getNode(ISD::SUB, DL, VT, Width1, Shift);
1029
1030   // The dance around Width1 is necessary for 0 special case.
1031   // Without it the CompShift might be 32, producing incorrect results in
1032   // Overflow. So we do the shift in two steps, the alternative is to
1033   // add a conditional to filter the special case.
1034
1035   SDValue Overflow = DAG.getNode(ISD::SHL, DL, VT, Hi, CompShift);
1036   Overflow = DAG.getNode(ISD::SHL, DL, VT, Overflow, One);
1037
1038   SDValue HiSmall = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, Shift);
1039   SDValue LoSmall = DAG.getNode(ISD::SRL, DL, VT, Lo, Shift);
1040   LoSmall = DAG.getNode(ISD::OR, DL, VT, LoSmall, Overflow);
1041
1042   SDValue LoBig = DAG.getNode(SRA ? ISD::SRA : ISD::SRL, DL, VT, Hi, BigShift);
1043   SDValue HiBig = SRA ? DAG.getNode(ISD::SRA, DL, VT, Hi, Width1) : Zero;
1044
1045   Hi = DAG.getSelectCC(DL, Shift, Width, HiSmall, HiBig, ISD::SETULT);
1046   Lo = DAG.getSelectCC(DL, Shift, Width, LoSmall, LoBig, ISD::SETULT);
1047
1048   return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT,VT), Lo, Hi);
1049 }
1050
1051 SDValue R600TargetLowering::LowerUADDSUBO(SDValue Op, SelectionDAG &DAG,
1052                                           unsigned mainop, unsigned ovf) const {
1053   SDLoc DL(Op);
1054   EVT VT = Op.getValueType();
1055
1056   SDValue Lo = Op.getOperand(0);
1057   SDValue Hi = Op.getOperand(1);
1058
1059   SDValue OVF = DAG.getNode(ovf, DL, VT, Lo, Hi);
1060   // Extend sign.
1061   OVF = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, OVF,
1062                     DAG.getValueType(MVT::i1));
1063
1064   SDValue Res = DAG.getNode(mainop, DL, VT, Lo, Hi);
1065
1066   return DAG.getNode(ISD::MERGE_VALUES, DL, DAG.getVTList(VT, VT), Res, OVF);
1067 }
1068
1069 SDValue R600TargetLowering::LowerFPTOUINT(SDValue Op, SelectionDAG &DAG) const {
1070   SDLoc DL(Op);
1071   return DAG.getNode(
1072       ISD::SETCC,
1073       DL,
1074       MVT::i1,
1075       Op, DAG.getConstantFP(0.0f, DL, MVT::f32),
1076       DAG.getCondCode(ISD::SETNE)
1077       );
1078 }
1079
1080 SDValue R600TargetLowering::LowerImplicitParameter(SelectionDAG &DAG, EVT VT,
1081                                                    SDLoc DL,
1082                                                    unsigned DwordOffset) const {
1083   unsigned ByteOffset = DwordOffset * 4;
1084   PointerType * PtrType = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1085                                       AMDGPUAS::CONSTANT_BUFFER_0);
1086
1087   // We shouldn't be using an offset wider than 16-bits for implicit parameters.
1088   assert(isInt<16>(ByteOffset));
1089
1090   return DAG.getLoad(VT, DL, DAG.getEntryNode(),
1091                      DAG.getConstant(ByteOffset, DL, MVT::i32), // PTR
1092                      MachinePointerInfo(ConstantPointerNull::get(PtrType)),
1093                      false, false, false, 0);
1094 }
1095
1096 bool R600TargetLowering::isZero(SDValue Op) const {
1097   if(ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) {
1098     return Cst->isNullValue();
1099   } else if(ConstantFPSDNode *CstFP = dyn_cast<ConstantFPSDNode>(Op)){
1100     return CstFP->isZero();
1101   } else {
1102     return false;
1103   }
1104 }
1105
1106 SDValue R600TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
1107   SDLoc DL(Op);
1108   EVT VT = Op.getValueType();
1109
1110   SDValue LHS = Op.getOperand(0);
1111   SDValue RHS = Op.getOperand(1);
1112   SDValue True = Op.getOperand(2);
1113   SDValue False = Op.getOperand(3);
1114   SDValue CC = Op.getOperand(4);
1115   SDValue Temp;
1116
1117   if (VT == MVT::f32) {
1118     DAGCombinerInfo DCI(DAG, AfterLegalizeVectorOps, true, nullptr);
1119     SDValue MinMax = CombineFMinMaxLegacy(DL, VT, LHS, RHS, True, False, CC, DCI);
1120     if (MinMax)
1121       return MinMax;
1122   }
1123
1124   // LHS and RHS are guaranteed to be the same value type
1125   EVT CompareVT = LHS.getValueType();
1126
1127   // Check if we can lower this to a native operation.
1128
1129   // Try to lower to a SET* instruction:
1130   //
1131   // SET* can match the following patterns:
1132   //
1133   // select_cc f32, f32, -1,  0, cc_supported
1134   // select_cc f32, f32, 1.0f, 0.0f, cc_supported
1135   // select_cc i32, i32, -1,  0, cc_supported
1136   //
1137
1138   // Move hardware True/False values to the correct operand.
1139   ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1140   ISD::CondCode InverseCC =
1141      ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1142   if (isHWTrueValue(False) && isHWFalseValue(True)) {
1143     if (isCondCodeLegal(InverseCC, CompareVT.getSimpleVT())) {
1144       std::swap(False, True);
1145       CC = DAG.getCondCode(InverseCC);
1146     } else {
1147       ISD::CondCode SwapInvCC = ISD::getSetCCSwappedOperands(InverseCC);
1148       if (isCondCodeLegal(SwapInvCC, CompareVT.getSimpleVT())) {
1149         std::swap(False, True);
1150         std::swap(LHS, RHS);
1151         CC = DAG.getCondCode(SwapInvCC);
1152       }
1153     }
1154   }
1155
1156   if (isHWTrueValue(True) && isHWFalseValue(False) &&
1157       (CompareVT == VT || VT == MVT::i32)) {
1158     // This can be matched by a SET* instruction.
1159     return DAG.getNode(ISD::SELECT_CC, DL, VT, LHS, RHS, True, False, CC);
1160   }
1161
1162   // Try to lower to a CND* instruction:
1163   //
1164   // CND* can match the following patterns:
1165   //
1166   // select_cc f32, 0.0, f32, f32, cc_supported
1167   // select_cc f32, 0.0, i32, i32, cc_supported
1168   // select_cc i32, 0,   f32, f32, cc_supported
1169   // select_cc i32, 0,   i32, i32, cc_supported
1170   //
1171
1172   // Try to move the zero value to the RHS
1173   if (isZero(LHS)) {
1174     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1175     // Try swapping the operands
1176     ISD::CondCode CCSwapped = ISD::getSetCCSwappedOperands(CCOpcode);
1177     if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1178       std::swap(LHS, RHS);
1179       CC = DAG.getCondCode(CCSwapped);
1180     } else {
1181       // Try inverting the conditon and then swapping the operands
1182       ISD::CondCode CCInv = ISD::getSetCCInverse(CCOpcode, CompareVT.isInteger());
1183       CCSwapped = ISD::getSetCCSwappedOperands(CCInv);
1184       if (isCondCodeLegal(CCSwapped, CompareVT.getSimpleVT())) {
1185         std::swap(True, False);
1186         std::swap(LHS, RHS);
1187         CC = DAG.getCondCode(CCSwapped);
1188       }
1189     }
1190   }
1191   if (isZero(RHS)) {
1192     SDValue Cond = LHS;
1193     SDValue Zero = RHS;
1194     ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
1195     if (CompareVT != VT) {
1196       // Bitcast True / False to the correct types.  This will end up being
1197       // a nop, but it allows us to define only a single pattern in the
1198       // .TD files for each CND* instruction rather than having to have
1199       // one pattern for integer True/False and one for fp True/False
1200       True = DAG.getNode(ISD::BITCAST, DL, CompareVT, True);
1201       False = DAG.getNode(ISD::BITCAST, DL, CompareVT, False);
1202     }
1203
1204     switch (CCOpcode) {
1205     case ISD::SETONE:
1206     case ISD::SETUNE:
1207     case ISD::SETNE:
1208       CCOpcode = ISD::getSetCCInverse(CCOpcode, CompareVT == MVT::i32);
1209       Temp = True;
1210       True = False;
1211       False = Temp;
1212       break;
1213     default:
1214       break;
1215     }
1216     SDValue SelectNode = DAG.getNode(ISD::SELECT_CC, DL, CompareVT,
1217         Cond, Zero,
1218         True, False,
1219         DAG.getCondCode(CCOpcode));
1220     return DAG.getNode(ISD::BITCAST, DL, VT, SelectNode);
1221   }
1222
1223   // If we make it this for it means we have no native instructions to handle
1224   // this SELECT_CC, so we must lower it.
1225   SDValue HWTrue, HWFalse;
1226
1227   if (CompareVT == MVT::f32) {
1228     HWTrue = DAG.getConstantFP(1.0f, DL, CompareVT);
1229     HWFalse = DAG.getConstantFP(0.0f, DL, CompareVT);
1230   } else if (CompareVT == MVT::i32) {
1231     HWTrue = DAG.getConstant(-1, DL, CompareVT);
1232     HWFalse = DAG.getConstant(0, DL, CompareVT);
1233   }
1234   else {
1235     llvm_unreachable("Unhandled value type in LowerSELECT_CC");
1236   }
1237
1238   // Lower this unsupported SELECT_CC into a combination of two supported
1239   // SELECT_CC operations.
1240   SDValue Cond = DAG.getNode(ISD::SELECT_CC, DL, CompareVT, LHS, RHS, HWTrue, HWFalse, CC);
1241
1242   return DAG.getNode(ISD::SELECT_CC, DL, VT,
1243       Cond, HWFalse,
1244       True, False,
1245       DAG.getCondCode(ISD::SETNE));
1246 }
1247
1248 /// LLVM generates byte-addressed pointers.  For indirect addressing, we need to
1249 /// convert these pointers to a register index.  Each register holds
1250 /// 16 bytes, (4 x 32bit sub-register), but we need to take into account the
1251 /// \p StackWidth, which tells us how many of the 4 sub-registrers will be used
1252 /// for indirect addressing.
1253 SDValue R600TargetLowering::stackPtrToRegIndex(SDValue Ptr,
1254                                                unsigned StackWidth,
1255                                                SelectionDAG &DAG) const {
1256   unsigned SRLPad;
1257   switch(StackWidth) {
1258   case 1:
1259     SRLPad = 2;
1260     break;
1261   case 2:
1262     SRLPad = 3;
1263     break;
1264   case 4:
1265     SRLPad = 4;
1266     break;
1267   default: llvm_unreachable("Invalid stack width");
1268   }
1269
1270   SDLoc DL(Ptr);
1271   return DAG.getNode(ISD::SRL, DL, Ptr.getValueType(), Ptr,
1272                      DAG.getConstant(SRLPad, DL, MVT::i32));
1273 }
1274
1275 void R600TargetLowering::getStackAddress(unsigned StackWidth,
1276                                          unsigned ElemIdx,
1277                                          unsigned &Channel,
1278                                          unsigned &PtrIncr) const {
1279   switch (StackWidth) {
1280   default:
1281   case 1:
1282     Channel = 0;
1283     if (ElemIdx > 0) {
1284       PtrIncr = 1;
1285     } else {
1286       PtrIncr = 0;
1287     }
1288     break;
1289   case 2:
1290     Channel = ElemIdx % 2;
1291     if (ElemIdx == 2) {
1292       PtrIncr = 1;
1293     } else {
1294       PtrIncr = 0;
1295     }
1296     break;
1297   case 4:
1298     Channel = ElemIdx;
1299     PtrIncr = 0;
1300     break;
1301   }
1302 }
1303
1304 SDValue R600TargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
1305   SDLoc DL(Op);
1306   StoreSDNode *StoreNode = cast<StoreSDNode>(Op);
1307   SDValue Chain = Op.getOperand(0);
1308   SDValue Value = Op.getOperand(1);
1309   SDValue Ptr = Op.getOperand(2);
1310
1311   SDValue Result = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1312   if (Result.getNode()) {
1313     return Result;
1314   }
1315
1316   if (StoreNode->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS) {
1317     if (StoreNode->isTruncatingStore()) {
1318       EVT VT = Value.getValueType();
1319       assert(VT.bitsLE(MVT::i32));
1320       EVT MemVT = StoreNode->getMemoryVT();
1321       SDValue MaskConstant;
1322       if (MemVT == MVT::i8) {
1323         MaskConstant = DAG.getConstant(0xFF, DL, MVT::i32);
1324       } else {
1325         assert(MemVT == MVT::i16);
1326         MaskConstant = DAG.getConstant(0xFFFF, DL, MVT::i32);
1327       }
1328       SDValue DWordAddr = DAG.getNode(ISD::SRL, DL, VT, Ptr,
1329                                       DAG.getConstant(2, DL, MVT::i32));
1330       SDValue ByteIndex = DAG.getNode(ISD::AND, DL, Ptr.getValueType(), Ptr,
1331                                       DAG.getConstant(0x00000003, DL, VT));
1332       SDValue TruncValue = DAG.getNode(ISD::AND, DL, VT, Value, MaskConstant);
1333       SDValue Shift = DAG.getNode(ISD::SHL, DL, VT, ByteIndex,
1334                                    DAG.getConstant(3, DL, VT));
1335       SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, VT, TruncValue, Shift);
1336       SDValue Mask = DAG.getNode(ISD::SHL, DL, VT, MaskConstant, Shift);
1337       // XXX: If we add a 64-bit ZW register class, then we could use a 2 x i32
1338       // vector instead.
1339       SDValue Src[4] = {
1340         ShiftedValue,
1341         DAG.getConstant(0, DL, MVT::i32),
1342         DAG.getConstant(0, DL, MVT::i32),
1343         Mask
1344       };
1345       SDValue Input = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v4i32, Src);
1346       SDValue Args[3] = { Chain, Input, DWordAddr };
1347       return DAG.getMemIntrinsicNode(AMDGPUISD::STORE_MSKOR, DL,
1348                                      Op->getVTList(), Args, MemVT,
1349                                      StoreNode->getMemOperand());
1350     } else if (Ptr->getOpcode() != AMDGPUISD::DWORDADDR &&
1351                Value.getValueType().bitsGE(MVT::i32)) {
1352       // Convert pointer from byte address to dword address.
1353       Ptr = DAG.getNode(AMDGPUISD::DWORDADDR, DL, Ptr.getValueType(),
1354                         DAG.getNode(ISD::SRL, DL, Ptr.getValueType(),
1355                                     Ptr, DAG.getConstant(2, DL, MVT::i32)));
1356
1357       if (StoreNode->isTruncatingStore() || StoreNode->isIndexed()) {
1358         llvm_unreachable("Truncated and indexed stores not supported yet");
1359       } else {
1360         Chain = DAG.getStore(Chain, DL, Value, Ptr, StoreNode->getMemOperand());
1361       }
1362       return Chain;
1363     }
1364   }
1365
1366   EVT ValueVT = Value.getValueType();
1367
1368   if (StoreNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1369     return SDValue();
1370   }
1371
1372   SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
1373   if (Ret.getNode()) {
1374     return Ret;
1375   }
1376   // Lowering for indirect addressing
1377
1378   const MachineFunction &MF = DAG.getMachineFunction();
1379   const AMDGPUFrameLowering *TFL =
1380       static_cast<const AMDGPUFrameLowering *>(Subtarget->getFrameLowering());
1381   unsigned StackWidth = TFL->getStackWidth(MF);
1382
1383   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1384
1385   if (ValueVT.isVector()) {
1386     unsigned NumElemVT = ValueVT.getVectorNumElements();
1387     EVT ElemVT = ValueVT.getVectorElementType();
1388     SmallVector<SDValue, 4> Stores(NumElemVT);
1389
1390     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1391                                       "vector width in load");
1392
1393     for (unsigned i = 0; i < NumElemVT; ++i) {
1394       unsigned Channel, PtrIncr;
1395       getStackAddress(StackWidth, i, Channel, PtrIncr);
1396       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1397                         DAG.getConstant(PtrIncr, DL, MVT::i32));
1398       SDValue Elem = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT,
1399                                  Value, DAG.getConstant(i, DL, MVT::i32));
1400
1401       Stores[i] = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1402                               Chain, Elem, Ptr,
1403                               DAG.getTargetConstant(Channel, DL, MVT::i32));
1404     }
1405      Chain =  DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores);
1406    } else {
1407     if (ValueVT == MVT::i8) {
1408       Value = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Value);
1409     }
1410     Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other, Chain, Value, Ptr,
1411     DAG.getTargetConstant(0, DL, MVT::i32)); // Channel
1412   }
1413
1414   return Chain;
1415 }
1416
1417 // return (512 + (kc_bank << 12)
1418 static int
1419 ConstantAddressBlock(unsigned AddressSpace) {
1420   switch (AddressSpace) {
1421   case AMDGPUAS::CONSTANT_BUFFER_0:
1422     return 512;
1423   case AMDGPUAS::CONSTANT_BUFFER_1:
1424     return 512 + 4096;
1425   case AMDGPUAS::CONSTANT_BUFFER_2:
1426     return 512 + 4096 * 2;
1427   case AMDGPUAS::CONSTANT_BUFFER_3:
1428     return 512 + 4096 * 3;
1429   case AMDGPUAS::CONSTANT_BUFFER_4:
1430     return 512 + 4096 * 4;
1431   case AMDGPUAS::CONSTANT_BUFFER_5:
1432     return 512 + 4096 * 5;
1433   case AMDGPUAS::CONSTANT_BUFFER_6:
1434     return 512 + 4096 * 6;
1435   case AMDGPUAS::CONSTANT_BUFFER_7:
1436     return 512 + 4096 * 7;
1437   case AMDGPUAS::CONSTANT_BUFFER_8:
1438     return 512 + 4096 * 8;
1439   case AMDGPUAS::CONSTANT_BUFFER_9:
1440     return 512 + 4096 * 9;
1441   case AMDGPUAS::CONSTANT_BUFFER_10:
1442     return 512 + 4096 * 10;
1443   case AMDGPUAS::CONSTANT_BUFFER_11:
1444     return 512 + 4096 * 11;
1445   case AMDGPUAS::CONSTANT_BUFFER_12:
1446     return 512 + 4096 * 12;
1447   case AMDGPUAS::CONSTANT_BUFFER_13:
1448     return 512 + 4096 * 13;
1449   case AMDGPUAS::CONSTANT_BUFFER_14:
1450     return 512 + 4096 * 14;
1451   case AMDGPUAS::CONSTANT_BUFFER_15:
1452     return 512 + 4096 * 15;
1453   default:
1454     return -1;
1455   }
1456 }
1457
1458 SDValue R600TargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const
1459 {
1460   EVT VT = Op.getValueType();
1461   SDLoc DL(Op);
1462   LoadSDNode *LoadNode = cast<LoadSDNode>(Op);
1463   SDValue Chain = Op.getOperand(0);
1464   SDValue Ptr = Op.getOperand(1);
1465   SDValue LoweredLoad;
1466
1467   if (SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG))
1468     return Ret;
1469
1470   // Lower loads constant address space global variable loads
1471   if (LoadNode->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
1472       isa<GlobalVariable>(GetUnderlyingObject(
1473           LoadNode->getMemOperand()->getValue(), DAG.getDataLayout()))) {
1474
1475     SDValue Ptr = DAG.getZExtOrTrunc(
1476         LoadNode->getBasePtr(), DL,
1477         getPointerTy(DAG.getDataLayout(), AMDGPUAS::PRIVATE_ADDRESS));
1478     Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1479         DAG.getConstant(2, DL, MVT::i32));
1480     return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op->getVTList(),
1481                        LoadNode->getChain(), Ptr,
1482                        DAG.getTargetConstant(0, DL, MVT::i32),
1483                        Op.getOperand(2));
1484   }
1485
1486   if (LoadNode->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS && VT.isVector()) {
1487     SDValue MergedValues[2] = {
1488       ScalarizeVectorLoad(Op, DAG),
1489       Chain
1490     };
1491     return DAG.getMergeValues(MergedValues, DL);
1492   }
1493
1494   int ConstantBlock = ConstantAddressBlock(LoadNode->getAddressSpace());
1495   if (ConstantBlock > -1 &&
1496       ((LoadNode->getExtensionType() == ISD::NON_EXTLOAD) ||
1497        (LoadNode->getExtensionType() == ISD::ZEXTLOAD))) {
1498     SDValue Result;
1499     if (isa<ConstantExpr>(LoadNode->getMemOperand()->getValue()) ||
1500         isa<Constant>(LoadNode->getMemOperand()->getValue()) ||
1501         isa<ConstantSDNode>(Ptr)) {
1502       SDValue Slots[4];
1503       for (unsigned i = 0; i < 4; i++) {
1504         // We want Const position encoded with the following formula :
1505         // (((512 + (kc_bank << 12) + const_index) << 2) + chan)
1506         // const_index is Ptr computed by llvm using an alignment of 16.
1507         // Thus we add (((512 + (kc_bank << 12)) + chan ) * 4 here and
1508         // then div by 4 at the ISel step
1509         SDValue NewPtr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
1510             DAG.getConstant(4 * i + ConstantBlock * 16, DL, MVT::i32));
1511         Slots[i] = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::i32, NewPtr);
1512       }
1513       EVT NewVT = MVT::v4i32;
1514       unsigned NumElements = 4;
1515       if (VT.isVector()) {
1516         NewVT = VT;
1517         NumElements = VT.getVectorNumElements();
1518       }
1519       Result = DAG.getNode(ISD::BUILD_VECTOR, DL, NewVT,
1520                            makeArrayRef(Slots, NumElements));
1521     } else {
1522       // non-constant ptr can't be folded, keeps it as a v4f32 load
1523       Result = DAG.getNode(AMDGPUISD::CONST_ADDRESS, DL, MVT::v4i32,
1524           DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
1525                       DAG.getConstant(4, DL, MVT::i32)),
1526                       DAG.getConstant(LoadNode->getAddressSpace() -
1527                                       AMDGPUAS::CONSTANT_BUFFER_0, DL, MVT::i32)
1528           );
1529     }
1530
1531     if (!VT.isVector()) {
1532       Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, Result,
1533                            DAG.getConstant(0, DL, MVT::i32));
1534     }
1535
1536     SDValue MergedValues[2] = {
1537       Result,
1538       Chain
1539     };
1540     return DAG.getMergeValues(MergedValues, DL);
1541   }
1542
1543   // For most operations returning SDValue() will result in the node being
1544   // expanded by the DAG Legalizer. This is not the case for ISD::LOAD, so we
1545   // need to manually expand loads that may be legal in some address spaces and
1546   // illegal in others. SEXT loads from CONSTANT_BUFFER_0 are supported for
1547   // compute shaders, since the data is sign extended when it is uploaded to the
1548   // buffer. However SEXT loads from other address spaces are not supported, so
1549   // we need to expand them here.
1550   if (LoadNode->getExtensionType() == ISD::SEXTLOAD) {
1551     EVT MemVT = LoadNode->getMemoryVT();
1552     assert(!MemVT.isVector() && (MemVT == MVT::i16 || MemVT == MVT::i8));
1553     SDValue NewLoad = DAG.getExtLoad(ISD::EXTLOAD, DL, VT, Chain, Ptr,
1554                                   LoadNode->getPointerInfo(), MemVT,
1555                                   LoadNode->isVolatile(),
1556                                   LoadNode->isNonTemporal(),
1557                                   LoadNode->isInvariant(),
1558                                   LoadNode->getAlignment());
1559     SDValue Res = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, NewLoad,
1560                               DAG.getValueType(MemVT));
1561
1562     SDValue MergedValues[2] = { Res, Chain };
1563     return DAG.getMergeValues(MergedValues, DL);
1564   }
1565
1566   if (LoadNode->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
1567     return SDValue();
1568   }
1569
1570   // Lowering for indirect addressing
1571   const MachineFunction &MF = DAG.getMachineFunction();
1572   const AMDGPUFrameLowering *TFL =
1573       static_cast<const AMDGPUFrameLowering *>(Subtarget->getFrameLowering());
1574   unsigned StackWidth = TFL->getStackWidth(MF);
1575
1576   Ptr = stackPtrToRegIndex(Ptr, StackWidth, DAG);
1577
1578   if (VT.isVector()) {
1579     unsigned NumElemVT = VT.getVectorNumElements();
1580     EVT ElemVT = VT.getVectorElementType();
1581     SDValue Loads[4];
1582
1583     assert(NumElemVT >= StackWidth && "Stack width cannot be greater than "
1584                                       "vector width in load");
1585
1586     for (unsigned i = 0; i < NumElemVT; ++i) {
1587       unsigned Channel, PtrIncr;
1588       getStackAddress(StackWidth, i, Channel, PtrIncr);
1589       Ptr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
1590                         DAG.getConstant(PtrIncr, DL, MVT::i32));
1591       Loads[i] = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, ElemVT,
1592                              Chain, Ptr,
1593                              DAG.getTargetConstant(Channel, DL, MVT::i32),
1594                              Op.getOperand(2));
1595     }
1596     for (unsigned i = NumElemVT; i < 4; ++i) {
1597       Loads[i] = DAG.getUNDEF(ElemVT);
1598     }
1599     EVT TargetVT = EVT::getVectorVT(*DAG.getContext(), ElemVT, 4);
1600     LoweredLoad = DAG.getNode(ISD::BUILD_VECTOR, DL, TargetVT, Loads);
1601   } else {
1602     LoweredLoad = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, VT,
1603                               Chain, Ptr,
1604                               DAG.getTargetConstant(0, DL, MVT::i32), // Channel
1605                               Op.getOperand(2));
1606   }
1607
1608   SDValue Ops[2] = {
1609     LoweredLoad,
1610     Chain
1611   };
1612
1613   return DAG.getMergeValues(Ops, DL);
1614 }
1615
1616 SDValue R600TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1617   SDValue Chain = Op.getOperand(0);
1618   SDValue Cond  = Op.getOperand(1);
1619   SDValue Jump  = Op.getOperand(2);
1620
1621   return DAG.getNode(AMDGPUISD::BRANCH_COND, SDLoc(Op), Op.getValueType(),
1622                      Chain, Jump, Cond);
1623 }
1624
1625 /// XXX Only kernel functions are supported, so we can assume for now that
1626 /// every function is a kernel function, but in the future we should use
1627 /// separate calling conventions for kernel and non-kernel functions.
1628 SDValue R600TargetLowering::LowerFormalArguments(
1629                                       SDValue Chain,
1630                                       CallingConv::ID CallConv,
1631                                       bool isVarArg,
1632                                       const SmallVectorImpl<ISD::InputArg> &Ins,
1633                                       SDLoc DL, SelectionDAG &DAG,
1634                                       SmallVectorImpl<SDValue> &InVals) const {
1635   SmallVector<CCValAssign, 16> ArgLocs;
1636   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), ArgLocs,
1637                  *DAG.getContext());
1638   MachineFunction &MF = DAG.getMachineFunction();
1639   R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
1640
1641   SmallVector<ISD::InputArg, 8> LocalIns;
1642
1643   getOriginalFunctionArgs(DAG, MF.getFunction(), Ins, LocalIns);
1644
1645   AnalyzeFormalArguments(CCInfo, LocalIns);
1646
1647   for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1648     CCValAssign &VA = ArgLocs[i];
1649     const ISD::InputArg &In = Ins[i];
1650     EVT VT = In.VT;
1651     EVT MemVT = VA.getLocVT();
1652     if (!VT.isVector() && MemVT.isVector()) {
1653       // Get load source type if scalarized.
1654       MemVT = MemVT.getVectorElementType();
1655     }
1656
1657     if (MFI->getShaderType() != ShaderType::COMPUTE) {
1658       unsigned Reg = MF.addLiveIn(VA.getLocReg(), &AMDGPU::R600_Reg128RegClass);
1659       SDValue Register = DAG.getCopyFromReg(Chain, DL, Reg, VT);
1660       InVals.push_back(Register);
1661       continue;
1662     }
1663
1664     PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
1665                                           AMDGPUAS::CONSTANT_BUFFER_0);
1666
1667     // i64 isn't a legal type, so the register type used ends up as i32, which
1668     // isn't expected here. It attempts to create this sextload, but it ends up
1669     // being invalid. Somehow this seems to work with i64 arguments, but breaks
1670     // for <1 x i64>.
1671
1672     // The first 36 bytes of the input buffer contains information about
1673     // thread group and global sizes.
1674     ISD::LoadExtType Ext = ISD::NON_EXTLOAD;
1675     if (MemVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) {
1676       // FIXME: This should really check the extload type, but the handling of
1677       // extload vector parameters seems to be broken.
1678
1679       // Ext = In.Flags.isSExt() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
1680       Ext = ISD::SEXTLOAD;
1681     }
1682
1683     // Compute the offset from the value.
1684     // XXX - I think PartOffset should give you this, but it seems to give the
1685     // size of the register which isn't useful.
1686
1687     unsigned ValBase = ArgLocs[In.getOrigArgIndex()].getLocMemOffset();
1688     unsigned PartOffset = VA.getLocMemOffset();
1689     unsigned Offset = 36 + VA.getLocMemOffset();
1690
1691     MachinePointerInfo PtrInfo(UndefValue::get(PtrTy), PartOffset - ValBase);
1692     SDValue Arg = DAG.getLoad(ISD::UNINDEXED, Ext, VT, DL, Chain,
1693                               DAG.getConstant(Offset, DL, MVT::i32),
1694                               DAG.getUNDEF(MVT::i32),
1695                               PtrInfo,
1696                               MemVT, false, true, true, 4);
1697
1698     // 4 is the preferred alignment for the CONSTANT memory space.
1699     InVals.push_back(Arg);
1700     MFI->ABIArgOffset = Offset + MemVT.getStoreSize();
1701   }
1702   return Chain;
1703 }
1704
1705 EVT R600TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1706                                            EVT VT) const {
1707    if (!VT.isVector())
1708      return MVT::i32;
1709    return VT.changeVectorElementTypeToInteger();
1710 }
1711
1712 static SDValue CompactSwizzlableVector(
1713   SelectionDAG &DAG, SDValue VectorEntry,
1714   DenseMap<unsigned, unsigned> &RemapSwizzle) {
1715   assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1716   assert(RemapSwizzle.empty());
1717   SDValue NewBldVec[4] = {
1718     VectorEntry.getOperand(0),
1719     VectorEntry.getOperand(1),
1720     VectorEntry.getOperand(2),
1721     VectorEntry.getOperand(3)
1722   };
1723
1724   for (unsigned i = 0; i < 4; i++) {
1725     if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1726       // We mask write here to teach later passes that the ith element of this
1727       // vector is undef. Thus we can use it to reduce 128 bits reg usage,
1728       // break false dependencies and additionnaly make assembly easier to read.
1729       RemapSwizzle[i] = 7; // SEL_MASK_WRITE
1730     if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(NewBldVec[i])) {
1731       if (C->isZero()) {
1732         RemapSwizzle[i] = 4; // SEL_0
1733         NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1734       } else if (C->isExactlyValue(1.0)) {
1735         RemapSwizzle[i] = 5; // SEL_1
1736         NewBldVec[i] = DAG.getUNDEF(MVT::f32);
1737       }
1738     }
1739
1740     if (NewBldVec[i].getOpcode() == ISD::UNDEF)
1741       continue;
1742     for (unsigned j = 0; j < i; j++) {
1743       if (NewBldVec[i] == NewBldVec[j]) {
1744         NewBldVec[i] = DAG.getUNDEF(NewBldVec[i].getValueType());
1745         RemapSwizzle[i] = j;
1746         break;
1747       }
1748     }
1749   }
1750
1751   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1752                      VectorEntry.getValueType(), NewBldVec);
1753 }
1754
1755 static SDValue ReorganizeVector(SelectionDAG &DAG, SDValue VectorEntry,
1756                                 DenseMap<unsigned, unsigned> &RemapSwizzle) {
1757   assert(VectorEntry.getOpcode() == ISD::BUILD_VECTOR);
1758   assert(RemapSwizzle.empty());
1759   SDValue NewBldVec[4] = {
1760       VectorEntry.getOperand(0),
1761       VectorEntry.getOperand(1),
1762       VectorEntry.getOperand(2),
1763       VectorEntry.getOperand(3)
1764   };
1765   bool isUnmovable[4] = { false, false, false, false };
1766   for (unsigned i = 0; i < 4; i++) {
1767     RemapSwizzle[i] = i;
1768     if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1769       unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1770           ->getZExtValue();
1771       if (i == Idx)
1772         isUnmovable[Idx] = true;
1773     }
1774   }
1775
1776   for (unsigned i = 0; i < 4; i++) {
1777     if (NewBldVec[i].getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
1778       unsigned Idx = dyn_cast<ConstantSDNode>(NewBldVec[i].getOperand(1))
1779           ->getZExtValue();
1780       if (isUnmovable[Idx])
1781         continue;
1782       // Swap i and Idx
1783       std::swap(NewBldVec[Idx], NewBldVec[i]);
1784       std::swap(RemapSwizzle[i], RemapSwizzle[Idx]);
1785       break;
1786     }
1787   }
1788
1789   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(VectorEntry),
1790                      VectorEntry.getValueType(), NewBldVec);
1791 }
1792
1793
1794 SDValue R600TargetLowering::OptimizeSwizzle(SDValue BuildVector,
1795                                             SDValue Swz[4], SelectionDAG &DAG,
1796                                             SDLoc DL) const {
1797   assert(BuildVector.getOpcode() == ISD::BUILD_VECTOR);
1798   // Old -> New swizzle values
1799   DenseMap<unsigned, unsigned> SwizzleRemap;
1800
1801   BuildVector = CompactSwizzlableVector(DAG, BuildVector, SwizzleRemap);
1802   for (unsigned i = 0; i < 4; i++) {
1803     unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
1804     if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1805       Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
1806   }
1807
1808   SwizzleRemap.clear();
1809   BuildVector = ReorganizeVector(DAG, BuildVector, SwizzleRemap);
1810   for (unsigned i = 0; i < 4; i++) {
1811     unsigned Idx = cast<ConstantSDNode>(Swz[i])->getZExtValue();
1812     if (SwizzleRemap.find(Idx) != SwizzleRemap.end())
1813       Swz[i] = DAG.getConstant(SwizzleRemap[Idx], DL, MVT::i32);
1814   }
1815
1816   return BuildVector;
1817 }
1818
1819
1820 //===----------------------------------------------------------------------===//
1821 // Custom DAG Optimizations
1822 //===----------------------------------------------------------------------===//
1823
1824 SDValue R600TargetLowering::PerformDAGCombine(SDNode *N,
1825                                               DAGCombinerInfo &DCI) const {
1826   SelectionDAG &DAG = DCI.DAG;
1827
1828   switch (N->getOpcode()) {
1829   default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1830   // (f32 fp_round (f64 uint_to_fp a)) -> (f32 uint_to_fp a)
1831   case ISD::FP_ROUND: {
1832       SDValue Arg = N->getOperand(0);
1833       if (Arg.getOpcode() == ISD::UINT_TO_FP && Arg.getValueType() == MVT::f64) {
1834         return DAG.getNode(ISD::UINT_TO_FP, SDLoc(N), N->getValueType(0),
1835                            Arg.getOperand(0));
1836       }
1837       break;
1838     }
1839
1840   // (i32 fp_to_sint (fneg (select_cc f32, f32, 1.0, 0.0 cc))) ->
1841   // (i32 select_cc f32, f32, -1, 0 cc)
1842   //
1843   // Mesa's GLSL frontend generates the above pattern a lot and we can lower
1844   // this to one of the SET*_DX10 instructions.
1845   case ISD::FP_TO_SINT: {
1846     SDValue FNeg = N->getOperand(0);
1847     if (FNeg.getOpcode() != ISD::FNEG) {
1848       return SDValue();
1849     }
1850     SDValue SelectCC = FNeg.getOperand(0);
1851     if (SelectCC.getOpcode() != ISD::SELECT_CC ||
1852         SelectCC.getOperand(0).getValueType() != MVT::f32 || // LHS
1853         SelectCC.getOperand(2).getValueType() != MVT::f32 || // True
1854         !isHWTrueValue(SelectCC.getOperand(2)) ||
1855         !isHWFalseValue(SelectCC.getOperand(3))) {
1856       return SDValue();
1857     }
1858
1859     SDLoc dl(N);
1860     return DAG.getNode(ISD::SELECT_CC, dl, N->getValueType(0),
1861                            SelectCC.getOperand(0), // LHS
1862                            SelectCC.getOperand(1), // RHS
1863                            DAG.getConstant(-1, dl, MVT::i32), // True
1864                            DAG.getConstant(0, dl, MVT::i32),  // False
1865                            SelectCC.getOperand(4)); // CC
1866
1867     break;
1868   }
1869
1870   // insert_vector_elt (build_vector elt0, ... , eltN), NewEltIdx, idx
1871   // => build_vector elt0, ... , NewEltIdx, ... , eltN
1872   case ISD::INSERT_VECTOR_ELT: {
1873     SDValue InVec = N->getOperand(0);
1874     SDValue InVal = N->getOperand(1);
1875     SDValue EltNo = N->getOperand(2);
1876     SDLoc dl(N);
1877
1878     // If the inserted element is an UNDEF, just use the input vector.
1879     if (InVal.getOpcode() == ISD::UNDEF)
1880       return InVec;
1881
1882     EVT VT = InVec.getValueType();
1883
1884     // If we can't generate a legal BUILD_VECTOR, exit
1885     if (!isOperationLegal(ISD::BUILD_VECTOR, VT))
1886       return SDValue();
1887
1888     // Check that we know which element is being inserted
1889     if (!isa<ConstantSDNode>(EltNo))
1890       return SDValue();
1891     unsigned Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
1892
1893     // Check that the operand is a BUILD_VECTOR (or UNDEF, which can essentially
1894     // be converted to a BUILD_VECTOR).  Fill in the Ops vector with the
1895     // vector elements.
1896     SmallVector<SDValue, 8> Ops;
1897     if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
1898       Ops.append(InVec.getNode()->op_begin(),
1899                  InVec.getNode()->op_end());
1900     } else if (InVec.getOpcode() == ISD::UNDEF) {
1901       unsigned NElts = VT.getVectorNumElements();
1902       Ops.append(NElts, DAG.getUNDEF(InVal.getValueType()));
1903     } else {
1904       return SDValue();
1905     }
1906
1907     // Insert the element
1908     if (Elt < Ops.size()) {
1909       // All the operands of BUILD_VECTOR must have the same type;
1910       // we enforce that here.
1911       EVT OpVT = Ops[0].getValueType();
1912       if (InVal.getValueType() != OpVT)
1913         InVal = OpVT.bitsGT(InVal.getValueType()) ?
1914           DAG.getNode(ISD::ANY_EXTEND, dl, OpVT, InVal) :
1915           DAG.getNode(ISD::TRUNCATE, dl, OpVT, InVal);
1916       Ops[Elt] = InVal;
1917     }
1918
1919     // Return the new vector
1920     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
1921   }
1922
1923   // Extract_vec (Build_vector) generated by custom lowering
1924   // also needs to be customly combined
1925   case ISD::EXTRACT_VECTOR_ELT: {
1926     SDValue Arg = N->getOperand(0);
1927     if (Arg.getOpcode() == ISD::BUILD_VECTOR) {
1928       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1929         unsigned Element = Const->getZExtValue();
1930         return Arg->getOperand(Element);
1931       }
1932     }
1933     if (Arg.getOpcode() == ISD::BITCAST &&
1934         Arg.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
1935       if (ConstantSDNode *Const = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
1936         unsigned Element = Const->getZExtValue();
1937         return DAG.getNode(ISD::BITCAST, SDLoc(N), N->getVTList(),
1938             Arg->getOperand(0).getOperand(Element));
1939       }
1940     }
1941     break;
1942   }
1943
1944   case ISD::SELECT_CC: {
1945     // Try common optimizations
1946     SDValue Ret = AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1947     if (Ret.getNode())
1948       return Ret;
1949
1950     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, seteq ->
1951     //      selectcc x, y, a, b, inv(cc)
1952     //
1953     // fold selectcc (selectcc x, y, a, b, cc), b, a, b, setne ->
1954     //      selectcc x, y, a, b, cc
1955     SDValue LHS = N->getOperand(0);
1956     if (LHS.getOpcode() != ISD::SELECT_CC) {
1957       return SDValue();
1958     }
1959
1960     SDValue RHS = N->getOperand(1);
1961     SDValue True = N->getOperand(2);
1962     SDValue False = N->getOperand(3);
1963     ISD::CondCode NCC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1964
1965     if (LHS.getOperand(2).getNode() != True.getNode() ||
1966         LHS.getOperand(3).getNode() != False.getNode() ||
1967         RHS.getNode() != False.getNode()) {
1968       return SDValue();
1969     }
1970
1971     switch (NCC) {
1972     default: return SDValue();
1973     case ISD::SETNE: return LHS;
1974     case ISD::SETEQ: {
1975       ISD::CondCode LHSCC = cast<CondCodeSDNode>(LHS.getOperand(4))->get();
1976       LHSCC = ISD::getSetCCInverse(LHSCC,
1977                                   LHS.getOperand(0).getValueType().isInteger());
1978       if (DCI.isBeforeLegalizeOps() ||
1979           isCondCodeLegal(LHSCC, LHS.getOperand(0).getSimpleValueType()))
1980         return DAG.getSelectCC(SDLoc(N),
1981                                LHS.getOperand(0),
1982                                LHS.getOperand(1),
1983                                LHS.getOperand(2),
1984                                LHS.getOperand(3),
1985                                LHSCC);
1986       break;
1987     }
1988     }
1989     return SDValue();
1990   }
1991
1992   case AMDGPUISD::EXPORT: {
1993     SDValue Arg = N->getOperand(1);
1994     if (Arg.getOpcode() != ISD::BUILD_VECTOR)
1995       break;
1996
1997     SDValue NewArgs[8] = {
1998       N->getOperand(0), // Chain
1999       SDValue(),
2000       N->getOperand(2), // ArrayBase
2001       N->getOperand(3), // Type
2002       N->getOperand(4), // SWZ_X
2003       N->getOperand(5), // SWZ_Y
2004       N->getOperand(6), // SWZ_Z
2005       N->getOperand(7) // SWZ_W
2006     };
2007     SDLoc DL(N);
2008     NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[4], DAG, DL);
2009     return DAG.getNode(AMDGPUISD::EXPORT, DL, N->getVTList(), NewArgs);
2010   }
2011   case AMDGPUISD::TEXTURE_FETCH: {
2012     SDValue Arg = N->getOperand(1);
2013     if (Arg.getOpcode() != ISD::BUILD_VECTOR)
2014       break;
2015
2016     SDValue NewArgs[19] = {
2017       N->getOperand(0),
2018       N->getOperand(1),
2019       N->getOperand(2),
2020       N->getOperand(3),
2021       N->getOperand(4),
2022       N->getOperand(5),
2023       N->getOperand(6),
2024       N->getOperand(7),
2025       N->getOperand(8),
2026       N->getOperand(9),
2027       N->getOperand(10),
2028       N->getOperand(11),
2029       N->getOperand(12),
2030       N->getOperand(13),
2031       N->getOperand(14),
2032       N->getOperand(15),
2033       N->getOperand(16),
2034       N->getOperand(17),
2035       N->getOperand(18),
2036     };
2037     SDLoc DL(N);
2038     NewArgs[1] = OptimizeSwizzle(N->getOperand(1), &NewArgs[2], DAG, DL);
2039     return DAG.getNode(AMDGPUISD::TEXTURE_FETCH, DL, N->getVTList(), NewArgs);
2040   }
2041   }
2042
2043   return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
2044 }
2045
2046 static bool
2047 FoldOperand(SDNode *ParentNode, unsigned SrcIdx, SDValue &Src, SDValue &Neg,
2048             SDValue &Abs, SDValue &Sel, SDValue &Imm, SelectionDAG &DAG) {
2049   const R600InstrInfo *TII =
2050       static_cast<const R600InstrInfo *>(DAG.getSubtarget().getInstrInfo());
2051   if (!Src.isMachineOpcode())
2052     return false;
2053   switch (Src.getMachineOpcode()) {
2054   case AMDGPU::FNEG_R600:
2055     if (!Neg.getNode())
2056       return false;
2057     Src = Src.getOperand(0);
2058     Neg = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
2059     return true;
2060   case AMDGPU::FABS_R600:
2061     if (!Abs.getNode())
2062       return false;
2063     Src = Src.getOperand(0);
2064     Abs = DAG.getTargetConstant(1, SDLoc(ParentNode), MVT::i32);
2065     return true;
2066   case AMDGPU::CONST_COPY: {
2067     unsigned Opcode = ParentNode->getMachineOpcode();
2068     bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2069
2070     if (!Sel.getNode())
2071       return false;
2072
2073     SDValue CstOffset = Src.getOperand(0);
2074     if (ParentNode->getValueType(0).isVector())
2075       return false;
2076
2077     // Gather constants values
2078     int SrcIndices[] = {
2079       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2080       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2081       TII->getOperandIdx(Opcode, AMDGPU::OpName::src2),
2082       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2083       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2084       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2085       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2086       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2087       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2088       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2089       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2090     };
2091     std::vector<unsigned> Consts;
2092     for (int OtherSrcIdx : SrcIndices) {
2093       int OtherSelIdx = TII->getSelIdx(Opcode, OtherSrcIdx);
2094       if (OtherSrcIdx < 0 || OtherSelIdx < 0)
2095         continue;
2096       if (HasDst) {
2097         OtherSrcIdx--;
2098         OtherSelIdx--;
2099       }
2100       if (RegisterSDNode *Reg =
2101           dyn_cast<RegisterSDNode>(ParentNode->getOperand(OtherSrcIdx))) {
2102         if (Reg->getReg() == AMDGPU::ALU_CONST) {
2103           ConstantSDNode *Cst
2104             = cast<ConstantSDNode>(ParentNode->getOperand(OtherSelIdx));
2105           Consts.push_back(Cst->getZExtValue());
2106         }
2107       }
2108     }
2109
2110     ConstantSDNode *Cst = cast<ConstantSDNode>(CstOffset);
2111     Consts.push_back(Cst->getZExtValue());
2112     if (!TII->fitsConstReadLimitations(Consts)) {
2113       return false;
2114     }
2115
2116     Sel = CstOffset;
2117     Src = DAG.getRegister(AMDGPU::ALU_CONST, MVT::f32);
2118     return true;
2119   }
2120   case AMDGPU::MOV_IMM_I32:
2121   case AMDGPU::MOV_IMM_F32: {
2122     unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
2123     uint64_t ImmValue = 0;
2124
2125
2126     if (Src.getMachineOpcode() == AMDGPU::MOV_IMM_F32) {
2127       ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Src.getOperand(0));
2128       float FloatValue = FPC->getValueAPF().convertToFloat();
2129       if (FloatValue == 0.0) {
2130         ImmReg = AMDGPU::ZERO;
2131       } else if (FloatValue == 0.5) {
2132         ImmReg = AMDGPU::HALF;
2133       } else if (FloatValue == 1.0) {
2134         ImmReg = AMDGPU::ONE;
2135       } else {
2136         ImmValue = FPC->getValueAPF().bitcastToAPInt().getZExtValue();
2137       }
2138     } else {
2139       ConstantSDNode *C = dyn_cast<ConstantSDNode>(Src.getOperand(0));
2140       uint64_t Value = C->getZExtValue();
2141       if (Value == 0) {
2142         ImmReg = AMDGPU::ZERO;
2143       } else if (Value == 1) {
2144         ImmReg = AMDGPU::ONE_INT;
2145       } else {
2146         ImmValue = Value;
2147       }
2148     }
2149
2150     // Check that we aren't already using an immediate.
2151     // XXX: It's possible for an instruction to have more than one
2152     // immediate operand, but this is not supported yet.
2153     if (ImmReg == AMDGPU::ALU_LITERAL_X) {
2154       if (!Imm.getNode())
2155         return false;
2156       ConstantSDNode *C = dyn_cast<ConstantSDNode>(Imm);
2157       assert(C);
2158       if (C->getZExtValue())
2159         return false;
2160       Imm = DAG.getTargetConstant(ImmValue, SDLoc(ParentNode), MVT::i32);
2161     }
2162     Src = DAG.getRegister(ImmReg, MVT::i32);
2163     return true;
2164   }
2165   default:
2166     return false;
2167   }
2168 }
2169
2170
2171 /// \brief Fold the instructions after selecting them
2172 SDNode *R600TargetLowering::PostISelFolding(MachineSDNode *Node,
2173                                             SelectionDAG &DAG) const {
2174   const R600InstrInfo *TII =
2175       static_cast<const R600InstrInfo *>(DAG.getSubtarget().getInstrInfo());
2176   if (!Node->isMachineOpcode())
2177     return Node;
2178   unsigned Opcode = Node->getMachineOpcode();
2179   SDValue FakeOp;
2180
2181   std::vector<SDValue> Ops(Node->op_begin(), Node->op_end());
2182
2183   if (Opcode == AMDGPU::DOT_4) {
2184     int OperandIdx[] = {
2185       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
2186       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
2187       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
2188       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
2189       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
2190       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
2191       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
2192       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
2193         };
2194     int NegIdx[] = {
2195       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
2196       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
2197       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
2198       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
2199       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
2200       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
2201       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
2202       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
2203     };
2204     int AbsIdx[] = {
2205       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
2206       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
2207       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
2208       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
2209       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
2210       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
2211       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
2212       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
2213     };
2214     for (unsigned i = 0; i < 8; i++) {
2215       if (OperandIdx[i] < 0)
2216         return Node;
2217       SDValue &Src = Ops[OperandIdx[i] - 1];
2218       SDValue &Neg = Ops[NegIdx[i] - 1];
2219       SDValue &Abs = Ops[AbsIdx[i] - 1];
2220       bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2221       int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2222       if (HasDst)
2223         SelIdx--;
2224       SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2225       if (FoldOperand(Node, i, Src, Neg, Abs, Sel, FakeOp, DAG))
2226         return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2227     }
2228   } else if (Opcode == AMDGPU::REG_SEQUENCE) {
2229     for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2) {
2230       SDValue &Src = Ops[i];
2231       if (FoldOperand(Node, i, Src, FakeOp, FakeOp, FakeOp, FakeOp, DAG))
2232         return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2233     }
2234   } else if (Opcode == AMDGPU::CLAMP_R600) {
2235     SDValue Src = Node->getOperand(0);
2236     if (!Src.isMachineOpcode() ||
2237         !TII->hasInstrModifiers(Src.getMachineOpcode()))
2238       return Node;
2239     int ClampIdx = TII->getOperandIdx(Src.getMachineOpcode(),
2240         AMDGPU::OpName::clamp);
2241     if (ClampIdx < 0)
2242       return Node;
2243     SDLoc DL(Node);
2244     std::vector<SDValue> Ops(Src->op_begin(), Src->op_end());
2245     Ops[ClampIdx - 1] = DAG.getTargetConstant(1, DL, MVT::i32);
2246     return DAG.getMachineNode(Src.getMachineOpcode(), DL,
2247                               Node->getVTList(), Ops);
2248   } else {
2249     if (!TII->hasInstrModifiers(Opcode))
2250       return Node;
2251     int OperandIdx[] = {
2252       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
2253       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
2254       TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
2255     };
2256     int NegIdx[] = {
2257       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
2258       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
2259       TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
2260     };
2261     int AbsIdx[] = {
2262       TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
2263       TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
2264       -1
2265     };
2266     for (unsigned i = 0; i < 3; i++) {
2267       if (OperandIdx[i] < 0)
2268         return Node;
2269       SDValue &Src = Ops[OperandIdx[i] - 1];
2270       SDValue &Neg = Ops[NegIdx[i] - 1];
2271       SDValue FakeAbs;
2272       SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
2273       bool HasDst = TII->getOperandIdx(Opcode, AMDGPU::OpName::dst) > -1;
2274       int SelIdx = TII->getSelIdx(Opcode, OperandIdx[i]);
2275       int ImmIdx = TII->getOperandIdx(Opcode, AMDGPU::OpName::literal);
2276       if (HasDst) {
2277         SelIdx--;
2278         ImmIdx--;
2279       }
2280       SDValue &Sel = (SelIdx > -1) ? Ops[SelIdx] : FakeOp;
2281       SDValue &Imm = Ops[ImmIdx];
2282       if (FoldOperand(Node, i, Src, Neg, Abs, Sel, Imm, DAG))
2283         return DAG.getMachineNode(Opcode, SDLoc(Node), Node->getVTList(), Ops);
2284     }
2285   }
2286
2287   return Node;
2288 }