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