[mips][mips64r6] Set ELF e_flags for MIPS32r6/MIPS64r6. Also do MIPS-I to MIPS-V
[oota-llvm.git] / lib / Target / Mips / MipsISelLowering.cpp
1 //===-- MipsISelLowering.cpp - Mips 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 // This file defines the interfaces that Mips uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14 #include "MipsISelLowering.h"
15 #include "InstPrinter/MipsInstPrinter.h"
16 #include "MCTargetDesc/MipsBaseInfo.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsSubtarget.h"
19 #include "MipsTargetMachine.h"
20 #include "MipsTargetObjectFile.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/CodeGen/CallingConvLower.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/SelectionDAGISel.h"
29 #include "llvm/CodeGen/ValueTypes.h"
30 #include "llvm/IR/CallingConv.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/GlobalVariable.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <cctype>
38
39 using namespace llvm;
40
41 #define DEBUG_TYPE "mips-lower"
42
43 STATISTIC(NumTailCalls, "Number of tail calls");
44
45 static cl::opt<bool>
46 LargeGOT("mxgot", cl::Hidden,
47          cl::desc("MIPS: Enable GOT larger than 64k."), cl::init(false));
48
49 static cl::opt<bool>
50 NoZeroDivCheck("mno-check-zero-division", cl::Hidden,
51                cl::desc("MIPS: Don't trap on integer division by zero."),
52                cl::init(false));
53
54 cl::opt<bool>
55 EnableMipsFastISel("mips-fast-isel", cl::Hidden,
56   cl::desc("Allow mips-fast-isel to be used"),
57   cl::init(false));
58
59 static const MCPhysReg O32IntRegs[4] = {
60   Mips::A0, Mips::A1, Mips::A2, Mips::A3
61 };
62
63 static const MCPhysReg Mips64IntRegs[8] = {
64   Mips::A0_64, Mips::A1_64, Mips::A2_64, Mips::A3_64,
65   Mips::T0_64, Mips::T1_64, Mips::T2_64, Mips::T3_64
66 };
67
68 static const MCPhysReg Mips64DPRegs[8] = {
69   Mips::D12_64, Mips::D13_64, Mips::D14_64, Mips::D15_64,
70   Mips::D16_64, Mips::D17_64, Mips::D18_64, Mips::D19_64
71 };
72
73 // If I is a shifted mask, set the size (Size) and the first bit of the
74 // mask (Pos), and return true.
75 // For example, if I is 0x003ff800, (Pos, Size) = (11, 11).
76 static bool isShiftedMask(uint64_t I, uint64_t &Pos, uint64_t &Size) {
77   if (!isShiftedMask_64(I))
78     return false;
79
80   Size = CountPopulation_64(I);
81   Pos = countTrailingZeros(I);
82   return true;
83 }
84
85 SDValue MipsTargetLowering::getGlobalReg(SelectionDAG &DAG, EVT Ty) const {
86   MipsFunctionInfo *FI = DAG.getMachineFunction().getInfo<MipsFunctionInfo>();
87   return DAG.getRegister(FI->getGlobalBaseReg(), Ty);
88 }
89
90 SDValue MipsTargetLowering::getTargetNode(GlobalAddressSDNode *N, EVT Ty,
91                                           SelectionDAG &DAG,
92                                           unsigned Flag) const {
93   return DAG.getTargetGlobalAddress(N->getGlobal(), SDLoc(N), Ty, 0, Flag);
94 }
95
96 SDValue MipsTargetLowering::getTargetNode(ExternalSymbolSDNode *N, EVT Ty,
97                                           SelectionDAG &DAG,
98                                           unsigned Flag) const {
99   return DAG.getTargetExternalSymbol(N->getSymbol(), Ty, Flag);
100 }
101
102 SDValue MipsTargetLowering::getTargetNode(BlockAddressSDNode *N, EVT Ty,
103                                           SelectionDAG &DAG,
104                                           unsigned Flag) const {
105   return DAG.getTargetBlockAddress(N->getBlockAddress(), Ty, 0, Flag);
106 }
107
108 SDValue MipsTargetLowering::getTargetNode(JumpTableSDNode *N, EVT Ty,
109                                           SelectionDAG &DAG,
110                                           unsigned Flag) const {
111   return DAG.getTargetJumpTable(N->getIndex(), Ty, Flag);
112 }
113
114 SDValue MipsTargetLowering::getTargetNode(ConstantPoolSDNode *N, EVT Ty,
115                                           SelectionDAG &DAG,
116                                           unsigned Flag) const {
117   return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlignment(),
118                                    N->getOffset(), Flag);
119 }
120
121 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
122   switch (Opcode) {
123   case MipsISD::JmpLink:           return "MipsISD::JmpLink";
124   case MipsISD::TailCall:          return "MipsISD::TailCall";
125   case MipsISD::Hi:                return "MipsISD::Hi";
126   case MipsISD::Lo:                return "MipsISD::Lo";
127   case MipsISD::GPRel:             return "MipsISD::GPRel";
128   case MipsISD::ThreadPointer:     return "MipsISD::ThreadPointer";
129   case MipsISD::Ret:               return "MipsISD::Ret";
130   case MipsISD::EH_RETURN:         return "MipsISD::EH_RETURN";
131   case MipsISD::FPBrcond:          return "MipsISD::FPBrcond";
132   case MipsISD::FPCmp:             return "MipsISD::FPCmp";
133   case MipsISD::CMovFP_T:          return "MipsISD::CMovFP_T";
134   case MipsISD::CMovFP_F:          return "MipsISD::CMovFP_F";
135   case MipsISD::TruncIntFP:        return "MipsISD::TruncIntFP";
136   case MipsISD::MFHI:              return "MipsISD::MFHI";
137   case MipsISD::MFLO:              return "MipsISD::MFLO";
138   case MipsISD::MTLOHI:            return "MipsISD::MTLOHI";
139   case MipsISD::Mult:              return "MipsISD::Mult";
140   case MipsISD::Multu:             return "MipsISD::Multu";
141   case MipsISD::MAdd:              return "MipsISD::MAdd";
142   case MipsISD::MAddu:             return "MipsISD::MAddu";
143   case MipsISD::MSub:              return "MipsISD::MSub";
144   case MipsISD::MSubu:             return "MipsISD::MSubu";
145   case MipsISD::DivRem:            return "MipsISD::DivRem";
146   case MipsISD::DivRemU:           return "MipsISD::DivRemU";
147   case MipsISD::DivRem16:          return "MipsISD::DivRem16";
148   case MipsISD::DivRemU16:         return "MipsISD::DivRemU16";
149   case MipsISD::BuildPairF64:      return "MipsISD::BuildPairF64";
150   case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
151   case MipsISD::Wrapper:           return "MipsISD::Wrapper";
152   case MipsISD::Sync:              return "MipsISD::Sync";
153   case MipsISD::Ext:               return "MipsISD::Ext";
154   case MipsISD::Ins:               return "MipsISD::Ins";
155   case MipsISD::LWL:               return "MipsISD::LWL";
156   case MipsISD::LWR:               return "MipsISD::LWR";
157   case MipsISD::SWL:               return "MipsISD::SWL";
158   case MipsISD::SWR:               return "MipsISD::SWR";
159   case MipsISD::LDL:               return "MipsISD::LDL";
160   case MipsISD::LDR:               return "MipsISD::LDR";
161   case MipsISD::SDL:               return "MipsISD::SDL";
162   case MipsISD::SDR:               return "MipsISD::SDR";
163   case MipsISD::EXTP:              return "MipsISD::EXTP";
164   case MipsISD::EXTPDP:            return "MipsISD::EXTPDP";
165   case MipsISD::EXTR_S_H:          return "MipsISD::EXTR_S_H";
166   case MipsISD::EXTR_W:            return "MipsISD::EXTR_W";
167   case MipsISD::EXTR_R_W:          return "MipsISD::EXTR_R_W";
168   case MipsISD::EXTR_RS_W:         return "MipsISD::EXTR_RS_W";
169   case MipsISD::SHILO:             return "MipsISD::SHILO";
170   case MipsISD::MTHLIP:            return "MipsISD::MTHLIP";
171   case MipsISD::MULT:              return "MipsISD::MULT";
172   case MipsISD::MULTU:             return "MipsISD::MULTU";
173   case MipsISD::MADD_DSP:          return "MipsISD::MADD_DSP";
174   case MipsISD::MADDU_DSP:         return "MipsISD::MADDU_DSP";
175   case MipsISD::MSUB_DSP:          return "MipsISD::MSUB_DSP";
176   case MipsISD::MSUBU_DSP:         return "MipsISD::MSUBU_DSP";
177   case MipsISD::SHLL_DSP:          return "MipsISD::SHLL_DSP";
178   case MipsISD::SHRA_DSP:          return "MipsISD::SHRA_DSP";
179   case MipsISD::SHRL_DSP:          return "MipsISD::SHRL_DSP";
180   case MipsISD::SETCC_DSP:         return "MipsISD::SETCC_DSP";
181   case MipsISD::SELECT_CC_DSP:     return "MipsISD::SELECT_CC_DSP";
182   case MipsISD::VALL_ZERO:         return "MipsISD::VALL_ZERO";
183   case MipsISD::VANY_ZERO:         return "MipsISD::VANY_ZERO";
184   case MipsISD::VALL_NONZERO:      return "MipsISD::VALL_NONZERO";
185   case MipsISD::VANY_NONZERO:      return "MipsISD::VANY_NONZERO";
186   case MipsISD::VCEQ:              return "MipsISD::VCEQ";
187   case MipsISD::VCLE_S:            return "MipsISD::VCLE_S";
188   case MipsISD::VCLE_U:            return "MipsISD::VCLE_U";
189   case MipsISD::VCLT_S:            return "MipsISD::VCLT_S";
190   case MipsISD::VCLT_U:            return "MipsISD::VCLT_U";
191   case MipsISD::VSMAX:             return "MipsISD::VSMAX";
192   case MipsISD::VSMIN:             return "MipsISD::VSMIN";
193   case MipsISD::VUMAX:             return "MipsISD::VUMAX";
194   case MipsISD::VUMIN:             return "MipsISD::VUMIN";
195   case MipsISD::VEXTRACT_SEXT_ELT: return "MipsISD::VEXTRACT_SEXT_ELT";
196   case MipsISD::VEXTRACT_ZEXT_ELT: return "MipsISD::VEXTRACT_ZEXT_ELT";
197   case MipsISD::VNOR:              return "MipsISD::VNOR";
198   case MipsISD::VSHF:              return "MipsISD::VSHF";
199   case MipsISD::SHF:               return "MipsISD::SHF";
200   case MipsISD::ILVEV:             return "MipsISD::ILVEV";
201   case MipsISD::ILVOD:             return "MipsISD::ILVOD";
202   case MipsISD::ILVL:              return "MipsISD::ILVL";
203   case MipsISD::ILVR:              return "MipsISD::ILVR";
204   case MipsISD::PCKEV:             return "MipsISD::PCKEV";
205   case MipsISD::PCKOD:             return "MipsISD::PCKOD";
206   case MipsISD::INSVE:             return "MipsISD::INSVE";
207   default:                         return nullptr;
208   }
209 }
210
211 MipsTargetLowering::MipsTargetLowering(MipsTargetMachine &TM)
212     : TargetLowering(TM, new MipsTargetObjectFile()),
213       Subtarget(&TM.getSubtarget<MipsSubtarget>()) {
214   // Mips does not have i1 type, so use i32 for
215   // setcc operations results (slt, sgt, ...).
216   setBooleanContents(ZeroOrOneBooleanContent);
217   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
218
219   // Load extented operations for i1 types must be promoted
220   setLoadExtAction(ISD::EXTLOAD,  MVT::i1,  Promote);
221   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
222   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
223
224   // MIPS doesn't have extending float->double load/store
225   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
226   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
227
228   // Used by legalize types to correctly generate the setcc result.
229   // Without this, every float setcc comes with a AND/OR with the result,
230   // we don't want this, since the fpcmp result goes to a flag register,
231   // which is used implicitly by brcond and select operations.
232   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
233
234   // Mips Custom Operations
235   setOperationAction(ISD::BR_JT,              MVT::Other, Custom);
236   setOperationAction(ISD::GlobalAddress,      MVT::i32,   Custom);
237   setOperationAction(ISD::BlockAddress,       MVT::i32,   Custom);
238   setOperationAction(ISD::GlobalTLSAddress,   MVT::i32,   Custom);
239   setOperationAction(ISD::JumpTable,          MVT::i32,   Custom);
240   setOperationAction(ISD::ConstantPool,       MVT::i32,   Custom);
241   setOperationAction(ISD::SELECT,             MVT::f32,   Custom);
242   setOperationAction(ISD::SELECT,             MVT::f64,   Custom);
243   setOperationAction(ISD::SELECT,             MVT::i32,   Custom);
244   setOperationAction(ISD::SELECT_CC,          MVT::f32,   Custom);
245   setOperationAction(ISD::SELECT_CC,          MVT::f64,   Custom);
246   setOperationAction(ISD::SETCC,              MVT::f32,   Custom);
247   setOperationAction(ISD::SETCC,              MVT::f64,   Custom);
248   setOperationAction(ISD::BRCOND,             MVT::Other, Custom);
249   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
250   setOperationAction(ISD::FCOPYSIGN,          MVT::f32,   Custom);
251   setOperationAction(ISD::FCOPYSIGN,          MVT::f64,   Custom);
252   setOperationAction(ISD::FP_TO_SINT,         MVT::i32,   Custom);
253
254   if (Subtarget->isGP64bit()) {
255     setOperationAction(ISD::GlobalAddress,      MVT::i64,   Custom);
256     setOperationAction(ISD::BlockAddress,       MVT::i64,   Custom);
257     setOperationAction(ISD::GlobalTLSAddress,   MVT::i64,   Custom);
258     setOperationAction(ISD::JumpTable,          MVT::i64,   Custom);
259     setOperationAction(ISD::ConstantPool,       MVT::i64,   Custom);
260     setOperationAction(ISD::SELECT,             MVT::i64,   Custom);
261     setOperationAction(ISD::LOAD,               MVT::i64,   Custom);
262     setOperationAction(ISD::STORE,              MVT::i64,   Custom);
263     setOperationAction(ISD::FP_TO_SINT,         MVT::i64,   Custom);
264   }
265
266   if (!Subtarget->isGP64bit()) {
267     setOperationAction(ISD::SHL_PARTS,          MVT::i32,   Custom);
268     setOperationAction(ISD::SRA_PARTS,          MVT::i32,   Custom);
269     setOperationAction(ISD::SRL_PARTS,          MVT::i32,   Custom);
270   }
271
272   setOperationAction(ISD::ADD,                MVT::i32,   Custom);
273   if (Subtarget->isGP64bit())
274     setOperationAction(ISD::ADD,                MVT::i64,   Custom);
275
276   setOperationAction(ISD::SDIV, MVT::i32, Expand);
277   setOperationAction(ISD::SREM, MVT::i32, Expand);
278   setOperationAction(ISD::UDIV, MVT::i32, Expand);
279   setOperationAction(ISD::UREM, MVT::i32, Expand);
280   setOperationAction(ISD::SDIV, MVT::i64, Expand);
281   setOperationAction(ISD::SREM, MVT::i64, Expand);
282   setOperationAction(ISD::UDIV, MVT::i64, Expand);
283   setOperationAction(ISD::UREM, MVT::i64, Expand);
284
285   // Operations not directly supported by Mips.
286   setOperationAction(ISD::BR_CC,             MVT::f32,   Expand);
287   setOperationAction(ISD::BR_CC,             MVT::f64,   Expand);
288   setOperationAction(ISD::BR_CC,             MVT::i32,   Expand);
289   setOperationAction(ISD::BR_CC,             MVT::i64,   Expand);
290   setOperationAction(ISD::SELECT_CC,         MVT::i32,   Expand);
291   setOperationAction(ISD::SELECT_CC,         MVT::i64,   Expand);
292   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
293   setOperationAction(ISD::UINT_TO_FP,        MVT::i64,   Expand);
294   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
295   setOperationAction(ISD::FP_TO_UINT,        MVT::i64,   Expand);
296   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
297   if (Subtarget->hasCnMips()) {
298     setOperationAction(ISD::CTPOP,           MVT::i32,   Legal);
299     setOperationAction(ISD::CTPOP,           MVT::i64,   Legal);
300   } else {
301     setOperationAction(ISD::CTPOP,           MVT::i32,   Expand);
302     setOperationAction(ISD::CTPOP,           MVT::i64,   Expand);
303   }
304   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
305   setOperationAction(ISD::CTTZ,              MVT::i64,   Expand);
306   setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::i32,   Expand);
307   setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::i64,   Expand);
308   setOperationAction(ISD::CTLZ_ZERO_UNDEF,   MVT::i32,   Expand);
309   setOperationAction(ISD::CTLZ_ZERO_UNDEF,   MVT::i64,   Expand);
310   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
311   setOperationAction(ISD::ROTL,              MVT::i64,   Expand);
312   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,  Expand);
313   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64,  Expand);
314
315   if (!Subtarget->hasMips32r2())
316     setOperationAction(ISD::ROTR, MVT::i32,   Expand);
317
318   if (!Subtarget->hasMips64r2())
319     setOperationAction(ISD::ROTR, MVT::i64,   Expand);
320
321   setOperationAction(ISD::FSIN,              MVT::f32,   Expand);
322   setOperationAction(ISD::FSIN,              MVT::f64,   Expand);
323   setOperationAction(ISD::FCOS,              MVT::f32,   Expand);
324   setOperationAction(ISD::FCOS,              MVT::f64,   Expand);
325   setOperationAction(ISD::FSINCOS,           MVT::f32,   Expand);
326   setOperationAction(ISD::FSINCOS,           MVT::f64,   Expand);
327   setOperationAction(ISD::FPOWI,             MVT::f32,   Expand);
328   setOperationAction(ISD::FPOW,              MVT::f32,   Expand);
329   setOperationAction(ISD::FPOW,              MVT::f64,   Expand);
330   setOperationAction(ISD::FLOG,              MVT::f32,   Expand);
331   setOperationAction(ISD::FLOG2,             MVT::f32,   Expand);
332   setOperationAction(ISD::FLOG10,            MVT::f32,   Expand);
333   setOperationAction(ISD::FEXP,              MVT::f32,   Expand);
334   setOperationAction(ISD::FMA,               MVT::f32,   Expand);
335   setOperationAction(ISD::FMA,               MVT::f64,   Expand);
336   setOperationAction(ISD::FREM,              MVT::f32,   Expand);
337   setOperationAction(ISD::FREM,              MVT::f64,   Expand);
338
339   setOperationAction(ISD::EH_RETURN, MVT::Other, Custom);
340
341   setOperationAction(ISD::VAARG,             MVT::Other, Expand);
342   setOperationAction(ISD::VACOPY,            MVT::Other, Expand);
343   setOperationAction(ISD::VAEND,             MVT::Other, Expand);
344
345   // Use the default for now
346   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
347   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
348
349   setOperationAction(ISD::ATOMIC_LOAD,       MVT::i32,    Expand);
350   setOperationAction(ISD::ATOMIC_LOAD,       MVT::i64,    Expand);
351   setOperationAction(ISD::ATOMIC_STORE,      MVT::i32,    Expand);
352   setOperationAction(ISD::ATOMIC_STORE,      MVT::i64,    Expand);
353
354   setInsertFencesForAtomic(true);
355
356   if (!Subtarget->hasMips32r2()) {
357     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
358     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
359   }
360
361   // MIPS16 lacks MIPS32's clz and clo instructions.
362   if (!Subtarget->hasMips32() || Subtarget->inMips16Mode())
363     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
364   if (!Subtarget->hasMips64())
365     setOperationAction(ISD::CTLZ, MVT::i64, Expand);
366
367   if (!Subtarget->hasMips32r2())
368     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
369   if (!Subtarget->hasMips64r2())
370     setOperationAction(ISD::BSWAP, MVT::i64, Expand);
371
372   if (Subtarget->isGP64bit()) {
373     setLoadExtAction(ISD::SEXTLOAD, MVT::i32, Custom);
374     setLoadExtAction(ISD::ZEXTLOAD, MVT::i32, Custom);
375     setLoadExtAction(ISD::EXTLOAD, MVT::i32, Custom);
376     setTruncStoreAction(MVT::i64, MVT::i32, Custom);
377   }
378
379   setOperationAction(ISD::TRAP, MVT::Other, Legal);
380
381   setTargetDAGCombine(ISD::SDIVREM);
382   setTargetDAGCombine(ISD::UDIVREM);
383   setTargetDAGCombine(ISD::SELECT);
384   setTargetDAGCombine(ISD::AND);
385   setTargetDAGCombine(ISD::OR);
386   setTargetDAGCombine(ISD::ADD);
387
388   setMinFunctionAlignment(Subtarget->isGP64bit() ? 3 : 2);
389
390   setStackPointerRegisterToSaveRestore(Subtarget->isABI_N64() ? Mips::SP_64
391                                                               : Mips::SP);
392
393   setExceptionPointerRegister(Subtarget->isABI_N64() ? Mips::A0_64 : Mips::A0);
394   setExceptionSelectorRegister(Subtarget->isABI_N64() ? Mips::A1_64 : Mips::A1);
395
396   MaxStoresPerMemcpy = 16;
397
398   isMicroMips = Subtarget->inMicroMipsMode();
399 }
400
401 const MipsTargetLowering *MipsTargetLowering::create(MipsTargetMachine &TM) {
402   if (TM.getSubtargetImpl()->inMips16Mode())
403     return llvm::createMips16TargetLowering(TM);
404
405   return llvm::createMipsSETargetLowering(TM);
406 }
407
408 // Create a fast isel object.
409 FastISel *
410 MipsTargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
411                                   const TargetLibraryInfo *libInfo) const {
412   if (!EnableMipsFastISel)
413     return TargetLowering::createFastISel(funcInfo, libInfo);
414   return Mips::createFastISel(funcInfo, libInfo);
415 }
416
417 EVT MipsTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
418   if (!VT.isVector())
419     return MVT::i32;
420   return VT.changeVectorElementTypeToInteger();
421 }
422
423 static SDValue performDivRemCombine(SDNode *N, SelectionDAG &DAG,
424                                     TargetLowering::DAGCombinerInfo &DCI,
425                                     const MipsSubtarget *Subtarget) {
426   if (DCI.isBeforeLegalizeOps())
427     return SDValue();
428
429   EVT Ty = N->getValueType(0);
430   unsigned LO = (Ty == MVT::i32) ? Mips::LO0 : Mips::LO0_64;
431   unsigned HI = (Ty == MVT::i32) ? Mips::HI0 : Mips::HI0_64;
432   unsigned Opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem16 :
433                                                   MipsISD::DivRemU16;
434   SDLoc DL(N);
435
436   SDValue DivRem = DAG.getNode(Opc, DL, MVT::Glue,
437                                N->getOperand(0), N->getOperand(1));
438   SDValue InChain = DAG.getEntryNode();
439   SDValue InGlue = DivRem;
440
441   // insert MFLO
442   if (N->hasAnyUseOfValue(0)) {
443     SDValue CopyFromLo = DAG.getCopyFromReg(InChain, DL, LO, Ty,
444                                             InGlue);
445     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
446     InChain = CopyFromLo.getValue(1);
447     InGlue = CopyFromLo.getValue(2);
448   }
449
450   // insert MFHI
451   if (N->hasAnyUseOfValue(1)) {
452     SDValue CopyFromHi = DAG.getCopyFromReg(InChain, DL,
453                                             HI, Ty, InGlue);
454     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
455   }
456
457   return SDValue();
458 }
459
460 static Mips::CondCode condCodeToFCC(ISD::CondCode CC) {
461   switch (CC) {
462   default: llvm_unreachable("Unknown fp condition code!");
463   case ISD::SETEQ:
464   case ISD::SETOEQ: return Mips::FCOND_OEQ;
465   case ISD::SETUNE: return Mips::FCOND_UNE;
466   case ISD::SETLT:
467   case ISD::SETOLT: return Mips::FCOND_OLT;
468   case ISD::SETGT:
469   case ISD::SETOGT: return Mips::FCOND_OGT;
470   case ISD::SETLE:
471   case ISD::SETOLE: return Mips::FCOND_OLE;
472   case ISD::SETGE:
473   case ISD::SETOGE: return Mips::FCOND_OGE;
474   case ISD::SETULT: return Mips::FCOND_ULT;
475   case ISD::SETULE: return Mips::FCOND_ULE;
476   case ISD::SETUGT: return Mips::FCOND_UGT;
477   case ISD::SETUGE: return Mips::FCOND_UGE;
478   case ISD::SETUO:  return Mips::FCOND_UN;
479   case ISD::SETO:   return Mips::FCOND_OR;
480   case ISD::SETNE:
481   case ISD::SETONE: return Mips::FCOND_ONE;
482   case ISD::SETUEQ: return Mips::FCOND_UEQ;
483   }
484 }
485
486
487 /// This function returns true if the floating point conditional branches and
488 /// conditional moves which use condition code CC should be inverted.
489 static bool invertFPCondCodeUser(Mips::CondCode CC) {
490   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
491     return false;
492
493   assert((CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT) &&
494          "Illegal Condition Code");
495
496   return true;
497 }
498
499 // Creates and returns an FPCmp node from a setcc node.
500 // Returns Op if setcc is not a floating point comparison.
501 static SDValue createFPCmp(SelectionDAG &DAG, const SDValue &Op) {
502   // must be a SETCC node
503   if (Op.getOpcode() != ISD::SETCC)
504     return Op;
505
506   SDValue LHS = Op.getOperand(0);
507
508   if (!LHS.getValueType().isFloatingPoint())
509     return Op;
510
511   SDValue RHS = Op.getOperand(1);
512   SDLoc DL(Op);
513
514   // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
515   // node if necessary.
516   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
517
518   return DAG.getNode(MipsISD::FPCmp, DL, MVT::Glue, LHS, RHS,
519                      DAG.getConstant(condCodeToFCC(CC), MVT::i32));
520 }
521
522 // Creates and returns a CMovFPT/F node.
523 static SDValue createCMovFP(SelectionDAG &DAG, SDValue Cond, SDValue True,
524                             SDValue False, SDLoc DL) {
525   ConstantSDNode *CC = cast<ConstantSDNode>(Cond.getOperand(2));
526   bool invert = invertFPCondCodeUser((Mips::CondCode)CC->getSExtValue());
527   SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
528
529   return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
530                      True.getValueType(), True, FCC0, False, Cond);
531 }
532
533 static SDValue performSELECTCombine(SDNode *N, SelectionDAG &DAG,
534                                     TargetLowering::DAGCombinerInfo &DCI,
535                                     const MipsSubtarget *Subtarget) {
536   if (DCI.isBeforeLegalizeOps())
537     return SDValue();
538
539   SDValue SetCC = N->getOperand(0);
540
541   if ((SetCC.getOpcode() != ISD::SETCC) ||
542       !SetCC.getOperand(0).getValueType().isInteger())
543     return SDValue();
544
545   SDValue False = N->getOperand(2);
546   EVT FalseTy = False.getValueType();
547
548   if (!FalseTy.isInteger())
549     return SDValue();
550
551   ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(False);
552
553   // If the RHS (False) is 0, we swap the order of the operands
554   // of ISD::SELECT (obviously also inverting the condition) so that we can
555   // take advantage of conditional moves using the $0 register.
556   // Example:
557   //   return (a != 0) ? x : 0;
558   //     load $reg, x
559   //     movz $reg, $0, a
560   if (!FalseC)
561     return SDValue();
562
563   const SDLoc DL(N);
564
565   if (!FalseC->getZExtValue()) {
566     ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
567     SDValue True = N->getOperand(1);
568
569     SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
570                          SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
571
572     return DAG.getNode(ISD::SELECT, DL, FalseTy, SetCC, False, True);
573   }
574
575   // If both operands are integer constants there's a possibility that we
576   // can do some interesting optimizations.
577   SDValue True = N->getOperand(1);
578   ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(True);
579
580   if (!TrueC || !True.getValueType().isInteger())
581     return SDValue();
582
583   // We'll also ignore MVT::i64 operands as this optimizations proves
584   // to be ineffective because of the required sign extensions as the result
585   // of a SETCC operator is always MVT::i32 for non-vector types.
586   if (True.getValueType() == MVT::i64)
587     return SDValue();
588
589   int64_t Diff = TrueC->getSExtValue() - FalseC->getSExtValue();
590
591   // 1)  (a < x) ? y : y-1
592   //  slti $reg1, a, x
593   //  addiu $reg2, $reg1, y-1
594   if (Diff == 1)
595     return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, False);
596
597   // 2)  (a < x) ? y-1 : y
598   //  slti $reg1, a, x
599   //  xor $reg1, $reg1, 1
600   //  addiu $reg2, $reg1, y-1
601   if (Diff == -1) {
602     ISD::CondCode CC = cast<CondCodeSDNode>(SetCC.getOperand(2))->get();
603     SetCC = DAG.getSetCC(DL, SetCC.getValueType(), SetCC.getOperand(0),
604                          SetCC.getOperand(1), ISD::getSetCCInverse(CC, true));
605     return DAG.getNode(ISD::ADD, DL, SetCC.getValueType(), SetCC, True);
606   }
607
608   // Couldn't optimize.
609   return SDValue();
610 }
611
612 static SDValue performANDCombine(SDNode *N, SelectionDAG &DAG,
613                                  TargetLowering::DAGCombinerInfo &DCI,
614                                  const MipsSubtarget *Subtarget) {
615   // Pattern match EXT.
616   //  $dst = and ((sra or srl) $src , pos), (2**size - 1)
617   //  => ext $dst, $src, size, pos
618   if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
619     return SDValue();
620
621   SDValue ShiftRight = N->getOperand(0), Mask = N->getOperand(1);
622   unsigned ShiftRightOpc = ShiftRight.getOpcode();
623
624   // Op's first operand must be a shift right.
625   if (ShiftRightOpc != ISD::SRA && ShiftRightOpc != ISD::SRL)
626     return SDValue();
627
628   // The second operand of the shift must be an immediate.
629   ConstantSDNode *CN;
630   if (!(CN = dyn_cast<ConstantSDNode>(ShiftRight.getOperand(1))))
631     return SDValue();
632
633   uint64_t Pos = CN->getZExtValue();
634   uint64_t SMPos, SMSize;
635
636   // Op's second operand must be a shifted mask.
637   if (!(CN = dyn_cast<ConstantSDNode>(Mask)) ||
638       !isShiftedMask(CN->getZExtValue(), SMPos, SMSize))
639     return SDValue();
640
641   // Return if the shifted mask does not start at bit 0 or the sum of its size
642   // and Pos exceeds the word's size.
643   EVT ValTy = N->getValueType(0);
644   if (SMPos != 0 || Pos + SMSize > ValTy.getSizeInBits())
645     return SDValue();
646
647   return DAG.getNode(MipsISD::Ext, SDLoc(N), ValTy,
648                      ShiftRight.getOperand(0), DAG.getConstant(Pos, MVT::i32),
649                      DAG.getConstant(SMSize, MVT::i32));
650 }
651
652 static SDValue performORCombine(SDNode *N, SelectionDAG &DAG,
653                                 TargetLowering::DAGCombinerInfo &DCI,
654                                 const MipsSubtarget *Subtarget) {
655   // Pattern match INS.
656   //  $dst = or (and $src1 , mask0), (and (shl $src, pos), mask1),
657   //  where mask1 = (2**size - 1) << pos, mask0 = ~mask1
658   //  => ins $dst, $src, size, pos, $src1
659   if (DCI.isBeforeLegalizeOps() || !Subtarget->hasExtractInsert())
660     return SDValue();
661
662   SDValue And0 = N->getOperand(0), And1 = N->getOperand(1);
663   uint64_t SMPos0, SMSize0, SMPos1, SMSize1;
664   ConstantSDNode *CN;
665
666   // See if Op's first operand matches (and $src1 , mask0).
667   if (And0.getOpcode() != ISD::AND)
668     return SDValue();
669
670   if (!(CN = dyn_cast<ConstantSDNode>(And0.getOperand(1))) ||
671       !isShiftedMask(~CN->getSExtValue(), SMPos0, SMSize0))
672     return SDValue();
673
674   // See if Op's second operand matches (and (shl $src, pos), mask1).
675   if (And1.getOpcode() != ISD::AND)
676     return SDValue();
677
678   if (!(CN = dyn_cast<ConstantSDNode>(And1.getOperand(1))) ||
679       !isShiftedMask(CN->getZExtValue(), SMPos1, SMSize1))
680     return SDValue();
681
682   // The shift masks must have the same position and size.
683   if (SMPos0 != SMPos1 || SMSize0 != SMSize1)
684     return SDValue();
685
686   SDValue Shl = And1.getOperand(0);
687   if (Shl.getOpcode() != ISD::SHL)
688     return SDValue();
689
690   if (!(CN = dyn_cast<ConstantSDNode>(Shl.getOperand(1))))
691     return SDValue();
692
693   unsigned Shamt = CN->getZExtValue();
694
695   // Return if the shift amount and the first bit position of mask are not the
696   // same.
697   EVT ValTy = N->getValueType(0);
698   if ((Shamt != SMPos0) || (SMPos0 + SMSize0 > ValTy.getSizeInBits()))
699     return SDValue();
700
701   return DAG.getNode(MipsISD::Ins, SDLoc(N), ValTy, Shl.getOperand(0),
702                      DAG.getConstant(SMPos0, MVT::i32),
703                      DAG.getConstant(SMSize0, MVT::i32), And0.getOperand(0));
704 }
705
706 static SDValue performADDCombine(SDNode *N, SelectionDAG &DAG,
707                                  TargetLowering::DAGCombinerInfo &DCI,
708                                  const MipsSubtarget *Subtarget) {
709   // (add v0, (add v1, abs_lo(tjt))) => (add (add v0, v1), abs_lo(tjt))
710
711   if (DCI.isBeforeLegalizeOps())
712     return SDValue();
713
714   SDValue Add = N->getOperand(1);
715
716   if (Add.getOpcode() != ISD::ADD)
717     return SDValue();
718
719   SDValue Lo = Add.getOperand(1);
720
721   if ((Lo.getOpcode() != MipsISD::Lo) ||
722       (Lo.getOperand(0).getOpcode() != ISD::TargetJumpTable))
723     return SDValue();
724
725   EVT ValTy = N->getValueType(0);
726   SDLoc DL(N);
727
728   SDValue Add1 = DAG.getNode(ISD::ADD, DL, ValTy, N->getOperand(0),
729                              Add.getOperand(0));
730   return DAG.getNode(ISD::ADD, DL, ValTy, Add1, Lo);
731 }
732
733 SDValue  MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
734   const {
735   SelectionDAG &DAG = DCI.DAG;
736   unsigned Opc = N->getOpcode();
737
738   switch (Opc) {
739   default: break;
740   case ISD::SDIVREM:
741   case ISD::UDIVREM:
742     return performDivRemCombine(N, DAG, DCI, Subtarget);
743   case ISD::SELECT:
744     return performSELECTCombine(N, DAG, DCI, Subtarget);
745   case ISD::AND:
746     return performANDCombine(N, DAG, DCI, Subtarget);
747   case ISD::OR:
748     return performORCombine(N, DAG, DCI, Subtarget);
749   case ISD::ADD:
750     return performADDCombine(N, DAG, DCI, Subtarget);
751   }
752
753   return SDValue();
754 }
755
756 void
757 MipsTargetLowering::LowerOperationWrapper(SDNode *N,
758                                           SmallVectorImpl<SDValue> &Results,
759                                           SelectionDAG &DAG) const {
760   SDValue Res = LowerOperation(SDValue(N, 0), DAG);
761
762   for (unsigned I = 0, E = Res->getNumValues(); I != E; ++I)
763     Results.push_back(Res.getValue(I));
764 }
765
766 void
767 MipsTargetLowering::ReplaceNodeResults(SDNode *N,
768                                        SmallVectorImpl<SDValue> &Results,
769                                        SelectionDAG &DAG) const {
770   return LowerOperationWrapper(N, Results, DAG);
771 }
772
773 SDValue MipsTargetLowering::
774 LowerOperation(SDValue Op, SelectionDAG &DAG) const
775 {
776   switch (Op.getOpcode())
777   {
778   case ISD::BR_JT:              return lowerBR_JT(Op, DAG);
779   case ISD::BRCOND:             return lowerBRCOND(Op, DAG);
780   case ISD::ConstantPool:       return lowerConstantPool(Op, DAG);
781   case ISD::GlobalAddress:      return lowerGlobalAddress(Op, DAG);
782   case ISD::BlockAddress:       return lowerBlockAddress(Op, DAG);
783   case ISD::GlobalTLSAddress:   return lowerGlobalTLSAddress(Op, DAG);
784   case ISD::JumpTable:          return lowerJumpTable(Op, DAG);
785   case ISD::SELECT:             return lowerSELECT(Op, DAG);
786   case ISD::SELECT_CC:          return lowerSELECT_CC(Op, DAG);
787   case ISD::SETCC:              return lowerSETCC(Op, DAG);
788   case ISD::VASTART:            return lowerVASTART(Op, DAG);
789   case ISD::FCOPYSIGN:          return lowerFCOPYSIGN(Op, DAG);
790   case ISD::FRAMEADDR:          return lowerFRAMEADDR(Op, DAG);
791   case ISD::RETURNADDR:         return lowerRETURNADDR(Op, DAG);
792   case ISD::EH_RETURN:          return lowerEH_RETURN(Op, DAG);
793   case ISD::ATOMIC_FENCE:       return lowerATOMIC_FENCE(Op, DAG);
794   case ISD::SHL_PARTS:          return lowerShiftLeftParts(Op, DAG);
795   case ISD::SRA_PARTS:          return lowerShiftRightParts(Op, DAG, true);
796   case ISD::SRL_PARTS:          return lowerShiftRightParts(Op, DAG, false);
797   case ISD::LOAD:               return lowerLOAD(Op, DAG);
798   case ISD::STORE:              return lowerSTORE(Op, DAG);
799   case ISD::ADD:                return lowerADD(Op, DAG);
800   case ISD::FP_TO_SINT:         return lowerFP_TO_SINT(Op, DAG);
801   }
802   return SDValue();
803 }
804
805 //===----------------------------------------------------------------------===//
806 //  Lower helper functions
807 //===----------------------------------------------------------------------===//
808
809 // addLiveIn - This helper function adds the specified physical register to the
810 // MachineFunction as a live in value.  It also creates a corresponding
811 // virtual register for it.
812 static unsigned
813 addLiveIn(MachineFunction &MF, unsigned PReg, const TargetRegisterClass *RC)
814 {
815   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
816   MF.getRegInfo().addLiveIn(PReg, VReg);
817   return VReg;
818 }
819
820 static MachineBasicBlock *insertDivByZeroTrap(MachineInstr *MI,
821                                               MachineBasicBlock &MBB,
822                                               const TargetInstrInfo &TII,
823                                               bool Is64Bit) {
824   if (NoZeroDivCheck)
825     return &MBB;
826
827   // Insert instruction "teq $divisor_reg, $zero, 7".
828   MachineBasicBlock::iterator I(MI);
829   MachineInstrBuilder MIB;
830   MachineOperand &Divisor = MI->getOperand(2);
831   MIB = BuildMI(MBB, std::next(I), MI->getDebugLoc(), TII.get(Mips::TEQ))
832     .addReg(Divisor.getReg(), getKillRegState(Divisor.isKill()))
833     .addReg(Mips::ZERO).addImm(7);
834
835   // Use the 32-bit sub-register if this is a 64-bit division.
836   if (Is64Bit)
837     MIB->getOperand(0).setSubReg(Mips::sub_32);
838
839   // Clear Divisor's kill flag.
840   Divisor.setIsKill(false);
841
842   // We would normally delete the original instruction here but in this case
843   // we only needed to inject an additional instruction rather than replace it.
844
845   return &MBB;
846 }
847
848 MachineBasicBlock *
849 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
850                                                 MachineBasicBlock *BB) const {
851   switch (MI->getOpcode()) {
852   default:
853     llvm_unreachable("Unexpected instr type to insert");
854   case Mips::ATOMIC_LOAD_ADD_I8:
855     return emitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
856   case Mips::ATOMIC_LOAD_ADD_I16:
857     return emitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
858   case Mips::ATOMIC_LOAD_ADD_I32:
859     return emitAtomicBinary(MI, BB, 4, Mips::ADDu);
860   case Mips::ATOMIC_LOAD_ADD_I64:
861     return emitAtomicBinary(MI, BB, 8, Mips::DADDu);
862
863   case Mips::ATOMIC_LOAD_AND_I8:
864     return emitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
865   case Mips::ATOMIC_LOAD_AND_I16:
866     return emitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
867   case Mips::ATOMIC_LOAD_AND_I32:
868     return emitAtomicBinary(MI, BB, 4, Mips::AND);
869   case Mips::ATOMIC_LOAD_AND_I64:
870     return emitAtomicBinary(MI, BB, 8, Mips::AND64);
871
872   case Mips::ATOMIC_LOAD_OR_I8:
873     return emitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
874   case Mips::ATOMIC_LOAD_OR_I16:
875     return emitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
876   case Mips::ATOMIC_LOAD_OR_I32:
877     return emitAtomicBinary(MI, BB, 4, Mips::OR);
878   case Mips::ATOMIC_LOAD_OR_I64:
879     return emitAtomicBinary(MI, BB, 8, Mips::OR64);
880
881   case Mips::ATOMIC_LOAD_XOR_I8:
882     return emitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
883   case Mips::ATOMIC_LOAD_XOR_I16:
884     return emitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
885   case Mips::ATOMIC_LOAD_XOR_I32:
886     return emitAtomicBinary(MI, BB, 4, Mips::XOR);
887   case Mips::ATOMIC_LOAD_XOR_I64:
888     return emitAtomicBinary(MI, BB, 8, Mips::XOR64);
889
890   case Mips::ATOMIC_LOAD_NAND_I8:
891     return emitAtomicBinaryPartword(MI, BB, 1, 0, true);
892   case Mips::ATOMIC_LOAD_NAND_I16:
893     return emitAtomicBinaryPartword(MI, BB, 2, 0, true);
894   case Mips::ATOMIC_LOAD_NAND_I32:
895     return emitAtomicBinary(MI, BB, 4, 0, true);
896   case Mips::ATOMIC_LOAD_NAND_I64:
897     return emitAtomicBinary(MI, BB, 8, 0, true);
898
899   case Mips::ATOMIC_LOAD_SUB_I8:
900     return emitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
901   case Mips::ATOMIC_LOAD_SUB_I16:
902     return emitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
903   case Mips::ATOMIC_LOAD_SUB_I32:
904     return emitAtomicBinary(MI, BB, 4, Mips::SUBu);
905   case Mips::ATOMIC_LOAD_SUB_I64:
906     return emitAtomicBinary(MI, BB, 8, Mips::DSUBu);
907
908   case Mips::ATOMIC_SWAP_I8:
909     return emitAtomicBinaryPartword(MI, BB, 1, 0);
910   case Mips::ATOMIC_SWAP_I16:
911     return emitAtomicBinaryPartword(MI, BB, 2, 0);
912   case Mips::ATOMIC_SWAP_I32:
913     return emitAtomicBinary(MI, BB, 4, 0);
914   case Mips::ATOMIC_SWAP_I64:
915     return emitAtomicBinary(MI, BB, 8, 0);
916
917   case Mips::ATOMIC_CMP_SWAP_I8:
918     return emitAtomicCmpSwapPartword(MI, BB, 1);
919   case Mips::ATOMIC_CMP_SWAP_I16:
920     return emitAtomicCmpSwapPartword(MI, BB, 2);
921   case Mips::ATOMIC_CMP_SWAP_I32:
922     return emitAtomicCmpSwap(MI, BB, 4);
923   case Mips::ATOMIC_CMP_SWAP_I64:
924     return emitAtomicCmpSwap(MI, BB, 8);
925   case Mips::PseudoSDIV:
926   case Mips::PseudoUDIV:
927   case Mips::DIV:
928   case Mips::DIVU:
929   case Mips::MOD:
930   case Mips::MODU:
931     return insertDivByZeroTrap(MI, *BB, *getTargetMachine().getInstrInfo(),
932                                false);
933   case Mips::PseudoDSDIV:
934   case Mips::PseudoDUDIV:
935   case Mips::DDIV:
936   case Mips::DDIVU:
937   case Mips::DMOD:
938   case Mips::DMODU:
939     return insertDivByZeroTrap(MI, *BB, *getTargetMachine().getInstrInfo(),
940                                true);
941   case Mips::SEL_D:
942     return emitSEL_D(MI, BB);
943   }
944 }
945
946 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
947 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
948 MachineBasicBlock *
949 MipsTargetLowering::emitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
950                                      unsigned Size, unsigned BinOpcode,
951                                      bool Nand) const {
952   assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicBinary.");
953
954   MachineFunction *MF = BB->getParent();
955   MachineRegisterInfo &RegInfo = MF->getRegInfo();
956   const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
957   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
958   DebugLoc DL = MI->getDebugLoc();
959   unsigned LL, SC, AND, NOR, ZERO, BEQ;
960
961   if (Size == 4) {
962     if (isMicroMips) {
963       LL = Mips::LL_MM;
964       SC = Mips::SC_MM;
965     } else {
966       LL = Subtarget->hasMips32r6() ? Mips::LL : Mips::LL_R6;
967       SC = Subtarget->hasMips32r6() ? Mips::SC : Mips::SC_R6;
968     }
969     AND = Mips::AND;
970     NOR = Mips::NOR;
971     ZERO = Mips::ZERO;
972     BEQ = Mips::BEQ;
973   } else {
974     LL = Subtarget->hasMips64r6() ? Mips::LLD : Mips::LLD_R6;
975     SC = Subtarget->hasMips64r6() ? Mips::SCD : Mips::SCD_R6;
976     AND = Mips::AND64;
977     NOR = Mips::NOR64;
978     ZERO = Mips::ZERO_64;
979     BEQ = Mips::BEQ64;
980   }
981
982   unsigned OldVal = MI->getOperand(0).getReg();
983   unsigned Ptr = MI->getOperand(1).getReg();
984   unsigned Incr = MI->getOperand(2).getReg();
985
986   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
987   unsigned AndRes = RegInfo.createVirtualRegister(RC);
988   unsigned Success = RegInfo.createVirtualRegister(RC);
989
990   // insert new blocks after the current block
991   const BasicBlock *LLVM_BB = BB->getBasicBlock();
992   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
993   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
994   MachineFunction::iterator It = BB;
995   ++It;
996   MF->insert(It, loopMBB);
997   MF->insert(It, exitMBB);
998
999   // Transfer the remainder of BB and its successor edges to exitMBB.
1000   exitMBB->splice(exitMBB->begin(), BB,
1001                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
1002   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1003
1004   //  thisMBB:
1005   //    ...
1006   //    fallthrough --> loopMBB
1007   BB->addSuccessor(loopMBB);
1008   loopMBB->addSuccessor(loopMBB);
1009   loopMBB->addSuccessor(exitMBB);
1010
1011   //  loopMBB:
1012   //    ll oldval, 0(ptr)
1013   //    <binop> storeval, oldval, incr
1014   //    sc success, storeval, 0(ptr)
1015   //    beq success, $0, loopMBB
1016   BB = loopMBB;
1017   BuildMI(BB, DL, TII->get(LL), OldVal).addReg(Ptr).addImm(0);
1018   if (Nand) {
1019     //  and andres, oldval, incr
1020     //  nor storeval, $0, andres
1021     BuildMI(BB, DL, TII->get(AND), AndRes).addReg(OldVal).addReg(Incr);
1022     BuildMI(BB, DL, TII->get(NOR), StoreVal).addReg(ZERO).addReg(AndRes);
1023   } else if (BinOpcode) {
1024     //  <binop> storeval, oldval, incr
1025     BuildMI(BB, DL, TII->get(BinOpcode), StoreVal).addReg(OldVal).addReg(Incr);
1026   } else {
1027     StoreVal = Incr;
1028   }
1029   BuildMI(BB, DL, TII->get(SC), Success).addReg(StoreVal).addReg(Ptr).addImm(0);
1030   BuildMI(BB, DL, TII->get(BEQ)).addReg(Success).addReg(ZERO).addMBB(loopMBB);
1031
1032   MI->eraseFromParent(); // The instruction is gone now.
1033
1034   return exitMBB;
1035 }
1036
1037 MachineBasicBlock *MipsTargetLowering::emitSignExtendToI32InReg(
1038     MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned DstReg,
1039     unsigned SrcReg) const {
1040   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1041   DebugLoc DL = MI->getDebugLoc();
1042
1043   if (Subtarget->hasMips32r2() && Size == 1) {
1044     BuildMI(BB, DL, TII->get(Mips::SEB), DstReg).addReg(SrcReg);
1045     return BB;
1046   }
1047
1048   if (Subtarget->hasMips32r2() && Size == 2) {
1049     BuildMI(BB, DL, TII->get(Mips::SEH), DstReg).addReg(SrcReg);
1050     return BB;
1051   }
1052
1053   MachineFunction *MF = BB->getParent();
1054   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1055   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1056   unsigned ScrReg = RegInfo.createVirtualRegister(RC);
1057
1058   assert(Size < 32);
1059   int64_t ShiftImm = 32 - (Size * 8);
1060
1061   BuildMI(BB, DL, TII->get(Mips::SLL), ScrReg).addReg(SrcReg).addImm(ShiftImm);
1062   BuildMI(BB, DL, TII->get(Mips::SRA), DstReg).addReg(ScrReg).addImm(ShiftImm);
1063
1064   return BB;
1065 }
1066
1067 MachineBasicBlock *MipsTargetLowering::emitAtomicBinaryPartword(
1068     MachineInstr *MI, MachineBasicBlock *BB, unsigned Size, unsigned BinOpcode,
1069     bool Nand) const {
1070   assert((Size == 1 || Size == 2) &&
1071          "Unsupported size for EmitAtomicBinaryPartial.");
1072
1073   MachineFunction *MF = BB->getParent();
1074   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1075   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1076   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1077   DebugLoc DL = MI->getDebugLoc();
1078
1079   unsigned Dest = MI->getOperand(0).getReg();
1080   unsigned Ptr = MI->getOperand(1).getReg();
1081   unsigned Incr = MI->getOperand(2).getReg();
1082
1083   unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1084   unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1085   unsigned Mask = RegInfo.createVirtualRegister(RC);
1086   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1087   unsigned NewVal = RegInfo.createVirtualRegister(RC);
1088   unsigned OldVal = RegInfo.createVirtualRegister(RC);
1089   unsigned Incr2 = RegInfo.createVirtualRegister(RC);
1090   unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1091   unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1092   unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1093   unsigned AndRes = RegInfo.createVirtualRegister(RC);
1094   unsigned BinOpRes = RegInfo.createVirtualRegister(RC);
1095   unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1096   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1097   unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1098   unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1099   unsigned Success = RegInfo.createVirtualRegister(RC);
1100
1101   // insert new blocks after the current block
1102   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1103   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1105   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1106   MachineFunction::iterator It = BB;
1107   ++It;
1108   MF->insert(It, loopMBB);
1109   MF->insert(It, sinkMBB);
1110   MF->insert(It, exitMBB);
1111
1112   // Transfer the remainder of BB and its successor edges to exitMBB.
1113   exitMBB->splice(exitMBB->begin(), BB,
1114                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
1115   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1116
1117   BB->addSuccessor(loopMBB);
1118   loopMBB->addSuccessor(loopMBB);
1119   loopMBB->addSuccessor(sinkMBB);
1120   sinkMBB->addSuccessor(exitMBB);
1121
1122   //  thisMBB:
1123   //    addiu   masklsb2,$0,-4                # 0xfffffffc
1124   //    and     alignedaddr,ptr,masklsb2
1125   //    andi    ptrlsb2,ptr,3
1126   //    sll     shiftamt,ptrlsb2,3
1127   //    ori     maskupper,$0,255               # 0xff
1128   //    sll     mask,maskupper,shiftamt
1129   //    nor     mask2,$0,mask
1130   //    sll     incr2,incr,shiftamt
1131
1132   int64_t MaskImm = (Size == 1) ? 255 : 65535;
1133   BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1134     .addReg(Mips::ZERO).addImm(-4);
1135   BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1136     .addReg(Ptr).addReg(MaskLSB2);
1137   BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1138   if (Subtarget->isLittle()) {
1139     BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1140   } else {
1141     unsigned Off = RegInfo.createVirtualRegister(RC);
1142     BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1143       .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1144     BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1145   }
1146   BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1147     .addReg(Mips::ZERO).addImm(MaskImm);
1148   BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1149     .addReg(MaskUpper).addReg(ShiftAmt);
1150   BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1151   BuildMI(BB, DL, TII->get(Mips::SLLV), Incr2).addReg(Incr).addReg(ShiftAmt);
1152
1153   // atomic.load.binop
1154   // loopMBB:
1155   //   ll      oldval,0(alignedaddr)
1156   //   binop   binopres,oldval,incr2
1157   //   and     newval,binopres,mask
1158   //   and     maskedoldval0,oldval,mask2
1159   //   or      storeval,maskedoldval0,newval
1160   //   sc      success,storeval,0(alignedaddr)
1161   //   beq     success,$0,loopMBB
1162
1163   // atomic.swap
1164   // loopMBB:
1165   //   ll      oldval,0(alignedaddr)
1166   //   and     newval,incr2,mask
1167   //   and     maskedoldval0,oldval,mask2
1168   //   or      storeval,maskedoldval0,newval
1169   //   sc      success,storeval,0(alignedaddr)
1170   //   beq     success,$0,loopMBB
1171
1172   BB = loopMBB;
1173   BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1174   if (Nand) {
1175     //  and andres, oldval, incr2
1176     //  nor binopres, $0, andres
1177     //  and newval, binopres, mask
1178     BuildMI(BB, DL, TII->get(Mips::AND), AndRes).addReg(OldVal).addReg(Incr2);
1179     BuildMI(BB, DL, TII->get(Mips::NOR), BinOpRes)
1180       .addReg(Mips::ZERO).addReg(AndRes);
1181     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1182   } else if (BinOpcode) {
1183     //  <binop> binopres, oldval, incr2
1184     //  and newval, binopres, mask
1185     BuildMI(BB, DL, TII->get(BinOpcode), BinOpRes).addReg(OldVal).addReg(Incr2);
1186     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(BinOpRes).addReg(Mask);
1187   } else { // atomic.swap
1188     //  and newval, incr2, mask
1189     BuildMI(BB, DL, TII->get(Mips::AND), NewVal).addReg(Incr2).addReg(Mask);
1190   }
1191
1192   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1193     .addReg(OldVal).addReg(Mask2);
1194   BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1195     .addReg(MaskedOldVal0).addReg(NewVal);
1196   BuildMI(BB, DL, TII->get(Mips::SC), Success)
1197     .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1198   BuildMI(BB, DL, TII->get(Mips::BEQ))
1199     .addReg(Success).addReg(Mips::ZERO).addMBB(loopMBB);
1200
1201   //  sinkMBB:
1202   //    and     maskedoldval1,oldval,mask
1203   //    srl     srlres,maskedoldval1,shiftamt
1204   //    sign_extend dest,srlres
1205   BB = sinkMBB;
1206
1207   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1208     .addReg(OldVal).addReg(Mask);
1209   BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1210       .addReg(MaskedOldVal1).addReg(ShiftAmt);
1211   BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1212
1213   MI->eraseFromParent(); // The instruction is gone now.
1214
1215   return exitMBB;
1216 }
1217
1218 MachineBasicBlock * MipsTargetLowering::emitAtomicCmpSwap(MachineInstr *MI,
1219                                                           MachineBasicBlock *BB,
1220                                                           unsigned Size) const {
1221   assert((Size == 4 || Size == 8) && "Unsupported size for EmitAtomicCmpSwap.");
1222
1223   MachineFunction *MF = BB->getParent();
1224   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1225   const TargetRegisterClass *RC = getRegClassFor(MVT::getIntegerVT(Size * 8));
1226   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1227   DebugLoc DL = MI->getDebugLoc();
1228   unsigned LL, SC, ZERO, BNE, BEQ;
1229
1230   if (Size == 4) {
1231     LL = isMicroMips ? Mips::LL_MM : Mips::LL;
1232     SC = isMicroMips ? Mips::SC_MM : Mips::SC;
1233     ZERO = Mips::ZERO;
1234     BNE = Mips::BNE;
1235     BEQ = Mips::BEQ;
1236   } else {
1237     LL = Mips::LLD;
1238     SC = Mips::SCD;
1239     ZERO = Mips::ZERO_64;
1240     BNE = Mips::BNE64;
1241     BEQ = Mips::BEQ64;
1242   }
1243
1244   unsigned Dest    = MI->getOperand(0).getReg();
1245   unsigned Ptr     = MI->getOperand(1).getReg();
1246   unsigned OldVal  = MI->getOperand(2).getReg();
1247   unsigned NewVal  = MI->getOperand(3).getReg();
1248
1249   unsigned Success = RegInfo.createVirtualRegister(RC);
1250
1251   // insert new blocks after the current block
1252   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1253   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1254   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1255   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1256   MachineFunction::iterator It = BB;
1257   ++It;
1258   MF->insert(It, loop1MBB);
1259   MF->insert(It, loop2MBB);
1260   MF->insert(It, exitMBB);
1261
1262   // Transfer the remainder of BB and its successor edges to exitMBB.
1263   exitMBB->splice(exitMBB->begin(), BB,
1264                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
1265   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1266
1267   //  thisMBB:
1268   //    ...
1269   //    fallthrough --> loop1MBB
1270   BB->addSuccessor(loop1MBB);
1271   loop1MBB->addSuccessor(exitMBB);
1272   loop1MBB->addSuccessor(loop2MBB);
1273   loop2MBB->addSuccessor(loop1MBB);
1274   loop2MBB->addSuccessor(exitMBB);
1275
1276   // loop1MBB:
1277   //   ll dest, 0(ptr)
1278   //   bne dest, oldval, exitMBB
1279   BB = loop1MBB;
1280   BuildMI(BB, DL, TII->get(LL), Dest).addReg(Ptr).addImm(0);
1281   BuildMI(BB, DL, TII->get(BNE))
1282     .addReg(Dest).addReg(OldVal).addMBB(exitMBB);
1283
1284   // loop2MBB:
1285   //   sc success, newval, 0(ptr)
1286   //   beq success, $0, loop1MBB
1287   BB = loop2MBB;
1288   BuildMI(BB, DL, TII->get(SC), Success)
1289     .addReg(NewVal).addReg(Ptr).addImm(0);
1290   BuildMI(BB, DL, TII->get(BEQ))
1291     .addReg(Success).addReg(ZERO).addMBB(loop1MBB);
1292
1293   MI->eraseFromParent(); // The instruction is gone now.
1294
1295   return exitMBB;
1296 }
1297
1298 MachineBasicBlock *
1299 MipsTargetLowering::emitAtomicCmpSwapPartword(MachineInstr *MI,
1300                                               MachineBasicBlock *BB,
1301                                               unsigned Size) const {
1302   assert((Size == 1 || Size == 2) &&
1303       "Unsupported size for EmitAtomicCmpSwapPartial.");
1304
1305   MachineFunction *MF = BB->getParent();
1306   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1307   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1308   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1309   DebugLoc DL = MI->getDebugLoc();
1310
1311   unsigned Dest    = MI->getOperand(0).getReg();
1312   unsigned Ptr     = MI->getOperand(1).getReg();
1313   unsigned CmpVal  = MI->getOperand(2).getReg();
1314   unsigned NewVal  = MI->getOperand(3).getReg();
1315
1316   unsigned AlignedAddr = RegInfo.createVirtualRegister(RC);
1317   unsigned ShiftAmt = RegInfo.createVirtualRegister(RC);
1318   unsigned Mask = RegInfo.createVirtualRegister(RC);
1319   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1320   unsigned ShiftedCmpVal = RegInfo.createVirtualRegister(RC);
1321   unsigned OldVal = RegInfo.createVirtualRegister(RC);
1322   unsigned MaskedOldVal0 = RegInfo.createVirtualRegister(RC);
1323   unsigned ShiftedNewVal = RegInfo.createVirtualRegister(RC);
1324   unsigned MaskLSB2 = RegInfo.createVirtualRegister(RC);
1325   unsigned PtrLSB2 = RegInfo.createVirtualRegister(RC);
1326   unsigned MaskUpper = RegInfo.createVirtualRegister(RC);
1327   unsigned MaskedCmpVal = RegInfo.createVirtualRegister(RC);
1328   unsigned MaskedNewVal = RegInfo.createVirtualRegister(RC);
1329   unsigned MaskedOldVal1 = RegInfo.createVirtualRegister(RC);
1330   unsigned StoreVal = RegInfo.createVirtualRegister(RC);
1331   unsigned SrlRes = RegInfo.createVirtualRegister(RC);
1332   unsigned Success = RegInfo.createVirtualRegister(RC);
1333
1334   // insert new blocks after the current block
1335   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1336   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1337   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1338   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1339   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1340   MachineFunction::iterator It = BB;
1341   ++It;
1342   MF->insert(It, loop1MBB);
1343   MF->insert(It, loop2MBB);
1344   MF->insert(It, sinkMBB);
1345   MF->insert(It, exitMBB);
1346
1347   // Transfer the remainder of BB and its successor edges to exitMBB.
1348   exitMBB->splice(exitMBB->begin(), BB,
1349                   std::next(MachineBasicBlock::iterator(MI)), BB->end());
1350   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1351
1352   BB->addSuccessor(loop1MBB);
1353   loop1MBB->addSuccessor(sinkMBB);
1354   loop1MBB->addSuccessor(loop2MBB);
1355   loop2MBB->addSuccessor(loop1MBB);
1356   loop2MBB->addSuccessor(sinkMBB);
1357   sinkMBB->addSuccessor(exitMBB);
1358
1359   // FIXME: computation of newval2 can be moved to loop2MBB.
1360   //  thisMBB:
1361   //    addiu   masklsb2,$0,-4                # 0xfffffffc
1362   //    and     alignedaddr,ptr,masklsb2
1363   //    andi    ptrlsb2,ptr,3
1364   //    sll     shiftamt,ptrlsb2,3
1365   //    ori     maskupper,$0,255               # 0xff
1366   //    sll     mask,maskupper,shiftamt
1367   //    nor     mask2,$0,mask
1368   //    andi    maskedcmpval,cmpval,255
1369   //    sll     shiftedcmpval,maskedcmpval,shiftamt
1370   //    andi    maskednewval,newval,255
1371   //    sll     shiftednewval,maskednewval,shiftamt
1372   int64_t MaskImm = (Size == 1) ? 255 : 65535;
1373   BuildMI(BB, DL, TII->get(Mips::ADDiu), MaskLSB2)
1374     .addReg(Mips::ZERO).addImm(-4);
1375   BuildMI(BB, DL, TII->get(Mips::AND), AlignedAddr)
1376     .addReg(Ptr).addReg(MaskLSB2);
1377   BuildMI(BB, DL, TII->get(Mips::ANDi), PtrLSB2).addReg(Ptr).addImm(3);
1378   if (Subtarget->isLittle()) {
1379     BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(PtrLSB2).addImm(3);
1380   } else {
1381     unsigned Off = RegInfo.createVirtualRegister(RC);
1382     BuildMI(BB, DL, TII->get(Mips::XORi), Off)
1383       .addReg(PtrLSB2).addImm((Size == 1) ? 3 : 2);
1384     BuildMI(BB, DL, TII->get(Mips::SLL), ShiftAmt).addReg(Off).addImm(3);
1385   }
1386   BuildMI(BB, DL, TII->get(Mips::ORi), MaskUpper)
1387     .addReg(Mips::ZERO).addImm(MaskImm);
1388   BuildMI(BB, DL, TII->get(Mips::SLLV), Mask)
1389     .addReg(MaskUpper).addReg(ShiftAmt);
1390   BuildMI(BB, DL, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1391   BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedCmpVal)
1392     .addReg(CmpVal).addImm(MaskImm);
1393   BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedCmpVal)
1394     .addReg(MaskedCmpVal).addReg(ShiftAmt);
1395   BuildMI(BB, DL, TII->get(Mips::ANDi), MaskedNewVal)
1396     .addReg(NewVal).addImm(MaskImm);
1397   BuildMI(BB, DL, TII->get(Mips::SLLV), ShiftedNewVal)
1398     .addReg(MaskedNewVal).addReg(ShiftAmt);
1399
1400   //  loop1MBB:
1401   //    ll      oldval,0(alginedaddr)
1402   //    and     maskedoldval0,oldval,mask
1403   //    bne     maskedoldval0,shiftedcmpval,sinkMBB
1404   BB = loop1MBB;
1405   BuildMI(BB, DL, TII->get(Mips::LL), OldVal).addReg(AlignedAddr).addImm(0);
1406   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal0)
1407     .addReg(OldVal).addReg(Mask);
1408   BuildMI(BB, DL, TII->get(Mips::BNE))
1409     .addReg(MaskedOldVal0).addReg(ShiftedCmpVal).addMBB(sinkMBB);
1410
1411   //  loop2MBB:
1412   //    and     maskedoldval1,oldval,mask2
1413   //    or      storeval,maskedoldval1,shiftednewval
1414   //    sc      success,storeval,0(alignedaddr)
1415   //    beq     success,$0,loop1MBB
1416   BB = loop2MBB;
1417   BuildMI(BB, DL, TII->get(Mips::AND), MaskedOldVal1)
1418     .addReg(OldVal).addReg(Mask2);
1419   BuildMI(BB, DL, TII->get(Mips::OR), StoreVal)
1420     .addReg(MaskedOldVal1).addReg(ShiftedNewVal);
1421   BuildMI(BB, DL, TII->get(Mips::SC), Success)
1422       .addReg(StoreVal).addReg(AlignedAddr).addImm(0);
1423   BuildMI(BB, DL, TII->get(Mips::BEQ))
1424       .addReg(Success).addReg(Mips::ZERO).addMBB(loop1MBB);
1425
1426   //  sinkMBB:
1427   //    srl     srlres,maskedoldval0,shiftamt
1428   //    sign_extend dest,srlres
1429   BB = sinkMBB;
1430
1431   BuildMI(BB, DL, TII->get(Mips::SRLV), SrlRes)
1432       .addReg(MaskedOldVal0).addReg(ShiftAmt);
1433   BB = emitSignExtendToI32InReg(MI, BB, Size, Dest, SrlRes);
1434
1435   MI->eraseFromParent();   // The instruction is gone now.
1436
1437   return exitMBB;
1438 }
1439
1440 MachineBasicBlock *MipsTargetLowering::emitSEL_D(MachineInstr *MI,
1441                                                  MachineBasicBlock *BB) const {
1442   MachineFunction *MF = BB->getParent();
1443   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
1444   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1445   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1446   DebugLoc DL = MI->getDebugLoc();
1447   MachineBasicBlock::iterator II(MI);
1448
1449   unsigned Fc = MI->getOperand(1).getReg();
1450   const auto &FGR64RegClass = TRI->getRegClass(Mips::FGR64RegClassID);
1451
1452   unsigned Fc2 = RegInfo.createVirtualRegister(FGR64RegClass);
1453
1454   BuildMI(*BB, II, DL, TII->get(Mips::SUBREG_TO_REG), Fc2)
1455       .addImm(0)
1456       .addReg(Fc)
1457       .addImm(Mips::sub_lo);
1458
1459   // We don't erase the original instruction, we just replace the condition
1460   // register with the 64-bit super-register.
1461   MI->getOperand(1).setReg(Fc2);
1462
1463   return BB;
1464 }
1465
1466 //===----------------------------------------------------------------------===//
1467 //  Misc Lower Operation implementation
1468 //===----------------------------------------------------------------------===//
1469 SDValue MipsTargetLowering::lowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
1470   SDValue Chain = Op.getOperand(0);
1471   SDValue Table = Op.getOperand(1);
1472   SDValue Index = Op.getOperand(2);
1473   SDLoc DL(Op);
1474   EVT PTy = getPointerTy();
1475   unsigned EntrySize =
1476     DAG.getMachineFunction().getJumpTableInfo()->getEntrySize(*getDataLayout());
1477
1478   Index = DAG.getNode(ISD::MUL, DL, PTy, Index,
1479                       DAG.getConstant(EntrySize, PTy));
1480   SDValue Addr = DAG.getNode(ISD::ADD, DL, PTy, Index, Table);
1481
1482   EVT MemVT = EVT::getIntegerVT(*DAG.getContext(), EntrySize * 8);
1483   Addr = DAG.getExtLoad(ISD::SEXTLOAD, DL, PTy, Chain, Addr,
1484                         MachinePointerInfo::getJumpTable(), MemVT, false, false,
1485                         0);
1486   Chain = Addr.getValue(1);
1487
1488   if ((getTargetMachine().getRelocationModel() == Reloc::PIC_) ||
1489       Subtarget->isABI_N64()) {
1490     // For PIC, the sequence is:
1491     // BRIND(load(Jumptable + index) + RelocBase)
1492     // RelocBase can be JumpTable, GOT or some sort of global base.
1493     Addr = DAG.getNode(ISD::ADD, DL, PTy, Addr,
1494                        getPICJumpTableRelocBase(Table, DAG));
1495   }
1496
1497   return DAG.getNode(ISD::BRIND, DL, MVT::Other, Chain, Addr);
1498 }
1499
1500 SDValue MipsTargetLowering::lowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
1501   // The first operand is the chain, the second is the condition, the third is
1502   // the block to branch to if the condition is true.
1503   SDValue Chain = Op.getOperand(0);
1504   SDValue Dest = Op.getOperand(2);
1505   SDLoc DL(Op);
1506
1507   assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6());
1508   SDValue CondRes = createFPCmp(DAG, Op.getOperand(1));
1509
1510   // Return if flag is not set by a floating point comparison.
1511   if (CondRes.getOpcode() != MipsISD::FPCmp)
1512     return Op;
1513
1514   SDValue CCNode  = CondRes.getOperand(2);
1515   Mips::CondCode CC =
1516     (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1517   unsigned Opc = invertFPCondCodeUser(CC) ? Mips::BRANCH_F : Mips::BRANCH_T;
1518   SDValue BrCode = DAG.getConstant(Opc, MVT::i32);
1519   SDValue FCC0 = DAG.getRegister(Mips::FCC0, MVT::i32);
1520   return DAG.getNode(MipsISD::FPBrcond, DL, Op.getValueType(), Chain, BrCode,
1521                      FCC0, Dest, CondRes);
1522 }
1523
1524 SDValue MipsTargetLowering::
1525 lowerSELECT(SDValue Op, SelectionDAG &DAG) const
1526 {
1527   assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6());
1528   SDValue Cond = createFPCmp(DAG, Op.getOperand(0));
1529
1530   // Return if flag is not set by a floating point comparison.
1531   if (Cond.getOpcode() != MipsISD::FPCmp)
1532     return Op;
1533
1534   return createCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1535                       SDLoc(Op));
1536 }
1537
1538 SDValue MipsTargetLowering::
1539 lowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const
1540 {
1541   SDLoc DL(Op);
1542   EVT Ty = Op.getOperand(0).getValueType();
1543   SDValue Cond = DAG.getNode(ISD::SETCC, DL,
1544                              getSetCCResultType(*DAG.getContext(), Ty),
1545                              Op.getOperand(0), Op.getOperand(1),
1546                              Op.getOperand(4));
1547
1548   return DAG.getNode(ISD::SELECT, DL, Op.getValueType(), Cond, Op.getOperand(2),
1549                      Op.getOperand(3));
1550 }
1551
1552 SDValue MipsTargetLowering::lowerSETCC(SDValue Op, SelectionDAG &DAG) const {
1553   assert(!Subtarget->hasMips32r6() && !Subtarget->hasMips64r6());
1554   SDValue Cond = createFPCmp(DAG, Op);
1555
1556   assert(Cond.getOpcode() == MipsISD::FPCmp &&
1557          "Floating point operand expected.");
1558
1559   SDValue True  = DAG.getConstant(1, MVT::i32);
1560   SDValue False = DAG.getConstant(0, MVT::i32);
1561
1562   return createCMovFP(DAG, Cond, True, False, SDLoc(Op));
1563 }
1564
1565 SDValue MipsTargetLowering::lowerGlobalAddress(SDValue Op,
1566                                                SelectionDAG &DAG) const {
1567   // FIXME there isn't actually debug info here
1568   SDLoc DL(Op);
1569   EVT Ty = Op.getValueType();
1570   GlobalAddressSDNode *N = cast<GlobalAddressSDNode>(Op);
1571   const GlobalValue *GV = N->getGlobal();
1572
1573   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1574       !Subtarget->isABI_N64()) {
1575     const MipsTargetObjectFile &TLOF =
1576       (const MipsTargetObjectFile&)getObjFileLowering();
1577
1578     // %gp_rel relocation
1579     if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1580       SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, 0,
1581                                               MipsII::MO_GPREL);
1582       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, DL,
1583                                       DAG.getVTList(MVT::i32), GA);
1584       SDValue GPReg = DAG.getRegister(Mips::GP, MVT::i32);
1585       return DAG.getNode(ISD::ADD, DL, MVT::i32, GPReg, GPRelNode);
1586     }
1587
1588     // %hi/%lo relocation
1589     return getAddrNonPIC(N, Ty, DAG);
1590   }
1591
1592   if (GV->hasInternalLinkage() || (GV->hasLocalLinkage() && !isa<Function>(GV)))
1593     return getAddrLocal(N, Ty, DAG,
1594                         Subtarget->isABI_N32() || Subtarget->isABI_N64());
1595
1596   if (LargeGOT)
1597     return getAddrGlobalLargeGOT(N, Ty, DAG, MipsII::MO_GOT_HI16,
1598                                  MipsII::MO_GOT_LO16, DAG.getEntryNode(),
1599                                  MachinePointerInfo::getGOT());
1600
1601   return getAddrGlobal(N, Ty, DAG,
1602                        (Subtarget->isABI_N32() || Subtarget->isABI_N64())
1603                            ? MipsII::MO_GOT_DISP
1604                            : MipsII::MO_GOT16,
1605                        DAG.getEntryNode(), MachinePointerInfo::getGOT());
1606 }
1607
1608 SDValue MipsTargetLowering::lowerBlockAddress(SDValue Op,
1609                                               SelectionDAG &DAG) const {
1610   BlockAddressSDNode *N = cast<BlockAddressSDNode>(Op);
1611   EVT Ty = Op.getValueType();
1612
1613   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1614       !Subtarget->isABI_N64())
1615     return getAddrNonPIC(N, Ty, DAG);
1616
1617   return getAddrLocal(N, Ty, DAG,
1618                       Subtarget->isABI_N32() || Subtarget->isABI_N64());
1619 }
1620
1621 SDValue MipsTargetLowering::
1622 lowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1623 {
1624   // If the relocation model is PIC, use the General Dynamic TLS Model or
1625   // Local Dynamic TLS model, otherwise use the Initial Exec or
1626   // Local Exec TLS Model.
1627
1628   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1629   SDLoc DL(GA);
1630   const GlobalValue *GV = GA->getGlobal();
1631   EVT PtrVT = getPointerTy();
1632
1633   TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1634
1635   if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1636     // General Dynamic and Local Dynamic TLS Model.
1637     unsigned Flag = (model == TLSModel::LocalDynamic) ? MipsII::MO_TLSLDM
1638                                                       : MipsII::MO_TLSGD;
1639
1640     SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, Flag);
1641     SDValue Argument = DAG.getNode(MipsISD::Wrapper, DL, PtrVT,
1642                                    getGlobalReg(DAG, PtrVT), TGA);
1643     unsigned PtrSize = PtrVT.getSizeInBits();
1644     IntegerType *PtrTy = Type::getIntNTy(*DAG.getContext(), PtrSize);
1645
1646     SDValue TlsGetAddr = DAG.getExternalSymbol("__tls_get_addr", PtrVT);
1647
1648     ArgListTy Args;
1649     ArgListEntry Entry;
1650     Entry.Node = Argument;
1651     Entry.Ty = PtrTy;
1652     Args.push_back(Entry);
1653
1654     TargetLowering::CallLoweringInfo CLI(DAG);
1655     CLI.setDebugLoc(DL).setChain(DAG.getEntryNode())
1656       .setCallee(CallingConv::C, PtrTy, TlsGetAddr, std::move(Args), 0);
1657     std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
1658
1659     SDValue Ret = CallResult.first;
1660
1661     if (model != TLSModel::LocalDynamic)
1662       return Ret;
1663
1664     SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1665                                                MipsII::MO_DTPREL_HI);
1666     SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1667     SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1668                                                MipsII::MO_DTPREL_LO);
1669     SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1670     SDValue Add = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Ret);
1671     return DAG.getNode(ISD::ADD, DL, PtrVT, Add, Lo);
1672   }
1673
1674   SDValue Offset;
1675   if (model == TLSModel::InitialExec) {
1676     // Initial Exec TLS Model
1677     SDValue TGA = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1678                                              MipsII::MO_GOTTPREL);
1679     TGA = DAG.getNode(MipsISD::Wrapper, DL, PtrVT, getGlobalReg(DAG, PtrVT),
1680                       TGA);
1681     Offset = DAG.getLoad(PtrVT, DL,
1682                          DAG.getEntryNode(), TGA, MachinePointerInfo(),
1683                          false, false, false, 0);
1684   } else {
1685     // Local Exec TLS Model
1686     assert(model == TLSModel::LocalExec);
1687     SDValue TGAHi = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1688                                                MipsII::MO_TPREL_HI);
1689     SDValue TGALo = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
1690                                                MipsII::MO_TPREL_LO);
1691     SDValue Hi = DAG.getNode(MipsISD::Hi, DL, PtrVT, TGAHi);
1692     SDValue Lo = DAG.getNode(MipsISD::Lo, DL, PtrVT, TGALo);
1693     Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Hi, Lo);
1694   }
1695
1696   SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, DL, PtrVT);
1697   return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadPointer, Offset);
1698 }
1699
1700 SDValue MipsTargetLowering::
1701 lowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1702 {
1703   JumpTableSDNode *N = cast<JumpTableSDNode>(Op);
1704   EVT Ty = Op.getValueType();
1705
1706   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1707       !Subtarget->isABI_N64())
1708     return getAddrNonPIC(N, Ty, DAG);
1709
1710   return getAddrLocal(N, Ty, DAG,
1711                       Subtarget->isABI_N32() || Subtarget->isABI_N64());
1712 }
1713
1714 SDValue MipsTargetLowering::
1715 lowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1716 {
1717   // gp_rel relocation
1718   // FIXME: we should reference the constant pool using small data sections,
1719   // but the asm printer currently doesn't support this feature without
1720   // hacking it. This feature should come soon so we can uncomment the
1721   // stuff below.
1722   //if (IsInSmallSection(C->getType())) {
1723   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1724   //  SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1725   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
1726   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1727   EVT Ty = Op.getValueType();
1728
1729   if (getTargetMachine().getRelocationModel() != Reloc::PIC_ &&
1730       !Subtarget->isABI_N64())
1731     return getAddrNonPIC(N, Ty, DAG);
1732
1733   return getAddrLocal(N, Ty, DAG,
1734                       Subtarget->isABI_N32() || Subtarget->isABI_N64());
1735 }
1736
1737 SDValue MipsTargetLowering::lowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1738   MachineFunction &MF = DAG.getMachineFunction();
1739   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1740
1741   SDLoc DL(Op);
1742   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1743                                  getPointerTy());
1744
1745   // vastart just stores the address of the VarArgsFrameIndex slot into the
1746   // memory location argument.
1747   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1748   return DAG.getStore(Op.getOperand(0), DL, FI, Op.getOperand(1),
1749                       MachinePointerInfo(SV), false, false, 0);
1750 }
1751
1752 static SDValue lowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG,
1753                                 bool HasExtractInsert) {
1754   EVT TyX = Op.getOperand(0).getValueType();
1755   EVT TyY = Op.getOperand(1).getValueType();
1756   SDValue Const1 = DAG.getConstant(1, MVT::i32);
1757   SDValue Const31 = DAG.getConstant(31, MVT::i32);
1758   SDLoc DL(Op);
1759   SDValue Res;
1760
1761   // If operand is of type f64, extract the upper 32-bit. Otherwise, bitcast it
1762   // to i32.
1763   SDValue X = (TyX == MVT::f32) ?
1764     DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(0)) :
1765     DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(0),
1766                 Const1);
1767   SDValue Y = (TyY == MVT::f32) ?
1768     DAG.getNode(ISD::BITCAST, DL, MVT::i32, Op.getOperand(1)) :
1769     DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32, Op.getOperand(1),
1770                 Const1);
1771
1772   if (HasExtractInsert) {
1773     // ext  E, Y, 31, 1  ; extract bit31 of Y
1774     // ins  X, E, 31, 1  ; insert extracted bit at bit31 of X
1775     SDValue E = DAG.getNode(MipsISD::Ext, DL, MVT::i32, Y, Const31, Const1);
1776     Res = DAG.getNode(MipsISD::Ins, DL, MVT::i32, E, Const31, Const1, X);
1777   } else {
1778     // sll SllX, X, 1
1779     // srl SrlX, SllX, 1
1780     // srl SrlY, Y, 31
1781     // sll SllY, SrlX, 31
1782     // or  Or, SrlX, SllY
1783     SDValue SllX = DAG.getNode(ISD::SHL, DL, MVT::i32, X, Const1);
1784     SDValue SrlX = DAG.getNode(ISD::SRL, DL, MVT::i32, SllX, Const1);
1785     SDValue SrlY = DAG.getNode(ISD::SRL, DL, MVT::i32, Y, Const31);
1786     SDValue SllY = DAG.getNode(ISD::SHL, DL, MVT::i32, SrlY, Const31);
1787     Res = DAG.getNode(ISD::OR, DL, MVT::i32, SrlX, SllY);
1788   }
1789
1790   if (TyX == MVT::f32)
1791     return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Res);
1792
1793   SDValue LowX = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
1794                              Op.getOperand(0), DAG.getConstant(0, MVT::i32));
1795   return DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64, LowX, Res);
1796 }
1797
1798 static SDValue lowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG,
1799                                 bool HasExtractInsert) {
1800   unsigned WidthX = Op.getOperand(0).getValueSizeInBits();
1801   unsigned WidthY = Op.getOperand(1).getValueSizeInBits();
1802   EVT TyX = MVT::getIntegerVT(WidthX), TyY = MVT::getIntegerVT(WidthY);
1803   SDValue Const1 = DAG.getConstant(1, MVT::i32);
1804   SDLoc DL(Op);
1805
1806   // Bitcast to integer nodes.
1807   SDValue X = DAG.getNode(ISD::BITCAST, DL, TyX, Op.getOperand(0));
1808   SDValue Y = DAG.getNode(ISD::BITCAST, DL, TyY, Op.getOperand(1));
1809
1810   if (HasExtractInsert) {
1811     // ext  E, Y, width(Y) - 1, 1  ; extract bit width(Y)-1 of Y
1812     // ins  X, E, width(X) - 1, 1  ; insert extracted bit at bit width(X)-1 of X
1813     SDValue E = DAG.getNode(MipsISD::Ext, DL, TyY, Y,
1814                             DAG.getConstant(WidthY - 1, MVT::i32), Const1);
1815
1816     if (WidthX > WidthY)
1817       E = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, E);
1818     else if (WidthY > WidthX)
1819       E = DAG.getNode(ISD::TRUNCATE, DL, TyX, E);
1820
1821     SDValue I = DAG.getNode(MipsISD::Ins, DL, TyX, E,
1822                             DAG.getConstant(WidthX - 1, MVT::i32), Const1, X);
1823     return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), I);
1824   }
1825
1826   // (d)sll SllX, X, 1
1827   // (d)srl SrlX, SllX, 1
1828   // (d)srl SrlY, Y, width(Y)-1
1829   // (d)sll SllY, SrlX, width(Y)-1
1830   // or     Or, SrlX, SllY
1831   SDValue SllX = DAG.getNode(ISD::SHL, DL, TyX, X, Const1);
1832   SDValue SrlX = DAG.getNode(ISD::SRL, DL, TyX, SllX, Const1);
1833   SDValue SrlY = DAG.getNode(ISD::SRL, DL, TyY, Y,
1834                              DAG.getConstant(WidthY - 1, MVT::i32));
1835
1836   if (WidthX > WidthY)
1837     SrlY = DAG.getNode(ISD::ZERO_EXTEND, DL, TyX, SrlY);
1838   else if (WidthY > WidthX)
1839     SrlY = DAG.getNode(ISD::TRUNCATE, DL, TyX, SrlY);
1840
1841   SDValue SllY = DAG.getNode(ISD::SHL, DL, TyX, SrlY,
1842                              DAG.getConstant(WidthX - 1, MVT::i32));
1843   SDValue Or = DAG.getNode(ISD::OR, DL, TyX, SrlX, SllY);
1844   return DAG.getNode(ISD::BITCAST, DL, Op.getOperand(0).getValueType(), Or);
1845 }
1846
1847 SDValue
1848 MipsTargetLowering::lowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) const {
1849   if (Subtarget->isGP64bit())
1850     return lowerFCOPYSIGN64(Op, DAG, Subtarget->hasExtractInsert());
1851
1852   return lowerFCOPYSIGN32(Op, DAG, Subtarget->hasExtractInsert());
1853 }
1854
1855 SDValue MipsTargetLowering::
1856 lowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1857   // check the depth
1858   assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1859          "Frame address can only be determined for current frame.");
1860
1861   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1862   MFI->setFrameAddressIsTaken(true);
1863   EVT VT = Op.getValueType();
1864   SDLoc DL(Op);
1865   SDValue FrameAddr =
1866       DAG.getCopyFromReg(DAG.getEntryNode(), DL,
1867                          Subtarget->isABI_N64() ? Mips::FP_64 : Mips::FP, VT);
1868   return FrameAddr;
1869 }
1870
1871 SDValue MipsTargetLowering::lowerRETURNADDR(SDValue Op,
1872                                             SelectionDAG &DAG) const {
1873   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
1874     return SDValue();
1875
1876   // check the depth
1877   assert((cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() == 0) &&
1878          "Return address can be determined only for current frame.");
1879
1880   MachineFunction &MF = DAG.getMachineFunction();
1881   MachineFrameInfo *MFI = MF.getFrameInfo();
1882   MVT VT = Op.getSimpleValueType();
1883   unsigned RA = Subtarget->isABI_N64() ? Mips::RA_64 : Mips::RA;
1884   MFI->setReturnAddressIsTaken(true);
1885
1886   // Return RA, which contains the return address. Mark it an implicit live-in.
1887   unsigned Reg = MF.addLiveIn(RA, getRegClassFor(VT));
1888   return DAG.getCopyFromReg(DAG.getEntryNode(), SDLoc(Op), Reg, VT);
1889 }
1890
1891 // An EH_RETURN is the result of lowering llvm.eh.return which in turn is
1892 // generated from __builtin_eh_return (offset, handler)
1893 // The effect of this is to adjust the stack pointer by "offset"
1894 // and then branch to "handler".
1895 SDValue MipsTargetLowering::lowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
1896                                                                      const {
1897   MachineFunction &MF = DAG.getMachineFunction();
1898   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1899
1900   MipsFI->setCallsEhReturn();
1901   SDValue Chain     = Op.getOperand(0);
1902   SDValue Offset    = Op.getOperand(1);
1903   SDValue Handler   = Op.getOperand(2);
1904   SDLoc DL(Op);
1905   EVT Ty = Subtarget->isABI_N64() ? MVT::i64 : MVT::i32;
1906
1907   // Store stack offset in V1, store jump target in V0. Glue CopyToReg and
1908   // EH_RETURN nodes, so that instructions are emitted back-to-back.
1909   unsigned OffsetReg = Subtarget->isABI_N64() ? Mips::V1_64 : Mips::V1;
1910   unsigned AddrReg = Subtarget->isABI_N64() ? Mips::V0_64 : Mips::V0;
1911   Chain = DAG.getCopyToReg(Chain, DL, OffsetReg, Offset, SDValue());
1912   Chain = DAG.getCopyToReg(Chain, DL, AddrReg, Handler, Chain.getValue(1));
1913   return DAG.getNode(MipsISD::EH_RETURN, DL, MVT::Other, Chain,
1914                      DAG.getRegister(OffsetReg, Ty),
1915                      DAG.getRegister(AddrReg, getPointerTy()),
1916                      Chain.getValue(1));
1917 }
1918
1919 SDValue MipsTargetLowering::lowerATOMIC_FENCE(SDValue Op,
1920                                               SelectionDAG &DAG) const {
1921   // FIXME: Need pseudo-fence for 'singlethread' fences
1922   // FIXME: Set SType for weaker fences where supported/appropriate.
1923   unsigned SType = 0;
1924   SDLoc DL(Op);
1925   return DAG.getNode(MipsISD::Sync, DL, MVT::Other, Op.getOperand(0),
1926                      DAG.getConstant(SType, MVT::i32));
1927 }
1928
1929 SDValue MipsTargetLowering::lowerShiftLeftParts(SDValue Op,
1930                                                 SelectionDAG &DAG) const {
1931   SDLoc DL(Op);
1932   SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1933   SDValue Shamt = Op.getOperand(2);
1934
1935   // if shamt < 32:
1936   //  lo = (shl lo, shamt)
1937   //  hi = (or (shl hi, shamt) (srl (srl lo, 1), ~shamt))
1938   // else:
1939   //  lo = 0
1940   //  hi = (shl lo, shamt[4:0])
1941   SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1942                             DAG.getConstant(-1, MVT::i32));
1943   SDValue ShiftRight1Lo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo,
1944                                       DAG.getConstant(1, MVT::i32));
1945   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, ShiftRight1Lo,
1946                                      Not);
1947   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, Shamt);
1948   SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1949   SDValue ShiftLeftLo = DAG.getNode(ISD::SHL, DL, MVT::i32, Lo, Shamt);
1950   SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1951                              DAG.getConstant(0x20, MVT::i32));
1952   Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1953                    DAG.getConstant(0, MVT::i32), ShiftLeftLo);
1954   Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftLeftLo, Or);
1955
1956   SDValue Ops[2] = {Lo, Hi};
1957   return DAG.getMergeValues(Ops, DL);
1958 }
1959
1960 SDValue MipsTargetLowering::lowerShiftRightParts(SDValue Op, SelectionDAG &DAG,
1961                                                  bool IsSRA) const {
1962   SDLoc DL(Op);
1963   SDValue Lo = Op.getOperand(0), Hi = Op.getOperand(1);
1964   SDValue Shamt = Op.getOperand(2);
1965
1966   // if shamt < 32:
1967   //  lo = (or (shl (shl hi, 1), ~shamt) (srl lo, shamt))
1968   //  if isSRA:
1969   //    hi = (sra hi, shamt)
1970   //  else:
1971   //    hi = (srl hi, shamt)
1972   // else:
1973   //  if isSRA:
1974   //   lo = (sra hi, shamt[4:0])
1975   //   hi = (sra hi, 31)
1976   //  else:
1977   //   lo = (srl hi, shamt[4:0])
1978   //   hi = 0
1979   SDValue Not = DAG.getNode(ISD::XOR, DL, MVT::i32, Shamt,
1980                             DAG.getConstant(-1, MVT::i32));
1981   SDValue ShiftLeft1Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi,
1982                                      DAG.getConstant(1, MVT::i32));
1983   SDValue ShiftLeftHi = DAG.getNode(ISD::SHL, DL, MVT::i32, ShiftLeft1Hi, Not);
1984   SDValue ShiftRightLo = DAG.getNode(ISD::SRL, DL, MVT::i32, Lo, Shamt);
1985   SDValue Or = DAG.getNode(ISD::OR, DL, MVT::i32, ShiftLeftHi, ShiftRightLo);
1986   SDValue ShiftRightHi = DAG.getNode(IsSRA ? ISD::SRA : ISD::SRL, DL, MVT::i32,
1987                                      Hi, Shamt);
1988   SDValue Cond = DAG.getNode(ISD::AND, DL, MVT::i32, Shamt,
1989                              DAG.getConstant(0x20, MVT::i32));
1990   SDValue Shift31 = DAG.getNode(ISD::SRA, DL, MVT::i32, Hi,
1991                                 DAG.getConstant(31, MVT::i32));
1992   Lo = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond, ShiftRightHi, Or);
1993   Hi = DAG.getNode(ISD::SELECT, DL, MVT::i32, Cond,
1994                    IsSRA ? Shift31 : DAG.getConstant(0, MVT::i32),
1995                    ShiftRightHi);
1996
1997   SDValue Ops[2] = {Lo, Hi};
1998   return DAG.getMergeValues(Ops, DL);
1999 }
2000
2001 static SDValue createLoadLR(unsigned Opc, SelectionDAG &DAG, LoadSDNode *LD,
2002                             SDValue Chain, SDValue Src, unsigned Offset) {
2003   SDValue Ptr = LD->getBasePtr();
2004   EVT VT = LD->getValueType(0), MemVT = LD->getMemoryVT();
2005   EVT BasePtrVT = Ptr.getValueType();
2006   SDLoc DL(LD);
2007   SDVTList VTList = DAG.getVTList(VT, MVT::Other);
2008
2009   if (Offset)
2010     Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2011                       DAG.getConstant(Offset, BasePtrVT));
2012
2013   SDValue Ops[] = { Chain, Ptr, Src };
2014   return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2015                                  LD->getMemOperand());
2016 }
2017
2018 // Expand an unaligned 32 or 64-bit integer load node.
2019 SDValue MipsTargetLowering::lowerLOAD(SDValue Op, SelectionDAG &DAG) const {
2020   LoadSDNode *LD = cast<LoadSDNode>(Op);
2021   EVT MemVT = LD->getMemoryVT();
2022
2023   if (Subtarget->systemSupportsUnalignedAccess())
2024     return Op;
2025
2026   // Return if load is aligned or if MemVT is neither i32 nor i64.
2027   if ((LD->getAlignment() >= MemVT.getSizeInBits() / 8) ||
2028       ((MemVT != MVT::i32) && (MemVT != MVT::i64)))
2029     return SDValue();
2030
2031   bool IsLittle = Subtarget->isLittle();
2032   EVT VT = Op.getValueType();
2033   ISD::LoadExtType ExtType = LD->getExtensionType();
2034   SDValue Chain = LD->getChain(), Undef = DAG.getUNDEF(VT);
2035
2036   assert((VT == MVT::i32) || (VT == MVT::i64));
2037
2038   // Expand
2039   //  (set dst, (i64 (load baseptr)))
2040   // to
2041   //  (set tmp, (ldl (add baseptr, 7), undef))
2042   //  (set dst, (ldr baseptr, tmp))
2043   if ((VT == MVT::i64) && (ExtType == ISD::NON_EXTLOAD)) {
2044     SDValue LDL = createLoadLR(MipsISD::LDL, DAG, LD, Chain, Undef,
2045                                IsLittle ? 7 : 0);
2046     return createLoadLR(MipsISD::LDR, DAG, LD, LDL.getValue(1), LDL,
2047                         IsLittle ? 0 : 7);
2048   }
2049
2050   SDValue LWL = createLoadLR(MipsISD::LWL, DAG, LD, Chain, Undef,
2051                              IsLittle ? 3 : 0);
2052   SDValue LWR = createLoadLR(MipsISD::LWR, DAG, LD, LWL.getValue(1), LWL,
2053                              IsLittle ? 0 : 3);
2054
2055   // Expand
2056   //  (set dst, (i32 (load baseptr))) or
2057   //  (set dst, (i64 (sextload baseptr))) or
2058   //  (set dst, (i64 (extload baseptr)))
2059   // to
2060   //  (set tmp, (lwl (add baseptr, 3), undef))
2061   //  (set dst, (lwr baseptr, tmp))
2062   if ((VT == MVT::i32) || (ExtType == ISD::SEXTLOAD) ||
2063       (ExtType == ISD::EXTLOAD))
2064     return LWR;
2065
2066   assert((VT == MVT::i64) && (ExtType == ISD::ZEXTLOAD));
2067
2068   // Expand
2069   //  (set dst, (i64 (zextload baseptr)))
2070   // to
2071   //  (set tmp0, (lwl (add baseptr, 3), undef))
2072   //  (set tmp1, (lwr baseptr, tmp0))
2073   //  (set tmp2, (shl tmp1, 32))
2074   //  (set dst, (srl tmp2, 32))
2075   SDLoc DL(LD);
2076   SDValue Const32 = DAG.getConstant(32, MVT::i32);
2077   SDValue SLL = DAG.getNode(ISD::SHL, DL, MVT::i64, LWR, Const32);
2078   SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i64, SLL, Const32);
2079   SDValue Ops[] = { SRL, LWR.getValue(1) };
2080   return DAG.getMergeValues(Ops, DL);
2081 }
2082
2083 static SDValue createStoreLR(unsigned Opc, SelectionDAG &DAG, StoreSDNode *SD,
2084                              SDValue Chain, unsigned Offset) {
2085   SDValue Ptr = SD->getBasePtr(), Value = SD->getValue();
2086   EVT MemVT = SD->getMemoryVT(), BasePtrVT = Ptr.getValueType();
2087   SDLoc DL(SD);
2088   SDVTList VTList = DAG.getVTList(MVT::Other);
2089
2090   if (Offset)
2091     Ptr = DAG.getNode(ISD::ADD, DL, BasePtrVT, Ptr,
2092                       DAG.getConstant(Offset, BasePtrVT));
2093
2094   SDValue Ops[] = { Chain, Value, Ptr };
2095   return DAG.getMemIntrinsicNode(Opc, DL, VTList, Ops, MemVT,
2096                                  SD->getMemOperand());
2097 }
2098
2099 // Expand an unaligned 32 or 64-bit integer store node.
2100 static SDValue lowerUnalignedIntStore(StoreSDNode *SD, SelectionDAG &DAG,
2101                                       bool IsLittle) {
2102   SDValue Value = SD->getValue(), Chain = SD->getChain();
2103   EVT VT = Value.getValueType();
2104
2105   // Expand
2106   //  (store val, baseptr) or
2107   //  (truncstore val, baseptr)
2108   // to
2109   //  (swl val, (add baseptr, 3))
2110   //  (swr val, baseptr)
2111   if ((VT == MVT::i32) || SD->isTruncatingStore()) {
2112     SDValue SWL = createStoreLR(MipsISD::SWL, DAG, SD, Chain,
2113                                 IsLittle ? 3 : 0);
2114     return createStoreLR(MipsISD::SWR, DAG, SD, SWL, IsLittle ? 0 : 3);
2115   }
2116
2117   assert(VT == MVT::i64);
2118
2119   // Expand
2120   //  (store val, baseptr)
2121   // to
2122   //  (sdl val, (add baseptr, 7))
2123   //  (sdr val, baseptr)
2124   SDValue SDL = createStoreLR(MipsISD::SDL, DAG, SD, Chain, IsLittle ? 7 : 0);
2125   return createStoreLR(MipsISD::SDR, DAG, SD, SDL, IsLittle ? 0 : 7);
2126 }
2127
2128 // Lower (store (fp_to_sint $fp) $ptr) to (store (TruncIntFP $fp), $ptr).
2129 static SDValue lowerFP_TO_SINT_STORE(StoreSDNode *SD, SelectionDAG &DAG) {
2130   SDValue Val = SD->getValue();
2131
2132   if (Val.getOpcode() != ISD::FP_TO_SINT)
2133     return SDValue();
2134
2135   EVT FPTy = EVT::getFloatingPointVT(Val.getValueSizeInBits());
2136   SDValue Tr = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Val), FPTy,
2137                            Val.getOperand(0));
2138
2139   return DAG.getStore(SD->getChain(), SDLoc(SD), Tr, SD->getBasePtr(),
2140                       SD->getPointerInfo(), SD->isVolatile(),
2141                       SD->isNonTemporal(), SD->getAlignment());
2142 }
2143
2144 SDValue MipsTargetLowering::lowerSTORE(SDValue Op, SelectionDAG &DAG) const {
2145   StoreSDNode *SD = cast<StoreSDNode>(Op);
2146   EVT MemVT = SD->getMemoryVT();
2147
2148   // Lower unaligned integer stores.
2149   if (!Subtarget->systemSupportsUnalignedAccess() &&
2150       (SD->getAlignment() < MemVT.getSizeInBits() / 8) &&
2151       ((MemVT == MVT::i32) || (MemVT == MVT::i64)))
2152     return lowerUnalignedIntStore(SD, DAG, Subtarget->isLittle());
2153
2154   return lowerFP_TO_SINT_STORE(SD, DAG);
2155 }
2156
2157 SDValue MipsTargetLowering::lowerADD(SDValue Op, SelectionDAG &DAG) const {
2158   if (Op->getOperand(0).getOpcode() != ISD::FRAMEADDR
2159       || cast<ConstantSDNode>
2160         (Op->getOperand(0).getOperand(0))->getZExtValue() != 0
2161       || Op->getOperand(1).getOpcode() != ISD::FRAME_TO_ARGS_OFFSET)
2162     return SDValue();
2163
2164   // The pattern
2165   //   (add (frameaddr 0), (frame_to_args_offset))
2166   // results from lowering llvm.eh.dwarf.cfa intrinsic. Transform it to
2167   //   (add FrameObject, 0)
2168   // where FrameObject is a fixed StackObject with offset 0 which points to
2169   // the old stack pointer.
2170   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2171   EVT ValTy = Op->getValueType(0);
2172   int FI = MFI->CreateFixedObject(Op.getValueSizeInBits() / 8, 0, false);
2173   SDValue InArgsAddr = DAG.getFrameIndex(FI, ValTy);
2174   return DAG.getNode(ISD::ADD, SDLoc(Op), ValTy, InArgsAddr,
2175                      DAG.getConstant(0, ValTy));
2176 }
2177
2178 SDValue MipsTargetLowering::lowerFP_TO_SINT(SDValue Op,
2179                                             SelectionDAG &DAG) const {
2180   EVT FPTy = EVT::getFloatingPointVT(Op.getValueSizeInBits());
2181   SDValue Trunc = DAG.getNode(MipsISD::TruncIntFP, SDLoc(Op), FPTy,
2182                               Op.getOperand(0));
2183   return DAG.getNode(ISD::BITCAST, SDLoc(Op), Op.getValueType(), Trunc);
2184 }
2185
2186 //===----------------------------------------------------------------------===//
2187 //                      Calling Convention Implementation
2188 //===----------------------------------------------------------------------===//
2189
2190 //===----------------------------------------------------------------------===//
2191 // TODO: Implement a generic logic using tblgen that can support this.
2192 // Mips O32 ABI rules:
2193 // ---
2194 // i32 - Passed in A0, A1, A2, A3 and stack
2195 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
2196 //       an argument. Otherwise, passed in A1, A2, A3 and stack.
2197 // f64 - Only passed in two aliased f32 registers if no int reg has been used
2198 //       yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
2199 //       not used, it must be shadowed. If only A3 is avaiable, shadow it and
2200 //       go to stack.
2201 //
2202 //  For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
2203 //===----------------------------------------------------------------------===//
2204
2205 static bool CC_MipsO32(unsigned ValNo, MVT ValVT, MVT LocVT,
2206                        CCValAssign::LocInfo LocInfo, ISD::ArgFlagsTy ArgFlags,
2207                        CCState &State, const MCPhysReg *F64Regs) {
2208
2209   static const unsigned IntRegsSize = 4, FloatRegsSize = 2;
2210
2211   static const MCPhysReg IntRegs[] = { Mips::A0, Mips::A1, Mips::A2, Mips::A3 };
2212   static const MCPhysReg F32Regs[] = { Mips::F12, Mips::F14 };
2213
2214   // Do not process byval args here.
2215   if (ArgFlags.isByVal())
2216     return true;
2217
2218   // Promote i8 and i16
2219   if (LocVT == MVT::i8 || LocVT == MVT::i16) {
2220     LocVT = MVT::i32;
2221     if (ArgFlags.isSExt())
2222       LocInfo = CCValAssign::SExt;
2223     else if (ArgFlags.isZExt())
2224       LocInfo = CCValAssign::ZExt;
2225     else
2226       LocInfo = CCValAssign::AExt;
2227   }
2228
2229   unsigned Reg;
2230
2231   // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
2232   // is true: function is vararg, argument is 3rd or higher, there is previous
2233   // argument which is not f32 or f64.
2234   bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
2235       || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
2236   unsigned OrigAlign = ArgFlags.getOrigAlign();
2237   bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
2238
2239   if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
2240     Reg = State.AllocateReg(IntRegs, IntRegsSize);
2241     // If this is the first part of an i64 arg,
2242     // the allocated register must be either A0 or A2.
2243     if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
2244       Reg = State.AllocateReg(IntRegs, IntRegsSize);
2245     LocVT = MVT::i32;
2246   } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
2247     // Allocate int register and shadow next int register. If first
2248     // available register is Mips::A1 or Mips::A3, shadow it too.
2249     Reg = State.AllocateReg(IntRegs, IntRegsSize);
2250     if (Reg == Mips::A1 || Reg == Mips::A3)
2251       Reg = State.AllocateReg(IntRegs, IntRegsSize);
2252     State.AllocateReg(IntRegs, IntRegsSize);
2253     LocVT = MVT::i32;
2254   } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
2255     // we are guaranteed to find an available float register
2256     if (ValVT == MVT::f32) {
2257       Reg = State.AllocateReg(F32Regs, FloatRegsSize);
2258       // Shadow int register
2259       State.AllocateReg(IntRegs, IntRegsSize);
2260     } else {
2261       Reg = State.AllocateReg(F64Regs, FloatRegsSize);
2262       // Shadow int registers
2263       unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
2264       if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
2265         State.AllocateReg(IntRegs, IntRegsSize);
2266       State.AllocateReg(IntRegs, IntRegsSize);
2267     }
2268   } else
2269     llvm_unreachable("Cannot handle this ValVT.");
2270
2271   if (!Reg) {
2272     unsigned Offset = State.AllocateStack(ValVT.getSizeInBits() >> 3,
2273                                           OrigAlign);
2274     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
2275   } else
2276     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
2277
2278   return false;
2279 }
2280
2281 static bool CC_MipsO32_FP32(unsigned ValNo, MVT ValVT,
2282                             MVT LocVT, CCValAssign::LocInfo LocInfo,
2283                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
2284   static const MCPhysReg F64Regs[] = { Mips::D6, Mips::D7 };
2285
2286   return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2287 }
2288
2289 static bool CC_MipsO32_FP64(unsigned ValNo, MVT ValVT,
2290                             MVT LocVT, CCValAssign::LocInfo LocInfo,
2291                             ISD::ArgFlagsTy ArgFlags, CCState &State) {
2292   static const MCPhysReg F64Regs[] = { Mips::D12_64, Mips::D14_64 };
2293
2294   return CC_MipsO32(ValNo, ValVT, LocVT, LocInfo, ArgFlags, State, F64Regs);
2295 }
2296
2297 #include "MipsGenCallingConv.inc"
2298
2299 //===----------------------------------------------------------------------===//
2300 //                  Call Calling Convention Implementation
2301 //===----------------------------------------------------------------------===//
2302
2303 // Return next O32 integer argument register.
2304 static unsigned getNextIntArgReg(unsigned Reg) {
2305   assert((Reg == Mips::A0) || (Reg == Mips::A2));
2306   return (Reg == Mips::A0) ? Mips::A1 : Mips::A3;
2307 }
2308
2309 SDValue
2310 MipsTargetLowering::passArgOnStack(SDValue StackPtr, unsigned Offset,
2311                                    SDValue Chain, SDValue Arg, SDLoc DL,
2312                                    bool IsTailCall, SelectionDAG &DAG) const {
2313   if (!IsTailCall) {
2314     SDValue PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
2315                                  DAG.getIntPtrConstant(Offset));
2316     return DAG.getStore(Chain, DL, Arg, PtrOff, MachinePointerInfo(), false,
2317                         false, 0);
2318   }
2319
2320   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2321   int FI = MFI->CreateFixedObject(Arg.getValueSizeInBits() / 8, Offset, false);
2322   SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2323   return DAG.getStore(Chain, DL, Arg, FIN, MachinePointerInfo(),
2324                       /*isVolatile=*/ true, false, 0);
2325 }
2326
2327 void MipsTargetLowering::
2328 getOpndList(SmallVectorImpl<SDValue> &Ops,
2329             std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
2330             bool IsPICCall, bool GlobalOrExternal, bool InternalLinkage,
2331             CallLoweringInfo &CLI, SDValue Callee, SDValue Chain) const {
2332   // Insert node "GP copy globalreg" before call to function.
2333   //
2334   // R_MIPS_CALL* operators (emitted when non-internal functions are called
2335   // in PIC mode) allow symbols to be resolved via lazy binding.
2336   // The lazy binding stub requires GP to point to the GOT.
2337   if (IsPICCall && !InternalLinkage) {
2338     unsigned GPReg = Subtarget->isABI_N64() ? Mips::GP_64 : Mips::GP;
2339     EVT Ty = Subtarget->isABI_N64() ? MVT::i64 : MVT::i32;
2340     RegsToPass.push_back(std::make_pair(GPReg, getGlobalReg(CLI.DAG, Ty)));
2341   }
2342
2343   // Build a sequence of copy-to-reg nodes chained together with token
2344   // chain and flag operands which copy the outgoing args into registers.
2345   // The InFlag in necessary since all emitted instructions must be
2346   // stuck together.
2347   SDValue InFlag;
2348
2349   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
2350     Chain = CLI.DAG.getCopyToReg(Chain, CLI.DL, RegsToPass[i].first,
2351                                  RegsToPass[i].second, InFlag);
2352     InFlag = Chain.getValue(1);
2353   }
2354
2355   // Add argument registers to the end of the list so that they are
2356   // known live into the call.
2357   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
2358     Ops.push_back(CLI.DAG.getRegister(RegsToPass[i].first,
2359                                       RegsToPass[i].second.getValueType()));
2360
2361   // Add a register mask operand representing the call-preserved registers.
2362   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2363   const uint32_t *Mask = TRI->getCallPreservedMask(CLI.CallConv);
2364   assert(Mask && "Missing call preserved mask for calling convention");
2365   if (Subtarget->inMips16HardFloat()) {
2366     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(CLI.Callee)) {
2367       llvm::StringRef Sym = G->getGlobal()->getName();
2368       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
2369       if (F && F->hasFnAttribute("__Mips16RetHelper")) {
2370         Mask = MipsRegisterInfo::getMips16RetHelperMask();
2371       }
2372     }
2373   }
2374   Ops.push_back(CLI.DAG.getRegisterMask(Mask));
2375
2376   if (InFlag.getNode())
2377     Ops.push_back(InFlag);
2378 }
2379
2380 /// LowerCall - functions arguments are copied from virtual regs to
2381 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
2382 SDValue
2383 MipsTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
2384                               SmallVectorImpl<SDValue> &InVals) const {
2385   SelectionDAG &DAG                     = CLI.DAG;
2386   SDLoc DL                              = CLI.DL;
2387   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
2388   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
2389   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
2390   SDValue Chain                         = CLI.Chain;
2391   SDValue Callee                        = CLI.Callee;
2392   bool &IsTailCall                      = CLI.IsTailCall;
2393   CallingConv::ID CallConv              = CLI.CallConv;
2394   bool IsVarArg                         = CLI.IsVarArg;
2395
2396   MachineFunction &MF = DAG.getMachineFunction();
2397   MachineFrameInfo *MFI = MF.getFrameInfo();
2398   const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
2399   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
2400   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
2401
2402   // Analyze operands of the call, assigning locations to each operand.
2403   SmallVector<CCValAssign, 16> ArgLocs;
2404   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2405                  getTargetMachine(), ArgLocs, *DAG.getContext());
2406   MipsCC::SpecialCallingConvType SpecialCallingConv =
2407     getSpecialCallingConv(Callee);
2408   MipsCC MipsCCInfo(CallConv, Subtarget->isABI_O32(), Subtarget->isFP64bit(),
2409                     CCInfo, SpecialCallingConv);
2410
2411   MipsCCInfo.analyzeCallOperands(Outs, IsVarArg,
2412                                  Subtarget->mipsSEUsesSoftFloat(),
2413                                  Callee.getNode(), CLI.getArgs());
2414
2415   // Get a count of how many bytes are to be pushed on the stack.
2416   unsigned NextStackOffset = CCInfo.getNextStackOffset();
2417
2418   // Check if it's really possible to do a tail call.
2419   if (IsTailCall)
2420     IsTailCall =
2421       isEligibleForTailCallOptimization(MipsCCInfo, NextStackOffset,
2422                                         *MF.getInfo<MipsFunctionInfo>());
2423
2424   if (!IsTailCall && CLI.CS && CLI.CS->isMustTailCall())
2425     report_fatal_error("failed to perform tail call elimination on a call "
2426                        "site marked musttail");
2427
2428   if (IsTailCall)
2429     ++NumTailCalls;
2430
2431   // Chain is the output chain of the last Load/Store or CopyToReg node.
2432   // ByValChain is the output chain of the last Memcpy node created for copying
2433   // byval arguments to the stack.
2434   unsigned StackAlignment = TFL->getStackAlignment();
2435   NextStackOffset = RoundUpToAlignment(NextStackOffset, StackAlignment);
2436   SDValue NextStackOffsetVal = DAG.getIntPtrConstant(NextStackOffset, true);
2437
2438   if (!IsTailCall)
2439     Chain = DAG.getCALLSEQ_START(Chain, NextStackOffsetVal, DL);
2440
2441   SDValue StackPtr = DAG.getCopyFromReg(
2442       Chain, DL, Subtarget->isABI_N64() ? Mips::SP_64 : Mips::SP,
2443       getPointerTy());
2444
2445   // With EABI is it possible to have 16 args on registers.
2446   std::deque< std::pair<unsigned, SDValue> > RegsToPass;
2447   SmallVector<SDValue, 8> MemOpChains;
2448   MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
2449
2450   // Walk the register/memloc assignments, inserting copies/loads.
2451   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2452     SDValue Arg = OutVals[i];
2453     CCValAssign &VA = ArgLocs[i];
2454     MVT ValVT = VA.getValVT(), LocVT = VA.getLocVT();
2455     ISD::ArgFlagsTy Flags = Outs[i].Flags;
2456
2457     // ByVal Arg.
2458     if (Flags.isByVal()) {
2459       assert(Flags.getByValSize() &&
2460              "ByVal args of size 0 should have been ignored by front-end.");
2461       assert(ByValArg != MipsCCInfo.byval_end());
2462       assert(!IsTailCall &&
2463              "Do not tail-call optimize if there is a byval argument.");
2464       passByValArg(Chain, DL, RegsToPass, MemOpChains, StackPtr, MFI, DAG, Arg,
2465                    MipsCCInfo, *ByValArg, Flags, Subtarget->isLittle());
2466       ++ByValArg;
2467       continue;
2468     }
2469
2470     // Promote the value if needed.
2471     switch (VA.getLocInfo()) {
2472     default: llvm_unreachable("Unknown loc info!");
2473     case CCValAssign::Full:
2474       if (VA.isRegLoc()) {
2475         if ((ValVT == MVT::f32 && LocVT == MVT::i32) ||
2476             (ValVT == MVT::f64 && LocVT == MVT::i64) ||
2477             (ValVT == MVT::i64 && LocVT == MVT::f64))
2478           Arg = DAG.getNode(ISD::BITCAST, DL, LocVT, Arg);
2479         else if (ValVT == MVT::f64 && LocVT == MVT::i32) {
2480           SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2481                                    Arg, DAG.getConstant(0, MVT::i32));
2482           SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, DL, MVT::i32,
2483                                    Arg, DAG.getConstant(1, MVT::i32));
2484           if (!Subtarget->isLittle())
2485             std::swap(Lo, Hi);
2486           unsigned LocRegLo = VA.getLocReg();
2487           unsigned LocRegHigh = getNextIntArgReg(LocRegLo);
2488           RegsToPass.push_back(std::make_pair(LocRegLo, Lo));
2489           RegsToPass.push_back(std::make_pair(LocRegHigh, Hi));
2490           continue;
2491         }
2492       }
2493       break;
2494     case CCValAssign::SExt:
2495       Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, LocVT, Arg);
2496       break;
2497     case CCValAssign::ZExt:
2498       Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, LocVT, Arg);
2499       break;
2500     case CCValAssign::AExt:
2501       Arg = DAG.getNode(ISD::ANY_EXTEND, DL, LocVT, Arg);
2502       break;
2503     }
2504
2505     // Arguments that can be passed on register must be kept at
2506     // RegsToPass vector
2507     if (VA.isRegLoc()) {
2508       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
2509       continue;
2510     }
2511
2512     // Register can't get to this point...
2513     assert(VA.isMemLoc());
2514
2515     // emit ISD::STORE whichs stores the
2516     // parameter value to a stack Location
2517     MemOpChains.push_back(passArgOnStack(StackPtr, VA.getLocMemOffset(),
2518                                          Chain, Arg, DL, IsTailCall, DAG));
2519   }
2520
2521   // Transform all store nodes into one single node because all store
2522   // nodes are independent of each other.
2523   if (!MemOpChains.empty())
2524     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
2525
2526   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
2527   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
2528   // node so that legalize doesn't hack it.
2529   bool IsPICCall =
2530       (Subtarget->isABI_N64() || IsPIC); // true if calls are translated to
2531                                          // jalr $25
2532   bool GlobalOrExternal = false, InternalLinkage = false;
2533   SDValue CalleeLo;
2534   EVT Ty = Callee.getValueType();
2535
2536   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
2537     if (IsPICCall) {
2538       const GlobalValue *Val = G->getGlobal();
2539       InternalLinkage = Val->hasInternalLinkage();
2540
2541       if (InternalLinkage)
2542         Callee = getAddrLocal(G, Ty, DAG,
2543                               Subtarget->isABI_N32() || Subtarget->isABI_N64());
2544       else if (LargeGOT)
2545         Callee = getAddrGlobalLargeGOT(G, Ty, DAG, MipsII::MO_CALL_HI16,
2546                                        MipsII::MO_CALL_LO16, Chain,
2547                                        FuncInfo->callPtrInfo(Val));
2548       else
2549         Callee = getAddrGlobal(G, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2550                                FuncInfo->callPtrInfo(Val));
2551     } else
2552       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
2553                                           MipsII::MO_NO_FLAG);
2554     GlobalOrExternal = true;
2555   }
2556   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
2557     const char *Sym = S->getSymbol();
2558
2559     if (!Subtarget->isABI_N64() && !IsPIC) // !N64 && static
2560       Callee = DAG.getTargetExternalSymbol(Sym, getPointerTy(),
2561                                             MipsII::MO_NO_FLAG);
2562     else if (LargeGOT)
2563       Callee = getAddrGlobalLargeGOT(S, Ty, DAG, MipsII::MO_CALL_HI16,
2564                                      MipsII::MO_CALL_LO16, Chain,
2565                                      FuncInfo->callPtrInfo(Sym));
2566     else // N64 || PIC
2567       Callee = getAddrGlobal(S, Ty, DAG, MipsII::MO_GOT_CALL, Chain,
2568                              FuncInfo->callPtrInfo(Sym));
2569
2570     GlobalOrExternal = true;
2571   }
2572
2573   SmallVector<SDValue, 8> Ops(1, Chain);
2574   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2575
2576   getOpndList(Ops, RegsToPass, IsPICCall, GlobalOrExternal, InternalLinkage,
2577               CLI, Callee, Chain);
2578
2579   if (IsTailCall)
2580     return DAG.getNode(MipsISD::TailCall, DL, MVT::Other, Ops);
2581
2582   Chain = DAG.getNode(MipsISD::JmpLink, DL, NodeTys, Ops);
2583   SDValue InFlag = Chain.getValue(1);
2584
2585   // Create the CALLSEQ_END node.
2586   Chain = DAG.getCALLSEQ_END(Chain, NextStackOffsetVal,
2587                              DAG.getIntPtrConstant(0, true), InFlag, DL);
2588   InFlag = Chain.getValue(1);
2589
2590   // Handle result values, copying them out of physregs into vregs that we
2591   // return.
2592   return LowerCallResult(Chain, InFlag, CallConv, IsVarArg,
2593                          Ins, DL, DAG, InVals, CLI.Callee.getNode(), CLI.RetTy);
2594 }
2595
2596 /// LowerCallResult - Lower the result values of a call into the
2597 /// appropriate copies out of appropriate physical registers.
2598 SDValue
2599 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
2600                                     CallingConv::ID CallConv, bool IsVarArg,
2601                                     const SmallVectorImpl<ISD::InputArg> &Ins,
2602                                     SDLoc DL, SelectionDAG &DAG,
2603                                     SmallVectorImpl<SDValue> &InVals,
2604                                     const SDNode *CallNode,
2605                                     const Type *RetTy) const {
2606   // Assign locations to each value returned by this call.
2607   SmallVector<CCValAssign, 16> RVLocs;
2608   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2609                  getTargetMachine(), RVLocs, *DAG.getContext());
2610   MipsCC MipsCCInfo(CallConv, Subtarget->isABI_O32(), Subtarget->isFP64bit(),
2611                     CCInfo);
2612
2613   MipsCCInfo.analyzeCallResult(Ins, Subtarget->mipsSEUsesSoftFloat(),
2614                                CallNode, RetTy);
2615
2616   // Copy all of the result registers out of their specified physreg.
2617   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2618     SDValue Val = DAG.getCopyFromReg(Chain, DL, RVLocs[i].getLocReg(),
2619                                      RVLocs[i].getLocVT(), InFlag);
2620     Chain = Val.getValue(1);
2621     InFlag = Val.getValue(2);
2622
2623     if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
2624       Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getValVT(), Val);
2625
2626     InVals.push_back(Val);
2627   }
2628
2629   return Chain;
2630 }
2631
2632 //===----------------------------------------------------------------------===//
2633 //             Formal Arguments Calling Convention Implementation
2634 //===----------------------------------------------------------------------===//
2635 /// LowerFormalArguments - transform physical registers into virtual registers
2636 /// and generate load operations for arguments places on the stack.
2637 SDValue
2638 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2639                                          CallingConv::ID CallConv,
2640                                          bool IsVarArg,
2641                                       const SmallVectorImpl<ISD::InputArg> &Ins,
2642                                          SDLoc DL, SelectionDAG &DAG,
2643                                          SmallVectorImpl<SDValue> &InVals)
2644                                           const {
2645   MachineFunction &MF = DAG.getMachineFunction();
2646   MachineFrameInfo *MFI = MF.getFrameInfo();
2647   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2648
2649   MipsFI->setVarArgsFrameIndex(0);
2650
2651   // Used with vargs to acumulate store chains.
2652   std::vector<SDValue> OutChains;
2653
2654   // Assign locations to all of the incoming arguments.
2655   SmallVector<CCValAssign, 16> ArgLocs;
2656   CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
2657                  getTargetMachine(), ArgLocs, *DAG.getContext());
2658   MipsCC MipsCCInfo(CallConv, Subtarget->isABI_O32(), Subtarget->isFP64bit(),
2659                     CCInfo);
2660   Function::const_arg_iterator FuncArg =
2661     DAG.getMachineFunction().getFunction()->arg_begin();
2662   bool UseSoftFloat = Subtarget->mipsSEUsesSoftFloat();
2663
2664   MipsCCInfo.analyzeFormalArguments(Ins, UseSoftFloat, FuncArg);
2665   MipsFI->setFormalArgInfo(CCInfo.getNextStackOffset(),
2666                            MipsCCInfo.hasByValArg());
2667
2668   unsigned CurArgIdx = 0;
2669   MipsCC::byval_iterator ByValArg = MipsCCInfo.byval_begin();
2670
2671   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2672     CCValAssign &VA = ArgLocs[i];
2673     std::advance(FuncArg, Ins[i].OrigArgIndex - CurArgIdx);
2674     CurArgIdx = Ins[i].OrigArgIndex;
2675     EVT ValVT = VA.getValVT();
2676     ISD::ArgFlagsTy Flags = Ins[i].Flags;
2677     bool IsRegLoc = VA.isRegLoc();
2678
2679     if (Flags.isByVal()) {
2680       assert(Flags.getByValSize() &&
2681              "ByVal args of size 0 should have been ignored by front-end.");
2682       assert(ByValArg != MipsCCInfo.byval_end());
2683       copyByValRegs(Chain, DL, OutChains, DAG, Flags, InVals, &*FuncArg,
2684                     MipsCCInfo, *ByValArg);
2685       ++ByValArg;
2686       continue;
2687     }
2688
2689     // Arguments stored on registers
2690     if (IsRegLoc) {
2691       MVT RegVT = VA.getLocVT();
2692       unsigned ArgReg = VA.getLocReg();
2693       const TargetRegisterClass *RC = getRegClassFor(RegVT);
2694
2695       // Transform the arguments stored on
2696       // physical registers into virtual ones
2697       unsigned Reg = addLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2698       SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegVT);
2699
2700       // If this is an 8 or 16-bit value, it has been passed promoted
2701       // to 32 bits.  Insert an assert[sz]ext to capture this, then
2702       // truncate to the right size.
2703       if (VA.getLocInfo() != CCValAssign::Full) {
2704         unsigned Opcode = 0;
2705         if (VA.getLocInfo() == CCValAssign::SExt)
2706           Opcode = ISD::AssertSext;
2707         else if (VA.getLocInfo() == CCValAssign::ZExt)
2708           Opcode = ISD::AssertZext;
2709         if (Opcode)
2710           ArgValue = DAG.getNode(Opcode, DL, RegVT, ArgValue,
2711                                  DAG.getValueType(ValVT));
2712         ArgValue = DAG.getNode(ISD::TRUNCATE, DL, ValVT, ArgValue);
2713       }
2714
2715       // Handle floating point arguments passed in integer registers and
2716       // long double arguments passed in floating point registers.
2717       if ((RegVT == MVT::i32 && ValVT == MVT::f32) ||
2718           (RegVT == MVT::i64 && ValVT == MVT::f64) ||
2719           (RegVT == MVT::f64 && ValVT == MVT::i64))
2720         ArgValue = DAG.getNode(ISD::BITCAST, DL, ValVT, ArgValue);
2721       else if (Subtarget->isABI_O32() && RegVT == MVT::i32 &&
2722                ValVT == MVT::f64) {
2723         unsigned Reg2 = addLiveIn(DAG.getMachineFunction(),
2724                                   getNextIntArgReg(ArgReg), RC);
2725         SDValue ArgValue2 = DAG.getCopyFromReg(Chain, DL, Reg2, RegVT);
2726         if (!Subtarget->isLittle())
2727           std::swap(ArgValue, ArgValue2);
2728         ArgValue = DAG.getNode(MipsISD::BuildPairF64, DL, MVT::f64,
2729                                ArgValue, ArgValue2);
2730       }
2731
2732       InVals.push_back(ArgValue);
2733     } else { // VA.isRegLoc()
2734
2735       // sanity check
2736       assert(VA.isMemLoc());
2737
2738       // The stack pointer offset is relative to the caller stack frame.
2739       int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
2740                                       VA.getLocMemOffset(), true);
2741
2742       // Create load nodes to retrieve arguments from the stack
2743       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
2744       SDValue Load = DAG.getLoad(ValVT, DL, Chain, FIN,
2745                                  MachinePointerInfo::getFixedStack(FI),
2746                                  false, false, false, 0);
2747       InVals.push_back(Load);
2748       OutChains.push_back(Load.getValue(1));
2749     }
2750   }
2751
2752   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2753     // The mips ABIs for returning structs by value requires that we copy
2754     // the sret argument into $v0 for the return. Save the argument into
2755     // a virtual register so that we can access it from the return points.
2756     if (Ins[i].Flags.isSRet()) {
2757       unsigned Reg = MipsFI->getSRetReturnReg();
2758       if (!Reg) {
2759         Reg = MF.getRegInfo().createVirtualRegister(
2760             getRegClassFor(Subtarget->isABI_N64() ? MVT::i64 : MVT::i32));
2761         MipsFI->setSRetReturnReg(Reg);
2762       }
2763       SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), DL, Reg, InVals[i]);
2764       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Copy, Chain);
2765       break;
2766     }
2767   }
2768
2769   if (IsVarArg)
2770     writeVarArgRegs(OutChains, MipsCCInfo, Chain, DL, DAG);
2771
2772   // All stores are grouped in one node to allow the matching between
2773   // the size of Ins and InVals. This only happens when on varg functions
2774   if (!OutChains.empty()) {
2775     OutChains.push_back(Chain);
2776     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, OutChains);
2777   }
2778
2779   return Chain;
2780 }
2781
2782 //===----------------------------------------------------------------------===//
2783 //               Return Value Calling Convention Implementation
2784 //===----------------------------------------------------------------------===//
2785
2786 bool
2787 MipsTargetLowering::CanLowerReturn(CallingConv::ID CallConv,
2788                                    MachineFunction &MF, bool IsVarArg,
2789                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
2790                                    LLVMContext &Context) const {
2791   SmallVector<CCValAssign, 16> RVLocs;
2792   CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(),
2793                  RVLocs, Context);
2794   return CCInfo.CheckReturn(Outs, RetCC_Mips);
2795 }
2796
2797 SDValue
2798 MipsTargetLowering::LowerReturn(SDValue Chain,
2799                                 CallingConv::ID CallConv, bool IsVarArg,
2800                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
2801                                 const SmallVectorImpl<SDValue> &OutVals,
2802                                 SDLoc DL, SelectionDAG &DAG) const {
2803   // CCValAssign - represent the assignment of
2804   // the return value to a location
2805   SmallVector<CCValAssign, 16> RVLocs;
2806   MachineFunction &MF = DAG.getMachineFunction();
2807
2808   // CCState - Info about the registers and stack slot.
2809   CCState CCInfo(CallConv, IsVarArg, MF, getTargetMachine(), RVLocs,
2810                  *DAG.getContext());
2811   MipsCC MipsCCInfo(CallConv, Subtarget->isABI_O32(), Subtarget->isFP64bit(),
2812                     CCInfo);
2813
2814   // Analyze return values.
2815   MipsCCInfo.analyzeReturn(Outs, Subtarget->mipsSEUsesSoftFloat(),
2816                            MF.getFunction()->getReturnType());
2817
2818   SDValue Flag;
2819   SmallVector<SDValue, 4> RetOps(1, Chain);
2820
2821   // Copy the result values into the output registers.
2822   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2823     SDValue Val = OutVals[i];
2824     CCValAssign &VA = RVLocs[i];
2825     assert(VA.isRegLoc() && "Can only return in registers!");
2826
2827     if (RVLocs[i].getValVT() != RVLocs[i].getLocVT())
2828       Val = DAG.getNode(ISD::BITCAST, DL, RVLocs[i].getLocVT(), Val);
2829
2830     Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), Val, Flag);
2831
2832     // Guarantee that all emitted copies are stuck together with flags.
2833     Flag = Chain.getValue(1);
2834     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2835   }
2836
2837   // The mips ABIs for returning structs by value requires that we copy
2838   // the sret argument into $v0 for the return. We saved the argument into
2839   // a virtual register in the entry block, so now we copy the value out
2840   // and into $v0.
2841   if (MF.getFunction()->hasStructRetAttr()) {
2842     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2843     unsigned Reg = MipsFI->getSRetReturnReg();
2844
2845     if (!Reg)
2846       llvm_unreachable("sret virtual register not created in the entry block");
2847     SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
2848     unsigned V0 = Subtarget->isABI_N64() ? Mips::V0_64 : Mips::V0;
2849
2850     Chain = DAG.getCopyToReg(Chain, DL, V0, Val, Flag);
2851     Flag = Chain.getValue(1);
2852     RetOps.push_back(DAG.getRegister(V0, getPointerTy()));
2853   }
2854
2855   RetOps[0] = Chain;  // Update chain.
2856
2857   // Add the flag if we have it.
2858   if (Flag.getNode())
2859     RetOps.push_back(Flag);
2860
2861   // Return on Mips is always a "jr $ra"
2862   return DAG.getNode(MipsISD::Ret, DL, MVT::Other, RetOps);
2863 }
2864
2865 //===----------------------------------------------------------------------===//
2866 //                           Mips Inline Assembly Support
2867 //===----------------------------------------------------------------------===//
2868
2869 /// getConstraintType - Given a constraint letter, return the type of
2870 /// constraint it is for this target.
2871 MipsTargetLowering::ConstraintType MipsTargetLowering::
2872 getConstraintType(const std::string &Constraint) const
2873 {
2874   // Mips specific constraints
2875   // GCC config/mips/constraints.md
2876   //
2877   // 'd' : An address register. Equivalent to r
2878   //       unless generating MIPS16 code.
2879   // 'y' : Equivalent to r; retained for
2880   //       backwards compatibility.
2881   // 'c' : A register suitable for use in an indirect
2882   //       jump. This will always be $25 for -mabicalls.
2883   // 'l' : The lo register. 1 word storage.
2884   // 'x' : The hilo register pair. Double word storage.
2885   if (Constraint.size() == 1) {
2886     switch (Constraint[0]) {
2887       default : break;
2888       case 'd':
2889       case 'y':
2890       case 'f':
2891       case 'c':
2892       case 'l':
2893       case 'x':
2894         return C_RegisterClass;
2895       case 'R':
2896         return C_Memory;
2897     }
2898   }
2899   return TargetLowering::getConstraintType(Constraint);
2900 }
2901
2902 /// Examine constraint type and operand type and determine a weight value.
2903 /// This object must already have been set up with the operand type
2904 /// and the current alternative constraint selected.
2905 TargetLowering::ConstraintWeight
2906 MipsTargetLowering::getSingleConstraintMatchWeight(
2907     AsmOperandInfo &info, const char *constraint) const {
2908   ConstraintWeight weight = CW_Invalid;
2909   Value *CallOperandVal = info.CallOperandVal;
2910     // If we don't have a value, we can't do a match,
2911     // but allow it at the lowest weight.
2912   if (!CallOperandVal)
2913     return CW_Default;
2914   Type *type = CallOperandVal->getType();
2915   // Look at the constraint type.
2916   switch (*constraint) {
2917   default:
2918     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2919     break;
2920   case 'd':
2921   case 'y':
2922     if (type->isIntegerTy())
2923       weight = CW_Register;
2924     break;
2925   case 'f': // FPU or MSA register
2926     if (Subtarget->hasMSA() && type->isVectorTy() &&
2927         cast<VectorType>(type)->getBitWidth() == 128)
2928       weight = CW_Register;
2929     else if (type->isFloatTy())
2930       weight = CW_Register;
2931     break;
2932   case 'c': // $25 for indirect jumps
2933   case 'l': // lo register
2934   case 'x': // hilo register pair
2935     if (type->isIntegerTy())
2936       weight = CW_SpecificReg;
2937     break;
2938   case 'I': // signed 16 bit immediate
2939   case 'J': // integer zero
2940   case 'K': // unsigned 16 bit immediate
2941   case 'L': // signed 32 bit immediate where lower 16 bits are 0
2942   case 'N': // immediate in the range of -65535 to -1 (inclusive)
2943   case 'O': // signed 15 bit immediate (+- 16383)
2944   case 'P': // immediate in the range of 65535 to 1 (inclusive)
2945     if (isa<ConstantInt>(CallOperandVal))
2946       weight = CW_Constant;
2947     break;
2948   case 'R':
2949     weight = CW_Memory;
2950     break;
2951   }
2952   return weight;
2953 }
2954
2955 /// This is a helper function to parse a physical register string and split it
2956 /// into non-numeric and numeric parts (Prefix and Reg). The first boolean flag
2957 /// that is returned indicates whether parsing was successful. The second flag
2958 /// is true if the numeric part exists.
2959 static std::pair<bool, bool>
2960 parsePhysicalReg(const StringRef &C, std::string &Prefix,
2961                  unsigned long long &Reg) {
2962   if (C.front() != '{' || C.back() != '}')
2963     return std::make_pair(false, false);
2964
2965   // Search for the first numeric character.
2966   StringRef::const_iterator I, B = C.begin() + 1, E = C.end() - 1;
2967   I = std::find_if(B, E, std::ptr_fun(isdigit));
2968
2969   Prefix.assign(B, I - B);
2970
2971   // The second flag is set to false if no numeric characters were found.
2972   if (I == E)
2973     return std::make_pair(true, false);
2974
2975   // Parse the numeric characters.
2976   return std::make_pair(!getAsUnsignedInteger(StringRef(I, E - I), 10, Reg),
2977                         true);
2978 }
2979
2980 std::pair<unsigned, const TargetRegisterClass *> MipsTargetLowering::
2981 parseRegForInlineAsmConstraint(const StringRef &C, MVT VT) const {
2982   const TargetRegisterInfo *TRI = getTargetMachine().getRegisterInfo();
2983   const TargetRegisterClass *RC;
2984   std::string Prefix;
2985   unsigned long long Reg;
2986
2987   std::pair<bool, bool> R = parsePhysicalReg(C, Prefix, Reg);
2988
2989   if (!R.first)
2990     return std::make_pair(0U, nullptr);
2991
2992   if ((Prefix == "hi" || Prefix == "lo")) { // Parse hi/lo.
2993     // No numeric characters follow "hi" or "lo".
2994     if (R.second)
2995       return std::make_pair(0U, nullptr);
2996
2997     RC = TRI->getRegClass(Prefix == "hi" ?
2998                           Mips::HI32RegClassID : Mips::LO32RegClassID);
2999     return std::make_pair(*(RC->begin()), RC);
3000   } else if (Prefix.compare(0, 4, "$msa") == 0) {
3001     // Parse $msa(ir|csr|access|save|modify|request|map|unmap)
3002
3003     // No numeric characters follow the name.
3004     if (R.second)
3005       return std::make_pair(0U, nullptr);
3006
3007     Reg = StringSwitch<unsigned long long>(Prefix)
3008               .Case("$msair", Mips::MSAIR)
3009               .Case("$msacsr", Mips::MSACSR)
3010               .Case("$msaaccess", Mips::MSAAccess)
3011               .Case("$msasave", Mips::MSASave)
3012               .Case("$msamodify", Mips::MSAModify)
3013               .Case("$msarequest", Mips::MSARequest)
3014               .Case("$msamap", Mips::MSAMap)
3015               .Case("$msaunmap", Mips::MSAUnmap)
3016               .Default(0);
3017
3018     if (!Reg)
3019       return std::make_pair(0U, nullptr);
3020
3021     RC = TRI->getRegClass(Mips::MSACtrlRegClassID);
3022     return std::make_pair(Reg, RC);
3023   }
3024
3025   if (!R.second)
3026     return std::make_pair(0U, nullptr);
3027
3028   if (Prefix == "$f") { // Parse $f0-$f31.
3029     // If the size of FP registers is 64-bit or Reg is an even number, select
3030     // the 64-bit register class. Otherwise, select the 32-bit register class.
3031     if (VT == MVT::Other)
3032       VT = (Subtarget->isFP64bit() || !(Reg % 2)) ? MVT::f64 : MVT::f32;
3033
3034     RC = getRegClassFor(VT);
3035
3036     if (RC == &Mips::AFGR64RegClass) {
3037       assert(Reg % 2 == 0);
3038       Reg >>= 1;
3039     }
3040   } else if (Prefix == "$fcc") // Parse $fcc0-$fcc7.
3041     RC = TRI->getRegClass(Mips::FCCRegClassID);
3042   else if (Prefix == "$w") { // Parse $w0-$w31.
3043     RC = getRegClassFor((VT == MVT::Other) ? MVT::v16i8 : VT);
3044   } else { // Parse $0-$31.
3045     assert(Prefix == "$");
3046     RC = getRegClassFor((VT == MVT::Other) ? MVT::i32 : VT);
3047   }
3048
3049   assert(Reg < RC->getNumRegs());
3050   return std::make_pair(*(RC->begin() + Reg), RC);
3051 }
3052
3053 /// Given a register class constraint, like 'r', if this corresponds directly
3054 /// to an LLVM register class, return a register of 0 and the register class
3055 /// pointer.
3056 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
3057 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
3058 {
3059   if (Constraint.size() == 1) {
3060     switch (Constraint[0]) {
3061     case 'd': // Address register. Same as 'r' unless generating MIPS16 code.
3062     case 'y': // Same as 'r'. Exists for compatibility.
3063     case 'r':
3064       if (VT == MVT::i32 || VT == MVT::i16 || VT == MVT::i8) {
3065         if (Subtarget->inMips16Mode())
3066           return std::make_pair(0U, &Mips::CPU16RegsRegClass);
3067         return std::make_pair(0U, &Mips::GPR32RegClass);
3068       }
3069       if (VT == MVT::i64 && !Subtarget->isGP64bit())
3070         return std::make_pair(0U, &Mips::GPR32RegClass);
3071       if (VT == MVT::i64 && Subtarget->isGP64bit())
3072         return std::make_pair(0U, &Mips::GPR64RegClass);
3073       // This will generate an error message
3074       return std::make_pair(0U, nullptr);
3075     case 'f': // FPU or MSA register
3076       if (VT == MVT::v16i8)
3077         return std::make_pair(0U, &Mips::MSA128BRegClass);
3078       else if (VT == MVT::v8i16 || VT == MVT::v8f16)
3079         return std::make_pair(0U, &Mips::MSA128HRegClass);
3080       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3081         return std::make_pair(0U, &Mips::MSA128WRegClass);
3082       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3083         return std::make_pair(0U, &Mips::MSA128DRegClass);
3084       else if (VT == MVT::f32)
3085         return std::make_pair(0U, &Mips::FGR32RegClass);
3086       else if ((VT == MVT::f64) && (!Subtarget->isSingleFloat())) {
3087         if (Subtarget->isFP64bit())
3088           return std::make_pair(0U, &Mips::FGR64RegClass);
3089         return std::make_pair(0U, &Mips::AFGR64RegClass);
3090       }
3091       break;
3092     case 'c': // register suitable for indirect jump
3093       if (VT == MVT::i32)
3094         return std::make_pair((unsigned)Mips::T9, &Mips::GPR32RegClass);
3095       assert(VT == MVT::i64 && "Unexpected type.");
3096       return std::make_pair((unsigned)Mips::T9_64, &Mips::GPR64RegClass);
3097     case 'l': // register suitable for indirect jump
3098       if (VT == MVT::i32)
3099         return std::make_pair((unsigned)Mips::LO0, &Mips::LO32RegClass);
3100       return std::make_pair((unsigned)Mips::LO0_64, &Mips::LO64RegClass);
3101     case 'x': // register suitable for indirect jump
3102       // Fixme: Not triggering the use of both hi and low
3103       // This will generate an error message
3104       return std::make_pair(0U, nullptr);
3105     }
3106   }
3107
3108   std::pair<unsigned, const TargetRegisterClass *> R;
3109   R = parseRegForInlineAsmConstraint(Constraint, VT);
3110
3111   if (R.second)
3112     return R;
3113
3114   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3115 }
3116
3117 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3118 /// vector.  If it is invalid, don't add anything to Ops.
3119 void MipsTargetLowering::LowerAsmOperandForConstraint(SDValue Op,
3120                                                      std::string &Constraint,
3121                                                      std::vector<SDValue>&Ops,
3122                                                      SelectionDAG &DAG) const {
3123   SDValue Result;
3124
3125   // Only support length 1 constraints for now.
3126   if (Constraint.length() > 1) return;
3127
3128   char ConstraintLetter = Constraint[0];
3129   switch (ConstraintLetter) {
3130   default: break; // This will fall through to the generic implementation
3131   case 'I': // Signed 16 bit constant
3132     // If this fails, the parent routine will give an error
3133     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3134       EVT Type = Op.getValueType();
3135       int64_t Val = C->getSExtValue();
3136       if (isInt<16>(Val)) {
3137         Result = DAG.getTargetConstant(Val, Type);
3138         break;
3139       }
3140     }
3141     return;
3142   case 'J': // integer zero
3143     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3144       EVT Type = Op.getValueType();
3145       int64_t Val = C->getZExtValue();
3146       if (Val == 0) {
3147         Result = DAG.getTargetConstant(0, Type);
3148         break;
3149       }
3150     }
3151     return;
3152   case 'K': // unsigned 16 bit immediate
3153     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3154       EVT Type = Op.getValueType();
3155       uint64_t Val = (uint64_t)C->getZExtValue();
3156       if (isUInt<16>(Val)) {
3157         Result = DAG.getTargetConstant(Val, Type);
3158         break;
3159       }
3160     }
3161     return;
3162   case 'L': // signed 32 bit immediate where lower 16 bits are 0
3163     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3164       EVT Type = Op.getValueType();
3165       int64_t Val = C->getSExtValue();
3166       if ((isInt<32>(Val)) && ((Val & 0xffff) == 0)){
3167         Result = DAG.getTargetConstant(Val, Type);
3168         break;
3169       }
3170     }
3171     return;
3172   case 'N': // immediate in the range of -65535 to -1 (inclusive)
3173     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3174       EVT Type = Op.getValueType();
3175       int64_t Val = C->getSExtValue();
3176       if ((Val >= -65535) && (Val <= -1)) {
3177         Result = DAG.getTargetConstant(Val, Type);
3178         break;
3179       }
3180     }
3181     return;
3182   case 'O': // signed 15 bit immediate
3183     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3184       EVT Type = Op.getValueType();
3185       int64_t Val = C->getSExtValue();
3186       if ((isInt<15>(Val))) {
3187         Result = DAG.getTargetConstant(Val, Type);
3188         break;
3189       }
3190     }
3191     return;
3192   case 'P': // immediate in the range of 1 to 65535 (inclusive)
3193     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3194       EVT Type = Op.getValueType();
3195       int64_t Val = C->getSExtValue();
3196       if ((Val <= 65535) && (Val >= 1)) {
3197         Result = DAG.getTargetConstant(Val, Type);
3198         break;
3199       }
3200     }
3201     return;
3202   }
3203
3204   if (Result.getNode()) {
3205     Ops.push_back(Result);
3206     return;
3207   }
3208
3209   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3210 }
3211
3212 bool MipsTargetLowering::isLegalAddressingMode(const AddrMode &AM,
3213                                                Type *Ty) const {
3214   // No global is ever allowed as a base.
3215   if (AM.BaseGV)
3216     return false;
3217
3218   switch (AM.Scale) {
3219   case 0: // "r+i" or just "i", depending on HasBaseReg.
3220     break;
3221   case 1:
3222     if (!AM.HasBaseReg) // allow "r+i".
3223       break;
3224     return false; // disallow "r+r" or "r+r+i".
3225   default:
3226     return false;
3227   }
3228
3229   return true;
3230 }
3231
3232 bool
3233 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3234   // The Mips target isn't yet aware of offsets.
3235   return false;
3236 }
3237
3238 EVT MipsTargetLowering::getOptimalMemOpType(uint64_t Size, unsigned DstAlign,
3239                                             unsigned SrcAlign,
3240                                             bool IsMemset, bool ZeroMemset,
3241                                             bool MemcpyStrSrc,
3242                                             MachineFunction &MF) const {
3243   if (Subtarget->hasMips64())
3244     return MVT::i64;
3245
3246   return MVT::i32;
3247 }
3248
3249 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
3250   if (VT != MVT::f32 && VT != MVT::f64)
3251     return false;
3252   if (Imm.isNegZero())
3253     return false;
3254   return Imm.isZero();
3255 }
3256
3257 unsigned MipsTargetLowering::getJumpTableEncoding() const {
3258   if (Subtarget->isABI_N64())
3259     return MachineJumpTableInfo::EK_GPRel64BlockAddress;
3260
3261   return TargetLowering::getJumpTableEncoding();
3262 }
3263
3264 /// This function returns true if CallSym is a long double emulation routine.
3265 static bool isF128SoftLibCall(const char *CallSym) {
3266   const char *const LibCalls[] =
3267     {"__addtf3", "__divtf3", "__eqtf2", "__extenddftf2", "__extendsftf2",
3268      "__fixtfdi", "__fixtfsi", "__fixtfti", "__fixunstfdi", "__fixunstfsi",
3269      "__fixunstfti", "__floatditf", "__floatsitf", "__floattitf",
3270      "__floatunditf", "__floatunsitf", "__floatuntitf", "__getf2", "__gttf2",
3271      "__letf2", "__lttf2", "__multf3", "__netf2", "__powitf2", "__subtf3",
3272      "__trunctfdf2", "__trunctfsf2", "__unordtf2",
3273      "ceill", "copysignl", "cosl", "exp2l", "expl", "floorl", "fmal", "fmodl",
3274      "log10l", "log2l", "logl", "nearbyintl", "powl", "rintl", "sinl", "sqrtl",
3275      "truncl"};
3276
3277   const char *const *End = LibCalls + array_lengthof(LibCalls);
3278
3279   // Check that LibCalls is sorted alphabetically.
3280   MipsTargetLowering::LTStr Comp;
3281
3282 #ifndef NDEBUG
3283   for (const char *const *I = LibCalls; I < End - 1; ++I)
3284     assert(Comp(*I, *(I + 1)));
3285 #endif
3286
3287   return std::binary_search(LibCalls, End, CallSym, Comp);
3288 }
3289
3290 /// This function returns true if Ty is fp128 or i128 which was originally a
3291 /// fp128.
3292 static bool originalTypeIsF128(const Type *Ty, const SDNode *CallNode) {
3293   if (Ty->isFP128Ty())
3294     return true;
3295
3296   const ExternalSymbolSDNode *ES =
3297     dyn_cast_or_null<const ExternalSymbolSDNode>(CallNode);
3298
3299   // If the Ty is i128 and the function being called is a long double emulation
3300   // routine, then the original type is f128.
3301   return (ES && Ty->isIntegerTy(128) && isF128SoftLibCall(ES->getSymbol()));
3302 }
3303
3304 MipsTargetLowering::MipsCC::SpecialCallingConvType
3305   MipsTargetLowering::getSpecialCallingConv(SDValue Callee) const {
3306   MipsCC::SpecialCallingConvType SpecialCallingConv =
3307     MipsCC::NoSpecialCallingConv;
3308   if (Subtarget->inMips16HardFloat()) {
3309     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
3310       llvm::StringRef Sym = G->getGlobal()->getName();
3311       Function *F = G->getGlobal()->getParent()->getFunction(Sym);
3312       if (F && F->hasFnAttribute("__Mips16RetHelper")) {
3313         SpecialCallingConv = MipsCC::Mips16RetHelperConv;
3314       }
3315     }
3316   }
3317   return SpecialCallingConv;
3318 }
3319
3320 MipsTargetLowering::MipsCC::MipsCC(
3321   CallingConv::ID CC, bool IsO32_, bool IsFP64_, CCState &Info,
3322   MipsCC::SpecialCallingConvType SpecialCallingConv_)
3323   : CCInfo(Info), CallConv(CC), IsO32(IsO32_), IsFP64(IsFP64_),
3324     SpecialCallingConv(SpecialCallingConv_){
3325   // Pre-allocate reserved argument area.
3326   CCInfo.AllocateStack(reservedArgArea(), 1);
3327 }
3328
3329
3330 void MipsTargetLowering::MipsCC::
3331 analyzeCallOperands(const SmallVectorImpl<ISD::OutputArg> &Args,
3332                     bool IsVarArg, bool IsSoftFloat, const SDNode *CallNode,
3333                     std::vector<ArgListEntry> &FuncArgs) {
3334   assert((CallConv != CallingConv::Fast || !IsVarArg) &&
3335          "CallingConv::Fast shouldn't be used for vararg functions.");
3336
3337   unsigned NumOpnds = Args.size();
3338   llvm::CCAssignFn *FixedFn = fixedArgFn(), *VarFn = varArgFn();
3339
3340   for (unsigned I = 0; I != NumOpnds; ++I) {
3341     MVT ArgVT = Args[I].VT;
3342     ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3343     bool R;
3344
3345     if (ArgFlags.isByVal()) {
3346       handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3347       continue;
3348     }
3349
3350     if (IsVarArg && !Args[I].IsFixed)
3351       R = VarFn(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo);
3352     else {
3353       MVT RegVT = getRegVT(ArgVT, FuncArgs[Args[I].OrigArgIndex].Ty, CallNode,
3354                            IsSoftFloat);
3355       R = FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo);
3356     }
3357
3358     if (R) {
3359 #ifndef NDEBUG
3360       dbgs() << "Call operand #" << I << " has unhandled type "
3361              << EVT(ArgVT).getEVTString();
3362 #endif
3363       llvm_unreachable(nullptr);
3364     }
3365   }
3366 }
3367
3368 void MipsTargetLowering::MipsCC::
3369 analyzeFormalArguments(const SmallVectorImpl<ISD::InputArg> &Args,
3370                        bool IsSoftFloat, Function::const_arg_iterator FuncArg) {
3371   unsigned NumArgs = Args.size();
3372   llvm::CCAssignFn *FixedFn = fixedArgFn();
3373   unsigned CurArgIdx = 0;
3374
3375   for (unsigned I = 0; I != NumArgs; ++I) {
3376     MVT ArgVT = Args[I].VT;
3377     ISD::ArgFlagsTy ArgFlags = Args[I].Flags;
3378     std::advance(FuncArg, Args[I].OrigArgIndex - CurArgIdx);
3379     CurArgIdx = Args[I].OrigArgIndex;
3380
3381     if (ArgFlags.isByVal()) {
3382       handleByValArg(I, ArgVT, ArgVT, CCValAssign::Full, ArgFlags);
3383       continue;
3384     }
3385
3386     MVT RegVT = getRegVT(ArgVT, FuncArg->getType(), nullptr, IsSoftFloat);
3387
3388     if (!FixedFn(I, ArgVT, RegVT, CCValAssign::Full, ArgFlags, CCInfo))
3389       continue;
3390
3391 #ifndef NDEBUG
3392     dbgs() << "Formal Arg #" << I << " has unhandled type "
3393            << EVT(ArgVT).getEVTString();
3394 #endif
3395     llvm_unreachable(nullptr);
3396   }
3397 }
3398
3399 template<typename Ty>
3400 void MipsTargetLowering::MipsCC::
3401 analyzeReturn(const SmallVectorImpl<Ty> &RetVals, bool IsSoftFloat,
3402               const SDNode *CallNode, const Type *RetTy) const {
3403   CCAssignFn *Fn;
3404
3405   if (IsSoftFloat && originalTypeIsF128(RetTy, CallNode))
3406     Fn = RetCC_F128Soft;
3407   else
3408     Fn = RetCC_Mips;
3409
3410   for (unsigned I = 0, E = RetVals.size(); I < E; ++I) {
3411     MVT VT = RetVals[I].VT;
3412     ISD::ArgFlagsTy Flags = RetVals[I].Flags;
3413     MVT RegVT = this->getRegVT(VT, RetTy, CallNode, IsSoftFloat);
3414
3415     if (Fn(I, VT, RegVT, CCValAssign::Full, Flags, this->CCInfo)) {
3416 #ifndef NDEBUG
3417       dbgs() << "Call result #" << I << " has unhandled type "
3418              << EVT(VT).getEVTString() << '\n';
3419 #endif
3420       llvm_unreachable(nullptr);
3421     }
3422   }
3423 }
3424
3425 void MipsTargetLowering::MipsCC::
3426 analyzeCallResult(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsSoftFloat,
3427                   const SDNode *CallNode, const Type *RetTy) const {
3428   analyzeReturn(Ins, IsSoftFloat, CallNode, RetTy);
3429 }
3430
3431 void MipsTargetLowering::MipsCC::
3432 analyzeReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsSoftFloat,
3433               const Type *RetTy) const {
3434   analyzeReturn(Outs, IsSoftFloat, nullptr, RetTy);
3435 }
3436
3437 void MipsTargetLowering::MipsCC::handleByValArg(unsigned ValNo, MVT ValVT,
3438                                                 MVT LocVT,
3439                                                 CCValAssign::LocInfo LocInfo,
3440                                                 ISD::ArgFlagsTy ArgFlags) {
3441   assert(ArgFlags.getByValSize() && "Byval argument's size shouldn't be 0.");
3442
3443   struct ByValArgInfo ByVal;
3444   unsigned RegSize = regSize();
3445   unsigned ByValSize = RoundUpToAlignment(ArgFlags.getByValSize(), RegSize);
3446   unsigned Align = std::min(std::max(ArgFlags.getByValAlign(), RegSize),
3447                             RegSize * 2);
3448
3449   if (useRegsForByval())
3450     allocateRegs(ByVal, ByValSize, Align);
3451
3452   // Allocate space on caller's stack.
3453   ByVal.Address = CCInfo.AllocateStack(ByValSize - RegSize * ByVal.NumRegs,
3454                                        Align);
3455   CCInfo.addLoc(CCValAssign::getMem(ValNo, ValVT, ByVal.Address, LocVT,
3456                                     LocInfo));
3457   ByValArgs.push_back(ByVal);
3458 }
3459
3460 unsigned MipsTargetLowering::MipsCC::numIntArgRegs() const {
3461   return IsO32 ? array_lengthof(O32IntRegs) : array_lengthof(Mips64IntRegs);
3462 }
3463
3464 unsigned MipsTargetLowering::MipsCC::reservedArgArea() const {
3465   return (IsO32 && (CallConv != CallingConv::Fast)) ? 16 : 0;
3466 }
3467
3468 const MCPhysReg *MipsTargetLowering::MipsCC::intArgRegs() const {
3469   return IsO32 ? O32IntRegs : Mips64IntRegs;
3470 }
3471
3472 llvm::CCAssignFn *MipsTargetLowering::MipsCC::fixedArgFn() const {
3473   if (CallConv == CallingConv::Fast)
3474     return CC_Mips_FastCC;
3475
3476   if (SpecialCallingConv == Mips16RetHelperConv)
3477     return CC_Mips16RetHelper;
3478   return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN;
3479 }
3480
3481 llvm::CCAssignFn *MipsTargetLowering::MipsCC::varArgFn() const {
3482   return IsO32 ? (IsFP64 ? CC_MipsO32_FP64 : CC_MipsO32_FP32) : CC_MipsN_VarArg;
3483 }
3484
3485 const MCPhysReg *MipsTargetLowering::MipsCC::shadowRegs() const {
3486   return IsO32 ? O32IntRegs : Mips64DPRegs;
3487 }
3488
3489 void MipsTargetLowering::MipsCC::allocateRegs(ByValArgInfo &ByVal,
3490                                               unsigned ByValSize,
3491                                               unsigned Align) {
3492   unsigned RegSize = regSize(), NumIntArgRegs = numIntArgRegs();
3493   const MCPhysReg *IntArgRegs = intArgRegs(), *ShadowRegs = shadowRegs();
3494   assert(!(ByValSize % RegSize) && !(Align % RegSize) &&
3495          "Byval argument's size and alignment should be a multiple of"
3496          "RegSize.");
3497
3498   ByVal.FirstIdx = CCInfo.getFirstUnallocated(IntArgRegs, NumIntArgRegs);
3499
3500   // If Align > RegSize, the first arg register must be even.
3501   if ((Align > RegSize) && (ByVal.FirstIdx % 2)) {
3502     CCInfo.AllocateReg(IntArgRegs[ByVal.FirstIdx], ShadowRegs[ByVal.FirstIdx]);
3503     ++ByVal.FirstIdx;
3504   }
3505
3506   // Mark the registers allocated.
3507   for (unsigned I = ByVal.FirstIdx; ByValSize && (I < NumIntArgRegs);
3508        ByValSize -= RegSize, ++I, ++ByVal.NumRegs)
3509     CCInfo.AllocateReg(IntArgRegs[I], ShadowRegs[I]);
3510 }
3511
3512 MVT MipsTargetLowering::MipsCC::getRegVT(MVT VT, const Type *OrigTy,
3513                                          const SDNode *CallNode,
3514                                          bool IsSoftFloat) const {
3515   if (IsSoftFloat || IsO32)
3516     return VT;
3517
3518   // Check if the original type was fp128.
3519   if (originalTypeIsF128(OrigTy, CallNode)) {
3520     assert(VT == MVT::i64);
3521     return MVT::f64;
3522   }
3523
3524   return VT;
3525 }
3526
3527 void MipsTargetLowering::
3528 copyByValRegs(SDValue Chain, SDLoc DL, std::vector<SDValue> &OutChains,
3529               SelectionDAG &DAG, const ISD::ArgFlagsTy &Flags,
3530               SmallVectorImpl<SDValue> &InVals, const Argument *FuncArg,
3531               const MipsCC &CC, const ByValArgInfo &ByVal) const {
3532   MachineFunction &MF = DAG.getMachineFunction();
3533   MachineFrameInfo *MFI = MF.getFrameInfo();
3534   unsigned RegAreaSize = ByVal.NumRegs * CC.regSize();
3535   unsigned FrameObjSize = std::max(Flags.getByValSize(), RegAreaSize);
3536   int FrameObjOffset;
3537
3538   if (RegAreaSize)
3539     FrameObjOffset = (int)CC.reservedArgArea() -
3540       (int)((CC.numIntArgRegs() - ByVal.FirstIdx) * CC.regSize());
3541   else
3542     FrameObjOffset = ByVal.Address;
3543
3544   // Create frame object.
3545   EVT PtrTy = getPointerTy();
3546   int FI = MFI->CreateFixedObject(FrameObjSize, FrameObjOffset, true);
3547   SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3548   InVals.push_back(FIN);
3549
3550   if (!ByVal.NumRegs)
3551     return;
3552
3553   // Copy arg registers.
3554   MVT RegTy = MVT::getIntegerVT(CC.regSize() * 8);
3555   const TargetRegisterClass *RC = getRegClassFor(RegTy);
3556
3557   for (unsigned I = 0; I < ByVal.NumRegs; ++I) {
3558     unsigned ArgReg = CC.intArgRegs()[ByVal.FirstIdx + I];
3559     unsigned VReg = addLiveIn(MF, ArgReg, RC);
3560     unsigned Offset = I * CC.regSize();
3561     SDValue StorePtr = DAG.getNode(ISD::ADD, DL, PtrTy, FIN,
3562                                    DAG.getConstant(Offset, PtrTy));
3563     SDValue Store = DAG.getStore(Chain, DL, DAG.getRegister(VReg, RegTy),
3564                                  StorePtr, MachinePointerInfo(FuncArg, Offset),
3565                                  false, false, 0);
3566     OutChains.push_back(Store);
3567   }
3568 }
3569
3570 // Copy byVal arg to registers and stack.
3571 void MipsTargetLowering::
3572 passByValArg(SDValue Chain, SDLoc DL,
3573              std::deque< std::pair<unsigned, SDValue> > &RegsToPass,
3574              SmallVectorImpl<SDValue> &MemOpChains, SDValue StackPtr,
3575              MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
3576              const MipsCC &CC, const ByValArgInfo &ByVal,
3577              const ISD::ArgFlagsTy &Flags, bool isLittle) const {
3578   unsigned ByValSizeInBytes = Flags.getByValSize();
3579   unsigned OffsetInBytes = 0; // From beginning of struct
3580   unsigned RegSizeInBytes = CC.regSize();
3581   unsigned Alignment = std::min(Flags.getByValAlign(), RegSizeInBytes);
3582   EVT PtrTy = getPointerTy(), RegTy = MVT::getIntegerVT(RegSizeInBytes * 8);
3583
3584   if (ByVal.NumRegs) {
3585     const MCPhysReg *ArgRegs = CC.intArgRegs();
3586     bool LeftoverBytes = (ByVal.NumRegs * RegSizeInBytes > ByValSizeInBytes);
3587     unsigned I = 0;
3588
3589     // Copy words to registers.
3590     for (; I < ByVal.NumRegs - LeftoverBytes;
3591          ++I, OffsetInBytes += RegSizeInBytes) {
3592       SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3593                                     DAG.getConstant(OffsetInBytes, PtrTy));
3594       SDValue LoadVal = DAG.getLoad(RegTy, DL, Chain, LoadPtr,
3595                                     MachinePointerInfo(), false, false, false,
3596                                     Alignment);
3597       MemOpChains.push_back(LoadVal.getValue(1));
3598       unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3599       RegsToPass.push_back(std::make_pair(ArgReg, LoadVal));
3600     }
3601
3602     // Return if the struct has been fully copied.
3603     if (ByValSizeInBytes == OffsetInBytes)
3604       return;
3605
3606     // Copy the remainder of the byval argument with sub-word loads and shifts.
3607     if (LeftoverBytes) {
3608       assert((ByValSizeInBytes > OffsetInBytes) &&
3609              (ByValSizeInBytes < OffsetInBytes + RegSizeInBytes) &&
3610              "Size of the remainder should be smaller than RegSizeInBytes.");
3611       SDValue Val;
3612
3613       for (unsigned LoadSizeInBytes = RegSizeInBytes / 2, TotalBytesLoaded = 0;
3614            OffsetInBytes < ByValSizeInBytes; LoadSizeInBytes /= 2) {
3615         unsigned RemainingSizeInBytes = ByValSizeInBytes - OffsetInBytes;
3616
3617         if (RemainingSizeInBytes < LoadSizeInBytes)
3618           continue;
3619
3620         // Load subword.
3621         SDValue LoadPtr = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3622                                       DAG.getConstant(OffsetInBytes, PtrTy));
3623         SDValue LoadVal = DAG.getExtLoad(
3624             ISD::ZEXTLOAD, DL, RegTy, Chain, LoadPtr, MachinePointerInfo(),
3625             MVT::getIntegerVT(LoadSizeInBytes * 8), false, false, Alignment);
3626         MemOpChains.push_back(LoadVal.getValue(1));
3627
3628         // Shift the loaded value.
3629         unsigned Shamt;
3630
3631         if (isLittle)
3632           Shamt = TotalBytesLoaded * 8;
3633         else
3634           Shamt = (RegSizeInBytes - (TotalBytesLoaded + LoadSizeInBytes)) * 8;
3635
3636         SDValue Shift = DAG.getNode(ISD::SHL, DL, RegTy, LoadVal,
3637                                     DAG.getConstant(Shamt, MVT::i32));
3638
3639         if (Val.getNode())
3640           Val = DAG.getNode(ISD::OR, DL, RegTy, Val, Shift);
3641         else
3642           Val = Shift;
3643
3644         OffsetInBytes += LoadSizeInBytes;
3645         TotalBytesLoaded += LoadSizeInBytes;
3646         Alignment = std::min(Alignment, LoadSizeInBytes);
3647       }
3648
3649       unsigned ArgReg = ArgRegs[ByVal.FirstIdx + I];
3650       RegsToPass.push_back(std::make_pair(ArgReg, Val));
3651       return;
3652     }
3653   }
3654
3655   // Copy remainder of byval arg to it with memcpy.
3656   unsigned MemCpySize = ByValSizeInBytes - OffsetInBytes;
3657   SDValue Src = DAG.getNode(ISD::ADD, DL, PtrTy, Arg,
3658                             DAG.getConstant(OffsetInBytes, PtrTy));
3659   SDValue Dst = DAG.getNode(ISD::ADD, DL, PtrTy, StackPtr,
3660                             DAG.getIntPtrConstant(ByVal.Address));
3661   Chain = DAG.getMemcpy(Chain, DL, Dst, Src, DAG.getConstant(MemCpySize, PtrTy),
3662                         Alignment, /*isVolatile=*/false, /*AlwaysInline=*/false,
3663                         MachinePointerInfo(), MachinePointerInfo());
3664   MemOpChains.push_back(Chain);
3665 }
3666
3667 void MipsTargetLowering::writeVarArgRegs(std::vector<SDValue> &OutChains,
3668                                          const MipsCC &CC, SDValue Chain,
3669                                          SDLoc DL, SelectionDAG &DAG) const {
3670   unsigned NumRegs = CC.numIntArgRegs();
3671   const MCPhysReg *ArgRegs = CC.intArgRegs();
3672   const CCState &CCInfo = CC.getCCInfo();
3673   unsigned Idx = CCInfo.getFirstUnallocated(ArgRegs, NumRegs);
3674   unsigned RegSize = CC.regSize();
3675   MVT RegTy = MVT::getIntegerVT(RegSize * 8);
3676   const TargetRegisterClass *RC = getRegClassFor(RegTy);
3677   MachineFunction &MF = DAG.getMachineFunction();
3678   MachineFrameInfo *MFI = MF.getFrameInfo();
3679   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
3680
3681   // Offset of the first variable argument from stack pointer.
3682   int VaArgOffset;
3683
3684   if (NumRegs == Idx)
3685     VaArgOffset = RoundUpToAlignment(CCInfo.getNextStackOffset(), RegSize);
3686   else
3687     VaArgOffset = (int)CC.reservedArgArea() - (int)(RegSize * (NumRegs - Idx));
3688
3689   // Record the frame index of the first variable argument
3690   // which is a value necessary to VASTART.
3691   int FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3692   MipsFI->setVarArgsFrameIndex(FI);
3693
3694   // Copy the integer registers that have not been used for argument passing
3695   // to the argument register save area. For O32, the save area is allocated
3696   // in the caller's stack frame, while for N32/64, it is allocated in the
3697   // callee's stack frame.
3698   for (unsigned I = Idx; I < NumRegs; ++I, VaArgOffset += RegSize) {
3699     unsigned Reg = addLiveIn(MF, ArgRegs[I], RC);
3700     SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, Reg, RegTy);
3701     FI = MFI->CreateFixedObject(RegSize, VaArgOffset, true);
3702     SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
3703     SDValue Store = DAG.getStore(Chain, DL, ArgValue, PtrOff,
3704                                  MachinePointerInfo(), false, false, 0);
3705     cast<StoreSDNode>(Store.getNode())->getMemOperand()->setValue((Value*)nullptr);
3706     OutChains.push_back(Store);
3707   }
3708 }