R600/SI: Lower 64-bit immediates using REG_SEQUENCE
[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 "R600InstrInfo.h"
18 #include "SIISelLowering.h"
19 #include "llvm/Analysis/ValueTracking.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/PseudoSourceValue.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/IR/ValueMap.h"
25 #include "llvm/Support/Compiler.h"
26 #include <list>
27 #include <queue>
28
29 using namespace llvm;
30
31 //===----------------------------------------------------------------------===//
32 // Instruction Selector Implementation
33 //===----------------------------------------------------------------------===//
34
35 namespace {
36 /// AMDGPU specific code to select AMDGPU machine instructions for
37 /// SelectionDAG operations.
38 class AMDGPUDAGToDAGISel : public SelectionDAGISel {
39   // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can
40   // make the right decision when generating code for different targets.
41   const AMDGPUSubtarget &Subtarget;
42 public:
43   AMDGPUDAGToDAGISel(TargetMachine &TM);
44   virtual ~AMDGPUDAGToDAGISel();
45
46   SDNode *Select(SDNode *N);
47   virtual const char *getPassName() const;
48   virtual void PostprocessISelDAG();
49
50 private:
51   bool isInlineImmediate(SDNode *N) const;
52   inline SDValue getSmallIPtrImm(unsigned Imm);
53   bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs,
54                    const R600InstrInfo *TII);
55   bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
56   bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &);
57
58   // Complex pattern selectors
59   bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
60   bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
61   bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
62   SDValue SimplifyI24(SDValue &Op);
63   bool SelectI24(SDValue Addr, SDValue &Op);
64   bool SelectU24(SDValue Addr, SDValue &Op);
65
66   static bool checkType(const Value *ptr, unsigned int addrspace);
67
68   static bool isGlobalStore(const StoreSDNode *N);
69   static bool isPrivateStore(const StoreSDNode *N);
70   static bool isLocalStore(const StoreSDNode *N);
71   static bool isRegionStore(const StoreSDNode *N);
72
73   bool isCPLoad(const LoadSDNode *N) const;
74   bool isConstantLoad(const LoadSDNode *N, int cbID) const;
75   bool isGlobalLoad(const LoadSDNode *N) const;
76   bool isParamLoad(const LoadSDNode *N) const;
77   bool isPrivateLoad(const LoadSDNode *N) const;
78   bool isLocalLoad(const LoadSDNode *N) const;
79   bool isRegionLoad(const LoadSDNode *N) const;
80
81   const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const;
82   bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr);
83   bool SelectGlobalValueVariableOffset(SDValue Addr,
84       SDValue &BaseReg, 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                                        ) {
97   return new AMDGPUDAGToDAGISel(TM);
98 }
99
100 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM)
101   : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
102 }
103
104 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
105 }
106
107 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const {
108   const SITargetLowering *TL
109       = static_cast<const SITargetLowering *>(getTargetLowering());
110   return TL->analyzeImmediate(N) == 0;
111 }
112
113 /// \brief Determine the register class for \p OpNo
114 /// \returns The register class of the virtual register that will be used for
115 /// the given operand number \OpNo or NULL if the register class cannot be
116 /// determined.
117 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N,
118                                                           unsigned OpNo) const {
119   if (!N->isMachineOpcode()) {
120     return NULL;
121   }
122   switch (N->getMachineOpcode()) {
123   default: {
124     const MCInstrDesc &Desc = TM.getInstrInfo()->get(N->getMachineOpcode());
125     unsigned OpIdx = Desc.getNumDefs() + OpNo;
126     if (OpIdx >= Desc.getNumOperands())
127       return NULL;
128     int RegClass = Desc.OpInfo[OpIdx].RegClass;
129     if (RegClass == -1) {
130       return NULL;
131     }
132     return TM.getRegisterInfo()->getRegClass(RegClass);
133   }
134   case AMDGPU::REG_SEQUENCE: {
135     const TargetRegisterClass *SuperRC = TM.getRegisterInfo()->getRegClass(
136                       cast<ConstantSDNode>(N->getOperand(0))->getZExtValue());
137     unsigned SubRegIdx =
138             dyn_cast<ConstantSDNode>(N->getOperand(OpNo + 1))->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 NULL;   // Already selected.
207   }
208   switch (Opc) {
209   default: break;
210   // We are selecting i64 ADD here instead of custom lower it during
211   // DAG legalization, so we can fold some i64 ADDs used for address
212   // calculation into the LOAD and STORE instructions.
213   case ISD::ADD: {
214     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
215     if (N->getValueType(0) != MVT::i64 ||
216         ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS)
217       break;
218
219     SDLoc DL(N);
220     SDValue LHS = N->getOperand(0);
221     SDValue RHS = N->getOperand(1);
222
223     SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
224     SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
225
226     SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
227                                          DL, MVT::i32, LHS, Sub0);
228     SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
229                                          DL, MVT::i32, LHS, Sub1);
230
231     SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
232                                          DL, MVT::i32, RHS, Sub0);
233     SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
234                                          DL, MVT::i32, RHS, Sub1);
235
236     SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue);
237
238     SmallVector<SDValue, 8> AddLoArgs;
239     AddLoArgs.push_back(SDValue(Lo0, 0));
240     AddLoArgs.push_back(SDValue(Lo1, 0));
241
242     SDNode *AddLo = CurDAG->getMachineNode(AMDGPU::S_ADD_I32, DL,
243                                            VTList, AddLoArgs);
244     SDValue Carry = SDValue(AddLo, 1);
245     SDNode *AddHi = CurDAG->getMachineNode(AMDGPU::S_ADDC_U32, DL,
246                                            MVT::i32, SDValue(Hi0, 0),
247                                            SDValue(Hi1, 0), Carry);
248
249     SDValue Args[5] = {
250       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
251       SDValue(AddLo,0),
252       Sub0,
253       SDValue(AddHi,0),
254       Sub1,
255     };
256     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args, 5);
257   }
258   case ISD::BUILD_VECTOR: {
259     unsigned RegClassID;
260     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
261     const AMDGPURegisterInfo *TRI =
262                    static_cast<const AMDGPURegisterInfo*>(TM.getRegisterInfo());
263     const SIRegisterInfo *SIRI =
264                    static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
265     EVT VT = N->getValueType(0);
266     unsigned NumVectorElts = VT.getVectorNumElements();
267     assert(VT.getVectorElementType().bitsEq(MVT::i32));
268     if (ST.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
269       bool UseVReg = true;
270       for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end();
271                                                     U != E; ++U) {
272         if (!U->isMachineOpcode()) {
273           continue;
274         }
275         const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo());
276         if (!RC) {
277           continue;
278         }
279         if (SIRI->isSGPRClass(RC)) {
280           UseVReg = false;
281         }
282       }
283       switch(NumVectorElts) {
284       case 1: RegClassID = UseVReg ? AMDGPU::VReg_32RegClassID :
285                                      AMDGPU::SReg_32RegClassID;
286         break;
287       case 2: RegClassID = UseVReg ? AMDGPU::VReg_64RegClassID :
288                                      AMDGPU::SReg_64RegClassID;
289         break;
290       case 4: RegClassID = UseVReg ? AMDGPU::VReg_128RegClassID :
291                                      AMDGPU::SReg_128RegClassID;
292         break;
293       case 8: RegClassID = UseVReg ? AMDGPU::VReg_256RegClassID :
294                                      AMDGPU::SReg_256RegClassID;
295         break;
296       case 16: RegClassID = UseVReg ? AMDGPU::VReg_512RegClassID :
297                                       AMDGPU::SReg_512RegClassID;
298         break;
299       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
300       }
301     } else {
302       // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG
303       // that adds a 128 bits reg copy when going through TwoAddressInstructions
304       // pass. We want to avoid 128 bits copies as much as possible because they
305       // can't be bundled by our scheduler.
306       switch(NumVectorElts) {
307       case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break;
308       case 4: RegClassID = AMDGPU::R600_Reg128RegClassID; break;
309       default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR");
310       }
311     }
312
313     SDValue RegClass = CurDAG->getTargetConstant(RegClassID, MVT::i32);
314
315     if (NumVectorElts == 1) {
316       return CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS,
317                                   VT.getVectorElementType(),
318                                   N->getOperand(0), RegClass);
319     }
320
321     assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not "
322                                   "supported yet");
323     // 16 = Max Num Vector Elements
324     // 2 = 2 REG_SEQUENCE operands per element (value, subreg index)
325     // 1 = Vector Register Class
326     SDValue RegSeqArgs[16 * 2 + 1];
327
328     RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, MVT::i32);
329     bool IsRegSeq = true;
330     for (unsigned i = 0; i < N->getNumOperands(); i++) {
331       // XXX: Why is this here?
332       if (dyn_cast<RegisterSDNode>(N->getOperand(i))) {
333         IsRegSeq = false;
334         break;
335       }
336       RegSeqArgs[1 + (2 * i)] = N->getOperand(i);
337       RegSeqArgs[1 + (2 * i) + 1] =
338               CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), MVT::i32);
339     }
340     if (!IsRegSeq)
341       break;
342     return CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(),
343         RegSeqArgs, 2 * N->getNumOperands() + 1);
344   }
345   case ISD::BUILD_PAIR: {
346     SDValue RC, SubReg0, SubReg1;
347     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
348     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) {
349       break;
350     }
351     if (N->getValueType(0) == MVT::i128) {
352       RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, MVT::i32);
353       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, MVT::i32);
354       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, MVT::i32);
355     } else if (N->getValueType(0) == MVT::i64) {
356       RC = CurDAG->getTargetConstant(AMDGPU::VSrc_64RegClassID, MVT::i32);
357       SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32);
358       SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32);
359     } else {
360       llvm_unreachable("Unhandled value type for BUILD_PAIR");
361     }
362     const SDValue Ops[] = { RC, N->getOperand(0), SubReg0,
363                             N->getOperand(1), SubReg1 };
364     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE,
365                                   SDLoc(N), N->getValueType(0), Ops);
366   }
367
368   case ISD::Constant:
369   case ISD::ConstantFP: {
370     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
371     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
372         N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N))
373       break;
374
375     uint64_t Imm;
376     if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N))
377       Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue();
378     else {
379       ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
380       assert(C);
381       Imm = C->getZExtValue();
382     }
383
384     SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
385                                 CurDAG->getConstant(Imm & 0xFFFFFFFF, MVT::i32));
386     SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SDLoc(N), MVT::i32,
387                                 CurDAG->getConstant(Imm >> 32, MVT::i32));
388     const SDValue Ops[] = {
389       CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, MVT::i32),
390       SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, MVT::i32),
391       SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, MVT::i32)
392     };
393
394     return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, SDLoc(N),
395                                   N->getValueType(0), Ops);
396   }
397
398   case AMDGPUISD::REGISTER_LOAD: {
399     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
400     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
401       break;
402     SDValue Addr, Offset;
403
404     SelectADDRIndirect(N->getOperand(1), Addr, Offset);
405     const SDValue Ops[] = {
406       Addr,
407       Offset,
408       CurDAG->getTargetConstant(0, MVT::i32),
409       N->getOperand(0),
410     };
411     return CurDAG->getMachineNode(AMDGPU::SI_RegisterLoad, SDLoc(N),
412                                   CurDAG->getVTList(MVT::i32, MVT::i64, MVT::Other),
413                                   Ops);
414   }
415   case AMDGPUISD::REGISTER_STORE: {
416     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
417     if (ST.getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS)
418       break;
419     SDValue Addr, Offset;
420     SelectADDRIndirect(N->getOperand(2), Addr, Offset);
421     const SDValue Ops[] = {
422       N->getOperand(1),
423       Addr,
424       Offset,
425       CurDAG->getTargetConstant(0, MVT::i32),
426       N->getOperand(0),
427     };
428     return CurDAG->getMachineNode(AMDGPU::SI_RegisterStorePseudo, SDLoc(N),
429                                         CurDAG->getVTList(MVT::Other),
430                                         Ops);
431   }
432   }
433   return SelectCode(N);
434 }
435
436
437 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
438   if (!ptr) {
439     return false;
440   }
441   Type *ptrType = ptr->getType();
442   return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
443 }
444
445 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
446   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
447 }
448
449 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
450   return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
451           && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
452           && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
453 }
454
455 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
456   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
457 }
458
459 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
460   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
461 }
462
463 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int CbId) const {
464   if (CbId == -1) {
465     return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS);
466   }
467   return checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_BUFFER_0 + CbId);
468 }
469
470 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) const {
471   if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) {
472     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
473     if (ST.getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS ||
474         N->getMemoryVT().bitsLT(MVT::i32)) {
475       return true;
476     }
477   }
478   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
479 }
480
481 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) const {
482   return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
483 }
484
485 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) const {
486   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
487 }
488
489 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) const {
490   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
491 }
492
493 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) const {
494   MachineMemOperand *MMO = N->getMemOperand();
495   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
496     if (MMO) {
497       const Value *V = MMO->getValue();
498       const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
499       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
500         return true;
501       }
502     }
503   }
504   return false;
505 }
506
507 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) const {
508   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
509     // Check to make sure we are not a constant pool load or a constant load
510     // that is marked as a private load
511     if (isCPLoad(N) || isConstantLoad(N, -1)) {
512       return false;
513     }
514   }
515   if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
516       && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
517       && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
518       && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
519       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
520       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
521     return true;
522   }
523   return false;
524 }
525
526 const char *AMDGPUDAGToDAGISel::getPassName() const {
527   return "AMDGPU DAG->DAG Pattern Instruction Selection";
528 }
529
530 #ifdef DEBUGTMP
531 #undef INT64_C
532 #endif
533 #undef DEBUGTMP
534
535 //===----------------------------------------------------------------------===//
536 // Complex Patterns
537 //===----------------------------------------------------------------------===//
538
539 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr,
540     SDValue& IntPtr) {
541   if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) {
542     IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, true);
543     return true;
544   }
545   return false;
546 }
547
548 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr,
549     SDValue& BaseReg, SDValue &Offset) {
550   if (!dyn_cast<ConstantSDNode>(Addr)) {
551     BaseReg = Addr;
552     Offset = CurDAG->getIntPtrConstant(0, true);
553     return true;
554   }
555   return false;
556 }
557
558 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
559                                            SDValue &Offset) {
560   ConstantSDNode * IMMOffset;
561
562   if (Addr.getOpcode() == ISD::ADD
563       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
564       && isInt<16>(IMMOffset->getZExtValue())) {
565
566       Base = Addr.getOperand(0);
567       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
568       return true;
569   // If the pointer address is constant, we can move it to the offset field.
570   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
571              && isInt<16>(IMMOffset->getZExtValue())) {
572     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
573                                   SDLoc(CurDAG->getEntryNode()),
574                                   AMDGPU::ZERO, MVT::i32);
575     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
576     return true;
577   }
578
579   // Default case, no offset
580   Base = Addr;
581   Offset = CurDAG->getTargetConstant(0, MVT::i32);
582   return true;
583 }
584
585 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base,
586                                             SDValue &Offset) {
587   ConstantSDNode *C;
588
589   if ((C = dyn_cast<ConstantSDNode>(Addr))) {
590     Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32);
591     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
592   } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) &&
593             (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) {
594     Base = Addr.getOperand(0);
595     Offset = CurDAG->getTargetConstant(C->getZExtValue(), MVT::i32);
596   } else {
597     Base = Addr;
598     Offset = CurDAG->getTargetConstant(0, MVT::i32);
599   }
600
601   return true;
602 }
603
604 SDValue AMDGPUDAGToDAGISel::SimplifyI24(SDValue &Op) {
605   APInt Demanded = APInt(32, 0x00FFFFFF);
606   APInt KnownZero, KnownOne;
607   TargetLowering::TargetLoweringOpt TLO(*CurDAG, true, true);
608   const TargetLowering *TLI = getTargetLowering();
609   if (TLI->SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO)) {
610     CurDAG->ReplaceAllUsesWith(Op, TLO.New);
611     CurDAG->RepositionNode(Op.getNode(), TLO.New.getNode());
612     return SimplifyI24(TLO.New);
613   } else {
614     return  Op;
615   }
616 }
617
618 bool AMDGPUDAGToDAGISel::SelectI24(SDValue Op, SDValue &I24) {
619
620   assert(Op.getValueType() == MVT::i32);
621
622   if (CurDAG->ComputeNumSignBits(Op) == 9) {
623     I24 = SimplifyI24(Op);
624     return true;
625   }
626   return false;
627 }
628
629 bool AMDGPUDAGToDAGISel::SelectU24(SDValue Op, SDValue &U24) {
630   APInt KnownZero;
631   APInt KnownOne;
632   CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
633
634   assert (Op.getValueType() == MVT::i32);
635
636   // ANY_EXTEND and EXTLOAD operations can only be done on types smaller than
637   // i32.  These smaller types are legal to use with the i24 instructions.
638   if ((KnownZero & APInt(KnownZero.getBitWidth(), 0xFF000000)) == 0xFF000000 ||
639        Op.getOpcode() == ISD::ANY_EXTEND ||
640        ISD::isEXTLoad(Op.getNode())) {
641     U24 = SimplifyI24(Op);
642     return true;
643   }
644   return false;
645 }
646
647 void AMDGPUDAGToDAGISel::PostprocessISelDAG() {
648   const AMDGPUTargetLowering& Lowering =
649     (*(const AMDGPUTargetLowering*)getTargetLowering());
650   bool IsModified = false;
651   do {
652     IsModified = false;
653     // Go over all selected nodes and try to fold them a bit more
654     for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
655          E = CurDAG->allnodes_end(); I != E; ++I) {
656
657       SDNode *Node = I;
658
659       MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
660       if (!MachineNode)
661         continue;
662
663       SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG);
664       if (ResNode != Node) {
665         ReplaceUses(Node, ResNode);
666         IsModified = true;
667       }
668     }
669     CurDAG->RemoveDeadNodes();
670   } while (IsModified);
671 }