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