Convert SelectionDAG::getMergeValues to use ArrayRef.
[oota-llvm.git] / lib / Target / R600 / SIISelLowering.cpp
1 //===-- SIISelLowering.cpp - SI DAG Lowering Implementation ---------------===//
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 Custom DAG lowering for SI
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "SIISelLowering.h"
16 #include "AMDGPU.h"
17 #include "AMDGPUSubtarget.h"
18 #include "AMDILIntrinsicInfo.h"
19 #include "SIInstrInfo.h"
20 #include "SIMachineFunctionInfo.h"
21 #include "SIRegisterInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/IR/Function.h"
27
28 using namespace llvm;
29
30 SITargetLowering::SITargetLowering(TargetMachine &TM) :
31     AMDGPUTargetLowering(TM) {
32   addRegisterClass(MVT::i1, &AMDGPU::SReg_64RegClass);
33   addRegisterClass(MVT::i64, &AMDGPU::VSrc_64RegClass);
34
35   addRegisterClass(MVT::v32i8, &AMDGPU::SReg_256RegClass);
36   addRegisterClass(MVT::v64i8, &AMDGPU::SReg_512RegClass);
37
38   addRegisterClass(MVT::i32, &AMDGPU::SReg_32RegClass);
39   addRegisterClass(MVT::f32, &AMDGPU::VSrc_32RegClass);
40
41   addRegisterClass(MVT::f64, &AMDGPU::VSrc_64RegClass);
42   addRegisterClass(MVT::v2i32, &AMDGPU::VSrc_64RegClass);
43   addRegisterClass(MVT::v2f32, &AMDGPU::VSrc_64RegClass);
44
45   addRegisterClass(MVT::v4i32, &AMDGPU::VSrc_128RegClass);
46   addRegisterClass(MVT::v4f32, &AMDGPU::VSrc_128RegClass);
47
48   addRegisterClass(MVT::v8i32, &AMDGPU::VReg_256RegClass);
49   addRegisterClass(MVT::v8f32, &AMDGPU::VReg_256RegClass);
50
51   addRegisterClass(MVT::v16i32, &AMDGPU::VReg_512RegClass);
52   addRegisterClass(MVT::v16f32, &AMDGPU::VReg_512RegClass);
53
54   computeRegisterProperties();
55
56   // Condition Codes
57   setCondCodeAction(ISD::SETONE, MVT::f32, Expand);
58   setCondCodeAction(ISD::SETUEQ, MVT::f32, Expand);
59   setCondCodeAction(ISD::SETUGE, MVT::f32, Expand);
60   setCondCodeAction(ISD::SETUGT, MVT::f32, Expand);
61   setCondCodeAction(ISD::SETULE, MVT::f32, Expand);
62   setCondCodeAction(ISD::SETULT, MVT::f32, Expand);
63
64   setCondCodeAction(ISD::SETONE, MVT::f64, Expand);
65   setCondCodeAction(ISD::SETUEQ, MVT::f64, Expand);
66   setCondCodeAction(ISD::SETUGE, MVT::f64, Expand);
67   setCondCodeAction(ISD::SETUGT, MVT::f64, Expand);
68   setCondCodeAction(ISD::SETULE, MVT::f64, Expand);
69   setCondCodeAction(ISD::SETULT, MVT::f64, Expand);
70
71   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i32, Expand);
72   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Expand);
73   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i32, Expand);
74   setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16f32, Expand);
75
76   setOperationAction(ISD::ADD, MVT::i32, Legal);
77   setOperationAction(ISD::ADDC, MVT::i32, Legal);
78   setOperationAction(ISD::ADDE, MVT::i32, Legal);
79
80   // We need to custom lower vector stores from local memory
81   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
82   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
83   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
84   setOperationAction(ISD::LOAD, MVT::v16i32, Custom);
85
86   setOperationAction(ISD::STORE, MVT::v8i32, Custom);
87   setOperationAction(ISD::STORE, MVT::v16i32, Custom);
88
89   // We need to custom lower loads/stores from private memory
90   setOperationAction(ISD::LOAD, MVT::i32, Custom);
91   setOperationAction(ISD::LOAD, MVT::i64, Custom);
92   setOperationAction(ISD::LOAD, MVT::v2i32, Custom);
93   setOperationAction(ISD::LOAD, MVT::v4i32, Custom);
94   setOperationAction(ISD::LOAD, MVT::v8i32, Custom);
95
96   setOperationAction(ISD::STORE, MVT::i1, Custom);
97   setOperationAction(ISD::STORE, MVT::i32, Custom);
98   setOperationAction(ISD::STORE, MVT::i64, Custom);
99   setOperationAction(ISD::STORE, MVT::v2i32, Custom);
100   setOperationAction(ISD::STORE, MVT::v4i32, Custom);
101
102   setOperationAction(ISD::SELECT, MVT::i64, Custom);
103   setOperationAction(ISD::SELECT, MVT::f64, Promote);
104   AddPromotedToType(ISD::SELECT, MVT::f64, MVT::i64);
105
106   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
107   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
108
109   setOperationAction(ISD::SELECT_CC, MVT::Other, Expand);
110
111   setOperationAction(ISD::SETCC, MVT::v2i1, Expand);
112   setOperationAction(ISD::SETCC, MVT::v4i1, Expand);
113
114   setOperationAction(ISD::ANY_EXTEND, MVT::i64, Custom);
115   setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
116   setOperationAction(ISD::ZERO_EXTEND, MVT::i64, Custom);
117
118   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Legal);
119   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i1, Custom);
120   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i1, Custom);
121
122   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8, Legal);
123   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i8, Custom);
124   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i8, Custom);
125
126   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Legal);
127   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v2i16, Custom);
128   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::v4i16, Custom);
129
130   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Custom);
131
132   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::Other, Custom);
133
134   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
135   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::f32, Custom);
136   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v16i8, Custom);
137   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::v4f32, Custom);
138
139   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
140
141   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
142   setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Custom);
143   setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Custom);
144   setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Expand);
145   setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, Expand);
146   setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, Expand);
147
148   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
149   setLoadExtAction(ISD::ZEXTLOAD, MVT::i8, Custom);
150   setLoadExtAction(ISD::ZEXTLOAD, MVT::i16, Custom);
151   setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Expand);
152
153   setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
154   setLoadExtAction(ISD::EXTLOAD, MVT::i8, Custom);
155   setLoadExtAction(ISD::EXTLOAD, MVT::i16, Custom);
156   setLoadExtAction(ISD::EXTLOAD, MVT::i32, Expand);
157   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
158
159   setTruncStoreAction(MVT::i32, MVT::i8, Custom);
160   setTruncStoreAction(MVT::i32, MVT::i16, Custom);
161   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
162   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
163   setTruncStoreAction(MVT::v8i32, MVT::v8i16, Expand);
164   setTruncStoreAction(MVT::v16i32, MVT::v16i16, Expand);
165
166   setOperationAction(ISD::LOAD, MVT::i1, Custom);
167
168   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
169   setOperationAction(ISD::GlobalAddress, MVT::i64, Custom);
170   setOperationAction(ISD::FrameIndex, MVT::i32, Custom);
171
172   // We only support LOAD/STORE and vector manipulation ops for vectors
173   // with > 4 elements.
174   MVT VecTypes[] = {
175     MVT::v8i32, MVT::v8f32, MVT::v16i32, MVT::v16f32
176   };
177
178   const size_t NumVecTypes = array_lengthof(VecTypes);
179   for (unsigned Type = 0; Type < NumVecTypes; ++Type) {
180     for (unsigned Op = 0; Op < ISD::BUILTIN_OP_END; ++Op) {
181       switch(Op) {
182       case ISD::LOAD:
183       case ISD::STORE:
184       case ISD::BUILD_VECTOR:
185       case ISD::BITCAST:
186       case ISD::EXTRACT_VECTOR_ELT:
187       case ISD::INSERT_VECTOR_ELT:
188       case ISD::CONCAT_VECTORS:
189       case ISD::INSERT_SUBVECTOR:
190       case ISD::EXTRACT_SUBVECTOR:
191         break;
192       default:
193         setOperationAction(Op, VecTypes[Type], Expand);
194         break;
195       }
196     }
197   }
198
199   for (int I = MVT::v1f64; I <= MVT::v8f64; ++I) {
200     MVT::SimpleValueType VT = static_cast<MVT::SimpleValueType>(I);
201     setOperationAction(ISD::FTRUNC, VT, Expand);
202     setOperationAction(ISD::FCEIL, VT, Expand);
203     setOperationAction(ISD::FFLOOR, VT, Expand);
204   }
205
206   if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS) {
207     setOperationAction(ISD::FTRUNC, MVT::f64, Legal);
208     setOperationAction(ISD::FCEIL, MVT::f64, Legal);
209     setOperationAction(ISD::FFLOOR, MVT::f64, Legal);
210     setOperationAction(ISD::FRINT, MVT::f64, Legal);
211   }
212
213   setTargetDAGCombine(ISD::SELECT_CC);
214   setTargetDAGCombine(ISD::SETCC);
215
216   setSchedulingPreference(Sched::RegPressure);
217 }
218
219 //===----------------------------------------------------------------------===//
220 // TargetLowering queries
221 //===----------------------------------------------------------------------===//
222
223 bool SITargetLowering::allowsUnalignedMemoryAccesses(EVT  VT,
224                                                      unsigned AddrSpace,
225                                                      bool *IsFast) const {
226   if (IsFast)
227     *IsFast = false;
228
229   // XXX: This depends on the address space and also we may want to revist
230   // the alignment values we specify in the DataLayout.
231
232   // TODO: I think v3i32 should allow unaligned accesses on CI with DS_READ_B96,
233   // which isn't a simple VT.
234   if (!VT.isSimple() || VT == MVT::Other)
235     return false;
236
237   // XXX - CI changes say "Support for unaligned memory accesses" but I don't
238   // see what for specifically. The wording everywhere else seems to be the
239   // same.
240
241   // 3.6.4 - Operations using pairs of VGPRs (for example: double-floats) have
242   // no alignment restrictions.
243   if (AddrSpace == AMDGPUAS::PRIVATE_ADDRESS) {
244     // Using any pair of GPRs should be the same as any other pair.
245     if (IsFast)
246       *IsFast = true;
247     return VT.bitsGE(MVT::i64);
248   }
249
250   // XXX - The only mention I see of this in the ISA manual is for LDS direct
251   // reads the "byte address and must be dword aligned". Is it also true for the
252   // normal loads and stores?
253   if (AddrSpace == AMDGPUAS::LOCAL_ADDRESS)
254     return false;
255
256   // 8.1.6 - For Dword or larger reads or writes, the two LSBs of the
257   // byte-address are ignored, thus forcing Dword alignment.
258   if (IsFast)
259     *IsFast = true;
260   return VT.bitsGT(MVT::i32);
261 }
262
263 bool SITargetLowering::shouldSplitVectorType(EVT VT) const {
264   return VT.getScalarType().bitsLE(MVT::i16);
265 }
266
267 bool SITargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
268                                                          Type *Ty) const {
269   const SIInstrInfo *TII =
270     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
271   return TII->isInlineConstant(Imm);
272 }
273
274 SDValue SITargetLowering::LowerParameter(SelectionDAG &DAG, EVT VT, EVT MemVT,
275                                          SDLoc DL, SDValue Chain,
276                                          unsigned Offset, bool Signed) const {
277   MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
278   PointerType *PtrTy = PointerType::get(VT.getTypeForEVT(*DAG.getContext()),
279                                             AMDGPUAS::CONSTANT_ADDRESS);
280   SDValue BasePtr =  DAG.getCopyFromReg(Chain, DL,
281                            MRI.getLiveInVirtReg(AMDGPU::SGPR0_SGPR1), MVT::i64);
282   SDValue Ptr = DAG.getNode(ISD::ADD, DL, MVT::i64, BasePtr,
283                                              DAG.getConstant(Offset, MVT::i64));
284   return DAG.getExtLoad(Signed ? ISD::SEXTLOAD : ISD::ZEXTLOAD, DL, VT, Chain, Ptr,
285                             MachinePointerInfo(UndefValue::get(PtrTy)), MemVT,
286                             false, false, MemVT.getSizeInBits() >> 3);
287
288 }
289
290 SDValue SITargetLowering::LowerFormalArguments(
291                                       SDValue Chain,
292                                       CallingConv::ID CallConv,
293                                       bool isVarArg,
294                                       const SmallVectorImpl<ISD::InputArg> &Ins,
295                                       SDLoc DL, SelectionDAG &DAG,
296                                       SmallVectorImpl<SDValue> &InVals) const {
297
298   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
299
300   MachineFunction &MF = DAG.getMachineFunction();
301   FunctionType *FType = MF.getFunction()->getFunctionType();
302   SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>();
303
304   assert(CallConv == CallingConv::C);
305
306   SmallVector<ISD::InputArg, 16> Splits;
307   uint32_t Skipped = 0;
308
309   for (unsigned i = 0, e = Ins.size(), PSInputNum = 0; i != e; ++i) {
310     const ISD::InputArg &Arg = Ins[i];
311
312     // First check if it's a PS input addr
313     if (Info->ShaderType == ShaderType::PIXEL && !Arg.Flags.isInReg() &&
314         !Arg.Flags.isByVal()) {
315
316       assert((PSInputNum <= 15) && "Too many PS inputs!");
317
318       if (!Arg.Used) {
319         // We can savely skip PS inputs
320         Skipped |= 1 << i;
321         ++PSInputNum;
322         continue;
323       }
324
325       Info->PSInputAddr |= 1 << PSInputNum++;
326     }
327
328     // Second split vertices into their elements
329     if (Info->ShaderType != ShaderType::COMPUTE && Arg.VT.isVector()) {
330       ISD::InputArg NewArg = Arg;
331       NewArg.Flags.setSplit();
332       NewArg.VT = Arg.VT.getVectorElementType();
333
334       // We REALLY want the ORIGINAL number of vertex elements here, e.g. a
335       // three or five element vertex only needs three or five registers,
336       // NOT four or eigth.
337       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
338       unsigned NumElements = ParamType->getVectorNumElements();
339
340       for (unsigned j = 0; j != NumElements; ++j) {
341         Splits.push_back(NewArg);
342         NewArg.PartOffset += NewArg.VT.getStoreSize();
343       }
344
345     } else if (Info->ShaderType != ShaderType::COMPUTE) {
346       Splits.push_back(Arg);
347     }
348   }
349
350   SmallVector<CCValAssign, 16> ArgLocs;
351   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
352                  getTargetMachine(), ArgLocs, *DAG.getContext());
353
354   // At least one interpolation mode must be enabled or else the GPU will hang.
355   if (Info->ShaderType == ShaderType::PIXEL && (Info->PSInputAddr & 0x7F) == 0) {
356     Info->PSInputAddr |= 1;
357     CCInfo.AllocateReg(AMDGPU::VGPR0);
358     CCInfo.AllocateReg(AMDGPU::VGPR1);
359   }
360
361   // The pointer to the list of arguments is stored in SGPR0, SGPR1
362   if (Info->ShaderType == ShaderType::COMPUTE) {
363     CCInfo.AllocateReg(AMDGPU::SGPR0);
364     CCInfo.AllocateReg(AMDGPU::SGPR1);
365     MF.addLiveIn(AMDGPU::SGPR0_SGPR1, &AMDGPU::SReg_64RegClass);
366   }
367
368   if (Info->ShaderType == ShaderType::COMPUTE) {
369     getOriginalFunctionArgs(DAG, DAG.getMachineFunction().getFunction(), Ins,
370                             Splits);
371   }
372
373   AnalyzeFormalArguments(CCInfo, Splits);
374
375   for (unsigned i = 0, e = Ins.size(), ArgIdx = 0; i != e; ++i) {
376
377     const ISD::InputArg &Arg = Ins[i];
378     if (Skipped & (1 << i)) {
379       InVals.push_back(DAG.getUNDEF(Arg.VT));
380       continue;
381     }
382
383     CCValAssign &VA = ArgLocs[ArgIdx++];
384     EVT VT = VA.getLocVT();
385
386     if (VA.isMemLoc()) {
387       VT = Ins[i].VT;
388       EVT MemVT = Splits[i].VT;
389       // The first 36 bytes of the input buffer contains information about
390       // thread group and global sizes.
391       SDValue Arg = LowerParameter(DAG, VT, MemVT,  DL, DAG.getRoot(),
392                                    36 + VA.getLocMemOffset(),
393                                    Ins[i].Flags.isSExt());
394       InVals.push_back(Arg);
395       continue;
396     }
397     assert(VA.isRegLoc() && "Parameter must be in a register!");
398
399     unsigned Reg = VA.getLocReg();
400
401     if (VT == MVT::i64) {
402       // For now assume it is a pointer
403       Reg = TRI->getMatchingSuperReg(Reg, AMDGPU::sub0,
404                                      &AMDGPU::SReg_64RegClass);
405       Reg = MF.addLiveIn(Reg, &AMDGPU::SReg_64RegClass);
406       InVals.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
407       continue;
408     }
409
410     const TargetRegisterClass *RC = TRI->getMinimalPhysRegClass(Reg, VT);
411
412     Reg = MF.addLiveIn(Reg, RC);
413     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, VT);
414
415     if (Arg.VT.isVector()) {
416
417       // Build a vector from the registers
418       Type *ParamType = FType->getParamType(Arg.OrigArgIndex);
419       unsigned NumElements = ParamType->getVectorNumElements();
420
421       SmallVector<SDValue, 4> Regs;
422       Regs.push_back(Val);
423       for (unsigned j = 1; j != NumElements; ++j) {
424         Reg = ArgLocs[ArgIdx++].getLocReg();
425         Reg = MF.addLiveIn(Reg, RC);
426         Regs.push_back(DAG.getCopyFromReg(Chain, DL, Reg, VT));
427       }
428
429       // Fill up the missing vector elements
430       NumElements = Arg.VT.getVectorNumElements() - NumElements;
431       for (unsigned j = 0; j != NumElements; ++j)
432         Regs.push_back(DAG.getUNDEF(VT));
433
434       InVals.push_back(DAG.getNode(ISD::BUILD_VECTOR, DL, Arg.VT, Regs));
435       continue;
436     }
437
438     InVals.push_back(Val);
439   }
440   return Chain;
441 }
442
443 MachineBasicBlock * SITargetLowering::EmitInstrWithCustomInserter(
444     MachineInstr * MI, MachineBasicBlock * BB) const {
445
446   MachineBasicBlock::iterator I = *MI;
447
448   switch (MI->getOpcode()) {
449   default:
450     return AMDGPUTargetLowering::EmitInstrWithCustomInserter(MI, BB);
451   case AMDGPU::BRANCH: return BB;
452   case AMDGPU::SI_ADDR64_RSRC: {
453     const SIInstrInfo *TII =
454       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
455     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
456     unsigned SuperReg = MI->getOperand(0).getReg();
457     unsigned SubRegLo = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
458     unsigned SubRegHi = MRI.createVirtualRegister(&AMDGPU::SGPR_64RegClass);
459     unsigned SubRegHiHi = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
460     unsigned SubRegHiLo = MRI.createVirtualRegister(&AMDGPU::SGPR_32RegClass);
461     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B64), SubRegLo)
462             .addOperand(MI->getOperand(1));
463     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiLo)
464             .addImm(0);
465     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::S_MOV_B32), SubRegHiHi)
466             .addImm(AMDGPU::RSRC_DATA_FORMAT >> 32);
467     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SubRegHi)
468             .addReg(SubRegHiLo)
469             .addImm(AMDGPU::sub0)
470             .addReg(SubRegHiHi)
471             .addImm(AMDGPU::sub1);
472     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::REG_SEQUENCE), SuperReg)
473             .addReg(SubRegLo)
474             .addImm(AMDGPU::sub0_sub1)
475             .addReg(SubRegHi)
476             .addImm(AMDGPU::sub2_sub3);
477     MI->eraseFromParent();
478     break;
479   }
480   case AMDGPU::V_SUB_F64: {
481     const SIInstrInfo *TII =
482       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
483     BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::V_ADD_F64),
484             MI->getOperand(0).getReg())
485             .addReg(MI->getOperand(1).getReg())
486             .addReg(MI->getOperand(2).getReg())
487             .addImm(0)  /* src2 */
488             .addImm(0)  /* ABS */
489             .addImm(0)  /* CLAMP */
490             .addImm(0)  /* OMOD */
491             .addImm(2); /* NEG */
492     MI->eraseFromParent();
493     break;
494   }
495   case AMDGPU::SI_RegisterStorePseudo: {
496     MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
497     const SIInstrInfo *TII =
498       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
499     unsigned Reg = MRI.createVirtualRegister(&AMDGPU::SReg_64RegClass);
500     MachineInstrBuilder MIB =
501         BuildMI(*BB, I, MI->getDebugLoc(), TII->get(AMDGPU::SI_RegisterStore),
502                 Reg);
503     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i)
504       MIB.addOperand(MI->getOperand(i));
505
506     MI->eraseFromParent();
507   }
508   }
509   return BB;
510 }
511
512 EVT SITargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
513   if (!VT.isVector()) {
514     return MVT::i1;
515   }
516   return MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
517 }
518
519 MVT SITargetLowering::getScalarShiftAmountTy(EVT VT) const {
520   return MVT::i32;
521 }
522
523 bool SITargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
524   VT = VT.getScalarType();
525
526   if (!VT.isSimple())
527     return false;
528
529   switch (VT.getSimpleVT().SimpleTy) {
530   case MVT::f32:
531     return false; /* There is V_MAD_F32 for f32 */
532   case MVT::f64:
533     return true;
534   default:
535     break;
536   }
537
538   return false;
539 }
540
541 //===----------------------------------------------------------------------===//
542 // Custom DAG Lowering Operations
543 //===----------------------------------------------------------------------===//
544
545 SDValue SITargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
546   MachineFunction &MF = DAG.getMachineFunction();
547   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
548   switch (Op.getOpcode()) {
549   default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
550   case ISD::BRCOND: return LowerBRCOND(Op, DAG);
551   case ISD::LOAD: {
552     LoadSDNode *Load = dyn_cast<LoadSDNode>(Op);
553     if (Op.getValueType().isVector() &&
554         (Load->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS ||
555          Load->getAddressSpace() == AMDGPUAS::PRIVATE_ADDRESS ||
556          (Load->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS &&
557           Op.getValueType().getVectorNumElements() > 4))) {
558       SDValue MergedValues[2] = {
559         SplitVectorLoad(Op, DAG),
560         Load->getChain()
561       };
562       return DAG.getMergeValues(MergedValues, SDLoc(Op));
563     } else {
564       return LowerLOAD(Op, DAG);
565     }
566   }
567
568   case ISD::SELECT: return LowerSELECT(Op, DAG);
569   case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
570   case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
571   case ISD::STORE: return LowerSTORE(Op, DAG);
572   case ISD::ANY_EXTEND: // Fall-through
573   case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, DAG);
574   case ISD::GlobalAddress: return LowerGlobalAddress(MFI, Op, DAG);
575   case ISD::INTRINSIC_WO_CHAIN: {
576     unsigned IntrinsicID =
577                          cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
578     EVT VT = Op.getValueType();
579     SDLoc DL(Op);
580     //XXX: Hardcoded we only use two to store the pointer to the parameters.
581     unsigned NumUserSGPRs = 2;
582     switch (IntrinsicID) {
583     default: return AMDGPUTargetLowering::LowerOperation(Op, DAG);
584     case Intrinsic::r600_read_ngroups_x:
585       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 0, false);
586     case Intrinsic::r600_read_ngroups_y:
587       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 4, false);
588     case Intrinsic::r600_read_ngroups_z:
589       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 8, false);
590     case Intrinsic::r600_read_global_size_x:
591       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 12, false);
592     case Intrinsic::r600_read_global_size_y:
593       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 16, false);
594     case Intrinsic::r600_read_global_size_z:
595       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 20, false);
596     case Intrinsic::r600_read_local_size_x:
597       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 24, false);
598     case Intrinsic::r600_read_local_size_y:
599       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 28, false);
600     case Intrinsic::r600_read_local_size_z:
601       return LowerParameter(DAG, VT, VT, DL, DAG.getEntryNode(), 32, false);
602     case Intrinsic::r600_read_tgid_x:
603       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
604                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 0), VT);
605     case Intrinsic::r600_read_tgid_y:
606       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
607                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 1), VT);
608     case Intrinsic::r600_read_tgid_z:
609       return CreateLiveInRegister(DAG, &AMDGPU::SReg_32RegClass,
610                      AMDGPU::SReg_32RegClass.getRegister(NumUserSGPRs + 2), VT);
611     case Intrinsic::r600_read_tidig_x:
612       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
613                                   AMDGPU::VGPR0, VT);
614     case Intrinsic::r600_read_tidig_y:
615       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
616                                   AMDGPU::VGPR1, VT);
617     case Intrinsic::r600_read_tidig_z:
618       return CreateLiveInRegister(DAG, &AMDGPU::VReg_32RegClass,
619                                   AMDGPU::VGPR2, VT);
620     case AMDGPUIntrinsic::SI_load_const: {
621       SDValue Ops [] = {
622         Op.getOperand(1),
623         Op.getOperand(2)
624       };
625
626       MachineMemOperand *MMO = MF.getMachineMemOperand(
627           MachinePointerInfo(),
628           MachineMemOperand::MOLoad | MachineMemOperand::MOInvariant,
629           VT.getSizeInBits() / 8, 4);
630       return DAG.getMemIntrinsicNode(AMDGPUISD::LOAD_CONSTANT, DL,
631                                      Op->getVTList(), Ops, VT, MMO);
632     }
633     case AMDGPUIntrinsic::SI_sample:
634       return LowerSampleIntrinsic(AMDGPUISD::SAMPLE, Op, DAG);
635     case AMDGPUIntrinsic::SI_sampleb:
636       return LowerSampleIntrinsic(AMDGPUISD::SAMPLEB, Op, DAG);
637     case AMDGPUIntrinsic::SI_sampled:
638       return LowerSampleIntrinsic(AMDGPUISD::SAMPLED, Op, DAG);
639     case AMDGPUIntrinsic::SI_samplel:
640       return LowerSampleIntrinsic(AMDGPUISD::SAMPLEL, Op, DAG);
641     case AMDGPUIntrinsic::SI_vs_load_input:
642       return DAG.getNode(AMDGPUISD::LOAD_INPUT, DL, VT,
643                          Op.getOperand(1),
644                          Op.getOperand(2),
645                          Op.getOperand(3));
646     }
647   }
648
649   case ISD::INTRINSIC_VOID:
650     SDValue Chain = Op.getOperand(0);
651     unsigned IntrinsicID = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
652
653     switch (IntrinsicID) {
654       case AMDGPUIntrinsic::SI_tbuffer_store: {
655         SDLoc DL(Op);
656         SDValue Ops [] = {
657           Chain,
658           Op.getOperand(2),
659           Op.getOperand(3),
660           Op.getOperand(4),
661           Op.getOperand(5),
662           Op.getOperand(6),
663           Op.getOperand(7),
664           Op.getOperand(8),
665           Op.getOperand(9),
666           Op.getOperand(10),
667           Op.getOperand(11),
668           Op.getOperand(12),
669           Op.getOperand(13),
670           Op.getOperand(14)
671         };
672         EVT VT = Op.getOperand(3).getValueType();
673
674         MachineMemOperand *MMO = MF.getMachineMemOperand(
675             MachinePointerInfo(),
676             MachineMemOperand::MOStore,
677             VT.getSizeInBits() / 8, 4);
678         return DAG.getMemIntrinsicNode(AMDGPUISD::TBUFFER_STORE_FORMAT, DL,
679                                        Op->getVTList(), Ops, VT, MMO);
680       }
681       default:
682         break;
683     }
684   }
685   return SDValue();
686 }
687
688 /// \brief Helper function for LowerBRCOND
689 static SDNode *findUser(SDValue Value, unsigned Opcode) {
690
691   SDNode *Parent = Value.getNode();
692   for (SDNode::use_iterator I = Parent->use_begin(), E = Parent->use_end();
693        I != E; ++I) {
694
695     if (I.getUse().get() != Value)
696       continue;
697
698     if (I->getOpcode() == Opcode)
699       return *I;
700   }
701   return nullptr;
702 }
703
704 /// This transforms the control flow intrinsics to get the branch destination as
705 /// last parameter, also switches branch target with BR if the need arise
706 SDValue SITargetLowering::LowerBRCOND(SDValue BRCOND,
707                                       SelectionDAG &DAG) const {
708
709   SDLoc DL(BRCOND);
710
711   SDNode *Intr = BRCOND.getOperand(1).getNode();
712   SDValue Target = BRCOND.getOperand(2);
713   SDNode *BR = nullptr;
714
715   if (Intr->getOpcode() == ISD::SETCC) {
716     // As long as we negate the condition everything is fine
717     SDNode *SetCC = Intr;
718     assert(SetCC->getConstantOperandVal(1) == 1);
719     assert(cast<CondCodeSDNode>(SetCC->getOperand(2).getNode())->get() ==
720            ISD::SETNE);
721     Intr = SetCC->getOperand(0).getNode();
722
723   } else {
724     // Get the target from BR if we don't negate the condition
725     BR = findUser(BRCOND, ISD::BR);
726     Target = BR->getOperand(1);
727   }
728
729   assert(Intr->getOpcode() == ISD::INTRINSIC_W_CHAIN);
730
731   // Build the result and
732   SmallVector<EVT, 4> Res;
733   for (unsigned i = 1, e = Intr->getNumValues(); i != e; ++i)
734     Res.push_back(Intr->getValueType(i));
735
736   // operands of the new intrinsic call
737   SmallVector<SDValue, 4> Ops;
738   Ops.push_back(BRCOND.getOperand(0));
739   for (unsigned i = 1, e = Intr->getNumOperands(); i != e; ++i)
740     Ops.push_back(Intr->getOperand(i));
741   Ops.push_back(Target);
742
743   // build the new intrinsic call
744   SDNode *Result = DAG.getNode(
745     Res.size() > 1 ? ISD::INTRINSIC_W_CHAIN : ISD::INTRINSIC_VOID, DL,
746     DAG.getVTList(Res), Ops).getNode();
747
748   if (BR) {
749     // Give the branch instruction our target
750     SDValue Ops[] = {
751       BR->getOperand(0),
752       BRCOND.getOperand(2)
753     };
754     DAG.MorphNodeTo(BR, ISD::BR, BR->getVTList(), Ops, 2);
755   }
756
757   SDValue Chain = SDValue(Result, Result->getNumValues() - 1);
758
759   // Copy the intrinsic results to registers
760   for (unsigned i = 1, e = Intr->getNumValues() - 1; i != e; ++i) {
761     SDNode *CopyToReg = findUser(SDValue(Intr, i), ISD::CopyToReg);
762     if (!CopyToReg)
763       continue;
764
765     Chain = DAG.getCopyToReg(
766       Chain, DL,
767       CopyToReg->getOperand(1),
768       SDValue(Result, i - 1),
769       SDValue());
770
771     DAG.ReplaceAllUsesWith(SDValue(CopyToReg, 0), CopyToReg->getOperand(0));
772   }
773
774   // Remove the old intrinsic from the chain
775   DAG.ReplaceAllUsesOfValueWith(
776     SDValue(Intr, Intr->getNumValues() - 1),
777     Intr->getOperand(0));
778
779   return Chain;
780 }
781
782 SDValue SITargetLowering::LowerLOAD(SDValue Op, SelectionDAG &DAG) const {
783   SDLoc DL(Op);
784   LoadSDNode *Load = cast<LoadSDNode>(Op);
785   SDValue Ret = AMDGPUTargetLowering::LowerLOAD(Op, DAG);
786   SDValue MergedValues[2];
787   MergedValues[1] = Load->getChain();
788   if (Ret.getNode()) {
789     MergedValues[0] = Ret;
790     return DAG.getMergeValues(MergedValues, DL);
791   }
792
793   if (Load->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS) {
794     return SDValue();
795   }
796
797   EVT MemVT = Load->getMemoryVT();
798
799   assert(!MemVT.isVector() && "Private loads should be scalarized");
800   assert(!MemVT.isFloatingPoint() && "FP loads should be promoted to int");
801
802   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Load->getBasePtr(),
803                             DAG.getConstant(2, MVT::i32));
804   Ret = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
805                     Load->getChain(), Ptr,
806                     DAG.getTargetConstant(0, MVT::i32),
807                     Op.getOperand(2));
808   if (MemVT.getSizeInBits() == 64) {
809     SDValue IncPtr = DAG.getNode(ISD::ADD, DL, MVT::i32, Ptr,
810                                  DAG.getConstant(1, MVT::i32));
811
812     SDValue LoadUpper = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
813                                     Load->getChain(), IncPtr,
814                                     DAG.getTargetConstant(0, MVT::i32),
815                                     Op.getOperand(2));
816
817     Ret = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ret, LoadUpper);
818   }
819
820   MergedValues[0] = Ret;
821   return DAG.getMergeValues(MergedValues, DL);
822
823 }
824
825 SDValue SITargetLowering::LowerSampleIntrinsic(unsigned Opcode,
826                                                const SDValue &Op,
827                                                SelectionDAG &DAG) const {
828   return DAG.getNode(Opcode, SDLoc(Op), Op.getValueType(), Op.getOperand(1),
829                      Op.getOperand(2),
830                      Op.getOperand(3),
831                      Op.getOperand(4));
832 }
833
834 SDValue SITargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
835   if (Op.getValueType() != MVT::i64)
836     return SDValue();
837
838   SDLoc DL(Op);
839   SDValue Cond = Op.getOperand(0);
840
841   SDValue Zero = DAG.getConstant(0, MVT::i32);
842   SDValue One = DAG.getConstant(1, MVT::i32);
843
844   SDValue LHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(1));
845   SDValue RHS = DAG.getNode(ISD::BITCAST, DL, MVT::v2i32, Op.getOperand(2));
846
847   SDValue Lo0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, Zero);
848   SDValue Lo1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, Zero);
849
850   SDValue Lo = DAG.getSelect(DL, MVT::i32, Cond, Lo0, Lo1);
851
852   SDValue Hi0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, LHS, One);
853   SDValue Hi1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i32, RHS, One);
854
855   SDValue Hi = DAG.getSelect(DL, MVT::i32, Cond, Hi0, Hi1);
856
857   SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i32, Lo, Hi);
858   return DAG.getNode(ISD::BITCAST, DL, MVT::i64, Res);
859 }
860
861 SDValue SITargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const {
862   SDValue LHS = Op.getOperand(0);
863   SDValue RHS = Op.getOperand(1);
864   SDValue True = Op.getOperand(2);
865   SDValue False = Op.getOperand(3);
866   SDValue CC = Op.getOperand(4);
867   EVT VT = Op.getValueType();
868   SDLoc DL(Op);
869
870   // Possible Min/Max pattern
871   SDValue MinMax = LowerMinMax(Op, DAG);
872   if (MinMax.getNode()) {
873     return MinMax;
874   }
875
876   SDValue Cond = DAG.getNode(ISD::SETCC, DL, MVT::i1, LHS, RHS, CC);
877   return DAG.getNode(ISD::SELECT, DL, VT, Cond, True, False);
878 }
879
880 SDValue SITargetLowering::LowerSIGN_EXTEND(SDValue Op,
881                                            SelectionDAG &DAG) const {
882   EVT VT = Op.getValueType();
883   SDLoc DL(Op);
884
885   if (VT != MVT::i64) {
886     return SDValue();
887   }
888
889   SDValue Hi = DAG.getNode(ISD::SRA, DL, MVT::i32, Op.getOperand(0),
890                                                  DAG.getConstant(31, MVT::i32));
891
892   return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Op.getOperand(0), Hi);
893 }
894
895 SDValue SITargetLowering::LowerSTORE(SDValue Op, SelectionDAG &DAG) const {
896   SDLoc DL(Op);
897   StoreSDNode *Store = cast<StoreSDNode>(Op);
898   EVT VT = Store->getMemoryVT();
899
900   SDValue Ret = AMDGPUTargetLowering::LowerSTORE(Op, DAG);
901   if (Ret.getNode())
902     return Ret;
903
904   if (VT.isVector() && VT.getVectorNumElements() >= 8)
905       return SplitVectorStore(Op, DAG);
906
907   if (VT == MVT::i1)
908     return DAG.getTruncStore(Store->getChain(), DL,
909                         DAG.getSExtOrTrunc(Store->getValue(), DL, MVT::i32),
910                         Store->getBasePtr(), MVT::i1, Store->getMemOperand());
911
912   if (Store->getAddressSpace() != AMDGPUAS::PRIVATE_ADDRESS)
913     return SDValue();
914
915   SDValue Ptr = DAG.getNode(ISD::SRL, DL, MVT::i32, Store->getBasePtr(),
916                             DAG.getConstant(2, MVT::i32));
917   SDValue Chain = Store->getChain();
918   SmallVector<SDValue, 8> Values;
919
920   if (Store->isTruncatingStore()) {
921     unsigned Mask = 0;
922     if (Store->getMemoryVT() == MVT::i8) {
923       Mask = 0xff;
924     } else if (Store->getMemoryVT() == MVT::i16) {
925       Mask = 0xffff;
926     }
927     SDValue Dst = DAG.getNode(AMDGPUISD::REGISTER_LOAD, DL, MVT::i32,
928                               Chain, Store->getBasePtr(),
929                               DAG.getConstant(0, MVT::i32));
930     SDValue ByteIdx = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getBasePtr(),
931                                   DAG.getConstant(0x3, MVT::i32));
932     SDValue ShiftAmt = DAG.getNode(ISD::SHL, DL, MVT::i32, ByteIdx,
933                                    DAG.getConstant(3, MVT::i32));
934     SDValue MaskedValue = DAG.getNode(ISD::AND, DL, MVT::i32, Store->getValue(),
935                                       DAG.getConstant(Mask, MVT::i32));
936     SDValue ShiftedValue = DAG.getNode(ISD::SHL, DL, MVT::i32,
937                                        MaskedValue, ShiftAmt);
938     SDValue RotrAmt = DAG.getNode(ISD::SUB, DL, MVT::i32,
939                                   DAG.getConstant(32, MVT::i32), ShiftAmt);
940     SDValue DstMask = DAG.getNode(ISD::ROTR, DL, MVT::i32,
941                                   DAG.getConstant(Mask, MVT::i32),
942                                   RotrAmt);
943     Dst = DAG.getNode(ISD::AND, DL, MVT::i32, Dst, DstMask);
944     Dst = DAG.getNode(ISD::OR, DL, MVT::i32, Dst, ShiftedValue);
945
946     Values.push_back(Dst);
947   } else if (VT == MVT::i64) {
948     for (unsigned i = 0; i < 2; ++i) {
949       Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
950                        Store->getValue(), DAG.getConstant(i, MVT::i32)));
951     }
952   } else if (VT == MVT::i128) {
953     for (unsigned i = 0; i < 2; ++i) {
954       for (unsigned j = 0; j < 2; ++j) {
955         Values.push_back(DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i32,
956                            DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64,
957                            Store->getValue(), DAG.getConstant(i, MVT::i32)),
958                          DAG.getConstant(j, MVT::i32)));
959       }
960     }
961   } else {
962     Values.push_back(Store->getValue());
963   }
964
965   for (unsigned i = 0; i < Values.size(); ++i) {
966     SDValue PartPtr = DAG.getNode(ISD::ADD, DL, MVT::i32,
967                                   Ptr, DAG.getConstant(i, MVT::i32));
968     Chain = DAG.getNode(AMDGPUISD::REGISTER_STORE, DL, MVT::Other,
969                         Chain, Values[i], PartPtr,
970                         DAG.getTargetConstant(0, MVT::i32));
971   }
972   return Chain;
973 }
974
975
976 SDValue SITargetLowering::LowerZERO_EXTEND(SDValue Op,
977                                            SelectionDAG &DAG) const {
978   EVT VT = Op.getValueType();
979   SDLoc DL(Op);
980
981   if (VT != MVT::i64) {
982     return SDValue();
983   }
984
985   SDValue Src = Op.getOperand(0);
986   if (Src.getValueType() != MVT::i32)
987     Src = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, Src);
988
989   SDValue Zero = DAG.getConstant(0, MVT::i32);
990   return DAG.getNode(ISD::BUILD_PAIR, DL, VT, Src, Zero);
991 }
992
993 //===----------------------------------------------------------------------===//
994 // Custom DAG optimizations
995 //===----------------------------------------------------------------------===//
996
997 SDValue SITargetLowering::PerformDAGCombine(SDNode *N,
998                                             DAGCombinerInfo &DCI) const {
999   SelectionDAG &DAG = DCI.DAG;
1000   SDLoc DL(N);
1001   EVT VT = N->getValueType(0);
1002
1003   switch (N->getOpcode()) {
1004     default: return AMDGPUTargetLowering::PerformDAGCombine(N, DCI);
1005     case ISD::SELECT_CC: {
1006       ConstantSDNode *True, *False;
1007       // i1 selectcc(l, r, -1, 0, cc) -> i1 setcc(l, r, cc)
1008       if ((True = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1009           && (False = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1010           && True->isAllOnesValue()
1011           && False->isNullValue()
1012           && VT == MVT::i1) {
1013         return DAG.getNode(ISD::SETCC, DL, VT, N->getOperand(0),
1014                            N->getOperand(1), N->getOperand(4));
1015
1016       }
1017       break;
1018     }
1019     case ISD::SETCC: {
1020       SDValue Arg0 = N->getOperand(0);
1021       SDValue Arg1 = N->getOperand(1);
1022       SDValue CC = N->getOperand(2);
1023       ConstantSDNode * C = nullptr;
1024       ISD::CondCode CCOp = dyn_cast<CondCodeSDNode>(CC)->get();
1025
1026       // i1 setcc (sext(i1), 0, setne) -> i1 setcc(i1, 0, setne)
1027       if (VT == MVT::i1
1028           && Arg0.getOpcode() == ISD::SIGN_EXTEND
1029           && Arg0.getOperand(0).getValueType() == MVT::i1
1030           && (C = dyn_cast<ConstantSDNode>(Arg1))
1031           && C->isNullValue()
1032           && CCOp == ISD::SETNE) {
1033         return SimplifySetCC(VT, Arg0.getOperand(0),
1034                              DAG.getConstant(0, MVT::i1), CCOp, true, DCI, DL);
1035       }
1036       break;
1037     }
1038   }
1039   return SDValue();
1040 }
1041
1042 /// \brief Test if RegClass is one of the VSrc classes
1043 static bool isVSrc(unsigned RegClass) {
1044   return AMDGPU::VSrc_32RegClassID == RegClass ||
1045          AMDGPU::VSrc_64RegClassID == RegClass;
1046 }
1047
1048 /// \brief Test if RegClass is one of the SSrc classes
1049 static bool isSSrc(unsigned RegClass) {
1050   return AMDGPU::SSrc_32RegClassID == RegClass ||
1051          AMDGPU::SSrc_64RegClassID == RegClass;
1052 }
1053
1054 /// \brief Analyze the possible immediate value Op
1055 ///
1056 /// Returns -1 if it isn't an immediate, 0 if it's and inline immediate
1057 /// and the immediate value if it's a literal immediate
1058 int32_t SITargetLowering::analyzeImmediate(const SDNode *N) const {
1059
1060   union {
1061     int32_t I;
1062     float F;
1063   } Imm;
1064
1065   if (const ConstantSDNode *Node = dyn_cast<ConstantSDNode>(N)) {
1066     if (Node->getZExtValue() >> 32) {
1067         return -1;
1068     }
1069     Imm.I = Node->getSExtValue();
1070   } else if (const ConstantFPSDNode *Node = dyn_cast<ConstantFPSDNode>(N)) {
1071     if (N->getValueType(0) != MVT::f32)
1072       return -1;
1073     Imm.F = Node->getValueAPF().convertToFloat();
1074   } else
1075     return -1; // It isn't an immediate
1076
1077   if ((Imm.I >= -16 && Imm.I <= 64) ||
1078       Imm.F == 0.5f || Imm.F == -0.5f ||
1079       Imm.F == 1.0f || Imm.F == -1.0f ||
1080       Imm.F == 2.0f || Imm.F == -2.0f ||
1081       Imm.F == 4.0f || Imm.F == -4.0f)
1082     return 0; // It's an inline immediate
1083
1084   return Imm.I; // It's a literal immediate
1085 }
1086
1087 /// \brief Try to fold an immediate directly into an instruction
1088 bool SITargetLowering::foldImm(SDValue &Operand, int32_t &Immediate,
1089                                bool &ScalarSlotUsed) const {
1090
1091   MachineSDNode *Mov = dyn_cast<MachineSDNode>(Operand);
1092   const SIInstrInfo *TII =
1093     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1094   if (!Mov || !TII->isMov(Mov->getMachineOpcode()))
1095     return false;
1096
1097   const SDValue &Op = Mov->getOperand(0);
1098   int32_t Value = analyzeImmediate(Op.getNode());
1099   if (Value == -1) {
1100     // Not an immediate at all
1101     return false;
1102
1103   } else if (Value == 0) {
1104     // Inline immediates can always be fold
1105     Operand = Op;
1106     return true;
1107
1108   } else if (Value == Immediate) {
1109     // Already fold literal immediate
1110     Operand = Op;
1111     return true;
1112
1113   } else if (!ScalarSlotUsed && !Immediate) {
1114     // Fold this literal immediate
1115     ScalarSlotUsed = true;
1116     Immediate = Value;
1117     Operand = Op;
1118     return true;
1119
1120   }
1121
1122   return false;
1123 }
1124
1125 const TargetRegisterClass *SITargetLowering::getRegClassForNode(
1126                                    SelectionDAG &DAG, const SDValue &Op) const {
1127   const SIInstrInfo *TII =
1128     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1129   const SIRegisterInfo &TRI = TII->getRegisterInfo();
1130
1131   if (!Op->isMachineOpcode()) {
1132     switch(Op->getOpcode()) {
1133     case ISD::CopyFromReg: {
1134       MachineRegisterInfo &MRI = DAG.getMachineFunction().getRegInfo();
1135       unsigned Reg = cast<RegisterSDNode>(Op->getOperand(1))->getReg();
1136       if (TargetRegisterInfo::isVirtualRegister(Reg)) {
1137         return MRI.getRegClass(Reg);
1138       }
1139       return TRI.getPhysRegClass(Reg);
1140     }
1141     default:  return nullptr;
1142     }
1143   }
1144   const MCInstrDesc &Desc = TII->get(Op->getMachineOpcode());
1145   int OpClassID = Desc.OpInfo[Op.getResNo()].RegClass;
1146   if (OpClassID != -1) {
1147     return TRI.getRegClass(OpClassID);
1148   }
1149   switch(Op.getMachineOpcode()) {
1150   case AMDGPU::COPY_TO_REGCLASS:
1151     // Operand 1 is the register class id for COPY_TO_REGCLASS instructions.
1152     OpClassID = cast<ConstantSDNode>(Op->getOperand(1))->getZExtValue();
1153
1154     // If the COPY_TO_REGCLASS instruction is copying to a VSrc register
1155     // class, then the register class for the value could be either a
1156     // VReg or and SReg.  In order to get a more accurate
1157     if (OpClassID == AMDGPU::VSrc_32RegClassID ||
1158         OpClassID == AMDGPU::VSrc_64RegClassID) {
1159       return getRegClassForNode(DAG, Op.getOperand(0));
1160     }
1161     return TRI.getRegClass(OpClassID);
1162   case AMDGPU::EXTRACT_SUBREG: {
1163     int SubIdx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1164     const TargetRegisterClass *SuperClass =
1165       getRegClassForNode(DAG, Op.getOperand(0));
1166     return TRI.getSubClassWithSubReg(SuperClass, SubIdx);
1167   }
1168   case AMDGPU::REG_SEQUENCE:
1169     // Operand 0 is the register class id for REG_SEQUENCE instructions.
1170     return TRI.getRegClass(
1171       cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue());
1172   default:
1173     return getRegClassFor(Op.getSimpleValueType());
1174   }
1175 }
1176
1177 /// \brief Does "Op" fit into register class "RegClass" ?
1178 bool SITargetLowering::fitsRegClass(SelectionDAG &DAG, const SDValue &Op,
1179                                     unsigned RegClass) const {
1180   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1181   const TargetRegisterClass *RC = getRegClassForNode(DAG, Op);
1182   if (!RC) {
1183     return false;
1184   }
1185   return TRI->getRegClass(RegClass)->hasSubClassEq(RC);
1186 }
1187
1188 /// \brief Make sure that we don't exeed the number of allowed scalars
1189 void SITargetLowering::ensureSRegLimit(SelectionDAG &DAG, SDValue &Operand,
1190                                        unsigned RegClass,
1191                                        bool &ScalarSlotUsed) const {
1192
1193   // First map the operands register class to a destination class
1194   if (RegClass == AMDGPU::VSrc_32RegClassID)
1195     RegClass = AMDGPU::VReg_32RegClassID;
1196   else if (RegClass == AMDGPU::VSrc_64RegClassID)
1197     RegClass = AMDGPU::VReg_64RegClassID;
1198   else
1199     return;
1200
1201   // Nothing to do if they fit naturally
1202   if (fitsRegClass(DAG, Operand, RegClass))
1203     return;
1204
1205   // If the scalar slot isn't used yet use it now
1206   if (!ScalarSlotUsed) {
1207     ScalarSlotUsed = true;
1208     return;
1209   }
1210
1211   // This is a conservative aproach. It is possible that we can't determine the
1212   // correct register class and copy too often, but better safe than sorry.
1213   SDValue RC = DAG.getTargetConstant(RegClass, MVT::i32);
1214   SDNode *Node = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS, SDLoc(),
1215                                     Operand.getValueType(), Operand, RC);
1216   Operand = SDValue(Node, 0);
1217 }
1218
1219 /// \returns true if \p Node's operands are different from the SDValue list
1220 /// \p Ops
1221 static bool isNodeChanged(const SDNode *Node, const std::vector<SDValue> &Ops) {
1222   for (unsigned i = 0, e = Node->getNumOperands(); i < e; ++i) {
1223     if (Ops[i].getNode() != Node->getOperand(i).getNode()) {
1224       return true;
1225     }
1226   }
1227   return false;
1228 }
1229
1230 /// \brief Try to fold the Nodes operands into the Node
1231 SDNode *SITargetLowering::foldOperands(MachineSDNode *Node,
1232                                        SelectionDAG &DAG) const {
1233
1234   // Original encoding (either e32 or e64)
1235   int Opcode = Node->getMachineOpcode();
1236   const SIInstrInfo *TII =
1237     static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1238   const MCInstrDesc *Desc = &TII->get(Opcode);
1239
1240   unsigned NumDefs = Desc->getNumDefs();
1241   unsigned NumOps = Desc->getNumOperands();
1242
1243   // Commuted opcode if available
1244   int OpcodeRev = Desc->isCommutable() ? TII->commuteOpcode(Opcode) : -1;
1245   const MCInstrDesc *DescRev = OpcodeRev == -1 ? nullptr : &TII->get(OpcodeRev);
1246
1247   assert(!DescRev || DescRev->getNumDefs() == NumDefs);
1248   assert(!DescRev || DescRev->getNumOperands() == NumOps);
1249
1250   // e64 version if available, -1 otherwise
1251   int OpcodeE64 = AMDGPU::getVOPe64(Opcode);
1252   const MCInstrDesc *DescE64 = OpcodeE64 == -1 ? nullptr : &TII->get(OpcodeE64);
1253
1254   assert(!DescE64 || DescE64->getNumDefs() == NumDefs);
1255   assert(!DescE64 || DescE64->getNumOperands() == (NumOps + 4));
1256
1257   int32_t Immediate = Desc->getSize() == 4 ? 0 : -1;
1258   bool HaveVSrc = false, HaveSSrc = false;
1259
1260   // First figure out what we alread have in this instruction
1261   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1262        i != e && Op < NumOps; ++i, ++Op) {
1263
1264     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1265     if (isVSrc(RegClass))
1266       HaveVSrc = true;
1267     else if (isSSrc(RegClass))
1268       HaveSSrc = true;
1269     else
1270       continue;
1271
1272     int32_t Imm = analyzeImmediate(Node->getOperand(i).getNode());
1273     if (Imm != -1 && Imm != 0) {
1274       // Literal immediate
1275       Immediate = Imm;
1276     }
1277   }
1278
1279   // If we neither have VSrc nor SSrc it makes no sense to continue
1280   if (!HaveVSrc && !HaveSSrc)
1281     return Node;
1282
1283   // No scalar allowed when we have both VSrc and SSrc
1284   bool ScalarSlotUsed = HaveVSrc && HaveSSrc;
1285
1286   // Second go over the operands and try to fold them
1287   std::vector<SDValue> Ops;
1288   bool Promote2e64 = false;
1289   for (unsigned i = 0, e = Node->getNumOperands(), Op = NumDefs;
1290        i != e && Op < NumOps; ++i, ++Op) {
1291
1292     const SDValue &Operand = Node->getOperand(i);
1293     Ops.push_back(Operand);
1294
1295     // Already folded immediate ?
1296     if (isa<ConstantSDNode>(Operand.getNode()) ||
1297         isa<ConstantFPSDNode>(Operand.getNode()))
1298       continue;
1299
1300     // Is this a VSrc or SSrc operand ?
1301     unsigned RegClass = Desc->OpInfo[Op].RegClass;
1302     if (isVSrc(RegClass) || isSSrc(RegClass)) {
1303       // Try to fold the immediates
1304       if (!foldImm(Ops[i], Immediate, ScalarSlotUsed)) {
1305         // Folding didn't worked, make sure we don't hit the SReg limit
1306         ensureSRegLimit(DAG, Ops[i], RegClass, ScalarSlotUsed);
1307       }
1308       continue;
1309     }
1310
1311     if (i == 1 && DescRev && fitsRegClass(DAG, Ops[0], RegClass)) {
1312
1313       unsigned OtherRegClass = Desc->OpInfo[NumDefs].RegClass;
1314       assert(isVSrc(OtherRegClass) || isSSrc(OtherRegClass));
1315
1316       // Test if it makes sense to swap operands
1317       if (foldImm(Ops[1], Immediate, ScalarSlotUsed) ||
1318           (!fitsRegClass(DAG, Ops[1], RegClass) &&
1319            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1320
1321         // Swap commutable operands
1322         std::swap(Ops[0], Ops[1]);
1323
1324         Desc = DescRev;
1325         DescRev = nullptr;
1326         continue;
1327       }
1328     }
1329
1330     if (DescE64 && !Immediate) {
1331
1332       // Test if it makes sense to switch to e64 encoding
1333       unsigned OtherRegClass = DescE64->OpInfo[Op].RegClass;
1334       if (!isVSrc(OtherRegClass) && !isSSrc(OtherRegClass))
1335         continue;
1336
1337       int32_t TmpImm = -1;
1338       if (foldImm(Ops[i], TmpImm, ScalarSlotUsed) ||
1339           (!fitsRegClass(DAG, Ops[i], RegClass) &&
1340            fitsRegClass(DAG, Ops[1], OtherRegClass))) {
1341
1342         // Switch to e64 encoding
1343         Immediate = -1;
1344         Promote2e64 = true;
1345         Desc = DescE64;
1346         DescE64 = nullptr;
1347       }
1348     }
1349   }
1350
1351   if (Promote2e64) {
1352     // Add the modifier flags while promoting
1353     for (unsigned i = 0; i < 4; ++i)
1354       Ops.push_back(DAG.getTargetConstant(0, MVT::i32));
1355   }
1356
1357   // Add optional chain and glue
1358   for (unsigned i = NumOps - NumDefs, e = Node->getNumOperands(); i < e; ++i)
1359     Ops.push_back(Node->getOperand(i));
1360
1361   // Nodes that have a glue result are not CSE'd by getMachineNode(), so in
1362   // this case a brand new node is always be created, even if the operands
1363   // are the same as before.  So, manually check if anything has been changed.
1364   if (Desc->Opcode == Opcode && !isNodeChanged(Node, Ops)) {
1365     return Node;
1366   }
1367
1368   // Create a complete new instruction
1369   return DAG.getMachineNode(Desc->Opcode, SDLoc(Node), Node->getVTList(), Ops);
1370 }
1371
1372 /// \brief Helper function for adjustWritemask
1373 static unsigned SubIdx2Lane(unsigned Idx) {
1374   switch (Idx) {
1375   default: return 0;
1376   case AMDGPU::sub0: return 0;
1377   case AMDGPU::sub1: return 1;
1378   case AMDGPU::sub2: return 2;
1379   case AMDGPU::sub3: return 3;
1380   }
1381 }
1382
1383 /// \brief Adjust the writemask of MIMG instructions
1384 void SITargetLowering::adjustWritemask(MachineSDNode *&Node,
1385                                        SelectionDAG &DAG) const {
1386   SDNode *Users[4] = { };
1387   unsigned Lane = 0;
1388   unsigned OldDmask = Node->getConstantOperandVal(0);
1389   unsigned NewDmask = 0;
1390
1391   // Try to figure out the used register components
1392   for (SDNode::use_iterator I = Node->use_begin(), E = Node->use_end();
1393        I != E; ++I) {
1394
1395     // Abort if we can't understand the usage
1396     if (!I->isMachineOpcode() ||
1397         I->getMachineOpcode() != TargetOpcode::EXTRACT_SUBREG)
1398       return;
1399
1400     // Lane means which subreg of %VGPRa_VGPRb_VGPRc_VGPRd is used.
1401     // Note that subregs are packed, i.e. Lane==0 is the first bit set
1402     // in OldDmask, so it can be any of X,Y,Z,W; Lane==1 is the second bit
1403     // set, etc.
1404     Lane = SubIdx2Lane(I->getConstantOperandVal(1));
1405
1406     // Set which texture component corresponds to the lane.
1407     unsigned Comp;
1408     for (unsigned i = 0, Dmask = OldDmask; i <= Lane; i++) {
1409       assert(Dmask);
1410       Comp = countTrailingZeros(Dmask);
1411       Dmask &= ~(1 << Comp);
1412     }
1413
1414     // Abort if we have more than one user per component
1415     if (Users[Lane])
1416       return;
1417
1418     Users[Lane] = *I;
1419     NewDmask |= 1 << Comp;
1420   }
1421
1422   // Abort if there's no change
1423   if (NewDmask == OldDmask)
1424     return;
1425
1426   // Adjust the writemask in the node
1427   std::vector<SDValue> Ops;
1428   Ops.push_back(DAG.getTargetConstant(NewDmask, MVT::i32));
1429   for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1430     Ops.push_back(Node->getOperand(i));
1431   Node = (MachineSDNode*)DAG.UpdateNodeOperands(Node, Ops.data(), Ops.size());
1432
1433   // If we only got one lane, replace it with a copy
1434   // (if NewDmask has only one bit set...)
1435   if (NewDmask && (NewDmask & (NewDmask-1)) == 0) {
1436     SDValue RC = DAG.getTargetConstant(AMDGPU::VReg_32RegClassID, MVT::i32);
1437     SDNode *Copy = DAG.getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1438                                       SDLoc(), Users[Lane]->getValueType(0),
1439                                       SDValue(Node, 0), RC);
1440     DAG.ReplaceAllUsesWith(Users[Lane], Copy);
1441     return;
1442   }
1443
1444   // Update the users of the node with the new indices
1445   for (unsigned i = 0, Idx = AMDGPU::sub0; i < 4; ++i) {
1446
1447     SDNode *User = Users[i];
1448     if (!User)
1449       continue;
1450
1451     SDValue Op = DAG.getTargetConstant(Idx, MVT::i32);
1452     DAG.UpdateNodeOperands(User, User->getOperand(0), Op);
1453
1454     switch (Idx) {
1455     default: break;
1456     case AMDGPU::sub0: Idx = AMDGPU::sub1; break;
1457     case AMDGPU::sub1: Idx = AMDGPU::sub2; break;
1458     case AMDGPU::sub2: Idx = AMDGPU::sub3; break;
1459     }
1460   }
1461 }
1462
1463 /// \brief Fold the instructions after slecting them
1464 SDNode *SITargetLowering::PostISelFolding(MachineSDNode *Node,
1465                                           SelectionDAG &DAG) const {
1466   const SIInstrInfo *TII =
1467       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1468   Node = AdjustRegClass(Node, DAG);
1469
1470   if (TII->isMIMG(Node->getMachineOpcode()))
1471     adjustWritemask(Node, DAG);
1472
1473   return foldOperands(Node, DAG);
1474 }
1475
1476 /// \brief Assign the register class depending on the number of
1477 /// bits set in the writemask
1478 void SITargetLowering::AdjustInstrPostInstrSelection(MachineInstr *MI,
1479                                                      SDNode *Node) const {
1480   const SIInstrInfo *TII =
1481       static_cast<const SIInstrInfo*>(getTargetMachine().getInstrInfo());
1482   if (!TII->isMIMG(MI->getOpcode()))
1483     return;
1484
1485   unsigned VReg = MI->getOperand(0).getReg();
1486   unsigned Writemask = MI->getOperand(1).getImm();
1487   unsigned BitsSet = 0;
1488   for (unsigned i = 0; i < 4; ++i)
1489     BitsSet += Writemask & (1 << i) ? 1 : 0;
1490
1491   const TargetRegisterClass *RC;
1492   switch (BitsSet) {
1493   default: return;
1494   case 1:  RC = &AMDGPU::VReg_32RegClass; break;
1495   case 2:  RC = &AMDGPU::VReg_64RegClass; break;
1496   case 3:  RC = &AMDGPU::VReg_96RegClass; break;
1497   }
1498
1499   unsigned NewOpcode = TII->getMaskedMIMGOp(MI->getOpcode(), BitsSet);
1500   MI->setDesc(TII->get(NewOpcode));
1501   MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
1502   MRI.setRegClass(VReg, RC);
1503 }
1504
1505 MachineSDNode *SITargetLowering::AdjustRegClass(MachineSDNode *N,
1506                                                 SelectionDAG &DAG) const {
1507
1508   SDLoc DL(N);
1509   unsigned NewOpcode = N->getMachineOpcode();
1510
1511   switch (N->getMachineOpcode()) {
1512   default: return N;
1513   case AMDGPU::S_LOAD_DWORD_IMM:
1514     NewOpcode = AMDGPU::BUFFER_LOAD_DWORD_ADDR64;
1515     // Fall-through
1516   case AMDGPU::S_LOAD_DWORDX2_SGPR:
1517     if (NewOpcode == N->getMachineOpcode()) {
1518       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX2_ADDR64;
1519     }
1520     // Fall-through
1521   case AMDGPU::S_LOAD_DWORDX4_IMM:
1522   case AMDGPU::S_LOAD_DWORDX4_SGPR: {
1523     if (NewOpcode == N->getMachineOpcode()) {
1524       NewOpcode = AMDGPU::BUFFER_LOAD_DWORDX4_ADDR64;
1525     }
1526     if (fitsRegClass(DAG, N->getOperand(0), AMDGPU::SReg_64RegClassID)) {
1527       return N;
1528     }
1529     ConstantSDNode *Offset = cast<ConstantSDNode>(N->getOperand(1));
1530     SDValue Ops[] = {
1531       SDValue(DAG.getMachineNode(AMDGPU::SI_ADDR64_RSRC, DL, MVT::i128,
1532                                  DAG.getConstant(0, MVT::i64)), 0),
1533       N->getOperand(0),
1534       DAG.getConstant(Offset->getSExtValue() << 2, MVT::i32)
1535     };
1536     return DAG.getMachineNode(NewOpcode, DL, N->getVTList(), Ops);
1537   }
1538   }
1539 }
1540
1541 SDValue SITargetLowering::CreateLiveInRegister(SelectionDAG &DAG,
1542                                                const TargetRegisterClass *RC,
1543                                                unsigned Reg, EVT VT) const {
1544   SDValue VReg = AMDGPUTargetLowering::CreateLiveInRegister(DAG, RC, Reg, VT);
1545
1546   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(DAG.getEntryNode()),
1547                             cast<RegisterSDNode>(VReg)->getReg(), VT);
1548 }