R600: Use LDS and vectors for private memory
[oota-llvm.git] / lib / Target / R600 / AMDGPUISelDAGToDAG.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 "AMDGPUSubtarget.h"
18 #include "R600InstrInfo.h"
19 #include "SIISelLowering.h"
20 #include "llvm/CodeGen/FunctionLoweringInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/IR/Function.h"
25
26 using namespace llvm;
27
28 //===----------------------------------------------------------------------===//
29 // Instruction Selector Implementation
30 //===----------------------------------------------------------------------===//
31
32 namespace {
33 /// AMDGPU specific code to select AMDGPU machine instructions for
34 /// SelectionDAG operations.
35 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
36   // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
37   // make the right decision when generating code for different targets.
38   const AMDGPUSubtarget &Subtarget;
39 public:
40   AMDGPUDAGToDAGISel(TargetMachine &TM);
41   virtual ~AMDGPUDAGToDAGISel();
42
43   SDNode *Select(SDNode *N) override;
44   const char *getPassName() const override;
45   void PostprocessISelDAG() override;
46
47 private:
48   bool isInlineImmediate(SDNode *N) const;
49   inline SDValue getSmallIPtrImm(unsigned Imm);
50   bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
51                    const R600InstrInfo *TII);
52   bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
53   bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
54
55   // Complex pattern selectors
56   bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
57   bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
58   bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
59
60   static bool checkType(const Value *ptr, unsigned int addrspace);
61   static bool checkPrivateAddress(const MachineMemOperand *Op);
62
63   static bool isGlobalStore(const StoreSDNode *N);
64   static bool isPrivateStore(const StoreSDNode *N);
65   static bool isLocalStore(const StoreSDNode *N);
66   static bool isRegionStore(const StoreSDNode *N);
67
68   bool isCPLoad(const LoadSDNode *N) const;
69   bool isConstantLoad(const LoadSDNode *N, int cbID) const;
70   bool isGlobalLoad(const LoadSDNode *N) const;
71   bool isParamLoad(const LoadSDNode *N) const;
72   bool isPrivateLoad(const LoadSDNode *N) const;
73   bool isLocalLoad(const LoadSDNode *N) const;
74   bool isRegionLoad(const LoadSDNode *N) const;
75
76   /// \returns True if the current basic block being selected is at control
77   ///          flow depth 0.  Meaning that the current block dominates the
78   //           exit block.
79   bool isCFDepth0() const;
80
81   const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
82   bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
83   bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg,
84                                        SDValue& Offset);
85   bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
86   bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset);
87
88   // Include the pieces autogenerated from the target description.
89 #include "AMDGPUGenDAGISel.inc"
90 };
91 }  // end anonymous namespace
92
93 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
94 // DAG, ready for instruction scheduling.
95 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) {
96   return new AMDGPUDAGToDAGISel(TM);
97 }
98
99 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
100   : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
101 }
102
103 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
104 }
105
106 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
107   const SITargetLowering *TL
108       = static_cast<const SITargetLowering *>(getTargetLowering());
109   return TL->analyzeImmediate(N) == 0;
110 }
111
112 /// \brief Determine the register class for \p OpNo
113 /// \returns The register class of the virtual register that will be used for
114 /// the given operand number \OpNo or NULL if the register class cannot be
115 /// determined.
116 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
117                                                           unsigned OpNo) const {
118   if (!N->isMachineOpcode())
119     return nullptr;
120
121   switch (N->getMachineOpcode()) {
122   default: {
123     const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
124     unsigned OpIdx = Desc.getNumDefs() + OpNo;
125     if (OpIdx >= Desc.getNumOperands())
126       return nullptr;
127     int RegClass = Desc.OpInfo[OpIdx].RegClass;
128     if (RegClass == -1)
129       return nullptr;
130
131     return TM.getRegisterInfo()->getRegClass(RegClass);
132   }
133   case AMDGPU::REG_SEQUENCE: {
134     unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
135     const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(RCID);
136
137     SDValue SubRegOp = N->getOperand(OpNo + 1);
138     unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue();
139     return TM.getRegisterInfo()->getSubClassWithSubReg(SuperRC, SubRegIdx);
140   }
141   }
142 }
143
144 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
145   return CurDAG->getTargetConstant(Imm, MVT::i32);
146 }
147
148 bool AMDGPUDAGToDAGISel::SelectADDRParam(
149   SDValue Addr, SDValue& R1, SDValue& R2) {
150
151   if (Addr.getOpcode() == ISD::FrameIndex) {
152     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
153       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
154       R2 = CurDAG->getTargetConstant(0, MVT::i32);
155     } else {
156       R1 = Addr;
157       R2 = CurDAG->getTargetConstant(0, MVT::i32);
158     }
159   } else if (Addr.getOpcode() == ISD::ADD) {
160     R1 = Addr.getOperand(0);
161     R2 = Addr.getOperand(1);
162   } else {
163     R1 = Addr;
164     R2 = CurDAG->getTargetConstant(0, MVT::i32);
165   }
166   return true;
167 }
168
169 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
170   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
171       Addr.getOpcode() == ISD::TargetGlobalAddress) {
172     return false;
173   }
174   return SelectADDRParam(Addr, R1, R2);
175 }
176
177
178 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
179   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
180       Addr.getOpcode() == ISD::TargetGlobalAddress) {
181     return false;
182   }
183
184   if (Addr.getOpcode() == ISD::FrameIndex) {
185     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
186       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
187       R2 = CurDAG->getTargetConstant(0, MVT::i64);
188     } else {
189       R1 = Addr;
190       R2 = CurDAG->getTargetConstant(0, MVT::i64);
191     }
192   } else if (Addr.getOpcode() == ISD::ADD) {
193     R1 = Addr.getOperand(0);
194     R2 = Addr.getOperand(1);
195   } else {
196     R1 = Addr;
197     R2 = CurDAG->getTargetConstant(0, MVT::i64);
198   }
199   return true;
200 }
201
202 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
203   unsigned int Opc = N->getOpcode();
204   if (N->isMachineOpcode()) {
205     N->setNodeId(-1);
206     return nullptr;   // Already selected.
207   }
208
209   const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
210   switch (Opc) {
211   default: break;
212   // We are selecting i64 ADD here instead of custom lower it during
213   // DAG legalization, so we can fold some i64 ADDs used for address
214   // calculation into the LOAD and STORE instructions.
215   case ISD::ADD: {
216     if (N->getValueType(0) != MVT::i64 ||
217         ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
218       break;
219
220     SDLoc DL(N);
221     SDValue LHS = N->getOperand(0);
222     SDValue RHS = N->getOperand(1);
223
224     SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
225     SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
226
227     SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
228                                          DL, MVT::i32, LHS, Sub0);
229     SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
230                                          DL, MVT::i32, LHS, Sub1);
231
232     SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
233                                          DL, MVT::i32, RHS, Sub0);
234     SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
235                                          DL, MVT::i32, RHS, Sub1);
236
237     SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
238
239     SmallVector<SDValue, 8> AddLoArgs;
240     AddLoArgs.push_back(SDValue(Lo0, 0));
241     AddLoArgs.push_back(SDValue(Lo1, 0));
242
243     SDNode *AddLo = CurDAG->getMachineNode(
244         isCFDepth0() ? AMDGPU::S_ADD_I32 : AMDGPU::V_ADD_I32_e32,
245         DL, VTList, AddLoArgs);
246     SDValue Carry = SDValue(AddLo, 1);
247     SDNode *AddHi = CurDAG->getMachineNode(
248         isCFDepth0() ? AMDGPU::S_ADDC_U32 : AMDGPU::V_ADDC_U32_e32,
249         DL, MVT::i32, SDValue(Hi0, 0), SDValue(Hi1, 0), Carry);
250
251     SDValue Args[5] = {
252       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
253       SDValue(AddLo,0),
254       Sub0,
255       SDValue(AddHi,0),
256       Sub1,
257     };
258     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args);
259   }
260   case ISD::SCALAR_TO_VECTOR:
261   case AMDGPUISD::BUILD_VERTICAL_VECTOR:
262   case ISD::BUILD_VECTOR: {
263     unsigned RegClassID;
264     const AMDGPURegisterInfo *TRI =
265                    static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
266     const SIRegisterInfo *SIRI =
267                    static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
268     EVT VT = N->getValueType(0);
269     unsigned NumVectorElts = VT.getVectorNumElements();
270     EVT EltVT = VT.getVectorElementType();
271     assert(EltVT.bitsEq(MVT::i32));
272     if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
273       bool UseVReg = true;
274       for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
275                                                     U != E; ++U) {
276         if (!U->isMachineOpcode()) {
277           continue;
278         }
279         const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
280         if (!RC) {
281           continue;
282         }
283         if (SIRI->isSGPRClass(RC)) {
284           UseVReg = false;
285         }
286       }
287       switch(NumVectorElts) {
288       case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
289                                      AMDGPU::SReg_32RegClassID;
290         break;
291       case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
292                                      AMDGPU::SReg_64RegClassID;
293         break;
294       case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
295                                      AMDGPU::SReg_128RegClassID;
296         break;
297       case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
298                                      AMDGPU::SReg_256RegClassID;
299         break;
300       case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
301                                       AMDGPU::SReg_512RegClassID;
302         break;
303       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
304       }
305     } else {
306       // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
307       // that adds a 128 bits reg copy when going through TwoAddressInstructions
308       // pass. We want to avoid 128 bits copies as much as possible because they
309       // can't be bundled by our scheduler.
310       switch(NumVectorElts) {
311       case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
312       case 4:
313         if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR)
314           RegClassID = AMDGPU::R600_Reg128VerticalRegClassID;
315         else
316           RegClassID = AMDGPU::R600_Reg128RegClassID;
317         break;
318       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
319       }
320     }
321
322     SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
323
324     if (NumVectorElts == 1) {
325       return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
326                                   N->getOperand(0), RegClass);
327     }
328
329     assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
330                                   "supported yet");
331     // 16 = Max Num Vector Elements
332     // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
333     // 1 = Vector Register Class
334     SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
335
336     RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
337     bool IsRegSeq = true;
338     unsigned NOps = N->getNumOperands();
339     for (unsigned i = 0; i < NOps; i++) {
340       // XXX: Why is this here?
341       if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
342         IsRegSeq = false;
343         break;
344       }
345       RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
346       RegSeqArgs[1 + (2 * i) + 1] =
347               CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
348     }
349
350     if (NOps != NumVectorElts) {
351       // Fill in the missing undef elements if this was a scalar_to_vector.
352       assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
353
354       MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
355                                                      SDLoc(N), EltVT);
356       for (unsigned i = NOps; i < NumVectorElts; ++i) {
357         RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
358         RegSeqArgs[1 + (2 * i) + 1] =
359           CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
360       }
361     }
362
363     if (!IsRegSeq)
364       break;
365     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
366                                 RegSeqArgs);
367   }
368   case ISD::BUILD_PAIR: {
369     SDValue RC, SubReg0, SubReg1;
370     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
371       break;
372     }
373     if (N->getValueType(0) == MVT::i128) {
374       RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
375       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
376       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
377     } else if (N->getValueType(0) == MVT::i64) {
378       RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
379       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
380       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
381     } else {
382       llvm_unreachable("Unhandled value type for BUILD_PAIR");
383     }
384     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
385                             N->getOperand(1), SubReg1 };
386     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
387                                   SDLoc(N), N->getValueType(0), Ops);
388   }
389
390   case ISD::Constant:
391   case ISD::ConstantFP: {
392     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
393     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
394         N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
395       break;
396
397     uint64_t Imm;
398     if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
399       Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
400     else {
401       ConstantSDNode *C = cast<ConstantSDNode>(N);
402       Imm = C->getZExtValue();
403     }
404
405     SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
406                                 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
407     SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
408                                 CurDAG->getConstant(Imm >> 32, MVT::i32));
409     const SDValue Ops[] = {
410       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
411       SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
412       SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
413     };
414
415     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
416                                   N->getValueType(0), Ops);
417   }
418
419   case AMDGPUISD::REGISTER_LOAD: {
420     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
421       break;
422     SDValue Addr, Offset;
423
424     SelectADDRIndirect(N->getOperand(1), Addr, Offset);
425     const SDValue Ops[] = {
426       Addr,
427       Offset,
428       CurDAG->getTargetConstant(0, MVT::i32),
429       N->getOperand(0),
430     };
431     return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
432                                   CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
433                                   Ops);
434   }
435   case AMDGPUISD::REGISTER_STORE: {
436     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
437       break;
438     SDValue Addr, Offset;
439     SelectADDRIndirect(N->getOperand(2), Addr, Offset);
440     const SDValue Ops[] = {
441       N->getOperand(1),
442       Addr,
443       Offset,
444       CurDAG->getTargetConstant(0, MVT::i32),
445       N->getOperand(0),
446     };
447     return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
448                                         CurDAG->getVTList(MVT::Other),
449                                         Ops);
450   }
451
452   case AMDGPUISD::BFE_I32:
453   case AMDGPUISD::BFE_U32: {
454     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
455       break;
456
457     // There is a scalar version available, but unlike the vector version which
458     // has a separate operand for the offset and width, the scalar version packs
459     // the width and offset into a single operand. Try to move to the scalar
460     // version if the offsets are constant, so that we can try to keep extended
461     // loads of kernel arguments in SGPRs.
462
463     // TODO: Technically we could try to pattern match scalar bitshifts of
464     // dynamic values, but it's probably not useful.
465     ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
466     if (!Offset)
467       break;
468
469     ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
470     if (!Width)
471       break;
472
473     bool Signed = Opc == AMDGPUISD::BFE_I32;
474
475     // Transformation function, pack the offset and width of a BFE into
476     // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
477     // source, bits [5:0] contain the offset and bits [22:16] the width.
478
479     uint32_t OffsetVal = Offset->getZExtValue();
480     uint32_t WidthVal = Width->getZExtValue();
481
482     uint32_t PackedVal = OffsetVal | WidthVal << 16;
483
484     SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
485     return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
486                                   SDLoc(N),
487                                   MVT::i32,
488                                   N->getOperand(0),
489                                   PackedOffsetWidth);
490
491   }
492   }
493   return SelectCode(N);
494 }
495
496
497 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
498   assert(AS != 0 && "Use checkPrivateAddress instead.");
499   if (!Ptr)
500     return false;
501
502   return Ptr->getType()->getPointerAddressSpace() == AS;
503 }
504
505 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
506   if (Op->getPseudoValue())
507     return true;
508
509   if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
510     return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
511
512   return false;
513 }
514
515 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
516   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
517 }
518
519 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
520   const Value *MemVal = N->getMemOperand()->getValue();
521   return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
522           !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
523           !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
524 }
525
526 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
527   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
528 }
529
530 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
531   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
532 }
533
534 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
535   const Value *MemVal = N->getMemOperand()->getValue();
536   if (CbId == -1)
537     return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
538
539   return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
540 }
541
542 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
543   if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
544     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
545     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
546         N->getMemoryVT().bitsLT(MVT::i32)) {
547       return true;
548     }
549   }
550   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
551 }
552
553 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
554   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
555 }
556
557 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) const {
558   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
559 }
560
561 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) const {
562   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
563 }
564
565 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
566   MachineMemOperand *MMO = N->getMemOperand();
567   if (checkPrivateAddress(N->getMemOperand())) {
568     if (MMO) {
569       const PseudoSourceValue *PSV = MMO->getPseudoValue();
570       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
571         return true;
572       }
573     }
574   }
575   return false;
576 }
577
578 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
579   if (checkPrivateAddress(N->getMemOperand())) {
580     // Check to make sure we are not a constant pool load or a constant load
581     // that is marked as a private load
582     if (isCPLoad(N) || isConstantLoad(N, -1)) {
583       return false;
584     }
585   }
586
587   const Value *MemVal = N->getMemOperand()->getValue();
588   if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
589       !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
590       !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
591       !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
592       !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
593       !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
594     return true;
595   }
596   return false;
597 }
598
599 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
600   // FIXME: Figure out a way to use DominatorTree analysis here.
601   const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
602   const Function *Fn = FuncInfo->Fn;
603   return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
604 }
605
606
607 const char *AMDGPUDAGToDAGISel::getPassName() const {
608   return "AMDGPU DAG->DAG Pattern Instruction Selection";
609 }
610
611 #ifdef DEBUGTMP
612 #undef INT64_C
613 #endif
614 #undef DEBUGTMP
615
616 //===----------------------------------------------------------------------===//
617 // Complex Patterns
618 //===----------------------------------------------------------------------===//
619
620 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
621                                                          SDValue& IntPtr) {
622   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
623     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
624     return true;
625   }
626   return false;
627 }
628
629 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
630     SDValue& BaseReg, SDValue &Offset) {
631   if (!isa<ConstantSDNode>(Addr)) {
632     BaseReg = Addr;
633     Offset = CurDAG->getIntPtrConstant(0, true);
634     return true;
635   }
636   return false;
637 }
638
639 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
640                                            SDValue &Offset) {
641   ConstantSDNode *IMMOffset;
642
643   if (Addr.getOpcode() == ISD::ADD
644       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
645       && isInt<16>(IMMOffset->getZExtValue())) {
646
647       Base = Addr.getOperand(0);
648       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
649       return true;
650   // If the pointer address is constant, we can move it to the offset field.
651   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
652              && isInt<16>(IMMOffset->getZExtValue())) {
653     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
654                                   SDLoc(CurDAG->getEntryNode()),
655                                   AMDGPU::ZERO, MVT::i32);
656     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
657     return true;
658   }
659
660   // Default case, no offset
661   Base = Addr;
662   Offset = CurDAG->getTargetConstant(0, MVT::i32);
663   return true;
664 }
665
666 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
667                                             SDValue &Offset) {
668   ConstantSDNode *C;
669
670   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
671     Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
672     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
673   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
674             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
675     Base = Addr.getOperand(0);
676     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
677   } else {
678     Base = Addr;
679     Offset = CurDAG->getTargetConstant(0, MVT::i32);
680   }
681
682   return true;
683 }
684
685 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
686   const AMDGPUTargetLowering& Lowering =
687     *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
688   bool IsModified = false;
689   do {
690     IsModified = false;
691     // Go over all selected nodes and try to fold them a bit more
692     for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
693          E = CurDAG->allnodes_end(); I != E; ++I) {
694
695       SDNode *Node = I;
696
697       MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
698       if (!MachineNode)
699         continue;
700
701       SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
702       if (ResNode != Node) {
703         ReplaceUses(Node, ResNode);
704         IsModified = true;
705       }
706     }
707     CurDAG->RemoveDeadNodes();
708   } while (IsModified);
709 }