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