R600: Move AMDGPUInstrInfo from AMDGPUTargetMachine into AMDGPUSubtarget
[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 ISD::BUILD_VECTOR: {
262     unsigned RegClassID;
263     const AMDGPURegisterInfo *TRI =
264                    static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
265     const SIRegisterInfo *SIRI =
266                    static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
267     EVT VT = N->getValueType(0);
268     unsigned NumVectorElts = VT.getVectorNumElements();
269     EVT EltVT = VT.getVectorElementType();
270     assert(EltVT.bitsEq(MVT::i32));
271     if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
272       bool UseVReg = true;
273       for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
274                                                     U != E; ++U) {
275         if (!U->isMachineOpcode()) {
276           continue;
277         }
278         const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
279         if (!RC) {
280           continue;
281         }
282         if (SIRI->isSGPRClass(RC)) {
283           UseVReg = false;
284         }
285       }
286       switch(NumVectorElts) {
287       case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
288                                      AMDGPU::SReg_32RegClassID;
289         break;
290       case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
291                                      AMDGPU::SReg_64RegClassID;
292         break;
293       case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
294                                      AMDGPU::SReg_128RegClassID;
295         break;
296       case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
297                                      AMDGPU::SReg_256RegClassID;
298         break;
299       case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
300                                       AMDGPU::SReg_512RegClassID;
301         break;
302       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
303       }
304     } else {
305       // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
306       // that adds a 128 bits reg copy when going through TwoAddressInstructions
307       // pass. We want to avoid 128 bits copies as much as possible because they
308       // can't be bundled by our scheduler.
309       switch(NumVectorElts) {
310       case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
311       case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
312       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
313       }
314     }
315
316     SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
317
318     if (NumVectorElts == 1) {
319       return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT,
320                                   N->getOperand(0), RegClass);
321     }
322
323     assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
324                                   "supported yet");
325     // 16 = Max Num Vector Elements
326     // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
327     // 1 = Vector Register Class
328     SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1);
329
330     RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
331     bool IsRegSeq = true;
332     unsigned NOps = N->getNumOperands();
333     for (unsigned i = 0; i < NOps; i++) {
334       // XXX: Why is this here?
335       if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
336         IsRegSeq = false;
337         break;
338       }
339       RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
340       RegSeqArgs[1 + (2 * i) + 1] =
341               CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
342     }
343
344     if (NOps != NumVectorElts) {
345       // Fill in the missing undef elements if this was a scalar_to_vector.
346       assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts);
347
348       MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
349                                                      SDLoc(N), EltVT);
350       for (unsigned i = NOps; i < NumVectorElts; ++i) {
351         RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0);
352         RegSeqArgs[1 + (2 * i) + 1] =
353           CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
354       }
355     }
356
357     if (!IsRegSeq)
358       break;
359     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
360                                 RegSeqArgs);
361   }
362   case ISD::BUILD_PAIR: {
363     SDValue RC, SubReg0, SubReg1;
364     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
365       break;
366     }
367     if (N->getValueType(0) == MVT::i128) {
368       RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
369       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
370       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
371     } else if (N->getValueType(0) == MVT::i64) {
372       RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32);
373       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
374       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
375     } else {
376       llvm_unreachable("Unhandled value type for BUILD_PAIR");
377     }
378     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
379                             N->getOperand(1), SubReg1 };
380     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
381                                   SDLoc(N), N->getValueType(0), Ops);
382   }
383
384   case ISD::Constant:
385   case ISD::ConstantFP: {
386     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
387     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
388         N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
389       break;
390
391     uint64_t Imm;
392     if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
393       Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
394     else {
395       ConstantSDNode *C = cast<ConstantSDNode>(N);
396       Imm = C->getZExtValue();
397     }
398
399     SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
400                                 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
401     SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
402                                 CurDAG->getConstant(Imm >> 32, MVT::i32));
403     const SDValue Ops[] = {
404       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
405       SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
406       SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
407     };
408
409     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
410                                   N->getValueType(0), Ops);
411   }
412
413   case AMDGPUISD::REGISTER_LOAD: {
414     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
415       break;
416     SDValue Addr, Offset;
417
418     SelectADDRIndirect(N->getOperand(1), Addr, Offset);
419     const SDValue Ops[] = {
420       Addr,
421       Offset,
422       CurDAG->getTargetConstant(0, MVT::i32),
423       N->getOperand(0),
424     };
425     return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
426                                   CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
427                                   Ops);
428   }
429   case AMDGPUISD::REGISTER_STORE: {
430     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
431       break;
432     SDValue Addr, Offset;
433     SelectADDRIndirect(N->getOperand(2), Addr, Offset);
434     const SDValue Ops[] = {
435       N->getOperand(1),
436       Addr,
437       Offset,
438       CurDAG->getTargetConstant(0, MVT::i32),
439       N->getOperand(0),
440     };
441     return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
442                                         CurDAG->getVTList(MVT::Other),
443                                         Ops);
444   }
445
446   case AMDGPUISD::BFE_I32:
447   case AMDGPUISD::BFE_U32: {
448     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
449       break;
450
451     // There is a scalar version available, but unlike the vector version which
452     // has a separate operand for the offset and width, the scalar version packs
453     // the width and offset into a single operand. Try to move to the scalar
454     // version if the offsets are constant, so that we can try to keep extended
455     // loads of kernel arguments in SGPRs.
456
457     // TODO: Technically we could try to pattern match scalar bitshifts of
458     // dynamic values, but it's probably not useful.
459     ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1));
460     if (!Offset)
461       break;
462
463     ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2));
464     if (!Width)
465       break;
466
467     bool Signed = Opc == AMDGPUISD::BFE_I32;
468
469     // Transformation function, pack the offset and width of a BFE into
470     // the format expected by the S_BFE_I32 / S_BFE_U32. In the second
471     // source, bits [5:0] contain the offset and bits [22:16] the width.
472
473     uint32_t OffsetVal = Offset->getZExtValue();
474     uint32_t WidthVal = Width->getZExtValue();
475
476     uint32_t PackedVal = OffsetVal | WidthVal << 16;
477
478     SDValue PackedOffsetWidth = CurDAG->getTargetConstant(PackedVal, MVT::i32);
479     return CurDAG->getMachineNode(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32,
480                                   SDLoc(N),
481                                   MVT::i32,
482                                   N->getOperand(0),
483                                   PackedOffsetWidth);
484
485   }
486   }
487   return SelectCode(N);
488 }
489
490
491 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) {
492   assert(AS != 0 && "Use checkPrivateAddress instead.");
493   if (!Ptr)
494     return false;
495
496   return Ptr->getType()->getPointerAddressSpace() == AS;
497 }
498
499 bool AMDGPUDAGToDAGISel::checkPrivateAddress(const MachineMemOperand *Op) {
500   if (Op->getPseudoValue())
501     return true;
502
503   if (PointerType *PT = dyn_cast<PointerType>(Op->getValue()->getType()))
504     return PT->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS;
505
506   return false;
507 }
508
509 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
510   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
511 }
512
513 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
514   const Value *MemVal = N->getMemOperand()->getValue();
515   return (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
516           !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
517           !checkType(MemVal, AMDGPUAS::REGION_ADDRESS));
518 }
519
520 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
521   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
522 }
523
524 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
525   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
526 }
527
528 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
529   const Value *MemVal = N->getMemOperand()->getValue();
530   if (CbId == -1)
531     return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS);
532
533   return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
534 }
535
536 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
537   if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
538     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
539     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
540         N->getMemoryVT().bitsLT(MVT::i32)) {
541       return true;
542     }
543   }
544   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS);
545 }
546
547 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
548   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::PARAM_I_ADDRESS);
549 }
550
551 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) const {
552   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS);
553 }
554
555 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) const {
556   return checkType(N->getMemOperand()->getValue(), AMDGPUAS::REGION_ADDRESS);
557 }
558
559 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
560   MachineMemOperand *MMO = N->getMemOperand();
561   if (checkPrivateAddress(N->getMemOperand())) {
562     if (MMO) {
563       const PseudoSourceValue *PSV = MMO->getPseudoValue();
564       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
565         return true;
566       }
567     }
568   }
569   return false;
570 }
571
572 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
573   if (checkPrivateAddress(N->getMemOperand())) {
574     // Check to make sure we are not a constant pool load or a constant load
575     // that is marked as a private load
576     if (isCPLoad(N) || isConstantLoad(N, -1)) {
577       return false;
578     }
579   }
580
581   const Value *MemVal = N->getMemOperand()->getValue();
582   if (!checkType(MemVal, AMDGPUAS::LOCAL_ADDRESS) &&
583       !checkType(MemVal, AMDGPUAS::GLOBAL_ADDRESS) &&
584       !checkType(MemVal, AMDGPUAS::REGION_ADDRESS) &&
585       !checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS) &&
586       !checkType(MemVal, AMDGPUAS::PARAM_D_ADDRESS) &&
587       !checkType(MemVal, AMDGPUAS::PARAM_I_ADDRESS)){
588     return true;
589   }
590   return false;
591 }
592
593 bool AMDGPUDAGToDAGISel::isCFDepth0() const {
594   // FIXME: Figure out a way to use DominatorTree analysis here.
595   const BasicBlock *CurBlock = FuncInfo->MBB->getBasicBlock();
596   const Function *Fn = FuncInfo->Fn;
597   return &Fn->front() == CurBlock || &Fn->back() == CurBlock;
598 }
599
600
601 const char *AMDGPUDAGToDAGISel::getPassName() const {
602   return "AMDGPU DAG->DAG Pattern Instruction Selection";
603 }
604
605 #ifdef DEBUGTMP
606 #undef INT64_C
607 #endif
608 #undef DEBUGTMP
609
610 //===----------------------------------------------------------------------===//
611 // Complex Patterns
612 //===----------------------------------------------------------------------===//
613
614 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
615                                                          SDValue& IntPtr) {
616   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
617     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
618     return true;
619   }
620   return false;
621 }
622
623 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
624     SDValue& BaseReg, SDValue &Offset) {
625   if (!isa<ConstantSDNode>(Addr)) {
626     BaseReg = Addr;
627     Offset = CurDAG->getIntPtrConstant(0, true);
628     return true;
629   }
630   return false;
631 }
632
633 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
634                                            SDValue &Offset) {
635   ConstantSDNode *IMMOffset;
636
637   if (Addr.getOpcode() == ISD::ADD
638       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
639       && isInt<16>(IMMOffset->getZExtValue())) {
640
641       Base = Addr.getOperand(0);
642       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
643       return true;
644   // If the pointer address is constant, we can move it to the offset field.
645   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
646              && isInt<16>(IMMOffset->getZExtValue())) {
647     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
648                                   SDLoc(CurDAG->getEntryNode()),
649                                   AMDGPU::ZERO, MVT::i32);
650     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
651     return true;
652   }
653
654   // Default case, no offset
655   Base = Addr;
656   Offset = CurDAG->getTargetConstant(0, MVT::i32);
657   return true;
658 }
659
660 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
661                                             SDValue &Offset) {
662   ConstantSDNode *C;
663
664   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
665     Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
666     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
667   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
668             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
669     Base = Addr.getOperand(0);
670     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
671   } else {
672     Base = Addr;
673     Offset = CurDAG->getTargetConstant(0, MVT::i32);
674   }
675
676   return true;
677 }
678
679 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
680   const AMDGPUTargetLowering& Lowering =
681     *static_cast<const AMDGPUTargetLowering*>(getTargetLowering());
682   bool IsModified = false;
683   do {
684     IsModified = false;
685     // Go over all selected nodes and try to fold them a bit more
686     for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
687          E = CurDAG->allnodes_end(); I != E; ++I) {
688
689       SDNode *Node = I;
690
691       MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
692       if (!MachineNode)
693         continue;
694
695       SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
696       if (ResNode != Node) {
697         ReplaceUses(Node, ResNode);
698         IsModified = true;
699       }
700     }
701     CurDAG->RemoveDeadNodes();
702   } while (IsModified);
703 }