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