1 //===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the interfaces that Sparc uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
15 #include "SparcISelLowering.h"
16 #include "MCTargetDesc/SparcBaseInfo.h"
17 #include "SparcMachineFunctionInfo.h"
18 #include "SparcRegisterInfo.h"
19 #include "SparcTargetMachine.h"
20 #include "SparcTargetObjectFile.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/SelectionDAG.h"
27 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/ErrorHandling.h"
35 //===----------------------------------------------------------------------===//
36 // Calling Convention Implementation
37 //===----------------------------------------------------------------------===//
39 static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
40 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
41 ISD::ArgFlagsTy &ArgFlags, CCState &State)
43 assert (ArgFlags.isSRet());
45 // Assign SRet argument.
46 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
52 static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
53 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
54 ISD::ArgFlagsTy &ArgFlags, CCState &State)
56 static const uint16_t RegList[] = {
57 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
59 // Try to get first reg.
60 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
61 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
63 // Assign whole thing in stack.
64 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
65 State.AllocateStack(8,4),
70 // Try to get second reg.
71 if (unsigned Reg = State.AllocateReg(RegList, 6))
72 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
74 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
75 State.AllocateStack(4,4),
80 // Allocate a full-sized argument for the 64-bit ABI.
81 static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
82 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
83 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
84 assert((LocVT == MVT::f32 || LocVT == MVT::f128
85 || LocVT.getSizeInBits() == 64) &&
86 "Can't handle non-64 bits locations");
88 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
89 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
90 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
91 unsigned Offset = State.AllocateStack(size, alignment);
94 if (LocVT == MVT::i64 && Offset < 6*8)
95 // Promote integers to %i0-%i5.
96 Reg = SP::I0 + Offset/8;
97 else if (LocVT == MVT::f64 && Offset < 16*8)
98 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
99 Reg = SP::D0 + Offset/8;
100 else if (LocVT == MVT::f32 && Offset < 16*8)
101 // Promote floats to %f1, %f3, ...
102 Reg = SP::F1 + Offset/4;
103 else if (LocVT == MVT::f128 && Offset < 16*8)
104 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
105 Reg = SP::Q0 + Offset/16;
107 // Promote to register when possible, otherwise use the stack slot.
109 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
113 // This argument goes on the stack in an 8-byte slot.
114 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
115 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
116 if (LocVT == MVT::f32)
119 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
123 // Allocate a half-sized argument for the 64-bit ABI.
125 // This is used when passing { float, int } structs by value in registers.
126 static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
127 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
128 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
129 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
130 unsigned Offset = State.AllocateStack(4, 4);
132 if (LocVT == MVT::f32 && Offset < 16*8) {
133 // Promote floats to %f0-%f31.
134 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
139 if (LocVT == MVT::i32 && Offset < 6*8) {
140 // Promote integers to %i0-%i5, using half the register.
141 unsigned Reg = SP::I0 + Offset/8;
143 LocInfo = CCValAssign::AExt;
145 // Set the Custom bit if this i32 goes in the high bits of a register.
147 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
150 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
154 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
158 #include "SparcGenCallingConv.inc"
160 // The calling conventions in SparcCallingConv.td are described in terms of the
161 // callee's register window. This function translates registers to the
162 // corresponding caller window %o register.
163 static unsigned toCallerWindow(unsigned Reg) {
164 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
165 if (Reg >= SP::I0 && Reg <= SP::I7)
166 return Reg - SP::I0 + SP::O0;
171 SparcTargetLowering::LowerReturn(SDValue Chain,
172 CallingConv::ID CallConv, bool IsVarArg,
173 const SmallVectorImpl<ISD::OutputArg> &Outs,
174 const SmallVectorImpl<SDValue> &OutVals,
175 SDLoc DL, SelectionDAG &DAG) const {
176 if (Subtarget->is64Bit())
177 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
178 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
182 SparcTargetLowering::LowerReturn_32(SDValue Chain,
183 CallingConv::ID CallConv, bool IsVarArg,
184 const SmallVectorImpl<ISD::OutputArg> &Outs,
185 const SmallVectorImpl<SDValue> &OutVals,
186 SDLoc DL, SelectionDAG &DAG) const {
187 MachineFunction &MF = DAG.getMachineFunction();
189 // CCValAssign - represent the assignment of the return value to locations.
190 SmallVector<CCValAssign, 16> RVLocs;
192 // CCState - Info about the registers and stack slot.
193 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
194 DAG.getTarget(), RVLocs, *DAG.getContext());
196 // Analyze return values.
197 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
200 SmallVector<SDValue, 4> RetOps(1, Chain);
201 // Make room for the return address offset.
202 RetOps.push_back(SDValue());
204 // Copy the result values into the output registers.
205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
206 CCValAssign &VA = RVLocs[i];
207 assert(VA.isRegLoc() && "Can only return in registers!");
209 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
212 // Guarantee that all emitted copies are stuck together with flags.
213 Flag = Chain.getValue(1);
214 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
217 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
218 // If the function returns a struct, copy the SRetReturnReg to I0
219 if (MF.getFunction()->hasStructRetAttr()) {
220 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
221 unsigned Reg = SFI->getSRetReturnReg();
223 llvm_unreachable("sret virtual register not created in the entry block");
224 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
225 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
226 Flag = Chain.getValue(1);
227 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
228 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
231 RetOps[0] = Chain; // Update chain.
232 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
234 // Add the flag if we have it.
236 RetOps.push_back(Flag);
238 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
239 &RetOps[0], RetOps.size());
242 // Lower return values for the 64-bit ABI.
243 // Return values are passed the exactly the same way as function arguments.
245 SparcTargetLowering::LowerReturn_64(SDValue Chain,
246 CallingConv::ID CallConv, bool IsVarArg,
247 const SmallVectorImpl<ISD::OutputArg> &Outs,
248 const SmallVectorImpl<SDValue> &OutVals,
249 SDLoc DL, SelectionDAG &DAG) const {
250 // CCValAssign - represent the assignment of the return value to locations.
251 SmallVector<CCValAssign, 16> RVLocs;
253 // CCState - Info about the registers and stack slot.
254 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
255 DAG.getTarget(), RVLocs, *DAG.getContext());
257 // Analyze return values.
258 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
261 SmallVector<SDValue, 4> RetOps(1, Chain);
263 // The second operand on the return instruction is the return address offset.
264 // The return address is always %i7+8 with the 64-bit ABI.
265 RetOps.push_back(DAG.getConstant(8, MVT::i32));
267 // Copy the result values into the output registers.
268 for (unsigned i = 0; i != RVLocs.size(); ++i) {
269 CCValAssign &VA = RVLocs[i];
270 assert(VA.isRegLoc() && "Can only return in registers!");
271 SDValue OutVal = OutVals[i];
273 // Integer return values must be sign or zero extended by the callee.
274 switch (VA.getLocInfo()) {
275 case CCValAssign::Full: break;
276 case CCValAssign::SExt:
277 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
279 case CCValAssign::ZExt:
280 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
282 case CCValAssign::AExt:
283 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
286 llvm_unreachable("Unknown loc info!");
289 // The custom bit on an i32 return value indicates that it should be passed
290 // in the high bits of the register.
291 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
292 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
293 DAG.getConstant(32, MVT::i32));
295 // The next value may go in the low bits of the same register.
296 // Handle both at once.
297 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
298 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
299 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
300 // Skip the next value, it's already done.
305 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
307 // Guarantee that all emitted copies are stuck together with flags.
308 Flag = Chain.getValue(1);
309 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
312 RetOps[0] = Chain; // Update chain.
314 // Add the flag if we have it.
316 RetOps.push_back(Flag);
318 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
319 &RetOps[0], RetOps.size());
322 SDValue SparcTargetLowering::
323 LowerFormalArguments(SDValue Chain,
324 CallingConv::ID CallConv,
326 const SmallVectorImpl<ISD::InputArg> &Ins,
329 SmallVectorImpl<SDValue> &InVals) const {
330 if (Subtarget->is64Bit())
331 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
333 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
337 /// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
338 /// passed in either one or two GPRs, including FP values. TODO: we should
339 /// pass FP values in FP registers for fastcc functions.
340 SDValue SparcTargetLowering::
341 LowerFormalArguments_32(SDValue Chain,
342 CallingConv::ID CallConv,
344 const SmallVectorImpl<ISD::InputArg> &Ins,
347 SmallVectorImpl<SDValue> &InVals) const {
348 MachineFunction &MF = DAG.getMachineFunction();
349 MachineRegisterInfo &RegInfo = MF.getRegInfo();
350 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
352 // Assign locations to all of the incoming arguments.
353 SmallVector<CCValAssign, 16> ArgLocs;
354 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
355 getTargetMachine(), ArgLocs, *DAG.getContext());
356 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
358 const unsigned StackOffset = 92;
360 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
361 CCValAssign &VA = ArgLocs[i];
363 if (i == 0 && Ins[i].Flags.isSRet()) {
364 // Get SRet from [%fp+64].
365 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
366 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
367 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
368 MachinePointerInfo(),
369 false, false, false, 0);
370 InVals.push_back(Arg);
375 if (VA.needsCustom()) {
376 assert(VA.getLocVT() == MVT::f64);
377 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
378 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
379 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
382 CCValAssign &NextVA = ArgLocs[++i];
385 if (NextVA.isMemLoc()) {
386 int FrameIdx = MF.getFrameInfo()->
387 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
388 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
389 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
390 MachinePointerInfo(),
391 false, false, false, 0);
393 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
394 &SP::IntRegsRegClass);
395 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
398 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
399 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
400 InVals.push_back(WholeValue);
403 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
404 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
405 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
406 if (VA.getLocVT() == MVT::f32)
407 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
408 else if (VA.getLocVT() != MVT::i32) {
409 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
410 DAG.getValueType(VA.getLocVT()));
411 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
413 InVals.push_back(Arg);
417 assert(VA.isMemLoc());
419 unsigned Offset = VA.getLocMemOffset()+StackOffset;
421 if (VA.needsCustom()) {
422 assert(VA.getValVT() == MVT::f64);
423 // If it is double-word aligned, just load.
424 if (Offset % 8 == 0) {
425 int FI = MF.getFrameInfo()->CreateFixedObject(8,
428 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
429 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
430 MachinePointerInfo(),
431 false,false, false, 0);
432 InVals.push_back(Load);
436 int FI = MF.getFrameInfo()->CreateFixedObject(4,
439 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
440 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
441 MachinePointerInfo(),
442 false, false, false, 0);
443 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
446 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
448 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
449 MachinePointerInfo(),
450 false, false, false, 0);
453 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
454 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
455 InVals.push_back(WholeValue);
459 int FI = MF.getFrameInfo()->CreateFixedObject(4,
462 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
464 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
465 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
466 MachinePointerInfo(),
467 false, false, false, 0);
469 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
470 // Sparc is big endian, so add an offset based on the ObjectVT.
471 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
472 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
473 DAG.getConstant(Offset, MVT::i32));
474 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
475 MachinePointerInfo(),
476 VA.getValVT(), false, false,0);
477 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
479 InVals.push_back(Load);
482 if (MF.getFunction()->hasStructRetAttr()) {
483 // Copy the SRet Argument to SRetReturnReg.
484 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
485 unsigned Reg = SFI->getSRetReturnReg();
487 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
488 SFI->setSRetReturnReg(Reg);
490 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
491 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
494 // Store remaining ArgRegs to the stack if this is a varargs function.
496 static const uint16_t ArgRegs[] = {
497 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
499 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
500 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
501 unsigned ArgOffset = CCInfo.getNextStackOffset();
502 if (NumAllocated == 6)
503 ArgOffset += StackOffset;
506 ArgOffset = 68+4*NumAllocated;
509 // Remember the vararg offset for the va_start implementation.
510 FuncInfo->setVarArgsFrameOffset(ArgOffset);
512 std::vector<SDValue> OutChains;
514 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
515 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
516 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
517 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
519 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
521 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
523 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
524 MachinePointerInfo(),
529 if (!OutChains.empty()) {
530 OutChains.push_back(Chain);
531 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
532 &OutChains[0], OutChains.size());
539 // Lower formal arguments for the 64 bit ABI.
540 SDValue SparcTargetLowering::
541 LowerFormalArguments_64(SDValue Chain,
542 CallingConv::ID CallConv,
544 const SmallVectorImpl<ISD::InputArg> &Ins,
547 SmallVectorImpl<SDValue> &InVals) const {
548 MachineFunction &MF = DAG.getMachineFunction();
550 // Analyze arguments according to CC_Sparc64.
551 SmallVector<CCValAssign, 16> ArgLocs;
552 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
553 getTargetMachine(), ArgLocs, *DAG.getContext());
554 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
556 // The argument array begins at %fp+BIAS+128, after the register save area.
557 const unsigned ArgArea = 128;
559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
560 CCValAssign &VA = ArgLocs[i];
562 // This argument is passed in a register.
563 // All integer register arguments are promoted by the caller to i64.
565 // Create a virtual register for the promoted live-in value.
566 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
567 getRegClassFor(VA.getLocVT()));
568 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
570 // Get the high bits for i32 struct elements.
571 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
572 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
573 DAG.getConstant(32, MVT::i32));
575 // The caller promoted the argument, so insert an Assert?ext SDNode so we
576 // won't promote the value again in this function.
577 switch (VA.getLocInfo()) {
578 case CCValAssign::SExt:
579 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
580 DAG.getValueType(VA.getValVT()));
582 case CCValAssign::ZExt:
583 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
584 DAG.getValueType(VA.getValVT()));
590 // Truncate the register down to the argument type.
592 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
594 InVals.push_back(Arg);
598 // The registers are exhausted. This argument was passed on the stack.
599 assert(VA.isMemLoc());
600 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
601 // beginning of the arguments area at %fp+BIAS+128.
602 unsigned Offset = VA.getLocMemOffset() + ArgArea;
603 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
604 // Adjust offset for extended arguments, SPARC is big-endian.
605 // The caller will have written the full slot with extended bytes, but we
606 // prefer our own extending loads.
608 Offset += 8 - ValSize;
609 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
610 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
611 DAG.getFrameIndex(FI, getPointerTy()),
612 MachinePointerInfo::getFixedStack(FI),
613 false, false, false, 0));
619 // This function takes variable arguments, some of which may have been passed
620 // in registers %i0-%i5. Variable floating point arguments are never passed
621 // in floating point registers. They go on %i0-%i5 or on the stack like
622 // integer arguments.
624 // The va_start intrinsic needs to know the offset to the first variable
626 unsigned ArgOffset = CCInfo.getNextStackOffset();
627 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
628 // Skip the 128 bytes of register save area.
629 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
630 Subtarget->getStackPointerBias());
632 // Save the variable arguments that were passed in registers.
633 // The caller is required to reserve stack space for 6 arguments regardless
634 // of how many arguments were actually passed.
635 SmallVector<SDValue, 8> OutChains;
636 for (; ArgOffset < 6*8; ArgOffset += 8) {
637 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
638 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
639 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
640 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
641 DAG.getFrameIndex(FI, getPointerTy()),
642 MachinePointerInfo::getFixedStack(FI),
646 if (!OutChains.empty())
647 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
648 &OutChains[0], OutChains.size());
654 SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
655 SmallVectorImpl<SDValue> &InVals) const {
656 if (Subtarget->is64Bit())
657 return LowerCall_64(CLI, InVals);
658 return LowerCall_32(CLI, InVals);
661 static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
662 ImmutableCallSite *CS) {
664 return CS->hasFnAttr(Attribute::ReturnsTwice);
666 const Function *CalleeFn = 0;
667 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
668 CalleeFn = dyn_cast<Function>(G->getGlobal());
669 } else if (ExternalSymbolSDNode *E =
670 dyn_cast<ExternalSymbolSDNode>(Callee)) {
671 const Function *Fn = DAG.getMachineFunction().getFunction();
672 const Module *M = Fn->getParent();
673 const char *CalleeName = E->getSymbol();
674 CalleeFn = M->getFunction(CalleeName);
679 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
682 // Lower a call for the 32-bit ABI.
684 SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
685 SmallVectorImpl<SDValue> &InVals) const {
686 SelectionDAG &DAG = CLI.DAG;
688 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
689 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
690 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
691 SDValue Chain = CLI.Chain;
692 SDValue Callee = CLI.Callee;
693 bool &isTailCall = CLI.IsTailCall;
694 CallingConv::ID CallConv = CLI.CallConv;
695 bool isVarArg = CLI.IsVarArg;
697 // Sparc target does not yet support tail call optimization.
700 // Analyze operands of the call, assigning locations to each operand.
701 SmallVector<CCValAssign, 16> ArgLocs;
702 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
703 DAG.getTarget(), ArgLocs, *DAG.getContext());
704 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
706 // Get the size of the outgoing arguments stack space requirement.
707 unsigned ArgsSize = CCInfo.getNextStackOffset();
709 // Keep stack frames 8-byte aligned.
710 ArgsSize = (ArgsSize+7) & ~7;
712 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
714 // Create local copies for byval args.
715 SmallVector<SDValue, 8> ByValArgs;
716 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
717 ISD::ArgFlagsTy Flags = Outs[i].Flags;
718 if (!Flags.isByVal())
721 SDValue Arg = OutVals[i];
722 unsigned Size = Flags.getByValSize();
723 unsigned Align = Flags.getByValAlign();
725 int FI = MFI->CreateStackObject(Size, Align, false);
726 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
727 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
729 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
730 false, // isVolatile,
731 (Size <= 32), // AlwaysInline if size <= 32
732 MachinePointerInfo(), MachinePointerInfo());
733 ByValArgs.push_back(FIPtr);
736 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
739 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
740 SmallVector<SDValue, 8> MemOpChains;
742 const unsigned StackOffset = 92;
743 bool hasStructRetAttr = false;
744 // Walk the register/memloc assignments, inserting copies/loads.
745 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
748 CCValAssign &VA = ArgLocs[i];
749 SDValue Arg = OutVals[realArgIdx];
751 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
753 // Use local copy if it is a byval arg.
755 Arg = ByValArgs[byvalArgIdx++];
757 // Promote the value if needed.
758 switch (VA.getLocInfo()) {
759 default: llvm_unreachable("Unknown loc info!");
760 case CCValAssign::Full: break;
761 case CCValAssign::SExt:
762 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
764 case CCValAssign::ZExt:
765 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
767 case CCValAssign::AExt:
768 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
770 case CCValAssign::BCvt:
771 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
775 if (Flags.isSRet()) {
776 assert(VA.needsCustom());
777 // store SRet argument in %sp+64
778 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
779 SDValue PtrOff = DAG.getIntPtrConstant(64);
780 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
781 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
782 MachinePointerInfo(),
784 hasStructRetAttr = true;
788 if (VA.needsCustom()) {
789 assert(VA.getLocVT() == MVT::f64);
792 unsigned Offset = VA.getLocMemOffset() + StackOffset;
793 // if it is double-word aligned, just store.
794 if (Offset % 8 == 0) {
795 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
796 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
797 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
798 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
799 MachinePointerInfo(),
805 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
806 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
807 Arg, StackPtr, MachinePointerInfo(),
809 // Sparc is big-endian, so the high part comes first.
810 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
811 MachinePointerInfo(), false, false, false, 0);
812 // Increment the pointer to the other half.
813 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
814 DAG.getIntPtrConstant(4));
815 // Load the low part.
816 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
817 MachinePointerInfo(), false, false, false, 0);
820 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
822 CCValAssign &NextVA = ArgLocs[++i];
823 if (NextVA.isRegLoc()) {
824 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
826 // Store the low part in stack.
827 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
828 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
829 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
830 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
831 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
832 MachinePointerInfo(),
836 unsigned Offset = VA.getLocMemOffset() + StackOffset;
837 // Store the high part.
838 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
839 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
840 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
841 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
842 MachinePointerInfo(),
844 // Store the low part.
845 PtrOff = DAG.getIntPtrConstant(Offset+4);
846 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
847 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
848 MachinePointerInfo(),
854 // Arguments that can be passed on register must be kept at
857 if (VA.getLocVT() != MVT::f32) {
858 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
861 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
862 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
866 assert(VA.isMemLoc());
868 // Create a store off the stack pointer for this argument.
869 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
870 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
871 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
872 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
873 MachinePointerInfo(),
878 // Emit all stores, make sure the occur before any copies into physregs.
879 if (!MemOpChains.empty())
880 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
881 &MemOpChains[0], MemOpChains.size());
883 // Build a sequence of copy-to-reg nodes chained together with token
884 // chain and flag operands which copy the outgoing args into registers.
885 // The InFlag in necessary since all emitted instructions must be
888 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
889 unsigned Reg = toCallerWindow(RegsToPass[i].first);
890 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
891 InFlag = Chain.getValue(1);
894 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
895 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
897 // If the callee is a GlobalAddress node (quite common, every direct call is)
898 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
899 // Likewise ExternalSymbol -> TargetExternalSymbol.
900 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
901 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
902 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
903 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
905 // Returns a chain & a flag for retval copy to use
906 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
907 SmallVector<SDValue, 8> Ops;
908 Ops.push_back(Chain);
909 Ops.push_back(Callee);
910 if (hasStructRetAttr)
911 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
912 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
913 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
914 RegsToPass[i].second.getValueType()));
916 // Add a register mask operand representing the call-preserved registers.
917 const SparcRegisterInfo *TRI =
918 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
919 const uint32_t *Mask = ((hasReturnsTwice)
920 ? TRI->getRTCallPreservedMask(CallConv)
921 : TRI->getCallPreservedMask(CallConv));
922 assert(Mask && "Missing call preserved mask for calling convention");
923 Ops.push_back(DAG.getRegisterMask(Mask));
925 if (InFlag.getNode())
926 Ops.push_back(InFlag);
928 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
929 InFlag = Chain.getValue(1);
931 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
932 DAG.getIntPtrConstant(0, true), InFlag, dl);
933 InFlag = Chain.getValue(1);
935 // Assign locations to each value returned by this call.
936 SmallVector<CCValAssign, 16> RVLocs;
937 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
938 DAG.getTarget(), RVLocs, *DAG.getContext());
940 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
942 // Copy all of the result registers out of their specified physreg.
943 for (unsigned i = 0; i != RVLocs.size(); ++i) {
944 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
945 RVLocs[i].getValVT(), InFlag).getValue(1);
946 InFlag = Chain.getValue(2);
947 InVals.push_back(Chain.getValue(0));
953 // This functions returns true if CalleeName is a ABI function that returns
954 // a long double (fp128).
955 static bool isFP128ABICall(const char *CalleeName)
957 static const char *const ABICalls[] =
958 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
960 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
961 "_Q_lltoq", "_Q_ulltoq",
964 for (const char * const *I = ABICalls; *I != 0; ++I)
965 if (strcmp(CalleeName, *I) == 0)
971 SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
973 const Function *CalleeFn = 0;
974 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
975 CalleeFn = dyn_cast<Function>(G->getGlobal());
976 } else if (ExternalSymbolSDNode *E =
977 dyn_cast<ExternalSymbolSDNode>(Callee)) {
978 const Function *Fn = DAG.getMachineFunction().getFunction();
979 const Module *M = Fn->getParent();
980 const char *CalleeName = E->getSymbol();
981 CalleeFn = M->getFunction(CalleeName);
982 if (!CalleeFn && isFP128ABICall(CalleeName))
983 return 16; // Return sizeof(fp128)
989 assert(CalleeFn->hasStructRetAttr() &&
990 "Callee does not have the StructRet attribute.");
992 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
993 Type *ElementTy = Ty->getElementType();
994 return getDataLayout()->getTypeAllocSize(ElementTy);
998 // Fixup floating point arguments in the ... part of a varargs call.
1000 // The SPARC v9 ABI requires that floating point arguments are treated the same
1001 // as integers when calling a varargs function. This does not apply to the
1002 // fixed arguments that are part of the function's prototype.
1004 // This function post-processes a CCValAssign array created by
1005 // AnalyzeCallOperands().
1006 static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1007 ArrayRef<ISD::OutputArg> Outs) {
1008 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1009 const CCValAssign &VA = ArgLocs[i];
1010 MVT ValTy = VA.getLocVT();
1011 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1012 // varargs functions.
1013 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
1015 // The fixed arguments to a varargs function still go in FP registers.
1016 if (Outs[VA.getValNo()].IsFixed)
1019 // This floating point argument should be reassigned.
1022 // Determine the offset into the argument array.
1023 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1024 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1025 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
1026 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1029 // This argument should go in %i0-%i5.
1030 unsigned IReg = SP::I0 + Offset/8;
1031 if (ValTy == MVT::f64)
1032 // Full register, just bitconvert into i64.
1033 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1034 IReg, MVT::i64, CCValAssign::BCvt);
1036 assert(ValTy == MVT::f128 && "Unexpected type!");
1037 // Full register, just bitconvert into i128 -- We will lower this into
1038 // two i64s in LowerCall_64.
1039 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1040 IReg, MVT::i128, CCValAssign::BCvt);
1043 // This needs to go to memory, we're out of integer registers.
1044 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1045 Offset, VA.getLocVT(), VA.getLocInfo());
1051 // Lower a call for the 64-bit ABI.
1053 SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1054 SmallVectorImpl<SDValue> &InVals) const {
1055 SelectionDAG &DAG = CLI.DAG;
1057 SDValue Chain = CLI.Chain;
1059 // Sparc target does not yet support tail call optimization.
1060 CLI.IsTailCall = false;
1062 // Analyze operands of the call, assigning locations to each operand.
1063 SmallVector<CCValAssign, 16> ArgLocs;
1064 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1065 DAG.getTarget(), ArgLocs, *DAG.getContext());
1066 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1068 // Get the size of the outgoing arguments stack space requirement.
1069 // The stack offset computed by CC_Sparc64 includes all arguments.
1070 // Called functions expect 6 argument words to exist in the stack frame, used
1072 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
1074 // Keep stack frames 16-byte aligned.
1075 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1077 // Varargs calls require special treatment.
1079 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1081 // Adjust the stack pointer to make room for the arguments.
1082 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1083 // with more than 6 arguments.
1084 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1087 // Collect the set of registers to pass to the function and their values.
1088 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1090 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1092 // Collect chains from all the memory opeations that copy arguments to the
1093 // stack. They must follow the stack pointer adjustment above and precede the
1094 // call instruction itself.
1095 SmallVector<SDValue, 8> MemOpChains;
1097 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1098 const CCValAssign &VA = ArgLocs[i];
1099 SDValue Arg = CLI.OutVals[i];
1101 // Promote the value if needed.
1102 switch (VA.getLocInfo()) {
1104 llvm_unreachable("Unknown location info!");
1105 case CCValAssign::Full:
1107 case CCValAssign::SExt:
1108 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1110 case CCValAssign::ZExt:
1111 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1113 case CCValAssign::AExt:
1114 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1116 case CCValAssign::BCvt:
1117 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1118 // SPARC does not support i128 natively. Lower it into two i64, see below.
1119 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1120 || VA.getLocVT() != MVT::i128)
1121 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1125 if (VA.isRegLoc()) {
1126 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1127 && VA.getLocVT() == MVT::i128) {
1128 // Store and reload into the interger register reg and reg+1.
1129 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1130 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
1131 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1132 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset);
1133 HiPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1135 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8);
1136 LoPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1139 // Store to %sp+BIAS+128+Offset
1140 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1141 MachinePointerInfo(),
1143 // Load into Reg and Reg+1
1144 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1145 MachinePointerInfo(),
1146 false, false, false, 0);
1147 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1148 MachinePointerInfo(),
1149 false, false, false, 0);
1150 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1152 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1157 // The custom bit on an i32 return value indicates that it should be
1158 // passed in the high bits of the register.
1159 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1160 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1161 DAG.getConstant(32, MVT::i32));
1163 // The next value may go in the low bits of the same register.
1164 // Handle both at once.
1165 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1166 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1167 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1169 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1170 // Skip the next value, it's already done.
1174 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
1178 assert(VA.isMemLoc());
1180 // Create a store off the stack pointer for this argument.
1181 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1182 // The argument area starts at %fp+BIAS+128 in the callee frame,
1183 // %sp+BIAS+128 in ours.
1184 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1185 Subtarget->getStackPointerBias() +
1187 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1188 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1189 MachinePointerInfo(),
1193 // Emit all stores, make sure they occur before the call.
1194 if (!MemOpChains.empty())
1195 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1196 &MemOpChains[0], MemOpChains.size());
1198 // Build a sequence of CopyToReg nodes glued together with token chain and
1199 // glue operands which copy the outgoing args into registers. The InGlue is
1200 // necessary since all emitted instructions must be stuck together in order
1201 // to pass the live physical registers.
1203 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1204 Chain = DAG.getCopyToReg(Chain, DL,
1205 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1206 InGlue = Chain.getValue(1);
1209 // If the callee is a GlobalAddress node (quite common, every direct call is)
1210 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1211 // Likewise ExternalSymbol -> TargetExternalSymbol.
1212 SDValue Callee = CLI.Callee;
1213 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
1214 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1215 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1216 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1217 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1219 // Build the operands for the call instruction itself.
1220 SmallVector<SDValue, 8> Ops;
1221 Ops.push_back(Chain);
1222 Ops.push_back(Callee);
1223 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1224 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1225 RegsToPass[i].second.getValueType()));
1227 // Add a register mask operand representing the call-preserved registers.
1228 const SparcRegisterInfo *TRI =
1229 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1230 const uint32_t *Mask = ((hasReturnsTwice)
1231 ? TRI->getRTCallPreservedMask(CLI.CallConv)
1232 : TRI->getCallPreservedMask(CLI.CallConv));
1233 assert(Mask && "Missing call preserved mask for calling convention");
1234 Ops.push_back(DAG.getRegisterMask(Mask));
1236 // Make sure the CopyToReg nodes are glued to the call instruction which
1237 // consumes the registers.
1238 if (InGlue.getNode())
1239 Ops.push_back(InGlue);
1241 // Now the call itself.
1242 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1243 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1244 InGlue = Chain.getValue(1);
1246 // Revert the stack pointer immediately after the call.
1247 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1248 DAG.getIntPtrConstant(0, true), InGlue, DL);
1249 InGlue = Chain.getValue(1);
1251 // Now extract the return values. This is more or less the same as
1252 // LowerFormalArguments_64.
1254 // Assign locations to each value returned by this call.
1255 SmallVector<CCValAssign, 16> RVLocs;
1256 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1257 DAG.getTarget(), RVLocs, *DAG.getContext());
1259 // Set inreg flag manually for codegen generated library calls that
1261 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == 0)
1262 CLI.Ins[0].Flags.setInReg();
1264 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
1266 // Copy all of the result registers out of their specified physreg.
1267 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1268 CCValAssign &VA = RVLocs[i];
1269 unsigned Reg = toCallerWindow(VA.getLocReg());
1271 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1272 // reside in the same register in the high and low bits. Reuse the
1273 // CopyFromReg previous node to avoid duplicate copies.
1275 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1276 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1277 RV = Chain.getValue(0);
1279 // But usually we'll create a new CopyFromReg for a different register.
1280 if (!RV.getNode()) {
1281 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1282 Chain = RV.getValue(1);
1283 InGlue = Chain.getValue(2);
1286 // Get the high bits for i32 struct elements.
1287 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1288 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1289 DAG.getConstant(32, MVT::i32));
1291 // The callee promoted the return value, so insert an Assert?ext SDNode so
1292 // we won't promote the value again in this function.
1293 switch (VA.getLocInfo()) {
1294 case CCValAssign::SExt:
1295 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1296 DAG.getValueType(VA.getValVT()));
1298 case CCValAssign::ZExt:
1299 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1300 DAG.getValueType(VA.getValVT()));
1306 // Truncate the register down to the return value type.
1307 if (VA.isExtInLoc())
1308 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1310 InVals.push_back(RV);
1316 //===----------------------------------------------------------------------===//
1317 // TargetLowering Implementation
1318 //===----------------------------------------------------------------------===//
1320 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1322 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1324 default: llvm_unreachable("Unknown integer condition code!");
1325 case ISD::SETEQ: return SPCC::ICC_E;
1326 case ISD::SETNE: return SPCC::ICC_NE;
1327 case ISD::SETLT: return SPCC::ICC_L;
1328 case ISD::SETGT: return SPCC::ICC_G;
1329 case ISD::SETLE: return SPCC::ICC_LE;
1330 case ISD::SETGE: return SPCC::ICC_GE;
1331 case ISD::SETULT: return SPCC::ICC_CS;
1332 case ISD::SETULE: return SPCC::ICC_LEU;
1333 case ISD::SETUGT: return SPCC::ICC_GU;
1334 case ISD::SETUGE: return SPCC::ICC_CC;
1338 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1340 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1342 default: llvm_unreachable("Unknown fp condition code!");
1344 case ISD::SETOEQ: return SPCC::FCC_E;
1346 case ISD::SETUNE: return SPCC::FCC_NE;
1348 case ISD::SETOLT: return SPCC::FCC_L;
1350 case ISD::SETOGT: return SPCC::FCC_G;
1352 case ISD::SETOLE: return SPCC::FCC_LE;
1354 case ISD::SETOGE: return SPCC::FCC_GE;
1355 case ISD::SETULT: return SPCC::FCC_UL;
1356 case ISD::SETULE: return SPCC::FCC_ULE;
1357 case ISD::SETUGT: return SPCC::FCC_UG;
1358 case ISD::SETUGE: return SPCC::FCC_UGE;
1359 case ISD::SETUO: return SPCC::FCC_U;
1360 case ISD::SETO: return SPCC::FCC_O;
1361 case ISD::SETONE: return SPCC::FCC_LG;
1362 case ISD::SETUEQ: return SPCC::FCC_UE;
1366 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
1367 : TargetLowering(TM, new SparcELFTargetObjectFile()) {
1368 Subtarget = &TM.getSubtarget<SparcSubtarget>();
1370 // Set up the register classes.
1371 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1372 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1373 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1374 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1375 if (Subtarget->is64Bit())
1376 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
1378 // Turn FP extload into load/fextend
1379 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1380 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1382 // Sparc doesn't have i1 sign extending load
1383 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
1385 // Turn FP truncstore into trunc + store.
1386 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1387 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1388 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
1390 // Custom legalize GlobalAddress nodes into LO/HI parts.
1391 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1392 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1393 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
1394 setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
1396 // Sparc doesn't have sext_inreg, replace them with shl/sra
1397 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1398 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1399 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1401 // Sparc has no REM or DIVREM operations.
1402 setOperationAction(ISD::UREM, MVT::i32, Expand);
1403 setOperationAction(ISD::SREM, MVT::i32, Expand);
1404 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1405 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1407 // ... nor does SparcV9.
1408 if (Subtarget->is64Bit()) {
1409 setOperationAction(ISD::UREM, MVT::i64, Expand);
1410 setOperationAction(ISD::SREM, MVT::i64, Expand);
1411 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1412 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1415 // Custom expand fp<->sint
1416 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1417 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
1418 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1419 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
1421 // Custom Expand fp<->uint
1422 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1423 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
1424 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1425 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
1427 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1428 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
1430 // Sparc has no select or setcc: expand to SELECT_CC.
1431 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1432 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1433 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1434 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1436 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1437 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1438 setOperationAction(ISD::SETCC, MVT::f64, Expand);
1439 setOperationAction(ISD::SETCC, MVT::f128, Expand);
1441 // Sparc doesn't have BRCOND either, it has BR_CC.
1442 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1443 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1444 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1445 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1446 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1447 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1448 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
1450 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1451 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1452 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
1453 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
1455 if (Subtarget->is64Bit()) {
1456 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1457 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1458 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1459 setOperationAction(ISD::SUBE, MVT::i64, Custom);
1460 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1461 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
1462 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1463 setOperationAction(ISD::SETCC, MVT::i64, Expand);
1464 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
1465 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
1467 setOperationAction(ISD::CTPOP, MVT::i64,
1468 Subtarget->usePopc() ? Legal : Expand);
1469 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1470 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1471 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1472 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1473 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1474 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1475 setOperationAction(ISD::ROTR , MVT::i64, Expand);
1476 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
1480 // FIXME: We insert fences for each atomics and generate sub-optimal code
1481 // for PSO/TSO. Also, implement other atomicrmw operations.
1483 setInsertFencesForAtomic(true);
1485 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
1486 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32,
1487 (Subtarget->isV9() ? Legal: Expand));
1490 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1492 // Custom Lower Atomic LOAD/STORE
1493 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1494 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1496 if (Subtarget->is64Bit()) {
1497 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
1498 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
1499 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1500 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1503 if (!Subtarget->isV9()) {
1504 // SparcV8 does not have FNEGD and FABSD.
1505 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1506 setOperationAction(ISD::FABS, MVT::f64, Custom);
1509 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1510 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1511 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1512 setOperationAction(ISD::FREM , MVT::f128, Expand);
1513 setOperationAction(ISD::FMA , MVT::f128, Expand);
1514 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1515 setOperationAction(ISD::FCOS , MVT::f64, Expand);
1516 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1517 setOperationAction(ISD::FREM , MVT::f64, Expand);
1518 setOperationAction(ISD::FMA , MVT::f64, Expand);
1519 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1520 setOperationAction(ISD::FCOS , MVT::f32, Expand);
1521 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
1522 setOperationAction(ISD::FREM , MVT::f32, Expand);
1523 setOperationAction(ISD::FMA , MVT::f32, Expand);
1524 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1525 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1526 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1527 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1528 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1529 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1530 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1531 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
1532 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1533 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1534 setOperationAction(ISD::FPOW , MVT::f128, Expand);
1535 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1536 setOperationAction(ISD::FPOW , MVT::f32, Expand);
1538 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1539 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1540 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1542 // FIXME: Sparc provides these multiplies, but we don't have them yet.
1543 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1544 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1546 if (Subtarget->is64Bit()) {
1547 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1548 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1549 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1550 setOperationAction(ISD::MULHS, MVT::i64, Expand);
1552 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1553 setOperationAction(ISD::SMULO, MVT::i64, Custom);
1556 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1557 setOperationAction(ISD::VASTART , MVT::Other, Custom);
1558 // VAARG needs to be lowered to not do unaligned accesses for doubles.
1559 setOperationAction(ISD::VAARG , MVT::Other, Custom);
1561 // Use the default implementation.
1562 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1563 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1564 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1565 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1566 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
1568 setExceptionPointerRegister(SP::I0);
1569 setExceptionSelectorRegister(SP::I1);
1571 setStackPointerRegisterToSaveRestore(SP::O6);
1573 setOperationAction(ISD::CTPOP, MVT::i32,
1574 Subtarget->usePopc() ? Legal : Expand);
1576 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1577 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1578 setOperationAction(ISD::STORE, MVT::f128, Legal);
1580 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1581 setOperationAction(ISD::STORE, MVT::f128, Custom);
1584 if (Subtarget->hasHardQuad()) {
1585 setOperationAction(ISD::FADD, MVT::f128, Legal);
1586 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1587 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1588 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1589 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1590 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1591 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1592 if (Subtarget->isV9()) {
1593 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1594 setOperationAction(ISD::FABS, MVT::f128, Legal);
1596 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1597 setOperationAction(ISD::FABS, MVT::f128, Custom);
1600 if (!Subtarget->is64Bit()) {
1601 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1602 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1603 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1604 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1608 // Custom legalize f128 operations.
1610 setOperationAction(ISD::FADD, MVT::f128, Custom);
1611 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1612 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1613 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1614 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1615 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1616 setOperationAction(ISD::FABS, MVT::f128, Custom);
1618 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1619 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1620 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1622 // Setup Runtime library names.
1623 if (Subtarget->is64Bit()) {
1624 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1625 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1626 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1627 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1628 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1629 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
1630 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
1631 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
1632 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
1633 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1634 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1635 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1636 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
1637 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1638 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1639 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1640 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1642 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1643 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1644 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1645 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1646 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1647 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
1648 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
1649 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
1650 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
1651 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1652 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1653 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1654 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1655 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1656 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1657 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1658 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1662 setMinFunctionAlignment(2);
1664 computeRegisterProperties();
1667 const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1670 case SPISD::CMPICC: return "SPISD::CMPICC";
1671 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1672 case SPISD::BRICC: return "SPISD::BRICC";
1673 case SPISD::BRXCC: return "SPISD::BRXCC";
1674 case SPISD::BRFCC: return "SPISD::BRFCC";
1675 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1676 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1677 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1678 case SPISD::Hi: return "SPISD::Hi";
1679 case SPISD::Lo: return "SPISD::Lo";
1680 case SPISD::FTOI: return "SPISD::FTOI";
1681 case SPISD::ITOF: return "SPISD::ITOF";
1682 case SPISD::FTOX: return "SPISD::FTOX";
1683 case SPISD::XTOF: return "SPISD::XTOF";
1684 case SPISD::CALL: return "SPISD::CALL";
1685 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
1686 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
1687 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1688 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1689 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1690 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
1694 EVT SparcTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1697 return VT.changeVectorElementTypeToInteger();
1700 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1701 /// be zero. Op is expected to be a target specific node. Used by DAG
1703 void SparcTargetLowering::computeMaskedBitsForTargetNode
1707 const SelectionDAG &DAG,
1708 unsigned Depth) const {
1709 APInt KnownZero2, KnownOne2;
1710 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
1712 switch (Op.getOpcode()) {
1714 case SPISD::SELECT_ICC:
1715 case SPISD::SELECT_XCC:
1716 case SPISD::SELECT_FCC:
1717 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1718 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
1719 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1720 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1722 // Only known if known in both the LHS and RHS.
1723 KnownOne &= KnownOne2;
1724 KnownZero &= KnownZero2;
1729 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1730 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1731 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1732 ISD::CondCode CC, unsigned &SPCC) {
1733 if (isa<ConstantSDNode>(RHS) &&
1734 cast<ConstantSDNode>(RHS)->isNullValue() &&
1736 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1737 LHS.getOpcode() == SPISD::SELECT_XCC) &&
1738 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1739 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1740 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1741 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1742 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1743 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1744 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
1745 SDValue CMPCC = LHS.getOperand(3);
1746 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1747 LHS = CMPCC.getOperand(0);
1748 RHS = CMPCC.getOperand(1);
1752 // Convert to a target node and set target flags.
1753 SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1754 SelectionDAG &DAG) const {
1755 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1756 return DAG.getTargetGlobalAddress(GA->getGlobal(),
1758 GA->getValueType(0),
1759 GA->getOffset(), TF);
1761 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1762 return DAG.getTargetConstantPool(CP->getConstVal(),
1763 CP->getValueType(0),
1765 CP->getOffset(), TF);
1767 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1768 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1773 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1774 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1775 ES->getValueType(0), TF);
1777 llvm_unreachable("Unhandled address SDNode");
1780 // Split Op into high and low parts according to HiTF and LoTF.
1781 // Return an ADD node combining the parts.
1782 SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1783 unsigned HiTF, unsigned LoTF,
1784 SelectionDAG &DAG) const {
1786 EVT VT = Op.getValueType();
1787 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1788 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1789 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1792 // Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1793 // or ExternalSymbol SDNode.
1794 SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
1796 EVT VT = getPointerTy();
1798 // Handle PIC mode first.
1799 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1800 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1801 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1802 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1803 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
1804 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1805 // function has calls.
1806 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1807 MFI->setHasCalls(true);
1808 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1809 MachinePointerInfo::getGOT(), false, false, false, 0);
1812 // This is one of the absolute code models.
1813 switch(getTargetMachine().getCodeModel()) {
1815 llvm_unreachable("Unsupported absolute code model");
1816 case CodeModel::Small:
1818 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1819 case CodeModel::Medium: {
1821 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
1822 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
1823 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1824 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1825 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1827 case CodeModel::Large: {
1829 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
1830 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
1831 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1832 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1837 SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
1838 SelectionDAG &DAG) const {
1839 return makeAddress(Op, DAG);
1842 SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
1843 SelectionDAG &DAG) const {
1844 return makeAddress(Op, DAG);
1847 SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1848 SelectionDAG &DAG) const {
1849 return makeAddress(Op, DAG);
1852 SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1853 SelectionDAG &DAG) const {
1855 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1857 const GlobalValue *GV = GA->getGlobal();
1858 EVT PtrVT = getPointerTy();
1860 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1862 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1863 unsigned HiTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_HI22
1864 : SPII::MO_TLS_LDM_HI22);
1865 unsigned LoTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_LO10
1866 : SPII::MO_TLS_LDM_LO10);
1867 unsigned addTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_ADD
1868 : SPII::MO_TLS_LDM_ADD);
1869 unsigned callTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_CALL
1870 : SPII::MO_TLS_LDM_CALL);
1872 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1873 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1874 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1875 withTargetFlags(Op, addTF, DAG));
1877 SDValue Chain = DAG.getEntryNode();
1880 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1881 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1882 InFlag = Chain.getValue(1);
1883 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1884 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1886 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1887 SmallVector<SDValue, 4> Ops;
1888 Ops.push_back(Chain);
1889 Ops.push_back(Callee);
1890 Ops.push_back(Symbol);
1891 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1892 const uint32_t *Mask = getTargetMachine()
1893 .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1894 assert(Mask && "Missing call preserved mask for calling convention");
1895 Ops.push_back(DAG.getRegisterMask(Mask));
1896 Ops.push_back(InFlag);
1897 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1898 InFlag = Chain.getValue(1);
1899 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1900 DAG.getIntPtrConstant(0, true), InFlag, DL);
1901 InFlag = Chain.getValue(1);
1902 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1904 if (model != TLSModel::LocalDynamic)
1907 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1908 withTargetFlags(Op, SPII::MO_TLS_LDO_HIX22, DAG));
1909 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1910 withTargetFlags(Op, SPII::MO_TLS_LDO_LOX10, DAG));
1911 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1912 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
1913 withTargetFlags(Op, SPII::MO_TLS_LDO_ADD, DAG));
1916 if (model == TLSModel::InitialExec) {
1917 unsigned ldTF = ((PtrVT == MVT::i64)? SPII::MO_TLS_IE_LDX
1918 : SPII::MO_TLS_IE_LD);
1920 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1922 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1923 // function has calls.
1924 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1925 MFI->setHasCalls(true);
1927 SDValue TGA = makeHiLoPair(Op,
1928 SPII::MO_TLS_IE_HI22, SPII::MO_TLS_IE_LO10, DAG);
1929 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1930 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1932 withTargetFlags(Op, ldTF, DAG));
1933 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1934 DAG.getRegister(SP::G7, PtrVT), Offset,
1935 withTargetFlags(Op, SPII::MO_TLS_IE_ADD, DAG));
1938 assert(model == TLSModel::LocalExec);
1939 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1940 withTargetFlags(Op, SPII::MO_TLS_LE_HIX22, DAG));
1941 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1942 withTargetFlags(Op, SPII::MO_TLS_LE_LOX10, DAG));
1943 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1945 return DAG.getNode(ISD::ADD, DL, PtrVT,
1946 DAG.getRegister(SP::G7, PtrVT), Offset);
1950 SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1951 SDValue Arg, SDLoc DL,
1952 SelectionDAG &DAG) const {
1953 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1954 EVT ArgVT = Arg.getValueType();
1955 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1961 if (ArgTy->isFP128Ty()) {
1962 // Create a stack object and pass the pointer to the library function.
1963 int FI = MFI->CreateStackObject(16, 8, false);
1964 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1965 Chain = DAG.getStore(Chain,
1969 MachinePointerInfo(),
1975 Entry.Ty = PointerType::getUnqual(ArgTy);
1977 Args.push_back(Entry);
1982 SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
1983 const char *LibFuncName,
1984 unsigned numArgs) const {
1988 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1990 SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
1991 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
1992 Type *RetTyABI = RetTy;
1993 SDValue Chain = DAG.getEntryNode();
1996 if (RetTy->isFP128Ty()) {
1997 // Create a Stack Object to receive the return value of type f128.
1999 int RetFI = MFI->CreateStackObject(16, 8, false);
2000 RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
2001 Entry.Node = RetPtr;
2002 Entry.Ty = PointerType::getUnqual(RetTy);
2003 if (!Subtarget->is64Bit())
2004 Entry.isSRet = true;
2005 Entry.isReturned = false;
2006 Args.push_back(Entry);
2007 RetTyABI = Type::getVoidTy(*DAG.getContext());
2010 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2011 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2012 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2015 CallLoweringInfo CLI(Chain,
2017 false, false, false, false,
2020 Callee, Args, DAG, SDLoc(Op));
2021 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2023 // chain is in second result.
2024 if (RetTyABI == RetTy)
2025 return CallInfo.first;
2027 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2029 Chain = CallInfo.second;
2031 // Load RetPtr to get the return value.
2032 return DAG.getLoad(Op.getValueType(),
2036 MachinePointerInfo(),
2037 false, false, false, 8);
2041 SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2044 SelectionDAG &DAG) const {
2046 const char *LibCall = 0;
2047 bool is64Bit = Subtarget->is64Bit();
2049 default: llvm_unreachable("Unhandled conditional code!");
2050 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2051 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2052 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2053 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2054 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2055 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2063 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2066 SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
2067 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2069 SDValue Chain = DAG.getEntryNode();
2070 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2071 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2074 CallLoweringInfo CLI(Chain,
2076 false, false, false, false,
2079 Callee, Args, DAG, DL);
2081 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2083 // result is in first, and chain is in second result.
2084 SDValue Result = CallInfo.first;
2088 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2089 SPCC = SPCC::ICC_NE;
2090 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2092 case SPCC::FCC_UL : {
2093 SDValue Mask = DAG.getTargetConstant(1, Result.getValueType());
2094 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2095 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2096 SPCC = SPCC::ICC_NE;
2097 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2099 case SPCC::FCC_ULE: {
2100 SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
2101 SPCC = SPCC::ICC_NE;
2102 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2104 case SPCC::FCC_UG : {
2105 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2107 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2109 case SPCC::FCC_UGE: {
2110 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2111 SPCC = SPCC::ICC_NE;
2112 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2115 case SPCC::FCC_U : {
2116 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2118 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2120 case SPCC::FCC_O : {
2121 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2122 SPCC = SPCC::ICC_NE;
2123 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2125 case SPCC::FCC_LG : {
2126 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2127 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2128 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2129 SPCC = SPCC::ICC_NE;
2130 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2132 case SPCC::FCC_UE : {
2133 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2134 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2135 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2137 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2143 LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2144 const SparcTargetLowering &TLI) {
2146 if (Op.getOperand(0).getValueType() == MVT::f64)
2147 return TLI.LowerF128Op(Op, DAG,
2148 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2150 if (Op.getOperand(0).getValueType() == MVT::f32)
2151 return TLI.LowerF128Op(Op, DAG,
2152 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2154 llvm_unreachable("fpextend with non-float operand!");
2155 return SDValue(0, 0);
2159 LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2160 const SparcTargetLowering &TLI) {
2161 // FP_ROUND on f64 and f32 are legal.
2162 if (Op.getOperand(0).getValueType() != MVT::f128)
2165 if (Op.getValueType() == MVT::f64)
2166 return TLI.LowerF128Op(Op, DAG,
2167 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2168 if (Op.getValueType() == MVT::f32)
2169 return TLI.LowerF128Op(Op, DAG,
2170 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2172 llvm_unreachable("fpround to non-float!");
2173 return SDValue(0, 0);
2176 static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2177 const SparcTargetLowering &TLI,
2180 EVT VT = Op.getValueType();
2181 assert(VT == MVT::i32 || VT == MVT::i64);
2183 // Expand f128 operations to fp128 abi calls.
2184 if (Op.getOperand(0).getValueType() == MVT::f128
2185 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2186 const char *libName = TLI.getLibcallName(VT == MVT::i32
2187 ? RTLIB::FPTOSINT_F128_I32
2188 : RTLIB::FPTOSINT_F128_I64);
2189 return TLI.LowerF128Op(Op, DAG, libName, 1);
2192 // Expand if the resulting type is illegal.
2193 if (!TLI.isTypeLegal(VT))
2194 return SDValue(0, 0);
2196 // Otherwise, Convert the fp value to integer in an FP register.
2198 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2200 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2202 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
2205 static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2206 const SparcTargetLowering &TLI,
2209 EVT OpVT = Op.getOperand(0).getValueType();
2210 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2212 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2214 // Expand f128 operations to fp128 ABI calls.
2215 if (Op.getValueType() == MVT::f128
2216 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2217 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2218 ? RTLIB::SINTTOFP_I32_F128
2219 : RTLIB::SINTTOFP_I64_F128);
2220 return TLI.LowerF128Op(Op, DAG, libName, 1);
2223 // Expand if the operand type is illegal.
2224 if (!TLI.isTypeLegal(OpVT))
2225 return SDValue(0, 0);
2227 // Otherwise, Convert the int value to FP in an FP register.
2228 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2229 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2230 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
2233 static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2234 const SparcTargetLowering &TLI,
2237 EVT VT = Op.getValueType();
2239 // Expand if it does not involve f128 or the target has support for
2240 // quad floating point instructions and the resulting type is legal.
2241 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2242 (hasHardQuad && TLI.isTypeLegal(VT)))
2243 return SDValue(0, 0);
2245 assert(VT == MVT::i32 || VT == MVT::i64);
2247 return TLI.LowerF128Op(Op, DAG,
2248 TLI.getLibcallName(VT == MVT::i32
2249 ? RTLIB::FPTOUINT_F128_I32
2250 : RTLIB::FPTOUINT_F128_I64),
2254 static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2255 const SparcTargetLowering &TLI,
2258 EVT OpVT = Op.getOperand(0).getValueType();
2259 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2261 // Expand if it does not involve f128 or the target has support for
2262 // quad floating point instructions and the operand type is legal.
2263 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
2264 return SDValue(0, 0);
2266 return TLI.LowerF128Op(Op, DAG,
2267 TLI.getLibcallName(OpVT == MVT::i32
2268 ? RTLIB::UINTTOFP_I32_F128
2269 : RTLIB::UINTTOFP_I64_F128),
2273 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2274 const SparcTargetLowering &TLI,
2276 SDValue Chain = Op.getOperand(0);
2277 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2278 SDValue LHS = Op.getOperand(2);
2279 SDValue RHS = Op.getOperand(3);
2280 SDValue Dest = Op.getOperand(4);
2282 unsigned Opc, SPCC = ~0U;
2284 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2285 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2286 LookThroughSetCC(LHS, RHS, CC, SPCC);
2288 // Get the condition flag.
2289 SDValue CompareFlag;
2290 if (LHS.getValueType().isInteger()) {
2291 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2292 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2293 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2294 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
2296 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2297 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2298 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2301 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2302 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2306 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2307 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2310 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2311 const SparcTargetLowering &TLI,
2313 SDValue LHS = Op.getOperand(0);
2314 SDValue RHS = Op.getOperand(1);
2315 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2316 SDValue TrueVal = Op.getOperand(2);
2317 SDValue FalseVal = Op.getOperand(3);
2319 unsigned Opc, SPCC = ~0U;
2321 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2322 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2323 LookThroughSetCC(LHS, RHS, CC, SPCC);
2325 SDValue CompareFlag;
2326 if (LHS.getValueType().isInteger()) {
2327 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2328 Opc = LHS.getValueType() == MVT::i32 ?
2329 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
2330 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2332 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2333 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2334 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2335 Opc = SPISD::SELECT_ICC;
2337 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2338 Opc = SPISD::SELECT_FCC;
2339 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2342 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
2343 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2346 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
2347 const SparcTargetLowering &TLI) {
2348 MachineFunction &MF = DAG.getMachineFunction();
2349 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2351 // Need frame address to find the address of VarArgsFrameIndex.
2352 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2354 // vastart just stores the address of the VarArgsFrameIndex slot into the
2355 // memory location argument.
2358 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2359 DAG.getRegister(SP::I6, TLI.getPointerTy()),
2360 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
2361 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2362 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
2363 MachinePointerInfo(SV), false, false, 0);
2366 static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
2367 SDNode *Node = Op.getNode();
2368 EVT VT = Node->getValueType(0);
2369 SDValue InChain = Node->getOperand(0);
2370 SDValue VAListPtr = Node->getOperand(1);
2371 EVT PtrVT = VAListPtr.getValueType();
2372 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2374 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
2375 MachinePointerInfo(SV), false, false, false, 0);
2376 // Increment the pointer, VAList, to the next vaarg.
2377 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2378 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2379 // Store the incremented VAList to the legalized pointer.
2380 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
2381 VAListPtr, MachinePointerInfo(SV), false, false, 0);
2382 // Load the actual argument out of the pointer VAList.
2383 // We can't count on greater alignment than the word size.
2384 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2385 false, false, false,
2386 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
2389 static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
2390 const SparcSubtarget *Subtarget) {
2391 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2392 SDValue Size = Op.getOperand(1); // Legalize the size.
2393 EVT VT = Size->getValueType(0);
2396 unsigned SPReg = SP::O6;
2397 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2398 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
2399 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
2401 // The resultant pointer is actually 16 words from the bottom of the stack,
2402 // to provide a register spill area.
2403 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2404 regSpillArea += Subtarget->getStackPointerBias();
2406 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
2407 DAG.getConstant(regSpillArea, VT));
2408 SDValue Ops[2] = { NewVal, Chain };
2409 return DAG.getMergeValues(Ops, 2, dl);
2413 static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
2415 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
2416 dl, MVT::Other, DAG.getEntryNode());
2420 static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2421 const SparcSubtarget *Subtarget) {
2422 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2423 MFI->setFrameAddressIsTaken(true);
2425 EVT VT = Op.getValueType();
2427 unsigned FrameReg = SP::I6;
2428 unsigned stackBias = Subtarget->getStackPointerBias();
2433 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2434 if (Subtarget->is64Bit())
2435 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2436 DAG.getIntPtrConstant(stackBias));
2440 // flush first to make sure the windowed registers' values are in stack
2441 SDValue Chain = getFLUSHW(Op, DAG);
2442 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2444 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2447 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2448 DAG.getIntPtrConstant(Offset));
2449 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2450 false, false, false, 0);
2452 if (Subtarget->is64Bit())
2453 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2454 DAG.getIntPtrConstant(stackBias));
2459 static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2460 const SparcSubtarget *Subtarget) {
2462 uint64_t depth = Op.getConstantOperandVal(0);
2464 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2468 static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2469 const SparcTargetLowering &TLI,
2470 const SparcSubtarget *Subtarget) {
2471 MachineFunction &MF = DAG.getMachineFunction();
2472 MachineFrameInfo *MFI = MF.getFrameInfo();
2473 MFI->setReturnAddressIsTaken(true);
2475 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
2478 EVT VT = Op.getValueType();
2480 uint64_t depth = Op.getConstantOperandVal(0);
2484 unsigned RetReg = MF.addLiveIn(SP::I7,
2485 TLI.getRegClassFor(TLI.getPointerTy()));
2486 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
2490 // Need frame address to find return address of the caller.
2491 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2493 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2494 SDValue Ptr = DAG.getNode(ISD::ADD,
2497 DAG.getIntPtrConstant(Offset));
2498 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2499 MachinePointerInfo(), false, false, false, 0);
2504 static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
2508 assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
2509 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
2511 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2512 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2513 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2515 SDValue SrcReg64 = Op.getOperand(0);
2516 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2518 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2521 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
2523 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2525 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2527 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2532 // Lower a f128 load into two f64 loads.
2533 static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2536 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2537 assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2538 && "Unexpected node type");
2540 unsigned alignment = LdNode->getAlignment();
2544 SDValue Hi64 = DAG.getLoad(MVT::f64,
2547 LdNode->getBasePtr(),
2548 LdNode->getPointerInfo(),
2549 false, false, false, alignment);
2550 EVT addrVT = LdNode->getBasePtr().getValueType();
2551 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2552 LdNode->getBasePtr(),
2553 DAG.getConstant(8, addrVT));
2554 SDValue Lo64 = DAG.getLoad(MVT::f64,
2558 LdNode->getPointerInfo(),
2559 false, false, false, alignment);
2561 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2562 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2564 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2566 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2568 SDValue(InFP128, 0),
2571 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2573 SDValue(InFP128, 0),
2576 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2577 SDValue(Lo64.getNode(), 1) };
2578 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2580 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2581 return DAG.getMergeValues(Ops, 2, dl);
2584 // Lower a f128 store into two f64 stores.
2585 static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2587 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2588 assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2589 && "Unexpected node type");
2590 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2591 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2593 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2598 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2604 unsigned alignment = StNode->getAlignment();
2608 SDValue OutChains[2];
2609 OutChains[0] = DAG.getStore(StNode->getChain(),
2612 StNode->getBasePtr(),
2613 MachinePointerInfo(),
2614 false, false, alignment);
2615 EVT addrVT = StNode->getBasePtr().getValueType();
2616 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2617 StNode->getBasePtr(),
2618 DAG.getConstant(8, addrVT));
2619 OutChains[1] = DAG.getStore(StNode->getChain(),
2623 MachinePointerInfo(),
2624 false, false, alignment);
2625 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2629 static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2630 const SparcTargetLowering &TLI,
2632 if (Op.getValueType() == MVT::f64)
2633 return LowerF64Op(Op, DAG, ISD::FNEG);
2634 if (Op.getValueType() == MVT::f128)
2635 return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2639 static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2640 if (Op.getValueType() == MVT::f64)
2641 return LowerF64Op(Op, DAG, ISD::FABS);
2642 if (Op.getValueType() != MVT::f128)
2645 // Lower fabs on f128 to fabs on f64
2646 // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2649 SDValue SrcReg128 = Op.getOperand(0);
2650 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2652 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2655 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2657 Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
2659 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2661 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2663 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2668 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
2670 if (Op.getValueType() != MVT::i64)
2674 SDValue Src1 = Op.getOperand(0);
2675 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2676 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2677 DAG.getConstant(32, MVT::i64));
2678 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2680 SDValue Src2 = Op.getOperand(1);
2681 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2682 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2683 DAG.getConstant(32, MVT::i64));
2684 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2687 bool hasChain = false;
2688 unsigned hiOpc = Op.getOpcode();
2689 switch (Op.getOpcode()) {
2690 default: llvm_unreachable("Invalid opcode");
2691 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2692 case ISD::ADDE: hasChain = true; break;
2693 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2694 case ISD::SUBE: hasChain = true; break;
2697 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2699 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2702 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2704 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2705 SDValue Carry = Hi.getValue(1);
2707 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2708 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2709 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2710 DAG.getConstant(32, MVT::i64));
2712 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2713 SDValue Ops[2] = { Dst, Carry };
2714 return DAG.getMergeValues(Ops, 2, dl);
2717 // Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2718 // in LegalizeDAG.cpp except the order of arguments to the library function.
2719 static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2720 const SparcTargetLowering &TLI)
2722 unsigned opcode = Op.getOpcode();
2723 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2725 bool isSigned = (opcode == ISD::SMULO);
2727 EVT WideVT = MVT::i128;
2729 SDValue LHS = Op.getOperand(0);
2731 if (LHS.getValueType() != VT)
2734 SDValue ShiftAmt = DAG.getConstant(63, VT);
2736 SDValue RHS = Op.getOperand(1);
2737 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2738 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2739 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2741 SDValue MulResult = TLI.makeLibCall(DAG,
2742 RTLIB::MUL_I128, WideVT,
2743 Args, 4, isSigned, dl).first;
2744 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
2745 MulResult, DAG.getIntPtrConstant(0));
2746 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
2747 MulResult, DAG.getIntPtrConstant(1));
2749 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2750 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2752 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, VT),
2755 // MulResult is a node with an illegal type. Because such things are not
2756 // generally permitted during this phase of legalization, delete the
2757 // node. The above EXTRACT_ELEMENT nodes should have been folded.
2758 DAG.DeleteNode(MulResult.getNode());
2760 SDValue Ops[2] = { BottomHalf, TopHalf } ;
2761 return DAG.getMergeValues(Ops, 2, dl);
2764 static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
2765 // Monotonic load/stores are legal.
2766 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
2769 // Otherwise, expand with a fence.
2774 SDValue SparcTargetLowering::
2775 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2777 bool hasHardQuad = Subtarget->hasHardQuad();
2778 bool is64Bit = Subtarget->is64Bit();
2779 bool isV9 = Subtarget->isV9();
2781 switch (Op.getOpcode()) {
2782 default: llvm_unreachable("Should not custom lower this!");
2784 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
2786 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
2788 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
2789 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
2790 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2791 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2792 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2794 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2796 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
2798 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
2800 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
2802 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
2804 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
2805 case ISD::VAARG: return LowerVAARG(Op, DAG);
2806 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
2809 case ISD::LOAD: return LowerF128Load(Op, DAG);
2810 case ISD::STORE: return LowerF128Store(Op, DAG);
2811 case ISD::FADD: return LowerF128Op(Op, DAG,
2812 getLibcallName(RTLIB::ADD_F128), 2);
2813 case ISD::FSUB: return LowerF128Op(Op, DAG,
2814 getLibcallName(RTLIB::SUB_F128), 2);
2815 case ISD::FMUL: return LowerF128Op(Op, DAG,
2816 getLibcallName(RTLIB::MUL_F128), 2);
2817 case ISD::FDIV: return LowerF128Op(Op, DAG,
2818 getLibcallName(RTLIB::DIV_F128), 2);
2819 case ISD::FSQRT: return LowerF128Op(Op, DAG,
2820 getLibcallName(RTLIB::SQRT_F128),1);
2821 case ISD::FNEG: return LowerFNEG(Op, DAG, *this, is64Bit);
2822 case ISD::FABS: return LowerFABS(Op, DAG, isV9);
2823 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
2824 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
2828 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
2830 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
2831 case ISD::ATOMIC_LOAD:
2832 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
2837 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2838 MachineBasicBlock *BB) const {
2839 switch (MI->getOpcode()) {
2840 default: llvm_unreachable("Unknown SELECT_CC!");
2841 case SP::SELECT_CC_Int_ICC:
2842 case SP::SELECT_CC_FP_ICC:
2843 case SP::SELECT_CC_DFP_ICC:
2844 case SP::SELECT_CC_QFP_ICC:
2845 return expandSelectCC(MI, BB, SP::BCOND);
2846 case SP::SELECT_CC_Int_FCC:
2847 case SP::SELECT_CC_FP_FCC:
2848 case SP::SELECT_CC_DFP_FCC:
2849 case SP::SELECT_CC_QFP_FCC:
2850 return expandSelectCC(MI, BB, SP::FBCOND);
2852 case SP::ATOMIC_LOAD_ADD_32:
2853 return expandAtomicRMW(MI, BB, SP::ADDrr);
2854 case SP::ATOMIC_LOAD_ADD_64:
2855 return expandAtomicRMW(MI, BB, SP::ADDXrr);
2856 case SP::ATOMIC_LOAD_SUB_32:
2857 return expandAtomicRMW(MI, BB, SP::SUBrr);
2858 case SP::ATOMIC_LOAD_SUB_64:
2859 return expandAtomicRMW(MI, BB, SP::SUBXrr);
2860 case SP::ATOMIC_LOAD_AND_32:
2861 return expandAtomicRMW(MI, BB, SP::ANDrr);
2862 case SP::ATOMIC_LOAD_AND_64:
2863 return expandAtomicRMW(MI, BB, SP::ANDXrr);
2864 case SP::ATOMIC_LOAD_OR_32:
2865 return expandAtomicRMW(MI, BB, SP::ORrr);
2866 case SP::ATOMIC_LOAD_OR_64:
2867 return expandAtomicRMW(MI, BB, SP::ORXrr);
2868 case SP::ATOMIC_LOAD_XOR_32:
2869 return expandAtomicRMW(MI, BB, SP::XORrr);
2870 case SP::ATOMIC_LOAD_XOR_64:
2871 return expandAtomicRMW(MI, BB, SP::XORXrr);
2872 case SP::ATOMIC_LOAD_NAND_32:
2873 return expandAtomicRMW(MI, BB, SP::ANDrr);
2874 case SP::ATOMIC_LOAD_NAND_64:
2875 return expandAtomicRMW(MI, BB, SP::ANDXrr);
2877 case SP::ATOMIC_SWAP_64:
2878 return expandAtomicRMW(MI, BB, 0);
2880 case SP::ATOMIC_LOAD_MAX_32:
2881 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_G);
2882 case SP::ATOMIC_LOAD_MAX_64:
2883 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_G);
2884 case SP::ATOMIC_LOAD_MIN_32:
2885 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_LE);
2886 case SP::ATOMIC_LOAD_MIN_64:
2887 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_LE);
2888 case SP::ATOMIC_LOAD_UMAX_32:
2889 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_GU);
2890 case SP::ATOMIC_LOAD_UMAX_64:
2891 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_GU);
2892 case SP::ATOMIC_LOAD_UMIN_32:
2893 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_LEU);
2894 case SP::ATOMIC_LOAD_UMIN_64:
2895 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_LEU);
2900 SparcTargetLowering::expandSelectCC(MachineInstr *MI,
2901 MachineBasicBlock *BB,
2902 unsigned BROpcode) const {
2903 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2904 DebugLoc dl = MI->getDebugLoc();
2905 unsigned CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
2907 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2908 // control-flow pattern. The incoming instruction knows the destination vreg
2909 // to set, the condition code register to branch on, the true/false values to
2910 // select between, and a branch opcode to use.
2911 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2912 MachineFunction::iterator It = BB;
2919 // fallthrough --> copy0MBB
2920 MachineBasicBlock *thisMBB = BB;
2921 MachineFunction *F = BB->getParent();
2922 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2923 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
2924 F->insert(It, copy0MBB);
2925 F->insert(It, sinkMBB);
2927 // Transfer the remainder of BB and its successor edges to sinkMBB.
2928 sinkMBB->splice(sinkMBB->begin(), BB,
2929 llvm::next(MachineBasicBlock::iterator(MI)),
2931 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2933 // Add the true and fallthrough blocks as its successors.
2934 BB->addSuccessor(copy0MBB);
2935 BB->addSuccessor(sinkMBB);
2937 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
2940 // %FalseValue = ...
2941 // # fallthrough to sinkMBB
2944 // Update machine-CFG edges
2945 BB->addSuccessor(sinkMBB);
2948 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2951 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
2952 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2953 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
2955 MI->eraseFromParent(); // The pseudo instruction is gone now.
2960 SparcTargetLowering::expandAtomicRMW(MachineInstr *MI,
2961 MachineBasicBlock *MBB,
2963 unsigned CondCode) const {
2964 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2965 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2966 DebugLoc DL = MI->getDebugLoc();
2968 // MI is an atomic read-modify-write instruction of the form:
2970 // rd = atomicrmw<op> addr, rs2
2972 // All three operands are registers.
2973 unsigned DestReg = MI->getOperand(0).getReg();
2974 unsigned AddrReg = MI->getOperand(1).getReg();
2975 unsigned Rs2Reg = MI->getOperand(2).getReg();
2977 // SelectionDAG has already inserted memory barriers before and after MI, so
2978 // we simply have to implement the operatiuon in terms of compare-and-swap.
2980 // %val0 = load %addr
2982 // %val = phi %val0, %dest
2983 // %upd = op %val, %rs2
2984 // %dest = cas %addr, %val, %upd
2989 bool is64Bit = SP::I64RegsRegClass.hasSubClassEq(MRI.getRegClass(DestReg));
2990 const TargetRegisterClass *ValueRC =
2991 is64Bit ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
2992 unsigned Val0Reg = MRI.createVirtualRegister(ValueRC);
2994 BuildMI(*MBB, MI, DL, TII.get(is64Bit ? SP::LDXri : SP::LDri), Val0Reg)
2995 .addReg(AddrReg).addImm(0);
2997 // Split the basic block MBB before MI and insert the loop block in the hole.
2998 MachineFunction::iterator MFI = MBB;
2999 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
3000 MachineFunction *MF = MBB->getParent();
3001 MachineBasicBlock *LoopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3002 MachineBasicBlock *DoneMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3004 MF->insert(MFI, LoopMBB);
3005 MF->insert(MFI, DoneMBB);
3007 // Move MI and following instructions to DoneMBB.
3008 DoneMBB->splice(DoneMBB->begin(), MBB, MI, MBB->end());
3009 DoneMBB->transferSuccessorsAndUpdatePHIs(MBB);
3011 // Connect the CFG again.
3012 MBB->addSuccessor(LoopMBB);
3013 LoopMBB->addSuccessor(LoopMBB);
3014 LoopMBB->addSuccessor(DoneMBB);
3016 // Build the loop block.
3017 unsigned ValReg = MRI.createVirtualRegister(ValueRC);
3018 // Opcode == 0 means try to write Rs2Reg directly (ATOMIC_SWAP).
3019 unsigned UpdReg = (Opcode ? MRI.createVirtualRegister(ValueRC) : Rs2Reg);
3021 BuildMI(LoopMBB, DL, TII.get(SP::PHI), ValReg)
3022 .addReg(Val0Reg).addMBB(MBB)
3023 .addReg(DestReg).addMBB(LoopMBB);
3026 // This is one of the min/max operations. We need a CMPrr followed by a
3028 BuildMI(LoopMBB, DL, TII.get(SP::CMPrr)).addReg(ValReg).addReg(Rs2Reg);
3029 BuildMI(LoopMBB, DL, TII.get(Opcode), UpdReg)
3030 .addReg(ValReg).addReg(Rs2Reg).addImm(CondCode);
3031 } else if (Opcode) {
3032 BuildMI(LoopMBB, DL, TII.get(Opcode), UpdReg)
3033 .addReg(ValReg).addReg(Rs2Reg);
3036 if (MI->getOpcode() == SP::ATOMIC_LOAD_NAND_32 ||
3037 MI->getOpcode() == SP::ATOMIC_LOAD_NAND_64) {
3038 unsigned TmpReg = UpdReg;
3039 UpdReg = MRI.createVirtualRegister(ValueRC);
3040 BuildMI(LoopMBB, DL, TII.get(SP::XORri), UpdReg).addReg(TmpReg).addImm(-1);
3043 BuildMI(LoopMBB, DL, TII.get(is64Bit ? SP::CASXrr : SP::CASrr), DestReg)
3044 .addReg(AddrReg).addReg(ValReg).addReg(UpdReg)
3045 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
3046 BuildMI(LoopMBB, DL, TII.get(SP::CMPrr)).addReg(ValReg).addReg(DestReg);
3047 BuildMI(LoopMBB, DL, TII.get(is64Bit ? SP::BPXCC : SP::BCOND))
3048 .addMBB(LoopMBB).addImm(SPCC::ICC_NE);
3050 MI->eraseFromParent();
3054 //===----------------------------------------------------------------------===//
3055 // Sparc Inline Assembly Support
3056 //===----------------------------------------------------------------------===//
3058 /// getConstraintType - Given a constraint letter, return the type of
3059 /// constraint it is for this target.
3060 SparcTargetLowering::ConstraintType
3061 SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
3062 if (Constraint.size() == 1) {
3063 switch (Constraint[0]) {
3065 case 'r': return C_RegisterClass;
3071 return TargetLowering::getConstraintType(Constraint);
3074 TargetLowering::ConstraintWeight SparcTargetLowering::
3075 getSingleConstraintMatchWeight(AsmOperandInfo &info,
3076 const char *constraint) const {
3077 ConstraintWeight weight = CW_Invalid;
3078 Value *CallOperandVal = info.CallOperandVal;
3079 // If we don't have a value, we can't do a match,
3080 // but allow it at the lowest weight.
3081 if (CallOperandVal == NULL)
3084 // Look at the constraint type.
3085 switch (*constraint) {
3087 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3090 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3091 if (isInt<13>(C->getSExtValue()))
3092 weight = CW_Constant;
3099 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3100 /// vector. If it is invalid, don't add anything to Ops.
3101 void SparcTargetLowering::
3102 LowerAsmOperandForConstraint(SDValue Op,
3103 std::string &Constraint,
3104 std::vector<SDValue> &Ops,
3105 SelectionDAG &DAG) const {
3106 SDValue Result(0, 0);
3108 // Only support length 1 constraints for now.
3109 if (Constraint.length() > 1)
3112 char ConstraintLetter = Constraint[0];
3113 switch (ConstraintLetter) {
3116 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3117 if (isInt<13>(C->getSExtValue())) {
3118 Result = DAG.getTargetConstant(C->getSExtValue(), Op.getValueType());
3125 if (Result.getNode()) {
3126 Ops.push_back(Result);
3129 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3132 std::pair<unsigned, const TargetRegisterClass*>
3133 SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
3135 if (Constraint.size() == 1) {
3136 switch (Constraint[0]) {
3138 return std::make_pair(0U, &SP::IntRegsRegClass);
3140 } else if (!Constraint.empty() && Constraint.size() <= 5
3141 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3142 // constraint = '{r<d>}'
3143 // Remove the braces from around the name.
3144 StringRef name(Constraint.data()+1, Constraint.size()-2);
3145 // Handle register aliases:
3150 uint64_t intVal = 0;
3151 if (name.substr(0, 1).equals("r")
3152 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3153 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3154 char regType = regTypes[intVal/8];
3155 char regIdx = '0' + (intVal % 8);
3156 char tmp[] = { '{', regType, regIdx, '}', 0 };
3157 std::string newConstraint = std::string(tmp);
3158 return TargetLowering::getRegForInlineAsmConstraint(newConstraint, VT);
3162 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3166 SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3167 // The Sparc target isn't yet aware of offsets.
3171 void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3172 SmallVectorImpl<SDValue>& Results,
3173 SelectionDAG &DAG) const {
3177 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3179 switch (N->getOpcode()) {
3181 llvm_unreachable("Do not know how to custom type legalize this operation!");
3183 case ISD::FP_TO_SINT:
3184 case ISD::FP_TO_UINT:
3185 // Custom lower only if it involves f128 or i64.
3186 if (N->getOperand(0).getValueType() != MVT::f128
3187 || N->getValueType(0) != MVT::i64)
3189 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3190 ? RTLIB::FPTOSINT_F128_I64
3191 : RTLIB::FPTOUINT_F128_I64);
3193 Results.push_back(LowerF128Op(SDValue(N, 0),
3195 getLibcallName(libCall),
3199 case ISD::SINT_TO_FP:
3200 case ISD::UINT_TO_FP:
3201 // Custom lower only if it involves f128 or i64.
3202 if (N->getValueType(0) != MVT::f128
3203 || N->getOperand(0).getValueType() != MVT::i64)
3206 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3207 ? RTLIB::SINTTOFP_I64_F128
3208 : RTLIB::UINTTOFP_I64_F128);
3210 Results.push_back(LowerF128Op(SDValue(N, 0),
3212 getLibcallName(libCall),