4c425616094febba0bf0931f65cf8c87cd328696
[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 <algorithm>
17 #include "MipsISelLowering.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsTargetMachine.h"
20 #include "MipsTargetObjectFile.h"
21 #include "MipsSubtarget.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/GlobalVariable.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/CallingConv.h"
27 #include "llvm/CodeGen/CallingConvLower.h"
28 #include "llvm/CodeGen/MachineFrameInfo.h"
29 #include "llvm/CodeGen/MachineFunction.h"
30 #include "llvm/CodeGen/MachineInstrBuilder.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/ValueTypes.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 using namespace llvm;
37
38 const char *MipsTargetLowering::getTargetNodeName(unsigned Opcode) const {
39   switch (Opcode) {
40   case MipsISD::JmpLink:           return "MipsISD::JmpLink";
41   case MipsISD::Hi:                return "MipsISD::Hi";
42   case MipsISD::Lo:                return "MipsISD::Lo";
43   case MipsISD::GPRel:             return "MipsISD::GPRel";
44   case MipsISD::Ret:               return "MipsISD::Ret";
45   case MipsISD::FPBrcond:          return "MipsISD::FPBrcond";
46   case MipsISD::FPCmp:             return "MipsISD::FPCmp";
47   case MipsISD::CMovFP_T:          return "MipsISD::CMovFP_T";
48   case MipsISD::CMovFP_F:          return "MipsISD::CMovFP_F";
49   case MipsISD::FPRound:           return "MipsISD::FPRound";
50   case MipsISD::MAdd:              return "MipsISD::MAdd";
51   case MipsISD::MAddu:             return "MipsISD::MAddu";
52   case MipsISD::MSub:              return "MipsISD::MSub";
53   case MipsISD::MSubu:             return "MipsISD::MSubu";
54   case MipsISD::DivRem:            return "MipsISD::DivRem";
55   case MipsISD::DivRemU:           return "MipsISD::DivRemU";
56   case MipsISD::BuildPairF64:      return "MipsISD::BuildPairF64";
57   case MipsISD::ExtractElementF64: return "MipsISD::ExtractElementF64";
58   default: return NULL;
59   }
60 }
61
62 MipsTargetLowering::
63 MipsTargetLowering(MipsTargetMachine &TM)
64   : TargetLowering(TM, new MipsTargetObjectFile()) {
65   Subtarget = &TM.getSubtarget<MipsSubtarget>();
66
67   // Mips does not have i1 type, so use i32 for
68   // setcc operations results (slt, sgt, ...).
69   setBooleanContents(ZeroOrOneBooleanContent);
70
71   // Set up the register classes
72   addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
73   addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
74
75   // When dealing with single precision only, use libcalls
76   if (!Subtarget->isSingleFloat())
77     if (!Subtarget->isFP64bit())
78       addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
79
80   // Load extented operations for i1 types must be promoted
81   setLoadExtAction(ISD::EXTLOAD,  MVT::i1,  Promote);
82   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
83   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
84
85   // MIPS doesn't have extending float->double load/store
86   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
87   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
88
89   // Used by legalize types to correctly generate the setcc result.
90   // Without this, every float setcc comes with a AND/OR with the result,
91   // we don't want this, since the fpcmp result goes to a flag register,
92   // which is used implicitly by brcond and select operations.
93   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
94
95   // Mips Custom Operations
96   setOperationAction(ISD::GlobalAddress,      MVT::i32,   Custom);
97   setOperationAction(ISD::BlockAddress,       MVT::i32,   Custom);
98   setOperationAction(ISD::GlobalTLSAddress,   MVT::i32,   Custom);
99   setOperationAction(ISD::JumpTable,          MVT::i32,   Custom);
100   setOperationAction(ISD::ConstantPool,       MVT::i32,   Custom);
101   setOperationAction(ISD::SELECT,             MVT::f32,   Custom);
102   setOperationAction(ISD::SELECT,             MVT::f64,   Custom);
103   setOperationAction(ISD::SELECT,             MVT::i32,   Custom);
104   setOperationAction(ISD::BRCOND,             MVT::Other, Custom);
105   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,   Custom);
106   setOperationAction(ISD::VASTART,            MVT::Other, Custom);
107
108   setOperationAction(ISD::SDIV, MVT::i32, Expand);
109   setOperationAction(ISD::SREM, MVT::i32, Expand);
110   setOperationAction(ISD::UDIV, MVT::i32, Expand);
111   setOperationAction(ISD::UREM, MVT::i32, Expand);
112
113   // Operations not directly supported by Mips.
114   setOperationAction(ISD::BR_JT,             MVT::Other, Expand);
115   setOperationAction(ISD::BR_CC,             MVT::Other, Expand);
116   setOperationAction(ISD::SELECT_CC,         MVT::Other, Expand);
117   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
118   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
119   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
120   setOperationAction(ISD::CTPOP,             MVT::i32,   Expand);
121   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
122   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
123
124   if (!Subtarget->isMips32r2())
125     setOperationAction(ISD::ROTR, MVT::i32,   Expand);
126
127   setOperationAction(ISD::SHL_PARTS,         MVT::i32,   Expand);
128   setOperationAction(ISD::SRA_PARTS,         MVT::i32,   Expand);
129   setOperationAction(ISD::SRL_PARTS,         MVT::i32,   Expand);
130   setOperationAction(ISD::FCOPYSIGN,         MVT::f32,   Custom);
131   setOperationAction(ISD::FCOPYSIGN,         MVT::f64,   Custom);
132   setOperationAction(ISD::FSIN,              MVT::f32,   Expand);
133   setOperationAction(ISD::FSIN,              MVT::f64,   Expand);
134   setOperationAction(ISD::FCOS,              MVT::f32,   Expand);
135   setOperationAction(ISD::FCOS,              MVT::f64,   Expand);
136   setOperationAction(ISD::FPOWI,             MVT::f32,   Expand);
137   setOperationAction(ISD::FPOW,              MVT::f32,   Expand);
138   setOperationAction(ISD::FPOW,              MVT::f64,   Expand);
139   setOperationAction(ISD::FLOG,              MVT::f32,   Expand);
140   setOperationAction(ISD::FLOG2,             MVT::f32,   Expand);
141   setOperationAction(ISD::FLOG10,            MVT::f32,   Expand);
142   setOperationAction(ISD::FEXP,              MVT::f32,   Expand);
143
144   setOperationAction(ISD::EXCEPTIONADDR,     MVT::i32, Expand);
145   setOperationAction(ISD::EHSELECTION,       MVT::i32, Expand);
146   
147   setOperationAction(ISD::VAARG,             MVT::Other, Expand);
148   setOperationAction(ISD::VACOPY,            MVT::Other, Expand);
149   setOperationAction(ISD::VAEND,             MVT::Other, Expand);
150
151   // Use the default for now
152   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
153   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
154   setOperationAction(ISD::MEMBARRIER,        MVT::Other, Expand);
155
156   if (Subtarget->isSingleFloat())
157     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
158
159   if (!Subtarget->hasSEInReg()) {
160     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
161     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
162   }
163
164   if (!Subtarget->hasBitCount())
165     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
166
167   if (!Subtarget->hasSwap())
168     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
169
170   setTargetDAGCombine(ISD::ADDE);
171   setTargetDAGCombine(ISD::SUBE);
172   setTargetDAGCombine(ISD::SDIVREM);
173   setTargetDAGCombine(ISD::UDIVREM);
174   setTargetDAGCombine(ISD::SETCC);
175
176   setMinFunctionAlignment(2);
177
178   setStackPointerRegisterToSaveRestore(Mips::SP);
179   computeRegisterProperties();
180
181   setExceptionPointerRegister(Mips::A0);
182   setExceptionSelectorRegister(Mips::A1);
183 }
184
185 MVT::SimpleValueType MipsTargetLowering::getSetCCResultType(EVT VT) const {
186   return MVT::i32;
187 }
188
189 // SelectMadd -
190 // Transforms a subgraph in CurDAG if the following pattern is found:
191 //  (addc multLo, Lo0), (adde multHi, Hi0),
192 // where,
193 //  multHi/Lo: product of multiplication
194 //  Lo0: initial value of Lo register
195 //  Hi0: initial value of Hi register
196 // Return true if pattern matching was successful.
197 static bool SelectMadd(SDNode* ADDENode, SelectionDAG* CurDAG) {
198   // ADDENode's second operand must be a flag output of an ADDC node in order
199   // for the matching to be successful.
200   SDNode* ADDCNode = ADDENode->getOperand(2).getNode();
201
202   if (ADDCNode->getOpcode() != ISD::ADDC)
203     return false;
204
205   SDValue MultHi = ADDENode->getOperand(0);
206   SDValue MultLo = ADDCNode->getOperand(0);
207   SDNode* MultNode = MultHi.getNode();
208   unsigned MultOpc = MultHi.getOpcode();
209
210   // MultHi and MultLo must be generated by the same node,
211   if (MultLo.getNode() != MultNode)
212     return false;
213
214   // and it must be a multiplication.
215   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
216     return false;
217
218   // MultLo amd MultHi must be the first and second output of MultNode
219   // respectively.
220   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
221     return false;
222
223   // Transform this to a MADD only if ADDENode and ADDCNode are the only users
224   // of the values of MultNode, in which case MultNode will be removed in later
225   // phases.
226   // If there exist users other than ADDENode or ADDCNode, this function returns
227   // here, which will result in MultNode being mapped to a single MULT
228   // instruction node rather than a pair of MULT and MADD instructions being
229   // produced.
230   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
231     return false;
232
233   SDValue Chain = CurDAG->getEntryNode();
234   DebugLoc dl = ADDENode->getDebugLoc();
235
236   // create MipsMAdd(u) node
237   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MAddu : MipsISD::MAdd;
238
239   SDValue MAdd = CurDAG->getNode(MultOpc, dl,
240                                  MVT::Glue,
241                                  MultNode->getOperand(0),// Factor 0
242                                  MultNode->getOperand(1),// Factor 1
243                                  ADDCNode->getOperand(1),// Lo0
244                                  ADDENode->getOperand(1));// Hi0
245
246   // create CopyFromReg nodes
247   SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
248                                               MAdd);
249   SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
250                                               Mips::HI, MVT::i32,
251                                               CopyFromLo.getValue(2));
252
253   // replace uses of adde and addc here
254   if (!SDValue(ADDCNode, 0).use_empty())
255     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDCNode, 0), CopyFromLo);
256
257   if (!SDValue(ADDENode, 0).use_empty())
258     CurDAG->ReplaceAllUsesOfValueWith(SDValue(ADDENode, 0), CopyFromHi);
259
260   return true;
261 }
262
263 // SelectMsub -
264 // Transforms a subgraph in CurDAG if the following pattern is found:
265 //  (addc Lo0, multLo), (sube Hi0, multHi),
266 // where,
267 //  multHi/Lo: product of multiplication
268 //  Lo0: initial value of Lo register
269 //  Hi0: initial value of Hi register
270 // Return true if pattern matching was successful.
271 static bool SelectMsub(SDNode* SUBENode, SelectionDAG* CurDAG) {
272   // SUBENode's second operand must be a flag output of an SUBC node in order
273   // for the matching to be successful.
274   SDNode* SUBCNode = SUBENode->getOperand(2).getNode();
275
276   if (SUBCNode->getOpcode() != ISD::SUBC)
277     return false;
278
279   SDValue MultHi = SUBENode->getOperand(1);
280   SDValue MultLo = SUBCNode->getOperand(1);
281   SDNode* MultNode = MultHi.getNode();
282   unsigned MultOpc = MultHi.getOpcode();
283
284   // MultHi and MultLo must be generated by the same node,
285   if (MultLo.getNode() != MultNode)
286     return false;
287
288   // and it must be a multiplication.
289   if (MultOpc != ISD::SMUL_LOHI && MultOpc != ISD::UMUL_LOHI)
290     return false;
291
292   // MultLo amd MultHi must be the first and second output of MultNode
293   // respectively.
294   if (MultHi.getResNo() != 1 || MultLo.getResNo() != 0)
295     return false;
296
297   // Transform this to a MSUB only if SUBENode and SUBCNode are the only users
298   // of the values of MultNode, in which case MultNode will be removed in later
299   // phases.
300   // If there exist users other than SUBENode or SUBCNode, this function returns
301   // here, which will result in MultNode being mapped to a single MULT
302   // instruction node rather than a pair of MULT and MSUB instructions being
303   // produced.
304   if (!MultHi.hasOneUse() || !MultLo.hasOneUse())
305     return false;
306
307   SDValue Chain = CurDAG->getEntryNode();
308   DebugLoc dl = SUBENode->getDebugLoc();
309
310   // create MipsSub(u) node
311   MultOpc = MultOpc == ISD::UMUL_LOHI ? MipsISD::MSubu : MipsISD::MSub;
312
313   SDValue MSub = CurDAG->getNode(MultOpc, dl,
314                                  MVT::Glue,
315                                  MultNode->getOperand(0),// Factor 0
316                                  MultNode->getOperand(1),// Factor 1
317                                  SUBCNode->getOperand(0),// Lo0
318                                  SUBENode->getOperand(0));// Hi0
319
320   // create CopyFromReg nodes
321   SDValue CopyFromLo = CurDAG->getCopyFromReg(Chain, dl, Mips::LO, MVT::i32,
322                                               MSub);
323   SDValue CopyFromHi = CurDAG->getCopyFromReg(CopyFromLo.getValue(1), dl,
324                                               Mips::HI, MVT::i32,
325                                               CopyFromLo.getValue(2));
326
327   // replace uses of sube and subc here
328   if (!SDValue(SUBCNode, 0).use_empty())
329     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBCNode, 0), CopyFromLo);
330
331   if (!SDValue(SUBENode, 0).use_empty())
332     CurDAG->ReplaceAllUsesOfValueWith(SDValue(SUBENode, 0), CopyFromHi);
333
334   return true;
335 }
336
337 static SDValue PerformADDECombine(SDNode *N, SelectionDAG& DAG,
338                                   TargetLowering::DAGCombinerInfo &DCI,
339                                   const MipsSubtarget* Subtarget) {
340   if (DCI.isBeforeLegalize())
341     return SDValue();
342
343   if (Subtarget->isMips32() && SelectMadd(N, &DAG))
344     return SDValue(N, 0);
345
346   return SDValue();
347 }
348
349 static SDValue PerformSUBECombine(SDNode *N, SelectionDAG& DAG,
350                                   TargetLowering::DAGCombinerInfo &DCI,
351                                   const MipsSubtarget* Subtarget) {
352   if (DCI.isBeforeLegalize())
353     return SDValue();
354
355   if (Subtarget->isMips32() && SelectMsub(N, &DAG))
356     return SDValue(N, 0);
357
358   return SDValue();
359 }
360
361 static SDValue PerformDivRemCombine(SDNode *N, SelectionDAG& DAG,
362                                     TargetLowering::DAGCombinerInfo &DCI,
363                                     const MipsSubtarget* Subtarget) {
364   if (DCI.isBeforeLegalizeOps())
365     return SDValue();
366
367   unsigned opc = N->getOpcode() == ISD::SDIVREM ? MipsISD::DivRem :
368                                                   MipsISD::DivRemU;
369   DebugLoc dl = N->getDebugLoc();
370
371   SDValue DivRem = DAG.getNode(opc, dl, MVT::Glue,
372                                N->getOperand(0), N->getOperand(1));
373   SDValue InChain = DAG.getEntryNode();
374   SDValue InGlue = DivRem;
375
376   // insert MFLO
377   if (N->hasAnyUseOfValue(0)) {
378     SDValue CopyFromLo = DAG.getCopyFromReg(InChain, dl, Mips::LO, MVT::i32,
379                                             InGlue);
380     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 0), CopyFromLo);
381     InChain = CopyFromLo.getValue(1);
382     InGlue = CopyFromLo.getValue(2);
383   }
384
385   // insert MFHI
386   if (N->hasAnyUseOfValue(1)) {
387     SDValue CopyFromHi = DAG.getCopyFromReg(InChain, dl,
388                                             Mips::HI, MVT::i32, InGlue);
389     DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), CopyFromHi);
390   }
391
392   return SDValue();
393 }
394
395 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
396   switch (CC) {
397   default: llvm_unreachable("Unknown fp condition code!");
398   case ISD::SETEQ:
399   case ISD::SETOEQ: return Mips::FCOND_OEQ;
400   case ISD::SETUNE: return Mips::FCOND_UNE;
401   case ISD::SETLT:
402   case ISD::SETOLT: return Mips::FCOND_OLT;
403   case ISD::SETGT:
404   case ISD::SETOGT: return Mips::FCOND_OGT;
405   case ISD::SETLE:
406   case ISD::SETOLE: return Mips::FCOND_OLE;
407   case ISD::SETGE:
408   case ISD::SETOGE: return Mips::FCOND_OGE;
409   case ISD::SETULT: return Mips::FCOND_ULT;
410   case ISD::SETULE: return Mips::FCOND_ULE;
411   case ISD::SETUGT: return Mips::FCOND_UGT;
412   case ISD::SETUGE: return Mips::FCOND_UGE;
413   case ISD::SETUO:  return Mips::FCOND_UN;
414   case ISD::SETO:   return Mips::FCOND_OR;
415   case ISD::SETNE:
416   case ISD::SETONE: return Mips::FCOND_ONE;
417   case ISD::SETUEQ: return Mips::FCOND_UEQ;
418   }
419 }
420
421
422 // Returns true if condition code has to be inverted.
423 static bool InvertFPCondCode(Mips::CondCode CC) {
424   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
425     return false;
426
427   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
428     return true;
429
430   assert(false && "Illegal Condition Code");
431   return false;
432 }
433
434 // Creates and returns an FPCmp node from a setcc node.
435 // Returns Op if setcc is not a floating point comparison.
436 static SDValue CreateFPCmp(SelectionDAG& DAG, const SDValue& Op) {
437   // must be a SETCC node
438   if (Op.getOpcode() != ISD::SETCC)
439     return Op;
440
441   SDValue LHS = Op.getOperand(0);
442
443   if (!LHS.getValueType().isFloatingPoint())
444     return Op;
445
446   SDValue RHS = Op.getOperand(1);
447   DebugLoc dl = Op.getDebugLoc();
448
449   // Assume the 3rd operand is a CondCodeSDNode. Add code to check the type of
450   // node if necessary.
451   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
452
453   return DAG.getNode(MipsISD::FPCmp, dl, MVT::Glue, LHS, RHS,
454                      DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
455 }
456
457 // Creates and returns a CMovFPT/F node.
458 static SDValue CreateCMovFP(SelectionDAG& DAG, SDValue Cond, SDValue True,
459                             SDValue False, DebugLoc DL) {
460   bool invert = InvertFPCondCode((Mips::CondCode)
461                                  cast<ConstantSDNode>(Cond.getOperand(2))
462                                  ->getSExtValue());
463
464   return DAG.getNode((invert ? MipsISD::CMovFP_F : MipsISD::CMovFP_T), DL,
465                      True.getValueType(), True, False, Cond);
466 }
467
468 static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG& DAG,
469                                    TargetLowering::DAGCombinerInfo &DCI,
470                                    const MipsSubtarget* Subtarget) {
471   if (DCI.isBeforeLegalizeOps())
472     return SDValue();
473
474   SDValue Cond = CreateFPCmp(DAG, SDValue(N, 0));
475
476   if (Cond.getOpcode() != MipsISD::FPCmp)
477     return SDValue();
478
479   SDValue True  = DAG.getConstant(1, MVT::i32);
480   SDValue False = DAG.getConstant(0, MVT::i32);
481
482   return CreateCMovFP(DAG, Cond, True, False, N->getDebugLoc());
483 }
484
485 SDValue  MipsTargetLowering::PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI)
486   const {
487   SelectionDAG &DAG = DCI.DAG;
488   unsigned opc = N->getOpcode();
489
490   switch (opc) {
491   default: break;
492   case ISD::ADDE:
493     return PerformADDECombine(N, DAG, DCI, Subtarget);
494   case ISD::SUBE:
495     return PerformSUBECombine(N, DAG, DCI, Subtarget);
496   case ISD::SDIVREM:
497   case ISD::UDIVREM:
498     return PerformDivRemCombine(N, DAG, DCI, Subtarget);
499   case ISD::SETCC:
500     return PerformSETCCCombine(N, DAG, DCI, Subtarget);
501   }
502
503   return SDValue();
504 }
505
506 SDValue MipsTargetLowering::
507 LowerOperation(SDValue Op, SelectionDAG &DAG) const
508 {
509   switch (Op.getOpcode())
510   {
511     case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
512     case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
513     case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
514     case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
515     case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
516     case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
517     case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
518     case ISD::SELECT:             return LowerSELECT(Op, DAG);
519     case ISD::VASTART:            return LowerVASTART(Op, DAG);
520     case ISD::FCOPYSIGN:          return LowerFCOPYSIGN(Op, DAG);
521   }
522   return SDValue();
523 }
524
525 //===----------------------------------------------------------------------===//
526 //  Lower helper functions
527 //===----------------------------------------------------------------------===//
528
529 // AddLiveIn - This helper function adds the specified physical register to the
530 // MachineFunction as a live in value.  It also creates a corresponding
531 // virtual register for it.
532 static unsigned
533 AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC)
534 {
535   assert(RC->contains(PReg) && "Not the correct regclass!");
536   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
537   MF.getRegInfo().addLiveIn(PReg, VReg);
538   return VReg;
539 }
540
541 // Get fp branch code (not opcode) from condition code.
542 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
543   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
544     return Mips::BRANCH_T;
545
546   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
547     return Mips::BRANCH_F;
548
549   return Mips::BRANCH_INVALID;
550 }
551
552 MachineBasicBlock *
553 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
554                                                 MachineBasicBlock *BB) const {
555   // There is no need to expand CMov instructions if target has
556   // conditional moves.
557   if (Subtarget->hasCondMov())
558     return BB;
559
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   case Mips::MOVT:
568   case Mips::MOVT_S:
569   case Mips::MOVT_D:
570     isFPCmp = true;
571     Opc = Mips::BC1F;
572     break;
573   case Mips::MOVF:
574   case Mips::MOVF_S:
575   case Mips::MOVF_D:
576     isFPCmp = true;
577     Opc = Mips::BC1T;
578     break;
579   case Mips::MOVZ_I:
580   case Mips::MOVZ_S:
581   case Mips::MOVZ_D:
582     Opc = Mips::BNE;
583     break;
584   case Mips::MOVN_I:
585   case Mips::MOVN_S:
586   case Mips::MOVN_D:
587     Opc = Mips::BEQ;
588     break;
589   }
590
591   // To "insert" a SELECT_CC instruction, we actually have to insert the
592   // diamond control-flow pattern.  The incoming instruction knows the
593   // destination vreg to set, the condition code register to branch on, the
594   // true/false values to select between, and a branch opcode to use.
595   const BasicBlock *LLVM_BB = BB->getBasicBlock();
596   MachineFunction::iterator It = BB;
597   ++It;
598
599   //  thisMBB:
600   //  ...
601   //   TrueVal = ...
602   //   setcc r1, r2, r3
603   //   bNE   r1, r0, copy1MBB
604   //   fallthrough --> copy0MBB
605   MachineBasicBlock *thisMBB  = BB;
606   MachineFunction *F = BB->getParent();
607   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
608   MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
609   F->insert(It, copy0MBB);
610   F->insert(It, sinkMBB);
611
612   // Transfer the remainder of BB and its successor edges to sinkMBB.
613   sinkMBB->splice(sinkMBB->begin(), BB,
614                   llvm::next(MachineBasicBlock::iterator(MI)),
615                   BB->end());
616   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
617
618   // Next, add the true and fallthrough blocks as its successors.
619   BB->addSuccessor(copy0MBB);
620   BB->addSuccessor(sinkMBB);
621
622   // Emit the right instruction according to the type of the operands compared
623   if (isFPCmp)
624     BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
625   else
626     BuildMI(BB, dl, TII->get(Opc)).addReg(MI->getOperand(2).getReg())
627       .addReg(Mips::ZERO).addMBB(sinkMBB);
628
629
630   //  copy0MBB:
631   //   %FalseValue = ...
632   //   # fallthrough to sinkMBB
633   BB = copy0MBB;
634
635   // Update machine-CFG edges
636   BB->addSuccessor(sinkMBB);
637
638   //  sinkMBB:
639   //   %Result = phi [ %TrueValue, thisMBB ], [ %FalseValue, copy0MBB ]
640   //  ...
641   BB = sinkMBB;
642
643   if (isFPCmp)
644     BuildMI(*BB, BB->begin(), dl,
645             TII->get(Mips::PHI), MI->getOperand(0).getReg())
646       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB)
647       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
648   else
649     BuildMI(*BB, BB->begin(), dl,
650             TII->get(Mips::PHI), MI->getOperand(0).getReg())
651       .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB)
652       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB);
653
654   MI->eraseFromParent();   // The pseudo instruction is gone now.
655   return BB;
656 }
657
658 //===----------------------------------------------------------------------===//
659 //  Misc Lower Operation implementation
660 //===----------------------------------------------------------------------===//
661 SDValue MipsTargetLowering::
662 LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const
663 {
664   unsigned StackAlignment =
665     getTargetMachine().getFrameLowering()->getStackAlignment();
666   assert(StackAlignment >=
667          cast<ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue() &&
668          "Cannot lower if the alignment of the allocated space is larger than \
669           that of the stack.");
670
671   SDValue Chain = Op.getOperand(0);
672   SDValue Size = Op.getOperand(1);
673   DebugLoc dl = Op.getDebugLoc();
674
675   // Get a reference from Mips stack pointer
676   SDValue StackPointer = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32);
677
678   // Subtract the dynamic size from the actual stack size to
679   // obtain the new stack size.
680   SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, StackPointer, Size);
681
682   // The Sub result contains the new stack start address, so it
683   // must be placed in the stack pointer register.
684   Chain = DAG.getCopyToReg(StackPointer.getValue(1), dl, Mips::SP, Sub,
685                            SDValue());
686   // Retrieve updated $sp. There is a glue input to prevent instructions that
687   // clobber $sp from being inserted between copytoreg and copyfromreg.
688   SDValue NewSP = DAG.getCopyFromReg(Chain, dl, Mips::SP, MVT::i32,
689                                      Chain.getValue(1));
690   
691   // The stack space reserved by alloca is located right above the argument
692   // area. It is aligned on a boundary that is a multiple of StackAlignment.
693   MachineFunction &MF = DAG.getMachineFunction();
694   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
695   unsigned SPOffset = (MipsFI->getMaxCallFrameSize() + StackAlignment - 1) /
696                       StackAlignment * StackAlignment;
697   SDValue AllocPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
698                                  DAG.getConstant(SPOffset, MVT::i32));
699
700   // This node always has two return values: a new stack pointer
701   // value and a chain
702   SDValue Ops[2] = { AllocPtr, NewSP.getValue(1) };
703   return DAG.getMergeValues(Ops, 2, dl);
704 }
705
706 SDValue MipsTargetLowering::
707 LowerBRCOND(SDValue Op, SelectionDAG &DAG) const
708 {
709   // The first operand is the chain, the second is the condition, the third is
710   // the block to branch to if the condition is true.
711   SDValue Chain = Op.getOperand(0);
712   SDValue Dest = Op.getOperand(2);
713   DebugLoc dl = Op.getDebugLoc();
714
715   SDValue CondRes = CreateFPCmp(DAG, Op.getOperand(1));
716
717   // Return if flag is not set by a floating point comparison.
718   if (CondRes.getOpcode() != MipsISD::FPCmp)
719     return Op;
720
721   SDValue CCNode  = CondRes.getOperand(2);
722   Mips::CondCode CC =
723     (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
724   SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32);
725
726   return DAG.getNode(MipsISD::FPBrcond, dl, Op.getValueType(), Chain, BrCode,
727                      Dest, CondRes);
728 }
729
730 SDValue MipsTargetLowering::
731 LowerSELECT(SDValue Op, SelectionDAG &DAG) const
732 {
733   SDValue Cond = CreateFPCmp(DAG, Op.getOperand(0));
734
735   // Return if flag is not set by a floating point comparison.
736   if (Cond.getOpcode() != MipsISD::FPCmp)
737     return Op;
738
739   return CreateCMovFP(DAG, Cond, Op.getOperand(1), Op.getOperand(2),
740                       Op.getDebugLoc());
741 }
742
743 SDValue MipsTargetLowering::LowerGlobalAddress(SDValue Op,
744                                                SelectionDAG &DAG) const {
745   // FIXME there isn't actually debug info here
746   DebugLoc dl = Op.getDebugLoc();
747   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
748
749   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
750     SDVTList VTs = DAG.getVTList(MVT::i32);
751
752     MipsTargetObjectFile &TLOF = (MipsTargetObjectFile&)getObjFileLowering();
753
754     // %gp_rel relocation
755     if (TLOF.IsGlobalInSmallSection(GV, getTargetMachine())) {
756       SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
757                                               MipsII::MO_GPREL);
758       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, dl, VTs, &GA, 1);
759       SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
760       return DAG.getNode(ISD::ADD, dl, MVT::i32, GOT, GPRelNode);
761     }
762     // %hi/%lo relocation
763     SDValue GAHi = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
764                                               MipsII::MO_ABS_HI);
765     SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
766                                               MipsII::MO_ABS_LO);
767     SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, VTs, &GAHi, 1);
768     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
769     return DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
770   } else {
771     SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
772                                             MipsII::MO_GOT);
773     SDValue ResNode = DAG.getLoad(MVT::i32, dl,
774                                   DAG.getEntryNode(), GA, MachinePointerInfo(),
775                                   false, false, 0);
776     // On functions and global targets not internal linked only
777     // a load from got/GP is necessary for PIC to work.
778     if (!GV->hasInternalLinkage() &&
779         (!GV->hasLocalLinkage() || isa<Function>(GV)))
780       return ResNode;
781     SDValue GALo = DAG.getTargetGlobalAddress(GV, dl, MVT::i32, 0,
782                                               MipsII::MO_ABS_LO);
783     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, GALo);
784     return DAG.getNode(ISD::ADD, dl, MVT::i32, ResNode, Lo);
785   }
786
787   llvm_unreachable("Dont know how to handle GlobalAddress");
788   return SDValue(0,0);
789 }
790
791 SDValue MipsTargetLowering::LowerBlockAddress(SDValue Op,
792                                               SelectionDAG &DAG) const {
793   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
794   // FIXME there isn't actually debug info here
795   DebugLoc dl = Op.getDebugLoc();
796
797   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
798     // %hi/%lo relocation
799     SDValue BAHi = DAG.getBlockAddress(BA, MVT::i32, true,
800                                        MipsII::MO_ABS_HI);
801     SDValue BALo = DAG.getBlockAddress(BA, MVT::i32, true,
802                                        MipsII::MO_ABS_LO);
803     SDValue Hi = DAG.getNode(MipsISD::Hi, dl, MVT::i32, BAHi);
804     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALo);
805     return DAG.getNode(ISD::ADD, dl, MVT::i32, Hi, Lo);
806   }
807
808   SDValue BAGOTOffset = DAG.getBlockAddress(BA, MVT::i32, true,
809                                             MipsII::MO_GOT);
810   SDValue BALOOffset = DAG.getBlockAddress(BA, MVT::i32, true,
811                                            MipsII::MO_ABS_LO);
812   SDValue Load = DAG.getLoad(MVT::i32, dl,
813                              DAG.getEntryNode(), BAGOTOffset,
814                              MachinePointerInfo(), false, false, 0);
815   SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, BALOOffset);
816   return DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
817 }
818
819 SDValue MipsTargetLowering::
820 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const
821 {
822   llvm_unreachable("TLS not implemented for MIPS.");
823   return SDValue(); // Not reached
824 }
825
826 SDValue MipsTargetLowering::
827 LowerJumpTable(SDValue Op, SelectionDAG &DAG) const
828 {
829   SDValue ResNode;
830   SDValue HiPart;
831   // FIXME there isn't actually debug info here
832   DebugLoc dl = Op.getDebugLoc();
833   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
834   unsigned char OpFlag = IsPIC ? MipsII::MO_GOT : MipsII::MO_ABS_HI;
835
836   EVT PtrVT = Op.getValueType();
837   JumpTableSDNode *JT  = cast<JumpTableSDNode>(Op);
838
839   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
840
841   if (!IsPIC) {
842     SDValue Ops[] = { JTI };
843     HiPart = DAG.getNode(MipsISD::Hi, dl, DAG.getVTList(MVT::i32), Ops, 1);
844   } else // Emit Load from Global Pointer
845     HiPart = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(), JTI,
846                          MachinePointerInfo(),
847                          false, false, 0);
848
849   SDValue JTILo = DAG.getTargetJumpTable(JT->getIndex(), PtrVT,
850                                          MipsII::MO_ABS_LO);
851   SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, JTILo);
852   ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
853
854   return ResNode;
855 }
856
857 SDValue MipsTargetLowering::
858 LowerConstantPool(SDValue Op, SelectionDAG &DAG) const
859 {
860   SDValue ResNode;
861   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
862   const Constant *C = N->getConstVal();
863   // FIXME there isn't actually debug info here
864   DebugLoc dl = Op.getDebugLoc();
865
866   // gp_rel relocation
867   // FIXME: we should reference the constant pool using small data sections,
868   // but the asm printer currently doesn't support this feature without
869   // hacking it. This feature should come soon so we can uncomment the
870   // stuff below.
871   //if (IsInSmallSection(C->getType())) {
872   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
873   //  SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(MVT::i32);
874   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode);
875
876   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
877     SDValue CPHi = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
878                                              N->getOffset(), MipsII::MO_ABS_HI);
879     SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
880                                              N->getOffset(), MipsII::MO_ABS_LO);
881     SDValue HiPart = DAG.getNode(MipsISD::Hi, dl, MVT::i32, CPHi);
882     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
883     ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, HiPart, Lo);
884   } else {
885     SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
886                                            N->getOffset(), MipsII::MO_GOT);
887     SDValue Load = DAG.getLoad(MVT::i32, dl, DAG.getEntryNode(),
888                                CP, MachinePointerInfo::getConstantPool(),
889                                false, false, 0);
890     SDValue CPLo = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment(),
891                                              N->getOffset(), MipsII::MO_ABS_LO);
892     SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CPLo);
893     ResNode = DAG.getNode(ISD::ADD, dl, MVT::i32, Load, Lo);
894   }
895
896   return ResNode;
897 }
898
899 SDValue MipsTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
900   MachineFunction &MF = DAG.getMachineFunction();
901   MipsFunctionInfo *FuncInfo = MF.getInfo<MipsFunctionInfo>();
902
903   DebugLoc dl = Op.getDebugLoc();
904   SDValue FI = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(),
905                                  getPointerTy());
906
907   // vastart just stores the address of the VarArgsFrameIndex slot into the
908   // memory location argument.
909   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
910   return DAG.getStore(Op.getOperand(0), dl, FI, Op.getOperand(1),
911                       MachinePointerInfo(SV),
912                       false, false, 0);
913 }
914
915 static SDValue LowerFCOPYSIGN32(SDValue Op, SelectionDAG &DAG) {
916   // FIXME: Use ext/ins instructions if target architecture is Mips32r2.
917   DebugLoc dl = Op.getDebugLoc();
918   SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(0));
919   SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op.getOperand(1));
920   SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op0,
921                              DAG.getConstant(0x7fffffff, MVT::i32));
922   SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op1,
923                              DAG.getConstant(0x80000000, MVT::i32));
924   SDValue Result = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
925   return DAG.getNode(ISD::BITCAST, dl, MVT::f32, Result);
926 }
927
928 static SDValue LowerFCOPYSIGN64(SDValue Op, SelectionDAG &DAG, bool isLittle) {
929   // FIXME: 
930   //  Use ext/ins instructions if target architecture is Mips32r2.
931   //  Eliminate redundant mfc1 and mtc1 instructions.
932   unsigned LoIdx = 0, HiIdx = 1;
933   
934   if (!isLittle)
935     std::swap(LoIdx, HiIdx);
936
937   DebugLoc dl = Op.getDebugLoc();
938   SDValue Word0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
939                               Op.getOperand(0),
940                               DAG.getConstant(LoIdx, MVT::i32));
941   SDValue Hi0 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
942                             Op.getOperand(0), DAG.getConstant(HiIdx, MVT::i32));
943   SDValue Hi1 = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
944                             Op.getOperand(1), DAG.getConstant(HiIdx, MVT::i32));
945   SDValue And0 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi0,
946                              DAG.getConstant(0x7fffffff, MVT::i32));
947   SDValue And1 = DAG.getNode(ISD::AND, dl, MVT::i32, Hi1,
948                              DAG.getConstant(0x80000000, MVT::i32));
949   SDValue Word1 = DAG.getNode(ISD::OR, dl, MVT::i32, And0, And1);
950
951   if (!isLittle)
952     std::swap(Word0, Word1);
953
954   return DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64, Word0, Word1);
955 }
956
957 SDValue MipsTargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG)
958   const {
959   EVT Ty = Op.getValueType();
960
961   assert(Ty == MVT::f32 || Ty == MVT::f64);
962
963   if (Ty == MVT::f32)
964     return LowerFCOPYSIGN32(Op, DAG);
965   else
966     return LowerFCOPYSIGN64(Op, DAG, Subtarget->isLittle());
967 }
968
969 //===----------------------------------------------------------------------===//
970 //                      Calling Convention Implementation
971 //===----------------------------------------------------------------------===//
972
973 #include "MipsGenCallingConv.inc"
974
975 //===----------------------------------------------------------------------===//
976 // TODO: Implement a generic logic using tblgen that can support this.
977 // Mips O32 ABI rules:
978 // ---
979 // i32 - Passed in A0, A1, A2, A3 and stack
980 // f32 - Only passed in f32 registers if no int reg has been used yet to hold
981 //       an argument. Otherwise, passed in A1, A2, A3 and stack.
982 // f64 - Only passed in two aliased f32 registers if no int reg has been used
983 //       yet to hold an argument. Otherwise, use A2, A3 and stack. If A1 is
984 //       not used, it must be shadowed. If only A3 is avaiable, shadow it and
985 //       go to stack.
986 //
987 //  For vararg functions, all arguments are passed in A0, A1, A2, A3 and stack.
988 //===----------------------------------------------------------------------===//
989
990 static bool CC_MipsO32(unsigned ValNo, MVT ValVT,
991                        MVT LocVT, CCValAssign::LocInfo LocInfo,
992                        ISD::ArgFlagsTy ArgFlags, CCState &State) {
993
994   static const unsigned IntRegsSize=4, FloatRegsSize=2;
995
996   static const unsigned IntRegs[] = {
997       Mips::A0, Mips::A1, Mips::A2, Mips::A3
998   };
999   static const unsigned F32Regs[] = {
1000       Mips::F12, Mips::F14
1001   };
1002   static const unsigned F64Regs[] = {
1003       Mips::D6, Mips::D7
1004   };
1005
1006   // ByVal Args
1007   if (ArgFlags.isByVal()) {
1008     State.HandleByVal(ValNo, ValVT, LocVT, LocInfo,
1009                       1 /*MinSize*/, 4 /*MinAlign*/, ArgFlags);
1010     unsigned NextReg = (State.getNextStackOffset() + 3) / 4;
1011     for (unsigned r = State.getFirstUnallocated(IntRegs, IntRegsSize);
1012          r < std::min(IntRegsSize, NextReg); ++r)
1013       State.AllocateReg(IntRegs[r]);
1014     return false;
1015   }
1016
1017   // Promote i8 and i16
1018   if (LocVT == MVT::i8 || LocVT == MVT::i16) {
1019     LocVT = MVT::i32;
1020     if (ArgFlags.isSExt())
1021       LocInfo = CCValAssign::SExt;
1022     else if (ArgFlags.isZExt())
1023       LocInfo = CCValAssign::ZExt;
1024     else
1025       LocInfo = CCValAssign::AExt;
1026   }
1027
1028   unsigned Reg;
1029
1030   // f32 and f64 are allocated in A0, A1, A2, A3 when either of the following
1031   // is true: function is vararg, argument is 3rd or higher, there is previous
1032   // argument which is not f32 or f64.
1033   bool AllocateFloatsInIntReg = State.isVarArg() || ValNo > 1
1034       || State.getFirstUnallocated(F32Regs, FloatRegsSize) != ValNo;
1035   unsigned OrigAlign = ArgFlags.getOrigAlign();
1036   bool isI64 = (ValVT == MVT::i32 && OrigAlign == 8);
1037
1038   if (ValVT == MVT::i32 || (ValVT == MVT::f32 && AllocateFloatsInIntReg)) {
1039     Reg = State.AllocateReg(IntRegs, IntRegsSize);
1040     // If this is the first part of an i64 arg,
1041     // the allocated register must be either A0 or A2.
1042     if (isI64 && (Reg == Mips::A1 || Reg == Mips::A3))
1043       Reg = State.AllocateReg(IntRegs, IntRegsSize);
1044     LocVT = MVT::i32;
1045   } else if (ValVT == MVT::f64 && AllocateFloatsInIntReg) {
1046     // Allocate int register and shadow next int register. If first
1047     // available register is Mips::A1 or Mips::A3, shadow it too.
1048     Reg = State.AllocateReg(IntRegs, IntRegsSize);
1049     if (Reg == Mips::A1 || Reg == Mips::A3)
1050       Reg = State.AllocateReg(IntRegs, IntRegsSize);
1051     State.AllocateReg(IntRegs, IntRegsSize);
1052     LocVT = MVT::i32;
1053   } else if (ValVT.isFloatingPoint() && !AllocateFloatsInIntReg) {
1054     // we are guaranteed to find an available float register
1055     if (ValVT == MVT::f32) {
1056       Reg = State.AllocateReg(F32Regs, FloatRegsSize);
1057       // Shadow int register
1058       State.AllocateReg(IntRegs, IntRegsSize);
1059     } else {
1060       Reg = State.AllocateReg(F64Regs, FloatRegsSize);
1061       // Shadow int registers
1062       unsigned Reg2 = State.AllocateReg(IntRegs, IntRegsSize);
1063       if (Reg2 == Mips::A1 || Reg2 == Mips::A3)
1064         State.AllocateReg(IntRegs, IntRegsSize);
1065       State.AllocateReg(IntRegs, IntRegsSize);
1066     }
1067   } else
1068     llvm_unreachable("Cannot handle this ValVT.");
1069
1070   unsigned SizeInBytes = ValVT.getSizeInBits() >> 3;
1071   unsigned Offset = State.AllocateStack(SizeInBytes, OrigAlign);
1072
1073   if (!Reg)
1074     State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
1075   else
1076     State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
1077
1078   return false; // CC must always match
1079 }
1080
1081 //===----------------------------------------------------------------------===//
1082 //                  Call Calling Convention Implementation
1083 //===----------------------------------------------------------------------===//
1084
1085 static const unsigned O32IntRegsSize = 4;
1086
1087 static const unsigned O32IntRegs[] = {
1088   Mips::A0, Mips::A1, Mips::A2, Mips::A3
1089 };
1090
1091 // Write ByVal Arg to arg registers and stack.
1092 static void
1093 WriteByValArg(SDValue& Chain, DebugLoc dl,
1094               SmallVector<std::pair<unsigned, SDValue>, 16>& RegsToPass,
1095               SmallVector<SDValue, 8>& MemOpChains, int& LastFI,
1096               MachineFrameInfo *MFI, SelectionDAG &DAG, SDValue Arg,
1097               const CCValAssign &VA, const ISD::ArgFlagsTy& Flags,
1098               MVT PtrType) {
1099   unsigned FirstWord = VA.getLocMemOffset() / 4;
1100   unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1101   unsigned LastWord = FirstWord + NumWords;
1102   unsigned CurWord;
1103
1104   // copy the first 4 words of byval arg to registers A0 - A3
1105   for (CurWord = FirstWord; CurWord < std::min(LastWord, O32IntRegsSize);
1106        ++CurWord) {
1107     SDValue LoadPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1108                                   DAG.getConstant((CurWord - FirstWord) * 4,
1109                                                   MVT::i32));
1110     SDValue LoadVal = DAG.getLoad(MVT::i32, dl, Chain, LoadPtr,
1111                                   MachinePointerInfo(),
1112                                   false, false, 0);
1113     MemOpChains.push_back(LoadVal.getValue(1));
1114     unsigned DstReg = O32IntRegs[CurWord];
1115     RegsToPass.push_back(std::make_pair(DstReg, LoadVal));
1116   }
1117
1118   // copy remaining part of byval arg to stack.
1119   if (CurWord < LastWord) {
1120     unsigned SizeInBytes = (LastWord - CurWord) * 4; 
1121     SDValue Src = DAG.getNode(ISD::ADD, dl, MVT::i32, Arg,
1122                               DAG.getConstant((CurWord - FirstWord) * 4,
1123                                               MVT::i32));
1124     LastFI = MFI->CreateFixedObject(SizeInBytes, CurWord * 4, true);
1125     SDValue Dst = DAG.getFrameIndex(LastFI, PtrType);
1126     Chain = DAG.getMemcpy(Chain, dl, Dst, Src,
1127                           DAG.getConstant(SizeInBytes, MVT::i32),
1128                           /*Align*/4,
1129                           /*isVolatile=*/false, /*AlwaysInline=*/false,
1130                           MachinePointerInfo(0), MachinePointerInfo(0));
1131     MemOpChains.push_back(Chain);
1132   }
1133 }
1134
1135 /// LowerCall - functions arguments are copied from virtual regs to
1136 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
1137 /// TODO: isTailCall.
1138 SDValue
1139 MipsTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1140                               CallingConv::ID CallConv, bool isVarArg,
1141                               bool &isTailCall,
1142                               const SmallVectorImpl<ISD::OutputArg> &Outs,
1143                               const SmallVectorImpl<SDValue> &OutVals,
1144                               const SmallVectorImpl<ISD::InputArg> &Ins,
1145                               DebugLoc dl, SelectionDAG &DAG,
1146                               SmallVectorImpl<SDValue> &InVals) const {
1147   // MIPs target does not yet support tail call optimization.
1148   isTailCall = false;
1149
1150   MachineFunction &MF = DAG.getMachineFunction();
1151   MachineFrameInfo *MFI = MF.getFrameInfo();
1152   const TargetFrameLowering *TFL = MF.getTarget().getFrameLowering();
1153   bool IsPIC = getTargetMachine().getRelocationModel() == Reloc::PIC_;
1154   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1155
1156   // Analyze operands of the call, assigning locations to each operand.
1157   SmallVector<CCValAssign, 16> ArgLocs;
1158   CCState CCInfo(CallConv, isVarArg, getTargetMachine(), ArgLocs,
1159                  *DAG.getContext());
1160
1161   if (Subtarget->isABI_O32())
1162     CCInfo.AnalyzeCallOperands(Outs, CC_MipsO32);
1163   else
1164     CCInfo.AnalyzeCallOperands(Outs, CC_Mips);
1165
1166   // Get a count of how many bytes are to be pushed on the stack.
1167   unsigned NumBytes = CCInfo.getNextStackOffset();
1168   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
1169
1170   // With EABI is it possible to have 16 args on registers.
1171   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
1172   SmallVector<SDValue, 8> MemOpChains;
1173
1174   MipsFI->setHasCall();
1175
1176   // If this is the first call, create a stack frame object that points to
1177   // a location to which .cprestore saves $gp. The offset of this frame object
1178   // is set to 0, since we know nothing about the size of the argument area at
1179   // this point.
1180   if (IsPIC && !MipsFI->getGPFI())
1181     MipsFI->setGPFI(MFI->CreateFixedObject(4, 0, true));
1182
1183   int FirstFI = -MFI->getNumFixedObjects() - 1, LastFI = 0; 
1184
1185   // Walk the register/memloc assignments, inserting copies/loads.
1186   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1187     SDValue Arg = OutVals[i];
1188     CCValAssign &VA = ArgLocs[i];
1189
1190     // Promote the value if needed.
1191     switch (VA.getLocInfo()) {
1192     default: llvm_unreachable("Unknown loc info!");
1193     case CCValAssign::Full:
1194       if (Subtarget->isABI_O32() && VA.isRegLoc()) {
1195         if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i32)
1196           Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
1197         if (VA.getValVT() == MVT::f64 && VA.getLocVT() == MVT::i32) {
1198           SDValue Lo = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1199                                    Arg, DAG.getConstant(0, MVT::i32));
1200           SDValue Hi = DAG.getNode(MipsISD::ExtractElementF64, dl, MVT::i32,
1201                                    Arg, DAG.getConstant(1, MVT::i32));
1202           if (!Subtarget->isLittle())
1203             std::swap(Lo, Hi);
1204           RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo));
1205           RegsToPass.push_back(std::make_pair(VA.getLocReg()+1, Hi));
1206           continue;
1207         }
1208       }
1209       break;
1210     case CCValAssign::SExt:
1211       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
1212       break;
1213     case CCValAssign::ZExt:
1214       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
1215       break;
1216     case CCValAssign::AExt:
1217       Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
1218       break;
1219     }
1220
1221     // Arguments that can be passed on register must be kept at
1222     // RegsToPass vector
1223     if (VA.isRegLoc()) {
1224       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1225       continue;
1226     }
1227
1228     // Register can't get to this point...
1229     assert(VA.isMemLoc());
1230
1231     // ByVal Arg. 
1232     ISD::ArgFlagsTy Flags = Outs[i].Flags;
1233     if (Flags.isByVal()) {
1234       assert(Subtarget->isABI_O32() &&
1235              "No support for ByVal args by ABIs other than O32 yet.");
1236       assert(Flags.getByValSize() &&
1237              "ByVal args of size 0 should have been ignored by front-end.");
1238       WriteByValArg(Chain, dl, RegsToPass, MemOpChains, LastFI, MFI, DAG, Arg,
1239                     VA, Flags, getPointerTy());
1240       continue;
1241     }
1242
1243     // Create the frame index object for this incoming parameter
1244     LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8, 
1245                                     VA.getLocMemOffset(), true);
1246     SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1247
1248     // emit ISD::STORE whichs stores the
1249     // parameter value to a stack Location
1250     MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
1251                                        MachinePointerInfo(),
1252                                        false, false, 0));
1253   }
1254
1255   // Transform all store nodes into one single node because all store
1256   // nodes are independent of each other.
1257   if (!MemOpChains.empty())
1258     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1259                         &MemOpChains[0], MemOpChains.size());
1260
1261   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
1262   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol
1263   // node so that legalize doesn't hack it.
1264   unsigned char OpFlag = IsPIC ? MipsII::MO_GOT_CALL : MipsII::MO_NO_FLAG;
1265   bool LoadSymAddr = false;
1266   SDValue CalleeLo;
1267
1268   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1269     if (IsPIC && G->getGlobal()->hasInternalLinkage()) {
1270       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1271                                           getPointerTy(), 0,MipsII:: MO_GOT);
1272       CalleeLo = DAG.getTargetGlobalAddress(G->getGlobal(), dl, getPointerTy(),
1273                                             0, MipsII::MO_ABS_LO);
1274     } else {
1275       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl,
1276                                           getPointerTy(), 0, OpFlag);
1277     }
1278
1279     LoadSymAddr = true;
1280   }
1281   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1282     Callee = DAG.getTargetExternalSymbol(S->getSymbol(),
1283                                 getPointerTy(), OpFlag);
1284     LoadSymAddr = true;
1285   }
1286
1287   SDValue InFlag;
1288
1289   // Create nodes that load address of callee and copy it to T9
1290   if (IsPIC) {
1291     if (LoadSymAddr) {
1292       // Load callee address
1293       SDValue LoadValue = DAG.getLoad(MVT::i32, dl, Chain, Callee,
1294                                       MachinePointerInfo::getGOT(),
1295                                       false, false, 0);
1296
1297       // Use GOT+LO if callee has internal linkage.
1298       if (CalleeLo.getNode()) {
1299         SDValue Lo = DAG.getNode(MipsISD::Lo, dl, MVT::i32, CalleeLo);
1300         Callee = DAG.getNode(ISD::ADD, dl, MVT::i32, LoadValue, Lo);
1301       } else
1302         Callee = LoadValue;
1303
1304       // Use chain output from LoadValue 
1305       Chain = LoadValue.getValue(1);
1306     }
1307
1308     // copy to T9
1309     Chain = DAG.getCopyToReg(Chain, dl, Mips::T9, Callee, SDValue(0, 0));
1310     InFlag = Chain.getValue(1);
1311     Callee = DAG.getRegister(Mips::T9, MVT::i32);
1312   }
1313
1314   // Build a sequence of copy-to-reg nodes chained together with token
1315   // chain and flag operands which copy the outgoing args into registers.
1316   // The InFlag in necessary since all emitted instructions must be
1317   // stuck together.
1318   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1319     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
1320                              RegsToPass[i].second, InFlag);
1321     InFlag = Chain.getValue(1);
1322   }
1323
1324   // MipsJmpLink = #chain, #target_address, #opt_in_flags...
1325   //             = Chain, Callee, Reg#1, Reg#2, ...
1326   //
1327   // Returns a chain & a flag for retval copy to use.
1328   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1329   SmallVector<SDValue, 8> Ops;
1330   Ops.push_back(Chain);
1331   Ops.push_back(Callee);
1332
1333   // Add argument registers to the end of the list so that they are
1334   // known live into the call.
1335   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1336     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1337                                   RegsToPass[i].second.getValueType()));
1338
1339   if (InFlag.getNode())
1340     Ops.push_back(InFlag);
1341
1342   Chain  = DAG.getNode(MipsISD::JmpLink, dl, NodeTys, &Ops[0], Ops.size());
1343   InFlag = Chain.getValue(1);
1344
1345   // Function can have an arbitrary number of calls, so
1346   // hold the LastArgStackLoc with the biggest offset.
1347   unsigned MaxCallFrameSize = MipsFI->getMaxCallFrameSize();
1348   unsigned NextStackOffset = CCInfo.getNextStackOffset();
1349
1350   // For O32, a minimum of four words (16 bytes) of argument space is
1351   // allocated.
1352   if (Subtarget->isABI_O32())
1353     NextStackOffset = std::max(NextStackOffset, (unsigned)16);
1354
1355   if (MaxCallFrameSize < NextStackOffset) {
1356     MipsFI->setMaxCallFrameSize(NextStackOffset);
1357
1358     if (IsPIC) {    
1359       // $gp restore slot must be aligned.
1360       unsigned StackAlignment = TFL->getStackAlignment();
1361       NextStackOffset = (NextStackOffset + StackAlignment - 1) / 
1362                         StackAlignment * StackAlignment;
1363       int GPFI = MipsFI->getGPFI();
1364       MFI->setObjectOffset(GPFI, NextStackOffset);
1365     }
1366   }
1367
1368   // Extend range of indices of frame objects for outgoing arguments that were
1369   // created during this function call. Skip this step if no such objects were
1370   // created.
1371   if (LastFI)
1372     MipsFI->extendOutArgFIRange(FirstFI, LastFI);
1373
1374   // Create the CALLSEQ_END node.
1375   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1376                              DAG.getIntPtrConstant(0, true), InFlag);
1377   InFlag = Chain.getValue(1);
1378
1379   // Handle result values, copying them out of physregs into vregs that we
1380   // return.
1381   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
1382                          Ins, dl, DAG, InVals);
1383 }
1384
1385 /// LowerCallResult - Lower the result values of a call into the
1386 /// appropriate copies out of appropriate physical registers.
1387 SDValue
1388 MipsTargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1389                                     CallingConv::ID CallConv, bool isVarArg,
1390                                     const SmallVectorImpl<ISD::InputArg> &Ins,
1391                                     DebugLoc dl, SelectionDAG &DAG,
1392                                     SmallVectorImpl<SDValue> &InVals) const {
1393
1394   // Assign locations to each value returned by this call.
1395   SmallVector<CCValAssign, 16> RVLocs;
1396   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1397                  RVLocs, *DAG.getContext());
1398
1399   CCInfo.AnalyzeCallResult(Ins, RetCC_Mips);
1400
1401   // Copy all of the result registers out of their specified physreg.
1402   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1403     Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
1404                                RVLocs[i].getValVT(), InFlag).getValue(1);
1405     InFlag = Chain.getValue(2);
1406     InVals.push_back(Chain.getValue(0));
1407   }
1408
1409   return Chain;
1410 }
1411
1412 //===----------------------------------------------------------------------===//
1413 //             Formal Arguments Calling Convention Implementation
1414 //===----------------------------------------------------------------------===//
1415 static void ReadByValArg(MachineFunction &MF, SDValue Chain, DebugLoc dl,
1416                          std::vector<SDValue>& OutChains,
1417                          SelectionDAG &DAG, unsigned NumWords, SDValue FIN,
1418                          const CCValAssign &VA, const ISD::ArgFlagsTy& Flags) {
1419   unsigned LocMem = VA.getLocMemOffset();
1420   unsigned FirstWord = LocMem / 4;
1421
1422   // copy register A0 - A3 to frame object
1423   for (unsigned i = 0; i < NumWords; ++i) {
1424     unsigned CurWord = FirstWord + i;
1425     if (CurWord >= O32IntRegsSize)
1426       break;
1427
1428     unsigned SrcReg = O32IntRegs[CurWord];
1429     unsigned Reg = AddLiveIn(MF, SrcReg, Mips::CPURegsRegisterClass);
1430     SDValue StorePtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIN,
1431                                    DAG.getConstant(i * 4, MVT::i32));
1432     SDValue Store = DAG.getStore(Chain, dl, DAG.getRegister(Reg, MVT::i32),
1433                                  StorePtr, MachinePointerInfo(), false,
1434                                  false, 0);
1435     OutChains.push_back(Store);
1436   }
1437 }
1438
1439 /// LowerFormalArguments - transform physical registers into virtual registers
1440 /// and generate load operations for arguments places on the stack.
1441 SDValue
1442 MipsTargetLowering::LowerFormalArguments(SDValue Chain,
1443                                          CallingConv::ID CallConv,
1444                                          bool isVarArg,
1445                                          const SmallVectorImpl<ISD::InputArg>
1446                                          &Ins,
1447                                          DebugLoc dl, SelectionDAG &DAG,
1448                                          SmallVectorImpl<SDValue> &InVals)
1449                                           const {
1450   MachineFunction &MF = DAG.getMachineFunction();
1451   MachineFrameInfo *MFI = MF.getFrameInfo();
1452   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1453
1454   MipsFI->setVarArgsFrameIndex(0);
1455
1456   // Used with vargs to acumulate store chains.
1457   std::vector<SDValue> OutChains;
1458
1459   // Assign locations to all of the incoming arguments.
1460   SmallVector<CCValAssign, 16> ArgLocs;
1461   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1462                  ArgLocs, *DAG.getContext());
1463
1464   if (Subtarget->isABI_O32())
1465     CCInfo.AnalyzeFormalArguments(Ins, CC_MipsO32);
1466   else
1467     CCInfo.AnalyzeFormalArguments(Ins, CC_Mips);
1468
1469   int LastFI = 0;// MipsFI->LastInArgFI is 0 at the entry of this function.
1470
1471   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1472     CCValAssign &VA = ArgLocs[i];
1473
1474     // Arguments stored on registers
1475     if (VA.isRegLoc()) {
1476       EVT RegVT = VA.getLocVT();
1477       unsigned ArgReg = VA.getLocReg();
1478       TargetRegisterClass *RC = 0;
1479
1480       if (RegVT == MVT::i32)
1481         RC = Mips::CPURegsRegisterClass;
1482       else if (RegVT == MVT::f32)
1483         RC = Mips::FGR32RegisterClass;
1484       else if (RegVT == MVT::f64) {
1485         if (!Subtarget->isSingleFloat())
1486           RC = Mips::AFGR64RegisterClass;
1487       } else
1488         llvm_unreachable("RegVT not supported by FormalArguments Lowering");
1489
1490       // Transform the arguments stored on
1491       // physical registers into virtual ones
1492       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), ArgReg, RC);
1493       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
1494
1495       // If this is an 8 or 16-bit value, it has been passed promoted
1496       // to 32 bits.  Insert an assert[sz]ext to capture this, then
1497       // truncate to the right size.
1498       if (VA.getLocInfo() != CCValAssign::Full) {
1499         unsigned Opcode = 0;
1500         if (VA.getLocInfo() == CCValAssign::SExt)
1501           Opcode = ISD::AssertSext;
1502         else if (VA.getLocInfo() == CCValAssign::ZExt)
1503           Opcode = ISD::AssertZext;
1504         if (Opcode)
1505           ArgValue = DAG.getNode(Opcode, dl, RegVT, ArgValue,
1506                                  DAG.getValueType(VA.getValVT()));
1507         ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
1508       }
1509
1510       // Handle O32 ABI cases: i32->f32 and (i32,i32)->f64
1511       if (Subtarget->isABI_O32()) {
1512         if (RegVT == MVT::i32 && VA.getValVT() == MVT::f32)
1513           ArgValue = DAG.getNode(ISD::BITCAST, dl, MVT::f32, ArgValue);
1514         if (RegVT == MVT::i32 && VA.getValVT() == MVT::f64) {
1515           unsigned Reg2 = AddLiveIn(DAG.getMachineFunction(),
1516                                     VA.getLocReg()+1, RC);
1517           SDValue ArgValue2 = DAG.getCopyFromReg(Chain, dl, Reg2, RegVT);
1518           if (!Subtarget->isLittle())
1519             std::swap(ArgValue, ArgValue2);
1520           ArgValue = DAG.getNode(MipsISD::BuildPairF64, dl, MVT::f64,
1521                                  ArgValue, ArgValue2);
1522         }
1523       }
1524
1525       InVals.push_back(ArgValue);
1526     } else { // VA.isRegLoc()
1527
1528       // sanity check
1529       assert(VA.isMemLoc());
1530
1531       ISD::ArgFlagsTy Flags = Ins[i].Flags;
1532
1533       if (Flags.isByVal()) {
1534         assert(Subtarget->isABI_O32() &&
1535                "No support for ByVal args by ABIs other than O32 yet.");
1536         assert(Flags.getByValSize() &&
1537                "ByVal args of size 0 should have been ignored by front-end.");
1538         unsigned NumWords = (Flags.getByValSize() + 3) / 4;
1539         LastFI = MFI->CreateFixedObject(NumWords * 4, VA.getLocMemOffset(),
1540                                         true);
1541         SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1542         InVals.push_back(FIN);
1543         ReadByValArg(MF, Chain, dl, OutChains, DAG, NumWords, FIN, VA, Flags);
1544
1545         continue;
1546       }
1547
1548       // The stack pointer offset is relative to the caller stack frame.
1549       LastFI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
1550                                       VA.getLocMemOffset(), true);
1551
1552       // Create load nodes to retrieve arguments from the stack
1553       SDValue FIN = DAG.getFrameIndex(LastFI, getPointerTy());
1554       InVals.push_back(DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
1555                                    MachinePointerInfo::getFixedStack(LastFI),
1556                                    false, false, 0));
1557     }
1558   }
1559
1560   // The mips ABIs for returning structs by value requires that we copy
1561   // the sret argument into $v0 for the return. Save the argument into
1562   // a virtual register so that we can access it from the return points.
1563   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1564     unsigned Reg = MipsFI->getSRetReturnReg();
1565     if (!Reg) {
1566       Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
1567       MipsFI->setSRetReturnReg(Reg);
1568     }
1569     SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
1570     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
1571   }
1572
1573   if (isVarArg && Subtarget->isABI_O32()) {
1574     // Record the frame index of the first variable argument
1575     // which is a value necessary to VASTART.    
1576     unsigned NextStackOffset = CCInfo.getNextStackOffset();
1577     assert(NextStackOffset % 4 == 0 &&
1578            "NextStackOffset must be aligned to 4-byte boundaries.");
1579     LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1580     MipsFI->setVarArgsFrameIndex(LastFI);
1581
1582     // If NextStackOffset is smaller than o32's 16-byte reserved argument area,
1583     // copy the integer registers that have not been used for argument passing
1584     // to the caller's stack frame.
1585     for (; NextStackOffset < 16; NextStackOffset += 4) {
1586       TargetRegisterClass *RC = Mips::CPURegsRegisterClass;
1587       unsigned Idx = NextStackOffset / 4;
1588       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), O32IntRegs[Idx], RC);
1589       SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, MVT::i32);
1590       LastFI = MFI->CreateFixedObject(4, NextStackOffset, true);
1591       SDValue PtrOff = DAG.getFrameIndex(LastFI, getPointerTy());
1592       OutChains.push_back(DAG.getStore(Chain, dl, ArgValue, PtrOff,
1593                                        MachinePointerInfo(),
1594                                        false, false, 0));
1595     }
1596   }
1597
1598   MipsFI->setLastInArgFI(LastFI);
1599
1600   // All stores are grouped in one node to allow the matching between
1601   // the size of Ins and InVals. This only happens when on varg functions
1602   if (!OutChains.empty()) {
1603     OutChains.push_back(Chain);
1604     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
1605                         &OutChains[0], OutChains.size());
1606   }
1607
1608   return Chain;
1609 }
1610
1611 //===----------------------------------------------------------------------===//
1612 //               Return Value Calling Convention Implementation
1613 //===----------------------------------------------------------------------===//
1614
1615 SDValue
1616 MipsTargetLowering::LowerReturn(SDValue Chain,
1617                                 CallingConv::ID CallConv, bool isVarArg,
1618                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
1619                                 const SmallVectorImpl<SDValue> &OutVals,
1620                                 DebugLoc dl, SelectionDAG &DAG) const {
1621
1622   // CCValAssign - represent the assignment of
1623   // the return value to a location
1624   SmallVector<CCValAssign, 16> RVLocs;
1625
1626   // CCState - Info about the registers and stack slot.
1627   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1628                  RVLocs, *DAG.getContext());
1629
1630   // Analize return values.
1631   CCInfo.AnalyzeReturn(Outs, RetCC_Mips);
1632
1633   // If this is the first return lowered for this function, add
1634   // the regs to the liveout set for the function.
1635   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
1636     for (unsigned i = 0; i != RVLocs.size(); ++i)
1637       if (RVLocs[i].isRegLoc())
1638         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1639   }
1640
1641   SDValue Flag;
1642
1643   // Copy the result values into the output registers.
1644   for (unsigned i = 0; i != RVLocs.size(); ++i) {
1645     CCValAssign &VA = RVLocs[i];
1646     assert(VA.isRegLoc() && "Can only return in registers!");
1647
1648     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
1649                              OutVals[i], Flag);
1650
1651     // guarantee that all emitted copies are
1652     // stuck together, avoiding something bad
1653     Flag = Chain.getValue(1);
1654   }
1655
1656   // The mips ABIs for returning structs by value requires that we copy
1657   // the sret argument into $v0 for the return. We saved the argument into
1658   // a virtual register in the entry block, so now we copy the value out
1659   // and into $v0.
1660   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1661     MachineFunction &MF      = DAG.getMachineFunction();
1662     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
1663     unsigned Reg = MipsFI->getSRetReturnReg();
1664
1665     if (!Reg)
1666       llvm_unreachable("sret virtual register not created in the entry block");
1667     SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
1668
1669     Chain = DAG.getCopyToReg(Chain, dl, Mips::V0, Val, Flag);
1670     Flag = Chain.getValue(1);
1671   }
1672
1673   // Return on Mips is always a "jr $ra"
1674   if (Flag.getNode())
1675     return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
1676                        Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
1677   else // Return Void
1678     return DAG.getNode(MipsISD::Ret, dl, MVT::Other,
1679                        Chain, DAG.getRegister(Mips::RA, MVT::i32));
1680 }
1681
1682 //===----------------------------------------------------------------------===//
1683 //                           Mips Inline Assembly Support
1684 //===----------------------------------------------------------------------===//
1685
1686 /// getConstraintType - Given a constraint letter, return the type of
1687 /// constraint it is for this target.
1688 MipsTargetLowering::ConstraintType MipsTargetLowering::
1689 getConstraintType(const std::string &Constraint) const
1690 {
1691   // Mips specific constrainy
1692   // GCC config/mips/constraints.md
1693   //
1694   // 'd' : An address register. Equivalent to r
1695   //       unless generating MIPS16 code.
1696   // 'y' : Equivalent to r; retained for
1697   //       backwards compatibility.
1698   // 'f' : Floating Point registers.
1699   if (Constraint.size() == 1) {
1700     switch (Constraint[0]) {
1701       default : break;
1702       case 'd':
1703       case 'y':
1704       case 'f':
1705         return C_RegisterClass;
1706         break;
1707     }
1708   }
1709   return TargetLowering::getConstraintType(Constraint);
1710 }
1711
1712 /// Examine constraint type and operand type and determine a weight value.
1713 /// This object must already have been set up with the operand type
1714 /// and the current alternative constraint selected.
1715 TargetLowering::ConstraintWeight
1716 MipsTargetLowering::getSingleConstraintMatchWeight(
1717     AsmOperandInfo &info, const char *constraint) const {
1718   ConstraintWeight weight = CW_Invalid;
1719   Value *CallOperandVal = info.CallOperandVal;
1720     // If we don't have a value, we can't do a match,
1721     // but allow it at the lowest weight.
1722   if (CallOperandVal == NULL)
1723     return CW_Default;
1724   const Type *type = CallOperandVal->getType();
1725   // Look at the constraint type.
1726   switch (*constraint) {
1727   default:
1728     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
1729     break;
1730   case 'd':
1731   case 'y':
1732     if (type->isIntegerTy())
1733       weight = CW_Register;
1734     break;
1735   case 'f':
1736     if (type->isFloatTy())
1737       weight = CW_Register;
1738     break;
1739   }
1740   return weight;
1741 }
1742
1743 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1744 /// return a list of registers that can be used to satisfy the constraint.
1745 /// This should only be used for C_RegisterClass constraints.
1746 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
1747 getRegForInlineAsmConstraint(const std::string &Constraint, EVT VT) const
1748 {
1749   if (Constraint.size() == 1) {
1750     switch (Constraint[0]) {
1751     case 'r':
1752       return std::make_pair(0U, Mips::CPURegsRegisterClass);
1753     case 'f':
1754       if (VT == MVT::f32)
1755         return std::make_pair(0U, Mips::FGR32RegisterClass);
1756       if (VT == MVT::f64)
1757         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1758           return std::make_pair(0U, Mips::AFGR64RegisterClass);
1759     }
1760   }
1761   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1762 }
1763
1764 /// Given a register class constraint, like 'r', if this corresponds directly
1765 /// to an LLVM register class, return a register of 0 and the register class
1766 /// pointer.
1767 std::vector<unsigned> MipsTargetLowering::
1768 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1769                                   EVT VT) const
1770 {
1771   if (Constraint.size() != 1)
1772     return std::vector<unsigned>();
1773
1774   switch (Constraint[0]) {
1775     default : break;
1776     case 'r':
1777     // GCC Mips Constraint Letters
1778     case 'd':
1779     case 'y':
1780       return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3,
1781              Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1,
1782              Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7,
1783              Mips::T8, 0);
1784
1785     case 'f':
1786       if (VT == MVT::f32) {
1787         if (Subtarget->isSingleFloat())
1788           return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1789                  Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1790                  Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1791                  Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1792                  Mips::F30, Mips::F31, 0);
1793         else
1794           return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8,
1795                  Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26,
1796                  Mips::F28, Mips::F30, 0);
1797       }
1798
1799       if (VT == MVT::f64)
1800         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1801           return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4,
1802                  Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13,
1803                  Mips::D14, Mips::D15, 0);
1804   }
1805   return std::vector<unsigned>();
1806 }
1807
1808 bool
1809 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1810   // The Mips target isn't yet aware of offsets.
1811   return false;
1812 }
1813
1814 bool MipsTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
1815   if (VT != MVT::f32 && VT != MVT::f64)
1816     return false;
1817   if (Imm.isNegZero())
1818     return false;
1819   return Imm.isZero();
1820 }