SparcISelLowering.cpp revision 3e94418e857d5e17b5d16dbc5abc8b5a8b4efac6
1//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
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"
31using namespace llvm;
32
33
34//===----------------------------------------------------------------------===//
35// Calling Convention Implementation
36//===----------------------------------------------------------------------===//
37
38static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
39                                 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
40                                 ISD::ArgFlagsTy &ArgFlags, CCState &State)
41{
42  assert (ArgFlags.isSRet());
43
44  // Assign SRet argument.
45  State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
46                                         0,
47                                         LocVT, LocInfo));
48  return true;
49}
50
51static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
52                                MVT &LocVT, CCValAssign::LocInfo &LocInfo,
53                                ISD::ArgFlagsTy &ArgFlags, CCState &State)
54{
55  static const uint16_t RegList[] = {
56    SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
57  };
58  // Try to get first reg.
59  if (unsigned Reg = State.AllocateReg(RegList, 6)) {
60    State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
61  } else {
62    // Assign whole thing in stack.
63    State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
64                                           State.AllocateStack(8,4),
65                                           LocVT, LocInfo));
66    return true;
67  }
68
69  // Try to get second reg.
70  if (unsigned Reg = State.AllocateReg(RegList, 6))
71    State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
72  else
73    State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
74                                           State.AllocateStack(4,4),
75                                           LocVT, LocInfo));
76  return true;
77}
78
79// Allocate a full-sized argument for the 64-bit ABI.
80static 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");
85
86  // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
87  unsigned Offset = State.AllocateStack(8, 8);
88  unsigned Reg = 0;
89
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;
99
100  // Promote to register when possible, otherwise use the stack slot.
101  if (Reg) {
102    State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
103    return true;
104  }
105
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)
110    Offset += 4;
111
112  State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
113  return true;
114}
115
116// Allocate a half-sized argument for the 64-bit ABI.
117//
118// This is used when passing { float, int } structs by value in registers.
119static 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);
124
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,
128                                     LocVT, LocInfo));
129    return true;
130  }
131
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;
135    LocVT = MVT::i64;
136    LocInfo = CCValAssign::AExt;
137
138    // Set the Custom bit if this i32 goes in the high bits of a register.
139    if (Offset % 8 == 0)
140      State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
141                                             LocVT, LocInfo));
142    else
143      State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
144    return true;
145  }
146
147  State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
148  return true;
149}
150
151#include "SparcGenCallingConv.inc"
152
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.
156static 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;
160  return Reg;
161}
162
163SDValue
164SparcTargetLowering::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);
172}
173
174SDValue
175SparcTargetLowering::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();
181
182  // CCValAssign - represent the assignment of the return value to locations.
183  SmallVector<CCValAssign, 16> RVLocs;
184
185  // CCState - Info about the registers and stack slot.
186  CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
187                 DAG.getTarget(), RVLocs, *DAG.getContext());
188
189  // Analyze return values.
190  CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
191
192  SDValue Flag;
193  SmallVector<SDValue, 4> RetOps(1, Chain);
194  // Make room for the return address offset.
195  RetOps.push_back(SDValue());
196
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!");
201
202    Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
203                             OutVals[i], Flag);
204
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()));
208  }
209
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();
215    if (!Reg)
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
222  }
223
224  RetOps[0] = Chain;  // Update chain.
225  RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
226
227  // Add the flag if we have it.
228  if (Flag.getNode())
229    RetOps.push_back(Flag);
230
231  return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
232                     &RetOps[0], RetOps.size());
233}
234
235// Lower return values for the 64-bit ABI.
236// Return values are passed the exactly the same way as function arguments.
237SDValue
238SparcTargetLowering::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;
245
246  // CCState - Info about the registers and stack slot.
247  CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
248                 DAG.getTarget(), RVLocs, *DAG.getContext());
249
250  // Analyze return values.
251  CCInfo.AnalyzeReturn(Outs, CC_Sparc64);
252
253  SDValue Flag;
254  SmallVector<SDValue, 4> RetOps(1, Chain);
255
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));
259
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];
265
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);
270      break;
271    case CCValAssign::ZExt:
272      OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
273      break;
274    case CCValAssign::AExt:
275      OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
276    default:
277      break;
278    }
279
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));
285
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.
292        ++i;
293      }
294    }
295
296    Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
297
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()));
301  }
302
303  RetOps[0] = Chain;  // Update chain.
304
305  // Add the flag if we have it.
306  if (Flag.getNode())
307    RetOps.push_back(Flag);
308
309  return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
310                     &RetOps[0], RetOps.size());
311}
312
313SDValue SparcTargetLowering::
314LowerFormalArguments(SDValue Chain,
315                     CallingConv::ID CallConv,
316                     bool IsVarArg,
317                     const SmallVectorImpl<ISD::InputArg> &Ins,
318                     SDLoc DL,
319                     SelectionDAG &DAG,
320                     SmallVectorImpl<SDValue> &InVals) const {
321  if (Subtarget->is64Bit())
322    return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
323                                   DL, DAG, InVals);
324  return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
325                                 DL, DAG, InVals);
326}
327
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.
331SDValue SparcTargetLowering::
332LowerFormalArguments_32(SDValue Chain,
333                        CallingConv::ID CallConv,
334                        bool isVarArg,
335                        const SmallVectorImpl<ISD::InputArg> &Ins,
336                        SDLoc dl,
337                        SelectionDAG &DAG,
338                        SmallVectorImpl<SDValue> &InVals) const {
339  MachineFunction &MF = DAG.getMachineFunction();
340  MachineRegisterInfo &RegInfo = MF.getRegInfo();
341  SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
342
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);
348
349  const unsigned StackOffset = 92;
350
351  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
352    CCValAssign &VA = ArgLocs[i];
353
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);
362      continue;
363    }
364
365    if (VA.isRegLoc()) {
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);
371
372        assert(i+1 < e);
373        CCValAssign &NextVA = ArgLocs[++i];
374
375        SDValue LoVal;
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);
383        } else {
384          unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
385                                        &SP::IntRegsRegClass);
386          LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
387        }
388        SDValue WholeValue =
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);
392        continue;
393      }
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);
403      }
404      InVals.push_back(Arg);
405      continue;
406    }
407
408    assert(VA.isMemLoc());
409
410    unsigned Offset = VA.getLocMemOffset()+StackOffset;
411
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,
417                                                      Offset,
418                                                      true);
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);
424        continue;
425      }
426
427      int FI = MF.getFrameInfo()->CreateFixedObject(4,
428                                                    Offset,
429                                                    true);
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,
435                                                     Offset+4,
436                                                     true);
437      SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
438
439      SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
440                                  MachinePointerInfo(),
441                                  false, false, false, 0);
442
443      SDValue WholeValue =
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);
447      continue;
448    }
449
450    int FI = MF.getFrameInfo()->CreateFixedObject(4,
451                                                  Offset,
452                                                  true);
453    SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
454    SDValue Load ;
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);
459    } else {
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);
469    }
470    InVals.push_back(Load);
471  }
472
473  if (MF.getFunction()->hasStructRetAttr()) {
474    // Copy the SRet Argument to SRetReturnReg.
475    SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
476    unsigned Reg = SFI->getSRetReturnReg();
477    if (!Reg) {
478      Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
479      SFI->setSRetReturnReg(Reg);
480    }
481    SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
482    Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
483  }
484
485  // Store remaining ArgRegs to the stack if this is a varargs function.
486  if (isVarArg) {
487    static const uint16_t ArgRegs[] = {
488      SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
489    };
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;
495    else {
496      assert(!ArgOffset);
497      ArgOffset = 68+4*NumAllocated;
498    }
499
500    // Remember the vararg offset for the va_start implementation.
501    FuncInfo->setVarArgsFrameOffset(ArgOffset);
502
503    std::vector<SDValue> OutChains;
504
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);
509
510      int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
511                                                          true);
512      SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
513
514      OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
515                                       MachinePointerInfo(),
516                                       false, false, 0));
517      ArgOffset += 4;
518    }
519
520    if (!OutChains.empty()) {
521      OutChains.push_back(Chain);
522      Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
523                          &OutChains[0], OutChains.size());
524    }
525  }
526
527  return Chain;
528}
529
530// Lower formal arguments for the 64 bit ABI.
531SDValue SparcTargetLowering::
532LowerFormalArguments_64(SDValue Chain,
533                        CallingConv::ID CallConv,
534                        bool IsVarArg,
535                        const SmallVectorImpl<ISD::InputArg> &Ins,
536                        SDLoc DL,
537                        SelectionDAG &DAG,
538                        SmallVectorImpl<SDValue> &InVals) const {
539  MachineFunction &MF = DAG.getMachineFunction();
540
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);
546
547  // The argument array begins at %fp+BIAS+128, after the register save area.
548  const unsigned ArgArea = 128;
549
550  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
551    CCValAssign &VA = ArgLocs[i];
552    if (VA.isRegLoc()) {
553      // This argument is passed in a register.
554      // All integer register arguments are promoted by the caller to i64.
555
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());
560
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));
565
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()));
572        break;
573      case CCValAssign::ZExt:
574        Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
575                          DAG.getValueType(VA.getValVT()));
576        break;
577      default:
578        break;
579      }
580
581      // Truncate the register down to the argument type.
582      if (VA.isExtInLoc())
583        Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
584
585      InVals.push_back(Arg);
586      continue;
587    }
588
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.
598    if (VA.isExtInLoc())
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));
605  }
606
607  if (!IsVarArg)
608    return Chain;
609
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.
614  //
615  // The va_start intrinsic needs to know the offset to the first variable
616  // argument.
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());
622
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),
634                                     false, false, 0));
635  }
636
637  if (!OutChains.empty())
638    Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
639                        &OutChains[0], OutChains.size());
640
641  return Chain;
642}
643
644SDValue
645SparcTargetLowering::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);
650}
651
652static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
653                                     ImmutableCallSite *CS) {
654  if (CS)
655    return CS->hasFnAttr(Attribute::ReturnsTwice);
656
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);
666  }
667
668  if (!CalleeFn)
669    return false;
670  return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
671}
672
673// Lower a call for the 32-bit ABI.
674SDValue
675SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
676                                  SmallVectorImpl<SDValue> &InVals) const {
677  SelectionDAG &DAG                     = CLI.DAG;
678  SDLoc &dl                             = CLI.DL;
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;
687
688  // Sparc target does not yet support tail call optimization.
689  isTailCall = false;
690
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);
696
697  // Get the size of the outgoing arguments stack space requirement.
698  unsigned ArgsSize = CCInfo.getNextStackOffset();
699
700  // Keep stack frames 8-byte aligned.
701  ArgsSize = (ArgsSize+7) & ~7;
702
703  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
704
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())
710      continue;
711
712    SDValue Arg = OutVals[i];
713    unsigned Size = Flags.getByValSize();
714    unsigned Align = Flags.getByValAlign();
715
716    int FI = MFI->CreateStackObject(Size, Align, false);
717    SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
718    SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
719
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);
725  }
726
727  Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
728                               dl);
729
730  SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
731  SmallVector<SDValue, 8> MemOpChains;
732
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();
737       i != e;
738       ++i, ++realArgIdx) {
739    CCValAssign &VA = ArgLocs[i];
740    SDValue Arg = OutVals[realArgIdx];
741
742    ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
743
744    // Use local copy if it is a byval arg.
745    if (Flags.isByVal())
746      Arg = ByValArgs[byvalArgIdx++];
747
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);
754      break;
755    case CCValAssign::ZExt:
756      Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
757      break;
758    case CCValAssign::AExt:
759      Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
760      break;
761    case CCValAssign::BCvt:
762      Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
763      break;
764    }
765
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(),
774                                         false, false, 0));
775      hasStructRetAttr = true;
776      continue;
777    }
778
779    if (VA.needsCustom()) {
780      assert(VA.getLocVT() == MVT::f64);
781
782      if (VA.isMemLoc()) {
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(),
791                                             false, false, 0));
792          continue;
793        }
794      }
795
796      SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
797      SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
798                                   Arg, StackPtr, MachinePointerInfo(),
799                                   false, false, 0);
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);
809
810      if (VA.isRegLoc()) {
811        RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
812        assert(i+1 != e);
813        CCValAssign &NextVA = ArgLocs[++i];
814        if (NextVA.isRegLoc()) {
815          RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
816        } else {
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(),
824                                             false, false, 0));
825        }
826      } else {
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(),
834                                           false, false, 0));
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(),
840                                           false, false, 0));
841      }
842      continue;
843    }
844
845    // Arguments that can be passed on register must be kept at
846    // RegsToPass vector
847    if (VA.isRegLoc()) {
848      if (VA.getLocVT() != MVT::f32) {
849        RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
850        continue;
851      }
852      Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
853      RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
854      continue;
855    }
856
857    assert(VA.isMemLoc());
858
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(),
865                                       false, false, 0));
866  }
867
868
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());
873
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
877  // stuck together.
878  SDValue InFlag;
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);
883  }
884
885  unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
886  bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
887
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);
895
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()));
906
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));
915
916  if (InFlag.getNode())
917    Ops.push_back(InFlag);
918
919  Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
920  InFlag = Chain.getValue(1);
921
922  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
923                             DAG.getIntPtrConstant(0, true), InFlag, dl);
924  InFlag = Chain.getValue(1);
925
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());
930
931  RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
932
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));
939  }
940
941  return Chain;
942}
943
944// This functions returns true if CalleeName is a ABI function that returns
945// a long double (fp128).
946static bool isFP128ABICall(const char *CalleeName)
947{
948  static const char *const ABICalls[] =
949    {  "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
950       "_Q_sqrt", "_Q_neg",
951       "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
952       "_Q_lltoq", "_Q_ulltoq",
953       0
954    };
955  for (const char * const *I = ABICalls; *I != 0; ++I)
956    if (strcmp(CalleeName, *I) == 0)
957      return true;
958  return false;
959}
960
961unsigned
962SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
963{
964  const Function *CalleeFn = 0;
965  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
966    CalleeFn = dyn_cast<Function>(G->getGlobal());
967  } else if (ExternalSymbolSDNode *E =
968             dyn_cast<ExternalSymbolSDNode>(Callee)) {
969    const Function *Fn = DAG.getMachineFunction().getFunction();
970    const Module *M = Fn->getParent();
971    const char *CalleeName = E->getSymbol();
972    CalleeFn = M->getFunction(CalleeName);
973    if (!CalleeFn && isFP128ABICall(CalleeName))
974      return 16; // Return sizeof(fp128)
975  }
976
977  if (!CalleeFn)
978    return 0;
979
980  assert(CalleeFn->hasStructRetAttr() &&
981         "Callee does not have the StructRet attribute.");
982
983  PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
984  Type *ElementTy = Ty->getElementType();
985  return getDataLayout()->getTypeAllocSize(ElementTy);
986}
987
988
989// Fixup floating point arguments in the ... part of a varargs call.
990//
991// The SPARC v9 ABI requires that floating point arguments are treated the same
992// as integers when calling a varargs function. This does not apply to the
993// fixed arguments that are part of the function's prototype.
994//
995// This function post-processes a CCValAssign array created by
996// AnalyzeCallOperands().
997static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
998                                   ArrayRef<ISD::OutputArg> Outs) {
999  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1000    const CCValAssign &VA = ArgLocs[i];
1001    // FIXME: What about f32 arguments? C promotes them to f64 when calling
1002    // varargs functions.
1003    if (!VA.isRegLoc() || VA.getLocVT() != MVT::f64)
1004      continue;
1005    // The fixed arguments to a varargs function still go in FP registers.
1006    if (Outs[VA.getValNo()].IsFixed)
1007      continue;
1008
1009    // This floating point argument should be reassigned.
1010    CCValAssign NewVA;
1011
1012    // Determine the offset into the argument array.
1013    unsigned Offset = 8 * (VA.getLocReg() - SP::D0);
1014    assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1015
1016    if (Offset < 6*8) {
1017      // This argument should go in %i0-%i5.
1018      unsigned IReg = SP::I0 + Offset/8;
1019      // Full register, just bitconvert into i64.
1020      NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1021                                  IReg, MVT::i64, CCValAssign::BCvt);
1022    } else {
1023      // This needs to go to memory, we're out of integer registers.
1024      NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1025                                  Offset, VA.getLocVT(), VA.getLocInfo());
1026    }
1027    ArgLocs[i] = NewVA;
1028  }
1029}
1030
1031// Lower a call for the 64-bit ABI.
1032SDValue
1033SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1034                                  SmallVectorImpl<SDValue> &InVals) const {
1035  SelectionDAG &DAG = CLI.DAG;
1036  SDLoc DL = CLI.DL;
1037  SDValue Chain = CLI.Chain;
1038
1039  // Sparc target does not yet support tail call optimization.
1040  CLI.IsTailCall = false;
1041
1042  // Analyze operands of the call, assigning locations to each operand.
1043  SmallVector<CCValAssign, 16> ArgLocs;
1044  CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1045                 DAG.getTarget(), ArgLocs, *DAG.getContext());
1046  CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1047
1048  // Get the size of the outgoing arguments stack space requirement.
1049  // The stack offset computed by CC_Sparc64 includes all arguments.
1050  // Called functions expect 6 argument words to exist in the stack frame, used
1051  // or not.
1052  unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
1053
1054  // Keep stack frames 16-byte aligned.
1055  ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1056
1057  // Varargs calls require special treatment.
1058  if (CLI.IsVarArg)
1059    fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1060
1061  // Adjust the stack pointer to make room for the arguments.
1062  // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1063  // with more than 6 arguments.
1064  Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1065                               DL);
1066
1067  // Collect the set of registers to pass to the function and their values.
1068  // This will be emitted as a sequence of CopyToReg nodes glued to the call
1069  // instruction.
1070  SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1071
1072  // Collect chains from all the memory opeations that copy arguments to the
1073  // stack. They must follow the stack pointer adjustment above and precede the
1074  // call instruction itself.
1075  SmallVector<SDValue, 8> MemOpChains;
1076
1077  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1078    const CCValAssign &VA = ArgLocs[i];
1079    SDValue Arg = CLI.OutVals[i];
1080
1081    // Promote the value if needed.
1082    switch (VA.getLocInfo()) {
1083    default:
1084      llvm_unreachable("Unknown location info!");
1085    case CCValAssign::Full:
1086      break;
1087    case CCValAssign::SExt:
1088      Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1089      break;
1090    case CCValAssign::ZExt:
1091      Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1092      break;
1093    case CCValAssign::AExt:
1094      Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1095      break;
1096    case CCValAssign::BCvt:
1097      Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
1098      break;
1099    }
1100
1101    if (VA.isRegLoc()) {
1102      // The custom bit on an i32 return value indicates that it should be
1103      // passed in the high bits of the register.
1104      if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1105        Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1106                          DAG.getConstant(32, MVT::i32));
1107
1108        // The next value may go in the low bits of the same register.
1109        // Handle both at once.
1110        if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1111            ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1112          SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1113                                   CLI.OutVals[i+1]);
1114          Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1115          // Skip the next value, it's already done.
1116          ++i;
1117        }
1118      }
1119      RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
1120      continue;
1121    }
1122
1123    assert(VA.isMemLoc());
1124
1125    // Create a store off the stack pointer for this argument.
1126    SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1127    // The argument area starts at %fp+BIAS+128 in the callee frame,
1128    // %sp+BIAS+128 in ours.
1129    SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1130                                           Subtarget->getStackPointerBias() +
1131                                           128);
1132    PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1133    MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1134                                       MachinePointerInfo(),
1135                                       false, false, 0));
1136  }
1137
1138  // Emit all stores, make sure they occur before the call.
1139  if (!MemOpChains.empty())
1140    Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1141                        &MemOpChains[0], MemOpChains.size());
1142
1143  // Build a sequence of CopyToReg nodes glued together with token chain and
1144  // glue operands which copy the outgoing args into registers. The InGlue is
1145  // necessary since all emitted instructions must be stuck together in order
1146  // to pass the live physical registers.
1147  SDValue InGlue;
1148  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1149    Chain = DAG.getCopyToReg(Chain, DL,
1150                             RegsToPass[i].first, RegsToPass[i].second, InGlue);
1151    InGlue = Chain.getValue(1);
1152  }
1153
1154  // If the callee is a GlobalAddress node (quite common, every direct call is)
1155  // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1156  // Likewise ExternalSymbol -> TargetExternalSymbol.
1157  SDValue Callee = CLI.Callee;
1158  bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
1159  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1160    Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1161  else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1162    Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1163
1164  // Build the operands for the call instruction itself.
1165  SmallVector<SDValue, 8> Ops;
1166  Ops.push_back(Chain);
1167  Ops.push_back(Callee);
1168  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1169    Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1170                                  RegsToPass[i].second.getValueType()));
1171
1172  // Add a register mask operand representing the call-preserved registers.
1173  const SparcRegisterInfo *TRI =
1174    ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1175  const uint32_t *Mask = ((hasReturnsTwice)
1176                          ? TRI->getRTCallPreservedMask(CLI.CallConv)
1177                          : TRI->getCallPreservedMask(CLI.CallConv));
1178  assert(Mask && "Missing call preserved mask for calling convention");
1179  Ops.push_back(DAG.getRegisterMask(Mask));
1180
1181  // Make sure the CopyToReg nodes are glued to the call instruction which
1182  // consumes the registers.
1183  if (InGlue.getNode())
1184    Ops.push_back(InGlue);
1185
1186  // Now the call itself.
1187  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1188  Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1189  InGlue = Chain.getValue(1);
1190
1191  // Revert the stack pointer immediately after the call.
1192  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1193                             DAG.getIntPtrConstant(0, true), InGlue, DL);
1194  InGlue = Chain.getValue(1);
1195
1196  // Now extract the return values. This is more or less the same as
1197  // LowerFormalArguments_64.
1198
1199  // Assign locations to each value returned by this call.
1200  SmallVector<CCValAssign, 16> RVLocs;
1201  CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1202                 DAG.getTarget(), RVLocs, *DAG.getContext());
1203  RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64);
1204
1205  // Copy all of the result registers out of their specified physreg.
1206  for (unsigned i = 0; i != RVLocs.size(); ++i) {
1207    CCValAssign &VA = RVLocs[i];
1208    unsigned Reg = toCallerWindow(VA.getLocReg());
1209
1210    // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1211    // reside in the same register in the high and low bits. Reuse the
1212    // CopyFromReg previous node to avoid duplicate copies.
1213    SDValue RV;
1214    if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1215      if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1216        RV = Chain.getValue(0);
1217
1218    // But usually we'll create a new CopyFromReg for a different register.
1219    if (!RV.getNode()) {
1220      RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1221      Chain = RV.getValue(1);
1222      InGlue = Chain.getValue(2);
1223    }
1224
1225    // Get the high bits for i32 struct elements.
1226    if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1227      RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1228                       DAG.getConstant(32, MVT::i32));
1229
1230    // The callee promoted the return value, so insert an Assert?ext SDNode so
1231    // we won't promote the value again in this function.
1232    switch (VA.getLocInfo()) {
1233    case CCValAssign::SExt:
1234      RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1235                       DAG.getValueType(VA.getValVT()));
1236      break;
1237    case CCValAssign::ZExt:
1238      RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1239                       DAG.getValueType(VA.getValVT()));
1240      break;
1241    default:
1242      break;
1243    }
1244
1245    // Truncate the register down to the return value type.
1246    if (VA.isExtInLoc())
1247      RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1248
1249    InVals.push_back(RV);
1250  }
1251
1252  return Chain;
1253}
1254
1255//===----------------------------------------------------------------------===//
1256// TargetLowering Implementation
1257//===----------------------------------------------------------------------===//
1258
1259/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1260/// condition.
1261static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1262  switch (CC) {
1263  default: llvm_unreachable("Unknown integer condition code!");
1264  case ISD::SETEQ:  return SPCC::ICC_E;
1265  case ISD::SETNE:  return SPCC::ICC_NE;
1266  case ISD::SETLT:  return SPCC::ICC_L;
1267  case ISD::SETGT:  return SPCC::ICC_G;
1268  case ISD::SETLE:  return SPCC::ICC_LE;
1269  case ISD::SETGE:  return SPCC::ICC_GE;
1270  case ISD::SETULT: return SPCC::ICC_CS;
1271  case ISD::SETULE: return SPCC::ICC_LEU;
1272  case ISD::SETUGT: return SPCC::ICC_GU;
1273  case ISD::SETUGE: return SPCC::ICC_CC;
1274  }
1275}
1276
1277/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1278/// FCC condition.
1279static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1280  switch (CC) {
1281  default: llvm_unreachable("Unknown fp condition code!");
1282  case ISD::SETEQ:
1283  case ISD::SETOEQ: return SPCC::FCC_E;
1284  case ISD::SETNE:
1285  case ISD::SETUNE: return SPCC::FCC_NE;
1286  case ISD::SETLT:
1287  case ISD::SETOLT: return SPCC::FCC_L;
1288  case ISD::SETGT:
1289  case ISD::SETOGT: return SPCC::FCC_G;
1290  case ISD::SETLE:
1291  case ISD::SETOLE: return SPCC::FCC_LE;
1292  case ISD::SETGE:
1293  case ISD::SETOGE: return SPCC::FCC_GE;
1294  case ISD::SETULT: return SPCC::FCC_UL;
1295  case ISD::SETULE: return SPCC::FCC_ULE;
1296  case ISD::SETUGT: return SPCC::FCC_UG;
1297  case ISD::SETUGE: return SPCC::FCC_UGE;
1298  case ISD::SETUO:  return SPCC::FCC_U;
1299  case ISD::SETO:   return SPCC::FCC_O;
1300  case ISD::SETONE: return SPCC::FCC_LG;
1301  case ISD::SETUEQ: return SPCC::FCC_UE;
1302  }
1303}
1304
1305SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
1306  : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
1307  Subtarget = &TM.getSubtarget<SparcSubtarget>();
1308
1309  // Set up the register classes.
1310  addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1311  addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1312  addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
1313  addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
1314  if (Subtarget->is64Bit())
1315    addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
1316
1317  // Turn FP extload into load/fextend
1318  setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
1319  setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1320
1321  // Sparc doesn't have i1 sign extending load
1322  setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
1323
1324  // Turn FP truncstore into trunc + store.
1325  setTruncStoreAction(MVT::f64, MVT::f32, Expand);
1326  setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1327  setTruncStoreAction(MVT::f128, MVT::f64, Expand);
1328
1329  // Custom legalize GlobalAddress nodes into LO/HI parts.
1330  setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1331  setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1332  setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
1333  setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
1334
1335  // Sparc doesn't have sext_inreg, replace them with shl/sra
1336  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1337  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1338  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
1339
1340  // Sparc has no REM or DIVREM operations.
1341  setOperationAction(ISD::UREM, MVT::i32, Expand);
1342  setOperationAction(ISD::SREM, MVT::i32, Expand);
1343  setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1344  setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
1345
1346  // ... nor does SparcV9.
1347  if (Subtarget->is64Bit()) {
1348    setOperationAction(ISD::UREM, MVT::i64, Expand);
1349    setOperationAction(ISD::SREM, MVT::i64, Expand);
1350    setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1351    setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1352  }
1353
1354  // Custom expand fp<->sint
1355  setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1356  setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
1357  setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1358  setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
1359
1360  // Custom Expand fp<->uint
1361  setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1362  setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
1363  setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1364  setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
1365
1366  setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1367  setOperationAction(ISD::BITCAST, MVT::i32, Expand);
1368
1369  // Sparc has no select or setcc: expand to SELECT_CC.
1370  setOperationAction(ISD::SELECT, MVT::i32, Expand);
1371  setOperationAction(ISD::SELECT, MVT::f32, Expand);
1372  setOperationAction(ISD::SELECT, MVT::f64, Expand);
1373  setOperationAction(ISD::SELECT, MVT::f128, Expand);
1374
1375  setOperationAction(ISD::SETCC, MVT::i32, Expand);
1376  setOperationAction(ISD::SETCC, MVT::f32, Expand);
1377  setOperationAction(ISD::SETCC, MVT::f64, Expand);
1378  setOperationAction(ISD::SETCC, MVT::f128, Expand);
1379
1380  // Sparc doesn't have BRCOND either, it has BR_CC.
1381  setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1382  setOperationAction(ISD::BRIND, MVT::Other, Expand);
1383  setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1384  setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1385  setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1386  setOperationAction(ISD::BR_CC, MVT::f64, Custom);
1387  setOperationAction(ISD::BR_CC, MVT::f128, Custom);
1388
1389  setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1390  setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1391  setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
1392  setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
1393
1394  if (Subtarget->is64Bit()) {
1395    setOperationAction(ISD::ADDC, MVT::i64, Custom);
1396    setOperationAction(ISD::ADDE, MVT::i64, Custom);
1397    setOperationAction(ISD::SUBC, MVT::i64, Custom);
1398    setOperationAction(ISD::SUBE, MVT::i64, Custom);
1399    setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1400    setOperationAction(ISD::BITCAST, MVT::i64, Expand);
1401    setOperationAction(ISD::SELECT, MVT::i64, Expand);
1402    setOperationAction(ISD::SETCC, MVT::i64, Expand);
1403    setOperationAction(ISD::BR_CC, MVT::i64, Custom);
1404    setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
1405
1406    setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1407    setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1408    setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1409    setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1410    setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1411    setOperationAction(ISD::BSWAP, MVT::i64, Expand);
1412    setOperationAction(ISD::ROTL , MVT::i64, Expand);
1413    setOperationAction(ISD::ROTR , MVT::i64, Expand);
1414  }
1415
1416  // FIXME: There are instructions available for ATOMIC_FENCE
1417  // on SparcV8 and later.
1418  setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
1419
1420  if (!Subtarget->isV9()) {
1421    // SparcV8 does not have FNEGD and FABSD.
1422    setOperationAction(ISD::FNEG, MVT::f64, Custom);
1423    setOperationAction(ISD::FABS, MVT::f64, Custom);
1424  }
1425
1426  setOperationAction(ISD::FSIN , MVT::f128, Expand);
1427  setOperationAction(ISD::FCOS , MVT::f128, Expand);
1428  setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1429  setOperationAction(ISD::FREM , MVT::f128, Expand);
1430  setOperationAction(ISD::FMA  , MVT::f128, Expand);
1431  setOperationAction(ISD::FSIN , MVT::f64, Expand);
1432  setOperationAction(ISD::FCOS , MVT::f64, Expand);
1433  setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
1434  setOperationAction(ISD::FREM , MVT::f64, Expand);
1435  setOperationAction(ISD::FMA  , MVT::f64, Expand);
1436  setOperationAction(ISD::FSIN , MVT::f32, Expand);
1437  setOperationAction(ISD::FCOS , MVT::f32, Expand);
1438  setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
1439  setOperationAction(ISD::FREM , MVT::f32, Expand);
1440  setOperationAction(ISD::FMA  , MVT::f32, Expand);
1441  setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1442  setOperationAction(ISD::CTTZ , MVT::i32, Expand);
1443  setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
1444  setOperationAction(ISD::CTLZ , MVT::i32, Expand);
1445  setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
1446  setOperationAction(ISD::ROTL , MVT::i32, Expand);
1447  setOperationAction(ISD::ROTR , MVT::i32, Expand);
1448  setOperationAction(ISD::BSWAP, MVT::i32, Expand);
1449  setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
1450  setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1451  setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
1452  setOperationAction(ISD::FPOW , MVT::f128, Expand);
1453  setOperationAction(ISD::FPOW , MVT::f64, Expand);
1454  setOperationAction(ISD::FPOW , MVT::f32, Expand);
1455
1456  setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1457  setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1458  setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
1459
1460  // FIXME: Sparc provides these multiplies, but we don't have them yet.
1461  setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1462  setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
1463
1464  // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
1465  setOperationAction(ISD::VASTART           , MVT::Other, Custom);
1466  // VAARG needs to be lowered to not do unaligned accesses for doubles.
1467  setOperationAction(ISD::VAARG             , MVT::Other, Custom);
1468
1469  // Use the default implementation.
1470  setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
1471  setOperationAction(ISD::VAEND             , MVT::Other, Expand);
1472  setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
1473  setOperationAction(ISD::STACKRESTORE      , MVT::Other, Expand);
1474  setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Custom);
1475
1476  setExceptionPointerRegister(SP::I0);
1477  setExceptionSelectorRegister(SP::I1);
1478
1479  setStackPointerRegisterToSaveRestore(SP::O6);
1480
1481  if (Subtarget->isV9())
1482    setOperationAction(ISD::CTPOP, MVT::i32, Legal);
1483
1484  if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1485    setOperationAction(ISD::LOAD, MVT::f128, Legal);
1486    setOperationAction(ISD::STORE, MVT::f128, Legal);
1487  } else {
1488    setOperationAction(ISD::LOAD, MVT::f128, Custom);
1489    setOperationAction(ISD::STORE, MVT::f128, Custom);
1490  }
1491
1492  if (Subtarget->hasHardQuad()) {
1493    setOperationAction(ISD::FADD,  MVT::f128, Legal);
1494    setOperationAction(ISD::FSUB,  MVT::f128, Legal);
1495    setOperationAction(ISD::FMUL,  MVT::f128, Legal);
1496    setOperationAction(ISD::FDIV,  MVT::f128, Legal);
1497    setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1498    setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1499    setOperationAction(ISD::FP_ROUND,  MVT::f64, Legal);
1500    if (Subtarget->isV9()) {
1501      setOperationAction(ISD::FNEG, MVT::f128, Legal);
1502      setOperationAction(ISD::FABS, MVT::f128, Legal);
1503    } else {
1504      setOperationAction(ISD::FNEG, MVT::f128, Custom);
1505      setOperationAction(ISD::FABS, MVT::f128, Custom);
1506    }
1507
1508    if (!Subtarget->is64Bit()) {
1509      setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1510      setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1511      setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1512      setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1513    }
1514
1515  } else {
1516    // Custom legalize f128 operations.
1517
1518    setOperationAction(ISD::FADD,  MVT::f128, Custom);
1519    setOperationAction(ISD::FSUB,  MVT::f128, Custom);
1520    setOperationAction(ISD::FMUL,  MVT::f128, Custom);
1521    setOperationAction(ISD::FDIV,  MVT::f128, Custom);
1522    setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1523    setOperationAction(ISD::FNEG,  MVT::f128, Custom);
1524    setOperationAction(ISD::FABS,  MVT::f128, Custom);
1525
1526    setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1527    setOperationAction(ISD::FP_ROUND,  MVT::f64, Custom);
1528    setOperationAction(ISD::FP_ROUND,  MVT::f32, Custom);
1529
1530    // Setup Runtime library names.
1531    if (Subtarget->is64Bit()) {
1532      setLibcallName(RTLIB::ADD_F128,  "_Qp_add");
1533      setLibcallName(RTLIB::SUB_F128,  "_Qp_sub");
1534      setLibcallName(RTLIB::MUL_F128,  "_Qp_mul");
1535      setLibcallName(RTLIB::DIV_F128,  "_Qp_div");
1536      setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1537      setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
1538      setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
1539      setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
1540      setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
1541      setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1542      setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1543      setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1544      setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
1545      setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1546      setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1547      setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1548      setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1549    } else {
1550      setLibcallName(RTLIB::ADD_F128,  "_Q_add");
1551      setLibcallName(RTLIB::SUB_F128,  "_Q_sub");
1552      setLibcallName(RTLIB::MUL_F128,  "_Q_mul");
1553      setLibcallName(RTLIB::DIV_F128,  "_Q_div");
1554      setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1555      setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
1556      setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
1557      setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
1558      setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
1559      setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1560      setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1561      setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1562      setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1563      setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1564      setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1565      setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1566      setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1567    }
1568  }
1569
1570  setMinFunctionAlignment(2);
1571
1572  computeRegisterProperties();
1573}
1574
1575const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1576  switch (Opcode) {
1577  default: return 0;
1578  case SPISD::CMPICC:     return "SPISD::CMPICC";
1579  case SPISD::CMPFCC:     return "SPISD::CMPFCC";
1580  case SPISD::BRICC:      return "SPISD::BRICC";
1581  case SPISD::BRXCC:      return "SPISD::BRXCC";
1582  case SPISD::BRFCC:      return "SPISD::BRFCC";
1583  case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
1584  case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
1585  case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1586  case SPISD::Hi:         return "SPISD::Hi";
1587  case SPISD::Lo:         return "SPISD::Lo";
1588  case SPISD::FTOI:       return "SPISD::FTOI";
1589  case SPISD::ITOF:       return "SPISD::ITOF";
1590  case SPISD::FTOX:       return "SPISD::FTOX";
1591  case SPISD::XTOF:       return "SPISD::XTOF";
1592  case SPISD::CALL:       return "SPISD::CALL";
1593  case SPISD::RET_FLAG:   return "SPISD::RET_FLAG";
1594  case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
1595  case SPISD::FLUSHW:     return "SPISD::FLUSHW";
1596  case SPISD::TLS_ADD:    return "SPISD::TLS_ADD";
1597  case SPISD::TLS_LD:     return "SPISD::TLS_LD";
1598  case SPISD::TLS_CALL:   return "SPISD::TLS_CALL";
1599  }
1600}
1601
1602/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1603/// be zero. Op is expected to be a target specific node. Used by DAG
1604/// combiner.
1605void SparcTargetLowering::computeMaskedBitsForTargetNode
1606                                (const SDValue Op,
1607                                 APInt &KnownZero,
1608                                 APInt &KnownOne,
1609                                 const SelectionDAG &DAG,
1610                                 unsigned Depth) const {
1611  APInt KnownZero2, KnownOne2;
1612  KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
1613
1614  switch (Op.getOpcode()) {
1615  default: break;
1616  case SPISD::SELECT_ICC:
1617  case SPISD::SELECT_XCC:
1618  case SPISD::SELECT_FCC:
1619    DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1620    DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
1621    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1622    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1623
1624    // Only known if known in both the LHS and RHS.
1625    KnownOne &= KnownOne2;
1626    KnownZero &= KnownZero2;
1627    break;
1628  }
1629}
1630
1631// Look at LHS/RHS/CC and see if they are a lowered setcc instruction.  If so
1632// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
1633static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
1634                             ISD::CondCode CC, unsigned &SPCC) {
1635  if (isa<ConstantSDNode>(RHS) &&
1636      cast<ConstantSDNode>(RHS)->isNullValue() &&
1637      CC == ISD::SETNE &&
1638      (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1639         LHS.getOpcode() == SPISD::SELECT_XCC) &&
1640        LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1641       (LHS.getOpcode() == SPISD::SELECT_FCC &&
1642        LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1643      isa<ConstantSDNode>(LHS.getOperand(0)) &&
1644      isa<ConstantSDNode>(LHS.getOperand(1)) &&
1645      cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1646      cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
1647    SDValue CMPCC = LHS.getOperand(3);
1648    SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
1649    LHS = CMPCC.getOperand(0);
1650    RHS = CMPCC.getOperand(1);
1651  }
1652}
1653
1654// Convert to a target node and set target flags.
1655SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1656                                             SelectionDAG &DAG) const {
1657  if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1658    return DAG.getTargetGlobalAddress(GA->getGlobal(),
1659                                      SDLoc(GA),
1660                                      GA->getValueType(0),
1661                                      GA->getOffset(), TF);
1662
1663  if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1664    return DAG.getTargetConstantPool(CP->getConstVal(),
1665                                     CP->getValueType(0),
1666                                     CP->getAlignment(),
1667                                     CP->getOffset(), TF);
1668
1669  if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1670    return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1671                                     Op.getValueType(),
1672                                     0,
1673                                     TF);
1674
1675  if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1676    return DAG.getTargetExternalSymbol(ES->getSymbol(),
1677                                       ES->getValueType(0), TF);
1678
1679  llvm_unreachable("Unhandled address SDNode");
1680}
1681
1682// Split Op into high and low parts according to HiTF and LoTF.
1683// Return an ADD node combining the parts.
1684SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1685                                          unsigned HiTF, unsigned LoTF,
1686                                          SelectionDAG &DAG) const {
1687  SDLoc DL(Op);
1688  EVT VT = Op.getValueType();
1689  SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1690  SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1691  return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1692}
1693
1694// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1695// or ExternalSymbol SDNode.
1696SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
1697  SDLoc DL(Op);
1698  EVT VT = getPointerTy();
1699
1700  // Handle PIC mode first.
1701  if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1702    // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1703    SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1704    SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1705    SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
1706    // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1707    // function has calls.
1708    MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1709    MFI->setHasCalls(true);
1710    return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1711                       MachinePointerInfo::getGOT(), false, false, false, 0);
1712  }
1713
1714  // This is one of the absolute code models.
1715  switch(getTargetMachine().getCodeModel()) {
1716  default:
1717    llvm_unreachable("Unsupported absolute code model");
1718  case CodeModel::JITDefault:
1719  case CodeModel::Small:
1720    // abs32.
1721    return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1722  case CodeModel::Medium: {
1723    // abs44.
1724    SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
1725    H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
1726    SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1727    L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1728    return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1729  }
1730  case CodeModel::Large: {
1731    // abs64.
1732    SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
1733    Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
1734    SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1735    return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1736  }
1737  }
1738}
1739
1740SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
1741                                                SelectionDAG &DAG) const {
1742  return makeAddress(Op, DAG);
1743}
1744
1745SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
1746                                               SelectionDAG &DAG) const {
1747  return makeAddress(Op, DAG);
1748}
1749
1750SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1751                                               SelectionDAG &DAG) const {
1752  return makeAddress(Op, DAG);
1753}
1754
1755SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1756                                                   SelectionDAG &DAG) const {
1757
1758  GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1759  SDLoc DL(GA);
1760  const GlobalValue *GV = GA->getGlobal();
1761  EVT PtrVT = getPointerTy();
1762
1763  TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1764
1765  if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1766    unsigned HiTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_HI22
1767                     : SPII::MO_TLS_LDM_HI22);
1768    unsigned LoTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_LO10
1769                     : SPII::MO_TLS_LDM_LO10);
1770    unsigned addTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_ADD
1771                      : SPII::MO_TLS_LDM_ADD);
1772    unsigned callTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_CALL
1773                       : SPII::MO_TLS_LDM_CALL);
1774
1775    SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1776    SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1777    SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1778                               withTargetFlags(Op, addTF, DAG));
1779
1780    SDValue Chain = DAG.getEntryNode();
1781    SDValue InFlag;
1782
1783    Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1784    Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1785    InFlag = Chain.getValue(1);
1786    SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1787    SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1788
1789    SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1790    SmallVector<SDValue, 4> Ops;
1791    Ops.push_back(Chain);
1792    Ops.push_back(Callee);
1793    Ops.push_back(Symbol);
1794    Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1795    const uint32_t *Mask = getTargetMachine()
1796      .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1797    assert(Mask && "Missing call preserved mask for calling convention");
1798    Ops.push_back(DAG.getRegisterMask(Mask));
1799    Ops.push_back(InFlag);
1800    Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1801    InFlag = Chain.getValue(1);
1802    Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1803                               DAG.getIntPtrConstant(0, true), InFlag, DL);
1804    InFlag = Chain.getValue(1);
1805    SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1806
1807    if (model != TLSModel::LocalDynamic)
1808      return Ret;
1809
1810    SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1811                             withTargetFlags(Op, SPII::MO_TLS_LDO_HIX22, DAG));
1812    SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1813                             withTargetFlags(Op, SPII::MO_TLS_LDO_LOX10, DAG));
1814    HiLo =  DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1815    return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
1816                       withTargetFlags(Op, SPII::MO_TLS_LDO_ADD, DAG));
1817  }
1818
1819  if (model == TLSModel::InitialExec) {
1820    unsigned ldTF     = ((PtrVT == MVT::i64)? SPII::MO_TLS_IE_LDX
1821                         : SPII::MO_TLS_IE_LD);
1822
1823    SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1824
1825    // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1826    // function has calls.
1827    MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1828    MFI->setHasCalls(true);
1829
1830    SDValue TGA = makeHiLoPair(Op,
1831                               SPII::MO_TLS_IE_HI22, SPII::MO_TLS_IE_LO10, DAG);
1832    SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1833    SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1834                                 DL, PtrVT, Ptr,
1835                                 withTargetFlags(Op, ldTF, DAG));
1836    return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1837                       DAG.getRegister(SP::G7, PtrVT), Offset,
1838                       withTargetFlags(Op, SPII::MO_TLS_IE_ADD, DAG));
1839  }
1840
1841  assert(model == TLSModel::LocalExec);
1842  SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1843                           withTargetFlags(Op, SPII::MO_TLS_LE_HIX22, DAG));
1844  SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1845                           withTargetFlags(Op, SPII::MO_TLS_LE_LOX10, DAG));
1846  SDValue Offset =  DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1847
1848  return DAG.getNode(ISD::ADD, DL, PtrVT,
1849                     DAG.getRegister(SP::G7, PtrVT), Offset);
1850}
1851
1852SDValue
1853SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1854                                          SDValue Arg, SDLoc DL,
1855                                          SelectionDAG &DAG) const {
1856  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1857  EVT ArgVT = Arg.getValueType();
1858  Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1859
1860  ArgListEntry Entry;
1861  Entry.Node = Arg;
1862  Entry.Ty   = ArgTy;
1863
1864  if (ArgTy->isFP128Ty()) {
1865    // Create a stack object and pass the pointer to the library function.
1866    int FI = MFI->CreateStackObject(16, 8, false);
1867    SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1868    Chain = DAG.getStore(Chain,
1869                         DL,
1870                         Entry.Node,
1871                         FIPtr,
1872                         MachinePointerInfo(),
1873                         false,
1874                         false,
1875                         8);
1876
1877    Entry.Node = FIPtr;
1878    Entry.Ty   = PointerType::getUnqual(ArgTy);
1879  }
1880  Args.push_back(Entry);
1881  return Chain;
1882}
1883
1884SDValue
1885SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
1886                                 const char *LibFuncName,
1887                                 unsigned numArgs) const {
1888
1889  ArgListTy Args;
1890
1891  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1892
1893  SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
1894  Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
1895  Type *RetTyABI = RetTy;
1896  SDValue Chain = DAG.getEntryNode();
1897  SDValue RetPtr;
1898
1899  if (RetTy->isFP128Ty()) {
1900    // Create a Stack Object to receive the return value of type f128.
1901    ArgListEntry Entry;
1902    int RetFI = MFI->CreateStackObject(16, 8, false);
1903    RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
1904    Entry.Node = RetPtr;
1905    Entry.Ty   = PointerType::getUnqual(RetTy);
1906    if (!Subtarget->is64Bit())
1907      Entry.isSRet = true;
1908    Entry.isReturned = false;
1909    Args.push_back(Entry);
1910    RetTyABI = Type::getVoidTy(*DAG.getContext());
1911  }
1912
1913  assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
1914  for (unsigned i = 0, e = numArgs; i != e; ++i) {
1915    Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
1916  }
1917  TargetLowering::
1918    CallLoweringInfo CLI(Chain,
1919                         RetTyABI,
1920                         false, false, false, false,
1921                         0, CallingConv::C,
1922                         false, false, true,
1923                         Callee, Args, DAG, SDLoc(Op));
1924  std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1925
1926  // chain is in second result.
1927  if (RetTyABI == RetTy)
1928    return CallInfo.first;
1929
1930  assert (RetTy->isFP128Ty() && "Unexpected return type!");
1931
1932  Chain = CallInfo.second;
1933
1934  // Load RetPtr to get the return value.
1935  return DAG.getLoad(Op.getValueType(),
1936                     SDLoc(Op),
1937                     Chain,
1938                     RetPtr,
1939                     MachinePointerInfo(),
1940                     false, false, false, 8);
1941}
1942
1943SDValue
1944SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
1945                                      unsigned &SPCC,
1946                                      SDLoc DL,
1947                                      SelectionDAG &DAG) const {
1948
1949  const char *LibCall = 0;
1950  bool is64Bit = Subtarget->is64Bit();
1951  switch(SPCC) {
1952  default: llvm_unreachable("Unhandled conditional code!");
1953  case SPCC::FCC_E  : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
1954  case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
1955  case SPCC::FCC_L  : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
1956  case SPCC::FCC_G  : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
1957  case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
1958  case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
1959  case SPCC::FCC_UL :
1960  case SPCC::FCC_ULE:
1961  case SPCC::FCC_UG :
1962  case SPCC::FCC_UGE:
1963  case SPCC::FCC_U  :
1964  case SPCC::FCC_O  :
1965  case SPCC::FCC_LG :
1966  case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
1967  }
1968
1969  SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
1970  Type *RetTy = Type::getInt32Ty(*DAG.getContext());
1971  ArgListTy Args;
1972  SDValue Chain = DAG.getEntryNode();
1973  Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
1974  Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
1975
1976  TargetLowering::
1977    CallLoweringInfo CLI(Chain,
1978                         RetTy,
1979                         false, false, false, false,
1980                         0, CallingConv::C,
1981                         false, false, true,
1982                         Callee, Args, DAG, DL);
1983
1984  std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1985
1986  // result is in first, and chain is in second result.
1987  SDValue Result =  CallInfo.first;
1988
1989  switch(SPCC) {
1990  default: {
1991    SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
1992    SPCC = SPCC::ICC_NE;
1993    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
1994  }
1995  case SPCC::FCC_UL : {
1996    SDValue Mask   = DAG.getTargetConstant(1, Result.getValueType());
1997    Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
1998    SDValue RHS    = DAG.getTargetConstant(0, Result.getValueType());
1999    SPCC = SPCC::ICC_NE;
2000    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2001  }
2002  case SPCC::FCC_ULE: {
2003    SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
2004    SPCC = SPCC::ICC_NE;
2005    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2006  }
2007  case SPCC::FCC_UG :  {
2008    SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2009    SPCC = SPCC::ICC_G;
2010    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2011  }
2012  case SPCC::FCC_UGE: {
2013    SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2014    SPCC = SPCC::ICC_NE;
2015    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2016  }
2017
2018  case SPCC::FCC_U  :  {
2019    SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2020    SPCC = SPCC::ICC_E;
2021    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2022  }
2023  case SPCC::FCC_O  :  {
2024    SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2025    SPCC = SPCC::ICC_NE;
2026    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2027  }
2028  case SPCC::FCC_LG :  {
2029    SDValue Mask   = DAG.getTargetConstant(3, Result.getValueType());
2030    Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2031    SDValue RHS    = DAG.getTargetConstant(0, Result.getValueType());
2032    SPCC = SPCC::ICC_NE;
2033    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2034  }
2035  case SPCC::FCC_UE : {
2036    SDValue Mask   = DAG.getTargetConstant(3, Result.getValueType());
2037    Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2038    SDValue RHS    = DAG.getTargetConstant(0, Result.getValueType());
2039    SPCC = SPCC::ICC_E;
2040    return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2041  }
2042  }
2043}
2044
2045static SDValue
2046LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2047                   const SparcTargetLowering &TLI) {
2048
2049  if (Op.getOperand(0).getValueType() == MVT::f64)
2050    return TLI.LowerF128Op(Op, DAG,
2051                           TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2052
2053  if (Op.getOperand(0).getValueType() == MVT::f32)
2054    return TLI.LowerF128Op(Op, DAG,
2055                           TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2056
2057  llvm_unreachable("fpextend with non-float operand!");
2058  return SDValue(0, 0);
2059}
2060
2061static SDValue
2062LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2063                  const SparcTargetLowering &TLI) {
2064  // FP_ROUND on f64 and f32 are legal.
2065  if (Op.getOperand(0).getValueType() != MVT::f128)
2066    return Op;
2067
2068  if (Op.getValueType() == MVT::f64)
2069    return TLI.LowerF128Op(Op, DAG,
2070                           TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2071  if (Op.getValueType() == MVT::f32)
2072    return TLI.LowerF128Op(Op, DAG,
2073                           TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2074
2075  llvm_unreachable("fpround to non-float!");
2076  return SDValue(0, 0);
2077}
2078
2079static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2080                               const SparcTargetLowering &TLI,
2081                               bool hasHardQuad) {
2082  SDLoc dl(Op);
2083  EVT VT = Op.getValueType();
2084  assert(VT == MVT::i32 || VT == MVT::i64);
2085
2086  // Expand f128 operations to fp128 abi calls.
2087  if (Op.getOperand(0).getValueType() == MVT::f128
2088      && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2089    const char *libName = TLI.getLibcallName(VT == MVT::i32
2090                                             ? RTLIB::FPTOSINT_F128_I32
2091                                             : RTLIB::FPTOSINT_F128_I64);
2092    return TLI.LowerF128Op(Op, DAG, libName, 1);
2093  }
2094
2095  // Expand if the resulting type is illegal.
2096  if (!TLI.isTypeLegal(VT))
2097    return SDValue(0, 0);
2098
2099  // Otherwise, Convert the fp value to integer in an FP register.
2100  if (VT == MVT::i32)
2101    Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2102  else
2103    Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2104
2105  return DAG.getNode(ISD::BITCAST, dl, VT, Op);
2106}
2107
2108static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2109                               const SparcTargetLowering &TLI,
2110                               bool hasHardQuad) {
2111  SDLoc dl(Op);
2112  EVT OpVT = Op.getOperand(0).getValueType();
2113  assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2114
2115  EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2116
2117  // Expand f128 operations to fp128 ABI calls.
2118  if (Op.getValueType() == MVT::f128
2119      && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2120    const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2121                                             ? RTLIB::SINTTOFP_I32_F128
2122                                             : RTLIB::SINTTOFP_I64_F128);
2123    return TLI.LowerF128Op(Op, DAG, libName, 1);
2124  }
2125
2126  // Expand if the operand type is illegal.
2127  if (!TLI.isTypeLegal(OpVT))
2128    return SDValue(0, 0);
2129
2130  // Otherwise, Convert the int value to FP in an FP register.
2131  SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2132  unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2133  return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
2134}
2135
2136static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2137                               const SparcTargetLowering &TLI,
2138                               bool hasHardQuad) {
2139  SDLoc dl(Op);
2140  EVT VT = Op.getValueType();
2141
2142  // Expand if it does not involve f128 or the target has support for
2143  // quad floating point instructions and the resulting type is legal.
2144  if (Op.getOperand(0).getValueType() != MVT::f128 ||
2145      (hasHardQuad && TLI.isTypeLegal(VT)))
2146    return SDValue(0, 0);
2147
2148  assert(VT == MVT::i32 || VT == MVT::i64);
2149
2150  return TLI.LowerF128Op(Op, DAG,
2151                         TLI.getLibcallName(VT == MVT::i32
2152                                            ? RTLIB::FPTOUINT_F128_I32
2153                                            : RTLIB::FPTOUINT_F128_I64),
2154                         1);
2155}
2156
2157static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2158                               const SparcTargetLowering &TLI,
2159                               bool hasHardQuad) {
2160  SDLoc dl(Op);
2161  EVT OpVT = Op.getOperand(0).getValueType();
2162  assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2163
2164  // Expand if it does not involve f128 or the target has support for
2165  // quad floating point instructions and the operand type is legal.
2166  if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
2167    return SDValue(0, 0);
2168
2169  return TLI.LowerF128Op(Op, DAG,
2170                         TLI.getLibcallName(OpVT == MVT::i32
2171                                            ? RTLIB::UINTTOFP_I32_F128
2172                                            : RTLIB::UINTTOFP_I64_F128),
2173                         1);
2174}
2175
2176static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2177                          const SparcTargetLowering &TLI,
2178                          bool hasHardQuad) {
2179  SDValue Chain = Op.getOperand(0);
2180  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2181  SDValue LHS = Op.getOperand(2);
2182  SDValue RHS = Op.getOperand(3);
2183  SDValue Dest = Op.getOperand(4);
2184  SDLoc dl(Op);
2185  unsigned Opc, SPCC = ~0U;
2186
2187  // If this is a br_cc of a "setcc", and if the setcc got lowered into
2188  // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2189  LookThroughSetCC(LHS, RHS, CC, SPCC);
2190
2191  // Get the condition flag.
2192  SDValue CompareFlag;
2193  if (LHS.getValueType().isInteger()) {
2194    CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2195    if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2196    // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2197    Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
2198  } else {
2199    if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2200      if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2201      CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2202      Opc = SPISD::BRICC;
2203    } else {
2204      CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2205      if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2206      Opc = SPISD::BRFCC;
2207    }
2208  }
2209  return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2210                     DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2211}
2212
2213static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2214                              const SparcTargetLowering &TLI,
2215                              bool hasHardQuad) {
2216  SDValue LHS = Op.getOperand(0);
2217  SDValue RHS = Op.getOperand(1);
2218  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2219  SDValue TrueVal = Op.getOperand(2);
2220  SDValue FalseVal = Op.getOperand(3);
2221  SDLoc dl(Op);
2222  unsigned Opc, SPCC = ~0U;
2223
2224  // If this is a select_cc of a "setcc", and if the setcc got lowered into
2225  // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2226  LookThroughSetCC(LHS, RHS, CC, SPCC);
2227
2228  SDValue CompareFlag;
2229  if (LHS.getValueType().isInteger()) {
2230    CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
2231    Opc = LHS.getValueType() == MVT::i32 ?
2232          SPISD::SELECT_ICC : SPISD::SELECT_XCC;
2233    if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2234  } else {
2235    if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2236      if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2237      CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2238      Opc = SPISD::SELECT_ICC;
2239    } else {
2240      CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2241      Opc = SPISD::SELECT_FCC;
2242      if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2243    }
2244  }
2245  return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
2246                     DAG.getConstant(SPCC, MVT::i32), CompareFlag);
2247}
2248
2249static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
2250                            const SparcTargetLowering &TLI) {
2251  MachineFunction &MF = DAG.getMachineFunction();
2252  SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2253
2254  // Need frame address to find the address of VarArgsFrameIndex.
2255  MF.getFrameInfo()->setFrameAddressIsTaken(true);
2256
2257  // vastart just stores the address of the VarArgsFrameIndex slot into the
2258  // memory location argument.
2259  SDLoc DL(Op);
2260  SDValue Offset =
2261    DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2262                DAG.getRegister(SP::I6, TLI.getPointerTy()),
2263                DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
2264  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2265  return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
2266                      MachinePointerInfo(SV), false, false, 0);
2267}
2268
2269static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
2270  SDNode *Node = Op.getNode();
2271  EVT VT = Node->getValueType(0);
2272  SDValue InChain = Node->getOperand(0);
2273  SDValue VAListPtr = Node->getOperand(1);
2274  EVT PtrVT = VAListPtr.getValueType();
2275  const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
2276  SDLoc DL(Node);
2277  SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
2278                               MachinePointerInfo(SV), false, false, false, 0);
2279  // Increment the pointer, VAList, to the next vaarg.
2280  SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2281                                DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2282  // Store the incremented VAList to the legalized pointer.
2283  InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
2284                         VAListPtr, MachinePointerInfo(SV), false, false, 0);
2285  // Load the actual argument out of the pointer VAList.
2286  // We can't count on greater alignment than the word size.
2287  return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2288                     false, false, false,
2289                     std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
2290}
2291
2292static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
2293  SDValue Chain = Op.getOperand(0);  // Legalize the chain.
2294  SDValue Size  = Op.getOperand(1);  // Legalize the size.
2295  SDLoc dl(Op);
2296
2297  unsigned SPReg = SP::O6;
2298  SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
2299  SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
2300  Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP);    // Output chain
2301
2302  // The resultant pointer is actually 16 words from the bottom of the stack,
2303  // to provide a register spill area.
2304  SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
2305                                 DAG.getConstant(96, MVT::i32));
2306  SDValue Ops[2] = { NewVal, Chain };
2307  return DAG.getMergeValues(Ops, 2, dl);
2308}
2309
2310
2311static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
2312  SDLoc dl(Op);
2313  SDValue Chain = DAG.getNode(SPISD::FLUSHW,
2314                              dl, MVT::Other, DAG.getEntryNode());
2315  return Chain;
2316}
2317
2318static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
2319  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2320  MFI->setFrameAddressIsTaken(true);
2321
2322  EVT VT = Op.getValueType();
2323  SDLoc dl(Op);
2324  unsigned FrameReg = SP::I6;
2325
2326  uint64_t depth = Op.getConstantOperandVal(0);
2327
2328  SDValue FrameAddr;
2329  if (depth == 0)
2330    FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2331  else {
2332    // flush first to make sure the windowed registers' values are in stack
2333    SDValue Chain = getFLUSHW(Op, DAG);
2334    FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2335
2336    for (uint64_t i = 0; i != depth; ++i) {
2337      SDValue Ptr = DAG.getNode(ISD::ADD,
2338                                dl, MVT::i32,
2339                                FrameAddr, DAG.getIntPtrConstant(56));
2340      FrameAddr = DAG.getLoad(MVT::i32, dl,
2341                              Chain,
2342                              Ptr,
2343                              MachinePointerInfo(), false, false, false, 0);
2344    }
2345  }
2346  return FrameAddr;
2347}
2348
2349static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2350                               const SparcTargetLowering &TLI) {
2351  MachineFunction &MF = DAG.getMachineFunction();
2352  MachineFrameInfo *MFI = MF.getFrameInfo();
2353  MFI->setReturnAddressIsTaken(true);
2354
2355  EVT VT = Op.getValueType();
2356  SDLoc dl(Op);
2357  uint64_t depth = Op.getConstantOperandVal(0);
2358
2359  SDValue RetAddr;
2360  if (depth == 0) {
2361    unsigned RetReg = MF.addLiveIn(SP::I7,
2362                                   TLI.getRegClassFor(TLI.getPointerTy()));
2363    RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
2364  } else {
2365    // Need frame address to find return address of the caller.
2366    MFI->setFrameAddressIsTaken(true);
2367
2368    // flush first to make sure the windowed registers' values are in stack
2369    SDValue Chain = getFLUSHW(Op, DAG);
2370    RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
2371
2372    for (uint64_t i = 0; i != depth; ++i) {
2373      SDValue Ptr = DAG.getNode(ISD::ADD,
2374                                dl, MVT::i32,
2375                                RetAddr,
2376                                DAG.getIntPtrConstant((i == depth-1)?60:56));
2377      RetAddr = DAG.getLoad(MVT::i32, dl,
2378                            Chain,
2379                            Ptr,
2380                            MachinePointerInfo(), false, false, false, 0);
2381    }
2382  }
2383  return RetAddr;
2384}
2385
2386static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
2387{
2388  SDLoc dl(Op);
2389
2390  assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
2391  assert(opcode == ISD::FNEG || opcode == ISD::FABS);
2392
2393  // Lower fneg/fabs on f64 to fneg/fabs on f32.
2394  // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2395  // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2396
2397  SDValue SrcReg64 = Op.getOperand(0);
2398  SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2399                                            SrcReg64);
2400  SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2401                                            SrcReg64);
2402
2403  Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
2404
2405  SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2406                                                dl, MVT::f64), 0);
2407  DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2408                                       DstReg64, Hi32);
2409  DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2410                                       DstReg64, Lo32);
2411  return DstReg64;
2412}
2413
2414// Lower a f128 load into two f64 loads.
2415static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2416{
2417  SDLoc dl(Op);
2418  LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2419  assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2420         && "Unexpected node type");
2421
2422  unsigned alignment = LdNode->getAlignment();
2423  if (alignment > 8)
2424    alignment = 8;
2425
2426  SDValue Hi64 = DAG.getLoad(MVT::f64,
2427                             dl,
2428                             LdNode->getChain(),
2429                             LdNode->getBasePtr(),
2430                             LdNode->getPointerInfo(),
2431                             false, false, false, alignment);
2432  EVT addrVT = LdNode->getBasePtr().getValueType();
2433  SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2434                              LdNode->getBasePtr(),
2435                              DAG.getConstant(8, addrVT));
2436  SDValue Lo64 = DAG.getLoad(MVT::f64,
2437                             dl,
2438                             LdNode->getChain(),
2439                             LoPtr,
2440                             LdNode->getPointerInfo(),
2441                             false, false, false, alignment);
2442
2443  SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2444  SDValue SubRegOdd  = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2445
2446  SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2447                                       dl, MVT::f128);
2448  InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2449                               MVT::f128,
2450                               SDValue(InFP128, 0),
2451                               Hi64,
2452                               SubRegEven);
2453  InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2454                               MVT::f128,
2455                               SDValue(InFP128, 0),
2456                               Lo64,
2457                               SubRegOdd);
2458  SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2459                           SDValue(Lo64.getNode(), 1) };
2460  SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2461                                 &OutChains[0], 2);
2462  SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2463  return DAG.getMergeValues(Ops, 2, dl);
2464}
2465
2466// Lower a f128 store into two f64 stores.
2467static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2468  SDLoc dl(Op);
2469  StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2470  assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2471         && "Unexpected node type");
2472  SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2473  SDValue SubRegOdd  = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2474
2475  SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2476                                    dl,
2477                                    MVT::f64,
2478                                    StNode->getValue(),
2479                                    SubRegEven);
2480  SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2481                                    dl,
2482                                    MVT::f64,
2483                                    StNode->getValue(),
2484                                    SubRegOdd);
2485
2486  unsigned alignment = StNode->getAlignment();
2487  if (alignment > 8)
2488    alignment = 8;
2489
2490  SDValue OutChains[2];
2491  OutChains[0] = DAG.getStore(StNode->getChain(),
2492                              dl,
2493                              SDValue(Hi64, 0),
2494                              StNode->getBasePtr(),
2495                              MachinePointerInfo(),
2496                              false, false, alignment);
2497  EVT addrVT = StNode->getBasePtr().getValueType();
2498  SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2499                              StNode->getBasePtr(),
2500                              DAG.getConstant(8, addrVT));
2501  OutChains[1] = DAG.getStore(StNode->getChain(),
2502                             dl,
2503                             SDValue(Lo64, 0),
2504                             LoPtr,
2505                             MachinePointerInfo(),
2506                             false, false, alignment);
2507  return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2508                     &OutChains[0], 2);
2509}
2510
2511static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2512                         const SparcTargetLowering &TLI,
2513                         bool is64Bit) {
2514  if (Op.getValueType() == MVT::f64)
2515    return LowerF64Op(Op, DAG, ISD::FNEG);
2516  if (Op.getValueType() == MVT::f128)
2517    return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2518  return Op;
2519}
2520
2521static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2522  if (Op.getValueType() == MVT::f64)
2523    return LowerF64Op(Op, DAG, ISD::FABS);
2524  if (Op.getValueType() != MVT::f128)
2525    return Op;
2526
2527  // Lower fabs on f128 to fabs on f64
2528  // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2529
2530  SDLoc dl(Op);
2531  SDValue SrcReg128 = Op.getOperand(0);
2532  SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2533                                            SrcReg128);
2534  SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2535                                            SrcReg128);
2536  if (isV9)
2537    Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2538  else
2539    Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
2540
2541  SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2542                                                 dl, MVT::f128), 0);
2543  DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2544                                        DstReg128, Hi64);
2545  DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2546                                        DstReg128, Lo64);
2547  return DstReg128;
2548}
2549
2550static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
2551
2552  if (Op.getValueType() != MVT::i64)
2553    return Op;
2554
2555  SDLoc dl(Op);
2556  SDValue Src1 = Op.getOperand(0);
2557  SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2558  SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2559                               DAG.getConstant(32, MVT::i64));
2560  Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2561
2562  SDValue Src2 = Op.getOperand(1);
2563  SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2564  SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2565                               DAG.getConstant(32, MVT::i64));
2566  Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2567
2568
2569  bool hasChain = false;
2570  unsigned hiOpc = Op.getOpcode();
2571  switch (Op.getOpcode()) {
2572  default: llvm_unreachable("Invalid opcode");
2573  case ISD::ADDC: hiOpc = ISD::ADDE; break;
2574  case ISD::ADDE: hasChain = true; break;
2575  case ISD::SUBC: hiOpc = ISD::SUBE; break;
2576  case ISD::SUBE: hasChain = true; break;
2577  }
2578  SDValue Lo;
2579  SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2580  if (hasChain) {
2581    Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2582                     Op.getOperand(2));
2583  } else {
2584    Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2585  }
2586  SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2587  SDValue Carry = Hi.getValue(1);
2588
2589  Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2590  Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2591  Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2592                   DAG.getConstant(32, MVT::i64));
2593
2594  SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2595  SDValue Ops[2] = { Dst, Carry };
2596  return DAG.getMergeValues(Ops, 2, dl);
2597}
2598
2599SDValue SparcTargetLowering::
2600LowerOperation(SDValue Op, SelectionDAG &DAG) const {
2601
2602  bool hasHardQuad = Subtarget->hasHardQuad();
2603  bool is64Bit     = Subtarget->is64Bit();
2604  bool isV9        = Subtarget->isV9();
2605
2606  switch (Op.getOpcode()) {
2607  default: llvm_unreachable("Should not custom lower this!");
2608
2609  case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG, *this);
2610  case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
2611  case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
2612  case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
2613  case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
2614  case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
2615  case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG, *this,
2616                                                       hasHardQuad);
2617  case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG, *this,
2618                                                       hasHardQuad);
2619  case ISD::FP_TO_UINT:         return LowerFP_TO_UINT(Op, DAG, *this,
2620                                                       hasHardQuad);
2621  case ISD::UINT_TO_FP:         return LowerUINT_TO_FP(Op, DAG, *this,
2622                                                       hasHardQuad);
2623  case ISD::BR_CC:              return LowerBR_CC(Op, DAG, *this,
2624                                                  hasHardQuad);
2625  case ISD::SELECT_CC:          return LowerSELECT_CC(Op, DAG, *this,
2626                                                      hasHardQuad);
2627  case ISD::VASTART:            return LowerVASTART(Op, DAG, *this);
2628  case ISD::VAARG:              return LowerVAARG(Op, DAG);
2629  case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
2630
2631  case ISD::LOAD:               return LowerF128Load(Op, DAG);
2632  case ISD::STORE:              return LowerF128Store(Op, DAG);
2633  case ISD::FADD:               return LowerF128Op(Op, DAG,
2634                                       getLibcallName(RTLIB::ADD_F128), 2);
2635  case ISD::FSUB:               return LowerF128Op(Op, DAG,
2636                                       getLibcallName(RTLIB::SUB_F128), 2);
2637  case ISD::FMUL:               return LowerF128Op(Op, DAG,
2638                                       getLibcallName(RTLIB::MUL_F128), 2);
2639  case ISD::FDIV:               return LowerF128Op(Op, DAG,
2640                                       getLibcallName(RTLIB::DIV_F128), 2);
2641  case ISD::FSQRT:              return LowerF128Op(Op, DAG,
2642                                       getLibcallName(RTLIB::SQRT_F128),1);
2643  case ISD::FNEG:               return LowerFNEG(Op, DAG, *this, is64Bit);
2644  case ISD::FABS:               return LowerFABS(Op, DAG, isV9);
2645  case ISD::FP_EXTEND:          return LowerF128_FPEXTEND(Op, DAG, *this);
2646  case ISD::FP_ROUND:           return LowerF128_FPROUND(Op, DAG, *this);
2647  case ISD::ADDC:
2648  case ISD::ADDE:
2649  case ISD::SUBC:
2650  case ISD::SUBE:               return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
2651  }
2652}
2653
2654MachineBasicBlock *
2655SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
2656                                                 MachineBasicBlock *BB) const {
2657  const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2658  unsigned BROpcode;
2659  unsigned CC;
2660  DebugLoc dl = MI->getDebugLoc();
2661  // Figure out the conditional branch opcode to use for this select_cc.
2662  switch (MI->getOpcode()) {
2663  default: llvm_unreachable("Unknown SELECT_CC!");
2664  case SP::SELECT_CC_Int_ICC:
2665  case SP::SELECT_CC_FP_ICC:
2666  case SP::SELECT_CC_DFP_ICC:
2667  case SP::SELECT_CC_QFP_ICC:
2668    BROpcode = SP::BCOND;
2669    break;
2670  case SP::SELECT_CC_Int_FCC:
2671  case SP::SELECT_CC_FP_FCC:
2672  case SP::SELECT_CC_DFP_FCC:
2673  case SP::SELECT_CC_QFP_FCC:
2674    BROpcode = SP::FBCOND;
2675    break;
2676  }
2677
2678  CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
2679
2680  // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2681  // control-flow pattern.  The incoming instruction knows the destination vreg
2682  // to set, the condition code register to branch on, the true/false values to
2683  // select between, and a branch opcode to use.
2684  const BasicBlock *LLVM_BB = BB->getBasicBlock();
2685  MachineFunction::iterator It = BB;
2686  ++It;
2687
2688  //  thisMBB:
2689  //  ...
2690  //   TrueVal = ...
2691  //   [f]bCC copy1MBB
2692  //   fallthrough --> copy0MBB
2693  MachineBasicBlock *thisMBB = BB;
2694  MachineFunction *F = BB->getParent();
2695  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2696  MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
2697  F->insert(It, copy0MBB);
2698  F->insert(It, sinkMBB);
2699
2700  // Transfer the remainder of BB and its successor edges to sinkMBB.
2701  sinkMBB->splice(sinkMBB->begin(), BB,
2702                  llvm::next(MachineBasicBlock::iterator(MI)),
2703                  BB->end());
2704  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2705
2706  // Add the true and fallthrough blocks as its successors.
2707  BB->addSuccessor(copy0MBB);
2708  BB->addSuccessor(sinkMBB);
2709
2710  BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
2711
2712  //  copy0MBB:
2713  //   %FalseValue = ...
2714  //   # fallthrough to sinkMBB
2715  BB = copy0MBB;
2716
2717  // Update machine-CFG edges
2718  BB->addSuccessor(sinkMBB);
2719
2720  //  sinkMBB:
2721  //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2722  //  ...
2723  BB = sinkMBB;
2724  BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
2725    .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2726    .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
2727
2728  MI->eraseFromParent();   // The pseudo instruction is gone now.
2729  return BB;
2730}
2731
2732//===----------------------------------------------------------------------===//
2733//                         Sparc Inline Assembly Support
2734//===----------------------------------------------------------------------===//
2735
2736/// getConstraintType - Given a constraint letter, return the type of
2737/// constraint it is for this target.
2738SparcTargetLowering::ConstraintType
2739SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
2740  if (Constraint.size() == 1) {
2741    switch (Constraint[0]) {
2742    default:  break;
2743    case 'r': return C_RegisterClass;
2744    }
2745  }
2746
2747  return TargetLowering::getConstraintType(Constraint);
2748}
2749
2750std::pair<unsigned, const TargetRegisterClass*>
2751SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
2752                                                  MVT VT) const {
2753  if (Constraint.size() == 1) {
2754    switch (Constraint[0]) {
2755    case 'r':
2756      return std::make_pair(0U, &SP::IntRegsRegClass);
2757    }
2758  }
2759
2760  return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2761}
2762
2763bool
2764SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2765  // The Sparc target isn't yet aware of offsets.
2766  return false;
2767}
2768
2769void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
2770                                             SmallVectorImpl<SDValue>& Results,
2771                                             SelectionDAG &DAG) const {
2772
2773  SDLoc dl(N);
2774
2775  RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
2776
2777  switch (N->getOpcode()) {
2778  default:
2779    llvm_unreachable("Do not know how to custom type legalize this operation!");
2780
2781  case ISD::FP_TO_SINT:
2782  case ISD::FP_TO_UINT:
2783    // Custom lower only if it involves f128 or i64.
2784    if (N->getOperand(0).getValueType() != MVT::f128
2785        || N->getValueType(0) != MVT::i64)
2786      return;
2787    libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
2788               ? RTLIB::FPTOSINT_F128_I64
2789               : RTLIB::FPTOUINT_F128_I64);
2790
2791    Results.push_back(LowerF128Op(SDValue(N, 0),
2792                                  DAG,
2793                                  getLibcallName(libCall),
2794                                  1));
2795    return;
2796
2797  case ISD::SINT_TO_FP:
2798  case ISD::UINT_TO_FP:
2799    // Custom lower only if it involves f128 or i64.
2800    if (N->getValueType(0) != MVT::f128
2801        || N->getOperand(0).getValueType() != MVT::i64)
2802      return;
2803
2804    libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
2805               ? RTLIB::SINTTOFP_I64_F128
2806               : RTLIB::UINTTOFP_I64_F128);
2807
2808    Results.push_back(LowerF128Op(SDValue(N, 0),
2809                                  DAG,
2810                                  getLibcallName(libCall),
2811                                  1));
2812    return;
2813  }
2814}
2815