R600: Expand vec4 INT <-> FP conversions
[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 <list>
24 #include <queue>
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);
44   virtual const char *getPassName() const;
45
46 private:
47   inline SDValue getSmallIPtrImm(unsigned Imm);
48
49   // Complex pattern selectors
50   bool SelectADDRParam(SDValue Addr, SDValue& R1, SDValue& R2);
51   bool SelectADDR(SDValue N, SDValue &R1, SDValue &R2);
52   bool SelectADDR64(SDValue N, SDValue &R1, SDValue &R2);
53
54   static bool checkType(const Value *ptr, unsigned int addrspace);
55   static const Value *getBasePointerValue(const Value *V);
56
57   static bool isGlobalStore(const StoreSDNode *N);
58   static bool isPrivateStore(const StoreSDNode *N);
59   static bool isLocalStore(const StoreSDNode *N);
60   static bool isRegionStore(const StoreSDNode *N);
61
62   static bool isCPLoad(const LoadSDNode *N);
63   static bool isConstantLoad(const LoadSDNode *N, int cbID);
64   static bool isGlobalLoad(const LoadSDNode *N);
65   static bool isParamLoad(const LoadSDNode *N);
66   static bool isPrivateLoad(const LoadSDNode *N);
67   static bool isLocalLoad(const LoadSDNode *N);
68   static bool isRegionLoad(const LoadSDNode *N);
69
70   bool SelectADDR8BitOffset(SDValue Addr, SDValue& Base, SDValue& Offset);
71   bool SelectADDRReg(SDValue Addr, SDValue& Base, SDValue& Offset);
72   bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset);
73
74   // Include the pieces autogenerated from the target description.
75 #include "AMDGPUGenDAGISel.inc"
76 };
77 }  // end anonymous namespace
78
79 /// \brief This pass converts a legalized DAG into a AMDGPU-specific
80 // DAG, ready for instruction scheduling.
81 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM
82                                        ) {
83   return new AMDGPUDAGToDAGISel(TM);
84 }
85
86 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM
87                                      )
88   : SelectionDAGISel(TM), Subtarget(TM.getSubtarget<AMDGPUSubtarget>()) {
89 }
90
91 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() {
92 }
93
94 SDValue AMDGPUDAGToDAGISel::getSmallIPtrImm(unsigned int Imm) {
95   return CurDAG->getTargetConstant(Imm, MVT::i32);
96 }
97
98 bool AMDGPUDAGToDAGISel::SelectADDRParam(
99     SDValue Addr, SDValue& R1, SDValue& R2) {
100
101   if (Addr.getOpcode() == ISD::FrameIndex) {
102     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
103       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
104       R2 = CurDAG->getTargetConstant(0, MVT::i32);
105     } else {
106       R1 = Addr;
107       R2 = CurDAG->getTargetConstant(0, MVT::i32);
108     }
109   } else if (Addr.getOpcode() == ISD::ADD) {
110     R1 = Addr.getOperand(0);
111     R2 = Addr.getOperand(1);
112   } else {
113     R1 = Addr;
114     R2 = CurDAG->getTargetConstant(0, MVT::i32);
115   }
116   return true;
117 }
118
119 bool AMDGPUDAGToDAGISel::SelectADDR(SDValue Addr, SDValue& R1, SDValue& R2) {
120   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
121       Addr.getOpcode() == ISD::TargetGlobalAddress) {
122     return false;
123   }
124   return SelectADDRParam(Addr, R1, R2);
125 }
126
127
128 bool AMDGPUDAGToDAGISel::SelectADDR64(SDValue Addr, SDValue& R1, SDValue& R2) {
129   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
130       Addr.getOpcode() == ISD::TargetGlobalAddress) {
131     return false;
132   }
133
134   if (Addr.getOpcode() == ISD::FrameIndex) {
135     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
136       R1 = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i64);
137       R2 = CurDAG->getTargetConstant(0, MVT::i64);
138     } else {
139       R1 = Addr;
140       R2 = CurDAG->getTargetConstant(0, MVT::i64);
141     }
142   } else if (Addr.getOpcode() == ISD::ADD) {
143     R1 = Addr.getOperand(0);
144     R2 = Addr.getOperand(1);
145   } else {
146     R1 = Addr;
147     R2 = CurDAG->getTargetConstant(0, MVT::i64);
148   }
149   return true;
150 }
151
152 SDNode *AMDGPUDAGToDAGISel::Select(SDNode *N) {
153   unsigned int Opc = N->getOpcode();
154   if (N->isMachineOpcode()) {
155     return NULL;   // Already selected.
156   }
157   switch (Opc) {
158   default: break;
159   case ISD::FrameIndex: {
160     if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) {
161       unsigned int FI = FIN->getIndex();
162       EVT OpVT = N->getValueType(0);
163       unsigned int NewOpc = AMDGPU::COPY;
164       SDValue TFI = CurDAG->getTargetFrameIndex(FI, MVT::i32);
165       return CurDAG->SelectNodeTo(N, NewOpc, OpVT, TFI);
166     }
167     break;
168   }
169   case ISD::ConstantFP:
170   case ISD::Constant: {
171     const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>();
172     // XXX: Custom immediate lowering not implemented yet.  Instead we use
173     // pseudo instructions defined in SIInstructions.td
174     if (ST.device()->getGeneration() > AMDGPUDeviceInfo::HD6XXX) {
175       break;
176     }
177     const R600InstrInfo *TII = static_cast<const R600InstrInfo*>(TM.getInstrInfo());
178
179     uint64_t ImmValue = 0;
180     unsigned ImmReg = AMDGPU::ALU_LITERAL_X;
181
182     if (N->getOpcode() == ISD::ConstantFP) {
183       // XXX: 64-bit Immediates not supported yet
184       assert(N->getValueType(0) != MVT::f64);
185
186       ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N);
187       APFloat Value = C->getValueAPF();
188       float FloatValue = Value.convertToFloat();
189       if (FloatValue == 0.0) {
190         ImmReg = AMDGPU::ZERO;
191       } else if (FloatValue == 0.5) {
192         ImmReg = AMDGPU::HALF;
193       } else if (FloatValue == 1.0) {
194         ImmReg = AMDGPU::ONE;
195       } else {
196         ImmValue = Value.bitcastToAPInt().getZExtValue();
197       }
198     } else {
199       // XXX: 64-bit Immediates not supported yet
200       assert(N->getValueType(0) != MVT::i64);
201
202       ConstantSDNode *C = dyn_cast<ConstantSDNode>(N);
203       if (C->getZExtValue() == 0) {
204         ImmReg = AMDGPU::ZERO;
205       } else if (C->getZExtValue() == 1) {
206         ImmReg = AMDGPU::ONE_INT;
207       } else {
208         ImmValue = C->getZExtValue();
209       }
210     }
211
212     for (SDNode::use_iterator Use = N->use_begin(), Next = llvm::next(Use);
213                               Use != SDNode::use_end(); Use = Next) {
214       Next = llvm::next(Use);
215       std::vector<SDValue> Ops;
216       for (unsigned i = 0; i < Use->getNumOperands(); ++i) {
217         Ops.push_back(Use->getOperand(i));
218       }
219
220       if (!Use->isMachineOpcode()) {
221           if (ImmReg == AMDGPU::ALU_LITERAL_X) {
222             // We can only use literal constants (e.g. AMDGPU::ZERO,
223             // AMDGPU::ONE, etc) in machine opcodes.
224             continue;
225           }
226       } else {
227         if (!TII->isALUInstr(Use->getMachineOpcode())) {
228           continue;
229         }
230
231         int ImmIdx = TII->getOperandIdx(Use->getMachineOpcode(), R600Operands::IMM);
232         assert(ImmIdx != -1);
233
234         // subtract one from ImmIdx, because the DST operand is usually index
235         // 0 for MachineInstrs, but we have no DST in the Ops vector.
236         ImmIdx--;
237
238         // Check that we aren't already using an immediate.
239         // XXX: It's possible for an instruction to have more than one
240         // immediate operand, but this is not supported yet.
241         if (ImmReg == AMDGPU::ALU_LITERAL_X) {
242           ConstantSDNode *C = dyn_cast<ConstantSDNode>(Use->getOperand(ImmIdx));
243           assert(C);
244
245           if (C->getZExtValue() != 0) {
246             // This instruction is already using an immediate.
247             continue;
248           }
249
250           // Set the immediate value
251           Ops[ImmIdx] = CurDAG->getTargetConstant(ImmValue, MVT::i32);
252         }
253       }
254       // Set the immediate register
255       Ops[Use.getOperandNo()] = CurDAG->getRegister(ImmReg, MVT::i32);
256
257       CurDAG->UpdateNodeOperands(*Use, Ops.data(), Use->getNumOperands());
258     }
259     break;
260   }
261   }
262   return SelectCode(N);
263 }
264
265 bool AMDGPUDAGToDAGISel::checkType(const Value *ptr, unsigned int addrspace) {
266   if (!ptr) {
267     return false;
268   }
269   Type *ptrType = ptr->getType();
270   return dyn_cast<PointerType>(ptrType)->getAddressSpace() == addrspace;
271 }
272
273 const Value * AMDGPUDAGToDAGISel::getBasePointerValue(const Value *V) {
274   if (!V) {
275     return NULL;
276   }
277   const Value *ret = NULL;
278   ValueMap<const Value *, bool> ValueBitMap;
279   std::queue<const Value *, std::list<const Value *> > ValueQueue;
280   ValueQueue.push(V);
281   while (!ValueQueue.empty()) {
282     V = ValueQueue.front();
283     if (ValueBitMap.find(V) == ValueBitMap.end()) {
284       ValueBitMap[V] = true;
285       if (dyn_cast<Argument>(V) && dyn_cast<PointerType>(V->getType())) {
286         ret = V;
287         break;
288       } else if (dyn_cast<GlobalVariable>(V)) {
289         ret = V;
290         break;
291       } else if (dyn_cast<Constant>(V)) {
292         const ConstantExpr *CE = dyn_cast<ConstantExpr>(V);
293         if (CE) {
294           ValueQueue.push(CE->getOperand(0));
295         }
296       } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
297         ret = AI;
298         break;
299       } else if (const Instruction *I = dyn_cast<Instruction>(V)) {
300         uint32_t numOps = I->getNumOperands();
301         for (uint32_t x = 0; x < numOps; ++x) {
302           ValueQueue.push(I->getOperand(x));
303         }
304       } else {
305         assert(!"Found a Value that we didn't know how to handle!");
306       }
307     }
308     ValueQueue.pop();
309   }
310   return ret;
311 }
312
313 bool AMDGPUDAGToDAGISel::isGlobalStore(const StoreSDNode *N) {
314   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
315 }
316
317 bool AMDGPUDAGToDAGISel::isPrivateStore(const StoreSDNode *N) {
318   return (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
319           && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
320           && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS));
321 }
322
323 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) {
324   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
325 }
326
327 bool AMDGPUDAGToDAGISel::isRegionStore(const StoreSDNode *N) {
328   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
329 }
330
331 bool AMDGPUDAGToDAGISel::isConstantLoad(const LoadSDNode *N, int cbID) {
332   if (checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)) {
333     return true;
334   }
335   MachineMemOperand *MMO = N->getMemOperand();
336   const Value *V = MMO->getValue();
337   const Value *BV = getBasePointerValue(V);
338   if (MMO
339       && MMO->getValue()
340       && ((V && dyn_cast<GlobalValue>(V))
341           || (BV && dyn_cast<GlobalValue>(
342                         getBasePointerValue(MMO->getValue()))))) {
343     return checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS);
344   } else {
345     return false;
346   }
347 }
348
349 bool AMDGPUDAGToDAGISel::isGlobalLoad(const LoadSDNode *N) {
350   return checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS);
351 }
352
353 bool AMDGPUDAGToDAGISel::isParamLoad(const LoadSDNode *N) {
354   return checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS);
355 }
356
357 bool AMDGPUDAGToDAGISel::isLocalLoad(const  LoadSDNode *N) {
358   return checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS);
359 }
360
361 bool AMDGPUDAGToDAGISel::isRegionLoad(const  LoadSDNode *N) {
362   return checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS);
363 }
364
365 bool AMDGPUDAGToDAGISel::isCPLoad(const LoadSDNode *N) {
366   MachineMemOperand *MMO = N->getMemOperand();
367   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
368     if (MMO) {
369       const Value *V = MMO->getValue();
370       const PseudoSourceValue *PSV = dyn_cast<PseudoSourceValue>(V);
371       if (PSV && PSV == PseudoSourceValue::getConstantPool()) {
372         return true;
373       }
374     }
375   }
376   return false;
377 }
378
379 bool AMDGPUDAGToDAGISel::isPrivateLoad(const LoadSDNode *N) {
380   if (checkType(N->getSrcValue(), AMDGPUAS::PRIVATE_ADDRESS)) {
381     // Check to make sure we are not a constant pool load or a constant load
382     // that is marked as a private load
383     if (isCPLoad(N) || isConstantLoad(N, -1)) {
384       return false;
385     }
386   }
387   if (!checkType(N->getSrcValue(), AMDGPUAS::LOCAL_ADDRESS)
388       && !checkType(N->getSrcValue(), AMDGPUAS::GLOBAL_ADDRESS)
389       && !checkType(N->getSrcValue(), AMDGPUAS::REGION_ADDRESS)
390       && !checkType(N->getSrcValue(), AMDGPUAS::CONSTANT_ADDRESS)
391       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_D_ADDRESS)
392       && !checkType(N->getSrcValue(), AMDGPUAS::PARAM_I_ADDRESS)) {
393     return true;
394   }
395   return false;
396 }
397
398 const char *AMDGPUDAGToDAGISel::getPassName() const {
399   return "AMDGPU DAG->DAG Pattern Instruction Selection";
400 }
401
402 #ifdef DEBUGTMP
403 #undef INT64_C
404 #endif
405 #undef DEBUGTMP
406
407 ///==== AMDGPU Functions ====///
408
409 bool AMDGPUDAGToDAGISel::SelectADDR8BitOffset(SDValue Addr, SDValue& Base,
410                                              SDValue& Offset) {
411   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
412       Addr.getOpcode() == ISD::TargetGlobalAddress) {
413     return false;
414   }
415
416
417   if (Addr.getOpcode() == ISD::ADD) {
418     bool Match = false;
419
420     // Find the base ptr and the offset
421     for (unsigned i = 0; i < Addr.getNumOperands(); i++) {
422       SDValue Arg = Addr.getOperand(i);
423       ConstantSDNode * OffsetNode = dyn_cast<ConstantSDNode>(Arg);
424       // This arg isn't a constant so it must be the base PTR.
425       if (!OffsetNode) {
426         Base = Addr.getOperand(i);
427         continue;
428       }
429       // Check if the constant argument fits in 8-bits.  The offset is in bytes
430       // so we need to convert it to dwords.
431       if (isUInt<8>(OffsetNode->getZExtValue() >> 2)) {
432         Match = true;
433         Offset = CurDAG->getTargetConstant(OffsetNode->getZExtValue() >> 2,
434                                            MVT::i32);
435       }
436     }
437     return Match;
438   }
439
440   // Default case, no offset
441   Base = Addr;
442   Offset = CurDAG->getTargetConstant(0, MVT::i32);
443   return true;
444 }
445
446 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base,
447                                            SDValue &Offset) {
448   ConstantSDNode * IMMOffset;
449
450   if (Addr.getOpcode() == ISD::ADD
451       && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
452       && isInt<16>(IMMOffset->getZExtValue())) {
453
454       Base = Addr.getOperand(0);
455       Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
456       return true;
457   // If the pointer address is constant, we can move it to the offset field.
458   } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr))
459              && isInt<16>(IMMOffset->getZExtValue())) {
460     Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
461                                   CurDAG->getEntryNode().getDebugLoc(),
462                                   AMDGPU::ZERO, MVT::i32);
463     Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), MVT::i32);
464     return true;
465   }
466
467   // Default case, no offset
468   Base = Addr;
469   Offset = CurDAG->getTargetConstant(0, MVT::i32);
470   return true;
471 }
472
473 bool AMDGPUDAGToDAGISel::SelectADDRReg(SDValue Addr, SDValue& Base,
474                                       SDValue& Offset) {
475   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
476       Addr.getOpcode() == ISD::TargetGlobalAddress  ||
477       Addr.getOpcode() != ISD::ADD) {
478     return false;
479   }
480
481   Base = Addr.getOperand(0);
482   Offset = Addr.getOperand(1);
483
484   return true;
485 }