Coding style fixes.
[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
15 #define DEBUG_TYPE "mips-lower"
16 #include "MipsISelLowering.h"
17 #include "MipsMachineFunction.h"
18 #include "MipsTargetMachine.h"
19 #include "MipsTargetObjectFile.h"
20 #include "MipsSubtarget.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/CallingConv.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAGISel.h"
32 #include "llvm/CodeGen/ValueTypes.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 using namespace llvm;
36
37 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
38   switch (Opcode) {
39   case MipsISD::JmpLink:           return "MipsISD::JmpLink";
40   case MipsISD::Hi:                return "MipsISD::Hi";
41   case MipsISD::Lo:                return "MipsISD::Lo";
42   case MipsISD::GPRel:             return "MipsISD::GPRel";
43   case MipsISD::TlsGd:             return "MipsISD::TlsGd";
44   case MipsISD::TprelHi:           return "MipsISD::TprelHi";
45   case MipsISD::TprelLo:           return "MipsISD::TprelLo";
46   case MipsISD::ThreadPointer:     return "MipsISD::ThreadPointer";
47   case MipsISD::Ret:               return "MipsISD::Ret";
48   case MipsISD::FPBrcond:          return "MipsISD::FPBrcond";
49   case MipsISD::FPCmp:             return "MipsISD::FPCmp";
50   case MipsISD::CMovFP_T:          return "MipsISD::CMovFP_T";
51   case MipsISD::CMovFP_F:          return "MipsISD::CMovFP_F";
52   case MipsISD::FPRound:           return "MipsISD::FPRound";
53   case MipsISD::MAdd:              return "MipsISD::MAdd";
54   case MipsISD::MAddu:             return "MipsISD::MAddu";
55   case MipsISD::MSub:              return "MipsISD::MSub";
56   case MipsISD::MSubu:             return "MipsISD::MSubu";
57   case MipsISD::DivRem:            return "MipsISD::DivRem";
58   case MipsISD::DivRemU:           return "MipsISD::DivRemU";
59   case MipsISD::BuildPairF64:      return "MipsISD::BuildPairF64";
60   case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
61   case MipsISD::WrapperPIC:        return "MipsISD::WrapperPIC";
62   default:                         return NULL;
63   }
64 }
65
66 MipsTargetLowering::
67 MipsTargetLowering(MipsTargetMachine &TM)
68   : TargetLowering(TM, new MipsTargetObjectFile()) {
69   Subtarget = &TM.getSubtarget<MipsSubtarget>();
70
71   // Mips does not have i1 type, so use i32 for
72   // setcc operations results (slt, sgt, ...).
73   setBooleanContents(ZeroOrOneBooleanContent);
74
75   // Set up the register classes
76   addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
77   addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
78
79   // When dealing with single precision only, use libcalls
80   if (!Subtarget->isSingleFloat())
81     if (!Subtarget->isFP64bit())
82       addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
83
84   // Load extented operations for i1 types must be promoted
85   setLoadExtAction(ISD::EXTLOAD,  MVT::i1,  Promote);
86   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
87   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
88
89   // MIPS doesn't have extending float->double load/store
90   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
91   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
92
93   // Used by legalize types to correctly generate the setcc result.
94   // Without this, every float setcc comes with a AND/OR with the result,
95   // we don't want this, since the fpcmp result goes to a flag register,
96   // which is used implicitly by brcond and select operations.
97   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
98
99   // Mips Custom Operations
100   setOperationAction(ISD::GlobalAddress,      MVT::i32,   Custom);
101   setOperationAction(ISD::BlockAddress,       MVT::i32,   Custom);
102   setOperationAction(ISD::GlobalTLSAddress,   MVT::i32,   Custom);
103   setOperationAction(ISD::JumpTable,          MVT::i32,   Custom);
104   setOperationAction(ISD::ConstantPool,       MVT::i32,   Custom);
105   setOperationAction(ISD::SELECT,             MVT::f32,   Custom);
106   setOperationAction(ISD::SELECT,             MVT::f64,   Custom);
107   setOperationAction(ISD::SELECT,             MVT::i32,   Custom);
108   setOperationAction(ISD::BRCOND,             MVT::Other, Custom);
109   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,   Custom);
110   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
111
112   setOperationAction(ISD::SDIV, MVT::i32, Expand);
113   setOperationAction(ISD::SREM, MVT::i32, Expand);
114   setOperationAction(ISD::UDIV, MVT::i32, Expand);
115   setOperationAction(ISD::UREM, MVT::i32, Expand);
116
117   // Operations not directly supported by Mips.
118   setOperationAction(ISD::BR_JT,             MVT::Other, Expand);
119   setOperationAction(ISD::BR_CC,             MVT::Other, Expand);
120   setOperationAction(ISD::SELECT_CC,         MVT::Other, Expand);
121   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
122   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
123   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
124   setOperationAction(ISD::CTPOP,             MVT::i32,   Expand);
125   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
126   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
127
128   if (!Subtarget->isMips32r2())
129     setOperationAction(ISD::ROTR, MVT::i32,   Expand);
130
131   setOperationAction(ISD::SHL_PARTS,         MVT::i32,   Expand);
132   setOperationAction(ISD::SRA_PARTS,         MVT::i32,   Expand);
133   setOperationAction(ISD::SRL_PARTS,         MVT::i32,   Expand);
134   setOperationAction(ISD::FCOPYSIGN,         MVT::f32,   Custom);
135   setOperationAction(ISD::FCOPYSIGN,         MVT::f64,   Custom);
136   setOperationAction(ISD::FSIN,              MVT::f32,   Expand);
137   setOperationAction(ISD::FSIN,              MVT::f64,   Expand);
138   setOperationAction(ISD::FCOS,              MVT::f32,   Expand);
139   setOperationAction(ISD::FCOS,              MVT::f64,   Expand);
140   setOperationAction(ISD::FPOWI,             MVT::f32,   Expand);
141   setOperationAction(ISD::FPOW,              MVT::f32,   Expand);
142   setOperationAction(ISD::FPOW,              MVT::f64,   Expand);
143   setOperationAction(ISD::FLOG,              MVT::f32,   Expand);
144   setOperationAction(ISD::FLOG2,             MVT::f32,   Expand);
145   setOperationAction(ISD::FLOG10,            MVT::f32,   Expand);
146   setOperationAction(ISD::FEXP,              MVT::f32,   Expand);
147
148   setOperationAction(ISD::EXCEPTIONADDR,     MVT::i32, Expand);
149   setOperationAction(ISD::EHSELECTION,       MVT::i32, Expand);
150   
151   setOperationAction(ISD::VAARG,             MVT::Other, Expand);
152   setOperationAction(ISD::VACOPY,            MVT::Other, Expand);
153   setOperationAction(ISD::VAEND,             MVT::Other, Expand);
154
155   // Use the default for now
156   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
157   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
158   setOperationAction(ISD::MEMBARRIER,        MVT::Other, Expand);
159
160   if (Subtarget->isSingleFloat())
161     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
162
163   if (!Subtarget->hasSEInReg()) {
164     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
165     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
166   }
167
168   if (!Subtarget->hasBitCount())
169     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
170
171   if (!Subtarget->hasSwap())
172     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
173
174   setTargetDAGCombine(ISD::ADDE);
175   setTargetDAGCombine(ISD::SUBE);
176   setTargetDAGCombine(ISD::SDIVREM);
177   setTargetDAGCombine(ISD::UDIVREM);
178   setTargetDAGCombine(ISD::SETCC);
179
180   setMinFunctionAlignment(2);
181
182   setStackPointerRegisterToSaveRestore(Mips::SP);
183   computeRegisterProperties();
184
185   setExceptionPointerRegister(Mips::A0);
186   setExceptionSelectorRegister(Mips::A1);
187 }
188
189 MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
190   return MVT::i32;
191 }
192
193 // SelectMadd -
194 // Transforms a subgraph in CurDAG if the following pattern is found:
195 //  (addc multLo, Lo0), (adde multHi, Hi0),
196 // where,
197 //  multHi/Lo: product of multiplication
198 //  Lo0: initial value of Lo register
199 //  Hi0: initial value of Hi register
200 // Return true if pattern matching was successful.
201 static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
202   // ADDENode's second operand must be a flag output of an ADDC node in order
203   // for the matching to be successful.
204   SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
205
206   if (ADDCNode->getOpcode() != ISD::ADDC)
207     return false;
208
209   SDValue MultHi = ADDENode->getOperand(0);
210   SDValue MultLo = ADDCNode->getOperand(0);
211   SDNode* MultNode = MultHi.getNode();
212   unsigned MultOpc = MultHi.getOpcode();
213
214   // MultHi and MultLo must be generated by the same node,
215   if (MultLo.getNode() != MultNode)
216     return false;
217
218   // and it must be a multiplication.
219   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
220     return false;
221
222   // MultLo amd MultHi must be the first and second output of MultNode
223   // respectively.
224   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
225     return false;
226
227   // Transform this to a MADD only if ADDENode and ADDCNode are the only users
228   // of the values of MultNode, in which case MultNode will be removed in later
229   // phases.
230   // If there exist users other than ADDENode or ADDCNode, this function returns
231   // here, which will result in MultNode being mapped to a single MULT
232   // instruction node rather than a pair of MULT and MADD instructions being
233   // produced.
234   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
235     return false;
236
237   SDValue Chain = CurDAG->getEntryNode();
238   DebugLoc dl = ADDENode->getDebugLoc();
239
240   // create MipsMAdd(u) node
241   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
242
243   SDValue MAdd = CurDAG->getNode(MultOpc, dl,
244                                  MVT::Glue,
245                                  MultNode->getOperand(0),// Factor 0
246                                  MultNode->getOperand(1),// Factor 1
247                                  ADDCNode->getOperand(1),// Lo0
248                                  ADDENode->getOperand(1));// Hi0
249
250   // create CopyFromReg nodes
251   SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
252                                               MAdd);
253   SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
254                                               Mips::HI, MVT::i32,
255                                               CopyFromLo.getValue(2));
256
257   // replace uses of adde and addc here
258   if (!SDValue(ADDCNode, 0).use_empty())
259     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
260
261   if (!SDValue(ADDENode, 0).use_empty())
262     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
263
264   return true;
265 }
266
267 // SelectMsub -
268 // Transforms a subgraph in CurDAG if the following pattern is found:
269 //  (addc Lo0, multLo), (sube Hi0, multHi),
270 // where,
271 //  multHi/Lo: product of multiplication
272 //  Lo0: initial value of Lo register
273 //  Hi0: initial value of Hi register
274 // Return true if pattern matching was successful.
275 static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
276   // SUBENode's second operand must be a flag output of an SUBC node in order
277   // for the matching to be successful.
278   SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
279
280   if (SUBCNode->getOpcode() != ISD::SUBC)
281     return false;
282
283   SDValue MultHi = SUBENode->getOperand(1);
284   SDValue MultLo = SUBCNode->getOperand(1);
285   SDNode* MultNode = MultHi.getNode();
286   unsigned MultOpc = MultHi.getOpcode();
287
288   // MultHi and MultLo must be generated by the same node,
289   if (MultLo.getNode() != MultNode)
290     return false;
291
292   // and it must be a multiplication.
293   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
294     return false;
295
296   // MultLo amd MultHi must be the first and second output of MultNode
297   // respectively.
298   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
299     return false;
300
301   // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
302   // of the values of MultNode, in which case MultNode will be removed in later
303   // phases.
304   // If there exist users other than SUBENode or SUBCNode, this function returns
305   // here, which will result in MultNode being mapped to a single MULT
306   // instruction node rather than a pair of MULT and MSUB instructions being
307   // produced.
308   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
309     return false;
310
311   SDValue Chain = CurDAG->getEntryNode();
312   DebugLoc dl = SUBENode->getDebugLoc();
313
314   // create MipsSub(u) node
315   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
316
317   SDValue MSub = CurDAG->getNode(MultOpc, dl,
318                                  MVT::Glue,
319                                  MultNode->getOperand(0),// Factor 0
320                                  MultNode->getOperand(1),// Factor 1
321                                  SUBCNode->getOperand(0),// Lo0
322                                  SUBENode->getOperand(0));// Hi0
323
324   // create CopyFromReg nodes
325   SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
326                                               MSub);
327   SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
328                                               Mips::HI, MVT::i32,
329                                               CopyFromLo.getValue(2));
330
331   // replace uses of sube and subc here
332   if (!SDValue(SUBCNode, 0).use_empty())
333     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
334
335   if (!SDValue(SUBENode, 0).use_empty())
336     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
337
338   return true;
339 }
340
341 static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
342                                   TargetLowering::DAGCombinerInfo &DCI,
343                                   const MipsSubtarget* Subtarget) {
344   if (DCI.isBeforeLegalize())
345     return SDValue();
346
347   if (Subtarget->isMips32() && SelectMadd(N, &DAG))
348     return SDValue(N, 0);
349
350   return SDValue();
351 }
352
353 static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
354                                   TargetLowering::DAGCombinerInfo &DCI,
355                                   const MipsSubtarget* Subtarget) {
356   if (DCI.isBeforeLegalize())
357     return SDValue();
358
359   if (Subtarget->isMips32() && SelectMsub(N, &DAG))
360     return SDValue(N, 0);
361
362   return SDValue();
363 }
364
365 static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
366                                     TargetLowering::DAGCombinerInfo &DCI,
367                                     const MipsSubtarget* Subtarget) {
368   if (DCI.isBeforeLegalizeOps())
369     return SDValue();
370
371   unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
372                                                   MipsISD::DivRemU;
373   DebugLoc dl = N->getDebugLoc();
374
375   SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
376                                N->getOperand(0), N->getOperand(1));
377   SDValue InChain = DAG.getEntryNode();
378   SDValue InGlue = DivRem;
379
380   // insert MFLO
381   if (N->hasAnyUseOfValue(0)) {
382     SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
383                                             InGlue);
384     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
385     InChain = CopyFromLo.getValue(1);
386     InGlue = CopyFromLo.getValue(2);
387   }
388
389   // insert MFHI
390   if (N->hasAnyUseOfValue(1)) {
391     SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
392                                             Mips::HI, MVT::i32, InGlue);
393     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
394   }
395
396   return SDValue();
397 }
398
399 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
400   switch (CC) {
401   default: llvm_unreachable("Unknown fp condition code!");
402   case ISD::SETEQ:
403   case ISD::SETOEQ: return Mips::FCOND_OEQ;
404   case ISD::SETUNE: return Mips::FCOND_UNE;
405   case ISD::SETLT:
406   case ISD::SETOLT: return Mips::FCOND_OLT;
407   case ISD::SETGT:
408   case ISD::SETOGT: return Mips::FCOND_OGT;
409   case ISD::SETLE:
410   case ISD::SETOLE: return Mips::FCOND_OLE;
411   case ISD::SETGE:
412   case ISD::SETOGE: return Mips::FCOND_OGE;
413   case ISD::SETULT: return Mips::FCOND_ULT;
414   case ISD::SETULE: return Mips::FCOND_ULE;
415   case ISD::SETUGT: return Mips::FCOND_UGT;
416   case ISD::SETUGE: return Mips::FCOND_UGE;
417   case ISD::SETUO:  return Mips::FCOND_UN;
418   case ISD::SETO:   return Mips::FCOND_OR;
419   case ISD::SETNE:
420   case ISD::SETONE: return Mips::FCOND_ONE;
421   case ISD::SETUEQ: return Mips::FCOND_UEQ;
422   }
423 }
424
425
426 // Returns true if condition code has to be inverted.
427 static bool InvertFPCondCode(Mips::CondCode CC) {
428   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
429     return false;
430
431   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
432     return true;
433
434   assert(false && "Illegal Condition Code");
435   return false;
436 }
437
438 // Creates and returns an FPCmp node from a setcc node.
439 // Returns Op if setcc is not a floating point comparison.
440 static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
441   // must be a SETCC node
442   if (Op.getOpcode() != ISD::SETCC)
443     return Op;
444
445   SDValue LHS = Op.getOperand(0);
446
447   if (!LHS.getValueType().isFloatingPoint())
448     return Op;
449
450   SDValue RHS = Op.getOperand(1);
451   DebugLoc dl = Op.getDebugLoc();
452
453   // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
454   // node if necessary.
455   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
456
457   return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
458                      DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
459 }
460
461 // Creates and returns a CMovFPT/F node.
462 static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
463                             SDValue False, DebugLoc DL) {
464   bool invert = InvertFPCondCode((Mips::CondCode)
465                                  cast<ConstantSDNode>(Cond.getOperand(2))
466                                  ->getSExtValue());
467
468   return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
469                      True.getValueType(), True, False, Cond);
470 }
471
472 static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
473                                    TargetLowering::DAGCombinerInfo &DCI,
474                                    const MipsSubtarget* Subtarget) {
475   if (DCI.isBeforeLegalizeOps())
476     return SDValue();
477
478   SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
479
480   if (Cond.getOpcode() != MipsISD::FPCmp)
481     return SDValue();
482
483   SDValue True  = DAG.getConstant(1, MVT::i32);
484   SDValue False = DAG.getConstant(0, MVT::i32);
485
486   return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
487 }
488
489 SDValue  MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
490   const {
491   SelectionDAG &DAG = DCI.DAG;
492   unsigned opc = N->getOpcode();
493
494   switch (opc) {
495   default: break;
496   case ISD::ADDE:
497     return PerformADDECombine(N, DAG, DCI, Subtarget);
498   case ISD::SUBE:
499     return PerformSUBECombine(N, DAG, DCI, Subtarget);
500   case ISD::SDIVREM:
501   case ISD::UDIVREM:
502     return PerformDivRemCombine(N, DAG, DCI, Subtarget);
503   case ISD::SETCC:
504     return PerformSETCCCombine(N, DAG, DCI, Subtarget);
505   }
506
507   return SDValue();
508 }
509
510 SDValue MipsTargetLowering::
511 LowerOperation(SDValue Op, SelectionDAG &DAG) const
512 {
513   switch (Op.getOpcode())
514   {
515     case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
516     case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
517     case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
518     case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
519     case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
520     case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
521     case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
522     case ISD::SELECT:             return LowerSELECT(Op, DAG);
523     case ISD::VASTART:            return LowerVASTART(Op, DAG);
524     case ISD::FCOPYSIGN:          return LowerFCOPYSIGN(Op, DAG);
525     case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
526   }
527   return SDValue();
528 }
529
530 //===----------------------------------------------------------------------===//
531 //  Lower helper functions
532 //===----------------------------------------------------------------------===//
533
534 // AddLiveIn - This helper function adds the specified physical register to the
535 // MachineFunction as a live in value.  It also creates a corresponding
536 // virtual register for it.
537 static unsigned
538 AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
539 {
540   assert(RC->contains(PReg) && "Not the correct regclass!");
541   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
542   MF.getRegInfo().addLiveIn(PReg, VReg);
543   return VReg;
544 }
545
546 // Get fp branch code (not opcode) from condition code.
547 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
548   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
549     return Mips::BRANCH_T;
550
551   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
552     return Mips::BRANCH_F;
553
554   return Mips::BRANCH_INVALID;
555 }
556
557 MachineBasicBlock *
558 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
559                                                 MachineBasicBlock *BB) const {
560   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
561   bool isFPCmp = false;
562   DebugLoc dl = MI->getDebugLoc();
563   unsigned Opc;
564
565   switch (MI->getOpcode()) {
566   default: assert(false && "Unexpected instr type to insert");
567
568   case Mips::ATOMIC_LOAD_ADD_I8:
569     return EmitAtomicBinaryPartword(MI, BB, 1, Mips::ADDu);
570   case Mips::ATOMIC_LOAD_ADD_I16:
571     return EmitAtomicBinaryPartword(MI, BB, 2, Mips::ADDu);
572   case Mips::ATOMIC_LOAD_ADD_I32:
573     return EmitAtomicBinary(MI, BB, 4, Mips::ADDu);
574
575   case Mips::ATOMIC_LOAD_AND_I8:
576     return EmitAtomicBinaryPartword(MI, BB, 1, Mips::AND);
577   case Mips::ATOMIC_LOAD_AND_I16:
578     return EmitAtomicBinaryPartword(MI, BB, 2, Mips::AND);
579   case Mips::ATOMIC_LOAD_AND_I32:
580     return EmitAtomicBinary(MI, BB, 4, Mips::AND);
581
582   case Mips::ATOMIC_LOAD_OR_I8:
583     return EmitAtomicBinaryPartword(MI, BB, 1, Mips::OR);
584   case Mips::ATOMIC_LOAD_OR_I16:
585     return EmitAtomicBinaryPartword(MI, BB, 2, Mips::OR);
586   case Mips::ATOMIC_LOAD_OR_I32:
587     return EmitAtomicBinary(MI, BB, 4, Mips::OR);
588
589   case Mips::ATOMIC_LOAD_XOR_I8:
590     return EmitAtomicBinaryPartword(MI, BB, 1, Mips::XOR);
591   case Mips::ATOMIC_LOAD_XOR_I16:
592     return EmitAtomicBinaryPartword(MI, BB, 2, Mips::XOR);
593   case Mips::ATOMIC_LOAD_XOR_I32:
594     return EmitAtomicBinary(MI, BB, 4, Mips::XOR);
595
596   case Mips::ATOMIC_LOAD_NAND_I8:
597     return EmitAtomicBinaryPartword(MI, BB, 1, 0, true);
598   case Mips::ATOMIC_LOAD_NAND_I16:
599     return EmitAtomicBinaryPartword(MI, BB, 2, 0, true);
600   case Mips::ATOMIC_LOAD_NAND_I32:
601     return EmitAtomicBinary(MI, BB, 4, 0, true);
602
603   case Mips::ATOMIC_LOAD_SUB_I8:
604     return EmitAtomicBinaryPartword(MI, BB, 1, Mips::SUBu);
605   case Mips::ATOMIC_LOAD_SUB_I16:
606     return EmitAtomicBinaryPartword(MI, BB, 2, Mips::SUBu);
607   case Mips::ATOMIC_LOAD_SUB_I32:
608     return EmitAtomicBinary(MI, BB, 4, Mips::SUBu);
609
610   case Mips::ATOMIC_SWAP_I8:
611     return EmitAtomicBinaryPartword(MI, BB, 1, 0);
612   case Mips::ATOMIC_SWAP_I16:
613     return EmitAtomicBinaryPartword(MI, BB, 2, 0);
614   case Mips::ATOMIC_SWAP_I32:
615     return EmitAtomicBinary(MI, BB, 4, 0);
616
617   case Mips::ATOMIC_CMP_SWAP_I8:
618     return EmitAtomicCmpSwapPartword(MI, BB, 1);
619   case Mips::ATOMIC_CMP_SWAP_I16:
620     return EmitAtomicCmpSwapPartword(MI, BB, 2);
621   case Mips::ATOMIC_CMP_SWAP_I32:
622     return EmitAtomicCmpSwap(MI, BB, 4);
623
624   case Mips::MOVT:
625   case Mips::MOVT_S:
626   case Mips::MOVT_D:
627     isFPCmp = true;
628     Opc = Mips::BC1F;
629     break;
630   case Mips::MOVF:
631   case Mips::MOVF_S:
632   case Mips::MOVF_D:
633     isFPCmp = true;
634     Opc = Mips::BC1T;
635     break;
636   case Mips::MOVZ_I:
637   case Mips::MOVZ_S:
638   case Mips::MOVZ_D:
639     Opc = Mips::BNE;
640     break;
641   case Mips::MOVN_I:
642   case Mips::MOVN_S:
643   case Mips::MOVN_D:
644     Opc = Mips::BEQ;
645     break;
646   }
647
648   // There is no need to expand CMov instructions if target has
649   // conditional moves.
650   if (Subtarget->hasCondMov())
651     return BB;
652
653   // To "insert" a SELECT_CC instruction, we actually have to insert the
654   // diamond control-flow pattern.  The incoming instruction knows the
655   // destination vreg to set, the condition code register to branch on, the
656   // true/false values to select between, and a branch opcode to use.
657   const BasicBlock *LLVM_BB = BB->getBasicBlock();
658   MachineFunction::iterator It = BB;
659   ++It;
660
661   //  thisMBB:
662   //  ...
663   //   TrueVal = ...
664   //   setcc r1, r2, r3
665   //   bNE   r1, r0, copy1MBB
666   //   fallthrough --> copy0MBB
667   MachineBasicBlock *thisMBB  = BB;
668   MachineFunction *F = BB->getParent();
669   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
670   MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
671   F->insert(It, copy0MBB);
672   F->insert(It, sinkMBB);
673
674   // Transfer the remainder of BB and its successor edges to sinkMBB.
675   sinkMBB->splice(sinkMBB->begin(), BB,
676                   llvm::next(MachineBasicBlock::iterator(MI)),
677                   BB->end());
678   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
679
680   // Next, add the true and fallthrough blocks as its successors.
681   BB->addSuccessor(copy0MBB);
682   BB->addSuccessor(sinkMBB);
683
684   // Emit the right instruction according to the type of the operands compared
685   if (isFPCmp)
686     BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
687   else
688     BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
689       .addReg(Mips::ZERO).addMBB(sinkMBB);
690
691
692   //  copy0MBB:
693   //   %FalseValue = ...
694   //   # fallthrough to sinkMBB
695   BB = copy0MBB;
696
697   // Update machine-CFG edges
698   BB->addSuccessor(sinkMBB);
699
700   //  sinkMBB:
701   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
702   //  ...
703   BB = sinkMBB;
704
705   if (isFPCmp)
706     BuildMI(*BB, BB->begin(), dl,
707             TII->get(Mips::PHI), MI->getOperand(0).getReg())
708       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
709       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
710   else
711     BuildMI(*BB, BB->begin(), dl,
712             TII->get(Mips::PHI), MI->getOperand(0).getReg())
713       .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
714       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
715
716   MI->eraseFromParent();   // The pseudo instruction is gone now.
717   return BB;
718 }
719
720 // This function also handles Mips::ATOMIC_SWAP_I32 (when BinOpcode == 0), and
721 // Mips::ATOMIC_LOAD_NAND_I32 (when Nand == true)
722 MachineBasicBlock *
723 MipsTargetLowering::EmitAtomicBinary(MachineInstr *MI, MachineBasicBlock *BB,
724                                      unsigned Size, unsigned BinOpcode, 
725                                      bool Nand) const {
726   assert(Size == 4 && "Unsupported size for EmitAtomicBinary.");
727
728   MachineFunction *MF = BB->getParent();
729   MachineRegisterInfo &RegInfo = MF->getRegInfo();
730   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
731   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
732   DebugLoc dl = MI->getDebugLoc();
733
734   unsigned Dest = MI->getOperand(0).getReg();
735   unsigned Ptr = MI->getOperand(1).getReg();
736   unsigned Incr = MI->getOperand(2).getReg();
737
738   unsigned Oldval = RegInfo.createVirtualRegister(RC);
739   unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
740   unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
741
742   // insert new blocks after the current block
743   const BasicBlock *LLVM_BB = BB->getBasicBlock();
744   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
745   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
746   MachineFunction::iterator It = BB;
747   ++It;
748   MF->insert(It, loopMBB);
749   MF->insert(It, exitMBB);
750
751   // Transfer the remainder of BB and its successor edges to exitMBB.
752   exitMBB->splice(exitMBB->begin(), BB,
753                   llvm::next(MachineBasicBlock::iterator(MI)),
754                   BB->end());
755   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
756
757   //  thisMBB:
758   //    ...
759   //    sw incr, fi(sp)           // store incr to stack (when BinOpcode == 0)
760   //    fallthrough --> loopMBB
761
762   // Note: for atomic.swap (when BinOpcode == 0), storing incr to stack before
763   // the loop and then loading it from stack in block loopMBB is necessary to
764   // prevent MachineLICM pass to hoist "or" instruction out of the block
765   // loopMBB.
766
767   int fi = 0;
768   if (BinOpcode == 0 && !Nand) {
769     // Get or create a temporary stack location.
770     MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
771     fi = MipsFI->getAtomicFrameIndex();
772     if (fi == -1) {
773       fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
774       MipsFI->setAtomicFrameIndex(fi);
775     }
776
777     BuildMI(BB, dl, TII->get(Mips::SW))
778         .addReg(Incr).addImm(0).addFrameIndex(fi);
779   }
780   BB->addSuccessor(loopMBB);
781
782   //  loopMBB:
783   //    ll oldval, 0(ptr)
784   //    or dest, $0, oldval
785   //    <binop> tmp1, oldval, incr
786   //    sc tmp1, 0(ptr)
787   //    beq tmp1, $0, loopMBB
788   BB = loopMBB;
789   BuildMI(BB, dl, TII->get(Mips::LL), Oldval).addImm(0).addReg(Ptr);
790   BuildMI(BB, dl, TII->get(Mips::OR), Dest).addReg(Mips::ZERO).addReg(Oldval);
791   if (Nand) {
792     //  and tmp2, oldval, incr
793     //  nor tmp1, $0, tmp2
794     BuildMI(BB, dl, TII->get(Mips::AND), Tmp2).addReg(Oldval).addReg(Incr);
795     BuildMI(BB, dl, TII->get(Mips::NOR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
796   } else if (BinOpcode) {
797     //  <binop> tmp1, oldval, incr
798     BuildMI(BB, dl, TII->get(BinOpcode), Tmp1).addReg(Oldval).addReg(Incr);
799   } else {
800     //  lw tmp2, fi(sp)              // load incr from stack
801     //  or tmp1, $zero, tmp2
802     BuildMI(BB, dl, TII->get(Mips::LW), Tmp2).addImm(0).addFrameIndex(fi);;
803     BuildMI(BB, dl, TII->get(Mips::OR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
804   }
805   BuildMI(BB, dl, TII->get(Mips::SC), Tmp1).addReg(Tmp1).addImm(0).addReg(Ptr);
806   BuildMI(BB, dl, TII->get(Mips::BEQ))
807     .addReg(Tmp1).addReg(Mips::ZERO).addMBB(loopMBB);
808   BB->addSuccessor(loopMBB);
809   BB->addSuccessor(exitMBB);
810
811   MI->eraseFromParent();   // The instruction is gone now.
812
813   return BB;
814 }
815
816 MachineBasicBlock *
817 MipsTargetLowering::EmitAtomicBinaryPartword(MachineInstr *MI,
818                                              MachineBasicBlock *BB,
819                                              unsigned Size, unsigned BinOpcode,
820                                              bool Nand) const {
821   assert((Size == 1 || Size == 2) &&
822       "Unsupported size for EmitAtomicBinaryPartial.");
823
824   MachineFunction *MF = BB->getParent();
825   MachineRegisterInfo &RegInfo = MF->getRegInfo();
826   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
827   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
828   DebugLoc dl = MI->getDebugLoc();
829
830   unsigned Dest = MI->getOperand(0).getReg();
831   unsigned Ptr = MI->getOperand(1).getReg();
832   unsigned Incr = MI->getOperand(2).getReg();
833
834   unsigned Addr = RegInfo.createVirtualRegister(RC);
835   unsigned Shift = RegInfo.createVirtualRegister(RC);
836   unsigned Mask = RegInfo.createVirtualRegister(RC);
837   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
838   unsigned Newval = RegInfo.createVirtualRegister(RC);
839   unsigned Oldval = RegInfo.createVirtualRegister(RC);
840   unsigned Incr2 = RegInfo.createVirtualRegister(RC);
841   unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
842   unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
843   unsigned Tmp3 = RegInfo.createVirtualRegister(RC);
844   unsigned Tmp4 = RegInfo.createVirtualRegister(RC);
845   unsigned Tmp5 = RegInfo.createVirtualRegister(RC);
846   unsigned Tmp6 = RegInfo.createVirtualRegister(RC);
847   unsigned Tmp7 = RegInfo.createVirtualRegister(RC);
848   unsigned Tmp8 = RegInfo.createVirtualRegister(RC);
849   unsigned Tmp9 = RegInfo.createVirtualRegister(RC);
850   unsigned Tmp10 = RegInfo.createVirtualRegister(RC);
851   unsigned Tmp11 = RegInfo.createVirtualRegister(RC);
852   unsigned Tmp12 = RegInfo.createVirtualRegister(RC);
853
854   // insert new blocks after the current block
855   const BasicBlock *LLVM_BB = BB->getBasicBlock();
856   MachineBasicBlock *loopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
857   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
858   MachineFunction::iterator It = BB;
859   ++It;
860   MF->insert(It, loopMBB);
861   MF->insert(It, exitMBB);
862
863   // Transfer the remainder of BB and its successor edges to exitMBB.
864   exitMBB->splice(exitMBB->begin(), BB,
865                   llvm::next(MachineBasicBlock::iterator(MI)),
866                   BB->end());
867   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
868
869   //  thisMBB:
870   //    addiu   tmp1,$0,-4                # 0xfffffffc
871   //    and     addr,ptr,tmp1
872   //    andi    tmp2,ptr,3
873   //    sll     shift,tmp2,3
874   //    ori     tmp3,$0,255               # 0xff
875   //    sll     mask,tmp3,shift
876   //    nor     mask2,$0,mask
877   //    andi    tmp4,incr,255
878   //    sll     incr2,tmp4,shift
879   //    sw      incr2, fi(sp)      // store incr2 to stack (when BinOpcode == 0)
880
881   // Note: for atomic.swap (when BinOpcode == 0), storing incr2 to stack before
882   // the loop and then loading it from stack in block loopMBB is necessary to
883   // prevent MachineLICM pass to hoist "or" instruction out of the block
884   // loopMBB.
885
886   int64_t MaskImm = (Size == 1) ? 255 : 65535;
887   BuildMI(BB, dl, TII->get(Mips::ADDiu), Tmp1).addReg(Mips::ZERO).addImm(-4);
888   BuildMI(BB, dl, TII->get(Mips::AND), Addr).addReg(Ptr).addReg(Tmp1);
889   BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp2).addReg(Ptr).addImm(3);
890   BuildMI(BB, dl, TII->get(Mips::SLL), Shift).addReg(Tmp2).addImm(3);
891   BuildMI(BB, dl, TII->get(Mips::ORi), Tmp3).addReg(Mips::ZERO).addImm(MaskImm);
892   BuildMI(BB, dl, TII->get(Mips::SLL), Mask).addReg(Tmp3).addReg(Shift);
893   BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
894   if (BinOpcode != Mips::SUBu) {
895     BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp4).addReg(Incr).addImm(MaskImm);
896     BuildMI(BB, dl, TII->get(Mips::SLL), Incr2).addReg(Tmp4).addReg(Shift);
897   } else {
898     BuildMI(BB, dl, TII->get(Mips::SUBu), Tmp4).addReg(Mips::ZERO).addReg(Incr);
899     BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp5).addReg(Tmp4).addImm(MaskImm);
900     BuildMI(BB, dl, TII->get(Mips::SLL), Incr2).addReg(Tmp5).addReg(Shift);
901   }
902
903   int fi = 0;
904   if (BinOpcode == 0 && !Nand) {
905     // Get or create a temporary stack location.
906     MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
907     fi = MipsFI->getAtomicFrameIndex();
908     if (fi == -1) {
909       fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
910       MipsFI->setAtomicFrameIndex(fi);
911     }
912
913     BuildMI(BB, dl, TII->get(Mips::SW))
914         .addReg(Incr2).addImm(0).addFrameIndex(fi);
915   }
916   BB->addSuccessor(loopMBB);
917
918   // loopMBB:
919   //   ll      oldval,0(addr)
920   //   binop   tmp7,oldval,incr2
921   //   and     newval,tmp7,mask
922   //   and     tmp8,oldval,mask2
923   //   or      tmp9,tmp8,newval
924   //   sc      tmp9,0(addr)
925   //   beq     tmp9,$0,loopMBB
926   BB = loopMBB;
927   BuildMI(BB, dl, TII->get(Mips::LL), Oldval).addImm(0).addReg(Addr);
928   if (Nand) {
929     //  and tmp6, oldval, incr2
930     //  nor tmp7, $0, tmp6
931     BuildMI(BB, dl, TII->get(Mips::AND), Tmp6).addReg(Oldval).addReg(Incr2);
932     BuildMI(BB, dl, TII->get(Mips::NOR), Tmp7).addReg(Mips::ZERO).addReg(Tmp6);
933   } else if (BinOpcode == Mips::SUBu) {
934     //  addu tmp7, oldval, incr2
935     BuildMI(BB, dl, TII->get(Mips::ADDu), Tmp7).addReg(Oldval).addReg(Incr2);
936   } else if (BinOpcode) {
937     //  <binop> tmp7, oldval, incr2
938     BuildMI(BB, dl, TII->get(BinOpcode), Tmp7).addReg(Oldval).addReg(Incr2);
939   } else {
940     //  lw tmp6, fi(sp)              // load incr2 from stack
941     //  or tmp7, $zero, tmp6
942     BuildMI(BB, dl, TII->get(Mips::LW), Tmp6).addImm(0).addFrameIndex(fi);;
943     BuildMI(BB, dl, TII->get(Mips::OR), Tmp7).addReg(Mips::ZERO).addReg(Tmp6);
944   }
945   BuildMI(BB, dl, TII->get(Mips::AND), Newval).addReg(Tmp7).addReg(Mask);
946   BuildMI(BB, dl, TII->get(Mips::AND), Tmp8).addReg(Oldval).addReg(Mask2);
947   BuildMI(BB, dl, TII->get(Mips::OR), Tmp9).addReg(Tmp8).addReg(Newval);
948   BuildMI(BB, dl, TII->get(Mips::SC), Tmp9).addReg(Tmp9).addImm(0).addReg(Addr);
949   BuildMI(BB, dl, TII->get(Mips::BEQ))
950       .addReg(Tmp9).addReg(Mips::ZERO).addMBB(loopMBB);
951   BB->addSuccessor(loopMBB);
952   BB->addSuccessor(exitMBB);
953
954   //  exitMBB:
955   //    and     tmp10,oldval,mask
956   //    srl     tmp11,tmp10,shift
957   //    sll     tmp12,tmp11,24
958   //    sra     dest,tmp12,24
959   BB = exitMBB;
960   int64_t ShiftImm = (Size == 1) ? 24 : 16;
961   // reverse order
962   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRA), Dest)
963       .addReg(Tmp12).addImm(ShiftImm);
964   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SLL), Tmp12)
965       .addReg(Tmp11).addImm(ShiftImm);
966   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRL), Tmp11)
967       .addReg(Tmp10).addReg(Shift);
968   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::AND), Tmp10)
969     .addReg(Oldval).addReg(Mask);
970
971   MI->eraseFromParent();   // The instruction is gone now.
972
973   return BB;
974 }
975
976 MachineBasicBlock *
977 MipsTargetLowering::EmitAtomicCmpSwap(MachineInstr *MI,
978                                       MachineBasicBlock *BB,
979                                       unsigned Size) const {
980   assert(Size == 4 && "Unsupported size for EmitAtomicCmpSwap.");
981
982   MachineFunction *MF = BB->getParent();
983   MachineRegisterInfo &RegInfo = MF->getRegInfo();
984   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
985   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
986   DebugLoc dl = MI->getDebugLoc();
987
988   unsigned Dest    = MI->getOperand(0).getReg();
989   unsigned Ptr     = MI->getOperand(1).getReg();
990   unsigned Oldval  = MI->getOperand(2).getReg();
991   unsigned Newval  = MI->getOperand(3).getReg();
992
993   unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
994   unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
995
996   // insert new blocks after the current block
997   const BasicBlock *LLVM_BB = BB->getBasicBlock();
998   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
999   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1000   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1001   MachineFunction::iterator It = BB;
1002   ++It;
1003   MF->insert(It, loop1MBB);
1004   MF->insert(It, loop2MBB);
1005   MF->insert(It, exitMBB);
1006
1007   // Transfer the remainder of BB and its successor edges to exitMBB.
1008   exitMBB->splice(exitMBB->begin(), BB,
1009                   llvm::next(MachineBasicBlock::iterator(MI)),
1010                   BB->end());
1011   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1012
1013   // Get or create a temporary stack location.
1014   MipsFunctionInfo *MipsFI = MF->getInfo<MipsFunctionInfo>();
1015   int fi = MipsFI->getAtomicFrameIndex();
1016   if (fi == -1) {
1017     fi = MF->getFrameInfo()->CreateStackObject(Size, Size, false);
1018     MipsFI->setAtomicFrameIndex(fi);
1019   }
1020
1021   //  thisMBB:
1022   //    ...
1023   //    sw newval, fi(sp)           // store newval to stack
1024   //    fallthrough --> loop1MBB
1025
1026   // Note: storing newval to stack before the loop and then loading it from
1027   // stack in block loop2MBB is necessary to prevent MachineLICM pass to
1028   // hoist "or" instruction out of the block loop2MBB.
1029
1030   BuildMI(BB, dl, TII->get(Mips::SW))
1031       .addReg(Newval).addImm(0).addFrameIndex(fi);
1032   BB->addSuccessor(loop1MBB);
1033
1034   // loop1MBB:
1035   //   ll dest, 0(ptr)
1036   //   bne dest, oldval, exitMBB
1037   BB = loop1MBB;
1038   BuildMI(BB, dl, TII->get(Mips::LL), Dest).addImm(0).addReg(Ptr);
1039   BuildMI(BB, dl, TII->get(Mips::BNE))
1040     .addReg(Dest).addReg(Oldval).addMBB(exitMBB);
1041   BB->addSuccessor(exitMBB);
1042   BB->addSuccessor(loop2MBB);
1043
1044   // loop2MBB:
1045   //   lw tmp2, fi(sp)              // load newval from stack
1046   //   or tmp1, $0, tmp2
1047   //   sc tmp1, 0(ptr)
1048   //   beq tmp1, $0, loop1MBB
1049   BB = loop2MBB;
1050   BuildMI(BB, dl, TII->get(Mips::LW), Tmp2).addImm(0).addFrameIndex(fi);;
1051   BuildMI(BB, dl, TII->get(Mips::OR), Tmp1).addReg(Mips::ZERO).addReg(Tmp2);
1052   BuildMI(BB, dl, TII->get(Mips::SC), Tmp1).addReg(Tmp1).addImm(0).addReg(Ptr);
1053   BuildMI(BB, dl, TII->get(Mips::BEQ))
1054     .addReg(Tmp1).addReg(Mips::ZERO).addMBB(loop1MBB);
1055   BB->addSuccessor(loop1MBB);
1056   BB->addSuccessor(exitMBB);
1057
1058   MI->eraseFromParent();   // The instruction is gone now.
1059
1060   return BB;
1061 }
1062
1063 MachineBasicBlock *
1064 MipsTargetLowering::EmitAtomicCmpSwapPartword(MachineInstr *MI,
1065                                               MachineBasicBlock *BB,
1066                                               unsigned Size) const {
1067   assert((Size == 1 || Size == 2) &&
1068       "Unsupported size for EmitAtomicCmpSwapPartial.");
1069
1070   MachineFunction *MF = BB->getParent();
1071   MachineRegisterInfo &RegInfo = MF->getRegInfo();
1072   const TargetRegisterClass *RC = getRegClassFor(MVT::i32);
1073   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
1074   DebugLoc dl = MI->getDebugLoc();
1075
1076   unsigned Dest    = MI->getOperand(0).getReg();
1077   unsigned Ptr     = MI->getOperand(1).getReg();
1078   unsigned Oldval  = MI->getOperand(2).getReg();
1079   unsigned Newval  = MI->getOperand(3).getReg();
1080
1081   unsigned Addr = RegInfo.createVirtualRegister(RC);
1082   unsigned Shift = RegInfo.createVirtualRegister(RC);
1083   unsigned Mask = RegInfo.createVirtualRegister(RC);
1084   unsigned Mask2 = RegInfo.createVirtualRegister(RC);
1085   unsigned Oldval2 = RegInfo.createVirtualRegister(RC);
1086   unsigned Oldval3 = RegInfo.createVirtualRegister(RC);
1087   unsigned Oldval4 = RegInfo.createVirtualRegister(RC);
1088   unsigned Newval2 = RegInfo.createVirtualRegister(RC);
1089   unsigned Tmp1 = RegInfo.createVirtualRegister(RC);
1090   unsigned Tmp2 = RegInfo.createVirtualRegister(RC);
1091   unsigned Tmp3 = RegInfo.createVirtualRegister(RC);
1092   unsigned Tmp4 = RegInfo.createVirtualRegister(RC);
1093   unsigned Tmp5 = RegInfo.createVirtualRegister(RC);
1094   unsigned Tmp6 = RegInfo.createVirtualRegister(RC);
1095   unsigned Tmp7 = RegInfo.createVirtualRegister(RC);
1096   unsigned Tmp8 = RegInfo.createVirtualRegister(RC);
1097   unsigned Tmp9 = RegInfo.createVirtualRegister(RC);
1098
1099   // insert new blocks after the current block
1100   const BasicBlock *LLVM_BB = BB->getBasicBlock();
1101   MachineBasicBlock *loop1MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1102   MachineBasicBlock *loop2MBB = MF->CreateMachineBasicBlock(LLVM_BB);
1103   MachineBasicBlock *exitMBB = MF->CreateMachineBasicBlock(LLVM_BB);
1104   MachineFunction::iterator It = BB;
1105   ++It;
1106   MF->insert(It, loop1MBB);
1107   MF->insert(It, loop2MBB);
1108   MF->insert(It, exitMBB);
1109
1110   // Transfer the remainder of BB and its successor edges to exitMBB.
1111   exitMBB->splice(exitMBB->begin(), BB,
1112                   llvm::next(MachineBasicBlock::iterator(MI)),
1113                   BB->end());
1114   exitMBB->transferSuccessorsAndUpdatePHIs(BB);
1115
1116   //  thisMBB:
1117   //    addiu   tmp1,$0,-4                # 0xfffffffc
1118   //    and     addr,ptr,tmp1
1119   //    andi    tmp2,ptr,3
1120   //    sll     shift,tmp2,3
1121   //    ori     tmp3,$0,255               # 0xff
1122   //    sll     mask,tmp3,shift
1123   //    nor     mask2,$0,mask
1124   //    andi    tmp4,oldval,255
1125   //    sll     oldval2,tmp4,shift
1126   //    andi    tmp5,newval,255
1127   //    sll     newval2,tmp5,shift
1128   int64_t MaskImm = (Size == 1) ? 255 : 65535;
1129   BuildMI(BB, dl, TII->get(Mips::ADDiu), Tmp1).addReg(Mips::ZERO).addImm(-4);
1130   BuildMI(BB, dl, TII->get(Mips::AND), Addr).addReg(Ptr).addReg(Tmp1);
1131   BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp2).addReg(Ptr).addImm(3);
1132   BuildMI(BB, dl, TII->get(Mips::SLL), Shift).addReg(Tmp2).addImm(3);
1133   BuildMI(BB, dl, TII->get(Mips::ORi), Tmp3).addReg(Mips::ZERO).addImm(MaskImm);
1134   BuildMI(BB, dl, TII->get(Mips::SLL), Mask).addReg(Tmp3).addReg(Shift);
1135   BuildMI(BB, dl, TII->get(Mips::NOR), Mask2).addReg(Mips::ZERO).addReg(Mask);
1136   BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp4).addReg(Oldval).addImm(MaskImm);
1137   BuildMI(BB, dl, TII->get(Mips::SLL), Oldval2).addReg(Tmp4).addReg(Shift);
1138   BuildMI(BB, dl, TII->get(Mips::ANDi), Tmp5).addReg(Newval).addImm(MaskImm);
1139   BuildMI(BB, dl, TII->get(Mips::SLL), Newval2).addReg(Tmp5).addReg(Shift);
1140   BB->addSuccessor(loop1MBB);
1141
1142   //  loop1MBB:
1143   //    ll      oldval3,0(addr)
1144   //    and     oldval4,oldval3,mask
1145   //    bne     oldval4,oldval2,exitMBB
1146   BB = loop1MBB;
1147   BuildMI(BB, dl, TII->get(Mips::LL), Oldval3).addImm(0).addReg(Addr);
1148   BuildMI(BB, dl, TII->get(Mips::AND), Oldval4).addReg(Oldval3).addReg(Mask);
1149   BuildMI(BB, dl, TII->get(Mips::BNE))
1150       .addReg(Oldval4).addReg(Oldval2).addMBB(exitMBB);
1151   BB->addSuccessor(exitMBB);
1152   BB->addSuccessor(loop2MBB);
1153
1154   //  loop2MBB:
1155   //    and     tmp6,oldval3,mask2
1156   //    or      tmp7,tmp6,newval2
1157   //    sc      tmp7,0(addr)
1158   //    beq     tmp7,$0,loop1MBB
1159   BB = loop2MBB;
1160   BuildMI(BB, dl, TII->get(Mips::AND), Tmp6).addReg(Oldval3).addReg(Mask2);
1161   BuildMI(BB, dl, TII->get(Mips::OR), Tmp7).addReg(Tmp6).addReg(Newval2);
1162   BuildMI(BB, dl, TII->get(Mips::SC), Tmp7)
1163       .addReg(Tmp7).addImm(0).addReg(Addr);
1164   BuildMI(BB, dl, TII->get(Mips::BEQ))
1165       .addReg(Tmp7).addReg(Mips::ZERO).addMBB(loop1MBB);
1166   BB->addSuccessor(loop1MBB);
1167   BB->addSuccessor(exitMBB);
1168
1169   //  exitMBB:
1170   //    srl     tmp8,oldval4,shift
1171   //    sll     tmp9,tmp8,24
1172   //    sra     dest,tmp9,24
1173   BB = exitMBB;
1174   int64_t ShiftImm = (Size == 1) ? 24 : 16;
1175   // reverse order
1176   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRA), Dest)
1177       .addReg(Tmp9).addImm(ShiftImm);
1178   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SLL), Tmp9)
1179       .addReg(Tmp8).addImm(ShiftImm);
1180   BuildMI(*BB, BB->begin(), dl, TII->get(Mips::SRL), Tmp8)
1181       .addReg(Oldval4).addReg(Shift);
1182
1183   MI->eraseFromParent();   // The instruction is gone now.
1184
1185   return BB;
1186 }
1187
1188 //===----------------------------------------------------------------------===//
1189 //  Misc Lower Operation implementation
1190 //===----------------------------------------------------------------------===//
1191 SDValue MipsTargetLowering::
1192 LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
1193 {
1194   unsigned StackAlignment =
1195     getTargetMachine().getFrameLowering()->getStackAlignment();
1196   assert(StackAlignment >=
1197          cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
1198          "Cannot lower if the alignment of the allocated space is larger than \
1199           that of the stack.");
1200
1201   SDValue Chain = Op.getOperand(0);
1202   SDValue Size = Op.getOperand(1);
1203   DebugLoc dl = Op.getDebugLoc();
1204
1205   // Get a reference from Mips stack pointer
1206   SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
1207
1208   // Subtract the dynamic size from the actual stack size to
1209   // obtain the new stack size.
1210   SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
1211
1212   // The Sub result contains the new stack start address, so it
1213   // must be placed in the stack pointer register.
1214   Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
1215                            SDValue());
1216   // Retrieve updated $sp. There is a glue input to prevent instructions that
1217   // clobber $sp from being inserted between copytoreg and copyfromreg.
1218   SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
1219                                      Chain.getValue(1));
1220   
1221   // The stack space reserved by alloca is located right above the argument
1222   // area. It is aligned on a boundary that is a multiple of StackAlignment.
1223   MachineFunction &MF = DAG.getMachineFunction();
1224   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1225   unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
1226                       StackAlignment * StackAlignment;
1227   SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1228                                  DAG.getConstant(SPOffset, MVT::i32));
1229
1230   // This node always has two return values: a new stack pointer
1231   // value and a chain
1232   SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
1233   return DAG.getMergeValues(Ops, 2, dl);
1234 }
1235
1236 SDValue MipsTargetLowering::
1237 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
1238 {
1239   // The first operand is the chain, the second is the condition, the third is
1240   // the block to branch to if the condition is true.
1241   SDValue Chain = Op.getOperand(0);
1242   SDValue Dest = Op.getOperand(2);
1243   DebugLoc dl = Op.getDebugLoc();
1244
1245   SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
1246
1247   // Return if flag is not set by a floating point comparison.
1248   if (CondRes.getOpcode() != MipsISD::FPCmp)
1249     return Op;
1250
1251   SDValue CCNode  = CondRes.getOperand(2);
1252   Mips::CondCode CC =
1253     (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
1254   SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
1255
1256   return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
1257                      Dest, CondRes);
1258 }
1259
1260 SDValue MipsTargetLowering::
1261 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
1262 {
1263   SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
1264
1265   // Return if flag is not set by a floating point comparison.
1266   if (Cond.getOpcode() != MipsISD::FPCmp)
1267     return Op;
1268
1269   return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
1270                       Op.getDebugLoc());
1271 }
1272
1273 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
1274                                                SelectionDAG &DAG) const {
1275   // FIXME there isn't actually debug info here
1276   DebugLoc dl = Op.getDebugLoc();
1277   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1278
1279   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1280     SDVTList VTs = DAG.getVTList(MVT::i32);
1281
1282     MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
1283
1284     // %gp_rel relocation
1285     if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
1286       SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1287                                               MipsII::MO_GPREL);
1288       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
1289       SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1290       return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
1291     }
1292     // %hi/%lo relocation
1293     SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1294                                               MipsII::MO_ABS_HI);
1295     SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1296                                               MipsII::MO_ABS_LO);
1297     SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
1298     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1299     return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1300   }
1301
1302   SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1303                                           MipsII::MO_GOT);
1304   GA = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, GA);
1305   SDValue ResNode = DAG.getLoad(MVT::i32, dl,
1306                                 DAG.getEntryNode(), GA, MachinePointerInfo(),
1307                                 false, false, 0);
1308   // On functions and global targets not internal linked only
1309   // a load from got/GP is necessary for PIC to work.
1310   if (!GV->hasInternalLinkage() &&
1311       (!GV->hasLocalLinkage() || isa<Function>(GV)))
1312     return ResNode;
1313   SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1314                                             MipsII::MO_ABS_LO);
1315   SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
1316   return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
1317 }
1318
1319 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
1320                                               SelectionDAG &DAG) const {
1321   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
1322   // FIXME there isn't actually debug info here
1323   DebugLoc dl = Op.getDebugLoc();
1324
1325   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1326     // %hi/%lo relocation
1327     SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
1328                                        MipsII::MO_ABS_HI);
1329     SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
1330                                        MipsII::MO_ABS_LO);
1331     SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
1332     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
1333     return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1334   }
1335
1336   SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1337                                             MipsII::MO_GOT);
1338   BAGOTOffset = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, BAGOTOffset);
1339   SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
1340                                            MipsII::MO_ABS_LO);
1341   SDValue Load = DAG.getLoad(MVT::i32, dl,
1342                              DAG.getEntryNode(), BAGOTOffset,
1343                              MachinePointerInfo(), false, false, 0);
1344   SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
1345   return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1346 }
1347
1348 SDValue MipsTargetLowering::
1349 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
1350 {
1351   // If the relocation model is PIC, use the General Dynamic TLS Model,
1352   // otherwise use the Initial Exec or Local Exec TLS Model.
1353   // TODO: implement Local Dynamic TLS model
1354
1355   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1356   DebugLoc dl = GA->getDebugLoc();
1357   const GlobalValue *GV = GA->getGlobal();
1358   EVT PtrVT = getPointerTy();
1359
1360   if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1361     // General Dynamic TLS Model
1362     SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32,
1363                                                  0, MipsII::MO_TLSGD);
1364     SDValue Tlsgd = DAG.getNode(MipsISD::TlsGd, dl, MVT::i32, TGA);
1365     SDValue GP = DAG.getRegister(Mips::GP, MVT::i32);
1366     SDValue Argument = DAG.getNode(ISD::ADD, dl, MVT::i32, GP, Tlsgd);
1367
1368     ArgListTy Args;
1369     ArgListEntry Entry;
1370     Entry.Node = Argument;
1371     Entry.Ty = (const Type *) Type::getInt32Ty(*DAG.getContext());
1372     Args.push_back(Entry);
1373     std::pair<SDValue, SDValue> CallResult =
1374         LowerCallTo(DAG.getEntryNode(),
1375                  (const Type *) Type::getInt32Ty(*DAG.getContext()),
1376                  false, false, false, false,
1377                  0, CallingConv::C, false, true,
1378                  DAG.getExternalSymbol("__tls_get_addr", PtrVT), Args, DAG, dl);
1379
1380     return CallResult.first;
1381   } else {
1382     SDValue Offset;
1383     if (GV->isDeclaration()) {
1384       // Initial Exec TLS Model
1385       SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1386                                               MipsII::MO_GOTTPREL);
1387       Offset = DAG.getLoad(MVT::i32, dl,
1388                                   DAG.getEntryNode(), TGA, MachinePointerInfo(),
1389                                   false, false, 0);
1390     } else {
1391       // Local Exec TLS Model
1392       SDVTList VTs = DAG.getVTList(MVT::i32);
1393       SDValue TGAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1394                                               MipsII::MO_TPREL_HI);
1395       SDValue TGALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
1396                                               MipsII::MO_TPREL_LO);
1397       SDValue Hi = DAG.getNode(MipsISD::TprelHi, dl, VTs, &TGAHi, 1);
1398       SDValue Lo = DAG.getNode(MipsISD::TprelLo, dl, MVT::i32, TGALo);
1399       Offset = DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
1400     }
1401
1402     SDValue ThreadPointer = DAG.getNode(MipsISD::ThreadPointer, dl, PtrVT);
1403     return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
1404   }
1405 }
1406
1407 SDValue MipsTargetLowering::
1408 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
1409 {
1410   SDValue ResNode;
1411   SDValue HiPart;
1412   // FIXME there isn't actually debug info here
1413   DebugLoc dl = Op.getDebugLoc();
1414   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1415   unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
1416
1417   EVT PtrVT = Op.getValueType();
1418   JumpTableSDNode *JT  = cast<JumpTableSDNode>(Op);
1419
1420   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
1421
1422   if (!IsPIC) {
1423     SDValue Ops[] = { JTI };
1424     HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
1425   } else {// Emit Load from Global Pointer
1426     JTI = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, JTI);
1427     HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
1428                          MachinePointerInfo(),
1429                          false, false, 0);
1430   }
1431
1432   SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
1433                                          MipsII::MO_ABS_LO);
1434   SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
1435   ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1436
1437   return ResNode;
1438 }
1439
1440 SDValue MipsTargetLowering::
1441 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
1442 {
1443   SDValue ResNode;
1444   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1445   const Constant *C = N->getConstVal();
1446   // FIXME there isn't actually debug info here
1447   DebugLoc dl = Op.getDebugLoc();
1448
1449   // gp_rel relocation
1450   // FIXME: we should reference the constant pool using small data sections,
1451   // but the asm printer currently doesn't support this feature without
1452   // hacking it. This feature should come soon so we can uncomment the
1453   // stuff below.
1454   //if (IsInSmallSection(C->getType())) {
1455   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
1456   //  SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
1457   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
1458
1459   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
1460     SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1461                                              N->getOffset(), MipsII::MO_ABS_HI);
1462     SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1463                                              N->getOffset(), MipsII::MO_ABS_LO);
1464     SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
1465     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
1466     ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
1467   } else {
1468     SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1469                                            N->getOffset(), MipsII::MO_GOT);
1470     CP = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, CP);
1471     SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
1472                                CP, MachinePointerInfo::getConstantPool(),
1473                                false, false, 0);
1474     SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
1475                                              N->getOffset(), MipsII::MO_ABS_LO);
1476     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
1477     ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
1478   }
1479
1480   return ResNode;
1481 }
1482
1483 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
1484   MachineFunction &MF = DAG.getMachineFunction();
1485   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
1486
1487   DebugLoc dl = Op.getDebugLoc();
1488   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
1489                                  getPointerTy());
1490
1491   // vastart just stores the address of the VarArgsFrameIndex slot into the
1492   // memory location argument.
1493   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1494   return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
1495                       MachinePointerInfo(SV),
1496                       false, false, 0);
1497 }
1498
1499 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
1500   // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
1501   DebugLoc dl = Op.getDebugLoc();
1502   SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
1503   SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
1504   SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
1505                              DAG.getConstant(0x7fffffff, MVT::i32));
1506   SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
1507                              DAG.getConstant(0x80000000, MVT::i32));
1508   SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1509   return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
1510 }
1511
1512 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
1513   // FIXME: 
1514   //  Use ext/ins instructions if target architecture is Mips32r2.
1515   //  Eliminate redundant mfc1 and mtc1 instructions.
1516   unsigned LoIdx = 0, HiIdx = 1;
1517   
1518   if (!isLittle)
1519     std::swap(LoIdx, HiIdx);
1520
1521   DebugLoc dl = Op.getDebugLoc();
1522   SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1523                               Op.getOperand(0),
1524                               DAG.getConstant(LoIdx, MVT::i32));
1525   SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1526                             Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
1527   SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1528                             Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
1529   SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
1530                              DAG.getConstant(0x7fffffff, MVT::i32));
1531   SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
1532                              DAG.getConstant(0x80000000, MVT::i32));
1533   SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
1534
1535   if (!isLittle)
1536     std::swap(Word0, Word1);
1537
1538   return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
1539 }
1540
1541 SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
1542   const {
1543   EVT Ty = Op.getValueType();
1544
1545   assert(Ty == MVT::f32 || Ty == MVT::f64);
1546
1547   if (Ty == MVT::f32)
1548     return LowerFCOPYSIGN32(Op, DAG);
1549   else
1550     return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
1551 }
1552
1553 SDValue MipsTargetLowering::
1554 LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
1555   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1556   assert((Depth == 0) &&
1557          "Frame address can only be determined for current frame.");
1558
1559   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1560   MFI->setFrameAddressIsTaken(true);
1561   EVT VT = Op.getValueType();
1562   DebugLoc dl = Op.getDebugLoc();
1563   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, Mips::FP, VT);
1564   return FrameAddr;
1565 }
1566
1567 //===----------------------------------------------------------------------===//
1568 //                      Calling Convention Implementation
1569 //===----------------------------------------------------------------------===//
1570
1571 #include "MipsGenCallingConv.inc"
1572
1573 //===----------------------------------------------------------------------===//
1574 // TODO: Implement a generic logic using tblgen that can support this.
1575 // Mips O32 ABI rules:
1576 // ---
1577 // i32 - Passed in A0, A1, A2, A3 and stack
1578 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
1579 //       an argument. Otherwise, passed in A1, A2, A3 and stack.
1580 // f64 - Only passed in two aliased f32 registers if no int reg has been used
1581 //       yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
1582 //       not used, it must be shadowed. If only A3 is avaiable, shadow it and
1583 //       go to stack.
1584 //
1585 //  For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
1586 //===----------------------------------------------------------------------===//
1587
1588 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
1589                        MVT LocVT, CCValAssign::LocInfo LocInfo,
1590                        ISD::ArgFlagsTy ArgFlags, CCState &State) {
1591
1592   static const unsigned IntRegsSize=4, FloatRegsSize=2;
1593
1594   static const unsigned IntRegs[] = {
1595       Mips::A0, Mips::A1, Mips::A2, Mips::A3
1596   };
1597   static const unsigned F32Regs[] = {
1598       Mips::F12, Mips::F14
1599   };
1600   static const unsigned F64Regs[] = {
1601       Mips::D6, Mips::D7
1602   };
1603
1604   // ByVal Args
1605   if (ArgFlags.isByVal()) {
1606     State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1607                       1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1608     unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1609     for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1610          r < std::min(IntRegsSize, NextReg); ++r)
1611       State.AllocateReg(IntRegs[r]);
1612     return false;
1613   }
1614
1615   // Promote i8 and i16
1616   if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1617     LocVT = MVT::i32;
1618     if (ArgFlags.isSExt())
1619       LocInfo = CCValAssign::SExt;
1620     else if (ArgFlags.isZExt())
1621       LocInfo = CCValAssign::ZExt;
1622     else
1623       LocInfo = CCValAssign::AExt;
1624   }
1625
1626   unsigned Reg;
1627
1628   // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1629   // is true: function is vararg, argument is 3rd or higher, there is previous
1630   // argument which is not f32 or f64.
1631   bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1632       || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
1633   unsigned OrigAlign = ArgFlags.getOrigAlign();
1634   bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
1635
1636   if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
1637     Reg = State.AllocateReg(IntRegs, IntRegsSize);
1638     // If this is the first part of an i64 arg,
1639     // the allocated register must be either A0 or A2.
1640     if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1641       Reg = State.AllocateReg(IntRegs, IntRegsSize);
1642     LocVT = MVT::i32;
1643   } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1644     // Allocate int register and shadow next int register. If first
1645     // available register is Mips::A1 or Mips::A3, shadow it too.
1646     Reg = State.AllocateReg(IntRegs, IntRegsSize);
1647     if (Reg == Mips::A1 || Reg == Mips::A3)
1648       Reg = State.AllocateReg(IntRegs, IntRegsSize);
1649     State.AllocateReg(IntRegs, IntRegsSize);
1650     LocVT = MVT::i32;
1651   } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1652     // we are guaranteed to find an available float register
1653     if (ValVT == MVT::f32) {
1654       Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1655       // Shadow int register
1656       State.AllocateReg(IntRegs, IntRegsSize);
1657     } else {
1658       Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1659       // Shadow int registers
1660       unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1661       if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1662         State.AllocateReg(IntRegs, IntRegsSize);
1663       State.AllocateReg(IntRegs, IntRegsSize);
1664     }
1665   } else
1666     llvm_unreachable("Cannot handle this ValVT.");
1667
1668   unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1669   unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1670
1671   if (!Reg)
1672     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1673   else
1674     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1675
1676   return false; // CC must always match
1677 }
1678
1679 //===----------------------------------------------------------------------===//
1680 //                  Call Calling Convention Implementation
1681 //===----------------------------------------------------------------------===//
1682
1683 static const unsigned O32IntRegsSize = 4;
1684
1685 static const unsigned O32IntRegs[] = {
1686   Mips::A0, Mips::A1, Mips::A2, Mips::A3
1687 };
1688
1689 // Write ByVal Arg to arg registers and stack.
1690 static void
1691 WriteByValArg(SDValue& Chain, DebugLoc dl,
1692               SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1693               SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1694               MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
1695               const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1696               MVT PtrType) {
1697   unsigned FirstWord = VA.getLocMemOffset() / 4;
1698   unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1699   unsigned LastWord = FirstWord + NumWords;
1700   unsigned CurWord;
1701
1702   // copy the first 4 words of byval arg to registers A0 - A3
1703   for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1704        ++CurWord) {
1705     SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1706                                   DAG.getConstant((CurWord - FirstWord) * 4,
1707                                                   MVT::i32));
1708     SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1709                                   MachinePointerInfo(),
1710                                   false, false, 0);
1711     MemOpChains.push_back(LoadVal.getValue(1));
1712     unsigned DstReg = O32IntRegs[CurWord];
1713     RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1714   }
1715
1716   // copy remaining part of byval arg to stack.
1717   if (CurWord < LastWord) {
1718     unsigned SizeInBytes = (LastWord - CurWord) * 4; 
1719     SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1720                               DAG.getConstant((CurWord - FirstWord) * 4,
1721                                               MVT::i32));
1722     LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1723     SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1724     Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1725                           DAG.getConstant(SizeInBytes, MVT::i32),
1726                           /*Align*/4,
1727                           /*isVolatile=*/false, /*AlwaysInline=*/false,
1728                           MachinePointerInfo(0), MachinePointerInfo(0));
1729     MemOpChains.push_back(Chain);
1730   }
1731 }
1732
1733 /// LowerCall - functions arguments are copied from virtual regs to
1734 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
1735 /// TODO: isTailCall.
1736 SDValue
1737 MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1738                               CallingConv::ID CallConv, bool isVarArg,
1739                               bool &isTailCall,
1740                               const SmallVectorImpl<ISD::OutputArg> &Outs,
1741                               const SmallVectorImpl<SDValue> &OutVals,
1742                               const SmallVectorImpl<ISD::InputArg> &Ins,
1743                               DebugLoc dl, SelectionDAG &DAG,
1744                               SmallVectorImpl<SDValue> &InVals) const {
1745   // MIPs target does not yet support tail call optimization.
1746   isTailCall = false;
1747
1748   MachineFunction &MF = DAG.getMachineFunction();
1749   MachineFrameInfo *MFI = MF.getFrameInfo();
1750   const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
1751   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1752   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1753
1754   // Analyze operands of the call, assigning locations to each operand.
1755   SmallVector<CCValAssign, 16> ArgLocs;
1756   CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1757                  *DAG.getContext());
1758
1759   if (Subtarget->isABI_O32())
1760     CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
1761   else
1762     CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
1763
1764   // Get a count of how many bytes are to be pushed on the stack.
1765   unsigned NumBytes = CCInfo.getNextStackOffset();
1766   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
1767
1768   // With EABI is it possible to have 16 args on registers.
1769   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1770   SmallVector<SDValue, 8> MemOpChains;
1771
1772   // If this is the first call, create a stack frame object that points to
1773   // a location to which .cprestore saves $gp. The offset of this frame object
1774   // is set to 0, since we know nothing about the size of the argument area at
1775   // this point.
1776   if (IsPIC && !MipsFI->getGPFI())
1777     MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1778
1779   int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0; 
1780
1781   // Walk the register/memloc assignments, inserting copies/loads.
1782   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1783     SDValue Arg = OutVals[i];
1784     CCValAssign &VA = ArgLocs[i];
1785
1786     // Promote the value if needed.
1787     switch (VA.getLocInfo()) {
1788     default: llvm_unreachable("Unknown loc info!");
1789     case CCValAssign::Full:
1790       if (Subtarget->isABI_O32() && VA.isRegLoc()) {
1791         if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
1792           Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
1793         if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
1794           SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1795                                    Arg, DAG.getConstant(0, MVT::i32));
1796           SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1797                                    Arg, DAG.getConstant(1, MVT::i32));
1798           if (!Subtarget->isLittle())
1799             std::swap(Lo, Hi);
1800           RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1801           RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1802           continue;
1803         }
1804       }
1805       break;
1806     case CCValAssign::SExt:
1807       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1808       break;
1809     case CCValAssign::ZExt:
1810       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1811       break;
1812     case CCValAssign::AExt:
1813       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1814       break;
1815     }
1816
1817     // Arguments that can be passed on register must be kept at
1818     // RegsToPass vector
1819     if (VA.isRegLoc()) {
1820       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1821       continue;
1822     }
1823
1824     // Register can't get to this point...
1825     assert(VA.isMemLoc());
1826
1827     // ByVal Arg. 
1828     ISD::ArgFlagsTy Flags = Outs[i].Flags;
1829     if (Flags.isByVal()) {
1830       assert(Subtarget->isABI_O32() &&
1831              "No support for ByVal args by ABIs other than O32 yet.");
1832       assert(Flags.getByValSize() &&
1833              "ByVal args of size 0 should have been ignored by front-end.");
1834       WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1835                     VA, Flags, getPointerTy());
1836       continue;
1837     }
1838
1839     // Create the frame index object for this incoming parameter
1840     LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, 
1841                                     VA.getLocMemOffset(), true);
1842     SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1843
1844     // emit ISD::STORE whichs stores the
1845     // parameter value to a stack Location
1846     MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1847                                        MachinePointerInfo(),
1848                                        false, false, 0));
1849   }
1850
1851   // Transform all store nodes into one single node because all store
1852   // nodes are independent of each other.
1853   if (!MemOpChains.empty())
1854     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1855                         &MemOpChains[0], MemOpChains.size());
1856
1857   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1858   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1859   // node so that legalize doesn't hack it.
1860   unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
1861   bool LoadSymAddr = false;
1862   SDValue CalleeLo;
1863
1864   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1865     if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1866       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1867                                           getPointerTy(), 0,MipsII:: MO_GOT);
1868       CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1869                                             0, MipsII::MO_ABS_LO);
1870     } else {
1871       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1872                                           getPointerTy(), 0, OpFlag);
1873     }
1874
1875     LoadSymAddr = true;
1876   }
1877   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1878     Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
1879                                 getPointerTy(), OpFlag);
1880     LoadSymAddr = true;
1881   }
1882
1883   SDValue InFlag;
1884
1885   // Create nodes that load address of callee and copy it to T9
1886   if (IsPIC) {
1887     if (LoadSymAddr) {
1888       // Load callee address
1889       Callee = DAG.getNode(MipsISD::WrapperPIC, dl, MVT::i32, Callee);
1890       SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1891                                       MachinePointerInfo::getGOT(),
1892                                       false, false, 0);
1893
1894       // Use GOT+LO if callee has internal linkage.
1895       if (CalleeLo.getNode()) {
1896         SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1897         Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1898       } else
1899         Callee = LoadValue;
1900
1901       // Use chain output from LoadValue 
1902       Chain = LoadValue.getValue(1);
1903     }
1904
1905     // copy to T9
1906     Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1907     InFlag = Chain.getValue(1);
1908     Callee = DAG.getRegister(Mips::T9, MVT::i32);
1909   }
1910
1911   // Build a sequence of copy-to-reg nodes chained together with token
1912   // chain and flag operands which copy the outgoing args into registers.
1913   // The InFlag in necessary since all emitted instructions must be
1914   // stuck together.
1915   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1916     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1917                              RegsToPass[i].second, InFlag);
1918     InFlag = Chain.getValue(1);
1919   }
1920
1921   // MipsJmpLink = #chain, #target_address, #opt_in_flags...
1922   //             = Chain, Callee, Reg#1, Reg#2, ...
1923   //
1924   // Returns a chain & a flag for retval copy to use.
1925   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1926   SmallVector<SDValue, 8> Ops;
1927   Ops.push_back(Chain);
1928   Ops.push_back(Callee);
1929
1930   // Add argument registers to the end of the list so that they are
1931   // known live into the call.
1932   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1933     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1934                                   RegsToPass[i].second.getValueType()));
1935
1936   if (InFlag.getNode())
1937     Ops.push_back(InFlag);
1938
1939   Chain  = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
1940   InFlag = Chain.getValue(1);
1941
1942   // Function can have an arbitrary number of calls, so
1943   // hold the LastArgStackLoc with the biggest offset.
1944   unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1945   unsigned NextStackOffset = CCInfo.getNextStackOffset();
1946
1947   // For O32, a minimum of four words (16 bytes) of argument space is
1948   // allocated.
1949   if (Subtarget->isABI_O32())
1950     NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1951
1952   if (MaxCallFrameSize < NextStackOffset) {
1953     MipsFI->setMaxCallFrameSize(NextStackOffset);
1954
1955     if (IsPIC) {    
1956       // $gp restore slot must be aligned.
1957       unsigned StackAlignment = TFL->getStackAlignment();
1958       NextStackOffset = (NextStackOffset + StackAlignment - 1) / 
1959                         StackAlignment * StackAlignment;
1960       int GPFI = MipsFI->getGPFI();
1961       MFI->setObjectOffset(GPFI, NextStackOffset);
1962     }
1963   }
1964
1965   // Extend range of indices of frame objects for outgoing arguments that were
1966   // created during this function call. Skip this step if no such objects were
1967   // created.
1968   if (LastFI)
1969     MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1970
1971   // Create the CALLSEQ_END node.
1972   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1973                              DAG.getIntPtrConstant(0, true), InFlag);
1974   InFlag = Chain.getValue(1);
1975
1976   // Handle result values, copying them out of physregs into vregs that we
1977   // return.
1978   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1979                          Ins, dl, DAG, InVals);
1980 }
1981
1982 /// LowerCallResult - Lower the result values of a call into the
1983 /// appropriate copies out of appropriate physical registers.
1984 SDValue
1985 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1986                                     CallingConv::ID CallConv, bool isVarArg,
1987                                     const SmallVectorImpl<ISD::InputArg> &Ins,
1988                                     DebugLoc dl, SelectionDAG &DAG,
1989                                     SmallVectorImpl<SDValue> &InVals) const {
1990   // Assign locations to each value returned by this call.
1991   SmallVector<CCValAssign, 16> RVLocs;
1992   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1993                  RVLocs, *DAG.getContext());
1994
1995   CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
1996
1997   // Copy all of the result registers out of their specified physreg.
1998   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1999     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
2000                                RVLocs[i].getValVT(), InFlag).getValue(1);
2001     InFlag = Chain.getValue(2);
2002     InVals.push_back(Chain.getValue(0));
2003   }
2004
2005   return Chain;
2006 }
2007
2008 //===----------------------------------------------------------------------===//
2009 //             Formal Arguments Calling Convention Implementation
2010 //===----------------------------------------------------------------------===//
2011 static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
2012                          std::vector<SDValue>& OutChains,
2013                          SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
2014                          const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
2015   unsigned LocMem = VA.getLocMemOffset();
2016   unsigned FirstWord = LocMem / 4;
2017
2018   // copy register A0 - A3 to frame object
2019   for (unsigned i = 0; i < NumWords; ++i) {
2020     unsigned CurWord = FirstWord + i;
2021     if (CurWord >= O32IntRegsSize)
2022       break;
2023
2024     unsigned SrcReg = O32IntRegs[CurWord];
2025     unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
2026     SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
2027                                    DAG.getConstant(i * 4, MVT::i32));
2028     SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
2029                                  StorePtr, MachinePointerInfo(), false,
2030                                  false, 0);
2031     OutChains.push_back(Store);
2032   }
2033 }
2034
2035 /// LowerFormalArguments - transform physical registers into virtual registers
2036 /// and generate load operations for arguments places on the stack.
2037 SDValue
2038 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
2039                                          CallingConv::ID CallConv,
2040                                          bool isVarArg,
2041                                          const SmallVectorImpl<ISD::InputArg>
2042                                          &Ins,
2043                                          DebugLoc dl, SelectionDAG &DAG,
2044                                          SmallVectorImpl<SDValue> &InVals)
2045                                           const {
2046   MachineFunction &MF = DAG.getMachineFunction();
2047   MachineFrameInfo *MFI = MF.getFrameInfo();
2048   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2049
2050   MipsFI->setVarArgsFrameIndex(0);
2051
2052   // Used with vargs to acumulate store chains.
2053   std::vector<SDValue> OutChains;
2054
2055   // Assign locations to all of the incoming arguments.
2056   SmallVector<CCValAssign, 16> ArgLocs;
2057   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2058                  ArgLocs, *DAG.getContext());
2059
2060   if (Subtarget->isABI_O32())
2061     CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
2062   else
2063     CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
2064
2065   int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
2066
2067   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2068     CCValAssign &VA = ArgLocs[i];
2069
2070     // Arguments stored on registers
2071     if (VA.isRegLoc()) {
2072       EVT RegVT = VA.getLocVT();
2073       unsigned ArgReg = VA.getLocReg();
2074       TargetRegisterClass *RC = 0;
2075
2076       if (RegVT == MVT::i32)
2077         RC = Mips::CPURegsRegisterClass;
2078       else if (RegVT == MVT::f32)
2079         RC = Mips::FGR32RegisterClass;
2080       else if (RegVT == MVT::f64) {
2081         if (!Subtarget->isSingleFloat())
2082           RC = Mips::AFGR64RegisterClass;
2083       } else
2084         llvm_unreachable("RegVT not supported by FormalArguments Lowering");
2085
2086       // Transform the arguments stored on
2087       // physical registers into virtual ones
2088       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
2089       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
2090
2091       // If this is an 8 or 16-bit value, it has been passed promoted
2092       // to 32 bits.  Insert an assert[sz]ext to capture this, then
2093       // truncate to the right size.
2094       if (VA.getLocInfo() != CCValAssign::Full) {
2095         unsigned Opcode = 0;
2096         if (VA.getLocInfo() == CCValAssign::SExt)
2097           Opcode = ISD::AssertSext;
2098         else if (VA.getLocInfo() == CCValAssign::ZExt)
2099           Opcode = ISD::AssertZext;
2100         if (Opcode)
2101           ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
2102                                  DAG.getValueType(VA.getValVT()));
2103         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2104       }
2105
2106       // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
2107       if (Subtarget->isABI_O32()) {
2108         if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
2109           ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
2110         if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
2111           unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
2112                                     VA.getLocReg()+1, RC);
2113           SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
2114           if (!Subtarget->isLittle())
2115             std::swap(ArgValue, ArgValue2);
2116           ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
2117                                  ArgValue, ArgValue2);
2118         }
2119       }
2120
2121       InVals.push_back(ArgValue);
2122     } else { // VA.isRegLoc()
2123
2124       // sanity check
2125       assert(VA.isMemLoc());
2126
2127       ISD::ArgFlagsTy Flags = Ins[i].Flags;
2128
2129       if (Flags.isByVal()) {
2130         assert(Subtarget->isABI_O32() &&
2131                "No support for ByVal args by ABIs other than O32 yet.");
2132         assert(Flags.getByValSize() &&
2133                "ByVal args of size 0 should have been ignored by front-end.");
2134         unsigned NumWords = (Flags.getByValSize() + 3) / 4;
2135         LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
2136                                         true);
2137         SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2138         InVals.push_back(FIN);
2139         ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
2140
2141         continue;
2142       }
2143
2144       // The stack pointer offset is relative to the caller stack frame.
2145       LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
2146                                       VA.getLocMemOffset(), true);
2147
2148       // Create load nodes to retrieve arguments from the stack
2149       SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
2150       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
2151                                    MachinePointerInfo::getFixedStack(LastFI),
2152                                    false, false, 0));
2153     }
2154   }
2155
2156   // The mips ABIs for returning structs by value requires that we copy
2157   // the sret argument into $v0 for the return. Save the argument into
2158   // a virtual register so that we can access it from the return points.
2159   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2160     unsigned Reg = MipsFI->getSRetReturnReg();
2161     if (!Reg) {
2162       Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
2163       MipsFI->setSRetReturnReg(Reg);
2164     }
2165     SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
2166     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
2167   }
2168
2169   if (isVarArg && Subtarget->isABI_O32()) {
2170     // Record the frame index of the first variable argument
2171     // which is a value necessary to VASTART.    
2172     unsigned NextStackOffset = CCInfo.getNextStackOffset();
2173     assert(NextStackOffset % 4 == 0 &&
2174            "NextStackOffset must be aligned to 4-byte boundaries.");
2175     LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2176     MipsFI->setVarArgsFrameIndex(LastFI);
2177
2178     // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
2179     // copy the integer registers that have not been used for argument passing
2180     // to the caller's stack frame.
2181     for (; NextStackOffset < 16; NextStackOffset += 4) {
2182       TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
2183       unsigned Idx = NextStackOffset / 4;
2184       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
2185       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
2186       LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
2187       SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
2188       OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
2189                                        MachinePointerInfo(),
2190                                        false, false, 0));
2191     }
2192   }
2193
2194   MipsFI->setLastInArgFI(LastFI);
2195
2196   // All stores are grouped in one node to allow the matching between
2197   // the size of Ins and InVals. This only happens when on varg functions
2198   if (!OutChains.empty()) {
2199     OutChains.push_back(Chain);
2200     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2201                         &OutChains[0], OutChains.size());
2202   }
2203
2204   return Chain;
2205 }
2206
2207 //===----------------------------------------------------------------------===//
2208 //               Return Value Calling Convention Implementation
2209 //===----------------------------------------------------------------------===//
2210
2211 SDValue
2212 MipsTargetLowering::LowerReturn(SDValue Chain,
2213                                 CallingConv::ID CallConv, bool isVarArg,
2214                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
2215                                 const SmallVectorImpl<SDValue> &OutVals,
2216                                 DebugLoc dl, SelectionDAG &DAG) const {
2217
2218   // CCValAssign - represent the assignment of
2219   // the return value to a location
2220   SmallVector<CCValAssign, 16> RVLocs;
2221
2222   // CCState - Info about the registers and stack slot.
2223   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
2224                  RVLocs, *DAG.getContext());
2225
2226   // Analize return values.
2227   CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
2228
2229   // If this is the first return lowered for this function, add
2230   // the regs to the liveout set for the function.
2231   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
2232     for (unsigned i = 0; i != RVLocs.size(); ++i)
2233       if (RVLocs[i].isRegLoc())
2234         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
2235   }
2236
2237   SDValue Flag;
2238
2239   // Copy the result values into the output registers.
2240   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2241     CCValAssign &VA = RVLocs[i];
2242     assert(VA.isRegLoc() && "Can only return in registers!");
2243
2244     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
2245                              OutVals[i], Flag);
2246
2247     // guarantee that all emitted copies are
2248     // stuck together, avoiding something bad
2249     Flag = Chain.getValue(1);
2250   }
2251
2252   // The mips ABIs for returning structs by value requires that we copy
2253   // the sret argument into $v0 for the return. We saved the argument into
2254   // a virtual register in the entry block, so now we copy the value out
2255   // and into $v0.
2256   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
2257     MachineFunction &MF      = DAG.getMachineFunction();
2258     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
2259     unsigned Reg = MipsFI->getSRetReturnReg();
2260
2261     if (!Reg)
2262       llvm_unreachable("sret virtual register not created in the entry block");
2263     SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
2264
2265     Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
2266     Flag = Chain.getValue(1);
2267   }
2268
2269   // Return on Mips is always a "jr $ra"
2270   if (Flag.getNode())
2271     return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
2272                        Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
2273   else // Return Void
2274     return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
2275                        Chain, DAG.getRegister(Mips::RA, MVT::i32));
2276 }
2277
2278 //===----------------------------------------------------------------------===//
2279 //                           Mips Inline Assembly Support
2280 //===----------------------------------------------------------------------===//
2281
2282 /// getConstraintType - Given a constraint letter, return the type of
2283 /// constraint it is for this target.
2284 MipsTargetLowering::ConstraintType MipsTargetLowering::
2285 getConstraintType(const std::string &Constraint) const
2286 {
2287   // Mips specific constrainy
2288   // GCC config/mips/constraints.md
2289   //
2290   // 'd' : An address register. Equivalent to r
2291   //       unless generating MIPS16 code.
2292   // 'y' : Equivalent to r; retained for
2293   //       backwards compatibility.
2294   // 'f' : Floating Point registers.
2295   if (Constraint.size() == 1) {
2296     switch (Constraint[0]) {
2297       default : break;
2298       case 'd':
2299       case 'y':
2300       case 'f':
2301         return C_RegisterClass;
2302         break;
2303     }
2304   }
2305   return TargetLowering::getConstraintType(Constraint);
2306 }
2307
2308 /// Examine constraint type and operand type and determine a weight value.
2309 /// This object must already have been set up with the operand type
2310 /// and the current alternative constraint selected.
2311 TargetLowering::ConstraintWeight
2312 MipsTargetLowering::getSingleConstraintMatchWeight(
2313     AsmOperandInfo &info, const char *constraint) const {
2314   ConstraintWeight weight = CW_Invalid;
2315   Value *CallOperandVal = info.CallOperandVal;
2316     // If we don't have a value, we can't do a match,
2317     // but allow it at the lowest weight.
2318   if (CallOperandVal == NULL)
2319     return CW_Default;
2320   const Type *type = CallOperandVal->getType();
2321   // Look at the constraint type.
2322   switch (*constraint) {
2323   default:
2324     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
2325     break;
2326   case 'd':
2327   case 'y':
2328     if (type->isIntegerTy())
2329       weight = CW_Register;
2330     break;
2331   case 'f':
2332     if (type->isFloatTy())
2333       weight = CW_Register;
2334     break;
2335   }
2336   return weight;
2337 }
2338
2339 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
2340 /// return a list of registers that can be used to satisfy the constraint.
2341 /// This should only be used for C_RegisterClass constraints.
2342 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
2343 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
2344 {
2345   if (Constraint.size() == 1) {
2346     switch (Constraint[0]) {
2347     case 'r':
2348       return std::make_pair(0U, Mips::CPURegsRegisterClass);
2349     case 'f':
2350       if (VT == MVT::f32)
2351         return std::make_pair(0U, Mips::FGR32RegisterClass);
2352       if (VT == MVT::f64)
2353         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2354           return std::make_pair(0U, Mips::AFGR64RegisterClass);
2355     }
2356   }
2357   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2358 }
2359
2360 /// Given a register class constraint, like 'r', if this corresponds directly
2361 /// to an LLVM register class, return a register of 0 and the register class
2362 /// pointer.
2363 std::vector<unsigned> MipsTargetLowering::
2364 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2365                                   EVT VT) const
2366 {
2367   if (Constraint.size() != 1)
2368     return std::vector<unsigned>();
2369
2370   switch (Constraint[0]) {
2371     default : break;
2372     case 'r':
2373     // GCC Mips Constraint Letters
2374     case 'd':
2375     case 'y':
2376       return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
2377              Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
2378              Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
2379              Mips::T8, 0);
2380
2381     case 'f':
2382       if (VT == MVT::f32) {
2383         if (Subtarget->isSingleFloat())
2384           return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
2385                  Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
2386                  Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
2387                  Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
2388                  Mips::F30, Mips::F31, 0);
2389         else
2390           return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
2391                  Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
2392                  Mips::F28, Mips::F30, 0);
2393       }
2394
2395       if (VT == MVT::f64)
2396         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
2397           return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
2398                  Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
2399                  Mips::D14, Mips::D15, 0);
2400   }
2401   return std::vector<unsigned>();
2402 }
2403
2404 bool
2405 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2406   // The Mips target isn't yet aware of offsets.
2407   return false;
2408 }
2409
2410 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
2411   if (VT != MVT::f32 && VT != MVT::f64)
2412     return false;
2413   if (Imm.isNegZero())
2414     return false;
2415   return Imm.isZero();
2416 }