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