[PowerPC] Fix PR16556 (handle undef ppcf128 in LowerFP_TO_INT).
[oota-llvm.git] / lib / Target / R600 / AMDILISelDAGToDAG.cpp
1 //===-- AMDILISelDAGToDAG.cpp - A dag to dag inst selector for AMDIL ------===//
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 Defines an instruction selector for the AMDGPU target.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "AMDGPUInstrInfo.h"
15 #include "AMDGPUISelLowering.h" // For AMDGPUISD
16 #include "AMDGPURegisterInfo.h"
17 #include "R600InstrInfo.h"
18 #include "SIISelLowering.h"
19 #include "llvm/ADT/ValueMap.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/PseudoSourceValue.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/Support/Compiler.h"
26 #include <list>
27 #include <queue>
28
29 using namespace llvm;
30
31 //===----------------------------------------------------------------------===//
32 // Instruction Selector Implementation
33 //===----------------------------------------------------------------------===//
34
35 namespace {
36 /// AMDGPU specific code to select AMDGPU machine instructions for
37 /// SelectionDAG operations.
38 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39   // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40   // make the right decision when generating code for different targets.
41   const AMDGPUSubtarget &Subtarget;
42 public:
43   AMDGPUDAGToDAGISel(TargetMachine &TM);
44   virtual ~AMDGPUDAGToDAGISel();
45
46   SDNode *Select(SDNode *N);
47   virtual const char *getPassName() const;
48   virtual void PostprocessISelDAG();
49
50 private:
51   inline SDValue getSmallIPtrImm(unsigned Imm);
52   bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
53                    const R600InstrInfo *TII, std::vector<unsigned> Cst);
54   bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
55   bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
56
57   // Complex pattern selectors
58   bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
59   bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
60   bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
61
62   static bool checkType(const Value *ptr, unsigned int addrspace);
63
64   static bool isGlobalStore(const StoreSDNode *N);
65   static bool isPrivateStore(const StoreSDNode *N);
66   static bool isLocalStore(const StoreSDNode *N);
67   static bool isRegionStore(const StoreSDNode *N);
68
69   bool isCPLoad(const LoadSDNode *N) const;
70   bool isConstantLoad(const LoadSDNode *N, int cbID) const;
71   bool isGlobalLoad(const LoadSDNode *N) const;
72   bool isParamLoad(const LoadSDNode *N) const;
73   bool isPrivateLoad(const LoadSDNode *N) const;
74   bool isLocalLoad(const LoadSDNode *N) const;
75   bool isRegionLoad(const LoadSDNode *N) const;
76
77   bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
78   bool SelectGlobalValueVariableOffset(SDValue Addr,
79       SDValue &BaseReg, SDValue& Offset);
80   bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
81   bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
82
83   // Include the pieces autogenerated from the target description.
84 #include "AMDGPUGenDAGISel.inc"
85 };
86 }  // end anonymous namespace
87
88 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
89 // DAG, ready for instruction scheduling.
90 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
91                                        ) {
92   return new AMDGPUDAGToDAGISel(TM);
93 }
94
95 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
96   : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
97 }
98
99 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
100 }
101
102 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
103   return CurDAG->getTargetConstant(Imm, MVT::i32);
104 }
105
106 bool AMDGPUDAGToDAGISel::SelectADDRParam(
107     SDValue Addr, SDValue& R1, SDValue& R2) {
108
109   if (Addr.getOpcode() == ISD::FrameIndex) {
110     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
111       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
112       R2 = CurDAG->getTargetConstant(0, MVT::i32);
113     } else {
114       R1 = Addr;
115       R2 = CurDAG->getTargetConstant(0, MVT::i32);
116     }
117   } else if (Addr.getOpcode() == ISD::ADD) {
118     R1 = Addr.getOperand(0);
119     R2 = Addr.getOperand(1);
120   } else {
121     R1 = Addr;
122     R2 = CurDAG->getTargetConstant(0, MVT::i32);
123   }
124   return true;
125 }
126
127 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
128   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
129       Addr.getOpcode() == ISD::TargetGlobalAddress) {
130     return false;
131   }
132   return SelectADDRParam(Addr, R1, R2);
133 }
134
135
136 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
137   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
138       Addr.getOpcode() == ISD::TargetGlobalAddress) {
139     return false;
140   }
141
142   if (Addr.getOpcode() == ISD::FrameIndex) {
143     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
144       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
145       R2 = CurDAG->getTargetConstant(0, MVT::i64);
146     } else {
147       R1 = Addr;
148       R2 = CurDAG->getTargetConstant(0, MVT::i64);
149     }
150   } else if (Addr.getOpcode() == ISD::ADD) {
151     R1 = Addr.getOperand(0);
152     R2 = Addr.getOperand(1);
153   } else {
154     R1 = Addr;
155     R2 = CurDAG->getTargetConstant(0, MVT::i64);
156   }
157   return true;
158 }
159
160 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
161   unsigned int Opc = N->getOpcode();
162   if (N->isMachineOpcode()) {
163     return NULL;   // Already selected.
164   }
165   switch (Opc) {
166   default: break;
167   case ISD::BUILD_VECTOR: {
168     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
169     if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
170       break;
171     }
172     // BUILD_VECTOR is usually lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
173     // that adds a 128 bits reg copy when going through TwoAddressInstructions
174     // pass. We want to avoid 128 bits copies as much as possible because they
175     // can't be bundled by our scheduler.
176     SDValue RegSeqArgs[9] = {
177       CurDAG->getTargetConstant(AMDGPU::R600_Reg128RegClassID, MVT::i32),
178       SDValue(), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
179       SDValue(), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32),
180       SDValue(), CurDAG->getTargetConstant(AMDGPU::sub2, MVT::i32),
181       SDValue(), CurDAG->getTargetConstant(AMDGPU::sub3, MVT::i32)
182     };
183     bool IsRegSeq = true;
184     for (unsigned i = 0; i < N->getNumOperands(); i++) {
185       if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
186         IsRegSeq = false;
187         break;
188       }
189       RegSeqArgs[2 * i + 1] = N->getOperand(i);
190     }
191     if (!IsRegSeq)
192       break;
193     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
194         RegSeqArgs, 2 * N->getNumOperands() + 1);
195   }
196   case ISD::BUILD_PAIR: {
197     SDValue RC, SubReg0, SubReg1;
198     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
199     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
200       break;
201     }
202     if (N->getValueType(0) == MVT::i128) {
203       RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
204       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
205       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
206     } else if (N->getValueType(0) == MVT::i64) {
207       RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
208       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
209       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
210     } else {
211       llvm_unreachable("Unhandled value type for BUILD_PAIR");
212     }
213     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
214                             N->getOperand(1), SubReg1 };
215     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
216                                   SDLoc(N), N->getValueType(0), Ops);
217   }
218
219   case ISD::ConstantFP:
220   case ISD::Constant: {
221     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
222     // XXX: Custom immediate lowering not implemented yet.  Instead we use
223     // pseudo instructions defined in SIInstructions.td
224     if (ST.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
225       break;
226     }
227     const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
228
229     uint64_t ImmValue = 0;
230     unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
231
232     if (N->getOpcode() == ISD::ConstantFP) {
233       // XXX: 64-bit Immediates not supported yet
234       assert(N->getValueType(0) != MVT::f64);
235
236       ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
237       APFloat Value = C->getValueAPF();
238       float FloatValue = Value.convertToFloat();
239       if (FloatValue == 0.0) {
240         ImmReg = AMDGPU::ZERO;
241       } else if (FloatValue == 0.5) {
242         ImmReg = AMDGPU::HALF;
243       } else if (FloatValue == 1.0) {
244         ImmReg = AMDGPU::ONE;
245       } else {
246         ImmValue = Value.bitcastToAPInt().getZExtValue();
247       }
248     } else {
249       // XXX: 64-bit Immediates not supported yet
250       assert(N->getValueType(0) != MVT::i64);
251
252       ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
253       if (C->getZExtValue() == 0) {
254         ImmReg = AMDGPU::ZERO;
255       } else if (C->getZExtValue() == 1) {
256         ImmReg = AMDGPU::ONE_INT;
257       } else {
258         ImmValue = C->getZExtValue();
259       }
260     }
261
262     for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
263                               Use != SDNode::use_end(); Use = Next) {
264       Next = llvm::next(Use);
265       std::vector<SDValue> Ops;
266       for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
267         Ops.push_back(Use->getOperand(i));
268       }
269
270       if (!Use->isMachineOpcode()) {
271           if (ImmReg == AMDGPU::ALU_LITERAL_X) {
272             // We can only use literal constants (e.g. AMDGPU::ZERO,
273             // AMDGPU::ONE, etc) in machine opcodes.
274             continue;
275           }
276       } else {
277         if (!TII->isALUInstr(Use->getMachineOpcode()) ||
278             (TII->get(Use->getMachineOpcode()).TSFlags &
279             R600_InstFlag::VECTOR)) {
280           continue;
281         }
282
283         int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(),
284                                         AMDGPU::OpName::literal);
285         if (ImmIdx == -1) {
286           continue;
287         }
288
289         if (TII->getOperandIdx(Use->getMachineOpcode(),
290                                AMDGPU::OpName::dst) != -1) {
291           // subtract one from ImmIdx, because the DST operand is usually index
292           // 0 for MachineInstrs, but we have no DST in the Ops vector.
293           ImmIdx--;
294         }
295
296         // Check that we aren't already using an immediate.
297         // XXX: It's possible for an instruction to have more than one
298         // immediate operand, but this is not supported yet.
299         if (ImmReg == AMDGPU::ALU_LITERAL_X) {
300           ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
301           assert(C);
302
303           if (C->getZExtValue() != 0) {
304             // This instruction is already using an immediate.
305             continue;
306           }
307
308           // Set the immediate value
309           Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
310         }
311       }
312       // Set the immediate register
313       Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
314
315       CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
316     }
317     break;
318   }
319   }
320   SDNode *Result = SelectCode(N);
321
322   // Fold operands of selected node
323
324   const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
325   if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
326     const R600InstrInfo *TII =
327         static_cast<const R600InstrInfo*>(TM.getInstrInfo());
328     if (Result && Result->isMachineOpcode() && Result->getMachineOpcode() == AMDGPU::DOT_4) {
329       bool IsModified = false;
330       do {
331         std::vector<SDValue> Ops;
332         for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
333             I != E; ++I)
334           Ops.push_back(*I);
335         IsModified = FoldDotOperands(Result->getMachineOpcode(), TII, Ops);
336         if (IsModified) {
337           Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
338         }
339       } while (IsModified);
340
341     }
342     if (Result && Result->isMachineOpcode() &&
343         !(TII->get(Result->getMachineOpcode()).TSFlags & R600_InstFlag::VECTOR)
344         && TII->hasInstrModifiers(Result->getMachineOpcode())) {
345       // Fold FNEG/FABS/CONST_ADDRESS
346       // TODO: Isel can generate multiple MachineInst, we need to recursively
347       // parse Result
348       bool IsModified = false;
349       do {
350         std::vector<SDValue> Ops;
351         for(SDNode::op_iterator I = Result->op_begin(), E = Result->op_end();
352             I != E; ++I)
353           Ops.push_back(*I);
354         IsModified = FoldOperands(Result->getMachineOpcode(), TII, Ops);
355         if (IsModified) {
356           Result = CurDAG->UpdateNodeOperands(Result, Ops.data(), Ops.size());
357         }
358       } while (IsModified);
359
360       // If node has a single use which is CLAMP_R600, folds it
361       if (Result->hasOneUse() && Result->isMachineOpcode()) {
362         SDNode *PotentialClamp = *Result->use_begin();
363         if (PotentialClamp->isMachineOpcode() &&
364             PotentialClamp->getMachineOpcode() == AMDGPU::CLAMP_R600) {
365           unsigned ClampIdx =
366             TII->getOperandIdx(Result->getMachineOpcode(), AMDGPU::OpName::clamp);
367           std::vector<SDValue> Ops;
368           unsigned NumOp = Result->getNumOperands();
369           for (unsigned i = 0; i < NumOp; ++i) {
370             Ops.push_back(Result->getOperand(i));
371           }
372           Ops[ClampIdx - 1] = CurDAG->getTargetConstant(1, MVT::i32);
373           Result = CurDAG->SelectNodeTo(PotentialClamp,
374               Result->getMachineOpcode(), PotentialClamp->getVTList(),
375               Ops.data(), NumOp);
376         }
377       }
378     }
379   }
380
381   return Result;
382 }
383
384 bool AMDGPUDAGToDAGISel::FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg,
385                                      SDValue &Abs, const R600InstrInfo *TII,
386                                      std::vector<unsigned> Consts) {
387   switch (Src.getOpcode()) {
388   case AMDGPUISD::CONST_ADDRESS: {
389     SDValue CstOffset;
390     if (Src.getValueType().isVector() ||
391         !SelectGlobalValueConstantOffset(Src.getOperand(0), CstOffset))
392       return false;
393
394     ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(CstOffset);
395     Consts.push_back(Cst->getZExtValue());
396     if (!TII->fitsConstReadLimitations(Consts))
397       return false;
398
399     Src = CurDAG->getRegister(AMDGPU::ALU_CONST, MVT::f32);
400     Sel = CstOffset;
401     return true;
402     }
403   case ISD::FNEG:
404     Src = Src.getOperand(0);
405     Neg = CurDAG->getTargetConstant(1, MVT::i32);
406     return true;
407   case ISD::FABS:
408     if (!Abs.getNode())
409       return false;
410     Src = Src.getOperand(0);
411     Abs = CurDAG->getTargetConstant(1, MVT::i32);
412     return true;
413   case ISD::BITCAST:
414     Src = Src.getOperand(0);
415     return true;
416   default:
417     return false;
418   }
419 }
420
421 bool AMDGPUDAGToDAGISel::FoldOperands(unsigned Opcode,
422     const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
423   int OperandIdx[] = {
424     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0),
425     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1),
426     TII->getOperandIdx(Opcode, AMDGPU::OpName::src2)
427   };
428   int SelIdx[] = {
429     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel),
430     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel),
431     TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_sel)
432   };
433   int NegIdx[] = {
434     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg),
435     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg),
436     TII->getOperandIdx(Opcode, AMDGPU::OpName::src2_neg)
437   };
438   int AbsIdx[] = {
439     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs),
440     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs),
441     -1
442   };
443
444   // Gather constants values
445   std::vector<unsigned> Consts;
446   for (unsigned j = 0; j < 3; j++) {
447     int SrcIdx = OperandIdx[j];
448     if (SrcIdx < 0)
449       break;
450     if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
451       if (Reg->getReg() == AMDGPU::ALU_CONST) {
452         ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
453         Consts.push_back(Cst->getZExtValue());
454       }
455     }
456   }
457
458   for (unsigned i = 0; i < 3; i++) {
459     if (OperandIdx[i] < 0)
460       return false;
461     SDValue &Src = Ops[OperandIdx[i] - 1];
462     SDValue &Sel = Ops[SelIdx[i] - 1];
463     SDValue &Neg = Ops[NegIdx[i] - 1];
464     SDValue FakeAbs;
465     SDValue &Abs = (AbsIdx[i] > -1) ? Ops[AbsIdx[i] - 1] : FakeAbs;
466     if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
467       return true;
468   }
469   return false;
470 }
471
472 bool AMDGPUDAGToDAGISel::FoldDotOperands(unsigned Opcode,
473     const R600InstrInfo *TII, std::vector<SDValue> &Ops) {
474   int OperandIdx[] = {
475     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_X),
476     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Y),
477     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_Z),
478     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_W),
479     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_X),
480     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Y),
481     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_Z),
482     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_W)
483   };
484   int SelIdx[] = {
485     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_X),
486     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Y),
487     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_Z),
488     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_sel_W),
489     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_X),
490     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Y),
491     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_Z),
492     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_sel_W)
493   };
494   int NegIdx[] = {
495     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_X),
496     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Y),
497     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_Z),
498     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_neg_W),
499     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_X),
500     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Y),
501     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_Z),
502     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_neg_W)
503   };
504   int AbsIdx[] = {
505     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_X),
506     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Y),
507     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_Z),
508     TII->getOperandIdx(Opcode, AMDGPU::OpName::src0_abs_W),
509     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_X),
510     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Y),
511     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_Z),
512     TII->getOperandIdx(Opcode, AMDGPU::OpName::src1_abs_W)
513   };
514
515   // Gather constants values
516   std::vector<unsigned> Consts;
517   for (unsigned j = 0; j < 8; j++) {
518     int SrcIdx = OperandIdx[j];
519     if (SrcIdx < 0)
520       break;
521     if (RegisterSDNode *Reg = dyn_cast<RegisterSDNode>(Ops[SrcIdx - 1])) {
522       if (Reg->getReg() == AMDGPU::ALU_CONST) {
523         ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Ops[SelIdx[j] - 1]);
524         Consts.push_back(Cst->getZExtValue());
525       }
526     }
527   }
528
529   for (unsigned i = 0; i < 8; i++) {
530     if (OperandIdx[i] < 0)
531       return false;
532     SDValue &Src = Ops[OperandIdx[i] - 1];
533     SDValue &Sel = Ops[SelIdx[i] - 1];
534     SDValue &Neg = Ops[NegIdx[i] - 1];
535     SDValue &Abs = Ops[AbsIdx[i] - 1];
536     if (FoldOperand(Src, Sel, Neg, Abs, TII, Consts))
537       return true;
538   }
539   return false;
540 }
541
542 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
543   if (!ptr) {
544     return false;
545   }
546   Type *ptrType = ptr->getType();
547   return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
548 }
549
550 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
551   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
552 }
553
554 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
555   return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
556           && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
557           && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
558 }
559
560 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
561   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
562 }
563
564 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
565   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
566 }
567
568 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) const {
569   if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
570     return true;
571   }
572
573   const DataLayout *DL = TM.getDataLayout();
574   MachineMemOperand *MMO = N->getMemOperand();
575   const Value *V = MMO->getValue();
576   const Value *BV = GetUnderlyingObject(V, DL, 0);
577   if (MMO
578       && MMO->getValue()
579       && ((V && dyn_cast<GlobalValue>(V))
580           || (BV && dyn_cast<GlobalValue>(
581                 GetUnderlyingObject(MMO->getValue(), DL, 0))))) {
582     return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
583   } else {
584     return false;
585   }
586 }
587
588 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
589   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
590 }
591
592 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
593   return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
594 }
595
596 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) const {
597   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
598 }
599
600 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) const {
601   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
602 }
603
604 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
605   MachineMemOperand *MMO = N->getMemOperand();
606   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
607     if (MMO) {
608       const Value *V = MMO->getValue();
609       const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
610       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
611         return true;
612       }
613     }
614   }
615   return false;
616 }
617
618 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
619   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
620     // Check to make sure we are not a constant pool load or a constant load
621     // that is marked as a private load
622     if (isCPLoad(N) || isConstantLoad(N, -1)) {
623       return false;
624     }
625   }
626   if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
627       && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
628       && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
629       && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
630       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
631       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
632     return true;
633   }
634   return false;
635 }
636
637 const char *AMDGPUDAGToDAGISel::getPassName() const {
638   return "AMDGPU DAG->DAG Pattern Instruction Selection";
639 }
640
641 #ifdef DEBUGTMP
642 #undef INT64_C
643 #endif
644 #undef DEBUGTMP
645
646 ///==== AMDGPU Functions ====///
647
648 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
649     SDValue& IntPtr) {
650   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
651     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
652     return true;
653   }
654   return false;
655 }
656
657 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
658     SDValue& BaseReg, SDValue &Offset) {
659   if (!dyn_cast<ConstantSDNode>(Addr)) {
660     BaseReg = Addr;
661     Offset = CurDAG->getIntPtrConstant(0, true);
662     return true;
663   }
664   return false;
665 }
666
667 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
668                                            SDValue &Offset) {
669   ConstantSDNode * IMMOffset;
670
671   if (Addr.getOpcode() == ISD::ADD
672       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
673       && isInt<16>(IMMOffset->getZExtValue())) {
674
675       Base = Addr.getOperand(0);
676       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
677       return true;
678   // If the pointer address is constant, we can move it to the offset field.
679   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
680              && isInt<16>(IMMOffset->getZExtValue())) {
681     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
682                                   SDLoc(CurDAG->getEntryNode()),
683                                   AMDGPU::ZERO, MVT::i32);
684     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
685     return true;
686   }
687
688   // Default case, no offset
689   Base = Addr;
690   Offset = CurDAG->getTargetConstant(0, MVT::i32);
691   return true;
692 }
693
694 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
695                                             SDValue &Offset) {
696   ConstantSDNode *C;
697
698   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
699     Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
700     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
701   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
702             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
703     Base = Addr.getOperand(0);
704     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
705   } else {
706     Base = Addr;
707     Offset = CurDAG->getTargetConstant(0, MVT::i32);
708   }
709
710   return true;
711 }
712
713 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
714
715   if (Subtarget.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) {
716     return;
717   }
718
719   // Go over all selected nodes and try to fold them a bit more
720   const AMDGPUTargetLowering& Lowering =
721     (*(const AMDGPUTargetLowering*)getTargetLowering());
722   for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
723        E = CurDAG->allnodes_end(); I != E; ++I) {
724
725     SDNode *Node = I;
726     switch (Node->getOpcode()) {
727     // Fix the register class in copy to CopyToReg nodes - ISel will always
728     // use SReg classes for 64-bit copies, but this is not always what we want.
729     case ISD::CopyToReg: {
730       unsigned Reg = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
731       SDValue Val = Node->getOperand(2);
732       const TargetRegisterClass *RC = RegInfo->getRegClass(Reg);
733       if (RC != &AMDGPU::SReg_64RegClass) {
734         continue;
735       }
736
737       if (!Val.getNode()->isMachineOpcode() ||
738           Val.getNode()->getMachineOpcode() == AMDGPU::IMPLICIT_DEF) {
739         continue;
740       }
741
742       const MCInstrDesc Desc = TM.getInstrInfo()->get(Val.getNode()->getMachineOpcode());
743       const TargetRegisterInfo *TRI = TM.getRegisterInfo();
744       RegInfo->setRegClass(Reg, TRI->getRegClass(Desc.OpInfo[0].RegClass));
745       continue;
746     }
747     }
748
749     MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
750     if (!MachineNode)
751       continue;
752
753     SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
754     if (ResNode != Node) {
755       ReplaceUses(Node, ResNode);
756     }
757   }
758 }