Use cast<> instead of unchecked dyn_cast
[oota-llvm.git] / lib / Target / R600 / AMDGPUISelLowering.cpp
1 //===-- AMDGPUISelLowering.cpp - AMDGPU Common DAG lowering functions -----===//
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 This is the parent TargetLowering class for hardware code gen
12 /// targets.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "AMDGPUISelLowering.h"
17 #include "AMDGPU.h"
18 #include "AMDGPUFrameLowering.h"
19 #include "AMDGPURegisterInfo.h"
20 #include "AMDGPUSubtarget.h"
21 #include "AMDILIntrinsicInfo.h"
22 #include "R600MachineFunctionInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/CodeGen/CallingConvLower.h"
26 #include "llvm/CodeGen/MachineFunction.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAG.h"
29 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DiagnosticInfo.h"
32 #include "llvm/IR/DiagnosticPrinter.h"
33
34 using namespace llvm;
35
36 namespace {
37
38 /// Diagnostic information for unimplemented or unsupported feature reporting.
39 class DiagnosticInfoUnsupported : public DiagnosticInfo {
40 private:
41   const Twine &Description;
42   const Function &Fn;
43
44   static int KindID;
45
46   static int getKindID() {
47     if (KindID == 0)
48       KindID = llvm::getNextAvailablePluginDiagnosticKind();
49     return KindID;
50   }
51
52 public:
53   DiagnosticInfoUnsupported(const Function &Fn, const Twine &Desc,
54                           DiagnosticSeverity Severity = DS_Error)
55     : DiagnosticInfo(getKindID(), Severity),
56       Description(Desc),
57       Fn(Fn) { }
58
59   const Function &getFunction() const { return Fn; }
60   const Twine &getDescription() const { return Description; }
61
62   void print(DiagnosticPrinter &DP) const override {
63     DP << "unsupported " << getDescription() << " in " << Fn.getName();
64   }
65
66   static bool classof(const DiagnosticInfo *DI) {
67     return DI->getKind() == getKindID();
68   }
69 };
70
71 int DiagnosticInfoUnsupported::KindID = 0;
72 }
73
74
75 static bool allocateStack(unsigned ValNo, MVT ValVT, MVT LocVT,
76                       CCValAssign::LocInfo LocInfo,
77                       ISD::ArgFlagsTy ArgFlags, CCState &State) {
78   unsigned Offset = State.AllocateStack(ValVT.getStoreSize(),
79                                         ArgFlags.getOrigAlign());
80   State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
81
82   return true;
83 }
84
85 #include "AMDGPUGenCallingConv.inc"
86
87 AMDGPUTargetLowering::AMDGPUTargetLowering(TargetMachine &TM) :
88   TargetLowering(TM, new TargetLoweringObjectFileELF()) {
89
90   Subtarget = &TM.getSubtarget<AMDGPUSubtarget>();
91
92   // Initialize target lowering borrowed from AMDIL
93   InitAMDILLowering();
94
95   // We need to custom lower some of the intrinsics
96   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
97
98   // Library functions.  These default to Expand, but we have instructions
99   // for them.
100   setOperationAction(ISD::FCEIL,  MVT::f32, Legal);
101   setOperationAction(ISD::FEXP2,  MVT::f32, Legal);
102   setOperationAction(ISD::FPOW,   MVT::f32, Legal);
103   setOperationAction(ISD::FLOG2,  MVT::f32, Legal);
104   setOperationAction(ISD::FABS,   MVT::f32, Legal);
105   setOperationAction(ISD::FFLOOR, MVT::f32, Legal);
106   setOperationAction(ISD::FRINT,  MVT::f32, Legal);
107   setOperationAction(ISD::FROUND, MVT::f32, Legal);
108   setOperationAction(ISD::FTRUNC, MVT::f32, Legal);
109
110   // The hardware supports ROTR, but not ROTL
111   setOperationAction(ISD::ROTL, MVT::i32, Expand);
112
113   // Lower floating point store/load to integer store/load to reduce the number
114   // of patterns in tablegen.
115   setOperationAction(ISD::STORE, MVT::f32, Promote);
116   AddPromotedToType(ISD::STORE, MVT::f32, MVT::i32);
117
118   setOperationAction(ISD::STORE, MVT::v2f32, Promote);
119   AddPromotedToType(ISD::STORE, MVT::v2f32, MVT::v2i32);
120
121   setOperationAction(ISD::STORE, MVT::v4f32, Promote);
122   AddPromotedToType(ISD::STORE, MVT::v4f32, MVT::v4i32);
123
124   setOperationAction(ISD::STORE, MVT::v8f32, Promote);
125   AddPromotedToType(ISD::STORE, MVT::v8f32, MVT::v8i32);
126
127   setOperationAction(ISD::STORE, MVT::v16f32, Promote);
128   AddPromotedToType(ISD::STORE, MVT::v16f32, MVT::v16i32);
129
130   setOperationAction(ISD::STORE, MVT::f64, Promote);
131   AddPromotedToType(ISD::STORE, MVT::f64, MVT::i64);
132
133   setOperationAction(ISD::STORE, MVT::v2f64, Promote);
134   AddPromotedToType(ISD::STORE, MVT::v2f64, MVT::v2i64);
135
136   // Custom lowering of vector stores is required for local address space
137   // stores.
138   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
139   // XXX: Native v2i32 local address space stores are possible, but not
140   // currently implemented.
141   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
142
143   setTruncStoreAction(MVT::v2i32, MVT::v2i16, Custom);
144   setTruncStoreAction(MVT::v2i32, MVT::v2i8, Custom);
145   setTruncStoreAction(MVT::v4i32, MVT::v4i8, Custom);
146
147   // XXX: This can be change to Custom, once ExpandVectorStores can
148   // handle 64-bit stores.
149   setTruncStoreAction(MVT::v4i32, MVT::v4i16, Expand);
150
151   setTruncStoreAction(MVT::i64, MVT::i16, Expand);
152   setTruncStoreAction(MVT::i64, MVT::i8, Expand);
153   setTruncStoreAction(MVT::i64, MVT::i1, Expand);
154   setTruncStoreAction(MVT::v2i64, MVT::v2i1, Expand);
155   setTruncStoreAction(MVT::v4i64, MVT::v4i1, Expand);
156
157
158   setOperationAction(ISD::LOAD, MVT::f32, Promote);
159   AddPromotedToType(ISD::LOAD, MVT::f32, MVT::i32);
160
161   setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
162   AddPromotedToType(ISD::LOAD, MVT::v2f32, MVT::v2i32);
163
164   setOperationAction(ISD::LOAD, MVT::v4f32, Promote);
165   AddPromotedToType(ISD::LOAD, MVT::v4f32, MVT::v4i32);
166
167   setOperationAction(ISD::LOAD, MVT::v8f32, Promote);
168   AddPromotedToType(ISD::LOAD, MVT::v8f32, MVT::v8i32);
169
170   setOperationAction(ISD::LOAD, MVT::v16f32, Promote);
171   AddPromotedToType(ISD::LOAD, MVT::v16f32, MVT::v16i32);
172
173   setOperationAction(ISD::LOAD, MVT::f64, Promote);
174   AddPromotedToType(ISD::LOAD, MVT::f64, MVT::i64);
175
176   setOperationAction(ISD::LOAD, MVT::v2f64, Promote);
177   AddPromotedToType(ISD::LOAD, MVT::v2f64, MVT::v2i64);
178
179   setOperationAction(ISD::CONCAT_VECTORS, MVT::v4i32, Custom);
180   setOperationAction(ISD::CONCAT_VECTORS, MVT::v4f32, Custom);
181   setOperationAction(ISD::CONCAT_VECTORS, MVT::v8i32, Custom);
182   setOperationAction(ISD::CONCAT_VECTORS, MVT::v8f32, Custom);
183   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2f32, Custom);
184   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v2i32, Custom);
185   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4f32, Custom);
186   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v4i32, Custom);
187   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f32, Custom);
188   setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8i32, Custom);
189
190   setLoadExtAction(ISD::EXTLOAD, MVT::v2i8, Expand);
191   setLoadExtAction(ISD::SEXTLOAD, MVT::v2i8, Expand);
192   setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i8, Expand);
193   setLoadExtAction(ISD::EXTLOAD, MVT::v4i8, Expand);
194   setLoadExtAction(ISD::SEXTLOAD, MVT::v4i8, Expand);
195   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i8, Expand);
196   setLoadExtAction(ISD::EXTLOAD, MVT::v2i16, Expand);
197   setLoadExtAction(ISD::SEXTLOAD, MVT::v2i16, Expand);
198   setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i16, Expand);
199   setLoadExtAction(ISD::EXTLOAD, MVT::v4i16, Expand);
200   setLoadExtAction(ISD::SEXTLOAD, MVT::v4i16, Expand);
201   setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i16, Expand);
202
203   setOperationAction(ISD::BR_CC, MVT::i1, Expand);
204
205   setOperationAction(ISD::SELECT_CC, MVT::i64, Expand);
206
207   setOperationAction(ISD::FNEG, MVT::v2f32, Expand);
208   setOperationAction(ISD::FNEG, MVT::v4f32, Expand);
209
210   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
211
212   setOperationAction(ISD::MUL, MVT::i64, Expand);
213   setOperationAction(ISD::SUB, MVT::i64, Expand);
214
215   setOperationAction(ISD::UDIV, MVT::i32, Expand);
216   setOperationAction(ISD::UDIVREM, MVT::i32, Custom);
217   setOperationAction(ISD::UDIVREM, MVT::i64, Custom);
218   setOperationAction(ISD::UREM, MVT::i32, Expand);
219   setOperationAction(ISD::VSELECT, MVT::v2f32, Expand);
220   setOperationAction(ISD::VSELECT, MVT::v4f32, Expand);
221
222   static const MVT::SimpleValueType IntTypes[] = {
223     MVT::v2i32, MVT::v4i32
224   };
225
226   for (MVT VT : IntTypes) {
227     //Expand the following operations for the current type by default
228     setOperationAction(ISD::ADD,  VT, Expand);
229     setOperationAction(ISD::AND,  VT, Expand);
230     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
231     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
232     setOperationAction(ISD::MUL,  VT, Expand);
233     setOperationAction(ISD::OR,   VT, Expand);
234     setOperationAction(ISD::SHL,  VT, Expand);
235     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
236     setOperationAction(ISD::SRL,  VT, Expand);
237     setOperationAction(ISD::SRA,  VT, Expand);
238     setOperationAction(ISD::SUB,  VT, Expand);
239     setOperationAction(ISD::UDIV, VT, Expand);
240     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
241     setOperationAction(ISD::UREM, VT, Expand);
242     setOperationAction(ISD::SELECT, VT, Expand);
243     setOperationAction(ISD::VSELECT, VT, Expand);
244     setOperationAction(ISD::XOR,  VT, Expand);
245   }
246
247   static const MVT::SimpleValueType FloatTypes[] = {
248     MVT::v2f32, MVT::v4f32
249   };
250
251   for (MVT VT : FloatTypes) {
252     setOperationAction(ISD::FABS, VT, Expand);
253     setOperationAction(ISD::FADD, VT, Expand);
254     setOperationAction(ISD::FCOS, VT, Expand);
255     setOperationAction(ISD::FDIV, VT, Expand);
256     setOperationAction(ISD::FPOW, VT, Expand);
257     setOperationAction(ISD::FFLOOR, VT, Expand);
258     setOperationAction(ISD::FTRUNC, VT, Expand);
259     setOperationAction(ISD::FMUL, VT, Expand);
260     setOperationAction(ISD::FRINT, VT, Expand);
261     setOperationAction(ISD::FSQRT, VT, Expand);
262     setOperationAction(ISD::FSIN, VT, Expand);
263     setOperationAction(ISD::FSUB, VT, Expand);
264     setOperationAction(ISD::SELECT, VT, Expand);
265   }
266
267   setTargetDAGCombine(ISD::MUL);
268   setTargetDAGCombine(ISD::SELECT_CC);
269 }
270
271 //===----------------------------------------------------------------------===//
272 // Target Information
273 //===----------------------------------------------------------------------===//
274
275 MVT AMDGPUTargetLowering::getVectorIdxTy() const {
276   return MVT::i32;
277 }
278
279 bool AMDGPUTargetLowering::isLoadBitCastBeneficial(EVT LoadTy,
280                                                    EVT CastTy) const {
281   if (LoadTy.getSizeInBits() != CastTy.getSizeInBits())
282     return true;
283
284   unsigned LScalarSize = LoadTy.getScalarType().getSizeInBits();
285   unsigned CastScalarSize = CastTy.getScalarType().getSizeInBits();
286
287   return ((LScalarSize <= CastScalarSize) ||
288           (CastScalarSize >= 32) ||
289           (LScalarSize < 32));
290 }
291
292 //===---------------------------------------------------------------------===//
293 // Target Properties
294 //===---------------------------------------------------------------------===//
295
296 bool AMDGPUTargetLowering::isFAbsFree(EVT VT) const {
297   assert(VT.isFloatingPoint());
298   return VT == MVT::f32;
299 }
300
301 bool AMDGPUTargetLowering::isFNegFree(EVT VT) const {
302   assert(VT.isFloatingPoint());
303   return VT == MVT::f32;
304 }
305
306 bool AMDGPUTargetLowering::isTruncateFree(EVT Source, EVT Dest) const {
307   // Truncate is just accessing a subregister.
308   return Dest.bitsLT(Source) && (Dest.getSizeInBits() % 32 == 0);
309 }
310
311 bool AMDGPUTargetLowering::isTruncateFree(Type *Source, Type *Dest) const {
312   // Truncate is just accessing a subregister.
313   return Dest->getPrimitiveSizeInBits() < Source->getPrimitiveSizeInBits() &&
314          (Dest->getPrimitiveSizeInBits() % 32 == 0);
315 }
316
317 bool AMDGPUTargetLowering::isZExtFree(Type *Src, Type *Dest) const {
318   const DataLayout *DL = getDataLayout();
319   unsigned SrcSize = DL->getTypeSizeInBits(Src->getScalarType());
320   unsigned DestSize = DL->getTypeSizeInBits(Dest->getScalarType());
321
322   return SrcSize == 32 && DestSize == 64;
323 }
324
325 bool AMDGPUTargetLowering::isZExtFree(EVT Src, EVT Dest) const {
326   // Any register load of a 64-bit value really requires 2 32-bit moves. For all
327   // practical purposes, the extra mov 0 to load a 64-bit is free.  As used,
328   // this will enable reducing 64-bit operations the 32-bit, which is always
329   // good.
330   return Src == MVT::i32 && Dest == MVT::i64;
331 }
332
333 bool AMDGPUTargetLowering::isNarrowingProfitable(EVT SrcVT, EVT DestVT) const {
334   // There aren't really 64-bit registers, but pairs of 32-bit ones and only a
335   // limited number of native 64-bit operations. Shrinking an operation to fit
336   // in a single 32-bit register should always be helpful. As currently used,
337   // this is much less general than the name suggests, and is only used in
338   // places trying to reduce the sizes of loads. Shrinking loads to < 32-bits is
339   // not profitable, and may actually be harmful.
340   return SrcVT.getSizeInBits() > 32 && DestVT.getSizeInBits() == 32;
341 }
342
343 //===---------------------------------------------------------------------===//
344 // TargetLowering Callbacks
345 //===---------------------------------------------------------------------===//
346
347 void AMDGPUTargetLowering::AnalyzeFormalArguments(CCState &State,
348                              const SmallVectorImpl<ISD::InputArg> &Ins) const {
349
350   State.AnalyzeFormalArguments(Ins, CC_AMDGPU);
351 }
352
353 SDValue AMDGPUTargetLowering::LowerReturn(
354                                      SDValue Chain,
355                                      CallingConv::ID CallConv,
356                                      bool isVarArg,
357                                      const SmallVectorImpl<ISD::OutputArg> &Outs,
358                                      const SmallVectorImpl<SDValue> &OutVals,
359                                      SDLoc DL, SelectionDAG &DAG) const {
360   return DAG.getNode(AMDGPUISD::RET_FLAG, DL, MVT::Other, Chain);
361 }
362
363 //===---------------------------------------------------------------------===//
364 // Target specific lowering
365 //===---------------------------------------------------------------------===//
366
367 SDValue AMDGPUTargetLowering::LowerCall(CallLoweringInfo &CLI,
368                                         SmallVectorImpl<SDValue> &InVals) const {
369   SDValue Callee = CLI.Callee;
370   SelectionDAG &DAG = CLI.DAG;
371
372   const Function &Fn = *DAG.getMachineFunction().getFunction();
373
374   StringRef FuncName("<unknown>");
375
376   if (const ExternalSymbolSDNode *G = dyn_cast<ExternalSymbolSDNode>(Callee))
377     FuncName = G->getSymbol();
378   else if (const GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
379     FuncName = G->getGlobal()->getName();
380
381   DiagnosticInfoUnsupported NoCalls(Fn, "call to function " + FuncName);
382   DAG.getContext()->diagnose(NoCalls);
383   return SDValue();
384 }
385
386 SDValue AMDGPUTargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG)
387     const {
388   switch (Op.getOpcode()) {
389   default:
390     Op.getNode()->dump();
391     llvm_unreachable("Custom lowering code for this"
392                      "instruction is not implemented yet!");
393     break;
394   // AMDIL DAG lowering
395   case ISD::SDIV: return LowerSDIV(Op, DAG);
396   case ISD::SREM: return LowerSREM(Op, DAG);
397   case ISD::SIGN_EXTEND_INREG: return LowerSIGN_EXTEND_INREG(Op, DAG);
398   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
399   // AMDGPU DAG lowering
400   case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, DAG);
401   case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op, DAG);
402   case ISD::FrameIndex: return LowerFrameIndex(Op, DAG);
403   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
404   case ISD::UDIVREM: return LowerUDIVREM(Op, DAG);
405   case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
406   }
407   return Op;
408 }
409
410 void AMDGPUTargetLowering::ReplaceNodeResults(SDNode *N,
411                                               SmallVectorImpl<SDValue> &Results,
412                                               SelectionDAG &DAG) const {
413   switch (N->getOpcode()) {
414   case ISD::SIGN_EXTEND_INREG:
415     // Different parts of legalization seem to interpret which type of
416     // sign_extend_inreg is the one to check for custom lowering. The extended
417     // from type is what really matters, but some places check for custom
418     // lowering of the result type. This results in trying to use
419     // ReplaceNodeResults to sext_in_reg to an illegal type, so we'll just do
420     // nothing here and let the illegal result integer be handled normally.
421     return;
422   case ISD::UDIV: {
423     SDValue Op = SDValue(N, 0);
424     SDLoc DL(Op);
425     EVT VT = Op.getValueType();
426     SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
427       N->getOperand(0), N->getOperand(1));
428     Results.push_back(UDIVREM);
429     break;
430   }
431   case ISD::UREM: {
432     SDValue Op = SDValue(N, 0);
433     SDLoc DL(Op);
434     EVT VT = Op.getValueType();
435     SDValue UDIVREM = DAG.getNode(ISD::UDIVREM, DL, DAG.getVTList(VT, VT),
436       N->getOperand(0), N->getOperand(1));
437     Results.push_back(UDIVREM.getValue(1));
438     break;
439   }
440   case ISD::UDIVREM: {
441     SDValue Op = SDValue(N, 0);
442     SDLoc DL(Op);
443     EVT VT = Op.getValueType();
444     EVT HalfVT = VT.getHalfSizedIntegerVT(*DAG.getContext());
445
446     SDValue one = DAG.getConstant(1, HalfVT);
447     SDValue zero = DAG.getConstant(0, HalfVT);
448
449     //HiLo split
450     SDValue LHS = N->getOperand(0);
451     SDValue LHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, zero);
452     SDValue LHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, LHS, one);
453
454     SDValue RHS = N->getOperand(1);
455     SDValue RHS_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, zero);
456     SDValue RHS_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, RHS, one);
457
458     // Get Speculative values
459     SDValue DIV_Part = DAG.getNode(ISD::UDIV, DL, HalfVT, LHS_Hi, RHS_Lo);
460     SDValue REM_Part = DAG.getNode(ISD::UREM, DL, HalfVT, LHS_Hi, RHS_Lo);
461
462     SDValue REM_Hi = zero;
463     SDValue REM_Lo = DAG.getSelectCC(DL, RHS_Hi, zero, REM_Part, LHS_Hi, ISD::SETEQ);
464
465     SDValue DIV_Hi = DAG.getSelectCC(DL, RHS_Hi, zero, DIV_Part, zero, ISD::SETEQ);
466     SDValue DIV_Lo = zero;
467
468     const unsigned halfBitWidth = HalfVT.getSizeInBits();
469
470     for (unsigned i = 0; i < halfBitWidth; ++i) {
471       SDValue POS = DAG.getConstant(halfBitWidth - i - 1, HalfVT);
472       // Get Value of high bit
473       SDValue HBit;
474       if (halfBitWidth == 32 && Subtarget->hasBFE()) {
475         HBit = DAG.getNode(AMDGPUISD::BFE_U32, DL, HalfVT, LHS_Lo, POS, one);
476       } else {
477         HBit = DAG.getNode(ISD::SRL, DL, HalfVT, LHS_Lo, POS);
478         HBit = DAG.getNode(ISD::AND, DL, HalfVT, HBit, one);
479       }
480
481       SDValue Carry = DAG.getNode(ISD::SRL, DL, HalfVT, REM_Lo,
482         DAG.getConstant(halfBitWidth - 1, HalfVT));
483       REM_Hi = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Hi, one);
484       REM_Hi = DAG.getNode(ISD::OR, DL, HalfVT, REM_Hi, Carry);
485
486       REM_Lo = DAG.getNode(ISD::SHL, DL, HalfVT, REM_Lo, one);
487       REM_Lo = DAG.getNode(ISD::OR, DL, HalfVT, REM_Lo, HBit);
488
489
490       SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
491
492       SDValue BIT = DAG.getConstant(1 << (halfBitWidth - i - 1), HalfVT);
493       SDValue realBIT = DAG.getSelectCC(DL, REM, RHS, BIT, zero, ISD::SETGE);
494
495       DIV_Lo = DAG.getNode(ISD::OR, DL, HalfVT, DIV_Lo, realBIT);
496
497       // Update REM
498
499       SDValue REM_sub = DAG.getNode(ISD::SUB, DL, VT, REM, RHS);
500
501       REM = DAG.getSelectCC(DL, REM, RHS, REM_sub, REM, ISD::SETGE);
502       REM_Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, zero);
503       REM_Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, HalfVT, REM, one);
504     }
505
506     SDValue REM = DAG.getNode(ISD::BUILD_PAIR, DL, VT, REM_Lo, REM_Hi);
507     SDValue DIV = DAG.getNode(ISD::BUILD_PAIR, DL, VT, DIV_Lo, DIV_Hi);
508     Results.push_back(DIV);
509     Results.push_back(REM);
510     break;
511   }
512   default:
513     return;
514   }
515 }
516
517 SDValue AMDGPUTargetLowering::LowerConstantInitializer(const Constant* Init,
518                                                        const GlobalValue *GV,
519                                                        const SDValue &InitPtr,
520                                                        SDValue Chain,
521                                                        SelectionDAG &DAG) const {
522   const DataLayout *TD = getTargetMachine().getDataLayout();
523   SDLoc DL(InitPtr);
524   if (const ConstantInt *CI = dyn_cast<ConstantInt>(Init)) {
525     EVT VT = EVT::getEVT(CI->getType());
526     PointerType *PtrTy = PointerType::get(CI->getType(), 0);
527     return DAG.getStore(Chain, DL,  DAG.getConstant(*CI, VT), InitPtr,
528                  MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
529                  TD->getPrefTypeAlignment(CI->getType()));
530   }
531
532   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Init)) {
533     EVT VT = EVT::getEVT(CFP->getType());
534     PointerType *PtrTy = PointerType::get(CFP->getType(), 0);
535     return DAG.getStore(Chain, DL, DAG.getConstantFP(*CFP, VT), InitPtr,
536                  MachinePointerInfo(UndefValue::get(PtrTy)), false, false,
537                  TD->getPrefTypeAlignment(CFP->getType()));
538   }
539
540   if (Init->getType()->isAggregateType()) {
541     EVT PtrVT = InitPtr.getValueType();
542     unsigned NumElements = Init->getType()->getArrayNumElements();
543     SmallVector<SDValue, 8> Chains;
544     for (unsigned i = 0; i < NumElements; ++i) {
545       SDValue Offset = DAG.getConstant(i * TD->getTypeAllocSize(
546           Init->getType()->getArrayElementType()), PtrVT);
547       SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, InitPtr, Offset);
548       Chains.push_back(LowerConstantInitializer(Init->getAggregateElement(i),
549                        GV, Ptr, Chain, DAG));
550     }
551
552     return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
553   }
554
555   Init->dump();
556   llvm_unreachable("Unhandled constant initializer");
557 }
558
559 SDValue AMDGPUTargetLowering::LowerGlobalAddress(AMDGPUMachineFunction* MFI,
560                                                  SDValue Op,
561                                                  SelectionDAG &DAG) const {
562
563   const DataLayout *TD = getTargetMachine().getDataLayout();
564   GlobalAddressSDNode *G = cast<GlobalAddressSDNode>(Op);
565   const GlobalValue *GV = G->getGlobal();
566
567   switch (G->getAddressSpace()) {
568   default: llvm_unreachable("Global Address lowering not implemented for this "
569                             "address space");
570   case AMDGPUAS::LOCAL_ADDRESS: {
571     // XXX: What does the value of G->getOffset() mean?
572     assert(G->getOffset() == 0 &&
573          "Do not know what to do with an non-zero offset");
574
575     unsigned Offset;
576     if (MFI->LocalMemoryObjects.count(GV) == 0) {
577       uint64_t Size = TD->getTypeAllocSize(GV->getType()->getElementType());
578       Offset = MFI->LDSSize;
579       MFI->LocalMemoryObjects[GV] = Offset;
580       // XXX: Account for alignment?
581       MFI->LDSSize += Size;
582     } else {
583       Offset = MFI->LocalMemoryObjects[GV];
584     }
585
586     return DAG.getConstant(Offset, getPointerTy(G->getAddressSpace()));
587   }
588   case AMDGPUAS::CONSTANT_ADDRESS: {
589     MachineFrameInfo *FrameInfo = DAG.getMachineFunction().getFrameInfo();
590     Type *EltType = GV->getType()->getElementType();
591     unsigned Size = TD->getTypeAllocSize(EltType);
592     unsigned Alignment = TD->getPrefTypeAlignment(EltType);
593
594     const GlobalVariable *Var = cast<GlobalVariable>(GV);
595     const Constant *Init = Var->getInitializer();
596     int FI = FrameInfo->CreateStackObject(Size, Alignment, false);
597     SDValue InitPtr = DAG.getFrameIndex(FI,
598         getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
599     SmallVector<SDNode*, 8> WorkList;
600
601     for (SDNode::use_iterator I = DAG.getEntryNode()->use_begin(),
602                               E = DAG.getEntryNode()->use_end(); I != E; ++I) {
603       if (I->getOpcode() != AMDGPUISD::REGISTER_LOAD && I->getOpcode() != ISD::LOAD)
604         continue;
605       WorkList.push_back(*I);
606     }
607     SDValue Chain = LowerConstantInitializer(Init, GV, InitPtr, DAG.getEntryNode(), DAG);
608     for (SmallVector<SDNode*, 8>::iterator I = WorkList.begin(),
609                                            E = WorkList.end(); I != E; ++I) {
610       SmallVector<SDValue, 8> Ops;
611       Ops.push_back(Chain);
612       for (unsigned i = 1; i < (*I)->getNumOperands(); ++i) {
613         Ops.push_back((*I)->getOperand(i));
614       }
615       DAG.UpdateNodeOperands(*I, Ops);
616     }
617     return DAG.getZExtOrTrunc(InitPtr, SDLoc(Op),
618         getPointerTy(AMDGPUAS::CONSTANT_ADDRESS));
619   }
620   }
621 }
622
623 SDValue AMDGPUTargetLowering::LowerCONCAT_VECTORS(SDValue Op,
624                                                   SelectionDAG &DAG) const {
625   SmallVector<SDValue, 8> Args;
626   SDValue A = Op.getOperand(0);
627   SDValue B = Op.getOperand(1);
628
629   DAG.ExtractVectorElements(A, Args);
630   DAG.ExtractVectorElements(B, Args);
631
632   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
633 }
634
635 SDValue AMDGPUTargetLowering::LowerEXTRACT_SUBVECTOR(SDValue Op,
636                                                      SelectionDAG &DAG) const {
637
638   SmallVector<SDValue, 8> Args;
639   unsigned Start = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
640   EVT VT = Op.getValueType();
641   DAG.ExtractVectorElements(Op.getOperand(0), Args, Start,
642                             VT.getVectorNumElements());
643
644   return DAG.getNode(ISD::BUILD_VECTOR, SDLoc(Op), Op.getValueType(), Args);
645 }
646
647 SDValue AMDGPUTargetLowering::LowerFrameIndex(SDValue Op,
648                                               SelectionDAG &DAG) const {
649
650   MachineFunction &MF = DAG.getMachineFunction();
651   const AMDGPUFrameLowering *TFL =
652    static_cast<const AMDGPUFrameLowering*>(getTargetMachine().getFrameLowering());
653
654   FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Op);
655   assert(FIN);
656
657   unsigned FrameIndex = FIN->getIndex();
658   unsigned Offset = TFL->getFrameIndexOffset(MF, FrameIndex);
659   return DAG.getConstant(Offset * 4 * TFL->getStackWidth(MF),
660                          Op.getValueType());
661 }
662
663 SDValue AMDGPUTargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op,
664     SelectionDAG &DAG) const {
665   unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
666   SDLoc DL(Op);
667   EVT VT = Op.getValueType();
668
669   switch (IntrinsicID) {
670     default: return Op;
671     case AMDGPUIntrinsic::AMDIL_abs:
672       return LowerIntrinsicIABS(Op, DAG);
673     case AMDGPUIntrinsic::AMDIL_exp:
674       return DAG.getNode(ISD::FEXP2, DL, VT, Op.getOperand(1));
675     case AMDGPUIntrinsic::AMDGPU_lrp:
676       return LowerIntrinsicLRP(Op, DAG);
677     case AMDGPUIntrinsic::AMDIL_fraction:
678       return DAG.getNode(AMDGPUISD::FRACT, DL, VT, Op.getOperand(1));
679     case AMDGPUIntrinsic::AMDIL_max:
680       return DAG.getNode(AMDGPUISD::FMAX, DL, VT, Op.getOperand(1),
681                                                   Op.getOperand(2));
682     case AMDGPUIntrinsic::AMDGPU_imax:
683       return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Op.getOperand(1),
684                                                   Op.getOperand(2));
685     case AMDGPUIntrinsic::AMDGPU_umax:
686       return DAG.getNode(AMDGPUISD::UMAX, DL, VT, Op.getOperand(1),
687                                                   Op.getOperand(2));
688     case AMDGPUIntrinsic::AMDIL_min:
689       return DAG.getNode(AMDGPUISD::FMIN, DL, VT, Op.getOperand(1),
690                                                   Op.getOperand(2));
691     case AMDGPUIntrinsic::AMDGPU_imin:
692       return DAG.getNode(AMDGPUISD::SMIN, DL, VT, Op.getOperand(1),
693                                                   Op.getOperand(2));
694     case AMDGPUIntrinsic::AMDGPU_umin:
695       return DAG.getNode(AMDGPUISD::UMIN, DL, VT, Op.getOperand(1),
696                                                   Op.getOperand(2));
697
698     case AMDGPUIntrinsic::AMDGPU_umul24:
699       return DAG.getNode(AMDGPUISD::MUL_U24, DL, VT,
700                          Op.getOperand(1), Op.getOperand(2));
701
702     case AMDGPUIntrinsic::AMDGPU_imul24:
703       return DAG.getNode(AMDGPUISD::MUL_I24, DL, VT,
704                          Op.getOperand(1), Op.getOperand(2));
705
706     case AMDGPUIntrinsic::AMDGPU_bfe_i32:
707       return DAG.getNode(AMDGPUISD::BFE_I32, DL, VT,
708                          Op.getOperand(1),
709                          Op.getOperand(2),
710                          Op.getOperand(3));
711
712     case AMDGPUIntrinsic::AMDGPU_bfe_u32:
713       return DAG.getNode(AMDGPUISD::BFE_U32, DL, VT,
714                          Op.getOperand(1),
715                          Op.getOperand(2),
716                          Op.getOperand(3));
717
718     case AMDGPUIntrinsic::AMDGPU_bfi:
719       return DAG.getNode(AMDGPUISD::BFI, DL, VT,
720                          Op.getOperand(1),
721                          Op.getOperand(2),
722                          Op.getOperand(3));
723
724     case AMDGPUIntrinsic::AMDGPU_bfm:
725       return DAG.getNode(AMDGPUISD::BFM, DL, VT,
726                          Op.getOperand(1),
727                          Op.getOperand(2));
728
729     case AMDGPUIntrinsic::AMDIL_round_nearest:
730       return DAG.getNode(ISD::FRINT, DL, VT, Op.getOperand(1));
731   }
732 }
733
734 ///IABS(a) = SMAX(sub(0, a), a)
735 SDValue AMDGPUTargetLowering::LowerIntrinsicIABS(SDValue Op,
736                                                  SelectionDAG &DAG) const {
737   SDLoc DL(Op);
738   EVT VT = Op.getValueType();
739   SDValue Neg = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
740                                               Op.getOperand(1));
741
742   return DAG.getNode(AMDGPUISD::SMAX, DL, VT, Neg, Op.getOperand(1));
743 }
744
745 /// Linear Interpolation
746 /// LRP(a, b, c) = muladd(a,  b, (1 - a) * c)
747 SDValue AMDGPUTargetLowering::LowerIntrinsicLRP(SDValue Op,
748                                                 SelectionDAG &DAG) const {
749   SDLoc DL(Op);
750   EVT VT = Op.getValueType();
751   SDValue OneSubA = DAG.getNode(ISD::FSUB, DL, VT,
752                                 DAG.getConstantFP(1.0f, MVT::f32),
753                                 Op.getOperand(1));
754   SDValue OneSubAC = DAG.getNode(ISD::FMUL, DL, VT, OneSubA,
755                                                     Op.getOperand(3));
756   return DAG.getNode(ISD::FADD, DL, VT,
757       DAG.getNode(ISD::FMUL, DL, VT, Op.getOperand(1), Op.getOperand(2)),
758       OneSubAC);
759 }
760
761 /// \brief Generate Min/Max node
762 SDValue AMDGPUTargetLowering::CombineMinMax(SDNode *N,
763                                             SelectionDAG &DAG) const {
764   SDLoc DL(N);
765   EVT VT = N->getValueType(0);
766
767   SDValue LHS = N->getOperand(0);
768   SDValue RHS = N->getOperand(1);
769   SDValue True = N->getOperand(2);
770   SDValue False = N->getOperand(3);
771   SDValue CC = N->getOperand(4);
772
773   if (VT != MVT::f32 ||
774       !((LHS == True && RHS == False) || (LHS == False && RHS == True))) {
775     return SDValue();
776   }
777
778   ISD::CondCode CCOpcode = cast<CondCodeSDNode>(CC)->get();
779   switch (CCOpcode) {
780   case ISD::SETOEQ:
781   case ISD::SETONE:
782   case ISD::SETUNE:
783   case ISD::SETNE:
784   case ISD::SETUEQ:
785   case ISD::SETEQ:
786   case ISD::SETFALSE:
787   case ISD::SETFALSE2:
788   case ISD::SETTRUE:
789   case ISD::SETTRUE2:
790   case ISD::SETUO:
791   case ISD::SETO:
792     llvm_unreachable("Operation should already be optimised!");
793   case ISD::SETULE:
794   case ISD::SETULT:
795   case ISD::SETOLE:
796   case ISD::SETOLT:
797   case ISD::SETLE:
798   case ISD::SETLT: {
799     unsigned Opc = (LHS == True) ? AMDGPUISD::FMIN : AMDGPUISD::FMAX;
800     return DAG.getNode(Opc, DL, VT, LHS, RHS);
801   }
802   case ISD::SETGT:
803   case ISD::SETGE:
804   case ISD::SETUGE:
805   case ISD::SETOGE:
806   case ISD::SETUGT:
807   case ISD::SETOGT: {
808     unsigned Opc = (LHS == True) ? AMDGPUISD::FMAX : AMDGPUISD::FMIN;
809     return DAG.getNode(Opc, DL, VT, LHS, RHS);
810   }
811   case ISD::SETCC_INVALID:
812     llvm_unreachable("Invalid setcc condcode!");
813   }
814   return SDValue();
815 }
816
817 SDValue AMDGPUTargetLowering::SplitVectorLoad(const SDValue &Op,
818                                               SelectionDAG &DAG) const {
819   LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
820   EVT MemEltVT = Load->getMemoryVT().getVectorElementType();
821   EVT EltVT = Op.getValueType().getVectorElementType();
822   EVT PtrVT = Load->getBasePtr().getValueType();
823   unsigned NumElts = Load->getMemoryVT().getVectorNumElements();
824   SmallVector<SDValue, 8> Loads;
825   SDLoc SL(Op);
826
827   for (unsigned i = 0, e = NumElts; i != e; ++i) {
828     SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT, Load->getBasePtr(),
829                     DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8), PtrVT));
830     Loads.push_back(DAG.getExtLoad(Load->getExtensionType(), SL, EltVT,
831                         Load->getChain(), Ptr,
832                         MachinePointerInfo(Load->getMemOperand()->getValue()),
833                         MemEltVT, Load->isVolatile(), Load->isNonTemporal(),
834                         Load->getAlignment()));
835   }
836   return DAG.getNode(ISD::BUILD_VECTOR, SL, Op.getValueType(), Loads);
837 }
838
839 SDValue AMDGPUTargetLowering::MergeVectorStore(const SDValue &Op,
840                                                SelectionDAG &DAG) const {
841   StoreSDNode *Store = dyn_cast<StoreSDNode>(Op);
842   EVT MemVT = Store->getMemoryVT();
843   unsigned MemBits = MemVT.getSizeInBits();
844
845   // Byte stores are really expensive, so if possible, try to pack 32-bit vector
846   // truncating store into an i32 store.
847   // XXX: We could also handle optimize other vector bitwidths.
848   if (!MemVT.isVector() || MemBits > 32) {
849     return SDValue();
850   }
851
852   SDLoc DL(Op);
853   SDValue Value = Store->getValue();
854   EVT VT = Value.getValueType();
855   EVT ElemVT = VT.getVectorElementType();
856   SDValue Ptr = Store->getBasePtr();
857   EVT MemEltVT = MemVT.getVectorElementType();
858   unsigned MemEltBits = MemEltVT.getSizeInBits();
859   unsigned MemNumElements = MemVT.getVectorNumElements();
860   unsigned PackedSize = MemVT.getStoreSizeInBits();
861   SDValue Mask = DAG.getConstant((1 << MemEltBits) - 1, MVT::i32);
862
863   assert(Value.getValueType().getScalarSizeInBits() >= 32);
864
865   SDValue PackedValue;
866   for (unsigned i = 0; i < MemNumElements; ++i) {
867     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ElemVT, Value,
868                               DAG.getConstant(i, MVT::i32));
869     Elt = DAG.getZExtOrTrunc(Elt, DL, MVT::i32);
870     Elt = DAG.getNode(ISD::AND, DL, MVT::i32, Elt, Mask); // getZeroExtendInReg
871
872     SDValue Shift = DAG.getConstant(MemEltBits * i, MVT::i32);
873     Elt = DAG.getNode(ISD::SHL, DL, MVT::i32, Elt, Shift);
874
875     if (i == 0) {
876       PackedValue = Elt;
877     } else {
878       PackedValue = DAG.getNode(ISD::OR, DL, MVT::i32, PackedValue, Elt);
879     }
880   }
881
882   if (PackedSize < 32) {
883     EVT PackedVT = EVT::getIntegerVT(*DAG.getContext(), PackedSize);
884     return DAG.getTruncStore(Store->getChain(), DL, PackedValue, Ptr,
885                              Store->getMemOperand()->getPointerInfo(),
886                              PackedVT,
887                              Store->isNonTemporal(), Store->isVolatile(),
888                              Store->getAlignment());
889   }
890
891   return DAG.getStore(Store->getChain(), DL, PackedValue, Ptr,
892                       Store->getMemOperand()->getPointerInfo(),
893                       Store->isVolatile(),  Store->isNonTemporal(),
894                       Store->getAlignment());
895 }
896
897 SDValue AMDGPUTargetLowering::SplitVectorStore(SDValue Op,
898                                             SelectionDAG &DAG) const {
899   StoreSDNode *Store = cast<StoreSDNode>(Op);
900   EVT MemEltVT = Store->getMemoryVT().getVectorElementType();
901   EVT EltVT = Store->getValue().getValueType().getVectorElementType();
902   EVT PtrVT = Store->getBasePtr().getValueType();
903   unsigned NumElts = Store->getMemoryVT().getVectorNumElements();
904   SDLoc SL(Op);
905
906   SmallVector<SDValue, 8> Chains;
907
908   for (unsigned i = 0, e = NumElts; i != e; ++i) {
909     SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SL, EltVT,
910                               Store->getValue(), DAG.getConstant(i, MVT::i32));
911     SDValue Ptr = DAG.getNode(ISD::ADD, SL, PtrVT,
912                               Store->getBasePtr(),
913                             DAG.getConstant(i * (MemEltVT.getSizeInBits() / 8),
914                                             PtrVT));
915     Chains.push_back(DAG.getTruncStore(Store->getChain(), SL, Val, Ptr,
916                          MachinePointerInfo(Store->getMemOperand()->getValue()),
917                          MemEltVT, Store->isVolatile(), Store->isNonTemporal(),
918                          Store->getAlignment()));
919   }
920   return DAG.getNode(ISD::TokenFactor, SL, MVT::Other, Chains);
921 }
922
923 SDValue AMDGPUTargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
924   SDLoc DL(Op);
925   LoadSDNode *Load = cast<LoadSDNode>(Op);
926   ISD::LoadExtType ExtType = Load->getExtensionType();
927   EVT VT = Op.getValueType();
928   EVT MemVT = Load->getMemoryVT();
929
930   if (ExtType != ISD::NON_EXTLOAD && !VT.isVector() && VT.getSizeInBits() > 32) {
931     // We can do the extload to 32-bits, and then need to separately extend to
932     // 64-bits.
933
934     SDValue ExtLoad32 = DAG.getExtLoad(ExtType, DL, MVT::i32,
935                                        Load->getChain(),
936                                        Load->getBasePtr(),
937                                        MemVT,
938                                        Load->getMemOperand());
939     return DAG.getNode(ISD::getExtForLoadExtType(ExtType), DL, VT, ExtLoad32);
940   }
941
942   if (ExtType == ISD::NON_EXTLOAD && VT.getSizeInBits() < 32) {
943     assert(VT == MVT::i1 && "Only i1 non-extloads expected");
944     // FIXME: Copied from PPC
945     // First, load into 32 bits, then truncate to 1 bit.
946
947     SDValue Chain = Load->getChain();
948     SDValue BasePtr = Load->getBasePtr();
949     MachineMemOperand *MMO = Load->getMemOperand();
950
951     SDValue NewLD = DAG.getExtLoad(ISD::EXTLOAD, DL, MVT::i32, Chain,
952                                    BasePtr, MVT::i8, MMO);
953     return DAG.getNode(ISD::TRUNCATE, DL, VT, NewLD);
954   }
955
956   // Lower loads constant address space global variable loads
957   if (Load->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS &&
958       isa<GlobalVariable>(
959           GetUnderlyingObject(Load->getMemOperand()->getValue()))) {
960
961     SDValue Ptr = DAG.getZExtOrTrunc(Load->getBasePtr(), DL,
962         getPointerTy(AMDGPUAS::PRIVATE_ADDRESS));
963     Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Ptr,
964         DAG.getConstant(2, MVT::i32));
965     return DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
966                        Load->getChain(), Ptr,
967                        DAG.getTargetConstant(0, MVT::i32), Op.getOperand(2));
968   }
969
970   if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS ||
971       ExtType == ISD::NON_EXTLOAD || Load->getMemoryVT().bitsGE(MVT::i32))
972     return SDValue();
973
974
975   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
976                             DAG.getConstant(2, MVT::i32));
977   SDValue Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, Op.getValueType(),
978                             Load->getChain(), Ptr,
979                             DAG.getTargetConstant(0, MVT::i32),
980                             Op.getOperand(2));
981   SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32,
982                                 Load->getBasePtr(),
983                                 DAG.getConstant(0x3, MVT::i32));
984   SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
985                                  DAG.getConstant(3, MVT::i32));
986
987   Ret = DAG.getNode(ISD::SRL, DL, MVT::i32, Ret, ShiftAmt);
988
989   EVT MemEltVT = MemVT.getScalarType();
990   if (ExtType == ISD::SEXTLOAD) {
991     SDValue MemEltVTNode = DAG.getValueType(MemEltVT);
992     return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, MVT::i32, Ret, MemEltVTNode);
993   }
994
995   return DAG.getZeroExtendInReg(Ret, DL, MemEltVT);
996 }
997
998 SDValue AMDGPUTargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
999   SDLoc DL(Op);
1000   SDValue Result = AMDGPUTargetLowering::MergeVectorStore(Op, DAG);
1001   if (Result.getNode()) {
1002     return Result;
1003   }
1004
1005   StoreSDNode *Store = cast<StoreSDNode>(Op);
1006   SDValue Chain = Store->getChain();
1007   if ((Store->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
1008        Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS) &&
1009       Store->getValue().getValueType().isVector()) {
1010     return SplitVectorStore(Op, DAG);
1011   }
1012
1013   EVT MemVT = Store->getMemoryVT();
1014   if (Store->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS &&
1015       MemVT.bitsLT(MVT::i32)) {
1016     unsigned Mask = 0;
1017     if (Store->getMemoryVT() == MVT::i8) {
1018       Mask = 0xff;
1019     } else if (Store->getMemoryVT() == MVT::i16) {
1020       Mask = 0xffff;
1021     }
1022     SDValue BasePtr = Store->getBasePtr();
1023     SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, BasePtr,
1024                               DAG.getConstant(2, MVT::i32));
1025     SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
1026                               Chain, Ptr, DAG.getTargetConstant(0, MVT::i32));
1027
1028     SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, BasePtr,
1029                                   DAG.getConstant(0x3, MVT::i32));
1030
1031     SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
1032                                    DAG.getConstant(3, MVT::i32));
1033
1034     SDValue SExtValue = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i32,
1035                                     Store->getValue());
1036
1037     SDValue MaskedValue = DAG.getZeroExtendInReg(SExtValue, DL, MemVT);
1038
1039     SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
1040                                        MaskedValue, ShiftAmt);
1041
1042     SDValue DstMask = DAG.getNode(ISD::SHL, DL, MVT::i32, DAG.getConstant(Mask, MVT::i32),
1043                                   ShiftAmt);
1044     DstMask = DAG.getNode(ISD::XOR, DL, MVT::i32, DstMask,
1045                           DAG.getConstant(0xffffffff, MVT::i32));
1046     Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
1047
1048     SDValue Value = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
1049     return DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
1050                        Chain, Value, Ptr, DAG.getTargetConstant(0, MVT::i32));
1051   }
1052   return SDValue();
1053 }
1054
1055 SDValue AMDGPUTargetLowering::LowerUDIVREM(SDValue Op,
1056                                            SelectionDAG &DAG) const {
1057   SDLoc DL(Op);
1058   EVT VT = Op.getValueType();
1059
1060   SDValue Num = Op.getOperand(0);
1061   SDValue Den = Op.getOperand(1);
1062
1063   // RCP =  URECIP(Den) = 2^32 / Den + e
1064   // e is rounding error.
1065   SDValue RCP = DAG.getNode(AMDGPUISD::URECIP, DL, VT, Den);
1066
1067   // RCP_LO = umulo(RCP, Den) */
1068   SDValue RCP_LO = DAG.getNode(ISD::UMULO, DL, VT, RCP, Den);
1069
1070   // RCP_HI = mulhu (RCP, Den) */
1071   SDValue RCP_HI = DAG.getNode(ISD::MULHU, DL, VT, RCP, Den);
1072
1073   // NEG_RCP_LO = -RCP_LO
1074   SDValue NEG_RCP_LO = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, VT),
1075                                                      RCP_LO);
1076
1077   // ABS_RCP_LO = (RCP_HI == 0 ? NEG_RCP_LO : RCP_LO)
1078   SDValue ABS_RCP_LO = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1079                                            NEG_RCP_LO, RCP_LO,
1080                                            ISD::SETEQ);
1081   // Calculate the rounding error from the URECIP instruction
1082   // E = mulhu(ABS_RCP_LO, RCP)
1083   SDValue E = DAG.getNode(ISD::MULHU, DL, VT, ABS_RCP_LO, RCP);
1084
1085   // RCP_A_E = RCP + E
1086   SDValue RCP_A_E = DAG.getNode(ISD::ADD, DL, VT, RCP, E);
1087
1088   // RCP_S_E = RCP - E
1089   SDValue RCP_S_E = DAG.getNode(ISD::SUB, DL, VT, RCP, E);
1090
1091   // Tmp0 = (RCP_HI == 0 ? RCP_A_E : RCP_SUB_E)
1092   SDValue Tmp0 = DAG.getSelectCC(DL, RCP_HI, DAG.getConstant(0, VT),
1093                                      RCP_A_E, RCP_S_E,
1094                                      ISD::SETEQ);
1095   // Quotient = mulhu(Tmp0, Num)
1096   SDValue Quotient = DAG.getNode(ISD::MULHU, DL, VT, Tmp0, Num);
1097
1098   // Num_S_Remainder = Quotient * Den
1099   SDValue Num_S_Remainder = DAG.getNode(ISD::UMULO, DL, VT, Quotient, Den);
1100
1101   // Remainder = Num - Num_S_Remainder
1102   SDValue Remainder = DAG.getNode(ISD::SUB, DL, VT, Num, Num_S_Remainder);
1103
1104   // Remainder_GE_Den = (Remainder >= Den ? -1 : 0)
1105   SDValue Remainder_GE_Den = DAG.getSelectCC(DL, Remainder, Den,
1106                                                  DAG.getConstant(-1, VT),
1107                                                  DAG.getConstant(0, VT),
1108                                                  ISD::SETUGE);
1109   // Remainder_GE_Zero = (Num >= Num_S_Remainder ? -1 : 0)
1110   SDValue Remainder_GE_Zero = DAG.getSelectCC(DL, Num,
1111                                                   Num_S_Remainder,
1112                                                   DAG.getConstant(-1, VT),
1113                                                   DAG.getConstant(0, VT),
1114                                                   ISD::SETUGE);
1115   // Tmp1 = Remainder_GE_Den & Remainder_GE_Zero
1116   SDValue Tmp1 = DAG.getNode(ISD::AND, DL, VT, Remainder_GE_Den,
1117                                                Remainder_GE_Zero);
1118
1119   // Calculate Division result:
1120
1121   // Quotient_A_One = Quotient + 1
1122   SDValue Quotient_A_One = DAG.getNode(ISD::ADD, DL, VT, Quotient,
1123                                                          DAG.getConstant(1, VT));
1124
1125   // Quotient_S_One = Quotient - 1
1126   SDValue Quotient_S_One = DAG.getNode(ISD::SUB, DL, VT, Quotient,
1127                                                          DAG.getConstant(1, VT));
1128
1129   // Div = (Tmp1 == 0 ? Quotient : Quotient_A_One)
1130   SDValue Div = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1131                                      Quotient, Quotient_A_One, ISD::SETEQ);
1132
1133   // Div = (Remainder_GE_Zero == 0 ? Quotient_S_One : Div)
1134   Div = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1135                             Quotient_S_One, Div, ISD::SETEQ);
1136
1137   // Calculate Rem result:
1138
1139   // Remainder_S_Den = Remainder - Den
1140   SDValue Remainder_S_Den = DAG.getNode(ISD::SUB, DL, VT, Remainder, Den);
1141
1142   // Remainder_A_Den = Remainder + Den
1143   SDValue Remainder_A_Den = DAG.getNode(ISD::ADD, DL, VT, Remainder, Den);
1144
1145   // Rem = (Tmp1 == 0 ? Remainder : Remainder_S_Den)
1146   SDValue Rem = DAG.getSelectCC(DL, Tmp1, DAG.getConstant(0, VT),
1147                                     Remainder, Remainder_S_Den, ISD::SETEQ);
1148
1149   // Rem = (Remainder_GE_Zero == 0 ? Remainder_A_Den : Rem)
1150   Rem = DAG.getSelectCC(DL, Remainder_GE_Zero, DAG.getConstant(0, VT),
1151                             Remainder_A_Den, Rem, ISD::SETEQ);
1152   SDValue Ops[2] = {
1153     Div,
1154     Rem
1155   };
1156   return DAG.getMergeValues(Ops, DL);
1157 }
1158
1159 SDValue AMDGPUTargetLowering::LowerUINT_TO_FP(SDValue Op,
1160                                                SelectionDAG &DAG) const {
1161   SDValue S0 = Op.getOperand(0);
1162   SDLoc DL(Op);
1163   if (Op.getValueType() != MVT::f32 || S0.getValueType() != MVT::i64)
1164     return SDValue();
1165
1166   // f32 uint_to_fp i64
1167   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1168                            DAG.getConstant(0, MVT::i32));
1169   SDValue FloatLo = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Lo);
1170   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32, S0,
1171                            DAG.getConstant(1, MVT::i32));
1172   SDValue FloatHi = DAG.getNode(ISD::UINT_TO_FP, DL, MVT::f32, Hi);
1173   FloatHi = DAG.getNode(ISD::FMUL, DL, MVT::f32, FloatHi,
1174                         DAG.getConstantFP(4294967296.0f, MVT::f32)); // 2^32
1175   return DAG.getNode(ISD::FADD, DL, MVT::f32, FloatLo, FloatHi);
1176
1177 }
1178
1179 SDValue AMDGPUTargetLowering::ExpandSIGN_EXTEND_INREG(SDValue Op,
1180                                                       unsigned BitsDiff,
1181                                                       SelectionDAG &DAG) const {
1182   MVT VT = Op.getSimpleValueType();
1183   SDLoc DL(Op);
1184   SDValue Shift = DAG.getConstant(BitsDiff, VT);
1185   // Shift left by 'Shift' bits.
1186   SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Op.getOperand(0), Shift);
1187   // Signed shift Right by 'Shift' bits.
1188   return DAG.getNode(ISD::SRA, DL, VT, Shl, Shift);
1189 }
1190
1191 SDValue AMDGPUTargetLowering::LowerSIGN_EXTEND_INREG(SDValue Op,
1192                                                      SelectionDAG &DAG) const {
1193   EVT ExtraVT = cast<VTSDNode>(Op.getOperand(1))->getVT();
1194   MVT VT = Op.getSimpleValueType();
1195   MVT ScalarVT = VT.getScalarType();
1196
1197   if (!VT.isVector())
1198     return SDValue();
1199
1200   SDValue Src = Op.getOperand(0);
1201   SDLoc DL(Op);
1202
1203   // TODO: Don't scalarize on Evergreen?
1204   unsigned NElts = VT.getVectorNumElements();
1205   SmallVector<SDValue, 8> Args;
1206   DAG.ExtractVectorElements(Src, Args, 0, NElts);
1207
1208   SDValue VTOp = DAG.getValueType(ExtraVT.getScalarType());
1209   for (unsigned I = 0; I < NElts; ++I)
1210     Args[I] = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, ScalarVT, Args[I], VTOp);
1211
1212   return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Args);
1213 }
1214
1215 //===----------------------------------------------------------------------===//
1216 // Custom DAG optimizations
1217 //===----------------------------------------------------------------------===//
1218
1219 static bool isU24(SDValue Op, SelectionDAG &DAG) {
1220   APInt KnownZero, KnownOne;
1221   EVT VT = Op.getValueType();
1222   DAG.computeKnownBits(Op, KnownZero, KnownOne);
1223
1224   return (VT.getSizeInBits() - KnownZero.countLeadingOnes()) <= 24;
1225 }
1226
1227 static bool isI24(SDValue Op, SelectionDAG &DAG) {
1228   EVT VT = Op.getValueType();
1229
1230   // In order for this to be a signed 24-bit value, bit 23, must
1231   // be a sign bit.
1232   return VT.getSizeInBits() >= 24 && // Types less than 24-bit should be treated
1233                                      // as unsigned 24-bit values.
1234          (VT.getSizeInBits() - DAG.ComputeNumSignBits(Op)) < 24;
1235 }
1236
1237 static void simplifyI24(SDValue Op, TargetLowering::DAGCombinerInfo &DCI) {
1238
1239   SelectionDAG &DAG = DCI.DAG;
1240   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
1241   EVT VT = Op.getValueType();
1242
1243   APInt Demanded = APInt::getLowBitsSet(VT.getSizeInBits(), 24);
1244   APInt KnownZero, KnownOne;
1245   TargetLowering::TargetLoweringOpt TLO(DAG, true, true);
1246   if (TLI.SimplifyDemandedBits(Op, Demanded, KnownZero, KnownOne, TLO))
1247     DCI.CommitTargetLoweringOpt(TLO);
1248 }
1249
1250 SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N,
1251                                             DAGCombinerInfo &DCI) const {
1252   SelectionDAG &DAG = DCI.DAG;
1253   SDLoc DL(N);
1254
1255   switch(N->getOpcode()) {
1256     default: break;
1257     case ISD::MUL: {
1258       EVT VT = N->getValueType(0);
1259       SDValue N0 = N->getOperand(0);
1260       SDValue N1 = N->getOperand(1);
1261       SDValue Mul;
1262
1263       // FIXME: Add support for 24-bit multiply with 64-bit output on SI.
1264       if (VT.isVector() || VT.getSizeInBits() > 32)
1265         break;
1266
1267       if (Subtarget->hasMulU24() && isU24(N0, DAG) && isU24(N1, DAG)) {
1268         N0 = DAG.getZExtOrTrunc(N0, DL, MVT::i32);
1269         N1 = DAG.getZExtOrTrunc(N1, DL, MVT::i32);
1270         Mul = DAG.getNode(AMDGPUISD::MUL_U24, DL, MVT::i32, N0, N1);
1271       } else if (Subtarget->hasMulI24() && isI24(N0, DAG) && isI24(N1, DAG)) {
1272         N0 = DAG.getSExtOrTrunc(N0, DL, MVT::i32);
1273         N1 = DAG.getSExtOrTrunc(N1, DL, MVT::i32);
1274         Mul = DAG.getNode(AMDGPUISD::MUL_I24, DL, MVT::i32, N0, N1);
1275       } else {
1276         break;
1277       }
1278
1279       // We need to use sext even for MUL_U24, because MUL_U24 is used
1280       // for signed multiply of 8 and 16-bit types.
1281       SDValue Reg = DAG.getSExtOrTrunc(Mul, DL, VT);
1282
1283       return Reg;
1284     }
1285     case AMDGPUISD::MUL_I24:
1286     case AMDGPUISD::MUL_U24: {
1287       SDValue N0 = N->getOperand(0);
1288       SDValue N1 = N->getOperand(1);
1289       simplifyI24(N0, DCI);
1290       simplifyI24(N1, DCI);
1291       return SDValue();
1292     }
1293     case ISD::SELECT_CC: {
1294       return CombineMinMax(N, DAG);
1295     }
1296   }
1297   return SDValue();
1298 }
1299
1300 //===----------------------------------------------------------------------===//
1301 // Helper functions
1302 //===----------------------------------------------------------------------===//
1303
1304 void AMDGPUTargetLowering::getOriginalFunctionArgs(
1305                                SelectionDAG &DAG,
1306                                const Function *F,
1307                                const SmallVectorImpl<ISD::InputArg> &Ins,
1308                                SmallVectorImpl<ISD::InputArg> &OrigIns) const {
1309
1310   for (unsigned i = 0, e = Ins.size(); i < e; ++i) {
1311     if (Ins[i].ArgVT == Ins[i].VT) {
1312       OrigIns.push_back(Ins[i]);
1313       continue;
1314     }
1315
1316     EVT VT;
1317     if (Ins[i].ArgVT.isVector() && !Ins[i].VT.isVector()) {
1318       // Vector has been split into scalars.
1319       VT = Ins[i].ArgVT.getVectorElementType();
1320     } else if (Ins[i].VT.isVector() && Ins[i].ArgVT.isVector() &&
1321                Ins[i].ArgVT.getVectorElementType() !=
1322                Ins[i].VT.getVectorElementType()) {
1323       // Vector elements have been promoted
1324       VT = Ins[i].ArgVT;
1325     } else {
1326       // Vector has been spilt into smaller vectors.
1327       VT = Ins[i].VT;
1328     }
1329
1330     ISD::InputArg Arg(Ins[i].Flags, VT, VT, Ins[i].Used,
1331                       Ins[i].OrigArgIndex, Ins[i].PartOffset);
1332     OrigIns.push_back(Arg);
1333   }
1334 }
1335
1336 bool AMDGPUTargetLowering::isHWTrueValue(SDValue Op) const {
1337   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1338     return CFP->isExactlyValue(1.0);
1339   }
1340   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1341     return C->isAllOnesValue();
1342   }
1343   return false;
1344 }
1345
1346 bool AMDGPUTargetLowering::isHWFalseValue(SDValue Op) const {
1347   if (ConstantFPSDNode * CFP = dyn_cast<ConstantFPSDNode>(Op)) {
1348     return CFP->getValueAPF().isZero();
1349   }
1350   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
1351     return C->isNullValue();
1352   }
1353   return false;
1354 }
1355
1356 SDValue AMDGPUTargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1357                                                   const TargetRegisterClass *RC,
1358                                                    unsigned Reg, EVT VT) const {
1359   MachineFunction &MF = DAG.getMachineFunction();
1360   MachineRegisterInfo &MRI = MF.getRegInfo();
1361   unsigned VirtualRegister;
1362   if (!MRI.isLiveIn(Reg)) {
1363     VirtualRegister = MRI.createVirtualRegister(RC);
1364     MRI.addLiveIn(Reg, VirtualRegister);
1365   } else {
1366     VirtualRegister = MRI.getLiveInVirtReg(Reg);
1367   }
1368   return DAG.getRegister(VirtualRegister, VT);
1369 }
1370
1371 #define NODE_NAME_CASE(node) case AMDGPUISD::node: return #node;
1372
1373 const char* AMDGPUTargetLowering::getTargetNodeName(unsigned Opcode) const {
1374   switch (Opcode) {
1375   default: return nullptr;
1376   // AMDIL DAG nodes
1377   NODE_NAME_CASE(CALL);
1378   NODE_NAME_CASE(UMUL);
1379   NODE_NAME_CASE(DIV_INF);
1380   NODE_NAME_CASE(RET_FLAG);
1381   NODE_NAME_CASE(BRANCH_COND);
1382
1383   // AMDGPU DAG nodes
1384   NODE_NAME_CASE(DWORDADDR)
1385   NODE_NAME_CASE(FRACT)
1386   NODE_NAME_CASE(FMAX)
1387   NODE_NAME_CASE(SMAX)
1388   NODE_NAME_CASE(UMAX)
1389   NODE_NAME_CASE(FMIN)
1390   NODE_NAME_CASE(SMIN)
1391   NODE_NAME_CASE(UMIN)
1392   NODE_NAME_CASE(BFE_U32)
1393   NODE_NAME_CASE(BFE_I32)
1394   NODE_NAME_CASE(BFI)
1395   NODE_NAME_CASE(BFM)
1396   NODE_NAME_CASE(MUL_U24)
1397   NODE_NAME_CASE(MUL_I24)
1398   NODE_NAME_CASE(URECIP)
1399   NODE_NAME_CASE(DOT4)
1400   NODE_NAME_CASE(EXPORT)
1401   NODE_NAME_CASE(CONST_ADDRESS)
1402   NODE_NAME_CASE(REGISTER_LOAD)
1403   NODE_NAME_CASE(REGISTER_STORE)
1404   NODE_NAME_CASE(LOAD_CONSTANT)
1405   NODE_NAME_CASE(LOAD_INPUT)
1406   NODE_NAME_CASE(SAMPLE)
1407   NODE_NAME_CASE(SAMPLEB)
1408   NODE_NAME_CASE(SAMPLED)
1409   NODE_NAME_CASE(SAMPLEL)
1410   NODE_NAME_CASE(STORE_MSKOR)
1411   NODE_NAME_CASE(TBUFFER_STORE_FORMAT)
1412   }
1413 }
1414
1415 static void computeKnownBitsForMinMax(const SDValue Op0,
1416                                       const SDValue Op1,
1417                                       APInt &KnownZero,
1418                                       APInt &KnownOne,
1419                                       const SelectionDAG &DAG,
1420                                       unsigned Depth) {
1421   APInt Op0Zero, Op0One;
1422   APInt Op1Zero, Op1One;
1423   DAG.computeKnownBits(Op0, Op0Zero, Op0One, Depth);
1424   DAG.computeKnownBits(Op1, Op1Zero, Op1One, Depth);
1425
1426   KnownZero = Op0Zero & Op1Zero;
1427   KnownOne = Op0One & Op1One;
1428 }
1429
1430 void AMDGPUTargetLowering::computeKnownBitsForTargetNode(
1431   const SDValue Op,
1432   APInt &KnownZero,
1433   APInt &KnownOne,
1434   const SelectionDAG &DAG,
1435   unsigned Depth) const {
1436
1437   KnownZero = KnownOne = APInt(KnownOne.getBitWidth(), 0); // Don't know anything.
1438   unsigned Opc = Op.getOpcode();
1439   switch (Opc) {
1440   case ISD::INTRINSIC_WO_CHAIN: {
1441     // FIXME: The intrinsic should just use the node.
1442     switch (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue()) {
1443     case AMDGPUIntrinsic::AMDGPU_imax:
1444     case AMDGPUIntrinsic::AMDGPU_umax:
1445     case AMDGPUIntrinsic::AMDGPU_imin:
1446     case AMDGPUIntrinsic::AMDGPU_umin:
1447       computeKnownBitsForMinMax(Op.getOperand(1), Op.getOperand(2),
1448                                 KnownZero, KnownOne, DAG, Depth);
1449       break;
1450     default:
1451       break;
1452     }
1453
1454     break;
1455   }
1456   case AMDGPUISD::SMAX:
1457   case AMDGPUISD::UMAX:
1458   case AMDGPUISD::SMIN:
1459   case AMDGPUISD::UMIN:
1460     computeKnownBitsForMinMax(Op.getOperand(0), Op.getOperand(1),
1461                               KnownZero, KnownOne, DAG, Depth);
1462     break;
1463   default:
1464     break;
1465   }
1466 }