Changed some methods order.
[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
17 #include "MipsISelLowering.h"
18 #include "MipsMachineFunction.h"
19 #include "MipsTargetMachine.h"
20 #include "MipsSubtarget.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Intrinsics.h"
25 #include "llvm/CallingConv.h"
26 #include "llvm/CodeGen/CallingConvLower.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/SelectionDAGISel.h"
32 #include "llvm/CodeGen/ValueTypes.h"
33 #include "llvm/Support/Debug.h"
34 #include <queue>
35 #include <set>
36
37 using namespace llvm;
38
39 const char *MipsTargetLowering::
40 getTargetNodeName(unsigned Opcode) const 
41 {
42   switch (Opcode) 
43   {
44     case MipsISD::JmpLink    : return "MipsISD::JmpLink";
45     case MipsISD::Hi         : return "MipsISD::Hi";
46     case MipsISD::Lo         : return "MipsISD::Lo";
47     case MipsISD::GPRel      : return "MipsISD::GPRel";
48     case MipsISD::Ret        : return "MipsISD::Ret";
49     case MipsISD::SelectCC   : return "MipsISD::SelectCC";
50     case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
51     case MipsISD::FPBrcond   : return "MipsISD::FPBrcond";
52     case MipsISD::FPCmp      : return "MipsISD::FPCmp";
53     default                  : return NULL;
54   }
55 }
56
57 MipsTargetLowering::
58 MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM) 
59 {
60   Subtarget = &TM.getSubtarget<MipsSubtarget>();
61
62   // Mips does not have i1 type, so use i32 for
63   // setcc operations results (slt, sgt, ...). 
64   setSetCCResultContents(ZeroOrOneSetCCResult);
65
66   // JumpTable targets must use GOT when using PIC_
67   setUsesGlobalOffsetTable(true);
68
69   // Set up the register classes
70   addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
71
72   // When dealing with single precision only, use libcalls
73   if (!Subtarget->isSingleFloat()) {
74     addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
75     if (!Subtarget->isFP64bit())
76       addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
77   } else 
78     addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
79
80   // Load extented operations for i1 types must be promoted 
81   setLoadXAction(ISD::EXTLOAD,  MVT::i1,  Promote);
82   setLoadXAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
83   setLoadXAction(ISD::SEXTLOAD, MVT::i1,  Promote);
84
85   // Mips Custom Operations
86   setOperationAction(ISD::GlobalAddress,    MVT::i32,   Custom);
87   setOperationAction(ISD::GlobalTLSAddress, MVT::i32,   Custom);
88   setOperationAction(ISD::RET,              MVT::Other, Custom);
89   setOperationAction(ISD::JumpTable,        MVT::i32,   Custom);
90   setOperationAction(ISD::ConstantPool,     MVT::i32,   Custom);
91   setOperationAction(ISD::SELECT,           MVT::f32,   Custom);
92   setOperationAction(ISD::SELECT,           MVT::i32,   Custom);
93   setOperationAction(ISD::SELECT_CC,        MVT::i32,   Custom);
94   setOperationAction(ISD::SETCC,            MVT::f32,   Custom);
95   setOperationAction(ISD::BRCOND,           MVT::Other, Custom);
96
97   // Operations not directly supported by Mips.
98   setOperationAction(ISD::BR_JT,             MVT::Other, Expand);
99   setOperationAction(ISD::BR_CC,             MVT::Other, Expand);
100   setOperationAction(ISD::SELECT_CC,         MVT::Other, Expand);
101   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
102   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
103   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
104   setOperationAction(ISD::CTPOP,             MVT::i32,   Expand);
105   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
106   setOperationAction(ISD::CTLZ,              MVT::i32,   Expand);
107   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
108   setOperationAction(ISD::ROTR,              MVT::i32,   Expand);
109   setOperationAction(ISD::BSWAP,             MVT::i32,   Expand);
110   setOperationAction(ISD::SHL_PARTS,         MVT::i32,   Expand);
111   setOperationAction(ISD::SRA_PARTS,         MVT::i32,   Expand);
112   setOperationAction(ISD::SRL_PARTS,         MVT::i32,   Expand);
113
114   // We don't have line number support yet.
115   setOperationAction(ISD::DBG_STOPPOINT,     MVT::Other, Expand);
116   setOperationAction(ISD::DEBUG_LOC,         MVT::Other, Expand);
117   setOperationAction(ISD::DBG_LABEL,         MVT::Other, Expand);
118   setOperationAction(ISD::EH_LABEL,          MVT::Other, Expand);
119
120   // Use the default for now
121   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
122   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
123   setOperationAction(ISD::MEMBARRIER,        MVT::Other, Expand);
124
125   if (Subtarget->isSingleFloat()) 
126     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
127
128   if (!Subtarget->hasSEInReg()) {
129     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
130     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
131   }
132
133   setStackPointerRegisterToSaveRestore(Mips::SP);
134   computeRegisterProperties();
135 }
136
137
138 MVT MipsTargetLowering::getSetCCResultType(const SDValue &) const {
139   return MVT::i32;
140 }
141
142
143 SDValue MipsTargetLowering::
144 LowerOperation(SDValue Op, SelectionDAG &DAG) 
145 {
146   switch (Op.getOpcode()) 
147   {
148     case ISD::BRCOND:           return LowerBRCOND(Op, DAG);
149     case ISD::CALL:             return LowerCALL(Op, DAG);
150     case ISD::ConstantPool:     return LowerConstantPool(Op, DAG);
151     case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
152     case ISD::GlobalAddress:    return LowerGlobalAddress(Op, DAG);
153     case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
154     case ISD::JumpTable:        return LowerJumpTable(Op, DAG);
155     case ISD::RET:              return LowerRET(Op, DAG);
156     case ISD::SELECT:           return LowerSELECT(Op, DAG);
157     case ISD::SELECT_CC:        return LowerSELECT_CC(Op, DAG);
158     case ISD::SETCC:            return LowerSETCC(Op, DAG);
159   }
160   return SDValue();
161 }
162
163 //===----------------------------------------------------------------------===//
164 //  Lower helper functions
165 //===----------------------------------------------------------------------===//
166
167 // AddLiveIn - This helper function adds the specified physical register to the
168 // MachineFunction as a live in value.  It also creates a corresponding
169 // virtual register for it.
170 static unsigned
171 AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC) 
172 {
173   assert(RC->contains(PReg) && "Not the correct regclass!");
174   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
175   MF.getRegInfo().addLiveIn(PReg, VReg);
176   return VReg;
177 }
178
179 // A address must be loaded from a small section if its size is less than the 
180 // small section size threshold. Data in this section must be addressed using 
181 // gp_rel operator.
182 bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
183   return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
184 }
185
186 // Discover if this global address can be placed into small data/bss section. 
187 bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
188 {
189   const TargetData *TD = getTargetData();
190   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
191
192   if (!GVA)
193     return false;
194   
195   const Type *Ty = GV->getType()->getElementType();
196   unsigned Size = TD->getABITypeSize(Ty);
197
198   // if this is a internal constant string, there is a special
199   // section for it, but not in small data/bss.
200   if (GVA->hasInitializer() && GV->hasInternalLinkage()) {
201     Constant *C = GVA->getInitializer();
202     const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
203     if (CVA && CVA->isCString()) 
204       return false;
205   }
206
207   return IsInSmallSection(Size);
208 }
209
210 // Get fp branch code (not opcode) from condition code.
211 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
212   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
213     return Mips::BRANCH_T;
214
215   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
216     return Mips::BRANCH_F;
217
218   return Mips::BRANCH_INVALID;
219 }
220   
221 static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
222   switch(BC) {
223     default:
224       assert(0 && "Unknown branch code");
225     case Mips::BRANCH_T  : return Mips::BC1T;
226     case Mips::BRANCH_F  : return Mips::BC1F;
227     case Mips::BRANCH_TL : return Mips::BC1TL;
228     case Mips::BRANCH_FL : return Mips::BC1FL;
229   }
230 }
231
232 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
233   switch (CC) {
234   default: assert(0 && "Unknown fp condition code!");
235   case ISD::SETEQ:  
236   case ISD::SETOEQ: return Mips::FCOND_EQ;
237   case ISD::SETUNE: return Mips::FCOND_OGL;
238   case ISD::SETLT:  
239   case ISD::SETOLT: return Mips::FCOND_OLT;
240   case ISD::SETGT:  
241   case ISD::SETOGT: return Mips::FCOND_OGT;
242   case ISD::SETLE:  
243   case ISD::SETOLE: return Mips::FCOND_OLE; 
244   case ISD::SETGE:
245   case ISD::SETOGE: return Mips::FCOND_OGE;
246   case ISD::SETULT: return Mips::FCOND_ULT;
247   case ISD::SETULE: return Mips::FCOND_ULE; 
248   case ISD::SETUGT: return Mips::FCOND_UGT;
249   case ISD::SETUGE: return Mips::FCOND_UGE;
250   case ISD::SETUO:  return Mips::FCOND_UN; 
251   case ISD::SETO:   return Mips::FCOND_OR;
252   case ISD::SETNE:  
253   case ISD::SETONE: return Mips::FCOND_NEQ;
254   case ISD::SETUEQ: return Mips::FCOND_UEQ;
255   }
256 }
257
258 MachineBasicBlock *
259 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
260                                                 MachineBasicBlock *BB) 
261 {
262   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
263   bool isFPCmp = false;
264
265   switch (MI->getOpcode()) {
266   default: assert(false && "Unexpected instr type to insert");
267   case Mips::Select_FCC:
268   case Mips::Select_FCC_SO32:
269   case Mips::Select_FCC_AS32:
270   case Mips::Select_FCC_D32:
271     isFPCmp = true; // FALL THROUGH
272   case Mips::Select_CC:
273   case Mips::Select_CC_SO32:
274   case Mips::Select_CC_AS32:
275   case Mips::Select_CC_D32: {
276     // To "insert" a SELECT_CC instruction, we actually have to insert the
277     // diamond control-flow pattern.  The incoming instruction knows the
278     // destination vreg to set, the condition code register to branch on, the
279     // true/false values to select between, and a branch opcode to use.
280     const BasicBlock *LLVM_BB = BB->getBasicBlock();
281     MachineFunction::iterator It = BB;
282     ++It;
283
284     //  thisMBB:
285     //  ...
286     //   TrueVal = ...
287     //   setcc r1, r2, r3
288     //   bNE   r1, r0, copy1MBB
289     //   fallthrough --> copy0MBB
290     MachineBasicBlock *thisMBB  = BB;
291     MachineFunction *F = BB->getParent();
292     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
293     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
294
295     // Emit the right instruction according to the type of the operands compared
296     if (isFPCmp) {
297       // Find the condiction code present in the setcc operation.
298       Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
299       // Get the branch opcode from the branch code.
300       unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
301       BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
302     } else
303       BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
304         .addReg(Mips::ZERO).addMBB(sinkMBB);
305
306     F->insert(It, copy0MBB);
307     F->insert(It, sinkMBB);
308     // Update machine-CFG edges by first adding all successors of the current
309     // block to the new block which will contain the Phi node for the select.
310     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
311         e = BB->succ_end(); i != e; ++i)
312       sinkMBB->addSuccessor(*i);
313     // Next, remove all successors of the current block, and add the true
314     // and fallthrough blocks as its successors.
315     while(!BB->succ_empty())
316       BB->removeSuccessor(BB->succ_begin());
317     BB->addSuccessor(copy0MBB);
318     BB->addSuccessor(sinkMBB);
319
320     //  copy0MBB:
321     //   %FalseValue = ...
322     //   # fallthrough to sinkMBB
323     BB = copy0MBB;
324
325     // Update machine-CFG edges
326     BB->addSuccessor(sinkMBB);
327
328     //  sinkMBB:
329     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
330     //  ...
331     BB = sinkMBB;
332     BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
333       .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
334       .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
335
336     F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
337     return BB;
338   }
339   }
340 }
341
342 //===----------------------------------------------------------------------===//
343 //  Misc Lower Operation implementation
344 //===----------------------------------------------------------------------===//
345
346 SDValue MipsTargetLowering::
347 LowerBRCOND(SDValue Op, SelectionDAG &DAG)
348 {
349   // The first operand is the chain, the second is the condition, the third is 
350   // the block to branch to if the condition is true.
351   SDValue Chain = Op.getOperand(0);
352   SDValue Dest = Op.getOperand(2);
353   SDValue CondRes; 
354
355   if (Op.getOperand(1).getOpcode() == ISD::AND)
356     CondRes = Op.getOperand(1).getOperand(0);
357   else if (Op.getOperand(1).getOpcode() == MipsISD::FPCmp)
358     CondRes = Op.getOperand(1);
359   else
360     assert(0 && "Incoming condition flag unknown");
361   
362   SDValue CCNode = CondRes.getOperand(2);
363   Mips::CondCode CC = (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getValue();
364   SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32); 
365
366   return DAG.getNode(MipsISD::FPBrcond, Op.getValueType(), Chain, BrCode, 
367              Dest, CondRes);
368 }
369
370 SDValue MipsTargetLowering::
371 LowerSETCC(SDValue Op, SelectionDAG &DAG)
372 {
373   // The operands to this are the left and right operands to compare (ops #0, 
374   // and #1) and the condition code to compare them with (op #2) as a 
375   // CondCodeSDNode.
376   SDValue LHS = Op.getOperand(0); 
377   SDValue RHS = Op.getOperand(1); 
378
379   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
380   
381   return DAG.getNode(MipsISD::FPCmp, Op.getValueType(), LHS, RHS, 
382                  DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
383 }
384
385 SDValue MipsTargetLowering::
386 LowerSELECT(SDValue Op, SelectionDAG &DAG) 
387 {
388   SDValue Cond  = Op.getOperand(0); 
389   SDValue True  = Op.getOperand(1);
390   SDValue False = Op.getOperand(2);
391
392   // this can be a fp select but with a setcc comming from a 
393   // integer compare.
394   if (Cond.getOpcode() == ISD::SETCC)
395     if (Cond.getOperand(0).getValueType().isInteger())
396       return DAG.getNode(MipsISD::SelectCC, True.getValueType(), 
397                          Cond, True, False);
398
399   // Otherwise we're dealing with floating point compare.
400   SDValue CondRes;
401   if (Cond.getOpcode() == ISD::AND)
402     CondRes = Cond.getOperand(0);
403   else if (Cond.getOpcode() == MipsISD::FPCmp)
404     CondRes = Cond;
405   else
406     assert(0 && "Incoming condition flag unknown");
407   
408   SDValue CCNode = CondRes.getOperand(2);
409   return DAG.getNode(MipsISD::FPSelectCC, True.getValueType(), 
410                      CondRes, True, False, CCNode);
411 }
412
413 SDValue MipsTargetLowering::
414 LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) 
415 {
416   SDValue LHS   = Op.getOperand(0); 
417   SDValue RHS   = Op.getOperand(1); 
418   SDValue True  = Op.getOperand(2);
419   SDValue False = Op.getOperand(3);
420   SDValue CC    = Op.getOperand(4);
421
422   SDValue SetCCRes = DAG.getNode(ISD::SETCC, LHS.getValueType(), LHS, RHS, CC);
423   return DAG.getNode(MipsISD::SelectCC, True.getValueType(), 
424                      SetCCRes, True, False);
425 }
426
427 SDValue MipsTargetLowering::
428 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) 
429 {
430   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
431   SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
432
433   if (!Subtarget->hasABICall()) {
434     if (isa<Function>(GV)) return GA;
435     const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
436     SDValue Ops[] = { GA };
437
438     if (IsGlobalInSmallSection(GV)) { // %gp_rel relocation
439       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1);
440       SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
441       return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); 
442     }
443     // %hi/%lo relocation
444     SDValue HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
445     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
446     return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
447
448   } else { // Abicall relocations, TODO: make this cleaner.
449     SDValue ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
450     // On functions and global targets not internal linked only
451     // a load from got/GP is necessary for PIC to work.
452     if (!GV->hasInternalLinkage() || isa<Function>(GV))
453       return ResNode;
454     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
455     return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo);
456   }
457
458   assert(0 && "Dont know how to handle GlobalAddress");
459   return SDValue(0,0);
460 }
461
462 SDValue MipsTargetLowering::
463 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
464 {
465   assert(0 && "TLS not implemented for MIPS.");
466   return SDValue(); // Not reached
467 }
468
469 SDValue MipsTargetLowering::
470 LowerJumpTable(SDValue Op, SelectionDAG &DAG) 
471 {
472   SDValue ResNode;
473   SDValue HiPart; 
474
475   MVT PtrVT = Op.getValueType();
476   JumpTableSDNode *JT  = cast<JumpTableSDNode>(Op);
477   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
478
479   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
480     const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
481     SDValue Ops[] = { JTI };
482     HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
483   } else // Emit Load from Global Pointer
484     HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
485
486   SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
487   ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
488
489   return ResNode;
490 }
491
492 SDValue MipsTargetLowering::
493 LowerConstantPool(SDValue Op, SelectionDAG &DAG) 
494 {
495   SDValue ResNode;
496   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
497   Constant *C = N->getConstVal();
498   SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
499
500   // gp_rel relocation
501   // FIXME: we should reference the constant pool using small data sections, 
502   // but the asm printer currently doens't support this feature without
503   // hacking it. This feature should come soon so we can uncomment the 
504   // stuff below.
505   //if (!Subtarget->hasABICall() &&  
506   //    IsInSmallSection(getTargetData()->getABITypeSize(C->getType()))) {
507   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
508   //  SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
509   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); 
510   //} else { // %hi/%lo relocation
511     SDValue HiPart = DAG.getNode(MipsISD::Hi, MVT::i32, CP);
512     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, CP);
513     ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
514   //}
515
516   return ResNode;
517 }
518
519 //===----------------------------------------------------------------------===//
520 //                      Calling Convention Implementation
521 //
522 //  The lower operations present on calling convention works on this order:
523 //      LowerCALL (virt regs --> phys regs, virt regs --> stack) 
524 //      LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
525 //      LowerRET (virt regs --> phys regs)
526 //      LowerCALL (phys regs --> virt regs)
527 //
528 //===----------------------------------------------------------------------===//
529
530 #include "MipsGenCallingConv.inc"
531
532 //===----------------------------------------------------------------------===//
533 //                  CALL Calling Convention Implementation
534 //===----------------------------------------------------------------------===//
535
536 /// Mips custom CALL implementation
537 SDValue MipsTargetLowering::
538 LowerCALL(SDValue Op, SelectionDAG &DAG)
539 {
540   unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
541
542   // By now, only CallingConv::C implemented
543   switch (CallingConv) {
544     default:
545       assert(0 && "Unsupported calling convention");
546     case CallingConv::Fast:
547     case CallingConv::C:
548       return LowerCCCCallTo(Op, DAG, CallingConv);
549   }
550 }
551
552 /// LowerCCCCallTo - functions arguments are copied from virtual
553 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
554 /// CALLSEQ_END are emitted.
555 /// TODO: isVarArg, isTailCall.
556 SDValue MipsTargetLowering::
557 LowerCCCCallTo(SDValue Op, SelectionDAG &DAG, unsigned CC) 
558 {
559   MachineFunction &MF = DAG.getMachineFunction();
560
561   SDValue Chain  = Op.getOperand(0);
562   SDValue Callee = Op.getOperand(4);
563   bool isVarArg    = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
564
565   MachineFrameInfo *MFI = MF.getFrameInfo();
566
567   // Analyze operands of the call, assigning locations to each operand.
568   SmallVector<CCValAssign, 16> ArgLocs;
569   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
570
571   // To meet O32 ABI, Mips must always allocate 16 bytes on
572   // the stack (even if less than 4 are used as arguments)
573   if (Subtarget->isABI_O32()) {
574     int VTsize = MVT(MVT::i32).getSizeInBits()/8;
575     MFI->CreateFixedObject(VTsize, (VTsize*3));
576   }
577
578   CCInfo.AnalyzeCallOperands(Op.Val, CC_Mips);
579   
580   // Get a count of how many bytes are to be pushed on the stack.
581   unsigned NumBytes = CCInfo.getNextStackOffset();
582   Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, 
583                                  getPointerTy()));
584
585   // With EABI is it possible to have 16 args on registers.
586   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
587   SmallVector<SDValue, 8> MemOpChains;
588
589   // First/LastArgStackLoc contains the first/last 
590   // "at stack" argument location.
591   int LastArgStackLoc = 0;
592   unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
593
594   // Walk the register/memloc assignments, inserting copies/loads.
595   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
596     CCValAssign &VA = ArgLocs[i];
597
598     // Arguments start after the 5 first operands of ISD::CALL
599     SDValue Arg = Op.getOperand(5+2*VA.getValNo());
600     
601     // Promote the value if needed.
602     switch (VA.getLocInfo()) {
603     default: assert(0 && "Unknown loc info!");
604     case CCValAssign::Full: break;
605     case CCValAssign::SExt:
606       Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
607       break;
608     case CCValAssign::ZExt:
609       Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
610       break;
611     case CCValAssign::AExt:
612       Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
613       break;
614     }
615     
616     // Arguments that can be passed on register must be kept at 
617     // RegsToPass vector
618     if (VA.isRegLoc()) {
619       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
620       continue;
621     }
622     
623     // Register cant get to this point...
624     assert(VA.isMemLoc());
625     
626     // Create the frame index object for this incoming parameter
627     // This guarantees that when allocating Local Area the firsts
628     // 16 bytes which are alwayes reserved won't be overwritten
629     // if O32 ABI is used. For EABI the first address is zero.
630     LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
631     int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
632                                     LastArgStackLoc);
633
634     SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
635
636     // emit ISD::STORE whichs stores the 
637     // parameter value to a stack Location
638     MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
639   }
640
641   // Transform all store nodes into one single node because all store
642   // nodes are independent of each other.
643   if (!MemOpChains.empty())     
644     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 
645                         &MemOpChains[0], MemOpChains.size());
646
647   // Build a sequence of copy-to-reg nodes chained together with token 
648   // chain and flag operands which copy the outgoing args into registers.
649   // The InFlag in necessary since all emited instructions must be
650   // stuck together.
651   SDValue InFlag;
652   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
653     Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, 
654                              RegsToPass[i].second, InFlag);
655     InFlag = Chain.getValue(1);
656   }
657
658   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
659   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 
660   // node so that legalize doesn't hack it. 
661   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 
662     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
663   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
664     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
665
666
667   // MipsJmpLink = #chain, #target_address, #opt_in_flags...
668   //             = Chain, Callee, Reg#1, Reg#2, ...  
669   //
670   // Returns a chain & a flag for retval copy to use.
671   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
672   SmallVector<SDValue, 8> Ops;
673   Ops.push_back(Chain);
674   Ops.push_back(Callee);
675
676   // Add argument registers to the end of the list so that they are 
677   // known live into the call.
678   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
679     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
680                                   RegsToPass[i].second.getValueType()));
681
682   if (InFlag.Val)
683     Ops.push_back(InFlag);
684
685   Chain  = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
686   InFlag = Chain.getValue(1);
687
688   // Create the CALLSEQ_END node.
689   Chain = DAG.getCALLSEQ_END(Chain,
690                              DAG.getConstant(NumBytes, getPointerTy()),
691                              DAG.getConstant(0, getPointerTy()),
692                              InFlag);
693   InFlag = Chain.getValue(1);
694
695   // Create a stack location to hold GP when PIC is used. This stack 
696   // location is used on function prologue to save GP and also after all 
697   // emited CALL's to restore GP. 
698   if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
699       // Function can have an arbitrary number of calls, so 
700       // hold the LastArgStackLoc with the biggest offset.
701       int FI;
702       MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
703       if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
704         LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
705         // Create the frame index only once. SPOffset here can be anything 
706         // (this will be fixed on processFunctionBeforeFrameFinalized)
707         if (MipsFI->getGPStackOffset() == -1) {
708           FI = MFI->CreateFixedObject(4, 0);
709           MipsFI->setGPFI(FI);
710         }
711         MipsFI->setGPStackOffset(LastArgStackLoc);
712       }
713
714       // Reload GP value.
715       FI = MipsFI->getGPFI();
716       SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
717       SDValue GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
718       Chain = GPLoad.getValue(1);
719       Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32), 
720                                GPLoad, SDValue(0,0));
721       InFlag = Chain.getValue(1);
722   }      
723
724   // Handle result values, copying them out of physregs into vregs that we
725   // return.
726   return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
727 }
728
729 /// LowerCallResult - Lower the result values of an ISD::CALL into the
730 /// appropriate copies out of appropriate physical registers.  This assumes that
731 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
732 /// being lowered. Returns a SDNode with the same number of values as the 
733 /// ISD::CALL.
734 SDNode *MipsTargetLowering::
735 LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall, 
736         unsigned CallingConv, SelectionDAG &DAG) {
737   
738   bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
739
740   // Assign locations to each value returned by this call.
741   SmallVector<CCValAssign, 16> RVLocs;
742   CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
743
744   CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
745   SmallVector<SDValue, 8> ResultVals;
746
747   // Copy all of the result registers out of their specified physreg.
748   for (unsigned i = 0; i != RVLocs.size(); ++i) {
749     Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
750                                  RVLocs[i].getValVT(), InFlag).getValue(1);
751     InFlag = Chain.getValue(2);
752     ResultVals.push_back(Chain.getValue(0));
753   }
754   
755   ResultVals.push_back(Chain);
756
757   // Merge everything together with a MERGE_VALUES node.
758   return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
759                             ResultVals.size()).Val;
760 }
761
762 //===----------------------------------------------------------------------===//
763 //             FORMAL_ARGUMENTS Calling Convention Implementation
764 //===----------------------------------------------------------------------===//
765
766 /// Mips custom FORMAL_ARGUMENTS implementation
767 SDValue MipsTargetLowering::
768 LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) 
769 {
770   unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
771   switch(CC) 
772   {
773     default:
774       assert(0 && "Unsupported calling convention");
775     case CallingConv::C:
776       return LowerCCCArguments(Op, DAG);
777   }
778 }
779
780 /// LowerCCCArguments - transform physical registers into
781 /// virtual registers and generate load operations for
782 /// arguments places on the stack.
783 /// TODO: isVarArg
784 SDValue MipsTargetLowering::
785 LowerCCCArguments(SDValue Op, SelectionDAG &DAG) 
786 {
787   SDValue Root        = Op.getOperand(0);
788   MachineFunction &MF   = DAG.getMachineFunction();
789   MachineFrameInfo *MFI = MF.getFrameInfo();
790   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
791
792   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
793   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
794
795   unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
796
797   // GP must be live into PIC and non-PIC call target.
798   AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
799
800   // Assign locations to all of the incoming arguments.
801   SmallVector<CCValAssign, 16> ArgLocs;
802   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
803
804   CCInfo.AnalyzeFormalArguments(Op.Val, CC_Mips);
805   SmallVector<SDValue, 16> ArgValues;
806   SDValue StackPtr;
807
808   unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
809
810   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
811
812     CCValAssign &VA = ArgLocs[i];
813
814     // Arguments stored on registers
815     if (VA.isRegLoc()) {
816       MVT RegVT = VA.getLocVT();
817       TargetRegisterClass *RC = 0;
818             
819       if (RegVT == MVT::i32)
820         RC = Mips::CPURegsRegisterClass; 
821       else if (RegVT == MVT::f32) {
822         if (Subtarget->isSingleFloat())
823           RC = Mips::FGR32RegisterClass;
824         else
825           RC = Mips::AFGR32RegisterClass;
826       } else if (RegVT == MVT::f64) {
827         if (!Subtarget->isSingleFloat()) 
828           RC = Mips::AFGR64RegisterClass;
829       } else  
830         assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
831
832       // Transform the arguments stored on 
833       // physical registers into virtual ones
834       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
835       SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
836       
837       // If this is an 8 or 16-bit value, it is really passed promoted 
838       // to 32 bits.  Insert an assert[sz]ext to capture this, then 
839       // truncate to the right size.
840       if (VA.getLocInfo() == CCValAssign::SExt)
841         ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
842                                DAG.getValueType(VA.getValVT()));
843       else if (VA.getLocInfo() == CCValAssign::ZExt)
844         ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
845                                DAG.getValueType(VA.getValVT()));
846       
847       if (VA.getLocInfo() != CCValAssign::Full)
848         ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
849
850       ArgValues.push_back(ArgValue);
851
852       // To meet ABI, when VARARGS are passed on registers, the registers
853       // must have their values written to the caller stack frame. 
854       if ((isVarArg) && (Subtarget->isABI_O32())) {
855         if (StackPtr.Val == 0)
856           StackPtr = DAG.getRegister(StackReg, getPointerTy());
857      
858         // The stack pointer offset is relative to the caller stack frame. 
859         // Since the real stack size is unknown here, a negative SPOffset 
860         // is used so there's a way to adjust these offsets when the stack
861         // size get known (on EliminateFrameIndex). A dummy SPOffset is 
862         // used instead of a direct negative address (which is recorded to
863         // be used on emitPrologue) to avoid mis-calc of the first stack 
864         // offset on PEI::calculateFrameObjectOffsets.
865         // Arguments are always 32-bit.
866         int FI = MFI->CreateFixedObject(4, 0);
867         MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
868         SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
869       
870         // emit ISD::STORE whichs stores the 
871         // parameter value to a stack Location
872         ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
873       }
874
875     } else { // VA.isRegLoc()
876
877       // sanity check
878       assert(VA.isMemLoc());
879       
880       // The stack pointer offset is relative to the caller stack frame. 
881       // Since the real stack size is unknown here, a negative SPOffset 
882       // is used so there's a way to adjust these offsets when the stack
883       // size get known (on EliminateFrameIndex). A dummy SPOffset is 
884       // used instead of a direct negative address (which is recorded to
885       // be used on emitPrologue) to avoid mis-calc of the first stack 
886       // offset on PEI::calculateFrameObjectOffsets.
887       // Arguments are always 32-bit.
888       unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
889       int FI = MFI->CreateFixedObject(ArgSize, 0);
890       MipsFI->recordLoadArgsFI(FI, -(ArgSize+
891         (FirstStackArgLoc + VA.getLocMemOffset())));
892
893       // Create load nodes to retrieve arguments from the stack
894       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
895       ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
896     }
897   }
898
899   // The mips ABIs for returning structs by value requires that we copy
900   // the sret argument into $v0 for the return. Save the argument into
901   // a virtual register so that we can access it from the return points.
902   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
903     unsigned Reg = MipsFI->getSRetReturnReg();
904     if (!Reg) {
905       Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
906       MipsFI->setSRetReturnReg(Reg);
907     }
908     SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
909     Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
910   }
911
912   ArgValues.push_back(Root);
913
914   // Return the new list of results.
915   return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
916                             ArgValues.size()).getValue(Op.ResNo);
917 }
918
919 //===----------------------------------------------------------------------===//
920 //               Return Value Calling Convention Implementation
921 //===----------------------------------------------------------------------===//
922
923 SDValue MipsTargetLowering::
924 LowerRET(SDValue Op, SelectionDAG &DAG)
925 {
926   // CCValAssign - represent the assignment of
927   // the return value to a location
928   SmallVector<CCValAssign, 16> RVLocs;
929   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
930   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
931
932   // CCState - Info about the registers and stack slot.
933   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
934
935   // Analize return values of ISD::RET
936   CCInfo.AnalyzeReturn(Op.Val, RetCC_Mips);
937
938   // If this is the first return lowered for this function, add 
939   // the regs to the liveout set for the function.
940   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
941     for (unsigned i = 0; i != RVLocs.size(); ++i)
942       if (RVLocs[i].isRegLoc())
943         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
944   }
945
946   // The chain is always operand #0
947   SDValue Chain = Op.getOperand(0);
948   SDValue Flag;
949
950   // Copy the result values into the output registers.
951   for (unsigned i = 0; i != RVLocs.size(); ++i) {
952     CCValAssign &VA = RVLocs[i];
953     assert(VA.isRegLoc() && "Can only return in registers!");
954
955     // ISD::RET => ret chain, (regnum1,val1), ...
956     // So i*2+1 index only the regnums
957     Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
958
959     // guarantee that all emitted copies are
960     // stuck together, avoiding something bad
961     Flag = Chain.getValue(1);
962   }
963
964   // The mips ABIs for returning structs by value requires that we copy
965   // the sret argument into $v0 for the return. We saved the argument into
966   // a virtual register in the entry block, so now we copy the value out
967   // and into $v0.
968   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
969     MachineFunction &MF      = DAG.getMachineFunction();
970     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
971     unsigned Reg = MipsFI->getSRetReturnReg();
972
973     if (!Reg) 
974       assert(0 && "sret virtual register not created in the entry block");
975     SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
976
977     Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
978     Flag = Chain.getValue(1);
979   }
980
981   // Return on Mips is always a "jr $ra"
982   if (Flag.Val)
983     return DAG.getNode(MipsISD::Ret, MVT::Other, 
984                        Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
985   else // Return Void
986     return DAG.getNode(MipsISD::Ret, MVT::Other, 
987                        Chain, DAG.getRegister(Mips::RA, MVT::i32));
988 }
989
990 //===----------------------------------------------------------------------===//
991 //                           Mips Inline Assembly Support
992 //===----------------------------------------------------------------------===//
993
994 /// getConstraintType - Given a constraint letter, return the type of
995 /// constraint it is for this target.
996 MipsTargetLowering::ConstraintType MipsTargetLowering::
997 getConstraintType(const std::string &Constraint) const 
998 {
999   // Mips specific constrainy 
1000   // GCC config/mips/constraints.md
1001   //
1002   // 'd' : An address register. Equivalent to r 
1003   //       unless generating MIPS16 code. 
1004   // 'y' : Equivalent to r; retained for 
1005   //       backwards compatibility. 
1006   // 'f' : Floating Point registers.      
1007   if (Constraint.size() == 1) {
1008     switch (Constraint[0]) {
1009       default : break;
1010       case 'd':     
1011       case 'y': 
1012       case 'f':
1013         return C_RegisterClass;
1014         break;
1015     }
1016   }
1017   return TargetLowering::getConstraintType(Constraint);
1018 }
1019
1020 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
1021 /// return a list of registers that can be used to satisfy the constraint.
1022 /// This should only be used for C_RegisterClass constraints.
1023 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
1024 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
1025 {
1026   if (Constraint.size() == 1) {
1027     switch (Constraint[0]) {
1028     case 'r':
1029       return std::make_pair(0U, Mips::CPURegsRegisterClass);
1030     case 'f':
1031       if (VT == MVT::f32) {
1032         if (Subtarget->isSingleFloat())
1033           return std::make_pair(0U, Mips::FGR32RegisterClass);
1034         else
1035           return std::make_pair(0U, Mips::AFGR32RegisterClass);
1036       }
1037       if (VT == MVT::f64)    
1038         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1039           return std::make_pair(0U, Mips::AFGR64RegisterClass);
1040     }
1041   }
1042   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1043 }
1044
1045 /// Given a register class constraint, like 'r', if this corresponds directly
1046 /// to an LLVM register class, return a register of 0 and the register class
1047 /// pointer.
1048 std::vector<unsigned> MipsTargetLowering::
1049 getRegClassForInlineAsmConstraint(const std::string &Constraint,
1050                                   MVT VT) const
1051 {
1052   if (Constraint.size() != 1)
1053     return std::vector<unsigned>();
1054
1055   switch (Constraint[0]) {         
1056     default : break;
1057     case 'r':
1058     // GCC Mips Constraint Letters
1059     case 'd':     
1060     case 'y': 
1061       return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3, 
1062              Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1, 
1063              Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7, 
1064              Mips::T8, 0);
1065
1066     case 'f':
1067       if (VT == MVT::f32) {
1068         if (Subtarget->isSingleFloat())
1069           return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
1070                  Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
1071                  Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
1072                  Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
1073                  Mips::F30, Mips::F31, 0);
1074         else
1075           return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8, 
1076                  Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26, 
1077                  Mips::F28, Mips::F30, 0);
1078       }
1079
1080       if (VT == MVT::f64)    
1081         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
1082           return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4, 
1083                  Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13, 
1084                  Mips::D14, Mips::D15, 0);
1085   }
1086   return std::vector<unsigned>();
1087 }