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