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->getABITypeSize(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->hasInternalLinkage()) {
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->hasInternalLinkage() || 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()->getABITypeSize(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 /// LowerCCCCallTo - functions arguments are copied from virtual
00577 /// regs to (physical regs)/(stack frame), CALLSEQ_START and
00578 /// CALLSEQ_END are emitted.
00579 /// TODO: isVarArg, isTailCall.
00580 SDValue MipsTargetLowering::
00581 LowerCALL(SDValue Op, SelectionDAG &DAG)
00582 {
00583   MachineFunction &MF = DAG.getMachineFunction();
00584 
00585   CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
00586   SDValue Chain = TheCall->getChain();
00587   SDValue Callee = TheCall->getCallee();
00588   bool isVarArg = TheCall->isVarArg();
00589   unsigned CC = TheCall->getCallingConv();
00590 
00591   MachineFrameInfo *MFI = MF.getFrameInfo();
00592 
00593   // Analyze operands of the call, assigning locations to each operand.
00594   SmallVector<CCValAssign, 16> ArgLocs;
00595   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
00596 
00597   // To meet O32 ABI, Mips must always allocate 16 bytes on
00598   // the stack (even if less than 4 are used as arguments)
00599   if (Subtarget->isABI_O32()) {
00600     int VTsize = MVT(MVT::i32).getSizeInBits()/8;
00601     MFI->CreateFixedObject(VTsize, (VTsize*3));
00602   }
00603 
00604   CCInfo.AnalyzeCallOperands(TheCall, CC_Mips);
00605   
00606   // Get a count of how many bytes are to be pushed on the stack.
00607   unsigned NumBytes = CCInfo.getNextStackOffset();
00608   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
00609 
00610   // With EABI is it possible to have 16 args on registers.
00611   SmallVector<std::pair<unsigned, SDValue>, 16> RegsToPass;
00612   SmallVector<SDValue, 8> MemOpChains;
00613 
00614   // First/LastArgStackLoc contains the first/last 
00615   // "at stack" argument location.
00616   int LastArgStackLoc = 0;
00617   unsigned FirstStackArgLoc = (Subtarget->isABI_EABI() ? 0 : 16);
00618 
00619   // Walk the register/memloc assignments, inserting copies/loads.
00620   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00621     CCValAssign &VA = ArgLocs[i];
00622 
00623     // Arguments start after the 5 first operands of ISD::CALL
00624     SDValue Arg = TheCall->getArg(i);
00625     
00626     // Promote the value if needed.
00627     switch (VA.getLocInfo()) {
00628     default: assert(0 && "Unknown loc info!");
00629     case CCValAssign::Full: break;
00630     case CCValAssign::SExt:
00631       Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
00632       break;
00633     case CCValAssign::ZExt:
00634       Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
00635       break;
00636     case CCValAssign::AExt:
00637       Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
00638       break;
00639     }
00640     
00641     // Arguments that can be passed on register must be kept at 
00642     // RegsToPass vector
00643     if (VA.isRegLoc()) {
00644       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
00645       continue;
00646     }
00647     
00648     // Register cant get to this point...
00649     assert(VA.isMemLoc());
00650     
00651     // Create the frame index object for this incoming parameter
00652     // This guarantees that when allocating Local Area the firsts
00653     // 16 bytes which are alwayes reserved won't be overwritten
00654     // if O32 ABI is used. For EABI the first address is zero.
00655     LastArgStackLoc = (FirstStackArgLoc + VA.getLocMemOffset());
00656     int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
00657                                     LastArgStackLoc);
00658 
00659     SDValue PtrOff = DAG.getFrameIndex(FI,getPointerTy());
00660 
00661     // emit ISD::STORE whichs stores the 
00662     // parameter value to a stack Location
00663     MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
00664   }
00665 
00666   // Transform all store nodes into one single node because all store
00667   // nodes are independent of each other.
00668   if (!MemOpChains.empty())     
00669     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, 
00670                         &MemOpChains[0], MemOpChains.size());
00671 
00672   // Build a sequence of copy-to-reg nodes chained together with token 
00673   // chain and flag operands which copy the outgoing args into registers.
00674   // The InFlag in necessary since all emited instructions must be
00675   // stuck together.
00676   SDValue InFlag;
00677   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00678     Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, 
00679                              RegsToPass[i].second, InFlag);
00680     InFlag = Chain.getValue(1);
00681   }
00682 
00683   // If the callee is a GlobalAddress/ExternalSymbol node (quite common, every
00684   // direct call is) turn it into a TargetGlobalAddress/TargetExternalSymbol 
00685   // node so that legalize doesn't hack it. 
00686   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 
00687     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
00688   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
00689     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
00690 
00691 
00692   // MipsJmpLink = #chain, #target_address, #opt_in_flags...
00693   //             = Chain, Callee, Reg#1, Reg#2, ...  
00694   //
00695   // Returns a chain & a flag for retval copy to use.
00696   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
00697   SmallVector<SDValue, 8> Ops;
00698   Ops.push_back(Chain);
00699   Ops.push_back(Callee);
00700 
00701   // Add argument registers to the end of the list so that they are 
00702   // known live into the call.
00703   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
00704     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
00705                                   RegsToPass[i].second.getValueType()));
00706 
00707   if (InFlag.getNode())
00708     Ops.push_back(InFlag);
00709 
00710   Chain  = DAG.getNode(MipsISD::JmpLink, NodeTys, &Ops[0], Ops.size());
00711   InFlag = Chain.getValue(1);
00712 
00713   // Create the CALLSEQ_END node.
00714   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(Num