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 "SparcMachineFunctionInfo.h"
17 #include "SparcRegisterInfo.h"
18 #include "SparcTargetMachine.h"
19 #include "MCTargetDesc/SparcBaseInfo.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Module.h"
30 #include "llvm/Support/ErrorHandling.h"
34 //===----------------------------------------------------------------------===//
35 // Calling Convention Implementation
36 //===----------------------------------------------------------------------===//
38 static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
39 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
40 ISD::ArgFlagsTy &ArgFlags, CCState &State)
42 assert (ArgFlags.isSRet());
44 // Assign SRet argument.
45 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
51 static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
52 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags, CCState &State)
55 static const uint16_t RegList[] = {
56 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
58 // Try to get first reg.
59 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
60 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
62 // Assign whole thing in stack.
63 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
64 State.AllocateStack(8,4),
69 // Try to get second reg.
70 if (unsigned Reg = State.AllocateReg(RegList, 6))
71 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
73 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
74 State.AllocateStack(4,4),
79 // Allocate a full-sized argument for the 64-bit ABI.
80 static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
81 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
82 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
83 assert((LocVT == MVT::f32 || LocVT.getSizeInBits() == 64) &&
84 "Can't handle non-64 bits locations");
86 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
87 unsigned Offset = State.AllocateStack(8, 8);
90 if (LocVT == MVT::i64 && Offset < 6*8)
91 // Promote integers to %i0-%i5.
92 Reg = SP::I0 + Offset/8;
93 else if (LocVT == MVT::f64 && Offset < 16*8)
94 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
95 Reg = SP::D0 + Offset/8;
96 else if (LocVT == MVT::f32 && Offset < 16*8)
97 // Promote floats to %f1, %f3, ...
98 Reg = SP::F1 + Offset/4;
100 // Promote to register when possible, otherwise use the stack slot.
102 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
106 // This argument goes on the stack in an 8-byte slot.
107 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
108 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
109 if (LocVT == MVT::f32)
112 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
116 // Allocate a half-sized argument for the 64-bit ABI.
118 // This is used when passing { float, int } structs by value in registers.
119 static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
120 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
121 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
122 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
123 unsigned Offset = State.AllocateStack(4, 4);
125 if (LocVT == MVT::f32 && Offset < 16*8) {
126 // Promote floats to %f0-%f31.
127 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
132 if (LocVT == MVT::i32 && Offset < 6*8) {
133 // Promote integers to %i0-%i5, using half the register.
134 unsigned Reg = SP::I0 + Offset/8;
136 LocInfo = CCValAssign::AExt;
138 // Set the Custom bit if this i32 goes in the high bits of a register.
140 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
143 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
147 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
151 #include "SparcGenCallingConv.inc"
153 // The calling conventions in SparcCallingConv.td are described in terms of the
154 // callee's register window. This function translates registers to the
155 // corresponding caller window %o register.
156 static unsigned toCallerWindow(unsigned Reg) {
157 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
158 if (Reg >= SP::I0 && Reg <= SP::I7)
159 return Reg - SP::I0 + SP::O0;
164 SparcTargetLowering::LowerReturn(SDValue Chain,
165 CallingConv::ID CallConv, bool IsVarArg,
166 const SmallVectorImpl<ISD::OutputArg> &Outs,
167 const SmallVectorImpl<SDValue> &OutVals,
168 SDLoc DL, SelectionDAG &DAG) const {
169 if (Subtarget->is64Bit())
170 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
171 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
175 SparcTargetLowering::LowerReturn_32(SDValue Chain,
176 CallingConv::ID CallConv, bool IsVarArg,
177 const SmallVectorImpl<ISD::OutputArg> &Outs,
178 const SmallVectorImpl<SDValue> &OutVals,
179 SDLoc DL, SelectionDAG &DAG) const {
180 MachineFunction &MF = DAG.getMachineFunction();
182 // CCValAssign - represent the assignment of the return value to locations.
183 SmallVector<CCValAssign, 16> RVLocs;
185 // CCState - Info about the registers and stack slot.
186 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
187 DAG.getTarget(), RVLocs, *DAG.getContext());
189 // Analyze return values.
190 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
193 SmallVector<SDValue, 4> RetOps(1, Chain);
194 // Make room for the return address offset.
195 RetOps.push_back(SDValue());
197 // Copy the result values into the output registers.
198 for (unsigned i = 0; i != RVLocs.size(); ++i) {
199 CCValAssign &VA = RVLocs[i];
200 assert(VA.isRegLoc() && "Can only return in registers!");
202 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
205 // Guarantee that all emitted copies are stuck together with flags.
206 Flag = Chain.getValue(1);
207 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
210 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
211 // If the function returns a struct, copy the SRetReturnReg to I0
212 if (MF.getFunction()->hasStructRetAttr()) {
213 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
214 unsigned Reg = SFI->getSRetReturnReg();
216 llvm_unreachable("sret virtual register not created in the entry block");
217 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
218 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
219 Flag = Chain.getValue(1);
220 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
221 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
224 RetOps[0] = Chain; // Update chain.
225 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
227 // Add the flag if we have it.
229 RetOps.push_back(Flag);
231 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
232 &RetOps[0], RetOps.size());
235 // Lower return values for the 64-bit ABI.
236 // Return values are passed the exactly the same way as function arguments.
238 SparcTargetLowering::LowerReturn_64(SDValue Chain,
239 CallingConv::ID CallConv, bool IsVarArg,
240 const SmallVectorImpl<ISD::OutputArg> &Outs,
241 const SmallVectorImpl<SDValue> &OutVals,
242 SDLoc DL, SelectionDAG &DAG) const {
243 // CCValAssign - represent the assignment of the return value to locations.
244 SmallVector<CCValAssign, 16> RVLocs;
246 // CCState - Info about the registers and stack slot.
247 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
248 DAG.getTarget(), RVLocs, *DAG.getContext());
250 // Analyze return values.
251 CCInfo.AnalyzeReturn(Outs, CC_Sparc64);
254 SmallVector<SDValue, 4> RetOps(1, Chain);
256 // The second operand on the return instruction is the return address offset.
257 // The return address is always %i7+8 with the 64-bit ABI.
258 RetOps.push_back(DAG.getConstant(8, MVT::i32));
260 // Copy the result values into the output registers.
261 for (unsigned i = 0; i != RVLocs.size(); ++i) {
262 CCValAssign &VA = RVLocs[i];
263 assert(VA.isRegLoc() && "Can only return in registers!");
264 SDValue OutVal = OutVals[i];
266 // Integer return values must be sign or zero extended by the callee.
267 switch (VA.getLocInfo()) {
268 case CCValAssign::SExt:
269 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
271 case CCValAssign::ZExt:
272 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
274 case CCValAssign::AExt:
275 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
280 // The custom bit on an i32 return value indicates that it should be passed
281 // in the high bits of the register.
282 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
283 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
284 DAG.getConstant(32, MVT::i32));
286 // The next value may go in the low bits of the same register.
287 // Handle both at once.
288 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
289 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
290 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
291 // Skip the next value, it's already done.
296 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
298 // Guarantee that all emitted copies are stuck together with flags.
299 Flag = Chain.getValue(1);
300 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
303 RetOps[0] = Chain; // Update chain.
305 // Add the flag if we have it.
307 RetOps.push_back(Flag);
309 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
310 &RetOps[0], RetOps.size());
313 SDValue SparcTargetLowering::
314 LowerFormalArguments(SDValue Chain,
315 CallingConv::ID CallConv,
317 const SmallVectorImpl<ISD::InputArg> &Ins,
320 SmallVectorImpl<SDValue> &InVals) const {
321 if (Subtarget->is64Bit())
322 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
324 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
328 /// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
329 /// passed in either one or two GPRs, including FP values. TODO: we should
330 /// pass FP values in FP registers for fastcc functions.
331 SDValue SparcTargetLowering::
332 LowerFormalArguments_32(SDValue Chain,
333 CallingConv::ID CallConv,
335 const SmallVectorImpl<ISD::InputArg> &Ins,
338 SmallVectorImpl<SDValue> &InVals) const {
339 MachineFunction &MF = DAG.getMachineFunction();
340 MachineRegisterInfo &RegInfo = MF.getRegInfo();
341 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
343 // Assign locations to all of the incoming arguments.
344 SmallVector<CCValAssign, 16> ArgLocs;
345 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
346 getTargetMachine(), ArgLocs, *DAG.getContext());
347 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
349 const unsigned StackOffset = 92;
351 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
352 CCValAssign &VA = ArgLocs[i];
354 if (i == 0 && Ins[i].Flags.isSRet()) {
355 // Get SRet from [%fp+64].
356 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
357 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
358 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
359 MachinePointerInfo(),
360 false, false, false, 0);
361 InVals.push_back(Arg);
366 if (VA.needsCustom()) {
367 assert(VA.getLocVT() == MVT::f64);
368 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
369 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
370 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
373 CCValAssign &NextVA = ArgLocs[++i];
376 if (NextVA.isMemLoc()) {
377 int FrameIdx = MF.getFrameInfo()->
378 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
379 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
380 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
381 MachinePointerInfo(),
382 false, false, false, 0);
384 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
385 &SP::IntRegsRegClass);
386 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
389 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
390 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
391 InVals.push_back(WholeValue);
394 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
395 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
396 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
397 if (VA.getLocVT() == MVT::f32)
398 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
399 else if (VA.getLocVT() != MVT::i32) {
400 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
401 DAG.getValueType(VA.getLocVT()));
402 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
404 InVals.push_back(Arg);
408 assert(VA.isMemLoc());
410 unsigned Offset = VA.getLocMemOffset()+StackOffset;
412 if (VA.needsCustom()) {
413 assert(VA.getValVT() == MVT::f64);
414 // If it is double-word aligned, just load.
415 if (Offset % 8 == 0) {
416 int FI = MF.getFrameInfo()->CreateFixedObject(8,
419 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
420 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
421 MachinePointerInfo(),
422 false,false, false, 0);
423 InVals.push_back(Load);
427 int FI = MF.getFrameInfo()->CreateFixedObject(4,
430 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
431 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
432 MachinePointerInfo(),
433 false, false, false, 0);
434 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
437 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
439 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
440 MachinePointerInfo(),
441 false, false, false, 0);
444 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
445 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
446 InVals.push_back(WholeValue);
450 int FI = MF.getFrameInfo()->CreateFixedObject(4,
453 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
455 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
456 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
457 MachinePointerInfo(),
458 false, false, false, 0);
460 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
461 // Sparc is big endian, so add an offset based on the ObjectVT.
462 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
463 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
464 DAG.getConstant(Offset, MVT::i32));
465 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
466 MachinePointerInfo(),
467 VA.getValVT(), false, false,0);
468 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
470 InVals.push_back(Load);
473 if (MF.getFunction()->hasStructRetAttr()) {
474 // Copy the SRet Argument to SRetReturnReg.
475 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
476 unsigned Reg = SFI->getSRetReturnReg();
478 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
479 SFI->setSRetReturnReg(Reg);
481 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
482 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
485 // Store remaining ArgRegs to the stack if this is a varargs function.
487 static const uint16_t ArgRegs[] = {
488 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
490 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
491 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
492 unsigned ArgOffset = CCInfo.getNextStackOffset();
493 if (NumAllocated == 6)
494 ArgOffset += StackOffset;
497 ArgOffset = 68+4*NumAllocated;
500 // Remember the vararg offset for the va_start implementation.
501 FuncInfo->setVarArgsFrameOffset(ArgOffset);
503 std::vector<SDValue> OutChains;
505 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
506 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
507 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
508 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
510 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
512 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
514 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
515 MachinePointerInfo(),
520 if (!OutChains.empty()) {
521 OutChains.push_back(Chain);
522 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
523 &OutChains[0], OutChains.size());
530 // Lower formal arguments for the 64 bit ABI.
531 SDValue SparcTargetLowering::
532 LowerFormalArguments_64(SDValue Chain,
533 CallingConv::ID CallConv,
535 const SmallVectorImpl<ISD::InputArg> &Ins,
538 SmallVectorImpl<SDValue> &InVals) const {
539 MachineFunction &MF = DAG.getMachineFunction();
541 // Analyze arguments according to CC_Sparc64.
542 SmallVector<CCValAssign, 16> ArgLocs;
543 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
544 getTargetMachine(), ArgLocs, *DAG.getContext());
545 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
547 // The argument array begins at %fp+BIAS+128, after the register save area.
548 const unsigned ArgArea = 128;
550 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
551 CCValAssign &VA = ArgLocs[i];
553 // This argument is passed in a register.
554 // All integer register arguments are promoted by the caller to i64.
556 // Create a virtual register for the promoted live-in value.
557 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
558 getRegClassFor(VA.getLocVT()));
559 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
561 // Get the high bits for i32 struct elements.
562 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
563 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
564 DAG.getConstant(32, MVT::i32));
566 // The caller promoted the argument, so insert an Assert?ext SDNode so we
567 // won't promote the value again in this function.
568 switch (VA.getLocInfo()) {
569 case CCValAssign::SExt:
570 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
571 DAG.getValueType(VA.getValVT()));
573 case CCValAssign::ZExt:
574 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
575 DAG.getValueType(VA.getValVT()));
581 // Truncate the register down to the argument type.
583 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
585 InVals.push_back(Arg);
589 // The registers are exhausted. This argument was passed on the stack.
590 assert(VA.isMemLoc());
591 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
592 // beginning of the arguments area at %fp+BIAS+128.
593 unsigned Offset = VA.getLocMemOffset() + ArgArea;
594 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
595 // Adjust offset for extended arguments, SPARC is big-endian.
596 // The caller will have written the full slot with extended bytes, but we
597 // prefer our own extending loads.
599 Offset += 8 - ValSize;
600 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
601 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
602 DAG.getFrameIndex(FI, getPointerTy()),
603 MachinePointerInfo::getFixedStack(FI),
604 false, false, false, 0));
610 // This function takes variable arguments, some of which may have been passed
611 // in registers %i0-%i5. Variable floating point arguments are never passed
612 // in floating point registers. They go on %i0-%i5 or on the stack like
613 // integer arguments.
615 // The va_start intrinsic needs to know the offset to the first variable
617 unsigned ArgOffset = CCInfo.getNextStackOffset();
618 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
619 // Skip the 128 bytes of register save area.
620 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
621 Subtarget->getStackPointerBias());
623 // Save the variable arguments that were passed in registers.
624 // The caller is required to reserve stack space for 6 arguments regardless
625 // of how many arguments were actually passed.
626 SmallVector<SDValue, 8> OutChains;
627 for (; ArgOffset < 6*8; ArgOffset += 8) {
628 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
629 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
630 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
631 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
632 DAG.getFrameIndex(FI, getPointerTy()),
633 MachinePointerInfo::getFixedStack(FI),
637 if (!OutChains.empty())
638 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
639 &OutChains[0], OutChains.size());
645 SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
646 SmallVectorImpl<SDValue> &InVals) const {
647 if (Subtarget->is64Bit())
648 return LowerCall_64(CLI, InVals);
649 return LowerCall_32(CLI, InVals);
652 static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
653 ImmutableCallSite *CS) {
655 return CS->hasFnAttr(Attribute::ReturnsTwice);
657 const Function *CalleeFn = 0;
658 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
659 CalleeFn = dyn_cast<Function>(G->getGlobal());
660 } else if (ExternalSymbolSDNode *E =
661 dyn_cast<ExternalSymbolSDNode>(Callee)) {
662 const Function *Fn = DAG.getMachineFunction().getFunction();
663 const Module *M = Fn->getParent();
664 const char *CalleeName = E->getSymbol();
665 CalleeFn = M->getFunction(CalleeName);
670 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
673 // Lower a call for the 32-bit ABI.
675 SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
676 SmallVectorImpl<SDValue> &InVals) const {
677 SelectionDAG &DAG = CLI.DAG;
679 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
680 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
681 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
682 SDValue Chain = CLI.Chain;
683 SDValue Callee = CLI.Callee;
684 bool &isTailCall = CLI.IsTailCall;
685 CallingConv::ID CallConv = CLI.CallConv;
686 bool isVarArg = CLI.IsVarArg;
688 // Sparc target does not yet support tail call optimization.
691 // Analyze operands of the call, assigning locations to each operand.
692 SmallVector<CCValAssign, 16> ArgLocs;
693 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
694 DAG.getTarget(), ArgLocs, *DAG.getContext());
695 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
697 // Get the size of the outgoing arguments stack space requirement.
698 unsigned ArgsSize = CCInfo.getNextStackOffset();
700 // Keep stack frames 8-byte aligned.
701 ArgsSize = (ArgsSize+7) & ~7;
703 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
705 // Create local copies for byval args.
706 SmallVector<SDValue, 8> ByValArgs;
707 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
708 ISD::ArgFlagsTy Flags = Outs[i].Flags;
709 if (!Flags.isByVal())
712 SDValue Arg = OutVals[i];
713 unsigned Size = Flags.getByValSize();
714 unsigned Align = Flags.getByValAlign();
716 int FI = MFI->CreateStackObject(Size, Align, false);
717 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
718 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
720 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
721 false, // isVolatile,
722 (Size <= 32), // AlwaysInline if size <= 32
723 MachinePointerInfo(), MachinePointerInfo());
724 ByValArgs.push_back(FIPtr);
727 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
730 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
731 SmallVector<SDValue, 8> MemOpChains;
733 const unsigned StackOffset = 92;
734 bool hasStructRetAttr = false;
735 // Walk the register/memloc assignments, inserting copies/loads.
736 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
739 CCValAssign &VA = ArgLocs[i];
740 SDValue Arg = OutVals[realArgIdx];
742 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
744 // Use local copy if it is a byval arg.
746 Arg = ByValArgs[byvalArgIdx++];
748 // Promote the value if needed.
749 switch (VA.getLocInfo()) {
750 default: llvm_unreachable("Unknown loc info!");
751 case CCValAssign::Full: break;
752 case CCValAssign::SExt:
753 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
755 case CCValAssign::ZExt:
756 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
758 case CCValAssign::AExt:
759 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
761 case CCValAssign::BCvt:
762 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
766 if (Flags.isSRet()) {
767 assert(VA.needsCustom());
768 // store SRet argument in %sp+64
769 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
770 SDValue PtrOff = DAG.getIntPtrConstant(64);
771 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
772 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
773 MachinePointerInfo(),
775 hasStructRetAttr = true;
779 if (VA.needsCustom()) {
780 assert(VA.getLocVT() == MVT::f64);
783 unsigned Offset = VA.getLocMemOffset() + StackOffset;
784 // if it is double-word aligned, just store.
785 if (Offset % 8 == 0) {
786 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
787 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
788 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
789 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
790 MachinePointerInfo(),
796 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
797 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
798 Arg, StackPtr, MachinePointerInfo(),
800 // Sparc is big-endian, so the high part comes first.
801 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
802 MachinePointerInfo(), false, false, false, 0);
803 // Increment the pointer to the other half.
804 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
805 DAG.getIntPtrConstant(4));
806 // Load the low part.
807 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
808 MachinePointerInfo(), false, false, false, 0);
811 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
813 CCValAssign &NextVA = ArgLocs[++i];
814 if (NextVA.isRegLoc()) {
815 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
817 // Store the low part in stack.
818 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
819 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
820 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
821 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
822 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
823 MachinePointerInfo(),
827 unsigned Offset = VA.getLocMemOffset() + StackOffset;
828 // Store the high part.
829 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
830 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
831 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
832 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
833 MachinePointerInfo(),
835 // Store the low part.
836 PtrOff = DAG.getIntPtrConstant(Offset+4);
837 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
838 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
839 MachinePointerInfo(),
845 // Arguments that can be passed on register must be kept at
848 if (VA.getLocVT() != MVT::f32) {
849 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
852 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
853 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
857 assert(VA.isMemLoc());
859 // Create a store off the stack pointer for this argument.
860 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
861 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
862 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
863 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
864 MachinePointerInfo(),
869 // Emit all stores, make sure the occur before any copies into physregs.
870 if (!MemOpChains.empty())
871 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
872 &MemOpChains[0], MemOpChains.size());
874 // Build a sequence of copy-to-reg nodes chained together with token
875 // chain and flag operands which copy the outgoing args into registers.
876 // The InFlag in necessary since all emitted instructions must be
879 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
880 unsigned Reg = toCallerWindow(RegsToPass[i].first);
881 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
882 InFlag = Chain.getValue(1);
885 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
886 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
888 // If the callee is a GlobalAddress node (quite common, every direct call is)
889 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
890 // Likewise ExternalSymbol -> TargetExternalSymbol.
891 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
892 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
893 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
894 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
896 // Returns a chain & a flag for retval copy to use
897 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
898 SmallVector<SDValue, 8> Ops;
899 Ops.push_back(Chain);
900 Ops.push_back(Callee);
901 if (hasStructRetAttr)
902 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
903 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
904 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
905 RegsToPass[i].second.getValueType()));
907 // Add a register mask operand representing the call-preserved registers.
908 const SparcRegisterInfo *TRI =
909 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
910 const uint32_t *Mask = ((hasReturnsTwice)
911 ? TRI->getRTCallPreservedMask(CallConv)
912 : TRI->getCallPreservedMask(CallConv));
913 assert(Mask && "Missing call preserved mask for calling convention");
914 Ops.push_back(DAG.getRegisterMask(Mask));
916 if (InFlag.getNode())
917 Ops.push_back(InFlag);
919 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
920 InFlag = Chain.getValue(1);
922 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
923 DAG.getIntPtrConstant(0, true), InFlag, dl);
924 InFlag = Chain.getValue(1);
926 // Assign locations to each value returned by this call.
927 SmallVector<CCValAssign, 16> RVLocs;
928 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
929 DAG.getTarget(), RVLocs, *DAG.getContext());
931 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
933 // Copy all of the result registers out of their specified physreg.
934 for (unsigned i = 0; i != RVLocs.size(); ++i) {
935 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
936 RVLocs[i].getValVT(), InFlag).getValue(1);
937 InFlag = Chain.getValue(2);
938 InVals.push_back(Chain.getValue(0));
944 // This functions returns true if CalleeName is a ABI function that returns
945 // a long double (fp128).
946 static bool isFP128ABICall(const char *CalleeName)
948 static const char *const ABICalls[] =
949 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
951 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
954 for (const char * const *I = ABICalls; I != 0; ++I)
955 if (strcmp(CalleeName, *I) == 0)
961 SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
963 const Function *CalleeFn = 0;
964 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
965 CalleeFn = dyn_cast<Function>(G->getGlobal());
966 } else if (ExternalSymbolSDNode *E =
967 dyn_cast<ExternalSymbolSDNode>(Callee)) {
968 const Function *Fn = DAG.getMachineFunction().getFunction();
969 const Module *M = Fn->getParent();
970 const char *CalleeName = E->getSymbol();
971 CalleeFn = M->getFunction(CalleeName);
972 if (!CalleeFn && isFP128ABICall(CalleeName))
973 return 16; // Return sizeof(fp128)
979 assert(CalleeFn->hasStructRetAttr() &&
980 "Callee does not have the StructRet attribute.");
982 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
983 Type *ElementTy = Ty->getElementType();
984 return getDataLayout()->getTypeAllocSize(ElementTy);
988 // Fixup floating point arguments in the ... part of a varargs call.
990 // The SPARC v9 ABI requires that floating point arguments are treated the same
991 // as integers when calling a varargs function. This does not apply to the
992 // fixed arguments that are part of the function's prototype.
994 // This function post-processes a CCValAssign array created by
995 // AnalyzeCallOperands().
996 static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
997 ArrayRef<ISD::OutputArg> Outs) {
998 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
999 const CCValAssign &VA = ArgLocs[i];
1000 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1001 // varargs functions.
1002 if (!VA.isRegLoc() || VA.getLocVT() != MVT::f64)
1004 // The fixed arguments to a varargs function still go in FP registers.
1005 if (Outs[VA.getValNo()].IsFixed)
1008 // This floating point argument should be reassigned.
1011 // Determine the offset into the argument array.
1012 unsigned Offset = 8 * (VA.getLocReg() - SP::D0);
1013 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1016 // This argument should go in %i0-%i5.
1017 unsigned IReg = SP::I0 + Offset/8;
1018 // Full register, just bitconvert into i64.
1019 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1020 IReg, MVT::i64, CCValAssign::BCvt);
1022 // This needs to go to memory, we're out of integer registers.
1023 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1024 Offset, VA.getLocVT(), VA.getLocInfo());
1030 // Lower a call for the 64-bit ABI.
1032 SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1033 SmallVectorImpl<SDValue> &InVals) const {
1034 SelectionDAG &DAG = CLI.DAG;
1036 SDValue Chain = CLI.Chain;
1038 // Analyze operands of the call, assigning locations to each operand.
1039 SmallVector<CCValAssign, 16> ArgLocs;
1040 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1041 DAG.getTarget(), ArgLocs, *DAG.getContext());
1042 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1044 // Get the size of the outgoing arguments stack space requirement.
1045 // The stack offset computed by CC_Sparc64 includes all arguments.
1046 // Called functions expect 6 argument words to exist in the stack frame, used
1048 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
1050 // Keep stack frames 16-byte aligned.
1051 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1053 // Varargs calls require special treatment.
1055 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1057 // Adjust the stack pointer to make room for the arguments.
1058 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1059 // with more than 6 arguments.
1060 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1063 // Collect the set of registers to pass to the function and their values.
1064 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1066 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1068 // Collect chains from all the memory opeations that copy arguments to the
1069 // stack. They must follow the stack pointer adjustment above and precede the
1070 // call instruction itself.
1071 SmallVector<SDValue, 8> MemOpChains;
1073 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1074 const CCValAssign &VA = ArgLocs[i];
1075 SDValue Arg = CLI.OutVals[i];
1077 // Promote the value if needed.
1078 switch (VA.getLocInfo()) {
1080 llvm_unreachable("Unknown location info!");
1081 case CCValAssign::Full:
1083 case CCValAssign::SExt:
1084 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1086 case CCValAssign::ZExt:
1087 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1089 case CCValAssign::AExt:
1090 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1092 case CCValAssign::BCvt:
1093 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1097 if (VA.isRegLoc()) {
1098 // The custom bit on an i32 return value indicates that it should be
1099 // passed in the high bits of the register.
1100 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1101 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1102 DAG.getConstant(32, MVT::i32));
1104 // The next value may go in the low bits of the same register.
1105 // Handle both at once.
1106 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1107 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1108 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1110 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1111 // Skip the next value, it's already done.
1115 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
1119 assert(VA.isMemLoc());
1121 // Create a store off the stack pointer for this argument.
1122 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1123 // The argument area starts at %fp+BIAS+128 in the callee frame,
1124 // %sp+BIAS+128 in ours.
1125 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1126 Subtarget->getStackPointerBias() +
1128 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1129 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1130 MachinePointerInfo(),
1134 // Emit all stores, make sure they occur before the call.
1135 if (!MemOpChains.empty())
1136 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1137 &MemOpChains[0], MemOpChains.size());
1139 // Build a sequence of CopyToReg nodes glued together with token chain and
1140 // glue operands which copy the outgoing args into registers. The InGlue is
1141 // necessary since all emitted instructions must be stuck together in order
1142 // to pass the live physical registers.
1144 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1145 Chain = DAG.getCopyToReg(Chain, DL,
1146 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1147 InGlue = Chain.getValue(1);
1150 // If the callee is a GlobalAddress node (quite common, every direct call is)
1151 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1152 // Likewise ExternalSymbol -> TargetExternalSymbol.
1153 SDValue Callee = CLI.Callee;
1154 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
1155 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1156 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1157 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1158 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1160 // Build the operands for the call instruction itself.
1161 SmallVector<SDValue, 8> Ops;
1162 Ops.push_back(Chain);
1163 Ops.push_back(Callee);
1164 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1165 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1166 RegsToPass[i].second.getValueType()));
1168 // Add a register mask operand representing the call-preserved registers.
1169 const SparcRegisterInfo *TRI =
1170 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1171 const uint32_t *Mask = ((hasReturnsTwice)
1172 ? TRI->getRTCallPreservedMask(CLI.CallConv)
1173 : TRI->getCallPreservedMask(CLI.CallConv));
1174 assert(Mask && "Missing call preserved mask for calling convention");
1175 Ops.push_back(DAG.getRegisterMask(Mask));
1177 // Make sure the CopyToReg nodes are glued to the call instruction which
1178 // consumes the registers.
1179 if (InGlue.getNode())
1180 Ops.push_back(InGlue);
1182 // Now the call itself.
1183 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1184 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1185 InGlue = Chain.getValue(1);
1187 // Revert the stack pointer immediately after the call.
1188 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1189 DAG.getIntPtrConstant(0, true), InGlue, DL);
1190 InGlue = Chain.getValue(1);
1192 // Now extract the return values. This is more or less the same as
1193 // LowerFormalArguments_64.
1195 // Assign locations to each value returned by this call.
1196 SmallVector<CCValAssign, 16> RVLocs;
1197 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1198 DAG.getTarget(), RVLocs, *DAG.getContext());
1199 RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64);
1201 // Copy all of the result registers out of their specified physreg.
1202 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1203 CCValAssign &VA = RVLocs[i];
1204 unsigned Reg = toCallerWindow(VA.getLocReg());
1206 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1207 // reside in the same register in the high and low bits. Reuse the
1208 // CopyFromReg previous node to avoid duplicate copies.
1210 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1211 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1212 RV = Chain.getValue(0);
1214 // But usually we'll create a new CopyFromReg for a different register.
1215 if (!RV.getNode()) {
1216 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1217 Chain = RV.getValue(1);
1218 InGlue = Chain.getValue(2);
1221 // Get the high bits for i32 struct elements.
1222 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1223 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1224 DAG.getConstant(32, MVT::i32));
1226 // The callee promoted the return value, so insert an Assert?ext SDNode so
1227 // we won't promote the value again in this function.
1228 switch (VA.getLocInfo()) {
1229 case CCValAssign::SExt:
1230 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1231 DAG.getValueType(VA.getValVT()));
1233 case CCValAssign::ZExt:
1234 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1235 DAG.getValueType(VA.getValVT()));
1241 // Truncate the register down to the return value type.
1242 if (VA.isExtInLoc())
1243 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1245 InVals.push_back(RV);
1251 //===----------------------------------------------------------------------===//
1252 // TargetLowering Implementation
1253 //===----------------------------------------------------------------------===//
1255 /// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1257 static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1259 default: llvm_unreachable("Unknown integer condition code!");
1260 case ISD::SETEQ: return SPCC::ICC_E;
1261 case ISD::SETNE: return SPCC::ICC_NE;
1262 case ISD::SETLT: return SPCC::ICC_L;
1263 case ISD::SETGT: return SPCC::ICC_G;
1264 case ISD::SETLE: return SPCC::ICC_LE;
1265 case ISD::SETGE: return SPCC::ICC_GE;
1266 case ISD::SETULT: return SPCC::ICC_CS;
1267 case ISD::SETULE: return SPCC::ICC_LEU;
1268 case ISD::SETUGT: return SPCC::ICC_GU;
1269 case ISD::SETUGE: return SPCC::ICC_CC;
1273 /// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1275 static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1277 default: llvm_unreachable("Unknown fp condition code!");
1279 case ISD::SETOEQ: return SPCC::FCC_E;
1281 case ISD::SETUNE: return SPCC::FCC_NE;
1283 case ISD::SETOLT: return SPCC::FCC_L;
1285 case ISD::SETOGT: return SPCC::FCC_G;
1287 case ISD::SETOLE: return SPCC::FCC_LE;
1289 case ISD::SETOGE: return SPCC::FCC_GE;
1290 case ISD::SETULT: return SPCC::FCC_UL;
1291 case ISD::SETULE: return SPCC::FCC_ULE;
1292 case ISD::SETUGT: return SPCC::FCC_UG;
1293 case ISD::SETUGE: return SPCC::FCC_UGE;
1294 case ISD::SETUO: return SPCC::FCC_U;
1295 case ISD::SETO: return SPCC::FCC_O;
1296 case ISD::SETONE: return SPCC::FCC_LG;
1297 case ISD::SETUEQ: return SPCC::FCC_UE;
1301 SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
1302 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
1303 Subtarget = &TM.getSubtarget<SparcSubtarget>();
1305 // Set up the register classes.
1306 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1307 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1308 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1309 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1310 if (Subtarget->is64Bit())
1311 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
1313 // Turn FP extload into load/fextend
1314 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1315 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1317 // Sparc doesn't have i1 sign extending load
1318 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
1320 // Turn FP truncstore into trunc + store.
1321 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1322 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1323 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
1325 // Custom legalize GlobalAddress nodes into LO/HI parts.
1326 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1327 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1328 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
1329 setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
1331 // Sparc doesn't have sext_inreg, replace them with shl/sra
1332 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1333 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1334 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1336 // Sparc has no REM or DIVREM operations.
1337 setOperationAction(ISD::UREM, MVT::i32, Expand);
1338 setOperationAction(ISD::SREM, MVT::i32, Expand);
1339 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1340 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1342 // Custom expand fp<->sint
1343 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1344 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
1347 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
1348 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
1350 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1351 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
1353 // Sparc has no select or setcc: expand to SELECT_CC.
1354 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1355 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1356 setOperationAction(ISD::SELECT, MVT::f64, Expand);
1357 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1359 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1360 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1361 setOperationAction(ISD::SETCC, MVT::f64, Expand);
1362 setOperationAction(ISD::SETCC, MVT::f128, Expand);
1364 // Sparc doesn't have BRCOND either, it has BR_CC.
1365 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1366 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1367 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1368 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1369 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1370 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1371 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
1373 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1374 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1375 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
1376 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
1378 if (Subtarget->is64Bit()) {
1379 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1380 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1381 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1382 setOperationAction(ISD::SUBE, MVT::i64, Custom);
1383 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1384 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
1385 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1386 setOperationAction(ISD::SETCC, MVT::i64, Expand);
1387 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
1388 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
1391 // FIXME: There are instructions available for ATOMIC_FENCE
1392 // on SparcV8 and later.
1393 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
1395 if (!Subtarget->isV9()) {
1396 // SparcV8 does not have FNEGD and FABSD.
1397 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1398 setOperationAction(ISD::FABS, MVT::f64, Custom);
1401 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1402 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1403 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1404 setOperationAction(ISD::FREM , MVT::f128, Expand);
1405 setOperationAction(ISD::FMA , MVT::f128, Expand);
1406 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1407 setOperationAction(ISD::FCOS , MVT::f64, Expand);
1408 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1409 setOperationAction(ISD::FREM , MVT::f64, Expand);
1410 setOperationAction(ISD::FMA , MVT::f64, Expand);
1411 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1412 setOperationAction(ISD::FCOS , MVT::f32, Expand);
1413 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
1414 setOperationAction(ISD::FREM , MVT::f32, Expand);
1415 setOperationAction(ISD::FMA , MVT::f32, Expand);
1416 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1417 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1418 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1419 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1420 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1421 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1422 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1423 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1424 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
1425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1427 setOperationAction(ISD::FPOW , MVT::f128, Expand);
1428 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1429 setOperationAction(ISD::FPOW , MVT::f32, Expand);
1431 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1432 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1433 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1435 // FIXME: Sparc provides these multiplies, but we don't have them yet.
1436 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1437 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1439 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1440 setOperationAction(ISD::VASTART , MVT::Other, Custom);
1441 // VAARG needs to be lowered to not do unaligned accesses for doubles.
1442 setOperationAction(ISD::VAARG , MVT::Other, Custom);
1444 // Use the default implementation.
1445 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1446 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1447 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1448 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1449 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
1451 setExceptionPointerRegister(SP::I0);
1452 setExceptionSelectorRegister(SP::I1);
1454 setStackPointerRegisterToSaveRestore(SP::O6);
1456 if (Subtarget->isV9())
1457 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
1459 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1460 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1461 setOperationAction(ISD::STORE, MVT::f128, Legal);
1463 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1464 setOperationAction(ISD::STORE, MVT::f128, Custom);
1467 if (Subtarget->hasHardQuad()) {
1468 setOperationAction(ISD::FADD, MVT::f128, Legal);
1469 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1470 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1471 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1472 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1473 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1474 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1475 if (Subtarget->isV9()) {
1476 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1477 setOperationAction(ISD::FABS, MVT::f128, Legal);
1479 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1480 setOperationAction(ISD::FABS, MVT::f128, Custom);
1483 // Custom legalize f128 operations.
1485 setOperationAction(ISD::FADD, MVT::f128, Custom);
1486 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1487 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1488 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1489 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1490 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1491 setOperationAction(ISD::FABS, MVT::f128, Custom);
1493 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1494 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1495 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1497 // Setup Runtime library names.
1498 if (Subtarget->is64Bit()) {
1499 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1500 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1501 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1502 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1503 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1504 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
1505 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
1506 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1507 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1508 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1509 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1511 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1512 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1513 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1514 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1515 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1516 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
1517 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
1518 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1519 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1520 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1521 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1525 setMinFunctionAlignment(2);
1527 computeRegisterProperties();
1530 const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1533 case SPISD::CMPICC: return "SPISD::CMPICC";
1534 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1535 case SPISD::BRICC: return "SPISD::BRICC";
1536 case SPISD::BRXCC: return "SPISD::BRXCC";
1537 case SPISD::BRFCC: return "SPISD::BRFCC";
1538 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1539 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1540 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1541 case SPISD::Hi: return "SPISD::Hi";
1542 case SPISD::Lo: return "SPISD::Lo";
1543 case SPISD::FTOI: return "SPISD::FTOI";
1544 case SPISD::ITOF: return "SPISD::ITOF";
1545 case SPISD::CALL: return "SPISD::CALL";
1546 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
1547 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
1548 case SPISD::FLUSHW: return "SPISD::FLUSHW";
1549 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1550 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1551 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
1555 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1556 /// be zero. Op is expected to be a target specific node. Used by DAG
1558 void SparcTargetLowering::computeMaskedBitsForTargetNode
1562 const SelectionDAG &DAG,
1563 unsigned Depth) const {
1564 APInt KnownZero2, KnownOne2;
1565 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
1567 switch (Op.getOpcode()) {
1569 case SPISD::SELECT_ICC:
1570 case SPISD::SELECT_XCC:
1571 case SPISD::SELECT_FCC:
1572 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1573 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
1574 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1575 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1577 // Only known if known in both the LHS and RHS.
1578 KnownOne &= KnownOne2;
1579 KnownZero &= KnownZero2;
1584 // Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1585 // set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1586 static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1587 ISD::CondCode CC, unsigned &SPCC) {
1588 if (isa<ConstantSDNode>(RHS) &&
1589 cast<ConstantSDNode>(RHS)->isNullValue() &&
1591 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1592 LHS.getOpcode() == SPISD::SELECT_XCC) &&
1593 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1594 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1595 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1596 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1597 isa<ConstantSDNode>(LHS.getOperand(1)) &&
1598 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1599 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
1600 SDValue CMPCC = LHS.getOperand(3);
1601 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1602 LHS = CMPCC.getOperand(0);
1603 RHS = CMPCC.getOperand(1);
1607 // Convert to a target node and set target flags.
1608 SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1609 SelectionDAG &DAG) const {
1610 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1611 return DAG.getTargetGlobalAddress(GA->getGlobal(),
1613 GA->getValueType(0),
1614 GA->getOffset(), TF);
1616 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1617 return DAG.getTargetConstantPool(CP->getConstVal(),
1618 CP->getValueType(0),
1620 CP->getOffset(), TF);
1622 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1623 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1628 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1629 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1630 ES->getValueType(0), TF);
1632 llvm_unreachable("Unhandled address SDNode");
1635 // Split Op into high and low parts according to HiTF and LoTF.
1636 // Return an ADD node combining the parts.
1637 SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1638 unsigned HiTF, unsigned LoTF,
1639 SelectionDAG &DAG) const {
1641 EVT VT = Op.getValueType();
1642 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1643 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1644 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1647 // Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1648 // or ExternalSymbol SDNode.
1649 SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
1651 EVT VT = getPointerTy();
1653 // Handle PIC mode first.
1654 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1655 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1656 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1657 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1658 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
1659 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1660 // function has calls.
1661 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1662 MFI->setHasCalls(true);
1663 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1664 MachinePointerInfo::getGOT(), false, false, false, 0);
1667 // This is one of the absolute code models.
1668 switch(getTargetMachine().getCodeModel()) {
1670 llvm_unreachable("Unsupported absolute code model");
1671 case CodeModel::JITDefault:
1672 case CodeModel::Small:
1674 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1675 case CodeModel::Medium: {
1677 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
1678 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
1679 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1680 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1681 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1683 case CodeModel::Large: {
1685 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
1686 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
1687 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1688 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1693 SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
1694 SelectionDAG &DAG) const {
1695 return makeAddress(Op, DAG);
1698 SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
1699 SelectionDAG &DAG) const {
1700 return makeAddress(Op, DAG);
1703 SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1704 SelectionDAG &DAG) const {
1705 return makeAddress(Op, DAG);
1708 SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1709 SelectionDAG &DAG) const {
1711 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1713 const GlobalValue *GV = GA->getGlobal();
1714 EVT PtrVT = getPointerTy();
1716 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1718 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1719 unsigned HiTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_HI22
1720 : SPII::MO_TLS_LDM_HI22);
1721 unsigned LoTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_LO10
1722 : SPII::MO_TLS_LDM_LO10);
1723 unsigned addTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_ADD
1724 : SPII::MO_TLS_LDM_ADD);
1725 unsigned callTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_CALL
1726 : SPII::MO_TLS_LDM_CALL);
1728 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1729 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1730 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1731 withTargetFlags(Op, addTF, DAG));
1733 SDValue Chain = DAG.getEntryNode();
1736 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1737 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1738 InFlag = Chain.getValue(1);
1739 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1740 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1742 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1743 SmallVector<SDValue, 4> Ops;
1744 Ops.push_back(Chain);
1745 Ops.push_back(Callee);
1746 Ops.push_back(Symbol);
1747 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1748 const uint32_t *Mask = getTargetMachine()
1749 .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1750 assert(Mask && "Missing call preserved mask for calling convention");
1751 Ops.push_back(DAG.getRegisterMask(Mask));
1752 Ops.push_back(InFlag);
1753 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1754 InFlag = Chain.getValue(1);
1755 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1756 DAG.getIntPtrConstant(0, true), InFlag, DL);
1757 InFlag = Chain.getValue(1);
1758 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1760 if (model != TLSModel::LocalDynamic)
1763 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1764 withTargetFlags(Op, SPII::MO_TLS_LDO_HIX22, DAG));
1765 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1766 withTargetFlags(Op, SPII::MO_TLS_LDO_LOX10, DAG));
1767 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1768 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
1769 withTargetFlags(Op, SPII::MO_TLS_LDO_ADD, DAG));
1772 if (model == TLSModel::InitialExec) {
1773 unsigned ldTF = ((PtrVT == MVT::i64)? SPII::MO_TLS_IE_LDX
1774 : SPII::MO_TLS_IE_LD);
1776 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1778 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1779 // function has calls.
1780 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1781 MFI->setHasCalls(true);
1783 SDValue TGA = makeHiLoPair(Op,
1784 SPII::MO_TLS_IE_HI22, SPII::MO_TLS_IE_LO10, DAG);
1785 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1786 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1788 withTargetFlags(Op, ldTF, DAG));
1789 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1790 DAG.getRegister(SP::G7, PtrVT), Offset,
1791 withTargetFlags(Op, SPII::MO_TLS_IE_ADD, DAG));
1794 assert(model == TLSModel::LocalExec);
1795 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1796 withTargetFlags(Op, SPII::MO_TLS_LE_HIX22, DAG));
1797 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1798 withTargetFlags(Op, SPII::MO_TLS_LE_LOX10, DAG));
1799 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1801 return DAG.getNode(ISD::ADD, DL, PtrVT,
1802 DAG.getRegister(SP::G7, PtrVT), Offset);
1806 SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1807 SDValue Arg, SDLoc DL,
1808 SelectionDAG &DAG) const {
1809 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1810 EVT ArgVT = Arg.getValueType();
1811 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1817 if (ArgTy->isFP128Ty()) {
1818 // Create a stack object and pass the pointer to the library function.
1819 int FI = MFI->CreateStackObject(16, 8, false);
1820 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1821 Chain = DAG.getStore(Chain,
1825 MachinePointerInfo(),
1831 Entry.Ty = PointerType::getUnqual(ArgTy);
1833 Args.push_back(Entry);
1838 SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
1839 const char *LibFuncName,
1840 unsigned numArgs) const {
1844 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1846 SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
1847 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
1848 Type *RetTyABI = RetTy;
1849 SDValue Chain = DAG.getEntryNode();
1852 if (RetTy->isFP128Ty()) {
1853 // Create a Stack Object to receive the return value of type f128.
1855 int RetFI = MFI->CreateStackObject(16, 8, false);
1856 RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
1857 Entry.Node = RetPtr;
1858 Entry.Ty = PointerType::getUnqual(RetTy);
1859 if (!Subtarget->is64Bit())
1860 Entry.isSRet = true;
1861 Entry.isReturned = false;
1862 Args.push_back(Entry);
1863 RetTyABI = Type::getVoidTy(*DAG.getContext());
1866 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
1867 for (unsigned i = 0, e = numArgs; i != e; ++i) {
1868 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
1871 CallLoweringInfo CLI(Chain,
1873 false, false, false, false,
1876 Callee, Args, DAG, SDLoc(Op));
1877 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1879 // chain is in second result.
1880 if (RetTyABI == RetTy)
1881 return CallInfo.first;
1883 assert (RetTy->isFP128Ty() && "Unexpected return type!");
1885 Chain = CallInfo.second;
1887 // Load RetPtr to get the return value.
1888 return DAG.getLoad(Op.getValueType(),
1892 MachinePointerInfo(),
1893 false, false, false, 8);
1897 SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
1900 SelectionDAG &DAG) const {
1902 const char *LibCall = 0;
1903 bool is64Bit = Subtarget->is64Bit();
1905 default: llvm_unreachable("Unhandled conditional code!");
1906 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
1907 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
1908 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
1909 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
1910 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
1911 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
1919 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
1922 SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
1923 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
1925 SDValue Chain = DAG.getEntryNode();
1926 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
1927 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
1930 CallLoweringInfo CLI(Chain,
1932 false, false, false, false,
1935 Callee, Args, DAG, DL);
1937 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1939 // result is in first, and chain is in second result.
1940 SDValue Result = CallInfo.first;
1944 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1945 SPCC = SPCC::ICC_NE;
1946 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1948 case SPCC::FCC_UL : {
1949 SDValue Mask = DAG.getTargetConstant(1, Result.getValueType());
1950 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
1951 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1952 SPCC = SPCC::ICC_NE;
1953 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1955 case SPCC::FCC_ULE: {
1956 SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
1957 SPCC = SPCC::ICC_NE;
1958 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1960 case SPCC::FCC_UG : {
1961 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
1963 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1965 case SPCC::FCC_UGE: {
1966 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
1967 SPCC = SPCC::ICC_NE;
1968 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1971 case SPCC::FCC_U : {
1972 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
1974 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1976 case SPCC::FCC_O : {
1977 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
1978 SPCC = SPCC::ICC_NE;
1979 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1981 case SPCC::FCC_LG : {
1982 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
1983 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
1984 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1985 SPCC = SPCC::ICC_NE;
1986 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1988 case SPCC::FCC_UE : {
1989 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
1990 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
1991 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1993 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1999 LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2000 const SparcTargetLowering &TLI) {
2002 if (Op.getOperand(0).getValueType() == MVT::f64)
2003 return TLI.LowerF128Op(Op, DAG,
2004 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2006 if (Op.getOperand(0).getValueType() == MVT::f32)
2007 return TLI.LowerF128Op(Op, DAG,
2008 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2010 llvm_unreachable("fpextend with non-float operand!");
2011 return SDValue(0, 0);
2015 LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2016 const SparcTargetLowering &TLI) {
2017 // FP_ROUND on f64 and f32 are legal.
2018 if (Op.getOperand(0).getValueType() != MVT::f128)
2021 if (Op.getValueType() == MVT::f64)
2022 return TLI.LowerF128Op(Op, DAG,
2023 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2024 if (Op.getValueType() == MVT::f32)
2025 return TLI.LowerF128Op(Op, DAG,
2026 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2028 llvm_unreachable("fpround to non-float!");
2029 return SDValue(0, 0);
2032 static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2033 const SparcTargetLowering &TLI,
2036 // Convert the fp value to integer in an FP register.
2037 assert(Op.getValueType() == MVT::i32);
2039 if (Op.getOperand(0).getValueType() == MVT::f128 && !hasHardQuad)
2040 return TLI.LowerF128Op(Op, DAG,
2041 TLI.getLibcallName(RTLIB::FPTOSINT_F128_I32), 1);
2043 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2044 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
2047 static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2048 const SparcTargetLowering &TLI,
2051 assert(Op.getOperand(0).getValueType() == MVT::i32);
2052 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
2053 // Convert the int value to FP in an FP register.
2054 if (Op.getValueType() == MVT::f128 && !hasHardQuad)
2055 return TLI.LowerF128Op(Op, DAG,
2056 TLI.getLibcallName(RTLIB::SINTTOFP_I32_F128), 1);
2057 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
2060 static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2061 const SparcTargetLowering &TLI,
2063 SDValue Chain = Op.getOperand(0);
2064 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2065 SDValue LHS = Op.getOperand(2);
2066 SDValue RHS = Op.getOperand(3);
2067 SDValue Dest = Op.getOperand(4);
2069 unsigned Opc, SPCC = ~0U;
2071 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2072 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2073 LookThroughSetCC(LHS, RHS, CC, SPCC);
2075 // Get the condition flag.
2076 SDValue CompareFlag;
2077 if (LHS.getValueType().isInteger()) {
2078 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2079 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2080 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2081 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
2083 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2084 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2085 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2088 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2089 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2093 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2094 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2097 static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2098 const SparcTargetLowering &TLI,
2100 SDValue LHS = Op.getOperand(0);
2101 SDValue RHS = Op.getOperand(1);
2102 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2103 SDValue TrueVal = Op.getOperand(2);
2104 SDValue FalseVal = Op.getOperand(3);
2106 unsigned Opc, SPCC = ~0U;
2108 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2109 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2110 LookThroughSetCC(LHS, RHS, CC, SPCC);
2112 SDValue CompareFlag;
2113 if (LHS.getValueType().isInteger()) {
2114 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2115 Opc = LHS.getValueType() == MVT::i32 ?
2116 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
2117 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2119 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2120 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2121 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2122 Opc = SPISD::SELECT_ICC;
2124 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2125 Opc = SPISD::SELECT_FCC;
2126 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2129 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
2130 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2133 static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
2134 const SparcTargetLowering &TLI) {
2135 MachineFunction &MF = DAG.getMachineFunction();
2136 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2138 // Need frame address to find the address of VarArgsFrameIndex.
2139 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2141 // vastart just stores the address of the VarArgsFrameIndex slot into the
2142 // memory location argument.
2145 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2146 DAG.getRegister(SP::I6, TLI.getPointerTy()),
2147 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
2148 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2149 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
2150 MachinePointerInfo(SV), false, false, 0);
2153 static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
2154 SDNode *Node = Op.getNode();
2155 EVT VT = Node->getValueType(0);
2156 SDValue InChain = Node->getOperand(0);
2157 SDValue VAListPtr = Node->getOperand(1);
2158 EVT PtrVT = VAListPtr.getValueType();
2159 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2161 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
2162 MachinePointerInfo(SV), false, false, false, 0);
2163 // Increment the pointer, VAList, to the next vaarg.
2164 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2165 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2166 // Store the incremented VAList to the legalized pointer.
2167 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
2168 VAListPtr, MachinePointerInfo(SV), false, false, 0);
2169 // Load the actual argument out of the pointer VAList.
2170 // We can't count on greater alignment than the word size.
2171 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2172 false, false, false,
2173 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
2176 static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
2177 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2178 SDValue Size = Op.getOperand(1); // Legalize the size.
2181 unsigned SPReg = SP::O6;
2182 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
2183 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
2184 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
2186 // The resultant pointer is actually 16 words from the bottom of the stack,
2187 // to provide a register spill area.
2188 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
2189 DAG.getConstant(96, MVT::i32));
2190 SDValue Ops[2] = { NewVal, Chain };
2191 return DAG.getMergeValues(Ops, 2, dl);
2195 static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
2197 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
2198 dl, MVT::Other, DAG.getEntryNode());
2202 static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
2203 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2204 MFI->setFrameAddressIsTaken(true);
2206 EVT VT = Op.getValueType();
2208 unsigned FrameReg = SP::I6;
2210 uint64_t depth = Op.getConstantOperandVal(0);
2214 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2216 // flush first to make sure the windowed registers' values are in stack
2217 SDValue Chain = getFLUSHW(Op, DAG);
2218 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2220 for (uint64_t i = 0; i != depth; ++i) {
2221 SDValue Ptr = DAG.getNode(ISD::ADD,
2223 FrameAddr, DAG.getIntPtrConstant(56));
2224 FrameAddr = DAG.getLoad(MVT::i32, dl,
2227 MachinePointerInfo(), false, false, false, 0);
2233 static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2234 const SparcTargetLowering &TLI) {
2235 MachineFunction &MF = DAG.getMachineFunction();
2236 MachineFrameInfo *MFI = MF.getFrameInfo();
2237 MFI->setReturnAddressIsTaken(true);
2239 EVT VT = Op.getValueType();
2241 uint64_t depth = Op.getConstantOperandVal(0);
2245 unsigned RetReg = MF.addLiveIn(SP::I7,
2246 TLI.getRegClassFor(TLI.getPointerTy()));
2247 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
2249 // Need frame address to find return address of the caller.
2250 MFI->setFrameAddressIsTaken(true);
2252 // flush first to make sure the windowed registers' values are in stack
2253 SDValue Chain = getFLUSHW(Op, DAG);
2254 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
2256 for (uint64_t i = 0; i != depth; ++i) {
2257 SDValue Ptr = DAG.getNode(ISD::ADD,
2260 DAG.getIntPtrConstant((i == depth-1)?60:56));
2261 RetAddr = DAG.getLoad(MVT::i32, dl,
2264 MachinePointerInfo(), false, false, false, 0);
2270 static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
2274 assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
2275 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
2277 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2278 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2279 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2281 SDValue SrcReg64 = Op.getOperand(0);
2282 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2284 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2287 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
2289 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2291 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2293 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2298 // Lower a f128 load into two f64 loads.
2299 static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2302 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2303 assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2304 && "Unexpected node type");
2306 unsigned alignment = LdNode->getAlignment();
2310 SDValue Hi64 = DAG.getLoad(MVT::f64,
2313 LdNode->getBasePtr(),
2314 LdNode->getPointerInfo(),
2315 false, false, false, alignment);
2316 EVT addrVT = LdNode->getBasePtr().getValueType();
2317 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2318 LdNode->getBasePtr(),
2319 DAG.getConstant(8, addrVT));
2320 SDValue Lo64 = DAG.getLoad(MVT::f64,
2324 LdNode->getPointerInfo(),
2325 false, false, false, alignment);
2327 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2328 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2330 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2332 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2334 SDValue(InFP128, 0),
2337 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2339 SDValue(InFP128, 0),
2342 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2343 SDValue(Lo64.getNode(), 1) };
2344 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2346 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2347 return DAG.getMergeValues(Ops, 2, dl);
2350 // Lower a f128 store into two f64 stores.
2351 static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2353 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2354 assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2355 && "Unexpected node type");
2356 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2357 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2359 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2364 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2370 unsigned alignment = StNode->getAlignment();
2374 SDValue OutChains[2];
2375 OutChains[0] = DAG.getStore(StNode->getChain(),
2378 StNode->getBasePtr(),
2379 MachinePointerInfo(),
2380 false, false, alignment);
2381 EVT addrVT = StNode->getBasePtr().getValueType();
2382 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2383 StNode->getBasePtr(),
2384 DAG.getConstant(8, addrVT));
2385 OutChains[1] = DAG.getStore(StNode->getChain(),
2389 MachinePointerInfo(),
2390 false, false, alignment);
2391 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2395 static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2396 const SparcTargetLowering &TLI,
2398 if (Op.getValueType() == MVT::f64)
2399 return LowerF64Op(Op, DAG, ISD::FNEG);
2400 if (Op.getValueType() == MVT::f128)
2401 return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2405 static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2406 if (Op.getValueType() == MVT::f64)
2407 return LowerF64Op(Op, DAG, ISD::FABS);
2408 if (Op.getValueType() != MVT::f128)
2411 // Lower fabs on f128 to fabs on f64
2412 // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2415 SDValue SrcReg128 = Op.getOperand(0);
2416 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2418 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2421 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2423 Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
2425 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2427 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2429 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2434 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
2436 if (Op.getValueType() != MVT::i64)
2440 SDValue Src1 = Op.getOperand(0);
2441 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2442 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2443 DAG.getConstant(32, MVT::i64));
2444 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2446 SDValue Src2 = Op.getOperand(1);
2447 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2448 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2449 DAG.getConstant(32, MVT::i64));
2450 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2453 bool hasChain = false;
2454 unsigned hiOpc = Op.getOpcode();
2455 switch (Op.getOpcode()) {
2456 default: llvm_unreachable("Invalid opcode");
2457 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2458 case ISD::ADDE: hasChain = true; break;
2459 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2460 case ISD::SUBE: hasChain = true; break;
2463 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2465 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2468 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2470 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2471 SDValue Carry = Hi.getValue(1);
2473 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2474 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2475 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2476 DAG.getConstant(32, MVT::i64));
2478 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2479 SDValue Ops[2] = { Dst, Carry };
2480 return DAG.getMergeValues(Ops, 2, dl);
2483 SDValue SparcTargetLowering::
2484 LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2486 bool hasHardQuad = Subtarget->hasHardQuad();
2487 bool is64Bit = Subtarget->is64Bit();
2488 bool isV9 = Subtarget->isV9();
2490 switch (Op.getOpcode()) {
2491 default: llvm_unreachable("Should not custom lower this!");
2493 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this);
2494 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
2495 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
2496 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
2497 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
2498 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
2499 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2501 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2503 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
2505 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
2507 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
2508 case ISD::VAARG: return LowerVAARG(Op, DAG);
2509 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2511 case ISD::LOAD: return LowerF128Load(Op, DAG);
2512 case ISD::STORE: return LowerF128Store(Op, DAG);
2513 case ISD::FADD: return LowerF128Op(Op, DAG,
2514 getLibcallName(RTLIB::ADD_F128), 2);
2515 case ISD::FSUB: return LowerF128Op(Op, DAG,
2516 getLibcallName(RTLIB::SUB_F128), 2);
2517 case ISD::FMUL: return LowerF128Op(Op, DAG,
2518 getLibcallName(RTLIB::MUL_F128), 2);
2519 case ISD::FDIV: return LowerF128Op(Op, DAG,
2520 getLibcallName(RTLIB::DIV_F128), 2);
2521 case ISD::FSQRT: return LowerF128Op(Op, DAG,
2522 getLibcallName(RTLIB::SQRT_F128),1);
2523 case ISD::FNEG: return LowerFNEG(Op, DAG, *this, is64Bit);
2524 case ISD::FABS: return LowerFABS(Op, DAG, isV9);
2525 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
2526 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
2530 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
2535 SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2536 MachineBasicBlock *BB) const {
2537 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2540 DebugLoc dl = MI->getDebugLoc();
2541 // Figure out the conditional branch opcode to use for this select_cc.
2542 switch (MI->getOpcode()) {
2543 default: llvm_unreachable("Unknown SELECT_CC!");
2544 case SP::SELECT_CC_Int_ICC:
2545 case SP::SELECT_CC_FP_ICC:
2546 case SP::SELECT_CC_DFP_ICC:
2547 case SP::SELECT_CC_QFP_ICC:
2548 BROpcode = SP::BCOND;
2550 case SP::SELECT_CC_Int_FCC:
2551 case SP::SELECT_CC_FP_FCC:
2552 case SP::SELECT_CC_DFP_FCC:
2553 case SP::SELECT_CC_QFP_FCC:
2554 BROpcode = SP::FBCOND;
2558 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
2560 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2561 // control-flow pattern. The incoming instruction knows the destination vreg
2562 // to set, the condition code register to branch on, the true/false values to
2563 // select between, and a branch opcode to use.
2564 const BasicBlock *LLVM_BB = BB->getBasicBlock();
2565 MachineFunction::iterator It = BB;
2572 // fallthrough --> copy0MBB
2573 MachineBasicBlock *thisMBB = BB;
2574 MachineFunction *F = BB->getParent();
2575 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2576 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
2577 F->insert(It, copy0MBB);
2578 F->insert(It, sinkMBB);
2580 // Transfer the remainder of BB and its successor edges to sinkMBB.
2581 sinkMBB->splice(sinkMBB->begin(), BB,
2582 llvm::next(MachineBasicBlock::iterator(MI)),
2584 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2586 // Add the true and fallthrough blocks as its successors.
2587 BB->addSuccessor(copy0MBB);
2588 BB->addSuccessor(sinkMBB);
2590 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
2593 // %FalseValue = ...
2594 // # fallthrough to sinkMBB
2597 // Update machine-CFG edges
2598 BB->addSuccessor(sinkMBB);
2601 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2604 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
2605 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2606 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
2608 MI->eraseFromParent(); // The pseudo instruction is gone now.
2612 //===----------------------------------------------------------------------===//
2613 // Sparc Inline Assembly Support
2614 //===----------------------------------------------------------------------===//
2616 /// getConstraintType - Given a constraint letter, return the type of
2617 /// constraint it is for this target.
2618 SparcTargetLowering::ConstraintType
2619 SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
2620 if (Constraint.size() == 1) {
2621 switch (Constraint[0]) {
2623 case 'r': return C_RegisterClass;
2627 return TargetLowering::getConstraintType(Constraint);
2630 std::pair<unsigned, const TargetRegisterClass*>
2631 SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
2633 if (Constraint.size() == 1) {
2634 switch (Constraint[0]) {
2636 return std::make_pair(0U, &SP::IntRegsRegClass);
2640 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2644 SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2645 // The Sparc target isn't yet aware of offsets.