LLVM API Documentation

SparcISelLowering.cpp

Go to the documentation of this file.
00001 //===-- SparcISelLowering.cpp - Sparc 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 implements the interfaces that Sparc uses to lower LLVM code into a
00011 // selection DAG.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "SparcISelLowering.h"
00016 #include "SparcTargetMachine.h"
00017 #include "llvm/Function.h"
00018 #include "llvm/CodeGen/CallingConvLower.h"
00019 #include "llvm/CodeGen/MachineFrameInfo.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineInstrBuilder.h"
00022 #include "llvm/CodeGen/MachineRegisterInfo.h"
00023 #include "llvm/CodeGen/SelectionDAG.h"
00024 #include "llvm/Target/TargetLoweringObjectFile.h"
00025 #include "llvm/ADT/VectorExtras.h"
00026 #include "llvm/Support/ErrorHandling.h"
00027 using namespace llvm;
00028 
00029 
00030 //===----------------------------------------------------------------------===//
00031 // Calling Convention Implementation
00032 //===----------------------------------------------------------------------===//
00033 
00034 #include "SparcGenCallingConv.inc"
00035 
00036 SDValue
00037 SparcTargetLowering::LowerReturn(SDValue Chain,
00038                                  unsigned CallConv, bool isVarArg,
00039                                  const SmallVectorImpl<ISD::OutputArg> &Outs,
00040                                  DebugLoc dl, SelectionDAG &DAG) {
00041 
00042   // CCValAssign - represent the assignment of the return value to locations.
00043   SmallVector<CCValAssign, 16> RVLocs;
00044 
00045   // CCState - Info about the registers and stack slot.
00046   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
00047                  RVLocs, *DAG.getContext());
00048 
00049   // Analize return values.
00050   CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
00051 
00052   // If this is the first return lowered for this function, add the regs to the
00053   // liveout set for the function.
00054   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
00055     for (unsigned i = 0; i != RVLocs.size(); ++i)
00056       if (RVLocs[i].isRegLoc())
00057         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
00058   }
00059 
00060   SDValue Flag;
00061 
00062   // Copy the result values into the output registers.
00063   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00064     CCValAssign &VA = RVLocs[i];
00065     assert(VA.isRegLoc() && "Can only return in registers!");
00066 
00067     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), 
00068                              Outs[i].Val, Flag);
00069 
00070     // Guarantee that all emitted copies are stuck together with flags.
00071     Flag = Chain.getValue(1);
00072   }
00073 
00074   if (Flag.getNode())
00075     return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
00076   return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
00077 }
00078 
00079 /// LowerFormalArguments - V8 uses a very simple ABI, where all values are
00080 /// passed in either one or two GPRs, including FP values.  TODO: we should
00081 /// pass FP values in FP registers for fastcc functions.
00082 SDValue
00083 SparcTargetLowering::LowerFormalArguments(SDValue Chain,
00084                                           unsigned CallConv, bool isVarArg,
00085                                           const SmallVectorImpl<ISD::InputArg>
00086                                             &Ins,
00087                                           DebugLoc dl, SelectionDAG &DAG,
00088                                           SmallVectorImpl<SDValue> &InVals) {
00089 
00090   MachineFunction &MF = DAG.getMachineFunction();
00091   MachineRegisterInfo &RegInfo = MF.getRegInfo();
00092 
00093   // Assign locations to all of the incoming arguments.
00094   SmallVector<CCValAssign, 16> ArgLocs;
00095   CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
00096                  ArgLocs, *DAG.getContext());
00097   CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
00098 
00099   static const unsigned ArgRegs[] = {
00100     SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
00101   };
00102   const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
00103   unsigned ArgOffset = 68;
00104 
00105   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00106     SDValue ArgValue;
00107     CCValAssign &VA = ArgLocs[i];
00108     // FIXME: We ignore the register assignments of AnalyzeFormalArguments
00109     // because it doesn't know how to split a double into two i32 registers.
00110     EVT ObjectVT = VA.getValVT();
00111     switch (ObjectVT.getSimpleVT().SimpleTy) {
00112     default: llvm_unreachable("Unhandled argument type!");
00113     case MVT::i1:
00114     case MVT::i8:
00115     case MVT::i16:
00116     case MVT::i32:
00117       if (!Ins[i].Used) {                  // Argument is dead.
00118         if (CurArgReg < ArgRegEnd) ++CurArgReg;
00119         InVals.push_back(DAG.getUNDEF(ObjectVT));
00120       } else if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
00121         unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
00122         MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
00123         SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
00124         if (ObjectVT != MVT::i32) {
00125           unsigned AssertOp = ISD::AssertSext;
00126           Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
00127                             DAG.getValueType(ObjectVT));
00128           Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
00129         }
00130         InVals.push_back(Arg);
00131       } else {
00132         int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
00133         SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
00134         SDValue Load;
00135         if (ObjectVT == MVT::i32) {
00136           Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
00137         } else {
00138           ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
00139 
00140           // Sparc is big endian, so add an offset based on the ObjectVT.
00141           unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
00142           FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
00143                               DAG.getConstant(Offset, MVT::i32));
00144           Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
00145                                 NULL, 0, ObjectVT);
00146           Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
00147         }
00148         InVals.push_back(Load);
00149       }
00150 
00151       ArgOffset += 4;
00152       break;
00153     case MVT::f32:
00154       if (!Ins[i].Used) {                  // Argument is dead.
00155         if (CurArgReg < ArgRegEnd) ++CurArgReg;
00156         InVals.push_back(DAG.getUNDEF(ObjectVT));
00157       } else if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
00158         // FP value is passed in an integer register.
00159         unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
00160         MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
00161         SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
00162 
00163         Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
00164         InVals.push_back(Arg);
00165       } else {
00166         int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
00167         SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
00168         SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0);
00169         InVals.push_back(Load);
00170       }
00171       ArgOffset += 4;
00172       break;
00173 
00174     case MVT::i64:
00175     case MVT::f64:
00176       if (!Ins[i].Used) {                // Argument is dead.
00177         if (CurArgReg < ArgRegEnd) ++CurArgReg;
00178         if (CurArgReg < ArgRegEnd) ++CurArgReg;
00179         InVals.push_back(DAG.getUNDEF(ObjectVT));
00180       } else {
00181         SDValue HiVal;
00182         if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
00183           unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
00184           MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
00185           HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
00186         } else {
00187           int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
00188           SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
00189           HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
00190         }
00191 
00192         SDValue LoVal;
00193         if (CurArgReg < ArgRegEnd) {  // Lives in an incoming GPR
00194           unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
00195           MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
00196           LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
00197         } else {
00198           int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
00199           SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
00200           LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
00201         }
00202 
00203         // Compose the two halves together into an i64 unit.
00204         SDValue WholeValue =
00205           DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
00206 
00207         // If we want a double, do a bit convert.
00208         if (ObjectVT == MVT::f64)
00209           WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
00210 
00211         InVals.push_back(WholeValue);
00212       }
00213       ArgOffset += 8;
00214       break;
00215     }
00216   }
00217 
00218   // Store remaining ArgRegs to the stack if this is a varargs function.
00219   if (isVarArg) {
00220     // Remember the vararg offset for the va_start implementation.
00221     VarArgsFrameOffset = ArgOffset;
00222 
00223     std::vector<SDValue> OutChains;
00224 
00225     for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
00226       unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
00227       MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
00228       SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
00229 
00230       int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
00231       SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
00232 
00233       OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0));
00234       ArgOffset += 4;
00235     }
00236 
00237     if (!OutChains.empty()) {
00238       OutChains.push_back(Chain);
00239       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
00240                           &OutChains[0], OutChains.size());
00241     }
00242   }
00243 
00244   return Chain;
00245 }
00246 
00247 SDValue
00248 SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
00249                                unsigned CallConv, bool isVarArg,
00250                                bool isTailCall,
00251                                const SmallVectorImpl<ISD::OutputArg> &Outs,
00252                                const SmallVectorImpl<ISD::InputArg> &Ins,
00253                                DebugLoc dl, SelectionDAG &DAG,
00254                                SmallVectorImpl<SDValue> &InVals) {
00255 
00256 #if 0
00257   // Analyze operands of the call, assigning locations to each operand.
00258   SmallVector<CCValAssign, 16> ArgLocs;
00259   CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
00260   CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
00261 
00262   // Get the size of the outgoing arguments stack space requirement.
00263   unsigned ArgsSize = CCInfo.getNextStackOffset();
00264   // FIXME: We can't use this until f64 is known to take two GPRs.
00265 #else
00266   (void)CC_Sparc32;
00267 
00268   // Count the size of the outgoing arguments.
00269   unsigned ArgsSize = 0;
00270   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
00271     switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
00272       default: llvm_unreachable("Unknown value type!");
00273       case MVT::i1:
00274       case MVT::i8:
00275       case MVT::i16:
00276       case MVT::i32:
00277       case MVT::f32:
00278         ArgsSize += 4;
00279         break;
00280       case MVT::i64:
00281       case MVT::f64:
00282         ArgsSize += 8;
00283         break;
00284     }
00285   }
00286   if (ArgsSize > 4*6)
00287     ArgsSize -= 4*6;    // Space for first 6 arguments is prereserved.
00288   else
00289     ArgsSize = 0;
00290 #endif
00291 
00292   // Keep stack frames 8-byte aligned.
00293   ArgsSize = (ArgsSize+7) & ~7;
00294 
00295   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
00296 
00297   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
00298   SmallVector<SDValue, 8> MemOpChains;
00299 
00300 #if 0
00301   // Walk the register/memloc assignments, inserting copies/loads.
00302   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
00303     CCValAssign &VA = ArgLocs[i];
00304     SDValue Arg = Outs[i].Val;
00305 
00306     // Promote the value if needed.
00307     switch (VA.getLocInfo()) {
00308     default: llvm_unreachable("Unknown loc info!");
00309     case CCValAssign::Full: break;
00310     case CCValAssign::SExt:
00311       Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
00312       break;
00313     case CCValAssign::ZExt:
00314       Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
00315       break;
00316     case CCValAssign::AExt:
00317       Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
00318       break;
00319     }
00320 
00321     // Arguments that can be passed on register must be kept at
00322     // RegsToPass vector
00323     if (VA.isRegLoc()) {
00324       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
00325       continue;
00326     }
00327 
00328     assert(VA.isMemLoc());
00329 
00330     // Create a store off the stack pointer for this argument.
00331     SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
00332     // FIXME: VERIFY THAT 68 IS RIGHT.
00333     SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
00334     PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
00335     MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
00336   }
00337 
00338 #else
00339   static const unsigned ArgRegs[] = {
00340     SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
00341   };
00342   unsigned ArgOffset = 68;
00343 
00344   for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
00345     SDValue Val = Outs[i].Val;
00346     EVT ObjectVT = Val.getValueType();
00347     SDValue ValToStore(0, 0);
00348     unsigned ObjSize;
00349     switch (ObjectVT.getSimpleVT().SimpleTy) {
00350     default: llvm_unreachable("Unhandled argument type!");
00351     case MVT::i32:
00352       ObjSize = 4;
00353 
00354       if (RegsToPass.size() >= 6) {
00355         ValToStore = Val;
00356       } else {
00357         RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
00358       }
00359       break;
00360     case MVT::f32:
00361       ObjSize = 4;
00362       if (RegsToPass.size() >= 6) {
00363         ValToStore = Val;
00364       } else {
00365         // Convert this to a FP value in an int reg.
00366         Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
00367         RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
00368       }
00369       break;
00370     case MVT::f64: {
00371       ObjSize = 8;
00372       if (RegsToPass.size() >= 6) {
00373         ValToStore = Val;    // Whole thing is passed in memory.
00374         break;
00375       }
00376 
00377       // Break into top and bottom parts by storing to the stack and loading
00378       // out the parts as integers.  Top part goes in a reg.
00379       SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
00380       SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, 
00381                                    Val, StackPtr, NULL, 0);
00382       // Sparc is big-endian, so the high part comes first.
00383       SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
00384       // Increment the pointer to the other half.
00385       StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
00386                              DAG.getIntPtrConstant(4));
00387       // Load the low part.
00388       SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
00389 
00390       RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
00391 
00392       if (RegsToPass.size() >= 6) {
00393         ValToStore = Lo;
00394         ArgOffset += 4;
00395         ObjSize = 4;
00396       } else {
00397         RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
00398       }
00399       break;
00400     }
00401     case MVT::i64: {
00402       ObjSize = 8;
00403       if (RegsToPass.size() >= 6) {
00404         ValToStore = Val;    // Whole thing is passed in memory.
00405         break;
00406       }
00407 
00408       // Split the value into top and bottom part.  Top part goes in a reg.
00409       SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
00410                                  DAG.getConstant(1, MVT::i32));
00411       SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
00412                                  DAG.getConstant(0, MVT::i32));
00413       RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
00414 
00415       if (RegsToPass.size() >= 6) {
00416         ValToStore = Lo;
00417         ArgOffset += 4;
00418         ObjSize = 4;
00419       } else {
00420         RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
00421       }
00422       break;
00423     }
00424     }
00425 
00426     if (ValToStore.getNode()) {
00427       SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
00428       SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
00429       PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
00430       MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore, 
00431                                          PtrOff, NULL, 0));
00432     }
00433     ArgOffset += ObjSize;
00434   }
00435 #endif
00436 
00437   // Emit all stores, make sure the occur before any copies into physregs.
00438   if (!MemOpChains.empty())
00439     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
00440                         &MemOpChains[0], MemOpChains.size());
00441 
00442   // Build a sequence of copy-to-reg nodes chained together with token
00443   // chain and flag operands which copy the outgoing args into registers.
00444   // The InFlag in necessary since all emited instructions must be
00445   // stuck together.
00446   SDValue InFlag;
00447   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
00448     unsigned Reg = RegsToPass[i].first;
00449     // Remap I0->I7 -> O0->O7.
00450     if (Reg >= SP::I0 && Reg <= SP::I7)
00451       Reg = Reg-SP::I0+SP::O0;
00452 
00453     Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
00454     InFlag = Chain.getValue(1);
00455   }
00456 
00457   // If the callee is a GlobalAddress node (quite common, every direct call is)
00458   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
00459   // Likewise ExternalSymbol -> TargetExternalSymbol.
00460   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
00461     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
00462   else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
00463     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
00464 
00465   std::vector<EVT> NodeTys;
00466   NodeTys.push_back(MVT::Other);   // Returns a chain
00467   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
00468   SDValue Ops[] = { Chain, Callee, InFlag };
00469   Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
00470   InFlag = Chain.getValue(1);
00471 
00472   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
00473                              DAG.getIntPtrConstant(0, true), InFlag);
00474   InFlag = Chain.getValue(1);
00475 
00476   // Assign locations to each value returned by this call.
00477   SmallVector<CCValAssign, 16> RVLocs;
00478   CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
00479                  RVLocs, *DAG.getContext());
00480 
00481   RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
00482 
00483   // Copy all of the result registers out of their specified physreg.
00484   for (unsigned i = 0; i != RVLocs.size(); ++i) {
00485     unsigned Reg = RVLocs[i].getLocReg();
00486 
00487     // Remap I0->I7 -> O0->O7.
00488     if (Reg >= SP::I0 && Reg <= SP::I7)
00489       Reg = Reg-SP::I0+SP::O0;
00490 
00491     Chain = DAG.getCopyFromReg(Chain, dl, Reg,
00492                                RVLocs[i].getValVT(), InFlag).getValue(1);
00493     InFlag = Chain.getValue(2);
00494     InVals.push_back(Chain.getValue(0));
00495   }
00496 
00497   return Chain;
00498 }
00499 
00500 
00501 
00502 //===----------------------------------------------------------------------===//
00503 // TargetLowering Implementation
00504 //===----------------------------------------------------------------------===//
00505 
00506 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
00507 /// condition.
00508 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
00509   switch (CC) {
00510   default: llvm_unreachable("Unknown integer condition code!");
00511   case ISD::SETEQ:  return SPCC::ICC_E;
00512   case ISD::SETNE:  return SPCC::ICC_NE;
00513   case ISD::SETLT:  return SPCC::ICC_L;
00514   case ISD::SETGT:  return SPCC::ICC_G;
00515   case ISD::SETLE:  return SPCC::ICC_LE;
00516   case ISD::SETGE:  return SPCC::ICC_GE;
00517   case ISD::SETULT: return SPCC::ICC_CS;
00518   case ISD::SETULE: return SPCC::ICC_LEU;
00519   case ISD::SETUGT: return SPCC::ICC_GU;
00520   case ISD::SETUGE: return SPCC::ICC_CC;
00521   }
00522 }
00523 
00524 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
00525 /// FCC condition.
00526 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
00527   switch (CC) {
00528   default: llvm_unreachable("Unknown fp condition code!");
00529   case ISD::SETEQ:
00530   case ISD::SETOEQ: return SPCC::FCC_E;
00531   case ISD::SETNE:
00532   case ISD::SETUNE: return SPCC::FCC_NE;
00533   case ISD::SETLT:
00534   case ISD::SETOLT: return SPCC::FCC_L;
00535   case ISD::SETGT:
00536   case ISD::SETOGT: return SPCC::FCC_G;
00537   case ISD::SETLE:
00538   case ISD::SETOLE: return SPCC::FCC_LE;
00539   case ISD::SETGE:
00540   case ISD::SETOGE: return SPCC::FCC_GE;
00541   case ISD::SETULT: return SPCC::FCC_UL;
00542   case ISD::SETULE: return SPCC::FCC_ULE;
00543   case ISD::SETUGT: return SPCC::FCC_UG;
00544   case ISD::SETUGE: return SPCC::FCC_UGE;
00545   case ISD::SETUO:  return SPCC::FCC_U;
00546   case ISD::SETO:   return SPCC::FCC_O;
00547   case ISD::SETONE: return SPCC::FCC_LG;
00548   case ISD::SETUEQ: return SPCC::FCC_UE;
00549   }
00550 }
00551 
00552 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
00553   : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
00554 
00555   // Set up the register classes.
00556   addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
00557   addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
00558   addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
00559 
00560   // Turn FP extload into load/fextend
00561   setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
00562   // Sparc doesn't have i1 sign extending load
00563   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
00564   // Turn FP truncstore into trunc + store.
00565   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
00566 
00567   // Custom legalize GlobalAddress nodes into LO/HI parts.
00568   setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
00569   setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
00570   setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
00571 
00572   // Sparc doesn't have sext_inreg, replace them with shl/sra
00573   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
00574   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
00575   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
00576 
00577   // Sparc has no REM or DIVREM operations.
00578   setOperationAction(ISD::UREM, MVT::i32, Expand);
00579   setOperationAction(ISD::SREM, MVT::i32, Expand);
00580   setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
00581   setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
00582 
00583   // Custom expand fp<->sint
00584   setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
00585   setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
00586 
00587   // Expand fp<->uint
00588   setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
00589   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
00590 
00591   setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
00592   setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
00593 
00594   // Sparc has no select or setcc: expand to SELECT_CC.
00595   setOperationAction(ISD::SELECT, MVT::i32, Expand);
00596   setOperationAction(ISD::SELECT, MVT::f32, Expand);
00597   setOperationAction(ISD::SELECT, MVT::f64, Expand);
00598   setOperationAction(ISD::SETCC, MVT::i32, Expand);
00599   setOperationAction(ISD::SETCC, MVT::f32, Expand);
00600   setOperationAction(ISD::SETCC, MVT::f64, Expand);
00601 
00602   // Sparc doesn't have BRCOND either, it has BR_CC.
00603   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
00604   setOperationAction(ISD::BRIND, MVT::Other, Expand);
00605   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
00606   setOperationAction(ISD::BR_CC, MVT::i32, Custom);
00607   setOperationAction(ISD::BR_CC, MVT::f32, Custom);
00608   setOperationAction(ISD::BR_CC, MVT::f64, Custom);
00609 
00610   setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
00611   setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
00612   setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
00613 
00614   // SPARC has no intrinsics for these particular operations.
00615   setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
00616 
00617   setOperationAction(ISD::FSIN , MVT::f64, Expand);
00618   setOperationAction(ISD::FCOS , MVT::f64, Expand);
00619   setOperationAction(ISD::FREM , MVT::f64, Expand);
00620   setOperationAction(ISD::FSIN , MVT::f32, Expand);
00621   setOperationAction(ISD::FCOS , MVT::f32, Expand);
00622   setOperationAction(ISD::FREM , MVT::f32, Expand);
00623   setOperationAction(ISD::CTPOP, MVT::i32, Expand);
00624   setOperationAction(ISD::CTTZ , MVT::i32, Expand);
00625   setOperationAction(ISD::CTLZ , MVT::i32, Expand);
00626   setOperationAction(ISD::ROTL , MVT::i32, Expand);
00627   setOperationAction(ISD::ROTR , MVT::i32, Expand);
00628   setOperationAction(ISD::BSWAP, MVT::i32, Expand);
00629   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
00630   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
00631   setOperationAction(ISD::FPOW , MVT::f64, Expand);
00632   setOperationAction(ISD::FPOW , MVT::f32, Expand);
00633 
00634   setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
00635   setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
00636   setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
00637 
00638   // FIXME: Sparc provides these multiplies, but we don't have them yet.
00639   setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
00640   setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
00641 
00642   // We don't have line number support yet.
00643   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
00644   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
00645   setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
00646   setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
00647 
00648   // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
00649   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
00650   // VAARG needs to be lowered to not do unaligned accesses for doubles.
00651   setOperationAction(ISD::VAARG             , MVT::Other, Custom);
00652 
00653   // Use the default implementation.
00654   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
00655   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
00656   setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
00657   setOperationAction(ISD::STACKRESTORE      , MVT::Other, Expand);
00658   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Custom);
00659 
00660   // No debug info support yet.
00661   setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
00662   setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
00663   setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
00664   setOperationAction(ISD::DECLARE, MVT::Other, Expand);
00665 
00666   setStackPointerRegisterToSaveRestore(SP::O6);
00667 
00668   if (TM.getSubtarget<SparcSubtarget>().isV9())
00669     setOperationAction(ISD::CTPOP, MVT::i32, Legal);
00670 
00671   computeRegisterProperties();
00672 }
00673 
00674 const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
00675   switch (Opcode) {
00676   default: return 0;
00677   case SPISD::CMPICC:     return "SPISD::CMPICC";
00678   case SPISD::CMPFCC:     return "SPISD::CMPFCC";
00679   case SPISD::BRICC:      return "SPISD::BRICC";
00680   case SPISD::BRFCC:      return "SPISD::BRFCC";
00681   case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
00682   case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
00683   case SPISD::Hi:         return "SPISD::Hi";
00684   case SPISD::Lo:         return "SPISD::Lo";
00685   case SPISD::FTOI:       return "SPISD::FTOI";
00686   case SPISD::ITOF:       return "SPISD::ITOF";
00687   case SPISD::CALL:       return "SPISD::CALL";
00688   case SPISD::RET_FLAG:   return "SPISD::RET_FLAG";
00689   }
00690 }
00691 
00692 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
00693 /// be zero. Op is expected to be a target specific node. Used by DAG
00694 /// combiner.
00695 void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
00696                                                          const APInt &Mask,
00697                                                          APInt &KnownZero,
00698                                                          APInt &KnownOne,
00699                                                          const SelectionDAG &DAG,
00700                                                          unsigned Depth) const {
00701   APInt KnownZero2, KnownOne2;
00702   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);   // Don't know anything.
00703 
00704   switch (Op.getOpcode()) {
00705   default: break;
00706   case SPISD::SELECT_ICC:
00707   case SPISD::SELECT_FCC:
00708     DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
00709                           Depth+1);
00710     DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
00711                           Depth+1);
00712     assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
00713     assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
00714 
00715     // Only known if known in both the LHS and RHS.
00716     KnownOne &= KnownOne2;
00717     KnownZero &= KnownZero2;
00718     break;
00719   }
00720 }
00721 
00722 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction.  If so
00723 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
00724 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
00725                              ISD::CondCode CC, unsigned &SPCC) {
00726   if (isa<ConstantSDNode>(RHS) &&
00727       cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
00728       CC == ISD::SETNE &&
00729       ((LHS.getOpcode() == SPISD::SELECT_ICC &&
00730         LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
00731        (LHS.getOpcode() == SPISD::SELECT_FCC &&
00732         LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
00733       isa<ConstantSDNode>(LHS.getOperand(0)) &&
00734       isa<ConstantSDNode>(LHS.getOperand(1)) &&
00735       cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
00736       cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
00737     SDValue CMPCC = LHS.getOperand(3);
00738     SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
00739     LHS = CMPCC.getOperand(0);
00740     RHS = CMPCC.getOperand(1);
00741   }
00742 }
00743 
00744 static SDValue LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) {
00745   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
00746   // FIXME there isn't really any debug info here
00747   DebugLoc dl = Op.getDebugLoc();
00748   SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
00749   SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
00750   SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
00751   return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
00752 }
00753 
00754 static SDValue LowerCONSTANTPOOL(SDValue Op, SelectionDAG &DAG) {
00755   ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
00756   // FIXME there isn't really any debug info here
00757   DebugLoc dl = Op.getDebugLoc();
00758   Constant *C = N->getConstVal();
00759   SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
00760   SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
00761   SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
00762   return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
00763 }
00764 
00765 static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
00766   DebugLoc dl = Op.getDebugLoc();
00767   // Convert the fp value to integer in an FP register.
00768   assert(Op.getValueType() == MVT::i32);
00769   Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
00770   return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
00771 }
00772 
00773 static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
00774   DebugLoc dl = Op.getDebugLoc();
00775   assert(Op.getOperand(0).getValueType() == MVT::i32);
00776   SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
00777   // Convert the int value to FP in an FP register.
00778   return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
00779 }
00780 
00781 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
00782   SDValue Chain = Op.getOperand(0);
00783   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
00784   SDValue LHS = Op.getOperand(2);
00785   SDValue RHS = Op.getOperand(3);
00786   SDValue Dest = Op.getOperand(4);
00787   DebugLoc dl = Op.getDebugLoc();
00788   unsigned Opc, SPCC = ~0U;
00789 
00790   // If this is a br_cc of a "setcc", and if the setcc got lowered into
00791   // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
00792   LookThroughSetCC(LHS, RHS, CC, SPCC);
00793 
00794   // Get the condition flag.
00795   SDValue CompareFlag;
00796   if (LHS.getValueType() == MVT::i32) {
00797     std::vector<EVT> VTs;
00798     VTs.push_back(MVT::i32);
00799     VTs.push_back(MVT::Flag);
00800     SDValue Ops[2] = { LHS, RHS };
00801     CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
00802     if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
00803     Opc = SPISD::BRICC;
00804   } else {
00805     CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
00806     if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
00807     Opc = SPISD::BRFCC;
00808   }
00809   return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
00810                      DAG.getConstant(SPCC, MVT::i32), CompareFlag);
00811 }
00812 
00813 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
00814   SDValue LHS = Op.getOperand(0);
00815   SDValue RHS = Op.getOperand(1);
00816   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
00817   SDValue TrueVal = Op.getOperand(2);
00818   SDValue FalseVal = Op.getOperand(3);
00819   DebugLoc dl = Op.getDebugLoc();
00820   unsigned Opc, SPCC = ~0U;
00821 
00822   // If this is a select_cc of a "setcc", and if the setcc got lowered into
00823   // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
00824   LookThroughSetCC(LHS, RHS, CC, SPCC);
00825 
00826   SDValue CompareFlag;
00827   if (LHS.getValueType() == MVT::i32) {
00828     std::vector<EVT> VTs;
00829     VTs.push_back(LHS.getValueType());   // subcc returns a value
00830     VTs.push_back(MVT::Flag);
00831     SDValue Ops[2] = { LHS, RHS };
00832     CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
00833     Opc = SPISD::SELECT_ICC;
00834     if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
00835   } else {
00836     CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
00837     Opc = SPISD::SELECT_FCC;
00838     if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
00839   }
00840   return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
00841                      DAG.getConstant(SPCC, MVT::i32), CompareFlag);
00842 }
00843 
00844 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
00845                               SparcTargetLowering &TLI) {
00846   // vastart just stores the address of the VarArgsFrameIndex slot into the
00847   // memory location argument.
00848   DebugLoc dl = Op.getDebugLoc();
00849   SDValue Offset = DAG.getNode(ISD::ADD, dl, MVT::i32,
00850                                  DAG.getRegister(SP::I6, MVT::i32),
00851                                  DAG.getConstant(TLI.getVarArgsFrameOffset(),
00852                                                  MVT::i32));
00853   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
00854   return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0);
00855 }
00856 
00857 static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
00858   SDNode *Node = Op.getNode();
00859   EVT VT = Node->getValueType(0);
00860   SDValue InChain = Node->getOperand(0);
00861   SDValue VAListPtr = Node->getOperand(1);
00862   const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
00863   DebugLoc dl = Node->getDebugLoc();
00864   SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0);
00865   // Increment the pointer, VAList, to the next vaarg
00866   SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
00867                                   DAG.getConstant(VT.getSizeInBits()/8,
00868                                                   MVT::i32));
00869   // Store the incremented VAList to the legalized pointer
00870   InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
00871                          VAListPtr, SV, 0);
00872   // Load the actual argument out of the pointer VAList, unless this is an
00873   // f64 load.
00874   if (VT != MVT::f64)
00875     return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0);
00876 
00877   // Otherwise, load it as i64, then do a bitconvert.
00878   SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0);
00879 
00880   // Bit-Convert the value to f64.
00881   SDValue Ops[2] = {
00882     DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
00883     V.getValue(1)
00884   };
00885   return DAG.getMergeValues(Ops, 2, dl);
00886 }
00887 
00888 static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
00889   SDValue Chain = Op.getOperand(0);  // Legalize the chain.
00890   SDValue Size  = Op.getOperand(1);  // Legalize the size.
00891   DebugLoc dl = Op.getDebugLoc();
00892 
00893   unsigned SPReg = SP::O6;
00894   SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
00895   SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
00896   Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP);    // Output chain
00897 
00898   // The resultant pointer is actually 16 words from the bottom of the stack,
00899   // to provide a register spill area.
00900   SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
00901                                  DAG.getConstant(96, MVT::i32));
00902   SDValue Ops[2] = { NewVal, Chain };
00903   return DAG.getMergeValues(Ops, 2, dl);
00904 }
00905 
00906 
00907 SDValue SparcTargetLowering::
00908 LowerOperation(SDValue Op, SelectionDAG &DAG) {
00909   switch (Op.getOpcode()) {
00910   default: llvm_unreachable("Should not custom lower this!");
00911   // Frame & Return address.  Currently unimplemented
00912   case ISD::RETURNADDR: return SDValue();
00913   case ISD::FRAMEADDR:  return SDValue();
00914   case ISD::GlobalTLSAddress:
00915     llvm_unreachable("TLS not implemented for Sparc.");
00916   case ISD::GlobalAddress:      return LowerGLOBALADDRESS(Op, DAG);
00917   case ISD::ConstantPool:       return LowerCONSTANTPOOL(Op, DAG);
00918   case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG);
00919   case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
00920   case ISD::BR_CC:              return LowerBR_CC(Op, DAG);
00921   case ISD::SELECT_CC:          return LowerSELECT_CC(Op, DAG);
00922   case ISD::VASTART:            return LowerVASTART(Op, DAG, *this);
00923   case ISD::VAARG:              return LowerVAARG(Op, DAG);
00924   case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
00925   }
00926 }
00927 
00928 MachineBasicBlock *
00929 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
00930                                                  MachineBasicBlock *BB) const {
00931   const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
00932   unsigned BROpcode;
00933   unsigned CC;
00934   DebugLoc dl = MI->getDebugLoc();
00935   // Figure out the conditional branch opcode to use for this select_cc.
00936   switch (MI->getOpcode()) {
00937   default: llvm_unreachable("Unknown SELECT_CC!");
00938   case SP::SELECT_CC_Int_ICC:
00939   case SP::SELECT_CC_FP_ICC:
00940   case SP::SELECT_CC_DFP_ICC:
00941     BROpcode = SP::BCOND;
00942     break;
00943   case SP::SELECT_CC_Int_FCC:
00944   case SP::SELECT_CC_FP_FCC:
00945   case SP::SELECT_CC_DFP_FCC:
00946     BROpcode = SP::FBCOND;
00947     break;
00948   }
00949 
00950   CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
00951 
00952   // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
00953   // control-flow pattern.  The incoming instruction knows the destination vreg
00954   // to set, the condition code register to branch on, the true/false values to
00955   // select between, and a branch opcode to use.
00956   const BasicBlock *LLVM_BB = BB->getBasicBlock();
00957   MachineFunction::iterator It = BB;
00958   ++It;
00959 
00960   //  thisMBB:
00961   //  ...
00962   //   TrueVal = ...
00963   //   [f]bCC copy1MBB
00964   //   fallthrough --> copy0MBB
00965   MachineBasicBlock *thisMBB = BB;
00966   MachineFunction *F = BB->getParent();
00967   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
00968   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
00969   BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
00970   F->insert(It, copy0MBB);
00971   F->insert(It, sinkMBB);
00972   // Update machine-CFG edges by transferring all successors of the current
00973   // block to the new block which will contain the Phi node for the select.
00974   sinkMBB->transferSuccessors(BB);
00975   // Next, add the true and fallthrough blocks as its successors.
00976   BB->addSuccessor(copy0MBB);
00977   BB->addSuccessor(sinkMBB);
00978 
00979   //  copy0MBB:
00980   //   %FalseValue = ...
00981   //   # fallthrough to sinkMBB
00982   BB = copy0MBB;
00983 
00984   // Update machine-CFG edges
00985   BB->addSuccessor(sinkMBB);
00986 
00987   //  sinkMBB:
00988   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
00989   //  ...
00990   BB = sinkMBB;
00991   BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
00992     .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
00993     .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
00994 
00995   F->DeleteMachineInstr(MI);   // The pseudo instruction is gone now.
00996   return BB;
00997 }
00998 
00999 //===----------------------------------------------------------------------===//
01000 //                         Sparc Inline Assembly Support
01001 //===----------------------------------------------------------------------===//
01002 
01003 /// getConstraintType - Given a constraint letter, return the type of
01004 /// constraint it is for this target.
01005 SparcTargetLowering::ConstraintType
01006 SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
01007   if (Constraint.size() == 1) {
01008     switch (Constraint[0]) {
01009     default:  break;
01010     case 'r': return C_RegisterClass;
01011     }
01012   }
01013 
01014   return TargetLowering::getConstraintType(Constraint);
01015 }
01016 
01017 std::pair<unsigned, const TargetRegisterClass*>
01018 SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
01019                                                   EVT VT) const {
01020   if (Constraint.size() == 1) {
01021     switch (Constraint[0]) {
01022     case 'r':
01023       return std::make_pair(0U, SP::IntRegsRegisterClass);
01024     }
01025   }
01026 
01027   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
01028 }
01029 
01030 std::vector<unsigned> SparcTargetLowering::
01031 getRegClassForInlineAsmConstraint(const std::string &Constraint,
01032                                   EVT VT) const {
01033   if (Constraint.size() != 1)
01034     return std::vector<unsigned>();
01035 
01036   switch (Constraint[0]) {
01037   default: break;
01038   case 'r':
01039     return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
01040                                  SP::L4, SP::L5, SP::L6, SP::L7,
01041                                  SP::I0, SP::I1, SP::I2, SP::I3,
01042                                  SP::I4, SP::I5,
01043                                  SP::O0, SP::O1, SP::O2, SP::O3,
01044                                  SP::O4, SP::O5, SP::O7, 0);
01045   }
01046 
01047   return std::vector<unsigned>();
01048 }
01049 
01050 bool
01051 SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
01052   // The Sparc target isn't yet aware of offsets.
01053   return false;
01054 }
01055 
01056 /// getFunctionAlignment - Return the Log2 alignment of this function.
01057 unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
01058   return 4;
01059 }