1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the interfaces that Sparc uses to lower LLVM code into a
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// selection DAG.
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "SparcISelLowering.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "SparcTargetMachine.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "SparcMachineFunctionInfo.h"
1819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/DerivedTypes.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Function.h"
2019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Module.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/CallingConvLower.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFrameInfo.h"
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunction.h"
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstrBuilder.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineRegisterInfo.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/SelectionDAG.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/VectorExtras.h"
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Calling Convention Implementation
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                 ISD::ArgFlagsTy &ArgFlags, CCState &State)
4019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman{
4119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert (ArgFlags.isSRet());
4219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
4319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //Assign SRet argument
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
4519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         0,
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         LocVT, LocInfo));
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
4819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
5019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MVT &LocVT, CCValAssign::LocInfo &LocInfo,
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                ISD::ArgFlagsTy &ArgFlags, CCState &State)
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman{
5419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  static const unsigned RegList[] = {
5519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
5619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  };
5719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //Try to get first reg
5819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (unsigned Reg = State.AllocateReg(RegList, 6)) {
5919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
6019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else {
6119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //Assign whole thing in stack
6219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
6319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           State.AllocateStack(8,4),
6419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           LocVT, LocInfo));
6519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return true;
6619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
6719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
6819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //Try to get second reg
6919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (unsigned Reg = State.AllocateReg(RegList, 6))
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else
7219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
7319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           State.AllocateStack(4,4),
7419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           LocVT, LocInfo));
7519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return true;
7619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
7719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "SparcGenCallingConv.inc"
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::LowerReturn(SDValue Chain,
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 CallingConv::ID CallConv, bool isVarArg,
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 const SmallVectorImpl<ISD::OutputArg> &Outs,
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 const SmallVectorImpl<SDValue> &OutVals,
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 DebugLoc dl, SelectionDAG &DAG) const {
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
8719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFunction &MF = DAG.getMachineFunction();
8819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // CCValAssign - represent the assignment of the return value to locations.
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> RVLocs;
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // CCState - Info about the registers and stack slot.
9319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
9419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		 DAG.getTarget(), RVLocs, *DAG.getContext());
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Analize return values.
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is the first return lowered for this function, add the regs to the
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // liveout set for the function.
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MF.getRegInfo().liveout_empty()) {
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0; i != RVLocs.size(); ++i)
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (RVLocs[i].isRegLoc())
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Flag;
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Copy the result values into the output registers.
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != RVLocs.size(); ++i) {
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = RVLocs[i];
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(VA.isRegLoc() && "Can only return in registers!");
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             OutVals[i], Flag);
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Guarantee that all emitted copies are stuck together with flags.
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Flag = Chain.getValue(1);
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned RetAddrOffset = 8; //Call Inst + Delay Slot
12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // If the function returns a struct, copy the SRetReturnReg to I0
12319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MF.getFunction()->hasStructRetAttr()) {
12419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
12519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Reg = SFI->getSRetReturnReg();
12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Reg)
12719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      llvm_unreachable("sret virtual register not created in the entry block");
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
12919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getCopyToReg(Chain, dl, SP::I0, Val, Flag);
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Flag = Chain.getValue(1);
13119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (MF.getRegInfo().liveout_empty())
13219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MF.getRegInfo().addLiveOut(SP::I0);
13319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
13419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
13519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
13619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RetAddrOffsetNode = DAG.getConstant(RetAddrOffset, MVT::i32);
13719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Flag.getNode())
13919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
14019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       RetAddrOffsetNode, Flag);
14119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     RetAddrOffsetNode);
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// passed in either one or two GPRs, including FP values.  TODO: we should
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// pass FP values in FP registers for fastcc functions.
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::LowerFormalArguments(SDValue Chain,
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          CallingConv::ID CallConv, bool isVarArg,
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          const SmallVectorImpl<ISD::InputArg>
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            &Ins,
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          DebugLoc dl, SelectionDAG &DAG,
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          SmallVectorImpl<SDValue> &InVals)
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                            const {
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineRegisterInfo &RegInfo = MF.getRegInfo();
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Assign locations to all of the incoming arguments.
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> ArgLocs;
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		 getTargetMachine(), ArgLocs, *DAG.getContext());
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const unsigned StackOffset = 92;
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = ArgLocs[i];
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
17219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (i == 0  && Ins[i].Flags.isSRet()) {
17319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      //Get SRet from [%fp+64]
17419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
17519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
17619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
17719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                MachinePointerInfo(),
17819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                false, false, 0);
17919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      InVals.push_back(Arg);
18019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
18119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
18319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VA.isRegLoc()) {
18419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VA.needsCustom()) {
18519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(VA.getLocVT() == MVT::f64);
18619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
18719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
18819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
18919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
19019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(i+1 < e);
19119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        CCValAssign &NextVA = ArgLocs[++i];
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue LoVal;
19419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (NextVA.isMemLoc()) {
19519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          int FrameIdx = MF.getFrameInfo()->
19619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman            CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
19819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
19919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo(),
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              false, false, 0);
20119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        } else {
20219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
20319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                        &SP::IntRegsRegClass);
20419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        }
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        SDValue WholeValue =
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman          DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
20819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
20919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        InVals.push_back(WholeValue);
21019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
21219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
21419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
21519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VA.getLocVT() == MVT::f32)
21619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
21719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      else if (VA.getLocVT() != MVT::i32) {
21819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
21919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          DAG.getValueType(VA.getLocVT()));
22019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
22119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      }
22219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      InVals.push_back(Arg);
22319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
22419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(VA.isMemLoc());
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
22819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Offset = VA.getLocMemOffset()+StackOffset;
22919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
23019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VA.needsCustom()) {
23119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(VA.getValVT() == MVT::f64);
23219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      //If it is double-word aligned, just load.
23319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (Offset % 8 == 0) {
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        int FI = MF.getFrameInfo()->CreateFixedObject(8,
23519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                      Offset,
23619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                      true);
23719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
23819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
23919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   MachinePointerInfo(),
24019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   false,false, 0);
24119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        InVals.push_back(Load);
24219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
24419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
24519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int FI = MF.getFrameInfo()->CreateFixedObject(4,
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                    Offset,
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                    true);
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
25019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  MachinePointerInfo(),
25119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  false, false, 0);
25219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
25319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                     Offset+4,
25419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                     true);
25519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
25619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
25719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
25819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  MachinePointerInfo(),
25919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                  false, false, 0);
26019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
26119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue WholeValue =
26219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
26319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
26419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      InVals.push_back(WholeValue);
26519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
26619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
26719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
26819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FI = MF.getFrameInfo()->CreateFixedObject(4,
26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  Offset,
27019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                                  true);
27119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
27219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Load ;
27319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
27419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
27519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         MachinePointerInfo(),
27619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         false, false, 0);
27719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    } else {
27819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
27919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // Sparc is big endian, so add an offset based on the ObjectVT.
28019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
28119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
28219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          DAG.getConstant(Offset, MVT::i32));
28319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
28419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo(),
28519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            VA.getValVT(), false, false,0);
28619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
28819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    InVals.push_back(Load);
28919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
29019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
29119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (MF.getFunction()->hasStructRetAttr()) {
29219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //Copy the SRet Argument to SRetReturnReg
29319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
29419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Reg = SFI->getSRetReturnReg();
29519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Reg) {
29619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
29719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SFI->setSRetReturnReg(Reg);
29819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
29919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
30019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store remaining ArgRegs to the stack if this is a varargs function.
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isVarArg) {
30519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    static const unsigned ArgRegs[] = {
30619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
30719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    };
30819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
30919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const unsigned *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
31019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned ArgOffset = CCInfo.getNextStackOffset();
31119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (NumAllocated == 6)
31219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ArgOffset += StackOffset;
31319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    else {
31419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(!ArgOffset);
31519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      ArgOffset = 68+4*NumAllocated;
31619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
31719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Remember the vararg offset for the va_start implementation.
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    FuncInfo->setVarArgsFrameOffset(ArgOffset);
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<SDValue> OutChains;
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                          true);
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
33219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
33319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       MachinePointerInfo(),
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       false, false, 0));
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ArgOffset += 4;
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!OutChains.empty()) {
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      OutChains.push_back(Chain);
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          &OutChains[0], OutChains.size());
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Chain;
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               CallingConv::ID CallConv, bool isVarArg,
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               bool &isTailCall,
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const SmallVectorImpl<ISD::OutputArg> &Outs,
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const SmallVectorImpl<SDValue> &OutVals,
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               const SmallVectorImpl<ISD::InputArg> &Ins,
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               DebugLoc dl, SelectionDAG &DAG,
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               SmallVectorImpl<SDValue> &InVals) const {
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc target does not yet support tail call optimization.
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  isTailCall = false;
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Analyze operands of the call, assigning locations to each operand.
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> ArgLocs;
36219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
36319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		 DAG.getTarget(), ArgLocs, *DAG.getContext());
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the size of the outgoing arguments stack space requirement.
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned ArgsSize = CCInfo.getNextStackOffset();
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Keep stack frames 8-byte aligned.
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ArgsSize = (ArgsSize+7) & ~7;
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
37219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
37319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
37419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  //Create local copies for byval args.
37519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<SDValue, 8> ByValArgs;
37619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0,  e = Outs.size(); i != e; ++i) {
37719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ISD::ArgFlagsTy Flags = Outs[i].Flags;
37819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (!Flags.isByVal())
37919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
38019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
38119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Arg = OutVals[i];
38219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Size = Flags.getByValSize();
38319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Align = Flags.getByValAlign();
38419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
38519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    int FI = MFI->CreateStackObject(Size, Align, false);
38619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
38719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
38819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
38919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
39019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          false,        //isVolatile,
39119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          (Size <= 32), //AlwaysInline if size <= 32
39219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                          MachinePointerInfo(), MachinePointerInfo());
39319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ByValArgs.push_back(FIPtr);
39419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
39519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<SDValue, 8> MemOpChains;
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
40119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const unsigned StackOffset = 92;
40219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  bool hasStructRetAttr = false;
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Walk the register/memloc assignments, inserting copies/loads.
40419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
40519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       i != e;
40619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman       ++i, ++realArgIdx) {
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CCValAssign &VA = ArgLocs[i];
40819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Arg = OutVals[realArgIdx];
40919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
41019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
41119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
41219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    //Use local copy if it is a byval arg.
41319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Flags.isByVal())
41419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = ByValArgs[byvalArgIdx++];
415894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Promote the value if needed.
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (VA.getLocInfo()) {
418894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default: llvm_unreachable("Unknown loc info!");
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::Full: break;
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::SExt:
42119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
423894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::ZExt:
42419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
425894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
426894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case CCValAssign::AExt:
42719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
42819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      break;
42919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    case CCValAssign::BCvt:
43019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
431894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      break;
432894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
433894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Flags.isSRet()) {
43519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(VA.needsCustom());
43619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      // store SRet argument in %sp+64
43719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
43819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue PtrOff = DAG.getIntPtrConstant(64);
43919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
44019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
44119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         MachinePointerInfo(),
44219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                         false, false, 0));
44319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      hasStructRetAttr = true;
444894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      continue;
445894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
446894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
44719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VA.needsCustom()) {
44819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      assert(VA.getLocVT() == MVT::f64);
44919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
45019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VA.isMemLoc()) {
45119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned Offset = VA.getLocMemOffset() + StackOffset;
45219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        //if it is double-word aligned, just store.
45319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (Offset % 8 == 0) {
45419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
45519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue PtrOff = DAG.getIntPtrConstant(Offset);
45619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
45719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
45819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             MachinePointerInfo(),
45919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             false, false, 0));
46019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          continue;
46119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
462894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
463894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
464894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
46519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
46619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   Arg, StackPtr, MachinePointerInfo(),
467894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   false, false, 0);
468894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Sparc is big-endian, so the high part comes first.
46919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
47019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MachinePointerInfo(), false, false, 0);
471894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Increment the pointer to the other half.
472894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
473894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getIntPtrConstant(4));
474894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // Load the low part.
47519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
47619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MachinePointerInfo(), false, false, 0);
47719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
47819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VA.isRegLoc()) {
47919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
48019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        assert(i+1 != e);
48119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        CCValAssign &NextVA = ArgLocs[++i];
48219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        if (NextVA.isRegLoc()) {
48319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
48419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        } else {
48519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          //Store the low part in stack.
48619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
48719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
48819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          SDValue PtrOff = DAG.getIntPtrConstant(Offset);
48919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
49019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman          MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
49119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             MachinePointerInfo(),
49219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                             false, false, 0));
49319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        }
494894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      } else {
49519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        unsigned Offset = VA.getLocMemOffset() + StackOffset;
49619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Store the high part.
49719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
49819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        SDValue PtrOff = DAG.getIntPtrConstant(Offset);
49919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
50019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
50119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           MachinePointerInfo(),
50219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           false, false, 0));
50319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        // Store the low part.
50419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        PtrOff = DAG.getIntPtrConstant(Offset+4);
50519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
50619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
50719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           MachinePointerInfo(),
50819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                           false, false, 0));
509894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
51019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
511894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
512894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
51319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Arguments that can be passed on register must be kept at
51419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // RegsToPass vector
51519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (VA.isRegLoc()) {
51619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      if (VA.getLocVT() != MVT::f32) {
51719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
51819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman        continue;
519894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
52019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
52119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
52219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      continue;
523894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
524894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
52519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    assert(VA.isMemLoc());
52619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
52719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // Create a store off the stack pointer for this argument.
52819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
52919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
53019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
53119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
53219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       MachinePointerInfo(),
53319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                       false, false, 0));
534894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
53519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
536894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
537894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Emit all stores, make sure the occur before any copies into physregs.
538894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!MemOpChains.empty())
539894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
540894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        &MemOpChains[0], MemOpChains.size());
541894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
542894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Build a sequence of copy-to-reg nodes chained together with token
543894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // chain and flag operands which copy the outgoing args into registers.
54419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // The InFlag in necessary since all emitted instructions must be
545894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // stuck together.
546894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue InFlag;
547894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
548894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = RegsToPass[i].first;
549894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Remap I0->I7 -> O0->O7.
550894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Reg >= SP::I0 && Reg <= SP::I7)
551894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = Reg-SP::I0+SP::O0;
552894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
553894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
554894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(1);
555894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
556894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
55819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
559894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If the callee is a GlobalAddress node (quite common, every direct call is)
560894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
561894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Likewise ExternalSymbol -> TargetExternalSymbol.
562894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
563894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
564894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
565894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
566894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
56719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Returns a chain & a flag for retval copy to use
56819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
56919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SmallVector<SDValue, 8> Ops;
57019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Ops.push_back(Chain);
57119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Ops.push_back(Callee);
57219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (hasStructRetAttr)
57319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
57419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
57519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    unsigned Reg = RegsToPass[i].first;
57619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    if (Reg >= SP::I0 && Reg <= SP::I7)
57719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      Reg = Reg-SP::I0+SP::O0;
57819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
57919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
58019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
58119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (InFlag.getNode())
58219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    Ops.push_back(InFlag);
58319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
58419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
585894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InFlag = Chain.getValue(1);
586894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
587894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
588894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             DAG.getIntPtrConstant(0, true), InFlag);
589894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InFlag = Chain.getValue(1);
590894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
591894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Assign locations to each value returned by this call.
592894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<CCValAssign, 16> RVLocs;
59319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
59419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman		 DAG.getTarget(), RVLocs, *DAG.getContext());
595894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
596894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
597894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
598894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Copy all of the result registers out of their specified physreg.
599894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0; i != RVLocs.size(); ++i) {
600894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned Reg = RVLocs[i].getLocReg();
601894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
602894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Remap I0->I7 -> O0->O7.
603894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Reg >= SP::I0 && Reg <= SP::I7)
604894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Reg = Reg-SP::I0+SP::O0;
605894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
606894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Chain = DAG.getCopyFromReg(Chain, dl, Reg,
607894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               RVLocs[i].getValVT(), InFlag).getValue(1);
608894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InFlag = Chain.getValue(2);
609894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    InVals.push_back(Chain.getValue(0));
610894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
611894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
612894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return Chain;
613894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
614894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
61519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanunsigned
61619bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
61719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman{
61819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  const Function *CalleeFn = 0;
61919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
62019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CalleeFn = dyn_cast<Function>(G->getGlobal());
62119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  } else if (ExternalSymbolSDNode *E =
62219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman             dyn_cast<ExternalSymbolSDNode>(Callee)) {
62319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const Function *Fn = DAG.getMachineFunction().getFunction();
62419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    const Module *M = Fn->getParent();
62519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CalleeFn = M->getFunction(E->getSymbol());
62619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
62719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
62819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (!CalleeFn)
62919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return 0;
630894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
63119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(CalleeFn->hasStructRetAttr() &&
63219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman         "Callee does not have the StructRet attribute.");
63319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
63419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
63519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Type *ElementTy = Ty->getElementType();
63619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return getTargetData()->getTypeAllocSize(ElementTy);
63719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
638894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
639894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
640894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// TargetLowering Implementation
641894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
642894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
643894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
644894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// condition.
645894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
646894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (CC) {
647894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown integer condition code!");
648894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETEQ:  return SPCC::ICC_E;
649894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETNE:  return SPCC::ICC_NE;
650894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLT:  return SPCC::ICC_L;
651894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGT:  return SPCC::ICC_G;
652894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLE:  return SPCC::ICC_LE;
653894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGE:  return SPCC::ICC_GE;
654894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULT: return SPCC::ICC_CS;
655894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULE: return SPCC::ICC_LEU;
656894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGT: return SPCC::ICC_GU;
657894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGE: return SPCC::ICC_CC;
658894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
659894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
660894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
661894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
662894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// FCC condition.
663894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
664894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (CC) {
665894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown fp condition code!");
666894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETEQ:
667894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOEQ: return SPCC::FCC_E;
668894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETNE:
669894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUNE: return SPCC::FCC_NE;
670894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLT:
671894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLT: return SPCC::FCC_L;
672894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGT:
673894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOGT: return SPCC::FCC_G;
674894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETLE:
675894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOLE: return SPCC::FCC_LE;
676894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETGE:
677894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETOGE: return SPCC::FCC_GE;
678894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULT: return SPCC::FCC_UL;
679894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETULE: return SPCC::FCC_ULE;
680894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGT: return SPCC::FCC_UG;
681894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUGE: return SPCC::FCC_UGE;
682894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUO:  return SPCC::FCC_U;
683894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETO:   return SPCC::FCC_O;
684894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETONE: return SPCC::FCC_LG;
685894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SETUEQ: return SPCC::FCC_UE;
686894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
687894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
688894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
689894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
690894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
691894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
692894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Set up the register classes.
693894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
694894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
695894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
696894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
697894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Turn FP extload into load/fextend
698894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
699894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc doesn't have i1 sign extending load
700894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
701894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Turn FP truncstore into trunc + store.
702894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setTruncStoreAction(MVT::f64, MVT::f32, Expand);
703894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
704894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Custom legalize GlobalAddress nodes into LO/HI parts.
705894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
706894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
707894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
708894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
709894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc doesn't have sext_inreg, replace them with shl/sra
710894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
711894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
712894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
713894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
714894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc has no REM or DIVREM operations.
715894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UREM, MVT::i32, Expand);
716894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SREM, MVT::i32, Expand);
717894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
718894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
719894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
720894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Custom expand fp<->sint
721894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
722894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
723894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
724894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Expand fp<->uint
725894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
726894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
727894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
72819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST, MVT::f32, Expand);
72919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::BITCAST, MVT::i32, Expand);
730894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
731894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc has no select or setcc: expand to SELECT_CC.
732894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT, MVT::i32, Expand);
733894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT, MVT::f32, Expand);
734894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT, MVT::f64, Expand);
735894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC, MVT::i32, Expand);
736894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC, MVT::f32, Expand);
737894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SETCC, MVT::f64, Expand);
738894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
739894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Sparc doesn't have BRCOND either, it has BR_CC.
740894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BRCOND, MVT::Other, Expand);
741894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BRIND, MVT::Other, Expand);
742894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_JT, MVT::Other, Expand);
743894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_CC, MVT::i32, Custom);
744894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_CC, MVT::f32, Custom);
745894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BR_CC, MVT::f64, Custom);
746894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
747894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
748894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
749894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
750894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
75119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: There are instructions available for ATOMIC_FENCE
75219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // on SparcV8 and later.
753894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
75419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
755894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
756894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FSIN , MVT::f64, Expand);
757894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FCOS , MVT::f64, Expand);
758894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FREM , MVT::f64, Expand);
75919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::FMA  , MVT::f64, Expand);
760894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FSIN , MVT::f32, Expand);
761894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FCOS , MVT::f32, Expand);
762894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FREM , MVT::f32, Expand);
76319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setOperationAction(ISD::FMA  , MVT::f32, Expand);
764894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::CTPOP, MVT::i32, Expand);
765894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::CTTZ , MVT::i32, Expand);
766894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::CTLZ , MVT::i32, Expand);
767894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ROTL , MVT::i32, Expand);
768894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::ROTR , MVT::i32, Expand);
769894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::BSWAP, MVT::i32, Expand);
770894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
771894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
772894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FPOW , MVT::f64, Expand);
773894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::FPOW , MVT::f32, Expand);
774894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
775894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
776894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
777894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
778894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
779894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Sparc provides these multiplies, but we don't have them yet.
780894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
781894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
782894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
783894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
784894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
785894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
786894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VASTART           , MVT::Other, Custom);
787894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // VAARG needs to be lowered to not do unaligned accesses for doubles.
788894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VAARG             , MVT::Other, Custom);
789894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
790894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Use the default implementation.
791894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
792894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::VAEND             , MVT::Other, Expand);
793894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::STACKSAVE         , MVT::Other, Expand);
794894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::STACKRESTORE      , MVT::Other, Expand);
795894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Custom);
796894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
797894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // No debug info support yet.
798894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
799894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
800894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  setStackPointerRegisterToSaveRestore(SP::O6);
801894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
802894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (TM.getSubtarget<SparcSubtarget>().isV9())
803894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    setOperationAction(ISD::CTPOP, MVT::i32, Legal);
804894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
80519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  setMinFunctionAlignment(2);
80619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
807894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  computeRegisterProperties();
808894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
809894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
810894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanconst char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
811894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Opcode) {
812894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: return 0;
813894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::CMPICC:     return "SPISD::CMPICC";
814894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::CMPFCC:     return "SPISD::CMPFCC";
815894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::BRICC:      return "SPISD::BRICC";
816894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::BRFCC:      return "SPISD::BRFCC";
817894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
818894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
819894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::Hi:         return "SPISD::Hi";
820894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::Lo:         return "SPISD::Lo";
821894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::FTOI:       return "SPISD::FTOI";
822894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::ITOF:       return "SPISD::ITOF";
823894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::CALL:       return "SPISD::CALL";
824894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::RET_FLAG:   return "SPISD::RET_FLAG";
82519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
82619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case SPISD::FLUSHW:     return "SPISD::FLUSHW";
827894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
828894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
829894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
830894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
831894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// be zero. Op is expected to be a target specific node. Used by DAG
832894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// combiner.
833894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
834894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         const APInt &Mask,
835894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         APInt &KnownZero,
836894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         APInt &KnownOne,
837894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         const SelectionDAG &DAG,
838894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                         unsigned Depth) const {
839894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  APInt KnownZero2, KnownOne2;
840894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);   // Don't know anything.
841894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
842894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
843894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: break;
844894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::SELECT_ICC:
845894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SPISD::SELECT_FCC:
846894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
847894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          Depth+1);
848894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
849894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          Depth+1);
850894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
851894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
852894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
853894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Only known if known in both the LHS and RHS.
854894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownOne &= KnownOne2;
855894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    KnownZero &= KnownZero2;
856894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
857894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
858894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
859894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
860894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// Look at LHS/RHS/CC and see if they are a lowered setcc instruction.  If so
861894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
862894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
863894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                             ISD::CondCode CC, unsigned &SPCC) {
864894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (isa<ConstantSDNode>(RHS) &&
865894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cast<ConstantSDNode>(RHS)->isNullValue() &&
866894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      CC == ISD::SETNE &&
867894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      ((LHS.getOpcode() == SPISD::SELECT_ICC &&
868894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
869894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman       (LHS.getOpcode() == SPISD::SELECT_FCC &&
870894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
871894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<ConstantSDNode>(LHS.getOperand(0)) &&
872894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      isa<ConstantSDNode>(LHS.getOperand(1)) &&
873894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
874894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
875894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue CMPCC = LHS.getOperand(3);
876894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
877894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    LHS = CMPCC.getOperand(0);
878894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    RHS = CMPCC.getOperand(1);
879894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
880894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
881894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
88219bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanSDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
883894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                SelectionDAG &DAG) const {
884894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
885894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME there isn't really any debug info here
886894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
887894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
888894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
889894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
890894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
89119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
892894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
89319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
894894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
895894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   getPointerTy());
896894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
89719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
898894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                GlobalBase, RelAddr);
89919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
90019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     AbsAddr, MachinePointerInfo(), false, false, 0);
901894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
902894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
903894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
904894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                               SelectionDAG &DAG) const {
905894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
906894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME there isn't really any debug info here
907894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
908894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Constant *C = N->getConstVal();
909894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
910894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
911894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
91219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
913894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
914894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
91519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
916894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   getPointerTy());
917894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
918894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
919894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                GlobalBase, RelAddr);
92019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
92119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                     AbsAddr, MachinePointerInfo(), false, false, 0);
922894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
923894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
924894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
925894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
926894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Convert the fp value to integer in an FP register.
927894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getValueType() == MVT::i32);
928894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
92919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
930894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
931894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
932894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
933894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
934894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Op.getOperand(0).getValueType() == MVT::i32);
93519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
936894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Convert the int value to FP in an FP register.
937894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
938894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
939894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
940894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
941894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Op.getOperand(0);
942894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
943894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LHS = Op.getOperand(2);
944894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RHS = Op.getOperand(3);
945894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Dest = Op.getOperand(4);
946894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
947894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc, SPCC = ~0U;
948894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
949894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a br_cc of a "setcc", and if the setcc got lowered into
950894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
951894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LookThroughSetCC(LHS, RHS, CC, SPCC);
952894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
953894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Get the condition flag.
954894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CompareFlag;
955894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LHS.getValueType() == MVT::i32) {
956894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<EVT> VTs;
957894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VTs.push_back(MVT::i32);
95819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VTs.push_back(MVT::Glue);
959894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[2] = { LHS, RHS };
960894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
961894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
962894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = SPISD::BRICC;
963894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
96419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
965894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
966894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = SPISD::BRFCC;
967894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
968894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
969894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getConstant(SPCC, MVT::i32), CompareFlag);
970894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
971894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
972894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
973894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue LHS = Op.getOperand(0);
974894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue RHS = Op.getOperand(1);
975894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
976894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue TrueVal = Op.getOperand(2);
977894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue FalseVal = Op.getOperand(3);
978894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
979894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned Opc, SPCC = ~0U;
980894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
981894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // If this is a select_cc of a "setcc", and if the setcc got lowered into
982894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
983894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  LookThroughSetCC(LHS, RHS, CC, SPCC);
984894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
985894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue CompareFlag;
986894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (LHS.getValueType() == MVT::i32) {
987894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<EVT> VTs;
988894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VTs.push_back(LHS.getValueType());   // subcc returns a value
98919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    VTs.push_back(MVT::Glue);
990894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SDValue Ops[2] = { LHS, RHS };
991894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
992894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = SPISD::SELECT_ICC;
993894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
994894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  } else {
99519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
996894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Opc = SPISD::SELECT_FCC;
997894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
998894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
999894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
1000894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                     DAG.getConstant(SPCC, MVT::i32), CompareFlag);
1001894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1002894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1003894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
1004894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            const SparcTargetLowering &TLI) {
1005894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction &MF = DAG.getMachineFunction();
1006894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
1007894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1008894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // vastart just stores the address of the VarArgsFrameIndex slot into the
1009894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // memory location argument.
1010894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
1011894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Offset =
1012894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DAG.getNode(ISD::ADD, dl, MVT::i32,
1013894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getRegister(SP::I6, MVT::i32),
1014894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
1015894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                MVT::i32));
1016894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
101719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
101819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                      MachinePointerInfo(SV), false, false, 0);
1019894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1020894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1021894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
1022894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDNode *Node = Op.getNode();
1023894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EVT VT = Node->getValueType(0);
1024894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue InChain = Node->getOperand(0);
1025894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue VAListPtr = Node->getOperand(1);
1026894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
1027894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Node->getDebugLoc();
102819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
102919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                               MachinePointerInfo(SV), false, false, 0);
1030894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Increment the pointer, VAList, to the next vaarg
1031894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
1032894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                  DAG.getConstant(VT.getSizeInBits()/8,
1033894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                  MVT::i32));
1034894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Store the incremented VAList to the legalized pointer
1035894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
103619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                         VAListPtr, MachinePointerInfo(SV), false, false, 0);
1037894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Load the actual argument out of the pointer VAList, unless this is an
1038894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // f64 load.
1039894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (VT != MVT::f64)
104019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
104119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                       false, false, 0);
1042894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1043894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Otherwise, load it as i64, then do a bitconvert.
104419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
1045894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                          false, false, 0);
1046894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1047894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Bit-Convert the value to f64.
1048894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[2] = {
104919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
1050894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    V.getValue(1)
1051894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
1052894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getMergeValues(Ops, 2, dl);
1053894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1054894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1055894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstatic SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1056894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Chain = Op.getOperand(0);  // Legalize the chain.
1057894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Size  = Op.getOperand(1);  // Legalize the size.
1058894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = Op.getDebugLoc();
1059894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1060894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned SPReg = SP::O6;
1061894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1062894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
1063894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP);    // Output chain
1064894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1065894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The resultant pointer is actually 16 words from the bottom of the stack,
1066894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to provide a register spill area.
1067894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1068894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 DAG.getConstant(96, MVT::i32));
1069894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SDValue Ops[2] = { NewVal, Chain };
1070894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return DAG.getMergeValues(Ops, 2, dl);
1071894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1072894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1073894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
107419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
107519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
107619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue Chain = DAG.getNode(SPISD::FLUSHW,
107719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              dl, MVT::Other, DAG.getEntryNode());
107819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return Chain;
107919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
108019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
108119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
108219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
108319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MFI->setFrameAddressIsTaken(true);
108419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
108519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
108619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
108719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned FrameReg = SP::I6;
108819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
108919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t depth = Op.getConstantOperandVal(0);
109019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
109119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue FrameAddr;
109219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (depth == 0)
109319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
109419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else {
109519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // flush first to make sure the windowed registers' values are in stack
109619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Chain = getFLUSHW(Op, DAG);
109719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
109819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
109919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (uint64_t i = 0; i != depth; ++i) {
110019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Ptr = DAG.getNode(ISD::ADD,
110119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                dl, MVT::i32,
110219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                FrameAddr, DAG.getIntPtrConstant(56));
110319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      FrameAddr = DAG.getLoad(MVT::i32, dl,
110419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              Chain,
110519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              Ptr,
110619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                              MachinePointerInfo(), false, false, 0);
110719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
110819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
110919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return FrameAddr;
111019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
111119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
111219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanstatic SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
111319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
111419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MFI->setReturnAddressIsTaken(true);
111519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
111619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EVT VT = Op.getValueType();
111719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  DebugLoc dl = Op.getDebugLoc();
111819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  unsigned RetReg = SP::I7;
111919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
112019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  uint64_t depth = Op.getConstantOperandVal(0);
112119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
112219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SDValue RetAddr;
112319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (depth == 0)
112419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
112519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  else {
112619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    // flush first to make sure the windowed registers' values are in stack
112719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SDValue Chain = getFLUSHW(Op, DAG);
112819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
112919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
113019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    for (uint64_t i = 0; i != depth; ++i) {
113119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      SDValue Ptr = DAG.getNode(ISD::ADD,
113219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                dl, MVT::i32,
113319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                RetAddr,
113419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                DAG.getIntPtrConstant((i == depth-1)?60:56));
113519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman      RetAddr = DAG.getLoad(MVT::i32, dl,
113619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            Chain,
113719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            Ptr,
113819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                            MachinePointerInfo(), false, false, 0);
113919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    }
114019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
114119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  return RetAddr;
114219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
114319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
1144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSDValue SparcTargetLowering::
1145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanLowerOperation(SDValue Op, SelectionDAG &DAG) const {
1146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Op.getOpcode()) {
1147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Should not custom lower this!");
114819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
114919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
1150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::GlobalTLSAddress:
1151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    llvm_unreachable("TLS not implemented for Sparc.");
1152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
1153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
1154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG);
1155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
1156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::BR_CC:              return LowerBR_CC(Op, DAG);
1157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::SELECT_CC:          return LowerSELECT_CC(Op, DAG);
1158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VASTART:            return LowerVASTART(Op, DAG, *this);
1159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::VAARG:              return LowerVAARG(Op, DAG);
1160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
1161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanMachineBasicBlock *
1165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
1166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                 MachineBasicBlock *BB) const {
1167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned BROpcode;
1169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  unsigned CC;
1170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DebugLoc dl = MI->getDebugLoc();
1171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Figure out the conditional branch opcode to use for this select_cc.
1172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (MI->getOpcode()) {
1173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  default: llvm_unreachable("Unknown SELECT_CC!");
1174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_Int_ICC:
1175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_FP_ICC:
1176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_DFP_ICC:
1177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BROpcode = SP::BCOND;
1178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_Int_FCC:
1180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_FP_FCC:
1181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case SP::SELECT_CC_DFP_FCC:
1182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    BROpcode = SP::FBCOND;
1183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
1184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
1187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // control-flow pattern.  The incoming instruction knows the destination vreg
1190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to set, the condition code register to branch on, the true/false values to
1191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // select between, and a branch opcode to use.
1192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const BasicBlock *LLVM_BB = BB->getBasicBlock();
1193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction::iterator It = BB;
1194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ++It;
1195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  thisMBB:
1197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ...
1198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   TrueVal = ...
1199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   [f]bCC copy1MBB
1200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   fallthrough --> copy0MBB
1201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *thisMBB = BB;
1202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *F = BB->getParent();
1203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
120519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  F->insert(It, copy0MBB);
120619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  F->insert(It, sinkMBB);
1207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Transfer the remainder of BB and its successor edges to sinkMBB.
1209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sinkMBB->splice(sinkMBB->begin(), BB,
1210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  llvm::next(MachineBasicBlock::iterator(MI)),
1211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  BB->end());
1212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the true and fallthrough blocks as its successors.
1215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->addSuccessor(copy0MBB);
1216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->addSuccessor(sinkMBB);
1217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
1219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  copy0MBB:
1221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   %FalseValue = ...
1222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   # fallthrough to sinkMBB
1223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB = copy0MBB;
1224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update machine-CFG edges
1226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB->addSuccessor(sinkMBB);
1227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  sinkMBB:
1229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  ...
1231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BB = sinkMBB;
1232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
1233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
1235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MI->eraseFromParent();   // The pseudo instruction is gone now.
1237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return BB;
1238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                         Sparc Inline Assembly Support
1242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
1243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// getConstraintType - Given a constraint letter, return the type of
1245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman/// constraint it is for this target.
1246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::ConstraintType
1247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint.size() == 1) {
1249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Constraint[0]) {
1250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    default:  break;
1251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'r': return C_RegisterClass;
1252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::getConstraintType(Constraint);
1256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanstd::pair<unsigned, const TargetRegisterClass*>
1259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                                  EVT VT) const {
1261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Constraint.size() == 1) {
1262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    switch (Constraint[0]) {
1263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    case 'r':
1264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return std::make_pair(0U, SP::IntRegsRegisterClass);
1265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
1266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
1267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
1271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanbool
1272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John BaumanSparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The Sparc target isn't yet aware of offsets.
1274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return false;
1275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
1276