blob: 28ac02a613eef474184ecea134b6b09d764ebb21 [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"
Dan Gohman1e93df62010-04-17 14:41:14 +000016#include "SparcMachineFunctionInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "SparcTargetMachine.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000018#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000024#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000025#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/Module.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{
Craig Topperc5eaae42012-03-11 07:57:25 +000053 static const uint16_t RegList[] = {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +000054 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(),
Bill Wendling56cb2292012-07-19 00:11:40 +000093 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
Dan Gohman475871a2008-07-27 21:46:04 +000098 SDValue Flag;
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +000099 SmallVector<SDValue, 4> RetOps(1, Chain);
100 // Make room for the return address offset.
101 RetOps.push_back(SDValue());
Chris Lattner5a65b922008-03-17 05:41:48 +0000102
103 // Copy the result values into the output registers.
104 for (unsigned i = 0; i != RVLocs.size(); ++i) {
105 CCValAssign &VA = RVLocs[i];
106 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +0000107
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000108 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +0000109 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000110
Chris Lattner5a65b922008-03-17 05:41:48 +0000111 // Guarantee that all emitted copies are stuck together with flags.
112 Flag = Chain.getValue(1);
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000113 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner5a65b922008-03-17 05:41:48 +0000114 }
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000115
116 unsigned RetAddrOffset = 8; //Call Inst + Delay Slot
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000117 // If the function returns a struct, copy the SRetReturnReg to I0
118 if (MF.getFunction()->hasStructRetAttr()) {
119 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
120 unsigned Reg = SFI->getSRetReturnReg();
121 if (!Reg)
122 llvm_unreachable("sret virtual register not created in the entry block");
123 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
124 Chain = DAG.getCopyToReg(Chain, dl, SP::I0, Val, Flag);
125 Flag = Chain.getValue(1);
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000126 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000127 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000128 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000129
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000130 RetOps[0] = Chain; // Update chain.
131 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000132
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000133 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +0000134 if (Flag.getNode())
Jakob Stoklund Olesen067e5a22013-02-05 18:16:58 +0000135 RetOps.push_back(Flag);
136
137 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other,
138 &RetOps[0], RetOps.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000139}
140
Dan Gohman98ca4f22009-08-05 01:29:28 +0000141/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
142/// passed in either one or two GPRs, including FP values. TODO: we should
143/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000144SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000145SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000146 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000147 const SmallVectorImpl<ISD::InputArg>
148 &Ins,
149 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000150 SmallVectorImpl<SDValue> &InVals)
151 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000152
Chris Lattner5a65b922008-03-17 05:41:48 +0000153 MachineFunction &MF = DAG.getMachineFunction();
154 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +0000155 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +0000156
157 // Assign locations to all of the incoming arguments.
158 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000159 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000160 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000161 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000162
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000163 const unsigned StackOffset = 92;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000164
Eli Friedmana786c7b2009-07-19 19:53:46 +0000165 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmana786c7b2009-07-19 19:53:46 +0000166 CCValAssign &VA = ArgLocs[i];
Chris Lattner5a65b922008-03-17 05:41:48 +0000167
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000168 if (i == 0 && Ins[i].Flags.isSRet()) {
169 //Get SRet from [%fp+64]
170 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
171 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
172 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
173 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000174 false, false, false, 0);
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000175 InVals.push_back(Arg);
176 continue;
177 }
178
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000179 if (VA.isRegLoc()) {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000180 if (VA.needsCustom()) {
181 assert(VA.getLocVT() == MVT::f64);
182 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
183 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
184 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000185
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000186 assert(i+1 < e);
187 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000188
Dan Gohman475871a2008-07-27 21:46:04 +0000189 SDValue LoVal;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000190 if (NextVA.isMemLoc()) {
191 int FrameIdx = MF.getFrameInfo()->
192 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000194 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
195 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000196 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000197 } else {
198 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patel68e6bee2011-02-21 23:21:26 +0000199 &SP::IntRegsRegClass);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000200 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000201 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000202 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000204 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000205 InVals.push_back(WholeValue);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000206 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000207 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000208 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
209 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
210 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
211 if (VA.getLocVT() == MVT::f32)
212 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
213 else if (VA.getLocVT() != MVT::i32) {
214 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
215 DAG.getValueType(VA.getLocVT()));
216 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
217 }
218 InVals.push_back(Arg);
219 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000220 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000221
222 assert(VA.isMemLoc());
223
224 unsigned Offset = VA.getLocMemOffset()+StackOffset;
225
226 if (VA.needsCustom()) {
227 assert(VA.getValVT() == MVT::f64);
228 //If it is double-word aligned, just load.
229 if (Offset % 8 == 0) {
230 int FI = MF.getFrameInfo()->CreateFixedObject(8,
231 Offset,
232 true);
233 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
234 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
235 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000236 false,false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000237 InVals.push_back(Load);
238 continue;
239 }
240
241 int FI = MF.getFrameInfo()->CreateFixedObject(4,
242 Offset,
243 true);
244 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
245 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
246 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000247 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000248 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
249 Offset+4,
250 true);
251 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
252
253 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
254 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000255 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000256
257 SDValue WholeValue =
258 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
259 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
260 InVals.push_back(WholeValue);
261 continue;
262 }
263
264 int FI = MF.getFrameInfo()->CreateFixedObject(4,
265 Offset,
266 true);
267 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
268 SDValue Load ;
269 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
270 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
271 MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000272 false, false, false, 0);
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000273 } else {
274 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
275 // Sparc is big endian, so add an offset based on the ObjectVT.
276 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
277 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
278 DAG.getConstant(Offset, MVT::i32));
Stuart Hastingsa9011292011-02-16 16:23:55 +0000279 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000280 MachinePointerInfo(),
281 VA.getValVT(), false, false,0);
282 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
283 }
284 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000285 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000286
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000287 if (MF.getFunction()->hasStructRetAttr()) {
288 //Copy the SRet Argument to SRetReturnReg
289 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
290 unsigned Reg = SFI->getSRetReturnReg();
291 if (!Reg) {
292 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
293 SFI->setSRetReturnReg(Reg);
294 }
295 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
296 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
297 }
298
Chris Lattner5a65b922008-03-17 05:41:48 +0000299 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000300 if (isVarArg) {
Craig Topperc5eaae42012-03-11 07:57:25 +0000301 static const uint16_t ArgRegs[] = {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000302 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
303 };
304 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
Craig Topperc5eaae42012-03-11 07:57:25 +0000305 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000306 unsigned ArgOffset = CCInfo.getNextStackOffset();
307 if (NumAllocated == 6)
308 ArgOffset += StackOffset;
309 else {
310 assert(!ArgOffset);
311 ArgOffset = 68+4*NumAllocated;
312 }
313
Chris Lattner5a65b922008-03-17 05:41:48 +0000314 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000315 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000316
Eli Friedmana786c7b2009-07-19 19:53:46 +0000317 std::vector<SDValue> OutChains;
318
Chris Lattner5a65b922008-03-17 05:41:48 +0000319 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
320 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
321 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000322 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000323
David Greene3f2bf852009-11-12 20:49:22 +0000324 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000325 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000326 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000327
Chris Lattner6229d0a2010-09-21 18:41:36 +0000328 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
329 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000330 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000331 ArgOffset += 4;
332 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000333
334 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000335 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000337 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000338 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000339 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000340
Dan Gohman98ca4f22009-08-05 01:29:28 +0000341 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000342}
343
Dan Gohman98ca4f22009-08-05 01:29:28 +0000344SDValue
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000345SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohmand858e902010-04-17 15:26:15 +0000346 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000347 SelectionDAG &DAG = CLI.DAG;
348 DebugLoc &dl = CLI.DL;
349 SmallVector<ISD::OutputArg, 32> &Outs = CLI.Outs;
350 SmallVector<SDValue, 32> &OutVals = CLI.OutVals;
351 SmallVector<ISD::InputArg, 32> &Ins = CLI.Ins;
352 SDValue Chain = CLI.Chain;
353 SDValue Callee = CLI.Callee;
354 bool &isTailCall = CLI.IsTailCall;
355 CallingConv::ID CallConv = CLI.CallConv;
356 bool isVarArg = CLI.IsVarArg;
357
Evan Cheng0c439eb2010-01-27 00:07:07 +0000358 // Sparc target does not yet support tail call optimization.
359 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000360
Chris Lattner315123f2008-03-17 06:58:37 +0000361 // Analyze operands of the call, assigning locations to each operand.
362 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000363 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000364 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +0000365 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000366
Chris Lattner315123f2008-03-17 06:58:37 +0000367 // Get the size of the outgoing arguments stack space requirement.
368 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000369
Chris Lattner5a65b922008-03-17 05:41:48 +0000370 // Keep stack frames 8-byte aligned.
371 ArgsSize = (ArgsSize+7) & ~7;
372
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000373 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
374
375 //Create local copies for byval args.
376 SmallVector<SDValue, 8> ByValArgs;
377 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
378 ISD::ArgFlagsTy Flags = Outs[i].Flags;
379 if (!Flags.isByVal())
380 continue;
381
382 SDValue Arg = OutVals[i];
383 unsigned Size = Flags.getByValSize();
384 unsigned Align = Flags.getByValAlign();
385
386 int FI = MFI->CreateStackObject(Size, Align, false);
387 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
388 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
389
390 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
391 false, //isVolatile,
392 (Size <= 32), //AlwaysInline if size <= 32
393 MachinePointerInfo(), MachinePointerInfo());
394 ByValArgs.push_back(FIPtr);
395 }
396
Chris Lattnere563bbc2008-10-11 22:08:30 +0000397 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000398
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
400 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000401
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000402 const unsigned StackOffset = 92;
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000403 bool hasStructRetAttr = false;
Chris Lattner315123f2008-03-17 06:58:37 +0000404 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000405 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000406 i != e;
407 ++i, ++realArgIdx) {
Chris Lattner315123f2008-03-17 06:58:37 +0000408 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000409 SDValue Arg = OutVals[realArgIdx];
Chris Lattner315123f2008-03-17 06:58:37 +0000410
Venkatraman Govindaraju46713292011-01-21 14:00:01 +0000411 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
412
413 //Use local copy if it is a byval arg.
414 if (Flags.isByVal())
415 Arg = ByValArgs[byvalArgIdx++];
416
Chris Lattner315123f2008-03-17 06:58:37 +0000417 // Promote the value if needed.
418 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000419 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000420 case CCValAssign::Full: break;
421 case CCValAssign::SExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000422 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000423 break;
424 case CCValAssign::ZExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000425 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000426 break;
427 case CCValAssign::AExt:
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000428 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
429 break;
430 case CCValAssign::BCvt:
431 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner315123f2008-03-17 06:58:37 +0000432 break;
433 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000434
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000435 if (Flags.isSRet()) {
436 assert(VA.needsCustom());
437 // store SRet argument in %sp+64
438 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
439 SDValue PtrOff = DAG.getIntPtrConstant(64);
440 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
441 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
442 MachinePointerInfo(),
443 false, false, 0));
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000444 hasStructRetAttr = true;
Venkatraman Govindaraju8184e282011-01-22 13:05:16 +0000445 continue;
446 }
447
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000448 if (VA.needsCustom()) {
449 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000450
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000451 if (VA.isMemLoc()) {
452 unsigned Offset = VA.getLocMemOffset() + StackOffset;
453 //if it is double-word aligned, just store.
454 if (Offset % 8 == 0) {
455 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
456 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
457 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
458 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
459 MachinePointerInfo(),
460 false, false, 0));
461 continue;
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000462 }
463 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000464
Owen Anderson825b72b2009-08-11 20:47:22 +0000465 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000466 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000467 Arg, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000468 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000469 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000470 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000471 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000472 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000473 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000474 DAG.getIntPtrConstant(4));
475 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000476 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +0000477 MachinePointerInfo(), false, false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000478
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000479 if (VA.isRegLoc()) {
480 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
481 assert(i+1 != e);
482 CCValAssign &NextVA = ArgLocs[++i];
483 if (NextVA.isRegLoc()) {
484 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
485 } else {
486 //Store the low part in stack.
487 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
488 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
489 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
490 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
491 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
492 MachinePointerInfo(),
493 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000494 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000495 } else {
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000496 unsigned Offset = VA.getLocMemOffset() + StackOffset;
497 // Store the high part.
498 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
499 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
500 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
501 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
502 MachinePointerInfo(),
503 false, false, 0));
504 // Store the low part.
505 PtrOff = DAG.getIntPtrConstant(Offset+4);
506 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
507 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
508 MachinePointerInfo(),
509 false, false, 0));
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000510 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000511 continue;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000512 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000513
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000514 // Arguments that can be passed on register must be kept at
515 // RegsToPass vector
516 if (VA.isRegLoc()) {
517 if (VA.getLocVT() != MVT::f32) {
518 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
519 continue;
520 }
521 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
522 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
523 continue;
Chris Lattner5a65b922008-03-17 05:41:48 +0000524 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000525
526 assert(VA.isMemLoc());
527
528 // Create a store off the stack pointer for this argument.
529 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
530 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
531 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
532 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
533 MachinePointerInfo(),
534 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000535 }
Venkatraman Govindaraju687ae962011-01-18 06:09:55 +0000536
Anton Korobeynikov53835702008-10-10 20:27:31 +0000537
Chris Lattner5a65b922008-03-17 05:41:48 +0000538 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000539 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000540 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000541 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000542
543 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000544 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000545 // The InFlag in necessary since all emitted instructions must be
Chris Lattner315123f2008-03-17 06:58:37 +0000546 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000547 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000548 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
549 unsigned Reg = RegsToPass[i].first;
550 // Remap I0->I7 -> O0->O7.
551 if (Reg >= SP::I0 && Reg <= SP::I7)
552 Reg = Reg-SP::I0+SP::O0;
553
Dale Johannesen33c960f2009-02-04 20:06:27 +0000554 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000555 InFlag = Chain.getValue(1);
556 }
557
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000558 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
559
Chris Lattner5a65b922008-03-17 05:41:48 +0000560 // If the callee is a GlobalAddress node (quite common, every direct call is)
561 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000562 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000563 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000564 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000565 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000567
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000568 // Returns a chain & a flag for retval copy to use
569 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
570 SmallVector<SDValue, 8> Ops;
571 Ops.push_back(Chain);
572 Ops.push_back(Callee);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000573 if (hasStructRetAttr)
574 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000575 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
576 unsigned Reg = RegsToPass[i].first;
577 if (Reg >= SP::I0 && Reg <= SP::I7)
578 Reg = Reg-SP::I0+SP::O0;
579
580 Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
581 }
582 if (InFlag.getNode())
583 Ops.push_back(InFlag);
584
585 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000586 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000587
Chris Lattnere563bbc2008-10-11 22:08:30 +0000588 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
589 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000590 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000591
Chris Lattner98949a62008-03-17 06:01:07 +0000592 // Assign locations to each value returned by this call.
593 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000594 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendling56cb2292012-07-19 00:11:40 +0000595 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000596
Dan Gohman98ca4f22009-08-05 01:29:28 +0000597 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000598
Chris Lattner98949a62008-03-17 06:01:07 +0000599 // Copy all of the result registers out of their specified physreg.
600 for (unsigned i = 0; i != RVLocs.size(); ++i) {
601 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000602
Chris Lattner98949a62008-03-17 06:01:07 +0000603 // Remap I0->I7 -> O0->O7.
604 if (Reg >= SP::I0 && Reg <= SP::I7)
605 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000606
Dale Johannesen33c960f2009-02-04 20:06:27 +0000607 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000608 RVLocs[i].getValVT(), InFlag).getValue(1);
609 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000610 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000611 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000612
Dan Gohman98ca4f22009-08-05 01:29:28 +0000613 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000614}
615
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000616unsigned
617SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
618{
619 const Function *CalleeFn = 0;
620 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
621 CalleeFn = dyn_cast<Function>(G->getGlobal());
622 } else if (ExternalSymbolSDNode *E =
623 dyn_cast<ExternalSymbolSDNode>(Callee)) {
624 const Function *Fn = DAG.getMachineFunction().getFunction();
625 const Module *M = Fn->getParent();
626 CalleeFn = M->getFunction(E->getSymbol());
627 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000628
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000629 if (!CalleeFn)
630 return 0;
631
632 assert(CalleeFn->hasStructRetAttr() &&
633 "Callee does not have the StructRet attribute.");
634
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000635 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
636 Type *ElementTy = Ty->getElementType();
Micah Villmow3574eca2012-10-08 16:38:25 +0000637 return getDataLayout()->getTypeAllocSize(ElementTy);
Venkatraman Govindaraju58269b92011-02-21 03:42:44 +0000638}
Chris Lattner5a65b922008-03-17 05:41:48 +0000639
Chris Lattnerd23405e2008-03-17 03:21:36 +0000640//===----------------------------------------------------------------------===//
641// TargetLowering Implementation
642//===----------------------------------------------------------------------===//
643
644/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
645/// condition.
646static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
647 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000648 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000649 case ISD::SETEQ: return SPCC::ICC_E;
650 case ISD::SETNE: return SPCC::ICC_NE;
651 case ISD::SETLT: return SPCC::ICC_L;
652 case ISD::SETGT: return SPCC::ICC_G;
653 case ISD::SETLE: return SPCC::ICC_LE;
654 case ISD::SETGE: return SPCC::ICC_GE;
655 case ISD::SETULT: return SPCC::ICC_CS;
656 case ISD::SETULE: return SPCC::ICC_LEU;
657 case ISD::SETUGT: return SPCC::ICC_GU;
658 case ISD::SETUGE: return SPCC::ICC_CC;
659 }
660}
661
662/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
663/// FCC condition.
664static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
665 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000666 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000667 case ISD::SETEQ:
668 case ISD::SETOEQ: return SPCC::FCC_E;
669 case ISD::SETNE:
670 case ISD::SETUNE: return SPCC::FCC_NE;
671 case ISD::SETLT:
672 case ISD::SETOLT: return SPCC::FCC_L;
673 case ISD::SETGT:
674 case ISD::SETOGT: return SPCC::FCC_G;
675 case ISD::SETLE:
676 case ISD::SETOLE: return SPCC::FCC_LE;
677 case ISD::SETGE:
678 case ISD::SETOGE: return SPCC::FCC_GE;
679 case ISD::SETULT: return SPCC::FCC_UL;
680 case ISD::SETULE: return SPCC::FCC_ULE;
681 case ISD::SETUGT: return SPCC::FCC_UG;
682 case ISD::SETUGE: return SPCC::FCC_UGE;
683 case ISD::SETUO: return SPCC::FCC_U;
684 case ISD::SETO: return SPCC::FCC_O;
685 case ISD::SETONE: return SPCC::FCC_LG;
686 case ISD::SETUEQ: return SPCC::FCC_UE;
687 }
688}
689
Chris Lattnerd23405e2008-03-17 03:21:36 +0000690SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000691 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000692
Chris Lattnerd23405e2008-03-17 03:21:36 +0000693 // Set up the register classes.
Craig Topperc9099502012-04-20 06:31:50 +0000694 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
695 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
696 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000697
698 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000700 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000701 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000702 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000704
705 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
707 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
708 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000709
Chris Lattnerd23405e2008-03-17 03:21:36 +0000710 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
712 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
713 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000714
715 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000716 setOperationAction(ISD::UREM, MVT::i32, Expand);
717 setOperationAction(ISD::SREM, MVT::i32, Expand);
718 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
719 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000720
721 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000722 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
723 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000724
725 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
727 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000728
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000729 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
730 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000731
Chris Lattnerd23405e2008-03-17 03:21:36 +0000732 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000733 setOperationAction(ISD::SELECT, MVT::i32, Expand);
734 setOperationAction(ISD::SELECT, MVT::f32, Expand);
735 setOperationAction(ISD::SELECT, MVT::f64, Expand);
736 setOperationAction(ISD::SETCC, MVT::i32, Expand);
737 setOperationAction(ISD::SETCC, MVT::f32, Expand);
738 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000739
Chris Lattnerd23405e2008-03-17 03:21:36 +0000740 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000741 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
742 setOperationAction(ISD::BRIND, MVT::Other, Expand);
743 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
744 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
745 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
746 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000747
Owen Anderson825b72b2009-08-11 20:47:22 +0000748 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
749 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
750 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000751
Eli Friedman14648462011-07-27 22:21:52 +0000752 // FIXME: There are instructions available for ATOMIC_FENCE
753 // on SparcV8 and later.
Owen Anderson825b72b2009-08-11 20:47:22 +0000754 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Eli Friedman14648462011-07-27 22:21:52 +0000755 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000756
Owen Anderson825b72b2009-08-11 20:47:22 +0000757 setOperationAction(ISD::FSIN , MVT::f64, Expand);
758 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000759 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000760 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000761 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000762 setOperationAction(ISD::FSIN , MVT::f32, Expand);
763 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng8688a582013-01-29 02:32:37 +0000764 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000765 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarich33390842011-07-08 21:39:21 +0000766 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000767 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
768 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000769 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000770 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth63974b22011-12-13 01:56:10 +0000771 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000772 setOperationAction(ISD::ROTL , MVT::i32, Expand);
773 setOperationAction(ISD::ROTR , MVT::i32, Expand);
774 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
775 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
776 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
777 setOperationAction(ISD::FPOW , MVT::f64, Expand);
778 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000779
Owen Anderson825b72b2009-08-11 20:47:22 +0000780 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
781 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
782 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000783
784 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000785 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
786 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000787
Owen Anderson825b72b2009-08-11 20:47:22 +0000788 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000789
Chris Lattnerd23405e2008-03-17 03:21:36 +0000790 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000791 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000792 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000793 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000794
Chris Lattnerd23405e2008-03-17 03:21:36 +0000795 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
797 setOperationAction(ISD::VAEND , MVT::Other, Expand);
798 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
799 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
800 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000801
802 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000803 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000804
Chris Lattnerd23405e2008-03-17 03:21:36 +0000805 setStackPointerRegisterToSaveRestore(SP::O6);
806
807 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000808 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000809
Eli Friedmanfc5d3052011-05-06 20:34:06 +0000810 setMinFunctionAlignment(2);
811
Chris Lattnerd23405e2008-03-17 03:21:36 +0000812 computeRegisterProperties();
813}
814
815const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
816 switch (Opcode) {
817 default: return 0;
818 case SPISD::CMPICC: return "SPISD::CMPICC";
819 case SPISD::CMPFCC: return "SPISD::CMPFCC";
820 case SPISD::BRICC: return "SPISD::BRICC";
821 case SPISD::BRFCC: return "SPISD::BRFCC";
822 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
823 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
824 case SPISD::Hi: return "SPISD::Hi";
825 case SPISD::Lo: return "SPISD::Lo";
826 case SPISD::FTOI: return "SPISD::FTOI";
827 case SPISD::ITOF: return "SPISD::ITOF";
828 case SPISD::CALL: return "SPISD::CALL";
829 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +0000830 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +0000831 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Chris Lattnerd23405e2008-03-17 03:21:36 +0000832 }
833}
834
835/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
836/// be zero. Op is expected to be a target specific node. Used by DAG
837/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000838void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000839 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000840 APInt &KnownOne,
841 const SelectionDAG &DAG,
842 unsigned Depth) const {
843 APInt KnownZero2, KnownOne2;
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000844 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000845
Chris Lattnerd23405e2008-03-17 03:21:36 +0000846 switch (Op.getOpcode()) {
847 default: break;
848 case SPISD::SELECT_ICC:
849 case SPISD::SELECT_FCC:
Rafael Espindola26c8dcc2012-04-04 12:51:34 +0000850 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
851 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, 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) {
Benjamin Kramer3853f742013-03-07 20:33:29 +0000958 EVT VTs[] = { MVT::i32, MVT::Glue };
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000960 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000961 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
962 Opc = SPISD::BRICC;
963 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000964 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000965 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
966 Opc = SPISD::BRFCC;
967 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
969 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000970}
971
Dan Gohman475871a2008-07-27 21:46:04 +0000972static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
973 SDValue LHS = Op.getOperand(0);
974 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000975 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue TrueVal = Op.getOperand(2);
977 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000978 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000980
Chris Lattnerd23405e2008-03-17 03:21:36 +0000981 // If this is a select_cc of a "setcc", and if the setcc got lowered into
982 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
983 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000984
Dan Gohman475871a2008-07-27 21:46:04 +0000985 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 if (LHS.getValueType() == MVT::i32) {
Benjamin Kramer3853f742013-03-07 20:33:29 +0000987 // subcc returns a value
988 EVT VTs[] = { LHS.getValueType(), MVT::Glue };
Dan Gohman475871a2008-07-27 21:46:04 +0000989 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000990 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000991 Opc = SPISD::SELECT_ICC;
992 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
993 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000994 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000995 Opc = SPISD::SELECT_FCC;
996 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
997 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000998 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000999 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001000}
1001
Dan Gohman475871a2008-07-27 21:46:04 +00001002static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +00001003 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +00001004 MachineFunction &MF = DAG.getMachineFunction();
1005 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
1006
Chris Lattnerd23405e2008-03-17 03:21:36 +00001007 // vastart just stores the address of the VarArgsFrameIndex slot into the
1008 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001009 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +00001010 SDValue Offset =
1011 DAG.getNode(ISD::ADD, dl, MVT::i32,
1012 DAG.getRegister(SP::I6, MVT::i32),
1013 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
1014 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001015 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +00001016 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
1017 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001018}
1019
Dan Gohman475871a2008-07-27 21:46:04 +00001020static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +00001022 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00001023 SDValue InChain = Node->getOperand(0);
1024 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001025 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +00001026 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001027 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001028 MachinePointerInfo(SV), false, false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001029 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +00001030 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00001031 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +00001032 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +00001033 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +00001034 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +00001035 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001036 // Load the actual argument out of the pointer VAList, unless this is an
1037 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +00001038 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001039 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001040 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001041
Chris Lattnerd23405e2008-03-17 03:21:36 +00001042 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +00001043 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
Pete Cooperd752e0f2011-11-08 18:42:53 +00001044 false, false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001045
Chris Lattnerd23405e2008-03-17 03:21:36 +00001046 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00001047 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001048 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +00001049 V.getValue(1)
1050 };
Dale Johannesen33c960f2009-02-04 20:06:27 +00001051 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001052}
1053
Dan Gohman475871a2008-07-27 21:46:04 +00001054static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
1055 SDValue Chain = Op.getOperand(0); // Legalize the chain.
1056 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +00001057 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001058
Chris Lattnerd23405e2008-03-17 03:21:36 +00001059 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +00001060 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
1061 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +00001062 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +00001063
Chris Lattnerd23405e2008-03-17 03:21:36 +00001064 // The resultant pointer is actually 16 words from the bottom of the stack,
1065 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +00001066 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
1067 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00001068 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +00001069 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001070}
1071
Chris Lattnerd23405e2008-03-17 03:21:36 +00001072
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001073static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001074 DebugLoc dl = Op.getDebugLoc();
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001075 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001076 dl, MVT::Other, DAG.getEntryNode());
1077 return Chain;
1078}
1079
1080static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
1081 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1082 MFI->setFrameAddressIsTaken(true);
1083
1084 EVT VT = Op.getValueType();
1085 DebugLoc dl = Op.getDebugLoc();
1086 unsigned FrameReg = SP::I6;
1087
1088 uint64_t depth = Op.getConstantOperandVal(0);
1089
1090 SDValue FrameAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001091 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001092 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
1093 else {
1094 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001095 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001096 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001097
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001098 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001099 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001100 dl, MVT::i32,
1101 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001102 FrameAddr = DAG.getLoad(MVT::i32, dl,
1103 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001104 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001105 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001106 }
1107 }
1108 return FrameAddr;
1109}
1110
1111static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
1112 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1113 MFI->setReturnAddressIsTaken(true);
1114
1115 EVT VT = Op.getValueType();
1116 DebugLoc dl = Op.getDebugLoc();
1117 unsigned RetReg = SP::I7;
1118
1119 uint64_t depth = Op.getConstantOperandVal(0);
1120
1121 SDValue RetAddr;
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001122 if (depth == 0)
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001123 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
1124 else {
1125 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001126 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001127 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001128
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001129 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001130 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001131 dl, MVT::i32,
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001132 RetAddr,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001133 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajufc3faa72011-01-21 22:00:00 +00001134 RetAddr = DAG.getLoad(MVT::i32, dl,
1135 Chain,
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001136 Ptr,
Pete Cooperd752e0f2011-11-08 18:42:53 +00001137 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001138 }
1139 }
1140 return RetAddr;
1141}
1142
Dan Gohman475871a2008-07-27 21:46:04 +00001143SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +00001144LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001145 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001146 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju860b64c2011-01-12 05:08:36 +00001147 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
1148 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001149 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001150 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001151 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1152 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001153 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1154 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1155 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1156 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1157 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1158 case ISD::VAARG: return LowerVAARG(Op, DAG);
1159 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001160 }
1161}
1162
1163MachineBasicBlock *
1164SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001165 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001166 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1167 unsigned BROpcode;
1168 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001169 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001170 // Figure out the conditional branch opcode to use for this select_cc.
1171 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001172 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001173 case SP::SELECT_CC_Int_ICC:
1174 case SP::SELECT_CC_FP_ICC:
1175 case SP::SELECT_CC_DFP_ICC:
1176 BROpcode = SP::BCOND;
1177 break;
1178 case SP::SELECT_CC_Int_FCC:
1179 case SP::SELECT_CC_FP_FCC:
1180 case SP::SELECT_CC_DFP_FCC:
1181 BROpcode = SP::FBCOND;
1182 break;
1183 }
1184
1185 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001186
Chris Lattnerd23405e2008-03-17 03:21:36 +00001187 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1188 // control-flow pattern. The incoming instruction knows the destination vreg
1189 // to set, the condition code register to branch on, the true/false values to
1190 // select between, and a branch opcode to use.
1191 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001192 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001193 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001194
Chris Lattnerd23405e2008-03-17 03:21:36 +00001195 // thisMBB:
1196 // ...
1197 // TrueVal = ...
1198 // [f]bCC copy1MBB
1199 // fallthrough --> copy0MBB
1200 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001201 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001202 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1203 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001204 F->insert(It, copy0MBB);
1205 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001206
1207 // Transfer the remainder of BB and its successor edges to sinkMBB.
1208 sinkMBB->splice(sinkMBB->begin(), BB,
1209 llvm::next(MachineBasicBlock::iterator(MI)),
1210 BB->end());
1211 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1212
1213 // Add the true and fallthrough blocks as its successors.
1214 BB->addSuccessor(copy0MBB);
1215 BB->addSuccessor(sinkMBB);
1216
Dale Johannesend552eee2009-02-13 02:31:35 +00001217 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001218
Chris Lattnerd23405e2008-03-17 03:21:36 +00001219 // copy0MBB:
1220 // %FalseValue = ...
1221 // # fallthrough to sinkMBB
1222 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001223
Chris Lattnerd23405e2008-03-17 03:21:36 +00001224 // Update machine-CFG edges
1225 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001226
Chris Lattnerd23405e2008-03-17 03:21:36 +00001227 // sinkMBB:
1228 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1229 // ...
1230 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001231 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001232 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1233 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001234
Dan Gohman14152b42010-07-06 20:24:04 +00001235 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001236 return BB;
1237}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001238
1239//===----------------------------------------------------------------------===//
1240// Sparc Inline Assembly Support
1241//===----------------------------------------------------------------------===//
1242
1243/// getConstraintType - Given a constraint letter, return the type of
1244/// constraint it is for this target.
1245SparcTargetLowering::ConstraintType
1246SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1247 if (Constraint.size() == 1) {
1248 switch (Constraint[0]) {
1249 default: break;
1250 case 'r': return C_RegisterClass;
1251 }
1252 }
1253
1254 return TargetLowering::getConstraintType(Constraint);
1255}
1256
1257std::pair<unsigned, const TargetRegisterClass*>
1258SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001259 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001260 if (Constraint.size() == 1) {
1261 switch (Constraint[0]) {
1262 case 'r':
Craig Topperc9099502012-04-20 06:31:50 +00001263 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001264 }
1265 }
1266
1267 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1268}
1269
Dan Gohman6520e202008-10-18 02:06:02 +00001270bool
1271SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1272 // The Sparc target isn't yet aware of offsets.
1273 return false;
1274}