LLVM API Documentation
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 "SparcMachineFunctionInfo.h" 00017 #include "SparcTargetMachine.h" 00018 #include "MCTargetDesc/SparcBaseInfo.h" 00019 #include "llvm/CodeGen/CallingConvLower.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineInstrBuilder.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/CodeGen/SelectionDAG.h" 00025 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h" 00026 #include "llvm/IR/DerivedTypes.h" 00027 #include "llvm/IR/Function.h" 00028 #include "llvm/IR/Module.h" 00029 #include "llvm/Support/ErrorHandling.h" 00030 using namespace llvm; 00031 00032 00033 //===----------------------------------------------------------------------===// 00034 // Calling Convention Implementation 00035 //===----------------------------------------------------------------------===// 00036 00037 static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT, 00038 MVT &LocVT, CCValAssign::LocInfo &LocInfo, 00039 ISD::ArgFlagsTy &ArgFlags, CCState &State) 00040 { 00041 assert (ArgFlags.isSRet()); 00042 00043 //Assign SRet argument 00044 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 00045 0, 00046 LocVT, LocInfo)); 00047 return true; 00048 } 00049 00050 static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT, 00051 MVT &LocVT, CCValAssign::LocInfo &LocInfo, 00052 ISD::ArgFlagsTy &ArgFlags, CCState &State) 00053 { 00054 static const uint16_t RegList[] = { 00055 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5 00056 }; 00057 //Try to get first reg 00058 if (unsigned Reg = State.AllocateReg(RegList, 6)) { 00059 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 00060 } else { 00061 //Assign whole thing in stack 00062 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 00063 State.AllocateStack(8,4), 00064 LocVT, LocInfo)); 00065 return true; 00066 } 00067 00068 //Try to get second reg 00069 if (unsigned Reg = State.AllocateReg(RegList, 6)) 00070 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 00071 else 00072 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT, 00073 State.AllocateStack(4,4), 00074 LocVT, LocInfo)); 00075 return true; 00076 } 00077 00078 // Allocate a full-sized argument for the 64-bit ABI. 00079 static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT, 00080 MVT &LocVT, CCValAssign::LocInfo &LocInfo, 00081 ISD::ArgFlagsTy &ArgFlags, CCState &State) { 00082 assert((LocVT == MVT::f32 || LocVT.getSizeInBits() == 64) && 00083 "Can't handle non-64 bits locations"); 00084 00085 // Stack space is allocated for all arguments starting from [%fp+BIAS+128]. 00086 unsigned Offset = State.AllocateStack(8, 8); 00087 unsigned Reg = 0; 00088 00089 if (LocVT == MVT::i64 && Offset < 6*8) 00090 // Promote integers to %i0-%i5. 00091 Reg = SP::I0 + Offset/8; 00092 else if (LocVT == MVT::f64 && Offset < 16*8) 00093 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15). 00094 Reg = SP::D0 + Offset/8; 00095 else if (LocVT == MVT::f32 && Offset < 16*8) 00096 // Promote floats to %f1, %f3, ... 00097 Reg = SP::F1 + Offset/4; 00098 00099 // Promote to register when possible, otherwise use the stack slot. 00100 if (Reg) { 00101 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 00102 return true; 00103 } 00104 00105 // This argument goes on the stack in an 8-byte slot. 00106 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to 00107 // the right-aligned float. The first 4 bytes of the stack slot are undefined. 00108 if (LocVT == MVT::f32) 00109 Offset += 4; 00110 00111 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 00112 return true; 00113 } 00114 00115 // Allocate a half-sized argument for the 64-bit ABI. 00116 // 00117 // This is used when passing { float, int } structs by value in registers. 00118 static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT, 00119 MVT &LocVT, CCValAssign::LocInfo &LocInfo, 00120 ISD::ArgFlagsTy &ArgFlags, CCState &State) { 00121 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations"); 00122 unsigned Offset = State.AllocateStack(4, 4); 00123 00124 if (LocVT == MVT::f32 && Offset < 16*8) { 00125 // Promote floats to %f0-%f31. 00126 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4, 00127 LocVT, LocInfo)); 00128 return true; 00129 } 00130 00131 if (LocVT == MVT::i32 && Offset < 6*8) { 00132 // Promote integers to %i0-%i5, using half the register. 00133 unsigned Reg = SP::I0 + Offset/8; 00134 LocVT = MVT::i64; 00135 LocInfo = CCValAssign::AExt; 00136 00137 // Set the Custom bit if this i32 goes in the high bits of a register. 00138 if (Offset % 8 == 0) 00139 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, 00140 LocVT, LocInfo)); 00141 else 00142 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo)); 00143 return true; 00144 } 00145 00146 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo)); 00147 return true; 00148 } 00149 00150 #include "SparcGenCallingConv.inc" 00151 00152 // The calling conventions in SparcCallingConv.td are described in terms of the 00153 // callee's register window. This function translates registers to the 00154 // corresponding caller window %o register. 00155 static unsigned toCallerWindow(unsigned Reg) { 00156 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum"); 00157 if (Reg >= SP::I0 && Reg <= SP::I7) 00158 return Reg - SP::I0 + SP::O0; 00159 return Reg; 00160 } 00161 00162 SDValue 00163 SparcTargetLowering::LowerReturn(SDValue Chain, 00164 CallingConv::ID CallConv, bool IsVarArg, 00165 const SmallVectorImpl<ISD::OutputArg> &Outs, 00166 const SmallVectorImpl<SDValue> &OutVals, 00167 DebugLoc DL, SelectionDAG &DAG) const { 00168 if (Subtarget->is64Bit()) 00169 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG); 00170 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG); 00171 } 00172 00173 SDValue 00174 SparcTargetLowering::LowerReturn_32(SDValue Chain, 00175 CallingConv::ID CallConv, bool IsVarArg, 00176 const SmallVectorImpl<ISD::OutputArg> &Outs, 00177 const SmallVectorImpl<SDValue> &OutVals, 00178 DebugLoc DL, SelectionDAG &DAG) const { 00179 MachineFunction &MF = DAG.getMachineFunction(); 00180 00181 // CCValAssign - represent the assignment of the return value to locations. 00182 SmallVector<CCValAssign, 16> RVLocs; 00183 00184 // CCState - Info about the registers and stack slot. 00185 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 00186 DAG.getTarget(), RVLocs, *DAG.getContext()); 00187 00188 // Analyze return values. 00189 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32); 00190 00191 SDValue Flag; 00192 SmallVector<SDValue, 4> RetOps(1, Chain); 00193 // Make room for the return address offset. 00194 RetOps.push_back(SDValue()); 00195 00196 // Copy the result values into the output registers. 00197 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00198 CCValAssign &VA = RVLocs[i]; 00199 assert(VA.isRegLoc() && "Can only return in registers!"); 00200 00201 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), 00202 OutVals[i], Flag); 00203 00204 // Guarantee that all emitted copies are stuck together with flags. 00205 Flag = Chain.getValue(1); 00206 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 00207 } 00208 00209 unsigned RetAddrOffset = 8; //Call Inst + Delay Slot 00210 // If the function returns a struct, copy the SRetReturnReg to I0 00211 if (MF.getFunction()->hasStructRetAttr()) { 00212 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>(); 00213 unsigned Reg = SFI->getSRetReturnReg(); 00214 if (!Reg) 00215 llvm_unreachable("sret virtual register not created in the entry block"); 00216 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy()); 00217 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag); 00218 Flag = Chain.getValue(1); 00219 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy())); 00220 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp 00221 } 00222 00223 RetOps[0] = Chain; // Update chain. 00224 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32); 00225 00226 // Add the flag if we have it. 00227 if (Flag.getNode()) 00228 RetOps.push_back(Flag); 00229 00230 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, 00231 &RetOps[0], RetOps.size()); 00232 } 00233 00234 // Lower return values for the 64-bit ABI. 00235 // Return values are passed the exactly the same way as function arguments. 00236 SDValue 00237 SparcTargetLowering::LowerReturn_64(SDValue Chain, 00238 CallingConv::ID CallConv, bool IsVarArg, 00239 const SmallVectorImpl<ISD::OutputArg> &Outs, 00240 const SmallVectorImpl<SDValue> &OutVals, 00241 DebugLoc DL, SelectionDAG &DAG) const { 00242 // CCValAssign - represent the assignment of the return value to locations. 00243 SmallVector<CCValAssign, 16> RVLocs; 00244 00245 // CCState - Info about the registers and stack slot. 00246 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 00247 DAG.getTarget(), RVLocs, *DAG.getContext()); 00248 00249 // Analyze return values. 00250 CCInfo.AnalyzeReturn(Outs, CC_Sparc64); 00251 00252 SDValue Flag; 00253 SmallVector<SDValue, 4> RetOps(1, Chain); 00254 00255 // The second operand on the return instruction is the return address offset. 00256 // The return address is always %i7+8 with the 64-bit ABI. 00257 RetOps.push_back(DAG.getConstant(8, MVT::i32)); 00258 00259 // Copy the result values into the output registers. 00260 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00261 CCValAssign &VA = RVLocs[i]; 00262 assert(VA.isRegLoc() && "Can only return in registers!"); 00263 SDValue OutVal = OutVals[i]; 00264 00265 // Integer return values must be sign or zero extended by the callee. 00266 switch (VA.getLocInfo()) { 00267 case CCValAssign::SExt: 00268 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal); 00269 break; 00270 case CCValAssign::ZExt: 00271 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal); 00272 break; 00273 case CCValAssign::AExt: 00274 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal); 00275 default: 00276 break; 00277 } 00278 00279 // The custom bit on an i32 return value indicates that it should be passed 00280 // in the high bits of the register. 00281 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) { 00282 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal, 00283 DAG.getConstant(32, MVT::i32)); 00284 00285 // The next value may go in the low bits of the same register. 00286 // Handle both at once. 00287 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) { 00288 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]); 00289 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV); 00290 // Skip the next value, it's already done. 00291 ++i; 00292 } 00293 } 00294 00295 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag); 00296 00297 // Guarantee that all emitted copies are stuck together with flags. 00298 Flag = Chain.getValue(1); 00299 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT())); 00300 } 00301 00302 RetOps[0] = Chain; // Update chain. 00303 00304 // Add the flag if we have it. 00305 if (Flag.getNode()) 00306 RetOps.push_back(Flag); 00307 00308 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other, 00309 &RetOps[0], RetOps.size()); 00310 } 00311 00312 SDValue SparcTargetLowering:: 00313 LowerFormalArguments(SDValue Chain, 00314 CallingConv::ID CallConv, 00315 bool IsVarArg, 00316 const SmallVectorImpl<ISD::InputArg> &Ins, 00317 DebugLoc DL, 00318 SelectionDAG &DAG, 00319 SmallVectorImpl<SDValue> &InVals) const { 00320 if (Subtarget->is64Bit()) 00321 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins, 00322 DL, DAG, InVals); 00323 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins, 00324 DL, DAG, InVals); 00325 } 00326 00327 /// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are 00328 /// passed in either one or two GPRs, including FP values. TODO: we should 00329 /// pass FP values in FP registers for fastcc functions. 00330 SDValue SparcTargetLowering:: 00331 LowerFormalArguments_32(SDValue Chain, 00332 CallingConv::ID CallConv, 00333 bool isVarArg, 00334 const SmallVectorImpl<ISD::InputArg> &Ins, 00335 DebugLoc dl, 00336 SelectionDAG &DAG, 00337 SmallVectorImpl<SDValue> &InVals) const { 00338 MachineFunction &MF = DAG.getMachineFunction(); 00339 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 00340 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>(); 00341 00342 // Assign locations to all of the incoming arguments. 00343 SmallVector<CCValAssign, 16> ArgLocs; 00344 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 00345 getTargetMachine(), ArgLocs, *DAG.getContext()); 00346 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32); 00347 00348 const unsigned StackOffset = 92; 00349 00350 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00351 CCValAssign &VA = ArgLocs[i]; 00352 00353 if (i == 0 && Ins[i].Flags.isSRet()) { 00354 //Get SRet from [%fp+64] 00355 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true); 00356 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32); 00357 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, 00358 MachinePointerInfo(), 00359 false, false, false, 0); 00360 InVals.push_back(Arg); 00361 continue; 00362 } 00363 00364 if (VA.isRegLoc()) { 00365 if (VA.needsCustom()) { 00366 assert(VA.getLocVT() == MVT::f64); 00367 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass); 00368 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi); 00369 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32); 00370 00371 assert(i+1 < e); 00372 CCValAssign &NextVA = ArgLocs[++i]; 00373 00374 SDValue LoVal; 00375 if (NextVA.isMemLoc()) { 00376 int FrameIdx = MF.getFrameInfo()-> 00377 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true); 00378 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32); 00379 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, 00380 MachinePointerInfo(), 00381 false, false, false, 0); 00382 } else { 00383 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(), 00384 &SP::IntRegsRegClass); 00385 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32); 00386 } 00387 SDValue WholeValue = 00388 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal); 00389 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue); 00390 InVals.push_back(WholeValue); 00391 continue; 00392 } 00393 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass); 00394 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg); 00395 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32); 00396 if (VA.getLocVT() == MVT::f32) 00397 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg); 00398 else if (VA.getLocVT() != MVT::i32) { 00399 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg, 00400 DAG.getValueType(VA.getLocVT())); 00401 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg); 00402 } 00403 InVals.push_back(Arg); 00404 continue; 00405 } 00406 00407 assert(VA.isMemLoc()); 00408 00409 unsigned Offset = VA.getLocMemOffset()+StackOffset; 00410 00411 if (VA.needsCustom()) { 00412 assert(VA.getValVT() == MVT::f64); 00413 //If it is double-word aligned, just load. 00414 if (Offset % 8 == 0) { 00415 int FI = MF.getFrameInfo()->CreateFixedObject(8, 00416 Offset, 00417 true); 00418 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy()); 00419 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, 00420 MachinePointerInfo(), 00421 false,false, false, 0); 00422 InVals.push_back(Load); 00423 continue; 00424 } 00425 00426 int FI = MF.getFrameInfo()->CreateFixedObject(4, 00427 Offset, 00428 true); 00429 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy()); 00430 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, 00431 MachinePointerInfo(), 00432 false, false, false, 0); 00433 int FI2 = MF.getFrameInfo()->CreateFixedObject(4, 00434 Offset+4, 00435 true); 00436 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy()); 00437 00438 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2, 00439 MachinePointerInfo(), 00440 false, false, false, 0); 00441 00442 SDValue WholeValue = 00443 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal); 00444 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue); 00445 InVals.push_back(WholeValue); 00446 continue; 00447 } 00448 00449 int FI = MF.getFrameInfo()->CreateFixedObject(4, 00450 Offset, 00451 true); 00452 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy()); 00453 SDValue Load ; 00454 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) { 00455 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr, 00456 MachinePointerInfo(), 00457 false, false, false, 0); 00458 } else { 00459 ISD::LoadExtType LoadOp = ISD::SEXTLOAD; 00460 // Sparc is big endian, so add an offset based on the ObjectVT. 00461 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8); 00462 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr, 00463 DAG.getConstant(Offset, MVT::i32)); 00464 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr, 00465 MachinePointerInfo(), 00466 VA.getValVT(), false, false,0); 00467 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load); 00468 } 00469 InVals.push_back(Load); 00470 } 00471 00472 if (MF.getFunction()->hasStructRetAttr()) { 00473 //Copy the SRet Argument to SRetReturnReg 00474 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>(); 00475 unsigned Reg = SFI->getSRetReturnReg(); 00476 if (!Reg) { 00477 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass); 00478 SFI->setSRetReturnReg(Reg); 00479 } 00480 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]); 00481 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain); 00482 } 00483 00484 // Store remaining ArgRegs to the stack if this is a varargs function. 00485 if (isVarArg) { 00486 static const uint16_t ArgRegs[] = { 00487 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5 00488 }; 00489 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6); 00490 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6; 00491 unsigned ArgOffset = CCInfo.getNextStackOffset(); 00492 if (NumAllocated == 6) 00493 ArgOffset += StackOffset; 00494 else { 00495 assert(!ArgOffset); 00496 ArgOffset = 68+4*NumAllocated; 00497 } 00498 00499 // Remember the vararg offset for the va_start implementation. 00500 FuncInfo->setVarArgsFrameOffset(ArgOffset); 00501 00502 std::vector<SDValue> OutChains; 00503 00504 for (; CurArgReg != ArgRegEnd; ++CurArgReg) { 00505 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass); 00506 MF.getRegInfo().addLiveIn(*CurArgReg, VReg); 00507 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32); 00508 00509 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset, 00510 true); 00511 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32); 00512 00513 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, 00514 MachinePointerInfo(), 00515 false, false, 0)); 00516 ArgOffset += 4; 00517 } 00518 00519 if (!OutChains.empty()) { 00520 OutChains.push_back(Chain); 00521 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00522 &OutChains[0], OutChains.size()); 00523 } 00524 } 00525 00526 return Chain; 00527 } 00528 00529 // Lower formal arguments for the 64 bit ABI. 00530 SDValue SparcTargetLowering:: 00531 LowerFormalArguments_64(SDValue Chain, 00532 CallingConv::ID CallConv, 00533 bool IsVarArg, 00534 const SmallVectorImpl<ISD::InputArg> &Ins, 00535 DebugLoc DL, 00536 SelectionDAG &DAG, 00537 SmallVectorImpl<SDValue> &InVals) const { 00538 MachineFunction &MF = DAG.getMachineFunction(); 00539 00540 // Analyze arguments according to CC_Sparc64. 00541 SmallVector<CCValAssign, 16> ArgLocs; 00542 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(), 00543 getTargetMachine(), ArgLocs, *DAG.getContext()); 00544 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64); 00545 00546 // The argument array begins at %fp+BIAS+128, after the register save area. 00547 const unsigned ArgArea = 128; 00548 00549 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00550 CCValAssign &VA = ArgLocs[i]; 00551 if (VA.isRegLoc()) { 00552 // This argument is passed in a register. 00553 // All integer register arguments are promoted by the caller to i64. 00554 00555 // Create a virtual register for the promoted live-in value. 00556 unsigned VReg = MF.addLiveIn(VA.getLocReg(), 00557 getRegClassFor(VA.getLocVT())); 00558 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT()); 00559 00560 // Get the high bits for i32 struct elements. 00561 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) 00562 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg, 00563 DAG.getConstant(32, MVT::i32)); 00564 00565 // The caller promoted the argument, so insert an Assert?ext SDNode so we 00566 // won't promote the value again in this function. 00567 switch (VA.getLocInfo()) { 00568 case CCValAssign::SExt: 00569 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg, 00570 DAG.getValueType(VA.getValVT())); 00571 break; 00572 case CCValAssign::ZExt: 00573 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg, 00574 DAG.getValueType(VA.getValVT())); 00575 break; 00576 default: 00577 break; 00578 } 00579 00580 // Truncate the register down to the argument type. 00581 if (VA.isExtInLoc()) 00582 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg); 00583 00584 InVals.push_back(Arg); 00585 continue; 00586 } 00587 00588 // The registers are exhausted. This argument was passed on the stack. 00589 assert(VA.isMemLoc()); 00590 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the 00591 // beginning of the arguments area at %fp+BIAS+128. 00592 unsigned Offset = VA.getLocMemOffset() + ArgArea; 00593 unsigned ValSize = VA.getValVT().getSizeInBits() / 8; 00594 // Adjust offset for extended arguments, SPARC is big-endian. 00595 // The caller will have written the full slot with extended bytes, but we 00596 // prefer our own extending loads. 00597 if (VA.isExtInLoc()) 00598 Offset += 8 - ValSize; 00599 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true); 00600 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, 00601 DAG.getFrameIndex(FI, getPointerTy()), 00602 MachinePointerInfo::getFixedStack(FI), 00603 false, false, false, 0)); 00604 } 00605 00606 if (!IsVarArg) 00607 return Chain; 00608 00609 // This function takes variable arguments, some of which may have been passed 00610 // in registers %i0-%i5. Variable floating point arguments are never passed 00611 // in floating point registers. They go on %i0-%i5 or on the stack like 00612 // integer arguments. 00613 // 00614 // The va_start intrinsic needs to know the offset to the first variable 00615 // argument. 00616 unsigned ArgOffset = CCInfo.getNextStackOffset(); 00617 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>(); 00618 // Skip the 128 bytes of register save area. 00619 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea + 00620 Subtarget->getStackPointerBias()); 00621 00622 // Save the variable arguments that were passed in registers. 00623 // The caller is required to reserve stack space for 6 arguments regardless 00624 // of how many arguments were actually passed. 00625 SmallVector<SDValue, 8> OutChains; 00626 for (; ArgOffset < 6*8; ArgOffset += 8) { 00627 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass); 00628 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64); 00629 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true); 00630 OutChains.push_back(DAG.getStore(Chain, DL, VArg, 00631 DAG.getFrameIndex(FI, getPointerTy()), 00632 MachinePointerInfo::getFixedStack(FI), 00633 false, false, 0)); 00634 } 00635 00636 if (!OutChains.empty()) 00637 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 00638 &OutChains[0], OutChains.size()); 00639 00640 return Chain; 00641 } 00642 00643 SDValue 00644 SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, 00645 SmallVectorImpl<SDValue> &InVals) const { 00646 if (Subtarget->is64Bit()) 00647 return LowerCall_64(CLI, InVals); 00648 return LowerCall_32(CLI, InVals); 00649 } 00650 00651 // Lower a call for the 32-bit ABI. 00652 SDValue 00653 SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI, 00654 SmallVectorImpl<SDValue> &InVals) const { 00655 SelectionDAG &DAG = CLI.DAG; 00656 DebugLoc &dl = CLI.DL; 00657 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs; 00658 SmallVector<SDValue, 32> &OutVals = CLI.OutVals; 00659 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins; 00660 SDValue Chain = CLI.Chain; 00661 SDValue Callee = CLI.Callee; 00662 bool &isTailCall = CLI.IsTailCall; 00663 CallingConv::ID CallConv = CLI.CallConv; 00664 bool isVarArg = CLI.IsVarArg; 00665 00666 // Sparc target does not yet support tail call optimization. 00667 isTailCall = false; 00668 00669 // Analyze operands of the call, assigning locations to each operand. 00670 SmallVector<CCValAssign, 16> ArgLocs; 00671 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), 00672 DAG.getTarget(), ArgLocs, *DAG.getContext()); 00673 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32); 00674 00675 // Get the size of the outgoing arguments stack space requirement. 00676 unsigned ArgsSize = CCInfo.getNextStackOffset(); 00677 00678 // Keep stack frames 8-byte aligned. 00679 ArgsSize = (ArgsSize+7) & ~7; 00680 00681 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 00682 00683 //Create local copies for byval args. 00684 SmallVector<SDValue, 8> ByValArgs; 00685 for (unsigned i = 0, e = Outs.size(); i != e; ++i) { 00686 ISD::ArgFlagsTy Flags = Outs[i].Flags; 00687 if (!Flags.isByVal()) 00688 continue; 00689 00690 SDValue Arg = OutVals[i]; 00691 unsigned Size = Flags.getByValSize(); 00692 unsigned Align = Flags.getByValAlign(); 00693 00694 int FI = MFI->CreateStackObject(Size, Align, false); 00695 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy()); 00696 SDValue SizeNode = DAG.getConstant(Size, MVT::i32); 00697 00698 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align, 00699 false, //isVolatile, 00700 (Size <= 32), //AlwaysInline if size <= 32 00701 MachinePointerInfo(), MachinePointerInfo()); 00702 ByValArgs.push_back(FIPtr); 00703 } 00704 00705 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true)); 00706 00707 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 00708 SmallVector<SDValue, 8> MemOpChains; 00709 00710 const unsigned StackOffset = 92; 00711 bool hasStructRetAttr = false; 00712 // Walk the register/memloc assignments, inserting copies/loads. 00713 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size(); 00714 i != e; 00715 ++i, ++realArgIdx) { 00716 CCValAssign &VA = ArgLocs[i]; 00717 SDValue Arg = OutVals[realArgIdx]; 00718 00719 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags; 00720 00721 //Use local copy if it is a byval arg. 00722 if (Flags.isByVal()) 00723 Arg = ByValArgs[byvalArgIdx++]; 00724 00725 // Promote the value if needed. 00726 switch (VA.getLocInfo()) { 00727 default: llvm_unreachable("Unknown loc info!"); 00728 case CCValAssign::Full: break; 00729 case CCValAssign::SExt: 00730 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg); 00731 break; 00732 case CCValAssign::ZExt: 00733 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg); 00734 break; 00735 case CCValAssign::AExt: 00736 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg); 00737 break; 00738 case CCValAssign::BCvt: 00739 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg); 00740 break; 00741 } 00742 00743 if (Flags.isSRet()) { 00744 assert(VA.needsCustom()); 00745 // store SRet argument in %sp+64 00746 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32); 00747 SDValue PtrOff = DAG.getIntPtrConstant(64); 00748 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00749 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 00750 MachinePointerInfo(), 00751 false, false, 0)); 00752 hasStructRetAttr = true; 00753 continue; 00754 } 00755 00756 if (VA.needsCustom()) { 00757 assert(VA.getLocVT() == MVT::f64); 00758 00759 if (VA.isMemLoc()) { 00760 unsigned Offset = VA.getLocMemOffset() + StackOffset; 00761 //if it is double-word aligned, just store. 00762 if (Offset % 8 == 0) { 00763 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32); 00764 SDValue PtrOff = DAG.getIntPtrConstant(Offset); 00765 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00766 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 00767 MachinePointerInfo(), 00768 false, false, 0)); 00769 continue; 00770 } 00771 } 00772 00773 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32); 00774 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, 00775 Arg, StackPtr, MachinePointerInfo(), 00776 false, false, 0); 00777 // Sparc is big-endian, so the high part comes first. 00778 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, 00779 MachinePointerInfo(), false, false, false, 0); 00780 // Increment the pointer to the other half. 00781 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr, 00782 DAG.getIntPtrConstant(4)); 00783 // Load the low part. 00784 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, 00785 MachinePointerInfo(), false, false, false, 0); 00786 00787 if (VA.isRegLoc()) { 00788 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi)); 00789 assert(i+1 != e); 00790 CCValAssign &NextVA = ArgLocs[++i]; 00791 if (NextVA.isRegLoc()) { 00792 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo)); 00793 } else { 00794 //Store the low part in stack. 00795 unsigned Offset = NextVA.getLocMemOffset() + StackOffset; 00796 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32); 00797 SDValue PtrOff = DAG.getIntPtrConstant(Offset); 00798 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00799 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff, 00800 MachinePointerInfo(), 00801 false, false, 0)); 00802 } 00803 } else { 00804 unsigned Offset = VA.getLocMemOffset() + StackOffset; 00805 // Store the high part. 00806 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32); 00807 SDValue PtrOff = DAG.getIntPtrConstant(Offset); 00808 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00809 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff, 00810 MachinePointerInfo(), 00811 false, false, 0)); 00812 // Store the low part. 00813 PtrOff = DAG.getIntPtrConstant(Offset+4); 00814 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00815 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff, 00816 MachinePointerInfo(), 00817 false, false, 0)); 00818 } 00819 continue; 00820 } 00821 00822 // Arguments that can be passed on register must be kept at 00823 // RegsToPass vector 00824 if (VA.isRegLoc()) { 00825 if (VA.getLocVT() != MVT::f32) { 00826 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 00827 continue; 00828 } 00829 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg); 00830 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); 00831 continue; 00832 } 00833 00834 assert(VA.isMemLoc()); 00835 00836 // Create a store off the stack pointer for this argument. 00837 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32); 00838 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset); 00839 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff); 00840 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff, 00841 MachinePointerInfo(), 00842 false, false, 0)); 00843 } 00844 00845 00846 // Emit all stores, make sure the occur before any copies into physregs. 00847 if (!MemOpChains.empty()) 00848 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, 00849 &MemOpChains[0], MemOpChains.size()); 00850 00851 // Build a sequence of copy-to-reg nodes chained together with token 00852 // chain and flag operands which copy the outgoing args into registers. 00853 // The InFlag in necessary since all emitted instructions must be 00854 // stuck together. 00855 SDValue InFlag; 00856 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 00857 unsigned Reg = toCallerWindow(RegsToPass[i].first); 00858 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag); 00859 InFlag = Chain.getValue(1); 00860 } 00861 00862 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0; 00863 00864 // If the callee is a GlobalAddress node (quite common, every direct call is) 00865 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 00866 // Likewise ExternalSymbol -> TargetExternalSymbol. 00867 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 00868 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32); 00869 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 00870 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32); 00871 00872 // Returns a chain & a flag for retval copy to use 00873 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 00874 SmallVector<SDValue, 8> Ops; 00875 Ops.push_back(Chain); 00876 Ops.push_back(Callee); 00877 if (hasStructRetAttr) 00878 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32)); 00879 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 00880 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first), 00881 RegsToPass[i].second.getValueType())); 00882 if (InFlag.getNode()) 00883 Ops.push_back(InFlag); 00884 00885 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size()); 00886 InFlag = Chain.getValue(1); 00887 00888 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true), 00889 DAG.getIntPtrConstant(0, true), InFlag); 00890 InFlag = Chain.getValue(1); 00891 00892 // Assign locations to each value returned by this call. 00893 SmallVector<CCValAssign, 16> RVLocs; 00894 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(), 00895 DAG.getTarget(), RVLocs, *DAG.getContext()); 00896 00897 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32); 00898 00899 // Copy all of the result registers out of their specified physreg. 00900 for (unsigned i = 0; i != RVLocs.size(); ++i) { 00901 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()), 00902 RVLocs[i].getValVT(), InFlag).getValue(1); 00903 InFlag = Chain.getValue(2); 00904 InVals.push_back(Chain.getValue(0)); 00905 } 00906 00907 return Chain; 00908 } 00909 00910 unsigned 00911 SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const 00912 { 00913 const Function *CalleeFn = 0; 00914 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) { 00915 CalleeFn = dyn_cast<Function>(G->getGlobal()); 00916 } else if (ExternalSymbolSDNode *E = 00917 dyn_cast<ExternalSymbolSDNode>(Callee)) { 00918 const Function *Fn = DAG.getMachineFunction().getFunction(); 00919 const Module *M = Fn->getParent(); 00920 CalleeFn = M->getFunction(E->getSymbol()); 00921 } 00922 00923 if (!CalleeFn) 00924 return 0; 00925 00926 assert(CalleeFn->hasStructRetAttr() && 00927 "Callee does not have the StructRet attribute."); 00928 00929 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType()); 00930 Type *ElementTy = Ty->getElementType(); 00931 return getDataLayout()->getTypeAllocSize(ElementTy); 00932 } 00933 00934 00935 // Fixup floating point arguments in the ... part of a varargs call. 00936 // 00937 // The SPARC v9 ABI requires that floating point arguments are treated the same 00938 // as integers when calling a varargs function. This does not apply to the 00939 // fixed arguments that are part of the function's prototype. 00940 // 00941 // This function post-processes a CCValAssign array created by 00942 // AnalyzeCallOperands(). 00943 static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs, 00944 ArrayRef<ISD::OutputArg> Outs) { 00945 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 00946 const CCValAssign &VA = ArgLocs[i]; 00947 // FIXME: What about f32 arguments? C promotes them to f64 when calling 00948 // varargs functions. 00949 if (!VA.isRegLoc() || VA.getLocVT() != MVT::f64) 00950 continue; 00951 // The fixed arguments to a varargs function still go in FP registers. 00952 if (Outs[VA.getValNo()].IsFixed) 00953 continue; 00954 00955 // This floating point argument should be reassigned. 00956 CCValAssign NewVA; 00957 00958 // Determine the offset into the argument array. 00959 unsigned Offset = 8 * (VA.getLocReg() - SP::D0); 00960 assert(Offset < 16*8 && "Offset out of range, bad register enum?"); 00961 00962 if (Offset < 6*8) { 00963 // This argument should go in %i0-%i5. 00964 unsigned IReg = SP::I0 + Offset/8; 00965 // Full register, just bitconvert into i64. 00966 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(), 00967 IReg, MVT::i64, CCValAssign::BCvt); 00968 } else { 00969 // This needs to go to memory, we're out of integer registers. 00970 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(), 00971 Offset, VA.getLocVT(), VA.getLocInfo()); 00972 } 00973 ArgLocs[i] = NewVA; 00974 } 00975 } 00976 00977 // Lower a call for the 64-bit ABI. 00978 SDValue 00979 SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI, 00980 SmallVectorImpl<SDValue> &InVals) const { 00981 SelectionDAG &DAG = CLI.DAG; 00982 DebugLoc DL = CLI.DL; 00983 SDValue Chain = CLI.Chain; 00984 00985 // Analyze operands of the call, assigning locations to each operand. 00986 SmallVector<CCValAssign, 16> ArgLocs; 00987 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), 00988 DAG.getTarget(), ArgLocs, *DAG.getContext()); 00989 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64); 00990 00991 // Get the size of the outgoing arguments stack space requirement. 00992 // The stack offset computed by CC_Sparc64 includes all arguments. 00993 // Called functions expect 6 argument words to exist in the stack frame, used 00994 // or not. 00995 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset()); 00996 00997 // Keep stack frames 16-byte aligned. 00998 ArgsSize = RoundUpToAlignment(ArgsSize, 16); 00999 01000 // Varargs calls require special treatment. 01001 if (CLI.IsVarArg) 01002 fixupVariableFloatArgs(ArgLocs, CLI.Outs); 01003 01004 // Adjust the stack pointer to make room for the arguments. 01005 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls 01006 // with more than 6 arguments. 01007 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true)); 01008 01009 // Collect the set of registers to pass to the function and their values. 01010 // This will be emitted as a sequence of CopyToReg nodes glued to the call 01011 // instruction. 01012 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass; 01013 01014 // Collect chains from all the memory opeations that copy arguments to the 01015 // stack. They must follow the stack pointer adjustment above and precede the 01016 // call instruction itself. 01017 SmallVector<SDValue, 8> MemOpChains; 01018 01019 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { 01020 const CCValAssign &VA = ArgLocs[i]; 01021 SDValue Arg = CLI.OutVals[i]; 01022 01023 // Promote the value if needed. 01024 switch (VA.getLocInfo()) { 01025 default: 01026 llvm_unreachable("Unknown location info!"); 01027 case CCValAssign::Full: 01028 break; 01029 case CCValAssign::SExt: 01030 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg); 01031 break; 01032 case CCValAssign::ZExt: 01033 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg); 01034 break; 01035 case CCValAssign::AExt: 01036 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg); 01037 break; 01038 case CCValAssign::BCvt: 01039 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg); 01040 break; 01041 } 01042 01043 if (VA.isRegLoc()) { 01044 // The custom bit on an i32 return value indicates that it should be 01045 // passed in the high bits of the register. 01046 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) { 01047 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg, 01048 DAG.getConstant(32, MVT::i32)); 01049 01050 // The next value may go in the low bits of the same register. 01051 // Handle both at once. 01052 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() && 01053 ArgLocs[i+1].getLocReg() == VA.getLocReg()) { 01054 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, 01055 CLI.OutVals[i+1]); 01056 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV); 01057 // Skip the next value, it's already done. 01058 ++i; 01059 } 01060 } 01061 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg)); 01062 continue; 01063 } 01064 01065 assert(VA.isMemLoc()); 01066 01067 // Create a store off the stack pointer for this argument. 01068 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy()); 01069 // The argument area starts at %fp+BIAS+128 in the callee frame, 01070 // %sp+BIAS+128 in ours. 01071 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() + 01072 Subtarget->getStackPointerBias() + 01073 128); 01074 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff); 01075 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff, 01076 MachinePointerInfo(), 01077 false, false, 0)); 01078 } 01079 01080 // Emit all stores, make sure they occur before the call. 01081 if (!MemOpChains.empty()) 01082 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, 01083 &MemOpChains[0], MemOpChains.size()); 01084 01085 // Build a sequence of CopyToReg nodes glued together with token chain and 01086 // glue operands which copy the outgoing args into registers. The InGlue is 01087 // necessary since all emitted instructions must be stuck together in order 01088 // to pass the live physical registers. 01089 SDValue InGlue; 01090 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { 01091 Chain = DAG.getCopyToReg(Chain, DL, 01092 RegsToPass[i].first, RegsToPass[i].second, InGlue); 01093 InGlue = Chain.getValue(1); 01094 } 01095 01096 // If the callee is a GlobalAddress node (quite common, every direct call is) 01097 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it. 01098 // Likewise ExternalSymbol -> TargetExternalSymbol. 01099 SDValue Callee = CLI.Callee; 01100 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) 01101 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy()); 01102 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee)) 01103 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy()); 01104 01105 // Build the operands for the call instruction itself. 01106 SmallVector<SDValue, 8> Ops; 01107 Ops.push_back(Chain); 01108 Ops.push_back(Callee); 01109 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) 01110 Ops.push_back(DAG.getRegister(RegsToPass[i].first, 01111 RegsToPass[i].second.getValueType())); 01112 01113 // Make sure the CopyToReg nodes are glued to the call instruction which 01114 // consumes the registers. 01115 if (InGlue.getNode()) 01116 Ops.push_back(InGlue); 01117 01118 // Now the call itself. 01119 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); 01120 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size()); 01121 InGlue = Chain.getValue(1); 01122 01123 // Revert the stack pointer immediately after the call. 01124 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true), 01125 DAG.getIntPtrConstant(0, true), InGlue); 01126 InGlue = Chain.getValue(1); 01127 01128 // Now extract the return values. This is more or less the same as 01129 // LowerFormalArguments_64. 01130 01131 // Assign locations to each value returned by this call. 01132 SmallVector<CCValAssign, 16> RVLocs; 01133 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(), 01134 DAG.getTarget(), RVLocs, *DAG.getContext()); 01135 RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64); 01136 01137 // Copy all of the result registers out of their specified physreg. 01138 for (unsigned i = 0; i != RVLocs.size(); ++i) { 01139 CCValAssign &VA = RVLocs[i]; 01140 unsigned Reg = toCallerWindow(VA.getLocReg()); 01141 01142 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can 01143 // reside in the same register in the high and low bits. Reuse the 01144 // CopyFromReg previous node to avoid duplicate copies. 01145 SDValue RV; 01146 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1))) 01147 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg) 01148 RV = Chain.getValue(0); 01149 01150 // But usually we'll create a new CopyFromReg for a different register. 01151 if (!RV.getNode()) { 01152 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue); 01153 Chain = RV.getValue(1); 01154 InGlue = Chain.getValue(2); 01155 } 01156 01157 // Get the high bits for i32 struct elements. 01158 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) 01159 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV, 01160 DAG.getConstant(32, MVT::i32)); 01161 01162 // The callee promoted the return value, so insert an Assert?ext SDNode so 01163 // we won't promote the value again in this function. 01164 switch (VA.getLocInfo()) { 01165 case CCValAssign::SExt: 01166 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV, 01167 DAG.getValueType(VA.getValVT())); 01168 break; 01169 case CCValAssign::ZExt: 01170 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV, 01171 DAG.getValueType(VA.getValVT())); 01172 break; 01173 default: 01174 break; 01175 } 01176 01177 // Truncate the register down to the return value type. 01178 if (VA.isExtInLoc()) 01179 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV); 01180 01181 InVals.push_back(RV); 01182 } 01183 01184 return Chain; 01185 } 01186 01187 //===----------------------------------------------------------------------===// 01188 // TargetLowering Implementation 01189 //===----------------------------------------------------------------------===// 01190 01191 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC 01192 /// condition. 01193 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) { 01194 switch (CC) { 01195 default: llvm_unreachable("Unknown integer condition code!"); 01196 case ISD::SETEQ: return SPCC::ICC_E; 01197 case ISD::SETNE: return SPCC::ICC_NE; 01198 case ISD::SETLT: return SPCC::ICC_L; 01199 case ISD::SETGT: return SPCC::ICC_G; 01200 case ISD::SETLE: return SPCC::ICC_LE; 01201 case ISD::SETGE: return SPCC::ICC_GE; 01202 case ISD::SETULT: return SPCC::ICC_CS; 01203 case ISD::SETULE: return SPCC::ICC_LEU; 01204 case ISD::SETUGT: return SPCC::ICC_GU; 01205 case ISD::SETUGE: return SPCC::ICC_CC; 01206 } 01207 } 01208 01209 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC 01210 /// FCC condition. 01211 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) { 01212 switch (CC) { 01213 default: llvm_unreachable("Unknown fp condition code!"); 01214 case ISD::SETEQ: 01215 case ISD::SETOEQ: return SPCC::FCC_E; 01216 case ISD::SETNE: 01217 case ISD::SETUNE: return SPCC::FCC_NE; 01218 case ISD::SETLT: 01219 case ISD::SETOLT: return SPCC::FCC_L; 01220 case ISD::SETGT: 01221 case ISD::SETOGT: return SPCC::FCC_G; 01222 case ISD::SETLE: 01223 case ISD::SETOLE: return SPCC::FCC_LE; 01224 case ISD::SETGE: 01225 case ISD::SETOGE: return SPCC::FCC_GE; 01226 case ISD::SETULT: return SPCC::FCC_UL; 01227 case ISD::SETULE: return SPCC::FCC_ULE; 01228 case ISD::SETUGT: return SPCC::FCC_UG; 01229 case ISD::SETUGE: return SPCC::FCC_UGE; 01230 case ISD::SETUO: return SPCC::FCC_U; 01231 case ISD::SETO: return SPCC::FCC_O; 01232 case ISD::SETONE: return SPCC::FCC_LG; 01233 case ISD::SETUEQ: return SPCC::FCC_UE; 01234 } 01235 } 01236 01237 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM) 01238 : TargetLowering(TM, new TargetLoweringObjectFileELF()) { 01239 Subtarget = &TM.getSubtarget<SparcSubtarget>(); 01240 01241 // Set up the register classes. 01242 addRegisterClass(MVT::i32, &SP::IntRegsRegClass); 01243 addRegisterClass(MVT::f32, &SP::FPRegsRegClass); 01244 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass); 01245 if (Subtarget->is64Bit()) 01246 addRegisterClass(MVT::i64, &SP::I64RegsRegClass); 01247 01248 // Turn FP extload into load/fextend 01249 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand); 01250 // Sparc doesn't have i1 sign extending load 01251 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote); 01252 // Turn FP truncstore into trunc + store. 01253 setTruncStoreAction(MVT::f64, MVT::f32, Expand); 01254 01255 // Custom legalize GlobalAddress nodes into LO/HI parts. 01256 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom); 01257 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom); 01258 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom); 01259 01260 // Sparc doesn't have sext_inreg, replace them with shl/sra 01261 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand); 01262 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); 01263 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); 01264 01265 // Sparc has no REM or DIVREM operations. 01266 setOperationAction(ISD::UREM, MVT::i32, Expand); 01267 setOperationAction(ISD::SREM, MVT::i32, Expand); 01268 setOperationAction(ISD::SDIVREM, MVT::i32, Expand); 01269 setOperationAction(ISD::UDIVREM, MVT::i32, Expand); 01270 01271 // Custom expand fp<->sint 01272 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); 01273 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); 01274 01275 // Expand fp<->uint 01276 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand); 01277 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand); 01278 01279 setOperationAction(ISD::BITCAST, MVT::f32, Expand); 01280 setOperationAction(ISD::BITCAST, MVT::i32, Expand); 01281 01282 // Sparc has no select or setcc: expand to SELECT_CC. 01283 setOperationAction(ISD::SELECT, MVT::i32, Expand); 01284 setOperationAction(ISD::SELECT, MVT::f32, Expand); 01285 setOperationAction(ISD::SELECT, MVT::f64, Expand); 01286 setOperationAction(ISD::SETCC, MVT::i32, Expand); 01287 setOperationAction(ISD::SETCC, MVT::f32, Expand); 01288 setOperationAction(ISD::SETCC, MVT::f64, Expand); 01289 01290 // Sparc doesn't have BRCOND either, it has BR_CC. 01291 setOperationAction(ISD::BRCOND, MVT::Other, Expand); 01292 setOperationAction(ISD::BRIND, MVT::Other, Expand); 01293 setOperationAction(ISD::BR_JT, MVT::Other, Expand); 01294 setOperationAction(ISD::BR_CC, MVT::i32, Custom); 01295 setOperationAction(ISD::BR_CC, MVT::f32, Custom); 01296 setOperationAction(ISD::BR_CC, MVT::f64, Custom); 01297 01298 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom); 01299 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom); 01300 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom); 01301 01302 if (Subtarget->is64Bit()) { 01303 setOperationAction(ISD::BR_CC, MVT::i64, Custom); 01304 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom); 01305 } 01306 01307 // FIXME: There are instructions available for ATOMIC_FENCE 01308 // on SparcV8 and later. 01309 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand); 01310 01311 setOperationAction(ISD::FSIN , MVT::f64, Expand); 01312 setOperationAction(ISD::FCOS , MVT::f64, Expand); 01313 setOperationAction(ISD::FSINCOS, MVT::f64, Expand); 01314 setOperationAction(ISD::FREM , MVT::f64, Expand); 01315 setOperationAction(ISD::FMA , MVT::f64, Expand); 01316 setOperationAction(ISD::FSIN , MVT::f32, Expand); 01317 setOperationAction(ISD::FCOS , MVT::f32, Expand); 01318 setOperationAction(ISD::FSINCOS, MVT::f32, Expand); 01319 setOperationAction(ISD::FREM , MVT::f32, Expand); 01320 setOperationAction(ISD::FMA , MVT::f32, Expand); 01321 setOperationAction(ISD::CTPOP, MVT::i32, Expand); 01322 setOperationAction(ISD::CTTZ , MVT::i32, Expand); 01323 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand); 01324 setOperationAction(ISD::CTLZ , MVT::i32, Expand); 01325 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand); 01326 setOperationAction(ISD::ROTL , MVT::i32, Expand); 01327 setOperationAction(ISD::ROTR , MVT::i32, Expand); 01328 setOperationAction(ISD::BSWAP, MVT::i32, Expand); 01329 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); 01330 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand); 01331 setOperationAction(ISD::FPOW , MVT::f64, Expand); 01332 setOperationAction(ISD::FPOW , MVT::f32, Expand); 01333 01334 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); 01335 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand); 01336 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand); 01337 01338 // FIXME: Sparc provides these multiplies, but we don't have them yet. 01339 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand); 01340 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand); 01341 01342 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); 01343 01344 // VASTART needs to be custom lowered to use the VarArgsFrameIndex. 01345 setOperationAction(ISD::VASTART , MVT::Other, Custom); 01346 // VAARG needs to be lowered to not do unaligned accesses for doubles. 01347 setOperationAction(ISD::VAARG , MVT::Other, Custom); 01348 01349 // Use the default implementation. 01350 setOperationAction(ISD::VACOPY , MVT::Other, Expand); 01351 setOperationAction(ISD::VAEND , MVT::Other, Expand); 01352 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand); 01353 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand); 01354 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom); 01355 01356 // No debug info support yet. 01357 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); 01358 01359 setStackPointerRegisterToSaveRestore(SP::O6); 01360 01361 if (TM.getSubtarget<SparcSubtarget>().isV9()) 01362 setOperationAction(ISD::CTPOP, MVT::i32, Legal); 01363 01364 setMinFunctionAlignment(2); 01365 01366 computeRegisterProperties(); 01367 } 01368 01369 const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const { 01370 switch (Opcode) { 01371 default: return 0; 01372 case SPISD::CMPICC: return "SPISD::CMPICC"; 01373 case SPISD::CMPFCC: return "SPISD::CMPFCC"; 01374 case SPISD::BRICC: return "SPISD::BRICC"; 01375 case SPISD::BRXCC: return "SPISD::BRXCC"; 01376 case SPISD::BRFCC: return "SPISD::BRFCC"; 01377 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC"; 01378 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC"; 01379 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC"; 01380 case SPISD::Hi: return "SPISD::Hi"; 01381 case SPISD::Lo: return "SPISD::Lo"; 01382 case SPISD::FTOI: return "SPISD::FTOI"; 01383 case SPISD::ITOF: return "SPISD::ITOF"; 01384 case SPISD::CALL: return "SPISD::CALL"; 01385 case SPISD::RET_FLAG: return "SPISD::RET_FLAG"; 01386 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG"; 01387 case SPISD::FLUSHW: return "SPISD::FLUSHW"; 01388 } 01389 } 01390 01391 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to 01392 /// be zero. Op is expected to be a target specific node. Used by DAG 01393 /// combiner. 01394 void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op, 01395 APInt &KnownZero, 01396 APInt &KnownOne, 01397 const SelectionDAG &DAG, 01398 unsigned Depth) const { 01399 APInt KnownZero2, KnownOne2; 01400 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0); 01401 01402 switch (Op.getOpcode()) { 01403 default: break; 01404 case SPISD::SELECT_ICC: 01405 case SPISD::SELECT_XCC: 01406 case SPISD::SELECT_FCC: 01407 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1); 01408 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1); 01409 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?"); 01410 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?"); 01411 01412 // Only known if known in both the LHS and RHS. 01413 KnownOne &= KnownOne2; 01414 KnownZero &= KnownZero2; 01415 break; 01416 } 01417 } 01418 01419 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so 01420 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition. 01421 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS, 01422 ISD::CondCode CC, unsigned &SPCC) { 01423 if (isa<ConstantSDNode>(RHS) && 01424 cast<ConstantSDNode>(RHS)->isNullValue() && 01425 CC == ISD::SETNE && 01426 (((LHS.getOpcode() == SPISD::SELECT_ICC || 01427 LHS.getOpcode() == SPISD::SELECT_XCC) && 01428 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) || 01429 (LHS.getOpcode() == SPISD::SELECT_FCC && 01430 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) && 01431 isa<ConstantSDNode>(LHS.getOperand(0)) && 01432 isa<ConstantSDNode>(LHS.getOperand(1)) && 01433 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() && 01434 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) { 01435 SDValue CMPCC = LHS.getOperand(3); 01436 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue(); 01437 LHS = CMPCC.getOperand(0); 01438 RHS = CMPCC.getOperand(1); 01439 } 01440 } 01441 01442 // Convert to a target node and set target flags. 01443 SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF, 01444 SelectionDAG &DAG) const { 01445 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) 01446 return DAG.getTargetGlobalAddress(GA->getGlobal(), 01447 GA->getDebugLoc(), 01448 GA->getValueType(0), 01449 GA->getOffset(), TF); 01450 01451 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op)) 01452 return DAG.getTargetConstantPool(CP->getConstVal(), 01453 CP->getValueType(0), 01454 CP->getAlignment(), 01455 CP->getOffset(), TF); 01456 01457 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op)) 01458 return DAG.getTargetExternalSymbol(ES->getSymbol(), 01459 ES->getValueType(0), TF); 01460 01461 llvm_unreachable("Unhandled address SDNode"); 01462 } 01463 01464 // Split Op into high and low parts according to HiTF and LoTF. 01465 // Return an ADD node combining the parts. 01466 SDValue SparcTargetLowering::makeHiLoPair(SDValue Op, 01467 unsigned HiTF, unsigned LoTF, 01468 SelectionDAG &DAG) const { 01469 DebugLoc DL = Op.getDebugLoc(); 01470 EVT VT = Op.getValueType(); 01471 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG)); 01472 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG)); 01473 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo); 01474 } 01475 01476 // Build SDNodes for producing an address from a GlobalAddress, ConstantPool, 01477 // or ExternalSymbol SDNode. 01478 SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const { 01479 DebugLoc DL = Op.getDebugLoc(); 01480 EVT VT = getPointerTy(); 01481 01482 // Handle PIC mode first. 01483 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) { 01484 // This is the pic32 code model, the GOT is known to be smaller than 4GB. 01485 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG); 01486 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT); 01487 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo); 01488 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr, 01489 MachinePointerInfo::getGOT(), false, false, false, 0); 01490 } 01491 01492 // This is one of the absolute code models. 01493 switch(getTargetMachine().getCodeModel()) { 01494 default: 01495 llvm_unreachable("Unsupported absolute code model"); 01496 case CodeModel::Small: 01497 // abs32. 01498 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG); 01499 case CodeModel::Medium: { 01500 // abs44. 01501 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG); 01502 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32)); 01503 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG); 01504 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44); 01505 return DAG.getNode(ISD::ADD, DL, VT, H44, L44); 01506 } 01507 case CodeModel::Large: { 01508 // abs64. 01509 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG); 01510 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32)); 01511 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG); 01512 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo); 01513 } 01514 } 01515 } 01516 01517 SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op, 01518 SelectionDAG &DAG) const { 01519 return makeAddress(Op, DAG); 01520 } 01521 01522 SDValue SparcTargetLowering::LowerConstantPool(SDValue Op, 01523 SelectionDAG &DAG) const { 01524 return makeAddress(Op, DAG); 01525 } 01526 01527 static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) { 01528 DebugLoc dl = Op.getDebugLoc(); 01529 // Convert the fp value to integer in an FP register. 01530 assert(Op.getValueType() == MVT::i32); 01531 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0)); 01532 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op); 01533 } 01534 01535 static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) { 01536 DebugLoc dl = Op.getDebugLoc(); 01537 assert(Op.getOperand(0).getValueType() == MVT::i32); 01538 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0)); 01539 // Convert the int value to FP in an FP register. 01540 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp); 01541 } 01542 01543 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) { 01544 SDValue Chain = Op.getOperand(0); 01545 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get(); 01546 SDValue LHS = Op.getOperand(2); 01547 SDValue RHS = Op.getOperand(3); 01548 SDValue Dest = Op.getOperand(4); 01549 DebugLoc dl = Op.getDebugLoc(); 01550 unsigned Opc, SPCC = ~0U; 01551 01552 // If this is a br_cc of a "setcc", and if the setcc got lowered into 01553 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values. 01554 LookThroughSetCC(LHS, RHS, CC, SPCC); 01555 01556 // Get the condition flag. 01557 SDValue CompareFlag; 01558 if (LHS.getValueType().isInteger()) { 01559 EVT VTs[] = { LHS.getValueType(), MVT::Glue }; 01560 SDValue Ops[2] = { LHS, RHS }; 01561 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1); 01562 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC); 01563 // 32-bit compares use the icc flags, 64-bit uses the xcc flags. 01564 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC; 01565 } else { 01566 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS); 01567 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC); 01568 Opc = SPISD::BRFCC; 01569 } 01570 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest, 01571 DAG.getConstant(SPCC, MVT::i32), CompareFlag); 01572 } 01573 01574 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) { 01575 SDValue LHS = Op.getOperand(0); 01576 SDValue RHS = Op.getOperand(1); 01577 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get(); 01578 SDValue TrueVal = Op.getOperand(2); 01579 SDValue FalseVal = Op.getOperand(3); 01580 DebugLoc dl = Op.getDebugLoc(); 01581 unsigned Opc, SPCC = ~0U; 01582 01583 // If this is a select_cc of a "setcc", and if the setcc got lowered into 01584 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values. 01585 LookThroughSetCC(LHS, RHS, CC, SPCC); 01586 01587 SDValue CompareFlag; 01588 if (LHS.getValueType().isInteger()) { 01589 // subcc returns a value 01590 EVT VTs[] = { LHS.getValueType(), MVT::Glue }; 01591 SDValue Ops[2] = { LHS, RHS }; 01592 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1); 01593 Opc = LHS.getValueType() == MVT::i32 ? 01594 SPISD::SELECT_ICC : SPISD::SELECT_XCC; 01595 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC); 01596 } else { 01597 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS); 01598 Opc = SPISD::SELECT_FCC; 01599 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC); 01600 } 01601 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal, 01602 DAG.getConstant(SPCC, MVT::i32), CompareFlag); 01603 } 01604 01605 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG, 01606 const SparcTargetLowering &TLI) { 01607 MachineFunction &MF = DAG.getMachineFunction(); 01608 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>(); 01609 01610 // vastart just stores the address of the VarArgsFrameIndex slot into the 01611 // memory location argument. 01612 DebugLoc DL = Op.getDebugLoc(); 01613 SDValue Offset = 01614 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(), 01615 DAG.getRegister(SP::I6, TLI.getPointerTy()), 01616 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset())); 01617 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); 01618 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1), 01619 MachinePointerInfo(SV), false, false, 0); 01620 } 01621 01622 static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) { 01623 SDNode *Node = Op.getNode(); 01624 EVT VT = Node->getValueType(0); 01625 SDValue InChain = Node->getOperand(0); 01626 SDValue VAListPtr = Node->getOperand(1); 01627 EVT PtrVT = VAListPtr.getValueType(); 01628 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue(); 01629 DebugLoc DL = Node->getDebugLoc(); 01630 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr, 01631 MachinePointerInfo(SV), false, false, false, 0); 01632 // Increment the pointer, VAList, to the next vaarg. 01633 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList, 01634 DAG.getIntPtrConstant(VT.getSizeInBits()/8)); 01635 // Store the incremented VAList to the legalized pointer. 01636 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr, 01637 VAListPtr, MachinePointerInfo(SV), false, false, 0); 01638 // Load the actual argument out of the pointer VAList. 01639 // We can't count on greater alignment than the word size. 01640 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(), 01641 false, false, false, 01642 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8); 01643 } 01644 01645 static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) { 01646 SDValue Chain = Op.getOperand(0); // Legalize the chain. 01647 SDValue Size = Op.getOperand(1); // Legalize the size. 01648 DebugLoc dl = Op.getDebugLoc(); 01649 01650 unsigned SPReg = SP::O6; 01651 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32); 01652 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value 01653 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain 01654 01655 // The resultant pointer is actually 16 words from the bottom of the stack, 01656 // to provide a register spill area. 01657 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP, 01658 DAG.getConstant(96, MVT::i32)); 01659 SDValue Ops[2] = { NewVal, Chain }; 01660 return DAG.getMergeValues(Ops, 2, dl); 01661 } 01662 01663 01664 static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) { 01665 DebugLoc dl = Op.getDebugLoc(); 01666 SDValue Chain = DAG.getNode(SPISD::FLUSHW, 01667 dl, MVT::Other, DAG.getEntryNode()); 01668 return Chain; 01669 } 01670 01671 static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) { 01672 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 01673 MFI->setFrameAddressIsTaken(true); 01674 01675 EVT VT = Op.getValueType(); 01676 DebugLoc dl = Op.getDebugLoc(); 01677 unsigned FrameReg = SP::I6; 01678 01679 uint64_t depth = Op.getConstantOperandVal(0); 01680 01681 SDValue FrameAddr; 01682 if (depth == 0) 01683 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); 01684 else { 01685 // flush first to make sure the windowed registers' values are in stack 01686 SDValue Chain = getFLUSHW(Op, DAG); 01687 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT); 01688 01689 for (uint64_t i = 0; i != depth; ++i) { 01690 SDValue Ptr = DAG.getNode(ISD::ADD, 01691 dl, MVT::i32, 01692 FrameAddr, DAG.getIntPtrConstant(56)); 01693 FrameAddr = DAG.getLoad(MVT::i32, dl, 01694 Chain, 01695 Ptr, 01696 MachinePointerInfo(), false, false, false, 0); 01697 } 01698 } 01699 return FrameAddr; 01700 } 01701 01702 static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) { 01703 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo(); 01704 MFI->setReturnAddressIsTaken(true); 01705 01706 EVT VT = Op.getValueType(); 01707 DebugLoc dl = Op.getDebugLoc(); 01708 unsigned RetReg = SP::I7; 01709 01710 uint64_t depth = Op.getConstantOperandVal(0); 01711 01712 SDValue RetAddr; 01713 if (depth == 0) 01714 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT); 01715 else { 01716 // flush first to make sure the windowed registers' values are in stack 01717 SDValue Chain = getFLUSHW(Op, DAG); 01718 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT); 01719 01720 for (uint64_t i = 0; i != depth; ++i) { 01721 SDValue Ptr = DAG.getNode(ISD::ADD, 01722 dl, MVT::i32, 01723 RetAddr, 01724 DAG.getIntPtrConstant((i == depth-1)?60:56)); 01725 RetAddr = DAG.getLoad(MVT::i32, dl, 01726 Chain, 01727 Ptr, 01728 MachinePointerInfo(), false, false, false, 0); 01729 } 01730 } 01731 return RetAddr; 01732 } 01733 01734 SDValue SparcTargetLowering:: 01735 LowerOperation(SDValue Op, SelectionDAG &DAG) const { 01736 switch (Op.getOpcode()) { 01737 default: llvm_unreachable("Should not custom lower this!"); 01738 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); 01739 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); 01740 case ISD::GlobalTLSAddress: 01741 llvm_unreachable("TLS not implemented for Sparc."); 01742 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); 01743 case ISD::ConstantPool: return LowerConstantPool(Op, DAG); 01744 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG); 01745 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); 01746 case ISD::BR_CC: return LowerBR_CC(Op, DAG); 01747 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG); 01748 case ISD::VASTART: return LowerVASTART(Op, DAG, *this); 01749 case ISD::VAARG: return LowerVAARG(Op, DAG); 01750 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); 01751 } 01752 } 01753 01754 MachineBasicBlock * 01755 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI, 01756 MachineBasicBlock *BB) const { 01757 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo(); 01758 unsigned BROpcode; 01759 unsigned CC; 01760 DebugLoc dl = MI->getDebugLoc(); 01761 // Figure out the conditional branch opcode to use for this select_cc. 01762 switch (MI->getOpcode()) { 01763 default: llvm_unreachable("Unknown SELECT_CC!"); 01764 case SP::SELECT_CC_Int_ICC: 01765 case SP::SELECT_CC_FP_ICC: 01766 case SP::SELECT_CC_DFP_ICC: 01767 BROpcode = SP::BCOND; 01768 break; 01769 case SP::SELECT_CC_Int_FCC: 01770 case SP::SELECT_CC_FP_FCC: 01771 case SP::SELECT_CC_DFP_FCC: 01772 BROpcode = SP::FBCOND; 01773 break; 01774 } 01775 01776 CC = (SPCC::CondCodes)MI->getOperand(3).getImm(); 01777 01778 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond 01779 // control-flow pattern. The incoming instruction knows the destination vreg 01780 // to set, the condition code register to branch on, the true/false values to 01781 // select between, and a branch opcode to use. 01782 const BasicBlock *LLVM_BB = BB->getBasicBlock(); 01783 MachineFunction::iterator It = BB; 01784 ++It; 01785 01786 // thisMBB: 01787 // ... 01788 // TrueVal = ... 01789 // [f]bCC copy1MBB 01790 // fallthrough --> copy0MBB 01791 MachineBasicBlock *thisMBB = BB; 01792 MachineFunction *F = BB->getParent(); 01793 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB); 01794 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB); 01795 F->insert(It, copy0MBB); 01796 F->insert(It, sinkMBB); 01797 01798 // Transfer the remainder of BB and its successor edges to sinkMBB. 01799 sinkMBB->splice(sinkMBB->begin(), BB, 01800 llvm::next(MachineBasicBlock::iterator(MI)), 01801 BB->end()); 01802 sinkMBB->transferSuccessorsAndUpdatePHIs(BB); 01803 01804 // Add the true and fallthrough blocks as its successors. 01805 BB->addSuccessor(copy0MBB); 01806 BB->addSuccessor(sinkMBB); 01807 01808 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC); 01809 01810 // copy0MBB: 01811 // %FalseValue = ... 01812 // # fallthrough to sinkMBB 01813 BB = copy0MBB; 01814 01815 // Update machine-CFG edges 01816 BB->addSuccessor(sinkMBB); 01817 01818 // sinkMBB: 01819 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ] 01820 // ... 01821 BB = sinkMBB; 01822 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg()) 01823 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB) 01824 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB); 01825 01826 MI->eraseFromParent(); // The pseudo instruction is gone now. 01827 return BB; 01828 } 01829 01830 //===----------------------------------------------------------------------===// 01831 // Sparc Inline Assembly Support 01832 //===----------------------------------------------------------------------===// 01833 01834 /// getConstraintType - Given a constraint letter, return the type of 01835 /// constraint it is for this target. 01836 SparcTargetLowering::ConstraintType 01837 SparcTargetLowering::getConstraintType(const std::string &Constraint) const { 01838 if (Constraint.size() == 1) { 01839 switch (Constraint[0]) { 01840 default: break; 01841 case 'r': return C_RegisterClass; 01842 } 01843 } 01844 01845 return TargetLowering::getConstraintType(Constraint); 01846 } 01847 01848 std::pair<unsigned, const TargetRegisterClass*> 01849 SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint, 01850 EVT VT) const { 01851 if (Constraint.size() == 1) { 01852 switch (Constraint[0]) { 01853 case 'r': 01854 return std::make_pair(0U, &SP::IntRegsRegClass); 01855 } 01856 } 01857 01858 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT); 01859 } 01860 01861 bool 01862 SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const { 01863 // The Sparc target isn't yet aware of offsets. 01864 return false; 01865 }