LLVM API Documentation

MipsISelLowering.cpp

Go to the documentation of this file.
00001 //===-- MipsISelLowering.cpp - Mips DAG Lowering Implementation -----------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the interfaces that Mips uses to lower LLVM code into a
00011 // selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "mips-lower"
00016 
00017 #include "MipsISelLowering.h"
00018 #include "MipsMachineFunction.h"
00019 #include "MipsTargetMachine.h"
00020 #include "MipsSubtarget.h"
00021 #include "llvm/DerivedTypes.h"
00022 #include "llvm/Function.h"
00023 #include "llvm/GlobalVariable.h"
00024 #include "llvm/Intrinsics.h"
00025 #include "llvm/CallingConv.h"
00026 #include "llvm/CodeGen/CallingConvLower.h"
00027 #include "llvm/CodeGen/MachineFrameInfo.h"
00028 #include "llvm/CodeGen/MachineFunction.h"
00029 #include "llvm/CodeGen/MachineInstrBuilder.h"
00030 #include "llvm/CodeGen/MachineRegisterInfo.h"
00031 #include "llvm/CodeGen/SelectionDAGISel.h"
00032 #include "llvm/CodeGen/ValueTypes.h"
00033 #include "llvm/Support/Debug.h"
00034 using namespace llvm;
00035 
00036 const char *MipsTargetLowering::
00037 getTargetNodeName(unsigned Opcode) const 
00038 {
00039   switch (Opcode) 
00040   {
00041     case MipsISD::JmpLink    : return "MipsISD::JmpLink";
00042     case MipsISD::Hi         : return "MipsISD::Hi";
00043     case MipsISD::Lo         : return "MipsISD::Lo";
00044     case MipsISD::GPRel      : return "MipsISD::GPRel";
00045     case MipsISD::Ret        : return "MipsISD::Ret";
00046     case MipsISD::CMov       : return "MipsISD::CMov";
00047     case MipsISD::SelectCC   : return "MipsISD::SelectCC";
00048     case MipsISD::FPSelectCC : return "MipsISD::FPSelectCC";
00049     case MipsISD::FPBrcond   : return "MipsISD::FPBrcond";
00050     case MipsISD::FPCmp      : return "MipsISD::FPCmp";
00051     default                  : return NULL;
00052   }
00053 }
00054 
00055 MipsTargetLowering::
00056 MipsTargetLowering(MipsTargetMachine &TM): TargetLowering(TM) 
00057 {
00058   Subtarget = &TM.getSubtarget<MipsSubtarget>();
00059 
00060   // Mips does not have i1 type, so use i32 for
00061   // setcc operations results (slt, sgt, ...). 
00062   setBooleanContents(ZeroOrOneBooleanContent);
00063 
00064   // JumpTable targets must use GOT when using PIC_
00065   setUsesGlobalOffsetTable(true);
00066 
00067   // Set up the register classes
00068   addRegisterClass(MVT::i32, Mips::CPURegsRegisterClass);
00069 
00070   // When dealing with single precision only, use libcalls
00071   if (!Subtarget->isSingleFloat()) {
00072     addRegisterClass(MVT::f32, Mips::AFGR32RegisterClass);
00073     if (!Subtarget->isFP64bit())
00074       addRegisterClass(MVT::f64, Mips::AFGR64RegisterClass);
00075   } else 
00076     addRegisterClass(MVT::f32, Mips::FGR32RegisterClass);
00077 
00078   // Legal fp constants
00079   addLegalFPImmediate(APFloat(+0.0f));
00080 
00081   // Load extented operations for i1 types must be promoted 
00082   setLoadExtAction(ISD::EXTLOAD,  MVT::i1,  Promote);
00083   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1,  Promote);
00084   setLoadExtAction(ISD::SEXTLOAD, MVT::i1,  Promote);
00085 
00086   // Used by legalize types to correctly generate the setcc result. 
00087   // Without this, every float setcc comes with a AND/OR with the result, 
00088   // we don't want this, since the fpcmp result goes to a flag register, 
00089   // which is used implicitly by brcond and select operations.
00090   AddPromotedToType(ISD::SETCC, MVT::i1, MVT::i32);
00091 
00092   // Mips Custom Operations
00093   setOperationAction(ISD::GlobalAddress,      MVT::i32,   Custom);
00094   setOperationAction(ISD::GlobalTLSAddress,   MVT::i32,   Custom);
00095   setOperationAction(ISD::RET,                MVT::Other, Custom);
00096   setOperationAction(ISD::JumpTable,          MVT::i32,   Custom);
00097   setOperationAction(ISD::ConstantPool,       MVT::i32,   Custom);
00098   setOperationAction(ISD::SELECT,             MVT::f32,   Custom);
00099   setOperationAction(ISD::SELECT,             MVT::i32,   Custom);
00100   setOperationAction(ISD::SETCC,              MVT::f32,   Custom);
00101   setOperationAction(ISD::BRCOND,             MVT::Other, Custom);
00102   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32,   Custom);
00103 
00104   // We custom lower AND/OR to handle the case where the DAG contain 'ands/ors' 
00105   // with operands comming from setcc fp comparions. This is necessary since 
00106   // the result from these setcc are in a flag registers (FCR31).
00107   setOperationAction(ISD::AND,              MVT::i32,   Custom);
00108   setOperationAction(ISD::OR,               MVT::i32,   Custom);
00109 
00110   // Operations not directly supported by Mips.
00111   setOperationAction(ISD::BR_JT,             MVT::Other, Expand);
00112   setOperationAction(ISD::BR_CC,             MVT::Other, Expand);
00113   setOperationAction(ISD::SELECT_CC,         MVT::Other, Expand);
00114   setOperationAction(ISD::UINT_TO_FP,        MVT::i32,   Expand);
00115   setOperationAction(ISD::FP_TO_UINT,        MVT::i32,   Expand);
00116   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1,    Expand);
00117   setOperationAction(ISD::CTPOP,             MVT::i32,   Expand);
00118   setOperationAction(ISD::CTTZ,              MVT::i32,   Expand);
00119   setOperationAction(ISD::ROTL,              MVT::i32,   Expand);
00120   setOperationAction(ISD::SHL_PARTS,         MVT::i32,   Expand);
00121   setOperationAction(ISD::SRA_PARTS,         MVT::i32,   Expand);
00122   setOperationAction(ISD::SRL_PARTS,         MVT::i32,   Expand);
00123   setOperationAction(ISD::FCOPYSIGN,         MVT::f32,   Expand);
00124 
00125   // We don't have line number support yet.
00126   setOperationAction(ISD::DBG_STOPPOINT,     MVT::Other, Expand);
00127   setOperationAction(ISD::DEBUG_LOC,         MVT::Other, Expand);
00128   setOperationAction(ISD::DBG_LABEL,         MVT::Other, Expand);
00129   setOperationAction(ISD::EH_LABEL,          MVT::Other, Expand);
00130 
00131   // Use the default for now
00132   setOperationAction(ISD::STACKSAVE,         MVT::Other, Expand);
00133   setOperationAction(ISD::STACKRESTORE,      MVT::Other, Expand);
00134   setOperationAction(ISD::MEMBARRIER,        MVT::Other, Expand);
00135 
00136   if (Subtarget->isSingleFloat())
00137     setOperationAction(ISD::SELECT_CC, MVT::f64, Expand);
00138 
00139   if (!Subtarget->hasSEInReg()) {
00140     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8,  Expand);
00141     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
00142   }
00143 
00144   if (!Subtarget->hasBitCount())
00145     setOperationAction(ISD::CTLZ, MVT::i32, Expand);
00146 
00147   if (!Subtarget->hasSwap())
00148     setOperationAction(ISD::BSWAP, MVT::i32, Expand);
00149 
00150   setStackPointerRegisterToSaveRestore(Mips::SP);
00151   computeRegisterProperties();
00152 }
00153 
00154 
00155 MVT MipsTargetLowering::getSetCCResultType(MVT VT) const {
00156   return MVT::i32;
00157 }
00158 
00159 
00160 SDValue MipsTargetLowering::
00161 LowerOperation(SDValue Op, SelectionDAG &DAG) 
00162 {
00163   switch (Op.getOpcode()) 
00164   {
00165     case ISD::AND:                return LowerANDOR(Op, DAG);
00166     case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
00167     case ISD::CALL:               return LowerCALL(Op, DAG);
00168     case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
00169     case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
00170     case ISD::FORMAL_ARGUMENTS:   return LowerFORMAL_ARGUMENTS(Op, DAG);
00171     case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
00172     case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
00173     case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
00174     case ISD::OR:                 return LowerANDOR(Op, DAG);
00175     case ISD::RET:                return LowerRET(Op, DAG);
00176     case ISD::SELECT:             return LowerSELECT(Op, DAG);
00177     case ISD::SETCC:              return LowerSETCC(Op, DAG);
00178   }
00179   return SDValue();
00180 }
00181 
00182 //===----------------------------------------------------------------------===//
00183 //  Lower helper functions
00184 //===----------------------------------------------------------------------===//
00185 
00186 // AddLiveIn - This helper function adds the specified physical register to the
00187 // MachineFunction as a live in value.  It also creates a corresponding
00188 // virtual register for it.
00189 static unsigned
00190 AddLiveIn(MachineFunction &MF, unsigned PReg, TargetRegisterClass *RC) 
00191 {
00192   assert(RC->contains(PReg) && "Not the correct regclass!");
00193   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
00194   MF.getRegInfo().addLiveIn(PReg, VReg);
00195   return VReg;
00196 }
00197 
00198 // A address must be loaded from a small section if its size is less than the 
00199 // small section size threshold. Data in this section must be addressed using 
00200 // gp_rel operator.
00201 bool MipsTargetLowering::IsInSmallSection(unsigned Size) {
00202   return (Size > 0 && (Size <= Subtarget->getSSectionThreshold()));
00203 }
00204 
00205 // Discover if this global address can be placed into small data/bss section. 
00206 bool MipsTargetLowering::IsGlobalInSmallSection(GlobalValue *GV)
00207 {
00208   const TargetData *TD = getTargetData();
00209   const GlobalVariable *GVA = dyn_cast<GlobalVariable>(GV);
00210 
00211   if (!GVA)
00212     return false;
00213   
00214   const Type *Ty = GV->getType()->getElementType();
00215   unsigned Size = TD->getTypePaddedSize(Ty);
00216 
00217   // if this is a internal constant string, there is a special
00218   // section for it, but not in small data/bss.
00219   if (GVA->hasInitializer() && GV->hasLocalLinkage()) {
00220     Constant *C = GVA->getInitializer();
00221     const ConstantArray *CVA = dyn_cast<ConstantArray>(C);
00222     if (CVA && CVA->isCString()) 
00223       return false;
00224   }
00225 
00226   return IsInSmallSection(Size);
00227 }
00228 
00229 // Get fp branch code (not opcode) from condition code.
00230 static Mips::FPBranchCode GetFPBranchCodeFromCond(Mips::CondCode CC) {
00231   if (CC >= Mips::FCOND_F && CC <= Mips::FCOND_NGT)
00232     return Mips::BRANCH_T;
00233 
00234   if (CC >= Mips::FCOND_T && CC <= Mips::FCOND_GT)
00235     return Mips::BRANCH_F;
00236 
00237   return Mips::BRANCH_INVALID;
00238 }
00239   
00240 static unsigned FPBranchCodeToOpc(Mips::FPBranchCode BC) {
00241   switch(BC) {
00242     default:
00243       assert(0 && "Unknown branch code");
00244     case Mips::BRANCH_T  : return Mips::BC1T;
00245     case Mips::BRANCH_F  : return Mips::BC1F;
00246     case Mips::BRANCH_TL : return Mips::BC1TL;
00247     case Mips::BRANCH_FL : return Mips::BC1FL;
00248   }
00249 }
00250 
00251 static Mips::CondCode FPCondCCodeToFCC(ISD::CondCode CC) {
00252   switch (CC) {
00253   default: assert(0 && "Unknown fp condition code!");
00254   case ISD::SETEQ:  
00255   case ISD::SETOEQ: return Mips::FCOND_EQ;
00256   case ISD::SETUNE: return Mips::FCOND_OGL;
00257   case ISD::SETLT:  
00258   case ISD::SETOLT: return Mips::FCOND_OLT;
00259   case ISD::SETGT:  
00260   case ISD::SETOGT: return Mips::FCOND_OGT;
00261   case ISD::SETLE:  
00262   case ISD::SETOLE: return Mips::FCOND_OLE; 
00263   case ISD::SETGE:
00264   case ISD::SETOGE: return Mips::FCOND_OGE;
00265   case ISD::SETULT: return Mips::FCOND_ULT;
00266   case ISD::SETULE: return Mips::FCOND_ULE; 
00267   case ISD::SETUGT: return Mips::FCOND_UGT;
00268   case ISD::SETUGE: return Mips::FCOND_UGE;
00269   case ISD::SETUO:  return Mips::FCOND_UN; 
00270   case ISD::SETO:   return Mips::FCOND_OR;
00271   case ISD::SETNE:  
00272   case ISD::SETONE: return Mips::FCOND_NEQ;
00273   case ISD::SETUEQ: return Mips::FCOND_UEQ;
00274   }
00275 }
00276 
00277 MachineBasicBlock *
00278 MipsTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
00279                                                 MachineBasicBlock *BB) 
00280 {
00281   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
00282   bool isFPCmp = false;
00283 
00284   switch (MI->getOpcode()) {
00285   default: assert(false && "Unexpected instr type to insert");
00286   case Mips::Select_FCC:
00287   case Mips::Select_FCC_SO32:
00288   case Mips::Select_FCC_AS32:
00289   case Mips::Select_FCC_D32:
00290     isFPCmp = true; // FALL THROUGH
00291   case Mips::Select_CC:
00292   case Mips::Select_CC_SO32:
00293   case Mips::Select_CC_AS32:
00294   case Mips::Select_CC_D32: {
00295     // To "insert" a SELECT_CC instruction, we actually have to insert the
00296     // diamond control-flow pattern.  The incoming instruction knows the
00297     // destination vreg to set, the condition code register to branch on, the
00298     // true/false values to select between, and a branch opcode to use.
00299     const BasicBlock *LLVM_BB = BB->getBasicBlock();
00300     MachineFunction::iterator It = BB;
00301     ++It;
00302 
00303     //  thisMBB:
00304     //  ...
00305     //   TrueVal = ...
00306     //   setcc r1, r2, r3
00307     //   bNE   r1, r0, copy1MBB
00308     //   fallthrough --> copy0MBB
00309     MachineBasicBlock *thisMBB  = BB;
00310     MachineFunction *F = BB->getParent();
00311     MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
00312     MachineBasicBlock *sinkMBB  = F->CreateMachineBasicBlock(LLVM_BB);
00313 
00314     // Emit the right instruction according to the type of the operands compared
00315     if (isFPCmp) {
00316       // Find the condiction code present in the setcc operation.
00317       Mips::CondCode CC = (Mips::CondCode)MI->getOperand(4).getImm();
00318       // Get the branch opcode from the branch code.
00319       unsigned Opc = FPBranchCodeToOpc(GetFPBranchCodeFromCond(CC));
00320       BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
00321     } else
00322       BuildMI(BB, TII->get(Mips::BNE)).addReg(MI->getOperand(1).getReg())
00323         .addReg(Mips::ZERO).addMBB(sinkMBB);
00324 
00325     F->insert(It, copy0MBB);
00326     F->insert(It, sinkMBB);
00327     // Update machine-CFG edges by first adding all successors of the current
00328     // block to the new block which will contain the Phi node for the select.
00329     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
00330         e = BB->succ_end(); i != e; ++i)
00331       sinkMBB->addSuccessor(*i);
00332     // Next, remove all successors of the current block, and add the true
00333     // and fallthrough blocks as its successors.
00334     while(!BB->succ_empty())
00335       BB->removeSuccessor(BB->succ_begin());
00336     BB->addSuccessor(copy0MBB);
00337     BB->addSuccessor(sinkMBB);
00338 
00339     //  copy0MBB:
00340     //   %FalseValue = ...
00341     //   # fallthrough to sinkMBB
00342     BB = copy0MBB;
00343 
00344     // Update machine-CFG edges
00345     BB->addSuccessor(sinkMBB);
00346 
00347     //  sinkMBB:
00348     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
00349     //  ...
00350     BB = sinkMBB;
00351     BuildMI(BB, TII->get(Mips::PHI), MI->getOperand(0).getReg())
00352       .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
00353       .addReg(MI->getOperand(3).getReg()).addMBB(thisMBB);
00354 
00355     F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
00356     return BB;
00357   }
00358   }
00359 }
00360 
00361 //===----------------------------------------------------------------------===//
00362 //  Misc Lower Operation implementation
00363 //===----------------------------------------------------------------------===//
00364 
00365 SDValue MipsTargetLowering::
00366 LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG)
00367 {
00368   SDValue Chain = Op.getOperand(0);
00369   SDValue Size = Op.getOperand(1);
00370 
00371   // Get a reference from Mips stack pointer
00372   SDValue StackPointer = DAG.getCopyFromReg(Chain, Mips::SP, MVT::i32);
00373 
00374   // Subtract the dynamic size from the actual stack size to
00375   // obtain the new stack size.
00376   SDValue Sub = DAG.getNode(ISD::SUB, MVT::i32, StackPointer, Size);
00377 
00378   // The Sub result contains the new stack start address, so it 
00379   // must be placed in the stack pointer register.
00380   Chain = DAG.getCopyToReg(StackPointer.getValue(1), Mips::SP, Sub);
00381   
00382   // This node always has two return values: a new stack pointer 
00383   // value and a chain
00384   SDValue Ops[2] = { Sub, Chain };
00385   return DAG.getMergeValues(Ops, 2);
00386 }
00387 
00388 SDValue MipsTargetLowering::
00389 LowerANDOR(SDValue Op, SelectionDAG &DAG)
00390 {
00391   SDValue LHS   = Op.getOperand(0);
00392   SDValue RHS   = Op.getOperand(1);
00393   
00394   if (LHS.getOpcode() != MipsISD::FPCmp || RHS.getOpcode() != MipsISD::FPCmp)
00395     return Op;
00396 
00397   SDValue True  = DAG.getConstant(1, MVT::i32);
00398   SDValue False = DAG.getConstant(0, MVT::i32);
00399 
00400   SDValue LSEL = DAG.getNode(MipsISD::FPSelectCC, True.getValueType(), 
00401                              LHS, True, False, LHS.getOperand(2));
00402   SDValue RSEL = DAG.getNode(MipsISD::FPSelectCC, True.getValueType(), 
00403                              RHS, True, False, RHS.getOperand(2));
00404 
00405   return DAG.getNode(Op.getOpcode(), MVT::i32, LSEL, RSEL);
00406 }
00407 
00408 SDValue MipsTargetLowering::
00409 LowerBRCOND(SDValue Op, SelectionDAG &DAG)
00410 {
00411   // The first operand is the chain, the second is the condition, the third is 
00412   // the block to branch to if the condition is true.
00413   SDValue Chain = Op.getOperand(0);
00414   SDValue Dest = Op.getOperand(2);
00415 
00416   if (Op.getOperand(1).getOpcode() != MipsISD::FPCmp)
00417     return Op;
00418   
00419   SDValue CondRes = Op.getOperand(1);
00420   SDValue CCNode  = CondRes.getOperand(2);
00421   Mips::CondCode CC =
00422     (Mips::CondCode)cast<ConstantSDNode>(CCNode)->getZExtValue();
00423   SDValue BrCode = DAG.getConstant(GetFPBranchCodeFromCond(CC), MVT::i32); 
00424 
00425   return DAG.getNode(MipsISD::FPBrcond, Op.getValueType(), Chain, BrCode, 
00426              Dest, CondRes);
00427 }
00428 
00429 SDValue MipsTargetLowering::
00430 LowerSETCC(SDValue Op, SelectionDAG &DAG)
00431 {
00432   // The operands to this are the left and right operands to compare (ops #0, 
00433   // and #1) and the condition code to compare them with (op #2) as a 
00434   // CondCodeSDNode.
00435   SDValue LHS = Op.getOperand(0); 
00436   SDValue RHS = Op.getOperand(1); 
00437 
00438   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
00439   
00440   return DAG.getNode(MipsISD::FPCmp, Op.getValueType(), LHS, RHS, 
00441                  DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32));
00442 }
00443 
00444 SDValue MipsTargetLowering::
00445 LowerSELECT(SDValue Op, SelectionDAG &DAG) 
00446 {
00447   SDValue Cond  = Op.getOperand(0); 
00448   SDValue True  = Op.getOperand(1);
00449   SDValue False = Op.getOperand(2);
00450 
00451   // if the incomming condition comes from a integer compare, the select 
00452   // operation must be SelectCC or a conditional move if the subtarget 
00453   // supports it.
00454   if (Cond.getOpcode() != MipsISD::FPCmp) {
00455     if (Subtarget->hasCondMov() && !True.getValueType().isFloatingPoint())
00456       return Op;
00457     return DAG.getNode(MipsISD::SelectCC, True.getValueType(), 
00458                        Cond, True, False);
00459   }
00460 
00461   // if the incomming condition comes from fpcmp, the select
00462   // operation must use FPSelectCC.
00463   SDValue CCNode = Cond.getOperand(2);
00464   return DAG.getNode(MipsISD::FPSelectCC, True.getValueType(), 
00465                      Cond, True, False, CCNode);
00466 }
00467 
00468 SDValue MipsTargetLowering::
00469 LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) 
00470 {
00471   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
00472   SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
00473 
00474   if (!Subtarget->hasABICall()) {
00475     const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
00476     SDValue Ops[] = { GA };
00477     // %gp_rel relocation
00478     if (!isa<Function>(GV) && IsGlobalInSmallSection(GV)) { 
00479       SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, VTs, 1, Ops, 1);
00480       SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
00481       return DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); 
00482     }
00483     // %hi/%lo relocation
00484     SDValue HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
00485     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
00486     return DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
00487 
00488   } else { // Abicall relocations, TODO: make this cleaner.
00489     SDValue ResNode = DAG.getLoad(MVT::i32, DAG.getEntryNode(), GA, NULL, 0);
00490     // On functions and global targets not internal linked only
00491     // a load from got/GP is necessary for PIC to work.
00492     if (!GV->hasLocalLinkage() || isa<Function>(GV))
00493       return ResNode;
00494     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, GA);
00495     return DAG.getNode(ISD::ADD, MVT::i32, ResNode, Lo);
00496   }
00497 
00498   assert(0 && "Dont know how to handle GlobalAddress");
00499   return SDValue(0,0);
00500 }
00501 
00502 SDValue MipsTargetLowering::
00503 LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG)
00504 {
00505   assert(0 && "TLS not implemented for MIPS.");
00506   return SDValue(); // Not reached
00507 }
00508 
00509 SDValue MipsTargetLowering::
00510 LowerJumpTable(SDValue Op, SelectionDAG &DAG) 
00511 {
00512   SDValue ResNode;
00513   SDValue HiPart; 
00514 
00515   MVT PtrVT = Op.getValueType();
00516   JumpTableSDNode *JT  = cast<JumpTableSDNode>(Op);
00517   SDValue JTI = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
00518 
00519   if (getTargetMachine().getRelocationModel() != Reloc::PIC_) {
00520     const MVT *VTs = DAG.getNodeValueTypes(MVT::i32);
00521     SDValue Ops[] = { JTI };
00522     HiPart = DAG.getNode(MipsISD::Hi, VTs, 1, Ops, 1);
00523   } else // Emit Load from Global Pointer
00524     HiPart = DAG.getLoad(MVT::i32, DAG.getEntryNode(), JTI, NULL, 0);
00525 
00526   SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, JTI);
00527   ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
00528 
00529   return ResNode;
00530 }
00531 
00532 SDValue MipsTargetLowering::
00533 LowerConstantPool(SDValue Op, SelectionDAG &DAG) 
00534 {
00535   SDValue ResNode;
00536   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
00537   Constant *C = N->getConstVal();
00538   SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
00539 
00540   // gp_rel relocation
00541   // FIXME: we should reference the constant pool using small data sections, 
00542   // but the asm printer currently doens't support this feature without
00543   // hacking it. This feature should come soon so we can uncomment the 
00544   // stuff below.
00545   //if (!Subtarget->hasABICall() &&  
00546   //    IsInSmallSection(getTargetData()->getTypePaddedSize(C->getType()))) {
00547   //  SDValue GPRelNode = DAG.getNode(MipsISD::GPRel, MVT::i32, CP);
00548   //  SDValue GOT = DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, MVT::i32);
00549   //  ResNode = DAG.getNode(ISD::ADD, MVT::i32, GOT, GPRelNode); 
00550   //} else { // %hi/%lo relocation
00551     SDValue HiPart = DAG.getNode(MipsISD::Hi, MVT::i32, CP);
00552     SDValue Lo = DAG.getNode(MipsISD::Lo, MVT::i32, CP);
00553     ResNode = DAG.getNode(ISD::ADD, MVT::i32, HiPart, Lo);
00554   //}
00555 
00556   return ResNode;
00557 }
00558 
00559 //===----------------------------------------------------------------------===//
00560 //                      Calling Convention Implementation
00561 //
00562 //  The lower operations present on calling convention works on this order:
00563 //      LowerCALL (virt regs --> phys regs, virt regs --> stack) 
00564 //      LowerFORMAL_ARGUMENTS (phys --> virt regs, stack --> virt regs)
00565 //      LowerRET (virt regs --> phys regs)
00566 //      LowerCALL (phys regs --> virt regs)
00567 //
00568 //===----------------------------------------------------------------------===//
00569 
00570 #include "MipsGenCallingConv.inc"
00571 
00572 //===----------------------------------------------------------------------===//
00573 //                  CALL Calling Convention Implementation
00574 //===----------------------------------------------------------------------===//
00575 
00576 /// LowerCALL - functions arguments are copied from virtual regs to 
00577 /// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
00578 /// TODO: isVarArg, isTailCall.
00579 SDValue MipsTargetLowering::
00580 LowerCALL(SDValue Op, SelectionDAG &DAG)
00581 {
00582   MachineFunction &MF = DAG.getMachineFunction();
00583 
00584   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
00585   SDValue Chain = TheCall->getChain();
00586   SDValue Callee = TheCall->getCallee();
00587   bool isVarArg = TheCall->isVarArg();
00588   unsigned CC = TheCall->getCallingConv();
00589 
00590   MachineFrameInfo *MFI = MF.getFrameInfo();
00591 
00592   // Analyze operands of the call, assigning locations to each operand.
00593   SmallVector<CCValAssign, 16> ArgLocs;
00594   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
00595 
00596   // To meet O32 ABI, Mips must always allocate 16 bytes on
00597   // the stack (even if less than 4 are used as arguments)
00598   if (Subtarget->isABI_O32()) {
00599     int VTsize = MVT(MVT::i32).getSizeInBits()/8;
00600     MFI->CreateFixedObject(VTsize, (VTsize*3));
00601   }
00602 
00603   CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
00604   
00605   // Get a count of how many bytes are to be pushed on the stack.
00606   unsigned NumBytes = CCInfo.getNextStackOffset();
00607   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
00608 
00609   // With EABI is it possible to have 16 args on registers.
00610   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
00611   SmallVector<SDValue, 8> MemOpChains;
00612 
00613   // First/LastArgStackLoc contains the first/last 
00614   // "at stack" argument location.
00615   int LastArgStackLoc = 0;
00616   unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
00617 
00618   // Walk the register/memloc assignments, inserting copies/loads.
00619   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00620     CCValAssign &VA = ArgLocs[i];
00621 
00622     // Arguments start after the 5 first operands of ISD::CALL
00623     SDValue Arg = TheCall->getArg(i);
00624     
00625     // Promote the value if needed.
00626     switch (VA.getLocInfo()) {
00627     default: assert(0 && "Unknown loc info!");
00628     case CCValAssign::Full: break;
00629     case CCValAssign::SExt:
00630       Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
00631       break;
00632     case CCValAssign::ZExt:
00633       Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
00634       break;
00635     case CCValAssign::AExt:
00636       Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
00637       break;
00638     }
00639     
00640     // Arguments that can be passed on register must be kept at 
00641     // RegsToPass vector
00642     if (VA.isRegLoc()) {
00643       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
00644       continue;
00645     }
00646     
00647     // Register cant get to this point...
00648     assert(VA.isMemLoc());
00649     
00650     // Create the frame index object for this incoming parameter
00651     // This guarantees that when allocating Local Area the firsts
00652     // 16 bytes which are alwayes reserved won't be overwritten
00653     // if O32 ABI is used. For EABI the first address is zero.
00654     LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
00655     int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
00656                                     LastArgStackLoc);
00657 
00658     SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
00659 
00660     // emit ISD::STORE whichs stores the 
00661     // parameter value to a stack Location
00662     MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
00663   }
00664 
00665   // Transform all store nodes into one single node because all store
00666   // nodes are independent of each other.
00667   if (!MemOpChains.empty())     
00668     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 
00669                         &MemOpChains[0], MemOpChains.size());
00670 
00671   // Build a sequence of copy-to-reg nodes chained together with token 
00672   // chain and flag operands which copy the outgoing args into registers.
00673   // The InFlag in necessary since all emited instructions must be
00674   // stuck together.
00675   SDValue InFlag;
00676   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00677     Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, 
00678                              RegsToPass[i].second, InFlag);
00679     InFlag = Chain.getValue(1);
00680   }
00681 
00682   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
00683   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 
00684   // node so that legalize doesn't hack it. 
00685   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 
00686     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
00687   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
00688     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
00689 
00690 
00691   // MipsJmpLink = #chain, #target_address, #opt_in_flags...
00692   //             = Chain, Callee, Reg#1, Reg#2, ...  
00693   //
00694   // Returns a chain & a flag for retval copy to use.
00695   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
00696   SmallVector<SDValue, 8> Ops;
00697   Ops.push_back(Chain);
00698   Ops.push_back(Callee);
00699 
00700   // Add argument registers to the end of the list so that they are 
00701   // known live into the call.
00702   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
00703     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
00704                                   RegsToPass[i].second.getValueType()));
00705 
00706   if (InFlag.getNode())
00707     Ops.push_back(InFlag);
00708 
00709   Chain  = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
00710   InFlag = Chain.getValue(1);
00711 
00712   // Create the CALLSEQ_END node.
00713   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
00714                              DAG.getIntPtrConstant(0, true), InFlag);
00715   InFlag = Chain.getValue(1);
00716 
00717   // Create a stack location to hold GP when PIC is used. This stack 
00718   // location is used on function prologue to save GP and also after all 
00719   // emited CALL's to restore GP. 
00720   if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
00721       // Function can have an arbitrary number of calls, so 
00722       // hold the LastArgStackLoc with the biggest offset.
00723       int FI;
00724       MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00725       if (LastArgStackLoc >= MipsFI->getGPStackOffset()) {
00726         LastArgStackLoc = (!LastArgStackLoc) ? (16) : (LastArgStackLoc+4);
00727         // Create the frame index only once. SPOffset here can be anything 
00728         // (this will be fixed on processFunctionBeforeFrameFinalized)
00729         if (MipsFI->getGPStackOffset() == -1) {
00730           FI = MFI->CreateFixedObject(4, 0);
00731           MipsFI->setGPFI(FI);
00732         }
00733         MipsFI->setGPStackOffset(LastArgStackLoc);
00734       }
00735 
00736       // Reload GP value.
00737       FI = MipsFI->getGPFI();
00738       SDValue FIN = DAG.getFrameIndex(FI,getPointerTy());
00739       SDValue GPLoad = DAG.getLoad(MVT::i32, Chain, FIN, NULL, 0);
00740       Chain = GPLoad.getValue(1);
00741       Chain = DAG.getCopyToReg(Chain, DAG.getRegister(Mips::GP, MVT::i32), 
00742                                GPLoad, SDValue(0,0));
00743       InFlag = Chain.getValue(1);
00744   }      
00745 
00746   // Handle result values, copying them out of physregs into vregs that we
00747   // return.
00748   return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG), Op.getResNo());
00749 }
00750 
00751 /// LowerCallResult - Lower the result values of an ISD::CALL into the
00752 /// appropriate copies out of appropriate physical registers.  This assumes that
00753 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
00754 /// being lowered. Returns a SDNode with the same number of values as the 
00755 /// ISD::CALL.
00756 SDNode *MipsTargetLowering::
00757 LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall, 
00758         unsigned CallingConv, SelectionDAG &DAG) {
00759   
00760   bool isVarArg = TheCall->isVarArg();
00761 
00762   // Assign locations to each value returned by this call.
00763   SmallVector<CCValAssign, 16> RVLocs;
00764   CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
00765 
00766   CCInfo.AnalyzeCallResult(TheCall, RetCC_Mips);
00767   SmallVector<SDValue, 8> ResultVals;
00768 
00769   // Copy all of the result registers out of their specified physreg.
00770   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00771     Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
00772                                  RVLocs[i].getValVT(), InFlag).getValue(1);
00773     InFlag = Chain.getValue(2);
00774     ResultVals.push_back(Chain.getValue(0));
00775   }
00776   
00777   ResultVals.push_back(Chain);
00778 
00779   // Merge everything together with a MERGE_VALUES node.
00780   return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
00781                      &ResultVals[0], ResultVals.size()).getNode();
00782 }
00783 
00784 //===----------------------------------------------------------------------===//
00785 //             FORMAL_ARGUMENTS Calling Convention Implementation
00786 //===----------------------------------------------------------------------===//
00787 
00788 /// LowerFORMAL_ARGUMENTS - transform physical registers into
00789 /// virtual registers and generate load operations for
00790 /// arguments places on the stack.
00791 /// TODO: isVarArg
00792 SDValue MipsTargetLowering::
00793 LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) 
00794 {
00795   SDValue Root = Op.getOperand(0);
00796   MachineFunction &MF = DAG.getMachineFunction();
00797   MachineFrameInfo *MFI = MF.getFrameInfo();
00798   MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00799 
00800   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
00801   unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
00802 
00803   unsigned StackReg = MF.getTarget().getRegisterInfo()->getFrameRegister(MF);
00804 
00805   // GP must be live into PIC and non-PIC call target.
00806   AddLiveIn(MF, Mips::GP, Mips::CPURegsRegisterClass);
00807 
00808   // Assign locations to all of the incoming arguments.
00809   SmallVector<CCValAssign, 16> ArgLocs;
00810   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
00811 
00812   CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_Mips);
00813   SmallVector<SDValue, 16> ArgValues;
00814   SDValue StackPtr;
00815 
00816   unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
00817 
00818   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00819 
00820     CCValAssign &VA = ArgLocs[i];
00821 
00822     // Arguments stored on registers
00823     if (VA.isRegLoc()) {
00824       MVT RegVT = VA.getLocVT();
00825       TargetRegisterClass *RC = 0;
00826             
00827       if (RegVT == MVT::i32)
00828         RC = Mips::CPURegsRegisterClass; 
00829       else if (RegVT == MVT::f32) {
00830         if (Subtarget->isSingleFloat())
00831           RC = Mips::FGR32RegisterClass;
00832         else
00833           RC = Mips::AFGR32RegisterClass;
00834       } else if (RegVT == MVT::f64) {
00835         if (!Subtarget->isSingleFloat()) 
00836           RC = Mips::AFGR64RegisterClass;
00837       } else  
00838         assert(0 && "RegVT not supported by FORMAL_ARGUMENTS Lowering");
00839 
00840       // Transform the arguments stored on 
00841       // physical registers into virtual ones
00842       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
00843       SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
00844       
00845       // If this is an 8 or 16-bit value, it is really passed promoted 
00846       // to 32 bits.  Insert an assert[sz]ext to capture this, then 
00847       // truncate to the right size.
00848       if (VA.getLocInfo() == CCValAssign::SExt)
00849         ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
00850                                DAG.getValueType(VA.getValVT()));
00851       else if (VA.getLocInfo() == CCValAssign::ZExt)
00852         ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
00853                                DAG.getValueType(VA.getValVT()));
00854       
00855       if (VA.getLocInfo() != CCValAssign::Full)
00856         ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
00857 
00858       ArgValues.push_back(ArgValue);
00859 
00860       // To meet ABI, when VARARGS are passed on registers, the registers
00861       // must have their values written to the caller stack frame. 
00862       if ((isVarArg) && (Subtarget->isABI_O32())) {
00863         if (StackPtr.getNode() == 0)
00864           StackPtr = DAG.getRegister(StackReg, getPointerTy());
00865      
00866         // The stack pointer offset is relative to the caller stack frame. 
00867         // Since the real stack size is unknown here, a negative SPOffset 
00868         // is used so there's a way to adjust these offsets when the stack
00869         // size get known (on EliminateFrameIndex). A dummy SPOffset is 
00870         // used instead of a direct negative address (which is recorded to
00871         // be used on emitPrologue) to avoid mis-calc of the first stack 
00872         // offset on PEI::calculateFrameObjectOffsets.
00873         // Arguments are always 32-bit.
00874         int FI = MFI->CreateFixedObject(4, 0);
00875         MipsFI->recordStoreVarArgsFI(FI, -(4+(i*4)));
00876         SDValue PtrOff = DAG.getFrameIndex(FI, getPointerTy());
00877       
00878         // emit ISD::STORE whichs stores the 
00879         // parameter value to a stack Location
00880         ArgValues.push_back(DAG.getStore(Root, ArgValue, PtrOff, NULL, 0));
00881       }
00882 
00883     } else { // VA.isRegLoc()
00884 
00885       // sanity check
00886       assert(VA.isMemLoc());
00887       
00888       // The stack pointer offset is relative to the caller stack frame. 
00889       // Since the real stack size is unknown here, a negative SPOffset 
00890       // is used so there's a way to adjust these offsets when the stack
00891       // size get known (on EliminateFrameIndex). A dummy SPOffset is 
00892       // used instead of a direct negative address (which is recorded to
00893       // be used on emitPrologue) to avoid mis-calc of the first stack 
00894       // offset on PEI::calculateFrameObjectOffsets.
00895       // Arguments are always 32-bit.
00896       unsigned ArgSize = VA.getLocVT().getSizeInBits()/8;
00897       int FI = MFI->CreateFixedObject(ArgSize, 0);
00898       MipsFI->recordLoadArgsFI(FI, -(ArgSize+
00899         (FirstStackArgLoc + VA.getLocMemOffset())));
00900 
00901       // Create load nodes to retrieve arguments from the stack
00902       SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
00903       ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
00904     }
00905   }
00906 
00907   // The mips ABIs for returning structs by value requires that we copy
00908   // the sret argument into $v0 for the return. Save the argument into
00909   // a virtual register so that we can access it from the return points.
00910   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
00911     unsigned Reg = MipsFI->getSRetReturnReg();
00912     if (!Reg) {
00913       Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i32));
00914       MipsFI->setSRetReturnReg(Reg);
00915     }
00916     SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
00917     Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
00918   }
00919 
00920   ArgValues.push_back(Root);
00921 
00922   // Return the new list of results.
00923   return DAG.getNode(ISD::MERGE_VALUES, Op.getNode()->getVTList(),
00924                      &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
00925 }
00926 
00927 //===----------------------------------------------------------------------===//
00928 //               Return Value Calling Convention Implementation
00929 //===----------------------------------------------------------------------===//
00930 
00931 SDValue MipsTargetLowering::
00932 LowerRET(SDValue Op, SelectionDAG &DAG)
00933 {
00934   // CCValAssign - represent the assignment of
00935   // the return value to a location
00936   SmallVector<CCValAssign, 16> RVLocs;
00937   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
00938   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
00939 
00940   // CCState - Info about the registers and stack slot.
00941   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
00942 
00943   // Analize return values of ISD::RET
00944   CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Mips);
00945 
00946   // If this is the first return lowered for this function, add 
00947   // the regs to the liveout set for the function.
00948   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
00949     for (unsigned i = 0; i != RVLocs.size(); ++i)
00950       if (RVLocs[i].isRegLoc())
00951         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
00952   }
00953 
00954   // The chain is always operand #0
00955   SDValue Chain = Op.getOperand(0);
00956   SDValue Flag;
00957 
00958   // Copy the result values into the output registers.
00959   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00960     CCValAssign &VA = RVLocs[i];
00961     assert(VA.isRegLoc() && "Can only return in registers!");
00962 
00963     // ISD::RET => ret chain, (regnum1,val1), ...
00964     // So i*2+1 index only the regnums
00965     Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
00966 
00967     // guarantee that all emitted copies are
00968     // stuck together, avoiding something bad
00969     Flag = Chain.getValue(1);
00970   }
00971 
00972   // The mips ABIs for returning structs by value requires that we copy
00973   // the sret argument into $v0 for the return. We saved the argument into
00974   // a virtual register in the entry block, so now we copy the value out
00975   // and into $v0.
00976   if (DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
00977     MachineFunction &MF      = DAG.getMachineFunction();
00978     MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>();
00979     unsigned Reg = MipsFI->getSRetReturnReg();
00980 
00981     if (!Reg) 
00982       assert(0 && "sret virtual register not created in the entry block");
00983     SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
00984 
00985     Chain = DAG.getCopyToReg(Chain, Mips::V0, Val, Flag);
00986     Flag = Chain.getValue(1);
00987   }
00988 
00989   // Return on Mips is always a "jr $ra"
00990   if (Flag.getNode())
00991     return DAG.getNode(MipsISD::Ret, MVT::Other, 
00992                        Chain, DAG.getRegister(Mips::RA, MVT::i32), Flag);
00993   else // Return Void
00994     return DAG.getNode(MipsISD::Ret, MVT::Other, 
00995                        Chain, DAG.getRegister(Mips::RA, MVT::i32));
00996 }
00997 
00998 //===----------------------------------------------------------------------===//
00999 //                           Mips Inline Assembly Support
01000 //===----------------------------------------------------------------------===//
01001 
01002 /// getConstraintType - Given a constraint letter, return the type of
01003 /// constraint it is for this target.
01004 MipsTargetLowering::ConstraintType MipsTargetLowering::
01005 getConstraintType(const std::string &Constraint) const 
01006 {
01007   // Mips specific constrainy 
01008   // GCC config/mips/constraints.md
01009   //
01010   // 'd' : An address register. Equivalent to r 
01011   //       unless generating MIPS16 code. 
01012   // 'y' : Equivalent to r; retained for 
01013   //       backwards compatibility. 
01014   // 'f' : Floating Point registers.      
01015   if (Constraint.size() == 1) {
01016     switch (Constraint[0]) {
01017       default : break;
01018       case 'd':     
01019       case 'y': 
01020       case 'f':
01021         return C_RegisterClass;
01022         break;
01023     }
01024   }
01025   return TargetLowering::getConstraintType(Constraint);
01026 }
01027 
01028 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
01029 /// return a list of registers that can be used to satisfy the constraint.
01030 /// This should only be used for C_RegisterClass constraints.
01031 std::pair<unsigned, const TargetRegisterClass*> MipsTargetLowering::
01032 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const
01033 {
01034   if (Constraint.size() == 1) {
01035     switch (Constraint[0]) {
01036     case 'r':
01037       return std::make_pair(0U, Mips::CPURegsRegisterClass);
01038     case 'f':
01039       if (VT == MVT::f32) {
01040         if (Subtarget->isSingleFloat())
01041           return std::make_pair(0U, Mips::FGR32RegisterClass);
01042         else
01043           return std::make_pair(0U, Mips::AFGR32RegisterClass);
01044       }
01045       if (VT == MVT::f64)    
01046         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
01047           return std::make_pair(0U, Mips::AFGR64RegisterClass);
01048     }
01049   }
01050   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
01051 }
01052 
01053 /// Given a register class constraint, like 'r', if this corresponds directly
01054 /// to an LLVM register class, return a register of 0 and the register class
01055 /// pointer.
01056 std::vector<unsigned> MipsTargetLowering::
01057 getRegClassForInlineAsmConstraint(const std::string &Constraint,
01058                                   MVT VT) const
01059 {
01060   if (Constraint.size() != 1)
01061     return std::vector<unsigned>();
01062 
01063   switch (Constraint[0]) {         
01064     default : break;
01065     case 'r':
01066     // GCC Mips Constraint Letters
01067     case 'd':     
01068     case 'y': 
01069       return make_vector<unsigned>(Mips::T0, Mips::T1, Mips::T2, Mips::T3, 
01070              Mips::T4, Mips::T5, Mips::T6, Mips::T7, Mips::S0, Mips::S1, 
01071              Mips::S2, Mips::S3, Mips::S4, Mips::S5, Mips::S6, Mips::S7, 
01072              Mips::T8, 0);
01073 
01074     case 'f':
01075       if (VT == MVT::f32) {
01076         if (Subtarget->isSingleFloat())
01077           return make_vector<unsigned>(Mips::F2, Mips::F3, Mips::F4, Mips::F5,
01078                  Mips::F6, Mips::F7, Mips::F8, Mips::F9, Mips::F10, Mips::F11,
01079                  Mips::F20, Mips::F21, Mips::F22, Mips::F23, Mips::F24,
01080                  Mips::F25, Mips::F26, Mips::F27, Mips::F28, Mips::F29,
01081                  Mips::F30, Mips::F31, 0);
01082         else
01083           return make_vector<unsigned>(Mips::F2, Mips::F4, Mips::F6, Mips::F8, 
01084                  Mips::F10, Mips::F20, Mips::F22, Mips::F24, Mips::F26, 
01085                  Mips::F28, Mips::F30, 0);
01086       }
01087 
01088       if (VT == MVT::f64)    
01089         if ((!Subtarget->isSingleFloat()) && (!Subtarget->isFP64bit()))
01090           return make_vector<unsigned>(Mips::D1, Mips::D2, Mips::D3, Mips::D4, 
01091                  Mips::D5, Mips::D10, Mips::D11, Mips::D12, Mips::D13, 
01092                  Mips::D14, Mips::D15, 0);
01093   }
01094   return std::vector<unsigned>();
01095 }
01096 
01097 bool
01098 MipsTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
01099   // The Mips target isn't yet aware of offsets.
01100   return false;
01101 }