blob: a6b63fbbb8e83cdf29939a6310565dae90a2bfba [file] [log] [blame]
Chris Lattnerd23405e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcISelLowering.h"
16#include "SparcTargetMachine.h"
Dan Gohman1e93df62010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/Function.h"
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +000020#include "llvm/Module.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000029using namespace llvm;
30
Chris Lattner5a65b922008-03-17 05:41:48 +000031
32//===----------------------------------------------------------------------===//
33// Calling Convention Implementation
34//===----------------------------------------------------------------------===//
35
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000036static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
37 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
38 ISD::ArgFlagsTy &ArgFlags, CCState &State)
39{
40 assert (ArgFlags.isSRet());
41
42 //Assign SRet argument
43 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
44 0,
45 LocVT, LocInfo));
46 return true;
47}
48
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000049static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
50 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
51 ISD::ArgFlagsTy &ArgFlags, CCState &State)
52{
53 static const unsigned RegList[] = {
54 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
55 };
56 //Try to get first reg
57 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
58 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
59 } else {
60 //Assign whole thing in stack
61 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
62 State.AllocateStack(8,4),
63 LocVT, LocInfo));
64 return true;
65 }
66
67 //Try to get second reg
68 if (unsigned Reg = State.AllocateReg(RegList, 6))
69 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
70 else
71 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
72 State.AllocateStack(4,4),
73 LocVT, LocInfo));
74 return true;
75}
76
Chris Lattner5a65b922008-03-17 05:41:48 +000077#include "SparcGenCallingConv.inc"
78
Dan Gohman98ca4f22009-08-05 01:29:28 +000079SDValue
80SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000081 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000082 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +000083 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000084 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000085
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000086 MachineFunction &MF = DAG.getMachineFunction();
87
Chris Lattner5a65b922008-03-17 05:41:48 +000088 // CCValAssign - represent the assignment of the return value to locations.
89 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000090
Chris Lattner5a65b922008-03-17 05:41:48 +000091 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +000092 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
93 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000094
Dan Gohman98ca4f22009-08-05 01:29:28 +000095 // Analize return values.
96 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000097
Chris Lattner5a65b922008-03-17 05:41:48 +000098 // If this is the first return lowered for this function, add the regs to the
99 // liveout set for the function.
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000100 if (MF.getRegInfo().liveout_empty()) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000101 for (unsigned i = 0; i != RVLocs.size(); ++i)
102 if (RVLocs[i].isRegLoc())
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000103 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner5a65b922008-03-17 05:41:48 +0000104 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000105
Dan Gohman475871a2008-07-27 21:46:04 +0000106 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +0000107
108 // Copy the result values into the output registers.
109 for (unsigned i = 0; i != RVLocs.size(); ++i) {
110 CCValAssign &VA = RVLocs[i];
111 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +0000112
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000113 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000114 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000115
Chris Lattner5a65b922008-03-17 05:41:48 +0000116 // Guarantee that all emitted copies are stuck together with flags.
117 Flag = Chain.getValue(1);
118 }
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000119
120 unsigned RetAddrOffset = 8; //Call Inst + Delay Slot
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000121 // If the function returns a struct, copy the SRetReturnReg to I0
122 if (MF.getFunction()->hasStructRetAttr()) {
123 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
124 unsigned Reg = SFI->getSRetReturnReg();
125 if (!Reg)
126 llvm_unreachable("sret virtual register not created in the entry block");
127 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
128 Chain = DAG.getCopyToReg(Chain, dl, SP::I0, Val, Flag);
129 Flag = Chain.getValue(1);
130 if (MF.getRegInfo().liveout_empty())
131 MF.getRegInfo().addLiveOut(SP::I0);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000132 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000133 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000134
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000135 SDValue RetAddrOffsetNode = DAG.getConstant(RetAddrOffset, MVT::i32);
136
Gabor Greifba36cb52008-08-28 21:40:38 +0000137 if (Flag.getNode())
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000138 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
139 RetAddrOffsetNode, Flag);
Eric Christopher471e4222011-06-08 23:55:35 +0000140 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000141 RetAddrOffsetNode);
Chris Lattner5a65b922008-03-17 05:41:48 +0000142}
143
Dan Gohman98ca4f22009-08-05 01:29:28 +0000144/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
145/// passed in either one or two GPRs, including FP values. TODO: we should
146/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000147SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000148SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000149 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000150 const SmallVectorImpl<ISD::InputArg>
151 &Ins,
152 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000153 SmallVectorImpl<SDValue> &InVals)
154 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155
Chris Lattner5a65b922008-03-17 05:41:48 +0000156 MachineFunction &MF = DAG.getMachineFunction();
157 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000158 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +0000159
160 // Assign locations to all of the incoming arguments.
161 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000162 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
163 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000164 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000165
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000166 const unsigned StackOffset = 92;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000167
Eli Friedmana786c7b2009-07-19 19:53:46 +0000168 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmana786c7b2009-07-19 19:53:46 +0000169 CCValAssign &VA = ArgLocs[i];
Chris Lattner5a65b922008-03-17 05:41:48 +0000170
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000171 if (i == 0 && Ins[i].Flags.isSRet()) {
172 //Get SRet from [%fp+64]
173 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
174 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
175 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
176 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000177 false, false, false, 0);
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000178 InVals.push_back(Arg);
179 continue;
180 }
181
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000182 if (VA.isRegLoc()) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000183 if (VA.needsCustom()) {
184 assert(VA.getLocVT() == MVT::f64);
185 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
186 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
187 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000188
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000189 assert(i+1 < e);
190 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000191
Dan Gohman475871a2008-07-27 21:46:04 +0000192 SDValue LoVal;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000193 if (NextVA.isMemLoc()) {
194 int FrameIdx = MF.getFrameInfo()->
195 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000196 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000197 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
198 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000199 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000200 } else {
201 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patel68e6bee2011-02-21 23:21:26 +0000202 &SP::IntRegsRegClass);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000203 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000204 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000205 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000206 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000207 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000208 InVals.push_back(WholeValue);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000209 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000210 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000211 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
212 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
213 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
214 if (VA.getLocVT() == MVT::f32)
215 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
216 else if (VA.getLocVT() != MVT::i32) {
217 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
218 DAG.getValueType(VA.getLocVT()));
219 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
220 }
221 InVals.push_back(Arg);
222 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000223 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000224
225 assert(VA.isMemLoc());
226
227 unsigned Offset = VA.getLocMemOffset()+StackOffset;
228
229 if (VA.needsCustom()) {
230 assert(VA.getValVT() == MVT::f64);
231 //If it is double-word aligned, just load.
232 if (Offset % 8 == 0) {
233 int FI = MF.getFrameInfo()->CreateFixedObject(8,
234 Offset,
235 true);
236 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
237 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
238 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000239 false,false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000240 InVals.push_back(Load);
241 continue;
242 }
243
244 int FI = MF.getFrameInfo()->CreateFixedObject(4,
245 Offset,
246 true);
247 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
248 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
249 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000250 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000251 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
252 Offset+4,
253 true);
254 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
255
256 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
257 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000258 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000259
260 SDValue WholeValue =
261 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
262 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
263 InVals.push_back(WholeValue);
264 continue;
265 }
266
267 int FI = MF.getFrameInfo()->CreateFixedObject(4,
268 Offset,
269 true);
270 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
271 SDValue Load ;
272 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
273 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
274 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000275 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000276 } else {
277 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
278 // Sparc is big endian, so add an offset based on the ObjectVT.
279 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
280 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
281 DAG.getConstant(Offset, MVT::i32));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000282 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000283 MachinePointerInfo(),
284 VA.getValVT(), false, false,0);
285 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
286 }
287 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000288 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000289
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000290 if (MF.getFunction()->hasStructRetAttr()) {
291 //Copy the SRet Argument to SRetReturnReg
292 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
293 unsigned Reg = SFI->getSRetReturnReg();
294 if (!Reg) {
295 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
296 SFI->setSRetReturnReg(Reg);
297 }
298 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
299 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
300 }
301
Chris Lattner5a65b922008-03-17 05:41:48 +0000302 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000303 if (isVarArg) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000304 static const unsigned ArgRegs[] = {
305 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
306 };
307 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
308 const unsigned *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
309 unsigned ArgOffset = CCInfo.getNextStackOffset();
310 if (NumAllocated == 6)
311 ArgOffset += StackOffset;
312 else {
313 assert(!ArgOffset);
314 ArgOffset = 68+4*NumAllocated;
315 }
316
Chris Lattner5a65b922008-03-17 05:41:48 +0000317 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000318 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000319
Eli Friedmana786c7b2009-07-19 19:53:46 +0000320 std::vector<SDValue> OutChains;
321
Chris Lattner5a65b922008-03-17 05:41:48 +0000322 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
323 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
324 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000325 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000326
David Greene3f2bf852009-11-12 20:49:22 +0000327 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000328 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000329 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000330
Chris Lattner6229d0a2010-09-21 18:41:36 +0000331 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
332 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000333 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000334 ArgOffset += 4;
335 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000336
337 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000338 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000340 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000341 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000342 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000343
Dan Gohman98ca4f22009-08-05 01:29:28 +0000344 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000345}
346
Dan Gohman98ca4f22009-08-05 01:29:28 +0000347SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000348SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000349 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +0000350 bool doesNotRet, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000351 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000352 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000353 const SmallVectorImpl<ISD::InputArg> &Ins,
354 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000355 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000356 // Sparc target does not yet support tail call optimization.
357 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000358
Chris Lattner315123f2008-03-17 06:58:37 +0000359 // Analyze operands of the call, assigning locations to each operand.
360 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000361 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
362 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000363 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000364
Chris Lattner315123f2008-03-17 06:58:37 +0000365 // Get the size of the outgoing arguments stack space requirement.
366 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000367
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 // Keep stack frames 8-byte aligned.
369 ArgsSize = (ArgsSize+7) & ~7;
370
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000371 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
372
373 //Create local copies for byval args.
374 SmallVector<SDValue, 8> ByValArgs;
375 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
376 ISD::ArgFlagsTy Flags = Outs[i].Flags;
377 if (!Flags.isByVal())
378 continue;
379
380 SDValue Arg = OutVals[i];
381 unsigned Size = Flags.getByValSize();
382 unsigned Align = Flags.getByValAlign();
383
384 int FI = MFI->CreateStackObject(Size, Align, false);
385 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
386 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
387
388 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
389 false, //isVolatile,
390 (Size <= 32), //AlwaysInline if size <= 32
391 MachinePointerInfo(), MachinePointerInfo());
392 ByValArgs.push_back(FIPtr);
393 }
394
Chris Lattnere563bbc2008-10-11 22:08:30 +0000395 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000396
Dan Gohman475871a2008-07-27 21:46:04 +0000397 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
398 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000399
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000400 const unsigned StackOffset = 92;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000401 bool hasStructRetAttr = false;
Chris Lattner315123f2008-03-17 06:58:37 +0000402 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000403 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000404 i != e;
405 ++i, ++realArgIdx) {
Chris Lattner315123f2008-03-17 06:58:37 +0000406 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000407 SDValue Arg = OutVals[realArgIdx];
Chris Lattner315123f2008-03-17 06:58:37 +0000408
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000409 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
410
411 //Use local copy if it is a byval arg.
412 if (Flags.isByVal())
413 Arg = ByValArgs[byvalArgIdx++];
414
Chris Lattner315123f2008-03-17 06:58:37 +0000415 // Promote the value if needed.
416 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000417 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000418 case CCValAssign::Full: break;
419 case CCValAssign::SExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000420 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000421 break;
422 case CCValAssign::ZExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000423 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000424 break;
425 case CCValAssign::AExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000426 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
427 break;
428 case CCValAssign::BCvt:
429 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000430 break;
431 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000432
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000433 if (Flags.isSRet()) {
434 assert(VA.needsCustom());
435 // store SRet argument in %sp+64
436 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
437 SDValue PtrOff = DAG.getIntPtrConstant(64);
438 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
439 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
440 MachinePointerInfo(),
441 false, false, 0));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000442 hasStructRetAttr = true;
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000443 continue;
444 }
445
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000446 if (VA.needsCustom()) {
447 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000448
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000449 if (VA.isMemLoc()) {
450 unsigned Offset = VA.getLocMemOffset() + StackOffset;
451 //if it is double-word aligned, just store.
452 if (Offset % 8 == 0) {
453 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
454 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
455 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
456 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
457 MachinePointerInfo(),
458 false, false, 0));
459 continue;
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000460 }
461 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000462
Owen Anderson825b72b2009-08-11 20:47:22 +0000463 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000464 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000465 Arg, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000466 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000467 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000468 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000469 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000470 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000471 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000472 DAG.getIntPtrConstant(4));
473 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000474 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000475 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000476
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000477 if (VA.isRegLoc()) {
478 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
479 assert(i+1 != e);
480 CCValAssign &NextVA = ArgLocs[++i];
481 if (NextVA.isRegLoc()) {
482 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
483 } else {
484 //Store the low part in stack.
485 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
486 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
487 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
488 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
489 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
490 MachinePointerInfo(),
491 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000492 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000493 } else {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000494 unsigned Offset = VA.getLocMemOffset() + StackOffset;
495 // Store the high part.
496 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
497 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
498 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
499 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
500 MachinePointerInfo(),
501 false, false, 0));
502 // Store the low part.
503 PtrOff = DAG.getIntPtrConstant(Offset+4);
504 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
505 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
506 MachinePointerInfo(),
507 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000508 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000509 continue;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000510 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000511
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000512 // Arguments that can be passed on register must be kept at
513 // RegsToPass vector
514 if (VA.isRegLoc()) {
515 if (VA.getLocVT() != MVT::f32) {
516 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
517 continue;
518 }
519 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
520 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
521 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000522 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000523
524 assert(VA.isMemLoc());
525
526 // Create a store off the stack pointer for this argument.
527 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
528 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
529 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
530 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
531 MachinePointerInfo(),
532 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000533 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000534
Anton Korobeynikov53835702008-10-10 20:27:31 +0000535
Chris Lattner5a65b922008-03-17 05:41:48 +0000536 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000537 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000538 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000539 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000540
541 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000542 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000543 // The InFlag in necessary since all emitted instructions must be
Chris Lattner315123f2008-03-17 06:58:37 +0000544 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000545 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000546 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
547 unsigned Reg = RegsToPass[i].first;
548 // Remap I0->I7 -> O0->O7.
549 if (Reg >= SP::I0 && Reg <= SP::I7)
550 Reg = Reg-SP::I0+SP::O0;
551
Dale Johannesen33c960f2009-02-04 20:06:27 +0000552 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000553 InFlag = Chain.getValue(1);
554 }
555
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000556 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
557
Chris Lattner5a65b922008-03-17 05:41:48 +0000558 // If the callee is a GlobalAddress node (quite common, every direct call is)
559 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000560 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000561 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000562 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000563 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000564 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000565
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000566 // Returns a chain & a flag for retval copy to use
567 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
568 SmallVector<SDValue, 8> Ops;
569 Ops.push_back(Chain);
570 Ops.push_back(Callee);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000571 if (hasStructRetAttr)
572 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
574 unsigned Reg = RegsToPass[i].first;
575 if (Reg >= SP::I0 && Reg <= SP::I7)
576 Reg = Reg-SP::I0+SP::O0;
577
578 Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
579 }
580 if (InFlag.getNode())
581 Ops.push_back(InFlag);
582
583 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000584 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000585
Chris Lattnere563bbc2008-10-11 22:08:30 +0000586 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
587 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000588 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000589
Chris Lattner98949a62008-03-17 06:01:07 +0000590 // Assign locations to each value returned by this call.
591 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000592 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
593 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000594
Dan Gohman98ca4f22009-08-05 01:29:28 +0000595 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000596
Chris Lattner98949a62008-03-17 06:01:07 +0000597 // Copy all of the result registers out of their specified physreg.
598 for (unsigned i = 0; i != RVLocs.size(); ++i) {
599 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000600
Chris Lattner98949a62008-03-17 06:01:07 +0000601 // Remap I0->I7 -> O0->O7.
602 if (Reg >= SP::I0 && Reg <= SP::I7)
603 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000604
Dale Johannesen33c960f2009-02-04 20:06:27 +0000605 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000606 RVLocs[i].getValVT(), InFlag).getValue(1);
607 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000608 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000609 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000610
Dan Gohman98ca4f22009-08-05 01:29:28 +0000611 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000612}
613
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000614unsigned
615SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
616{
617 const Function *CalleeFn = 0;
618 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
619 CalleeFn = dyn_cast<Function>(G->getGlobal());
620 } else if (ExternalSymbolSDNode *E =
621 dyn_cast<ExternalSymbolSDNode>(Callee)) {
622 const Function *Fn = DAG.getMachineFunction().getFunction();
623 const Module *M = Fn->getParent();
624 CalleeFn = M->getFunction(E->getSymbol());
625 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000626
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000627 if (!CalleeFn)
628 return 0;
629
630 assert(CalleeFn->hasStructRetAttr() &&
631 "Callee does not have the StructRet attribute.");
632
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000633 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
634 Type *ElementTy = Ty->getElementType();
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000635 return getTargetData()->getTypeAllocSize(ElementTy);
636}
Chris Lattner5a65b922008-03-17 05:41:48 +0000637
Chris Lattnerd23405e2008-03-17 03:21:36 +0000638//===----------------------------------------------------------------------===//
639// TargetLowering Implementation
640//===----------------------------------------------------------------------===//
641
642/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
643/// condition.
644static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
645 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000646 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000647 case ISD::SETEQ: return SPCC::ICC_E;
648 case ISD::SETNE: return SPCC::ICC_NE;
649 case ISD::SETLT: return SPCC::ICC_L;
650 case ISD::SETGT: return SPCC::ICC_G;
651 case ISD::SETLE: return SPCC::ICC_LE;
652 case ISD::SETGE: return SPCC::ICC_GE;
653 case ISD::SETULT: return SPCC::ICC_CS;
654 case ISD::SETULE: return SPCC::ICC_LEU;
655 case ISD::SETUGT: return SPCC::ICC_GU;
656 case ISD::SETUGE: return SPCC::ICC_CC;
657 }
658}
659
660/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
661/// FCC condition.
662static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
663 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000664 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000665 case ISD::SETEQ:
666 case ISD::SETOEQ: return SPCC::FCC_E;
667 case ISD::SETNE:
668 case ISD::SETUNE: return SPCC::FCC_NE;
669 case ISD::SETLT:
670 case ISD::SETOLT: return SPCC::FCC_L;
671 case ISD::SETGT:
672 case ISD::SETOGT: return SPCC::FCC_G;
673 case ISD::SETLE:
674 case ISD::SETOLE: return SPCC::FCC_LE;
675 case ISD::SETGE:
676 case ISD::SETOGE: return SPCC::FCC_GE;
677 case ISD::SETULT: return SPCC::FCC_UL;
678 case ISD::SETULE: return SPCC::FCC_ULE;
679 case ISD::SETUGT: return SPCC::FCC_UG;
680 case ISD::SETUGE: return SPCC::FCC_UGE;
681 case ISD::SETUO: return SPCC::FCC_U;
682 case ISD::SETO: return SPCC::FCC_O;
683 case ISD::SETONE: return SPCC::FCC_LG;
684 case ISD::SETUEQ: return SPCC::FCC_UE;
685 }
686}
687
Chris Lattnerd23405e2008-03-17 03:21:36 +0000688SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000689 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000690
Chris Lattnerd23405e2008-03-17 03:21:36 +0000691 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000692 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
693 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
694 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000695
696 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000698 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000700 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000702
703 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000704 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
705 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
706 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000707
Chris Lattnerd23405e2008-03-17 03:21:36 +0000708 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
710 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
711 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000712
713 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 setOperationAction(ISD::UREM, MVT::i32, Expand);
715 setOperationAction(ISD::SREM, MVT::i32, Expand);
716 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
717 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000718
719 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000720 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
721 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000722
723 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000724 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
725 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000726
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000727 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
728 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000729
Chris Lattnerd23405e2008-03-17 03:21:36 +0000730 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000731 setOperationAction(ISD::SELECT, MVT::i32, Expand);
732 setOperationAction(ISD::SELECT, MVT::f32, Expand);
733 setOperationAction(ISD::SELECT, MVT::f64, Expand);
734 setOperationAction(ISD::SETCC, MVT::i32, Expand);
735 setOperationAction(ISD::SETCC, MVT::f32, Expand);
736 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000737
Chris Lattnerd23405e2008-03-17 03:21:36 +0000738 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000739 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
740 setOperationAction(ISD::BRIND, MVT::Other, Expand);
741 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
742 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
743 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
744 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000745
Owen Anderson825b72b2009-08-11 20:47:22 +0000746 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
747 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
748 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000749
Eli Friedman14648462011-07-27 22:21:52 +0000750 // FIXME: There are instructions available for ATOMIC_FENCE
751 // on SparcV8 and later.
Owen Anderson825b72b2009-08-11 20:47:22 +0000752 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000753 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000754
Owen Anderson825b72b2009-08-11 20:47:22 +0000755 setOperationAction(ISD::FSIN , MVT::f64, Expand);
756 setOperationAction(ISD::FCOS , MVT::f64, Expand);
757 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000758 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000759 setOperationAction(ISD::FSIN , MVT::f32, Expand);
760 setOperationAction(ISD::FCOS , MVT::f32, Expand);
761 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000762 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000763 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
764 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000765 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000766 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000767 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000768 setOperationAction(ISD::ROTL , MVT::i32, Expand);
769 setOperationAction(ISD::ROTR , MVT::i32, Expand);
770 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
771 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
772 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
773 setOperationAction(ISD::FPOW , MVT::f64, Expand);
774 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000775
Owen Anderson825b72b2009-08-11 20:47:22 +0000776 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
777 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
778 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000779
780 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000781 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
782 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000783
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000785
Chris Lattnerd23405e2008-03-17 03:21:36 +0000786 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000788 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000790
Chris Lattnerd23405e2008-03-17 03:21:36 +0000791 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000792 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
793 setOperationAction(ISD::VAEND , MVT::Other, Expand);
794 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
795 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
796 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000797
798 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000799 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000800
Chris Lattnerd23405e2008-03-17 03:21:36 +0000801 setStackPointerRegisterToSaveRestore(SP::O6);
802
803 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000804 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000805
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000806 setMinFunctionAlignment(2);
807
Chris Lattnerd23405e2008-03-17 03:21:36 +0000808 computeRegisterProperties();
809}
810
811const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
812 switch (Opcode) {
813 default: return 0;
814 case SPISD::CMPICC: return "SPISD::CMPICC";
815 case SPISD::CMPFCC: return "SPISD::CMPFCC";
816 case SPISD::BRICC: return "SPISD::BRICC";
817 case SPISD::BRFCC: return "SPISD::BRFCC";
818 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
819 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
820 case SPISD::Hi: return "SPISD::Hi";
821 case SPISD::Lo: return "SPISD::Lo";
822 case SPISD::FTOI: return "SPISD::FTOI";
823 case SPISD::ITOF: return "SPISD::ITOF";
824 case SPISD::CALL: return "SPISD::CALL";
825 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +0000826 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +0000827 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Chris Lattnerd23405e2008-03-17 03:21:36 +0000828 }
829}
830
831/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
832/// be zero. Op is expected to be a target specific node. Used by DAG
833/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000834void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000835 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000836 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837 APInt &KnownOne,
838 const SelectionDAG &DAG,
839 unsigned Depth) const {
840 APInt KnownZero2, KnownOne2;
841 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000842
Chris Lattnerd23405e2008-03-17 03:21:36 +0000843 switch (Op.getOpcode()) {
844 default: break;
845 case SPISD::SELECT_ICC:
846 case SPISD::SELECT_FCC:
847 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
848 Depth+1);
849 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
850 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000851 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
852 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
853
Chris Lattnerd23405e2008-03-17 03:21:36 +0000854 // Only known if known in both the LHS and RHS.
855 KnownOne &= KnownOne2;
856 KnownZero &= KnownZero2;
857 break;
858 }
859}
860
Chris Lattnerd23405e2008-03-17 03:21:36 +0000861// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
862// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000863static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000864 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000865 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000866 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000867 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000868 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
869 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
870 (LHS.getOpcode() == SPISD::SELECT_FCC &&
871 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
872 isa<ConstantSDNode>(LHS.getOperand(0)) &&
873 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000874 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
875 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000876 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000877 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000878 LHS = CMPCC.getOperand(0);
879 RHS = CMPCC.getOperand(1);
880 }
881}
882
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000883SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000884 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000885 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000886 // FIXME there isn't really any debug info here
887 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000888 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
890 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000891
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000892 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000893 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000894
Chris Lattnerdb486a62009-09-15 17:46:24 +0000895 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
896 getPointerTy());
897 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000898 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000899 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000900 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000901 AbsAddr, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000902}
903
Chris Lattnerdb486a62009-09-15 17:46:24 +0000904SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000905 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000906 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000907 // FIXME there isn't really any debug info here
908 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000909 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
911 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
912 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000913 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000914 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
915
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000916 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000917 getPointerTy());
918 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
919 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
920 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000921 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000922 AbsAddr, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000923}
924
Dan Gohman475871a2008-07-27 21:46:04 +0000925static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000926 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000927 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000928 assert(Op.getValueType() == MVT::i32);
929 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000930 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000931}
932
Dan Gohman475871a2008-07-27 21:46:04 +0000933static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000934 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000935 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000936 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000937 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000938 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000939}
940
Dan Gohman475871a2008-07-27 21:46:04 +0000941static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
942 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000943 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SDValue LHS = Op.getOperand(2);
945 SDValue RHS = Op.getOperand(3);
946 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000947 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000948 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000949
Chris Lattnerd23405e2008-03-17 03:21:36 +0000950 // If this is a br_cc of a "setcc", and if the setcc got lowered into
951 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
952 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000953
Chris Lattnerd23405e2008-03-17 03:21:36 +0000954 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000956 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000957 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 VTs.push_back(MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000959 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000961 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000962 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
963 Opc = SPISD::BRICC;
964 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000965 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000966 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
967 Opc = SPISD::BRFCC;
968 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
970 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000971}
972
Dan Gohman475871a2008-07-27 21:46:04 +0000973static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
974 SDValue LHS = Op.getOperand(0);
975 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000976 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000977 SDValue TrueVal = Op.getOperand(2);
978 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000979 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000980 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000981
Chris Lattnerd23405e2008-03-17 03:21:36 +0000982 // If this is a select_cc of a "setcc", and if the setcc got lowered into
983 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
984 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000985
Dan Gohman475871a2008-07-27 21:46:04 +0000986 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000987 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000988 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000989 VTs.push_back(LHS.getValueType()); // subcc returns a value
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000990 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000991 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000992 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000993 Opc = SPISD::SELECT_ICC;
994 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
995 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000996 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000997 Opc = SPISD::SELECT_FCC;
998 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
999 }
Dale Johannesen3484c092009-02-05 22:07:54 +00001000 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +00001001 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001002}
1003
Dan Gohman475871a2008-07-27 21:46:04 +00001004static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001005 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001006 MachineFunction &MF = DAG.getMachineFunction();
1007 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
1008
Chris Lattnerd23405e2008-03-17 03:21:36 +00001009 // vastart just stores the address of the VarArgsFrameIndex slot into the
1010 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001011 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001012 SDValue Offset =
1013 DAG.getNode(ISD::ADD, dl, MVT::i32,
1014 DAG.getRegister(SP::I6, MVT::i32),
1015 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
1016 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001017 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001018 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
1019 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001020}
1021
Dan Gohman475871a2008-07-27 21:46:04 +00001022static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001023 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001024 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001025 SDValue InChain = Node->getOperand(0);
1026 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001027 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001028 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001029 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001030 MachinePointerInfo(SV), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001031 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001033 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +00001034 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001035 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001036 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00001037 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001038 // Load the actual argument out of the pointer VAList, unless this is an
1039 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +00001040 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001041 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001042 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001043
Chris Lattnerd23405e2008-03-17 03:21:36 +00001044 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001045 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001046 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001047
Chris Lattnerd23405e2008-03-17 03:21:36 +00001048 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001049 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001050 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +00001051 V.getValue(1)
1052 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00001053 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001054}
1055
Dan Gohman475871a2008-07-27 21:46:04 +00001056static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1057 SDValue Chain = Op.getOperand(0); // Legalize the chain.
1058 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +00001059 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001060
Chris Lattnerd23405e2008-03-17 03:21:36 +00001061 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +00001062 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1063 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +00001064 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +00001065
Chris Lattnerd23405e2008-03-17 03:21:36 +00001066 // The resultant pointer is actually 16 words from the bottom of the stack,
1067 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +00001068 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1069 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001070 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +00001071 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001072}
1073
Chris Lattnerd23405e2008-03-17 03:21:36 +00001074
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001075static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001076 DebugLoc dl = Op.getDebugLoc();
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001077 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001078 dl, MVT::Other, DAG.getEntryNode());
1079 return Chain;
1080}
1081
1082static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1083 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1084 MFI->setFrameAddressIsTaken(true);
1085
1086 EVT VT = Op.getValueType();
1087 DebugLoc dl = Op.getDebugLoc();
1088 unsigned FrameReg = SP::I6;
1089
1090 uint64_t depth = Op.getConstantOperandVal(0);
1091
1092 SDValue FrameAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001093 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001094 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1095 else {
1096 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001097 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001098 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001099
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001100 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001101 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001102 dl, MVT::i32,
1103 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001104 FrameAddr = DAG.getLoad(MVT::i32, dl,
1105 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001106 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001107 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001108 }
1109 }
1110 return FrameAddr;
1111}
1112
1113static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
1114 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1115 MFI->setReturnAddressIsTaken(true);
1116
1117 EVT VT = Op.getValueType();
1118 DebugLoc dl = Op.getDebugLoc();
1119 unsigned RetReg = SP::I7;
1120
1121 uint64_t depth = Op.getConstantOperandVal(0);
1122
1123 SDValue RetAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001124 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001125 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
1126 else {
1127 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001128 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001129 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001130
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001131 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001132 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001133 dl, MVT::i32,
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001134 RetAddr,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001135 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001136 RetAddr = DAG.getLoad(MVT::i32, dl,
1137 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001138 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001139 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001140 }
1141 }
1142 return RetAddr;
1143}
1144
Dan Gohman475871a2008-07-27 21:46:04 +00001145SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001146LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001147 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001148 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001149 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1150 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001151 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001152 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001153 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1154 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001155 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1156 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1157 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1158 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1159 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1160 case ISD::VAARG: return LowerVAARG(Op, DAG);
1161 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001162 }
1163}
1164
1165MachineBasicBlock *
1166SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001167 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001168 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1169 unsigned BROpcode;
1170 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001171 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001172 // Figure out the conditional branch opcode to use for this select_cc.
1173 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001174 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001175 case SP::SELECT_CC_Int_ICC:
1176 case SP::SELECT_CC_FP_ICC:
1177 case SP::SELECT_CC_DFP_ICC:
1178 BROpcode = SP::BCOND;
1179 break;
1180 case SP::SELECT_CC_Int_FCC:
1181 case SP::SELECT_CC_FP_FCC:
1182 case SP::SELECT_CC_DFP_FCC:
1183 BROpcode = SP::FBCOND;
1184 break;
1185 }
1186
1187 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001188
Chris Lattnerd23405e2008-03-17 03:21:36 +00001189 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1190 // control-flow pattern. The incoming instruction knows the destination vreg
1191 // to set, the condition code register to branch on, the true/false values to
1192 // select between, and a branch opcode to use.
1193 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001194 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001195 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001196
Chris Lattnerd23405e2008-03-17 03:21:36 +00001197 // thisMBB:
1198 // ...
1199 // TrueVal = ...
1200 // [f]bCC copy1MBB
1201 // fallthrough --> copy0MBB
1202 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001203 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001204 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1205 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001206 F->insert(It, copy0MBB);
1207 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001208
1209 // Transfer the remainder of BB and its successor edges to sinkMBB.
1210 sinkMBB->splice(sinkMBB->begin(), BB,
1211 llvm::next(MachineBasicBlock::iterator(MI)),
1212 BB->end());
1213 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1214
1215 // Add the true and fallthrough blocks as its successors.
1216 BB->addSuccessor(copy0MBB);
1217 BB->addSuccessor(sinkMBB);
1218
Dale Johannesend552eee2009-02-13 02:31:35 +00001219 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001220
Chris Lattnerd23405e2008-03-17 03:21:36 +00001221 // copy0MBB:
1222 // %FalseValue = ...
1223 // # fallthrough to sinkMBB
1224 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001225
Chris Lattnerd23405e2008-03-17 03:21:36 +00001226 // Update machine-CFG edges
1227 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001228
Chris Lattnerd23405e2008-03-17 03:21:36 +00001229 // sinkMBB:
1230 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1231 // ...
1232 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001233 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001234 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1235 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001236
Dan Gohman14152b42010-07-06 20:24:04 +00001237 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001238 return BB;
1239}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001240
1241//===----------------------------------------------------------------------===//
1242// Sparc Inline Assembly Support
1243//===----------------------------------------------------------------------===//
1244
1245/// getConstraintType - Given a constraint letter, return the type of
1246/// constraint it is for this target.
1247SparcTargetLowering::ConstraintType
1248SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1249 if (Constraint.size() == 1) {
1250 switch (Constraint[0]) {
1251 default: break;
1252 case 'r': return C_RegisterClass;
1253 }
1254 }
1255
1256 return TargetLowering::getConstraintType(Constraint);
1257}
1258
1259std::pair<unsigned, const TargetRegisterClass*>
1260SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001261 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001262 if (Constraint.size() == 1) {
1263 switch (Constraint[0]) {
1264 case 'r':
1265 return std::make_pair(0U, SP::IntRegsRegisterClass);
1266 }
1267 }
1268
1269 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1270}
1271
Dan Gohman6520e202008-10-18 02:06:02 +00001272bool
1273SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1274 // The Sparc target isn't yet aware of offsets.
1275 return false;
1276}