blob: 3608d3b54280e07ce585655696979f8e4de8475d [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"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000028#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000030using namespace llvm;
31
Chris Lattner5a65b922008-03-17 05:41:48 +000032
33//===----------------------------------------------------------------------===//
34// Calling Convention Implementation
35//===----------------------------------------------------------------------===//
36
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000037static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
38 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
39 ISD::ArgFlagsTy &ArgFlags, CCState &State)
40{
41 assert (ArgFlags.isSRet());
42
43 //Assign SRet argument
44 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
45 0,
46 LocVT, LocInfo));
47 return true;
48}
49
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000050static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
51 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
52 ISD::ArgFlagsTy &ArgFlags, CCState &State)
53{
54 static const unsigned RegList[] = {
55 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
56 };
57 //Try to get first reg
58 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
59 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
60 } else {
61 //Assign whole thing in stack
62 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
63 State.AllocateStack(8,4),
64 LocVT, LocInfo));
65 return true;
66 }
67
68 //Try to get second reg
69 if (unsigned Reg = State.AllocateReg(RegList, 6))
70 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
71 else
72 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
73 State.AllocateStack(4,4),
74 LocVT, LocInfo));
75 return true;
76}
77
Chris Lattner5a65b922008-03-17 05:41:48 +000078#include "SparcGenCallingConv.inc"
79
Dan Gohman98ca4f22009-08-05 01:29:28 +000080SDValue
81SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000082 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000083 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +000084 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000085 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000086
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +000087 MachineFunction &MF = DAG.getMachineFunction();
88
Chris Lattner5a65b922008-03-17 05:41:48 +000089 // CCValAssign - represent the assignment of the return value to locations.
90 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000091
Chris Lattner5a65b922008-03-17 05:41:48 +000092 // CCState - Info about the registers and stack slot.
Eric Christopher471e4222011-06-08 23:55:35 +000093 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
94 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000095
Dan Gohman98ca4f22009-08-05 01:29:28 +000096 // Analize return values.
97 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000098
Chris Lattner5a65b922008-03-17 05:41:48 +000099 // If this is the first return lowered for this function, add the regs to the
100 // liveout set for the function.
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000101 if (MF.getRegInfo().liveout_empty()) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000102 for (unsigned i = 0; i != RVLocs.size(); ++i)
103 if (RVLocs[i].isRegLoc())
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000104 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner5a65b922008-03-17 05:41:48 +0000105 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000106
Dan Gohman475871a2008-07-27 21:46:04 +0000107 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +0000108
109 // Copy the result values into the output registers.
110 for (unsigned i = 0; i != RVLocs.size(); ++i) {
111 CCValAssign &VA = RVLocs[i];
112 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +0000113
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000114 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000115 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000116
Chris Lattner5a65b922008-03-17 05:41:48 +0000117 // Guarantee that all emitted copies are stuck together with flags.
118 Flag = Chain.getValue(1);
119 }
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000120
121 unsigned RetAddrOffset = 8; //Call Inst + Delay Slot
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000122 // If the function returns a struct, copy the SRetReturnReg to I0
123 if (MF.getFunction()->hasStructRetAttr()) {
124 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
125 unsigned Reg = SFI->getSRetReturnReg();
126 if (!Reg)
127 llvm_unreachable("sret virtual register not created in the entry block");
128 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
129 Chain = DAG.getCopyToReg(Chain, dl, SP::I0, Val, Flag);
130 Flag = Chain.getValue(1);
131 if (MF.getRegInfo().liveout_empty())
132 MF.getRegInfo().addLiveOut(SP::I0);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000133 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000134 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000135
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000136 SDValue RetAddrOffsetNode = DAG.getConstant(RetAddrOffset, MVT::i32);
137
Gabor Greifba36cb52008-08-28 21:40:38 +0000138 if (Flag.getNode())
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000139 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
140 RetAddrOffsetNode, Flag);
Eric Christopher471e4222011-06-08 23:55:35 +0000141 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain,
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000142 RetAddrOffsetNode);
Chris Lattner5a65b922008-03-17 05:41:48 +0000143}
144
Dan Gohman98ca4f22009-08-05 01:29:28 +0000145/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
146/// passed in either one or two GPRs, including FP values. TODO: we should
147/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000148SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000149SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000150 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000151 const SmallVectorImpl<ISD::InputArg>
152 &Ins,
153 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000154 SmallVectorImpl<SDValue> &InVals)
155 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000156
Chris Lattner5a65b922008-03-17 05:41:48 +0000157 MachineFunction &MF = DAG.getMachineFunction();
158 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000159 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +0000160
161 // Assign locations to all of the incoming arguments.
162 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000163 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
164 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000165 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000166
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000167 const unsigned StackOffset = 92;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000168
Eli Friedmana786c7b2009-07-19 19:53:46 +0000169 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmana786c7b2009-07-19 19:53:46 +0000170 CCValAssign &VA = ArgLocs[i];
Chris Lattner5a65b922008-03-17 05:41:48 +0000171
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000172 if (i == 0 && Ins[i].Flags.isSRet()) {
173 //Get SRet from [%fp+64]
174 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
175 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
176 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
177 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000178 false, false, false, 0);
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000179 InVals.push_back(Arg);
180 continue;
181 }
182
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000183 if (VA.isRegLoc()) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000184 if (VA.needsCustom()) {
185 assert(VA.getLocVT() == MVT::f64);
186 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
187 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
188 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000189
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000190 assert(i+1 < e);
191 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000192
Dan Gohman475871a2008-07-27 21:46:04 +0000193 SDValue LoVal;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000194 if (NextVA.isMemLoc()) {
195 int FrameIdx = MF.getFrameInfo()->
196 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000198 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
199 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000200 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000201 } else {
202 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patel68e6bee2011-02-21 23:21:26 +0000203 &SP::IntRegsRegClass);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000204 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000206 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000208 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000209 InVals.push_back(WholeValue);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000210 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000211 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000212 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
213 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
214 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
215 if (VA.getLocVT() == MVT::f32)
216 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
217 else if (VA.getLocVT() != MVT::i32) {
218 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
219 DAG.getValueType(VA.getLocVT()));
220 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
221 }
222 InVals.push_back(Arg);
223 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000224 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000225
226 assert(VA.isMemLoc());
227
228 unsigned Offset = VA.getLocMemOffset()+StackOffset;
229
230 if (VA.needsCustom()) {
231 assert(VA.getValVT() == MVT::f64);
232 //If it is double-word aligned, just load.
233 if (Offset % 8 == 0) {
234 int FI = MF.getFrameInfo()->CreateFixedObject(8,
235 Offset,
236 true);
237 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
238 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
239 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000240 false,false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000241 InVals.push_back(Load);
242 continue;
243 }
244
245 int FI = MF.getFrameInfo()->CreateFixedObject(4,
246 Offset,
247 true);
248 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
249 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
250 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000251 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000252 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
253 Offset+4,
254 true);
255 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
256
257 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
258 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000259 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000260
261 SDValue WholeValue =
262 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
263 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
264 InVals.push_back(WholeValue);
265 continue;
266 }
267
268 int FI = MF.getFrameInfo()->CreateFixedObject(4,
269 Offset,
270 true);
271 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
272 SDValue Load ;
273 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
274 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
275 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000276 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000277 } else {
278 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
279 // Sparc is big endian, so add an offset based on the ObjectVT.
280 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
281 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
282 DAG.getConstant(Offset, MVT::i32));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000283 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000284 MachinePointerInfo(),
285 VA.getValVT(), false, false,0);
286 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
287 }
288 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000289 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000290
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000291 if (MF.getFunction()->hasStructRetAttr()) {
292 //Copy the SRet Argument to SRetReturnReg
293 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
294 unsigned Reg = SFI->getSRetReturnReg();
295 if (!Reg) {
296 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
297 SFI->setSRetReturnReg(Reg);
298 }
299 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
300 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
301 }
302
Chris Lattner5a65b922008-03-17 05:41:48 +0000303 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000304 if (isVarArg) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000305 static const unsigned ArgRegs[] = {
306 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
307 };
308 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
309 const unsigned *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
310 unsigned ArgOffset = CCInfo.getNextStackOffset();
311 if (NumAllocated == 6)
312 ArgOffset += StackOffset;
313 else {
314 assert(!ArgOffset);
315 ArgOffset = 68+4*NumAllocated;
316 }
317
Chris Lattner5a65b922008-03-17 05:41:48 +0000318 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000319 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000320
Eli Friedmana786c7b2009-07-19 19:53:46 +0000321 std::vector<SDValue> OutChains;
322
Chris Lattner5a65b922008-03-17 05:41:48 +0000323 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
324 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
325 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000327
David Greene3f2bf852009-11-12 20:49:22 +0000328 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000329 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000330 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000331
Chris Lattner6229d0a2010-09-21 18:41:36 +0000332 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
333 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000334 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000335 ArgOffset += 4;
336 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000337
338 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000339 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000340 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000341 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000342 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000343 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000344
Dan Gohman98ca4f22009-08-05 01:29:28 +0000345 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000346}
347
Dan Gohman98ca4f22009-08-05 01:29:28 +0000348SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000349SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000350 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000351 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000352 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000353 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000354 const SmallVectorImpl<ISD::InputArg> &Ins,
355 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000356 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000357 // Sparc target does not yet support tail call optimization.
358 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000359
Chris Lattner315123f2008-03-17 06:58:37 +0000360 // Analyze operands of the call, assigning locations to each operand.
361 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000362 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
363 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000364 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000365
Chris Lattner315123f2008-03-17 06:58:37 +0000366 // Get the size of the outgoing arguments stack space requirement.
367 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000368
Chris Lattner5a65b922008-03-17 05:41:48 +0000369 // Keep stack frames 8-byte aligned.
370 ArgsSize = (ArgsSize+7) & ~7;
371
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000372 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
373
374 //Create local copies for byval args.
375 SmallVector<SDValue, 8> ByValArgs;
376 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
377 ISD::ArgFlagsTy Flags = Outs[i].Flags;
378 if (!Flags.isByVal())
379 continue;
380
381 SDValue Arg = OutVals[i];
382 unsigned Size = Flags.getByValSize();
383 unsigned Align = Flags.getByValAlign();
384
385 int FI = MFI->CreateStackObject(Size, Align, false);
386 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
387 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
388
389 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
390 false, //isVolatile,
391 (Size <= 32), //AlwaysInline if size <= 32
392 MachinePointerInfo(), MachinePointerInfo());
393 ByValArgs.push_back(FIPtr);
394 }
395
Chris Lattnere563bbc2008-10-11 22:08:30 +0000396 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000397
Dan Gohman475871a2008-07-27 21:46:04 +0000398 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
399 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000400
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000401 const unsigned StackOffset = 92;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000402 bool hasStructRetAttr = false;
Chris Lattner315123f2008-03-17 06:58:37 +0000403 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000404 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000405 i != e;
406 ++i, ++realArgIdx) {
Chris Lattner315123f2008-03-17 06:58:37 +0000407 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000408 SDValue Arg = OutVals[realArgIdx];
Chris Lattner315123f2008-03-17 06:58:37 +0000409
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000410 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
411
412 //Use local copy if it is a byval arg.
413 if (Flags.isByVal())
414 Arg = ByValArgs[byvalArgIdx++];
415
Chris Lattner315123f2008-03-17 06:58:37 +0000416 // Promote the value if needed.
417 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000418 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000419 case CCValAssign::Full: break;
420 case CCValAssign::SExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000421 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000422 break;
423 case CCValAssign::ZExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000424 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000425 break;
426 case CCValAssign::AExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000427 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
428 break;
429 case CCValAssign::BCvt:
430 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000431 break;
432 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000433
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000434 if (Flags.isSRet()) {
435 assert(VA.needsCustom());
436 // store SRet argument in %sp+64
437 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
438 SDValue PtrOff = DAG.getIntPtrConstant(64);
439 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
440 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
441 MachinePointerInfo(),
442 false, false, 0));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000443 hasStructRetAttr = true;
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000444 continue;
445 }
446
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000447 if (VA.needsCustom()) {
448 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000449
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000450 if (VA.isMemLoc()) {
451 unsigned Offset = VA.getLocMemOffset() + StackOffset;
452 //if it is double-word aligned, just store.
453 if (Offset % 8 == 0) {
454 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
455 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
456 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
457 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
458 MachinePointerInfo(),
459 false, false, 0));
460 continue;
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000461 }
462 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000463
Owen Anderson825b72b2009-08-11 20:47:22 +0000464 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000465 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000466 Arg, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000467 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000468 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000469 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000470 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000471 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000472 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000473 DAG.getIntPtrConstant(4));
474 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000475 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000476 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000477
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000478 if (VA.isRegLoc()) {
479 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
480 assert(i+1 != e);
481 CCValAssign &NextVA = ArgLocs[++i];
482 if (NextVA.isRegLoc()) {
483 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
484 } else {
485 //Store the low part in stack.
486 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
487 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
488 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
489 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
490 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
491 MachinePointerInfo(),
492 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000493 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000494 } else {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000495 unsigned Offset = VA.getLocMemOffset() + StackOffset;
496 // Store the high part.
497 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
498 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
499 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
500 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
501 MachinePointerInfo(),
502 false, false, 0));
503 // Store the low part.
504 PtrOff = DAG.getIntPtrConstant(Offset+4);
505 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
506 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
507 MachinePointerInfo(),
508 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000509 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000510 continue;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000511 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000512
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000513 // Arguments that can be passed on register must be kept at
514 // RegsToPass vector
515 if (VA.isRegLoc()) {
516 if (VA.getLocVT() != MVT::f32) {
517 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
518 continue;
519 }
520 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
521 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
522 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000523 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000524
525 assert(VA.isMemLoc());
526
527 // Create a store off the stack pointer for this argument.
528 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
529 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
530 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
531 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
532 MachinePointerInfo(),
533 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000534 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000535
Anton Korobeynikov53835702008-10-10 20:27:31 +0000536
Chris Lattner5a65b922008-03-17 05:41:48 +0000537 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000538 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000539 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000540 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000541
542 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000543 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000544 // The InFlag in necessary since all emitted instructions must be
Chris Lattner315123f2008-03-17 06:58:37 +0000545 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000546 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000547 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
548 unsigned Reg = RegsToPass[i].first;
549 // Remap I0->I7 -> O0->O7.
550 if (Reg >= SP::I0 && Reg <= SP::I7)
551 Reg = Reg-SP::I0+SP::O0;
552
Dale Johannesen33c960f2009-02-04 20:06:27 +0000553 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000554 InFlag = Chain.getValue(1);
555 }
556
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000557 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
558
Chris Lattner5a65b922008-03-17 05:41:48 +0000559 // If the callee is a GlobalAddress node (quite common, every direct call is)
560 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000561 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000562 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000563 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000564 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000565 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000566
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000567 // Returns a chain & a flag for retval copy to use
568 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
569 SmallVector<SDValue, 8> Ops;
570 Ops.push_back(Chain);
571 Ops.push_back(Callee);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000572 if (hasStructRetAttr)
573 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000574 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
575 unsigned Reg = RegsToPass[i].first;
576 if (Reg >= SP::I0 && Reg <= SP::I7)
577 Reg = Reg-SP::I0+SP::O0;
578
579 Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
580 }
581 if (InFlag.getNode())
582 Ops.push_back(InFlag);
583
584 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000585 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000586
Chris Lattnere563bbc2008-10-11 22:08:30 +0000587 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
588 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000589 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000590
Chris Lattner98949a62008-03-17 06:01:07 +0000591 // Assign locations to each value returned by this call.
592 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000593 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
594 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000595
Dan Gohman98ca4f22009-08-05 01:29:28 +0000596 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000597
Chris Lattner98949a62008-03-17 06:01:07 +0000598 // Copy all of the result registers out of their specified physreg.
599 for (unsigned i = 0; i != RVLocs.size(); ++i) {
600 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000601
Chris Lattner98949a62008-03-17 06:01:07 +0000602 // Remap I0->I7 -> O0->O7.
603 if (Reg >= SP::I0 && Reg <= SP::I7)
604 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000605
Dale Johannesen33c960f2009-02-04 20:06:27 +0000606 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000607 RVLocs[i].getValVT(), InFlag).getValue(1);
608 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000609 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000610 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000611
Dan Gohman98ca4f22009-08-05 01:29:28 +0000612 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000613}
614
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000615unsigned
616SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
617{
618 const Function *CalleeFn = 0;
619 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
620 CalleeFn = dyn_cast<Function>(G->getGlobal());
621 } else if (ExternalSymbolSDNode *E =
622 dyn_cast<ExternalSymbolSDNode>(Callee)) {
623 const Function *Fn = DAG.getMachineFunction().getFunction();
624 const Module *M = Fn->getParent();
625 CalleeFn = M->getFunction(E->getSymbol());
626 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000627
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000628 if (!CalleeFn)
629 return 0;
630
631 assert(CalleeFn->hasStructRetAttr() &&
632 "Callee does not have the StructRet attribute.");
633
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000634 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
635 Type *ElementTy = Ty->getElementType();
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000636 return getTargetData()->getTypeAllocSize(ElementTy);
637}
Chris Lattner5a65b922008-03-17 05:41:48 +0000638
Chris Lattnerd23405e2008-03-17 03:21:36 +0000639//===----------------------------------------------------------------------===//
640// TargetLowering Implementation
641//===----------------------------------------------------------------------===//
642
643/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
644/// condition.
645static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
646 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000647 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000648 case ISD::SETEQ: return SPCC::ICC_E;
649 case ISD::SETNE: return SPCC::ICC_NE;
650 case ISD::SETLT: return SPCC::ICC_L;
651 case ISD::SETGT: return SPCC::ICC_G;
652 case ISD::SETLE: return SPCC::ICC_LE;
653 case ISD::SETGE: return SPCC::ICC_GE;
654 case ISD::SETULT: return SPCC::ICC_CS;
655 case ISD::SETULE: return SPCC::ICC_LEU;
656 case ISD::SETUGT: return SPCC::ICC_GU;
657 case ISD::SETUGE: return SPCC::ICC_CC;
658 }
659}
660
661/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
662/// FCC condition.
663static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
664 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000665 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 case ISD::SETEQ:
667 case ISD::SETOEQ: return SPCC::FCC_E;
668 case ISD::SETNE:
669 case ISD::SETUNE: return SPCC::FCC_NE;
670 case ISD::SETLT:
671 case ISD::SETOLT: return SPCC::FCC_L;
672 case ISD::SETGT:
673 case ISD::SETOGT: return SPCC::FCC_G;
674 case ISD::SETLE:
675 case ISD::SETOLE: return SPCC::FCC_LE;
676 case ISD::SETGE:
677 case ISD::SETOGE: return SPCC::FCC_GE;
678 case ISD::SETULT: return SPCC::FCC_UL;
679 case ISD::SETULE: return SPCC::FCC_ULE;
680 case ISD::SETUGT: return SPCC::FCC_UG;
681 case ISD::SETUGE: return SPCC::FCC_UGE;
682 case ISD::SETUO: return SPCC::FCC_U;
683 case ISD::SETO: return SPCC::FCC_O;
684 case ISD::SETONE: return SPCC::FCC_LG;
685 case ISD::SETUEQ: return SPCC::FCC_UE;
686 }
687}
688
Chris Lattnerd23405e2008-03-17 03:21:36 +0000689SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000690 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000691
Chris Lattnerd23405e2008-03-17 03:21:36 +0000692 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000693 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
694 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
695 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000696
697 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000699 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000700 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000701 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000703
704 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000705 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
706 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
707 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000708
Chris Lattnerd23405e2008-03-17 03:21:36 +0000709 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000710 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
711 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
712 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000713
714 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000715 setOperationAction(ISD::UREM, MVT::i32, Expand);
716 setOperationAction(ISD::SREM, MVT::i32, Expand);
717 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
718 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000719
720 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
722 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000723
724 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000725 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
726 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000727
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000728 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
729 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000730
Chris Lattnerd23405e2008-03-17 03:21:36 +0000731 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000732 setOperationAction(ISD::SELECT, MVT::i32, Expand);
733 setOperationAction(ISD::SELECT, MVT::f32, Expand);
734 setOperationAction(ISD::SELECT, MVT::f64, Expand);
735 setOperationAction(ISD::SETCC, MVT::i32, Expand);
736 setOperationAction(ISD::SETCC, MVT::f32, Expand);
737 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000738
Chris Lattnerd23405e2008-03-17 03:21:36 +0000739 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000740 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
741 setOperationAction(ISD::BRIND, MVT::Other, Expand);
742 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
743 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
744 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
745 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000746
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
748 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000750
Eli Friedman14648462011-07-27 22:21:52 +0000751 // FIXME: There are instructions available for ATOMIC_FENCE
752 // on SparcV8 and later.
Owen Anderson825b72b2009-08-11 20:47:22 +0000753 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000754 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000755
Owen Anderson825b72b2009-08-11 20:47:22 +0000756 setOperationAction(ISD::FSIN , MVT::f64, Expand);
757 setOperationAction(ISD::FCOS , MVT::f64, Expand);
758 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000759 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FSIN , MVT::f32, Expand);
761 setOperationAction(ISD::FCOS , MVT::f32, Expand);
762 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000763 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000764 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
765 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000766 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000768 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 setOperationAction(ISD::ROTL , MVT::i32, Expand);
770 setOperationAction(ISD::ROTR , MVT::i32, Expand);
771 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
772 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
773 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
774 setOperationAction(ISD::FPOW , MVT::f64, Expand);
775 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000776
Owen Anderson825b72b2009-08-11 20:47:22 +0000777 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
778 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
779 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000780
781 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000782 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
783 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000784
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000786
Chris Lattnerd23405e2008-03-17 03:21:36 +0000787 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000789 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000790 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000791
Chris Lattnerd23405e2008-03-17 03:21:36 +0000792 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
794 setOperationAction(ISD::VAEND , MVT::Other, Expand);
795 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
796 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
797 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000798
799 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000800 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000801
Chris Lattnerd23405e2008-03-17 03:21:36 +0000802 setStackPointerRegisterToSaveRestore(SP::O6);
803
804 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000805 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000806
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000807 setMinFunctionAlignment(2);
808
Chris Lattnerd23405e2008-03-17 03:21:36 +0000809 computeRegisterProperties();
810}
811
812const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
813 switch (Opcode) {
814 default: return 0;
815 case SPISD::CMPICC: return "SPISD::CMPICC";
816 case SPISD::CMPFCC: return "SPISD::CMPFCC";
817 case SPISD::BRICC: return "SPISD::BRICC";
818 case SPISD::BRFCC: return "SPISD::BRFCC";
819 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
820 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
821 case SPISD::Hi: return "SPISD::Hi";
822 case SPISD::Lo: return "SPISD::Lo";
823 case SPISD::FTOI: return "SPISD::FTOI";
824 case SPISD::ITOF: return "SPISD::ITOF";
825 case SPISD::CALL: return "SPISD::CALL";
826 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +0000827 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +0000828 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Chris Lattnerd23405e2008-03-17 03:21:36 +0000829 }
830}
831
832/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
833/// be zero. Op is expected to be a target specific node. Used by DAG
834/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000835void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000836 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000837 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000838 APInt &KnownOne,
839 const SelectionDAG &DAG,
840 unsigned Depth) const {
841 APInt KnownZero2, KnownOne2;
842 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000843
Chris Lattnerd23405e2008-03-17 03:21:36 +0000844 switch (Op.getOpcode()) {
845 default: break;
846 case SPISD::SELECT_ICC:
847 case SPISD::SELECT_FCC:
848 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
849 Depth+1);
850 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
851 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000852 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
853 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
854
Chris Lattnerd23405e2008-03-17 03:21:36 +0000855 // Only known if known in both the LHS and RHS.
856 KnownOne &= KnownOne2;
857 KnownZero &= KnownZero2;
858 break;
859 }
860}
861
Chris Lattnerd23405e2008-03-17 03:21:36 +0000862// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
863// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000864static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000865 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000866 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000867 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000868 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000869 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
870 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
871 (LHS.getOpcode() == SPISD::SELECT_FCC &&
872 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
873 isa<ConstantSDNode>(LHS.getOperand(0)) &&
874 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000875 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
876 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000877 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000878 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000879 LHS = CMPCC.getOperand(0);
880 RHS = CMPCC.getOperand(1);
881 }
882}
883
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000884SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000885 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000886 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000887 // FIXME there isn't really any debug info here
888 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000889 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000890 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
891 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000892
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000893 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000894 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895
Chris Lattnerdb486a62009-09-15 17:46:24 +0000896 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
897 getPointerTy());
898 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000900 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000901 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000902 AbsAddr, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000903}
904
Chris Lattnerdb486a62009-09-15 17:46:24 +0000905SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000906 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000907 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000908 // FIXME there isn't really any debug info here
909 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000910 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000911 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
912 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
913 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000914 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000915 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
916
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000917 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000918 getPointerTy());
919 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
920 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
921 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000922 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000923 AbsAddr, MachinePointerInfo(), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000924}
925
Dan Gohman475871a2008-07-27 21:46:04 +0000926static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000927 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000928 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000929 assert(Op.getValueType() == MVT::i32);
930 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000931 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000932}
933
Dan Gohman475871a2008-07-27 21:46:04 +0000934static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000935 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000936 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000937 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000938 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000939 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000940}
941
Dan Gohman475871a2008-07-27 21:46:04 +0000942static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
943 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000944 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000945 SDValue LHS = Op.getOperand(2);
946 SDValue RHS = Op.getOperand(3);
947 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000948 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000949 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000950
Chris Lattnerd23405e2008-03-17 03:21:36 +0000951 // If this is a br_cc of a "setcc", and if the setcc got lowered into
952 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
953 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000954
Chris Lattnerd23405e2008-03-17 03:21:36 +0000955 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000956 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000958 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000959 VTs.push_back(MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000960 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000961 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000962 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000963 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
964 Opc = SPISD::BRICC;
965 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000966 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000967 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
968 Opc = SPISD::BRFCC;
969 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000970 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
971 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000972}
973
Dan Gohman475871a2008-07-27 21:46:04 +0000974static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
975 SDValue LHS = Op.getOperand(0);
976 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000977 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000978 SDValue TrueVal = Op.getOperand(2);
979 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000980 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000981 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000982
Chris Lattnerd23405e2008-03-17 03:21:36 +0000983 // If this is a select_cc of a "setcc", and if the setcc got lowered into
984 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
985 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000986
Dan Gohman475871a2008-07-27 21:46:04 +0000987 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000988 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000989 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000990 VTs.push_back(LHS.getValueType()); // subcc returns a value
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000991 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000992 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000993 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000994 Opc = SPISD::SELECT_ICC;
995 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
996 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000997 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000998 Opc = SPISD::SELECT_FCC;
999 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
1000 }
Dale Johannesen3484c092009-02-05 22:07:54 +00001001 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +00001002 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001003}
1004
Dan Gohman475871a2008-07-27 21:46:04 +00001005static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001006 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001007 MachineFunction &MF = DAG.getMachineFunction();
1008 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
1009
Chris Lattnerd23405e2008-03-17 03:21:36 +00001010 // vastart just stores the address of the VarArgsFrameIndex slot into the
1011 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001012 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001013 SDValue Offset =
1014 DAG.getNode(ISD::ADD, dl, MVT::i32,
1015 DAG.getRegister(SP::I6, MVT::i32),
1016 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
1017 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001018 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001019 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
1020 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001021}
1022
Dan Gohman475871a2008-07-27 21:46:04 +00001023static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001024 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001025 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001026 SDValue InChain = Node->getOperand(0);
1027 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001028 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001029 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001030 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001031 MachinePointerInfo(SV), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001032 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +00001033 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001034 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +00001035 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001036 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001037 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00001038 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001039 // Load the actual argument out of the pointer VAList, unless this is an
1040 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +00001041 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001042 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001043 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001044
Chris Lattnerd23405e2008-03-17 03:21:36 +00001045 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001046 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001047 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001048
Chris Lattnerd23405e2008-03-17 03:21:36 +00001049 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001050 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001051 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +00001052 V.getValue(1)
1053 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00001054 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001055}
1056
Dan Gohman475871a2008-07-27 21:46:04 +00001057static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1058 SDValue Chain = Op.getOperand(0); // Legalize the chain.
1059 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +00001060 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001061
Chris Lattnerd23405e2008-03-17 03:21:36 +00001062 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +00001063 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1064 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +00001065 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +00001066
Chris Lattnerd23405e2008-03-17 03:21:36 +00001067 // The resultant pointer is actually 16 words from the bottom of the stack,
1068 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +00001069 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1070 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001071 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +00001072 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001073}
1074
Chris Lattnerd23405e2008-03-17 03:21:36 +00001075
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001076static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001077 DebugLoc dl = Op.getDebugLoc();
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001078 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001079 dl, MVT::Other, DAG.getEntryNode());
1080 return Chain;
1081}
1082
1083static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1084 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1085 MFI->setFrameAddressIsTaken(true);
1086
1087 EVT VT = Op.getValueType();
1088 DebugLoc dl = Op.getDebugLoc();
1089 unsigned FrameReg = SP::I6;
1090
1091 uint64_t depth = Op.getConstantOperandVal(0);
1092
1093 SDValue FrameAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001094 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001095 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1096 else {
1097 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001098 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001099 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001100
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001101 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001102 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001103 dl, MVT::i32,
1104 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001105 FrameAddr = DAG.getLoad(MVT::i32, dl,
1106 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001107 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001108 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001109 }
1110 }
1111 return FrameAddr;
1112}
1113
1114static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
1115 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1116 MFI->setReturnAddressIsTaken(true);
1117
1118 EVT VT = Op.getValueType();
1119 DebugLoc dl = Op.getDebugLoc();
1120 unsigned RetReg = SP::I7;
1121
1122 uint64_t depth = Op.getConstantOperandVal(0);
1123
1124 SDValue RetAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001125 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001126 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
1127 else {
1128 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001129 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001130 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001131
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001132 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001133 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001134 dl, MVT::i32,
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001135 RetAddr,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001136 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001137 RetAddr = DAG.getLoad(MVT::i32, dl,
1138 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001139 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001140 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001141 }
1142 }
1143 return RetAddr;
1144}
1145
Dan Gohman475871a2008-07-27 21:46:04 +00001146SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001147LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001148 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001149 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001150 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1151 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001152 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001153 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001154 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1155 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001156 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1157 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1158 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1159 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1160 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1161 case ISD::VAARG: return LowerVAARG(Op, DAG);
1162 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001163 }
1164}
1165
1166MachineBasicBlock *
1167SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001168 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001169 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1170 unsigned BROpcode;
1171 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001172 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001173 // Figure out the conditional branch opcode to use for this select_cc.
1174 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001175 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001176 case SP::SELECT_CC_Int_ICC:
1177 case SP::SELECT_CC_FP_ICC:
1178 case SP::SELECT_CC_DFP_ICC:
1179 BROpcode = SP::BCOND;
1180 break;
1181 case SP::SELECT_CC_Int_FCC:
1182 case SP::SELECT_CC_FP_FCC:
1183 case SP::SELECT_CC_DFP_FCC:
1184 BROpcode = SP::FBCOND;
1185 break;
1186 }
1187
1188 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001189
Chris Lattnerd23405e2008-03-17 03:21:36 +00001190 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1191 // control-flow pattern. The incoming instruction knows the destination vreg
1192 // to set, the condition code register to branch on, the true/false values to
1193 // select between, and a branch opcode to use.
1194 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001195 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001196 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001197
Chris Lattnerd23405e2008-03-17 03:21:36 +00001198 // thisMBB:
1199 // ...
1200 // TrueVal = ...
1201 // [f]bCC copy1MBB
1202 // fallthrough --> copy0MBB
1203 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001204 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001205 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1206 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001207 F->insert(It, copy0MBB);
1208 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001209
1210 // Transfer the remainder of BB and its successor edges to sinkMBB.
1211 sinkMBB->splice(sinkMBB->begin(), BB,
1212 llvm::next(MachineBasicBlock::iterator(MI)),
1213 BB->end());
1214 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1215
1216 // Add the true and fallthrough blocks as its successors.
1217 BB->addSuccessor(copy0MBB);
1218 BB->addSuccessor(sinkMBB);
1219
Dale Johannesend552eee2009-02-13 02:31:35 +00001220 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001221
Chris Lattnerd23405e2008-03-17 03:21:36 +00001222 // copy0MBB:
1223 // %FalseValue = ...
1224 // # fallthrough to sinkMBB
1225 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001226
Chris Lattnerd23405e2008-03-17 03:21:36 +00001227 // Update machine-CFG edges
1228 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001229
Chris Lattnerd23405e2008-03-17 03:21:36 +00001230 // sinkMBB:
1231 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1232 // ...
1233 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001234 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001235 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1236 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001237
Dan Gohman14152b42010-07-06 20:24:04 +00001238 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001239 return BB;
1240}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001241
1242//===----------------------------------------------------------------------===//
1243// Sparc Inline Assembly Support
1244//===----------------------------------------------------------------------===//
1245
1246/// getConstraintType - Given a constraint letter, return the type of
1247/// constraint it is for this target.
1248SparcTargetLowering::ConstraintType
1249SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1250 if (Constraint.size() == 1) {
1251 switch (Constraint[0]) {
1252 default: break;
1253 case 'r': return C_RegisterClass;
1254 }
1255 }
1256
1257 return TargetLowering::getConstraintType(Constraint);
1258}
1259
1260std::pair<unsigned, const TargetRegisterClass*>
1261SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001262 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001263 if (Constraint.size() == 1) {
1264 switch (Constraint[0]) {
1265 case 'r':
1266 return std::make_pair(0U, SP::IntRegsRegisterClass);
1267 }
1268 }
1269
1270 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1271}
1272
Dan Gohman6520e202008-10-18 02:06:02 +00001273bool
1274SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1275 // The Sparc target isn't yet aware of offsets.
1276 return false;
1277}