blob: 32c278b6689f4dc477feeb31eb4d5275e1a37501 [file] [log] [blame]
Chris Lattner0a1762e2008-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"
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +000016#include "MCTargetDesc/SparcMCExpr.h"
Dan Gohman31ae5862010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000018#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "SparcTargetMachine.h"
Venkatraman Govindarajufd5c1f92014-01-29 04:51:35 +000020#include "SparcTargetObjectFile.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000021#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-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 Korobeynikovab663a02010-02-15 22:37:53 +000027#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000031#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000032using namespace llvm;
33
Chris Lattner49b269d2008-03-17 05:41:48 +000034
35//===----------------------------------------------------------------------===//
36// Calling Convention Implementation
37//===----------------------------------------------------------------------===//
38
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000039static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
40 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
41 ISD::ArgFlagsTy &ArgFlags, CCState &State)
42{
43 assert (ArgFlags.isSRet());
44
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000045 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000046 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
47 0,
48 LocVT, LocInfo));
49 return true;
50}
51
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000052static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
53 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
54 ISD::ArgFlagsTy &ArgFlags, CCState &State)
55{
Craig Topperbef78fc2012-03-11 07:57:25 +000056 static const uint16_t RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000057 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
58 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000059 // Try to get first reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000060 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
61 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
62 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000063 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000064 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
65 State.AllocateStack(8,4),
66 LocVT, LocInfo));
67 return true;
68 }
69
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000070 // Try to get second reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000071 if (unsigned Reg = State.AllocateReg(RegList, 6))
72 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
73 else
74 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
75 State.AllocateStack(4,4),
76 LocVT, LocInfo));
77 return true;
78}
79
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000080// Allocate a full-sized argument for the 64-bit ABI.
81static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
82 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
83 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +000084 assert((LocVT == MVT::f32 || LocVT == MVT::f128
85 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000086 "Can't handle non-64 bits locations");
87
88 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +000089 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
90 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
91 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000092 unsigned Reg = 0;
93
94 if (LocVT == MVT::i64 && Offset < 6*8)
95 // Promote integers to %i0-%i5.
96 Reg = SP::I0 + Offset/8;
97 else if (LocVT == MVT::f64 && Offset < 16*8)
98 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
99 Reg = SP::D0 + Offset/8;
100 else if (LocVT == MVT::f32 && Offset < 16*8)
101 // Promote floats to %f1, %f3, ...
102 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000103 else if (LocVT == MVT::f128 && Offset < 16*8)
104 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
105 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000106
107 // Promote to register when possible, otherwise use the stack slot.
108 if (Reg) {
109 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
110 return true;
111 }
112
113 // This argument goes on the stack in an 8-byte slot.
114 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
115 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
116 if (LocVT == MVT::f32)
117 Offset += 4;
118
119 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
120 return true;
121}
122
123// Allocate a half-sized argument for the 64-bit ABI.
124//
125// This is used when passing { float, int } structs by value in registers.
126static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
127 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
128 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
129 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
130 unsigned Offset = State.AllocateStack(4, 4);
131
132 if (LocVT == MVT::f32 && Offset < 16*8) {
133 // Promote floats to %f0-%f31.
134 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
135 LocVT, LocInfo));
136 return true;
137 }
138
139 if (LocVT == MVT::i32 && Offset < 6*8) {
140 // Promote integers to %i0-%i5, using half the register.
141 unsigned Reg = SP::I0 + Offset/8;
142 LocVT = MVT::i64;
143 LocInfo = CCValAssign::AExt;
144
145 // Set the Custom bit if this i32 goes in the high bits of a register.
146 if (Offset % 8 == 0)
147 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
148 LocVT, LocInfo));
149 else
150 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
151 return true;
152 }
153
154 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
155 return true;
156}
157
Chris Lattner49b269d2008-03-17 05:41:48 +0000158#include "SparcGenCallingConv.inc"
159
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000160// The calling conventions in SparcCallingConv.td are described in terms of the
161// callee's register window. This function translates registers to the
162// corresponding caller window %o register.
163static unsigned toCallerWindow(unsigned Reg) {
164 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
165 if (Reg >= SP::I0 && Reg <= SP::I7)
166 return Reg - SP::I0 + SP::O0;
167 return Reg;
168}
169
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000170SDValue
171SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000172 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000173 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000174 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000175 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000176 if (Subtarget->is64Bit())
177 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
178 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
179}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000180
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000181SDValue
182SparcTargetLowering::LowerReturn_32(SDValue Chain,
183 CallingConv::ID CallConv, bool IsVarArg,
184 const SmallVectorImpl<ISD::OutputArg> &Outs,
185 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000186 SDLoc DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000187 MachineFunction &MF = DAG.getMachineFunction();
188
Chris Lattner49b269d2008-03-17 05:41:48 +0000189 // CCValAssign - represent the assignment of the return value to locations.
190 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000191
Chris Lattner49b269d2008-03-17 05:41:48 +0000192 // CCState - Info about the registers and stack slot.
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000193 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000194 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000195
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000196 // Analyze return values.
197 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000198
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000199 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000200 SmallVector<SDValue, 4> RetOps(1, Chain);
201 // Make room for the return address offset.
202 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000203
204 // Copy the result values into the output registers.
205 for (unsigned i = 0; i != RVLocs.size(); ++i) {
206 CCValAssign &VA = RVLocs[i];
207 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000208
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000209 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000210 OutVals[i], Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000211
Chris Lattner49b269d2008-03-17 05:41:48 +0000212 // Guarantee that all emitted copies are stuck together with flags.
213 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000214 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000215 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000216
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000217 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000218 // If the function returns a struct, copy the SRetReturnReg to I0
219 if (MF.getFunction()->hasStructRetAttr()) {
220 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
221 unsigned Reg = SFI->getSRetReturnReg();
222 if (!Reg)
223 llvm_unreachable("sret virtual register not created in the entry block");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000224 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
225 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000226 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000227 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000228 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000229 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000230
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000231 RetOps[0] = Chain; // Update chain.
232 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000233
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000234 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000235 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000236 RetOps.push_back(Flag);
237
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000238 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
239 &RetOps[0], RetOps.size());
240}
241
242// Lower return values for the 64-bit ABI.
243// Return values are passed the exactly the same way as function arguments.
244SDValue
245SparcTargetLowering::LowerReturn_64(SDValue Chain,
246 CallingConv::ID CallConv, bool IsVarArg,
247 const SmallVectorImpl<ISD::OutputArg> &Outs,
248 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000249 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000250 // CCValAssign - represent the assignment of the return value to locations.
251 SmallVector<CCValAssign, 16> RVLocs;
252
253 // CCState - Info about the registers and stack slot.
254 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
255 DAG.getTarget(), RVLocs, *DAG.getContext());
256
257 // Analyze return values.
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +0000258 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc64);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000259
260 SDValue Flag;
261 SmallVector<SDValue, 4> RetOps(1, Chain);
262
263 // The second operand on the return instruction is the return address offset.
264 // The return address is always %i7+8 with the 64-bit ABI.
265 RetOps.push_back(DAG.getConstant(8, MVT::i32));
266
267 // Copy the result values into the output registers.
268 for (unsigned i = 0; i != RVLocs.size(); ++i) {
269 CCValAssign &VA = RVLocs[i];
270 assert(VA.isRegLoc() && "Can only return in registers!");
271 SDValue OutVal = OutVals[i];
272
273 // Integer return values must be sign or zero extended by the callee.
274 switch (VA.getLocInfo()) {
Lang Hames06234ec2014-01-14 19:56:36 +0000275 case CCValAssign::Full: break;
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000276 case CCValAssign::SExt:
277 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
278 break;
279 case CCValAssign::ZExt:
280 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
281 break;
282 case CCValAssign::AExt:
283 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000284 break;
Lang Hames06234ec2014-01-14 19:56:36 +0000285 default:
286 llvm_unreachable("Unknown loc info!");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000287 }
288
289 // The custom bit on an i32 return value indicates that it should be passed
290 // in the high bits of the register.
291 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
292 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
293 DAG.getConstant(32, MVT::i32));
294
295 // The next value may go in the low bits of the same register.
296 // Handle both at once.
297 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
298 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
299 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
300 // Skip the next value, it's already done.
301 ++i;
302 }
303 }
304
305 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
306
307 // Guarantee that all emitted copies are stuck together with flags.
308 Flag = Chain.getValue(1);
309 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
310 }
311
312 RetOps[0] = Chain; // Update chain.
313
314 // Add the flag if we have it.
315 if (Flag.getNode())
316 RetOps.push_back(Flag);
317
318 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000319 &RetOps[0], RetOps.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000320}
321
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000322SDValue SparcTargetLowering::
323LowerFormalArguments(SDValue Chain,
324 CallingConv::ID CallConv,
325 bool IsVarArg,
326 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000327 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000328 SelectionDAG &DAG,
329 SmallVectorImpl<SDValue> &InVals) const {
330 if (Subtarget->is64Bit())
331 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
332 DL, DAG, InVals);
333 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
334 DL, DAG, InVals);
335}
336
337/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000338/// passed in either one or two GPRs, including FP values. TODO: we should
339/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000340SDValue SparcTargetLowering::
341LowerFormalArguments_32(SDValue Chain,
342 CallingConv::ID CallConv,
343 bool isVarArg,
344 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000345 SDLoc dl,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000346 SelectionDAG &DAG,
347 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000348 MachineFunction &MF = DAG.getMachineFunction();
349 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000350 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000351
352 // Assign locations to all of the incoming arguments.
353 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000354 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000355 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000356 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000357
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000358 const unsigned StackOffset = 92;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000359
Eli Friedmanbe853b72009-07-19 19:53:46 +0000360 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000361 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000362
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000363 if (i == 0 && Ins[i].Flags.isSRet()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000364 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000365 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
366 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
367 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
368 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000369 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000370 InVals.push_back(Arg);
371 continue;
372 }
373
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000374 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000375 if (VA.needsCustom()) {
376 assert(VA.getLocVT() == MVT::f64);
377 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
378 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
379 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000380
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000381 assert(i+1 < e);
382 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000383
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000384 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000385 if (NextVA.isMemLoc()) {
386 int FrameIdx = MF.getFrameInfo()->
387 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000388 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000389 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
390 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000391 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000392 } else {
393 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000394 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000395 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000396 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000397 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000398 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000399 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000400 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000401 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000402 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000403 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
404 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
405 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
406 if (VA.getLocVT() == MVT::f32)
407 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
408 else if (VA.getLocVT() != MVT::i32) {
409 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
410 DAG.getValueType(VA.getLocVT()));
411 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
412 }
413 InVals.push_back(Arg);
414 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000415 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000416
417 assert(VA.isMemLoc());
418
419 unsigned Offset = VA.getLocMemOffset()+StackOffset;
420
421 if (VA.needsCustom()) {
422 assert(VA.getValVT() == MVT::f64);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000423 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000424 if (Offset % 8 == 0) {
425 int FI = MF.getFrameInfo()->CreateFixedObject(8,
426 Offset,
427 true);
428 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
429 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
430 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000431 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000432 InVals.push_back(Load);
433 continue;
434 }
435
436 int FI = MF.getFrameInfo()->CreateFixedObject(4,
437 Offset,
438 true);
439 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
440 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
441 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000442 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000443 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
444 Offset+4,
445 true);
446 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
447
448 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
449 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000450 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000451
452 SDValue WholeValue =
453 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
454 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
455 InVals.push_back(WholeValue);
456 continue;
457 }
458
459 int FI = MF.getFrameInfo()->CreateFixedObject(4,
460 Offset,
461 true);
462 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
463 SDValue Load ;
464 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
465 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
466 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000467 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000468 } else {
469 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
470 // Sparc is big endian, so add an offset based on the ObjectVT.
471 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
472 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
473 DAG.getConstant(Offset, MVT::i32));
Stuart Hastings81c43062011-02-16 16:23:55 +0000474 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000475 MachinePointerInfo(),
476 VA.getValVT(), false, false,0);
477 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
478 }
479 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000480 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000481
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000482 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000483 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000484 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
485 unsigned Reg = SFI->getSRetReturnReg();
486 if (!Reg) {
487 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
488 SFI->setSRetReturnReg(Reg);
489 }
490 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
491 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
492 }
493
Chris Lattner49b269d2008-03-17 05:41:48 +0000494 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000495 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +0000496 static const uint16_t ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000497 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
498 };
499 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
Craig Topperbef78fc2012-03-11 07:57:25 +0000500 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000501 unsigned ArgOffset = CCInfo.getNextStackOffset();
502 if (NumAllocated == 6)
503 ArgOffset += StackOffset;
504 else {
505 assert(!ArgOffset);
506 ArgOffset = 68+4*NumAllocated;
507 }
508
Chris Lattner49b269d2008-03-17 05:41:48 +0000509 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000510 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000511
Eli Friedmanbe853b72009-07-19 19:53:46 +0000512 std::vector<SDValue> OutChains;
513
Chris Lattner49b269d2008-03-17 05:41:48 +0000514 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
515 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
516 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000517 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000518
David Greene1fbe0542009-11-12 20:49:22 +0000519 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000520 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000521 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000522
Chris Lattner676c61d2010-09-21 18:41:36 +0000523 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
524 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000525 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000526 ArgOffset += 4;
527 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000528
529 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000530 OutChains.push_back(Chain);
Owen Anderson9f944592009-08-11 20:47:22 +0000531 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000532 &OutChains[0], OutChains.size());
Eli Friedmanbe853b72009-07-19 19:53:46 +0000533 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000534 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000535
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000536 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000537}
538
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000539// Lower formal arguments for the 64 bit ABI.
540SDValue SparcTargetLowering::
541LowerFormalArguments_64(SDValue Chain,
542 CallingConv::ID CallConv,
543 bool IsVarArg,
544 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000545 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000546 SelectionDAG &DAG,
547 SmallVectorImpl<SDValue> &InVals) const {
548 MachineFunction &MF = DAG.getMachineFunction();
549
550 // Analyze arguments according to CC_Sparc64.
551 SmallVector<CCValAssign, 16> ArgLocs;
552 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
553 getTargetMachine(), ArgLocs, *DAG.getContext());
554 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
555
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000556 // The argument array begins at %fp+BIAS+128, after the register save area.
557 const unsigned ArgArea = 128;
558
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000559 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
560 CCValAssign &VA = ArgLocs[i];
561 if (VA.isRegLoc()) {
562 // This argument is passed in a register.
563 // All integer register arguments are promoted by the caller to i64.
564
565 // Create a virtual register for the promoted live-in value.
566 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
567 getRegClassFor(VA.getLocVT()));
568 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
569
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000570 // Get the high bits for i32 struct elements.
571 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
572 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
573 DAG.getConstant(32, MVT::i32));
574
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000575 // The caller promoted the argument, so insert an Assert?ext SDNode so we
576 // won't promote the value again in this function.
577 switch (VA.getLocInfo()) {
578 case CCValAssign::SExt:
579 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
580 DAG.getValueType(VA.getValVT()));
581 break;
582 case CCValAssign::ZExt:
583 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
584 DAG.getValueType(VA.getValVT()));
585 break;
586 default:
587 break;
588 }
589
590 // Truncate the register down to the argument type.
591 if (VA.isExtInLoc())
592 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
593
594 InVals.push_back(Arg);
595 continue;
596 }
597
598 // The registers are exhausted. This argument was passed on the stack.
599 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000600 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
601 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000602 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000603 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
604 // Adjust offset for extended arguments, SPARC is big-endian.
605 // The caller will have written the full slot with extended bytes, but we
606 // prefer our own extending loads.
607 if (VA.isExtInLoc())
608 Offset += 8 - ValSize;
609 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
610 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
611 DAG.getFrameIndex(FI, getPointerTy()),
612 MachinePointerInfo::getFixedStack(FI),
613 false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000614 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000615
616 if (!IsVarArg)
617 return Chain;
618
619 // This function takes variable arguments, some of which may have been passed
620 // in registers %i0-%i5. Variable floating point arguments are never passed
621 // in floating point registers. They go on %i0-%i5 or on the stack like
622 // integer arguments.
623 //
624 // The va_start intrinsic needs to know the offset to the first variable
625 // argument.
626 unsigned ArgOffset = CCInfo.getNextStackOffset();
627 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
628 // Skip the 128 bytes of register save area.
629 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
630 Subtarget->getStackPointerBias());
631
632 // Save the variable arguments that were passed in registers.
633 // The caller is required to reserve stack space for 6 arguments regardless
634 // of how many arguments were actually passed.
635 SmallVector<SDValue, 8> OutChains;
636 for (; ArgOffset < 6*8; ArgOffset += 8) {
637 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
638 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
639 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
640 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
641 DAG.getFrameIndex(FI, getPointerTy()),
642 MachinePointerInfo::getFixedStack(FI),
643 false, false, 0));
644 }
645
646 if (!OutChains.empty())
647 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
648 &OutChains[0], OutChains.size());
649
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000650 return Chain;
651}
652
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000653SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000654SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000655 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000656 if (Subtarget->is64Bit())
657 return LowerCall_64(CLI, InVals);
658 return LowerCall_32(CLI, InVals);
659}
660
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000661static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
662 ImmutableCallSite *CS) {
663 if (CS)
664 return CS->hasFnAttr(Attribute::ReturnsTwice);
665
666 const Function *CalleeFn = 0;
667 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
668 CalleeFn = dyn_cast<Function>(G->getGlobal());
669 } else if (ExternalSymbolSDNode *E =
670 dyn_cast<ExternalSymbolSDNode>(Callee)) {
671 const Function *Fn = DAG.getMachineFunction().getFunction();
672 const Module *M = Fn->getParent();
673 const char *CalleeName = E->getSymbol();
674 CalleeFn = M->getFunction(CalleeName);
675 }
676
677 if (!CalleeFn)
678 return false;
679 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
680}
681
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000682// Lower a call for the 32-bit ABI.
683SDValue
684SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
685 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000686 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000687 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000688 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
689 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
690 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000691 SDValue Chain = CLI.Chain;
692 SDValue Callee = CLI.Callee;
693 bool &isTailCall = CLI.IsTailCall;
694 CallingConv::ID CallConv = CLI.CallConv;
695 bool isVarArg = CLI.IsVarArg;
696
Evan Cheng67a69dd2010-01-27 00:07:07 +0000697 // Sparc target does not yet support tail call optimization.
698 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000699
Chris Lattner7d4152b2008-03-17 06:58:37 +0000700 // Analyze operands of the call, assigning locations to each operand.
701 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000702 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000703 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000704 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000705
Chris Lattner7d4152b2008-03-17 06:58:37 +0000706 // Get the size of the outgoing arguments stack space requirement.
707 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000708
Chris Lattner49b269d2008-03-17 05:41:48 +0000709 // Keep stack frames 8-byte aligned.
710 ArgsSize = (ArgsSize+7) & ~7;
711
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000712 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
713
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000714 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000715 SmallVector<SDValue, 8> ByValArgs;
716 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
717 ISD::ArgFlagsTy Flags = Outs[i].Flags;
718 if (!Flags.isByVal())
719 continue;
720
721 SDValue Arg = OutVals[i];
722 unsigned Size = Flags.getByValSize();
723 unsigned Align = Flags.getByValAlign();
724
725 int FI = MFI->CreateStackObject(Size, Align, false);
726 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
727 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
728
729 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000730 false, // isVolatile,
731 (Size <= 32), // AlwaysInline if size <= 32
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000732 MachinePointerInfo(), MachinePointerInfo());
733 ByValArgs.push_back(FIPtr);
734 }
735
Andrew Trickad6d08a2013-05-29 22:03:55 +0000736 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
737 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000738
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000739 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
740 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000741
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000742 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000743 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000744 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000745 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000746 i != e;
747 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000748 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000749 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000750
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000751 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
752
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000753 // Use local copy if it is a byval arg.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000754 if (Flags.isByVal())
755 Arg = ByValArgs[byvalArgIdx++];
756
Chris Lattner7d4152b2008-03-17 06:58:37 +0000757 // Promote the value if needed.
758 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000759 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000760 case CCValAssign::Full: break;
761 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000762 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000763 break;
764 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000765 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000766 break;
767 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000768 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
769 break;
770 case CCValAssign::BCvt:
771 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000772 break;
773 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000774
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000775 if (Flags.isSRet()) {
776 assert(VA.needsCustom());
777 // store SRet argument in %sp+64
778 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
779 SDValue PtrOff = DAG.getIntPtrConstant(64);
780 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
781 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
782 MachinePointerInfo(),
783 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000784 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000785 continue;
786 }
787
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000788 if (VA.needsCustom()) {
789 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000790
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000791 if (VA.isMemLoc()) {
792 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000793 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000794 if (Offset % 8 == 0) {
795 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
796 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
797 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
798 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
799 MachinePointerInfo(),
800 false, false, 0));
801 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000802 }
803 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000804
Owen Anderson9f944592009-08-11 20:47:22 +0000805 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +0000806 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000807 Arg, StackPtr, MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000808 false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000809 // Sparc is big-endian, so the high part comes first.
Chris Lattner7727d052010-09-21 06:44:06 +0000810 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000811 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000812 // Increment the pointer to the other half.
Dale Johannesen021052a2009-02-04 20:06:27 +0000813 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000814 DAG.getIntPtrConstant(4));
815 // Load the low part.
Chris Lattner7727d052010-09-21 06:44:06 +0000816 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000817 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000818
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000819 if (VA.isRegLoc()) {
820 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
821 assert(i+1 != e);
822 CCValAssign &NextVA = ArgLocs[++i];
823 if (NextVA.isRegLoc()) {
824 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
825 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000826 // Store the low part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000827 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
828 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
829 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
830 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
831 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
832 MachinePointerInfo(),
833 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000834 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000835 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000836 unsigned Offset = VA.getLocMemOffset() + StackOffset;
837 // Store the high part.
838 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
839 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
840 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
841 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
842 MachinePointerInfo(),
843 false, false, 0));
844 // Store the low part.
845 PtrOff = DAG.getIntPtrConstant(Offset+4);
846 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
847 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
848 MachinePointerInfo(),
849 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000850 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000851 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000852 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000853
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000854 // Arguments that can be passed on register must be kept at
855 // RegsToPass vector
856 if (VA.isRegLoc()) {
857 if (VA.getLocVT() != MVT::f32) {
858 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
859 continue;
860 }
861 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
862 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
863 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000864 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000865
866 assert(VA.isMemLoc());
867
868 // Create a store off the stack pointer for this argument.
869 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
870 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
871 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
872 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
873 MachinePointerInfo(),
874 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000875 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000876
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000877
Chris Lattner49b269d2008-03-17 05:41:48 +0000878 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000879 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000880 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner7d4152b2008-03-17 06:58:37 +0000881 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000882
883 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000884 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000885 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000886 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000887 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000888 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000889 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000890 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000891 InFlag = Chain.getValue(1);
892 }
893
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000894 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000895 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000896
Chris Lattner49b269d2008-03-17 05:41:48 +0000897 // If the callee is a GlobalAddress node (quite common, every direct call is)
898 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000899 // Likewise ExternalSymbol -> TargetExternalSymbol.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000900 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
901 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Chris Lattner49b269d2008-03-17 05:41:48 +0000902 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000903 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32, 0, TF);
Bill Wendling24c79f22008-09-16 21:48:12 +0000904 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +0000905 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32, TF);
Chris Lattner49b269d2008-03-17 05:41:48 +0000906
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000907 // Returns a chain & a flag for retval copy to use
908 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
909 SmallVector<SDValue, 8> Ops;
910 Ops.push_back(Chain);
911 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000912 if (hasStructRetAttr)
913 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000914 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
915 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
916 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000917
918 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000919 const SparcRegisterInfo *TRI =
920 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
921 const uint32_t *Mask = ((hasReturnsTwice)
922 ? TRI->getRTCallPreservedMask(CallConv)
923 : TRI->getCallPreservedMask(CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000924 assert(Mask && "Missing call preserved mask for calling convention");
925 Ops.push_back(DAG.getRegisterMask(Mask));
926
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000927 if (InFlag.getNode())
928 Ops.push_back(InFlag);
929
930 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000931 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000932
Chris Lattner27539552008-10-11 22:08:30 +0000933 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000934 DAG.getIntPtrConstant(0, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000935 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000936
Chris Lattnerdb26db22008-03-17 06:01:07 +0000937 // Assign locations to each value returned by this call.
938 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000939 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000940 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000941
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000942 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000943
Chris Lattnerdb26db22008-03-17 06:01:07 +0000944 // Copy all of the result registers out of their specified physreg.
945 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000946 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
Chris Lattnerdb26db22008-03-17 06:01:07 +0000947 RVLocs[i].getValVT(), InFlag).getValue(1);
948 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000949 InVals.push_back(Chain.getValue(0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000950 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000951
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000952 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000953}
954
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000955// This functions returns true if CalleeName is a ABI function that returns
956// a long double (fp128).
957static bool isFP128ABICall(const char *CalleeName)
958{
959 static const char *const ABICalls[] =
960 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
961 "_Q_sqrt", "_Q_neg",
962 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000963 "_Q_lltoq", "_Q_ulltoq",
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000964 0
965 };
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000966 for (const char * const *I = ABICalls; *I != 0; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000967 if (strcmp(CalleeName, *I) == 0)
968 return true;
969 return false;
970}
971
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000972unsigned
973SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
974{
975 const Function *CalleeFn = 0;
976 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
977 CalleeFn = dyn_cast<Function>(G->getGlobal());
978 } else if (ExternalSymbolSDNode *E =
979 dyn_cast<ExternalSymbolSDNode>(Callee)) {
980 const Function *Fn = DAG.getMachineFunction().getFunction();
981 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000982 const char *CalleeName = E->getSymbol();
983 CalleeFn = M->getFunction(CalleeName);
984 if (!CalleeFn && isFP128ABICall(CalleeName))
985 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000986 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000987
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000988 if (!CalleeFn)
989 return 0;
990
991 assert(CalleeFn->hasStructRetAttr() &&
992 "Callee does not have the StructRet attribute.");
993
Chris Lattner229907c2011-07-18 04:54:35 +0000994 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
995 Type *ElementTy = Ty->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000996 return getDataLayout()->getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000997}
Chris Lattner49b269d2008-03-17 05:41:48 +0000998
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +0000999
1000// Fixup floating point arguments in the ... part of a varargs call.
1001//
1002// The SPARC v9 ABI requires that floating point arguments are treated the same
1003// as integers when calling a varargs function. This does not apply to the
1004// fixed arguments that are part of the function's prototype.
1005//
1006// This function post-processes a CCValAssign array created by
1007// AnalyzeCallOperands().
1008static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1009 ArrayRef<ISD::OutputArg> Outs) {
1010 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1011 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001012 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001013 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1014 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001015 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001016 continue;
1017 // The fixed arguments to a varargs function still go in FP registers.
1018 if (Outs[VA.getValNo()].IsFixed)
1019 continue;
1020
1021 // This floating point argument should be reassigned.
1022 CCValAssign NewVA;
1023
1024 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001025 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1026 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1027 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001028 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1029
1030 if (Offset < 6*8) {
1031 // This argument should go in %i0-%i5.
1032 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001033 if (ValTy == MVT::f64)
1034 // Full register, just bitconvert into i64.
1035 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1036 IReg, MVT::i64, CCValAssign::BCvt);
1037 else {
1038 assert(ValTy == MVT::f128 && "Unexpected type!");
1039 // Full register, just bitconvert into i128 -- We will lower this into
1040 // two i64s in LowerCall_64.
1041 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1042 IReg, MVT::i128, CCValAssign::BCvt);
1043 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001044 } else {
1045 // This needs to go to memory, we're out of integer registers.
1046 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1047 Offset, VA.getLocVT(), VA.getLocInfo());
1048 }
1049 ArgLocs[i] = NewVA;
1050 }
1051}
1052
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001053// Lower a call for the 64-bit ABI.
1054SDValue
1055SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1056 SmallVectorImpl<SDValue> &InVals) const {
1057 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001058 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001059 SDValue Chain = CLI.Chain;
1060
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001061 // Sparc target does not yet support tail call optimization.
1062 CLI.IsTailCall = false;
1063
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001064 // Analyze operands of the call, assigning locations to each operand.
1065 SmallVector<CCValAssign, 16> ArgLocs;
1066 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1067 DAG.getTarget(), ArgLocs, *DAG.getContext());
1068 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1069
1070 // Get the size of the outgoing arguments stack space requirement.
1071 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001072 // Called functions expect 6 argument words to exist in the stack frame, used
1073 // or not.
1074 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001075
1076 // Keep stack frames 16-byte aligned.
1077 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1078
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001079 // Varargs calls require special treatment.
1080 if (CLI.IsVarArg)
1081 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1082
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001083 // Adjust the stack pointer to make room for the arguments.
1084 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1085 // with more than 6 arguments.
Andrew Trickad6d08a2013-05-29 22:03:55 +00001086 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1087 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001088
1089 // Collect the set of registers to pass to the function and their values.
1090 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1091 // instruction.
1092 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1093
1094 // Collect chains from all the memory opeations that copy arguments to the
1095 // stack. They must follow the stack pointer adjustment above and precede the
1096 // call instruction itself.
1097 SmallVector<SDValue, 8> MemOpChains;
1098
1099 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1100 const CCValAssign &VA = ArgLocs[i];
1101 SDValue Arg = CLI.OutVals[i];
1102
1103 // Promote the value if needed.
1104 switch (VA.getLocInfo()) {
1105 default:
1106 llvm_unreachable("Unknown location info!");
1107 case CCValAssign::Full:
1108 break;
1109 case CCValAssign::SExt:
1110 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1111 break;
1112 case CCValAssign::ZExt:
1113 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1114 break;
1115 case CCValAssign::AExt:
1116 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1117 break;
1118 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001119 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1120 // SPARC does not support i128 natively. Lower it into two i64, see below.
1121 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1122 || VA.getLocVT() != MVT::i128)
1123 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001124 break;
1125 }
1126
1127 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001128 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1129 && VA.getLocVT() == MVT::i128) {
1130 // Store and reload into the interger register reg and reg+1.
1131 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1132 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
1133 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1134 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset);
1135 HiPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1136 HiPtrOff);
1137 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8);
1138 LoPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1139 LoPtrOff);
1140
1141 // Store to %sp+BIAS+128+Offset
1142 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1143 MachinePointerInfo(),
1144 false, false, 0);
1145 // Load into Reg and Reg+1
1146 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1147 MachinePointerInfo(),
1148 false, false, false, 0);
1149 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1150 MachinePointerInfo(),
1151 false, false, false, 0);
1152 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1153 Hi64));
1154 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1155 Lo64));
1156 continue;
1157 }
1158
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001159 // The custom bit on an i32 return value indicates that it should be
1160 // passed in the high bits of the register.
1161 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1162 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1163 DAG.getConstant(32, MVT::i32));
1164
1165 // The next value may go in the low bits of the same register.
1166 // Handle both at once.
1167 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1168 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1169 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1170 CLI.OutVals[i+1]);
1171 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1172 // Skip the next value, it's already done.
1173 ++i;
1174 }
1175 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001176 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001177 continue;
1178 }
1179
1180 assert(VA.isMemLoc());
1181
1182 // Create a store off the stack pointer for this argument.
1183 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1184 // The argument area starts at %fp+BIAS+128 in the callee frame,
1185 // %sp+BIAS+128 in ours.
1186 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1187 Subtarget->getStackPointerBias() +
1188 128);
1189 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1190 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1191 MachinePointerInfo(),
1192 false, false, 0));
1193 }
1194
1195 // Emit all stores, make sure they occur before the call.
1196 if (!MemOpChains.empty())
1197 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1198 &MemOpChains[0], MemOpChains.size());
1199
1200 // Build a sequence of CopyToReg nodes glued together with token chain and
1201 // glue operands which copy the outgoing args into registers. The InGlue is
1202 // necessary since all emitted instructions must be stuck together in order
1203 // to pass the live physical registers.
1204 SDValue InGlue;
1205 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1206 Chain = DAG.getCopyToReg(Chain, DL,
1207 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1208 InGlue = Chain.getValue(1);
1209 }
1210
1211 // If the callee is a GlobalAddress node (quite common, every direct call is)
1212 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1213 // Likewise ExternalSymbol -> TargetExternalSymbol.
1214 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001215 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001216 unsigned TF = ((getTargetMachine().getRelocationModel() == Reloc::PIC_)
1217 ? SparcMCExpr::VK_Sparc_WPLT30 : 0);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001218 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001219 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy(), 0,
1220 TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001221 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001222 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy(), TF);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001223
1224 // Build the operands for the call instruction itself.
1225 SmallVector<SDValue, 8> Ops;
1226 Ops.push_back(Chain);
1227 Ops.push_back(Callee);
1228 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1229 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1230 RegsToPass[i].second.getValueType()));
1231
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001232 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001233 const SparcRegisterInfo *TRI =
1234 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1235 const uint32_t *Mask = ((hasReturnsTwice)
1236 ? TRI->getRTCallPreservedMask(CLI.CallConv)
1237 : TRI->getCallPreservedMask(CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001238 assert(Mask && "Missing call preserved mask for calling convention");
1239 Ops.push_back(DAG.getRegisterMask(Mask));
1240
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001241 // Make sure the CopyToReg nodes are glued to the call instruction which
1242 // consumes the registers.
1243 if (InGlue.getNode())
1244 Ops.push_back(InGlue);
1245
1246 // Now the call itself.
1247 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1248 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1249 InGlue = Chain.getValue(1);
1250
1251 // Revert the stack pointer immediately after the call.
1252 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001253 DAG.getIntPtrConstant(0, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001254 InGlue = Chain.getValue(1);
1255
1256 // Now extract the return values. This is more or less the same as
1257 // LowerFormalArguments_64.
1258
1259 // Assign locations to each value returned by this call.
1260 SmallVector<CCValAssign, 16> RVLocs;
1261 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1262 DAG.getTarget(), RVLocs, *DAG.getContext());
Venkatraman Govindaraju5ac9c8f2013-12-29 04:27:21 +00001263
1264 // Set inreg flag manually for codegen generated library calls that
1265 // return float.
1266 if (CLI.Ins.size() == 1 && CLI.Ins[0].VT == MVT::f32 && CLI.CS == 0)
1267 CLI.Ins[0].Flags.setInReg();
1268
Jakob Stoklund Olesene7084a12014-01-12 04:13:17 +00001269 RVInfo.AnalyzeCallResult(CLI.Ins, RetCC_Sparc64);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001270
1271 // Copy all of the result registers out of their specified physreg.
1272 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1273 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001274 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001275
1276 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1277 // reside in the same register in the high and low bits. Reuse the
1278 // CopyFromReg previous node to avoid duplicate copies.
1279 SDValue RV;
1280 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1281 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1282 RV = Chain.getValue(0);
1283
1284 // But usually we'll create a new CopyFromReg for a different register.
1285 if (!RV.getNode()) {
1286 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1287 Chain = RV.getValue(1);
1288 InGlue = Chain.getValue(2);
1289 }
1290
1291 // Get the high bits for i32 struct elements.
1292 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1293 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1294 DAG.getConstant(32, MVT::i32));
1295
1296 // The callee promoted the return value, so insert an Assert?ext SDNode so
1297 // we won't promote the value again in this function.
1298 switch (VA.getLocInfo()) {
1299 case CCValAssign::SExt:
1300 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1301 DAG.getValueType(VA.getValVT()));
1302 break;
1303 case CCValAssign::ZExt:
1304 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1305 DAG.getValueType(VA.getValVT()));
1306 break;
1307 default:
1308 break;
1309 }
1310
1311 // Truncate the register down to the return value type.
1312 if (VA.isExtInLoc())
1313 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1314
1315 InVals.push_back(RV);
1316 }
1317
1318 return Chain;
1319}
1320
Chris Lattner0a1762e2008-03-17 03:21:36 +00001321//===----------------------------------------------------------------------===//
1322// TargetLowering Implementation
1323//===----------------------------------------------------------------------===//
1324
1325/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1326/// condition.
1327static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1328 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001329 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001330 case ISD::SETEQ: return SPCC::ICC_E;
1331 case ISD::SETNE: return SPCC::ICC_NE;
1332 case ISD::SETLT: return SPCC::ICC_L;
1333 case ISD::SETGT: return SPCC::ICC_G;
1334 case ISD::SETLE: return SPCC::ICC_LE;
1335 case ISD::SETGE: return SPCC::ICC_GE;
1336 case ISD::SETULT: return SPCC::ICC_CS;
1337 case ISD::SETULE: return SPCC::ICC_LEU;
1338 case ISD::SETUGT: return SPCC::ICC_GU;
1339 case ISD::SETUGE: return SPCC::ICC_CC;
1340 }
1341}
1342
1343/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1344/// FCC condition.
1345static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1346 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001347 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001348 case ISD::SETEQ:
1349 case ISD::SETOEQ: return SPCC::FCC_E;
1350 case ISD::SETNE:
1351 case ISD::SETUNE: return SPCC::FCC_NE;
1352 case ISD::SETLT:
1353 case ISD::SETOLT: return SPCC::FCC_L;
1354 case ISD::SETGT:
1355 case ISD::SETOGT: return SPCC::FCC_G;
1356 case ISD::SETLE:
1357 case ISD::SETOLE: return SPCC::FCC_LE;
1358 case ISD::SETGE:
1359 case ISD::SETOGE: return SPCC::FCC_GE;
1360 case ISD::SETULT: return SPCC::FCC_UL;
1361 case ISD::SETULE: return SPCC::FCC_ULE;
1362 case ISD::SETUGT: return SPCC::FCC_UG;
1363 case ISD::SETUGE: return SPCC::FCC_UGE;
1364 case ISD::SETUO: return SPCC::FCC_U;
1365 case ISD::SETO: return SPCC::FCC_O;
1366 case ISD::SETONE: return SPCC::FCC_LG;
1367 case ISD::SETUEQ: return SPCC::FCC_UE;
1368 }
1369}
1370
Chris Lattner0a1762e2008-03-17 03:21:36 +00001371SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Venkatraman Govindarajufd5c1f92014-01-29 04:51:35 +00001372 : TargetLowering(TM, new SparcELFTargetObjectFile()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001373 Subtarget = &TM.getSubtarget<SparcSubtarget>();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001374
Chris Lattner0a1762e2008-03-17 03:21:36 +00001375 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001376 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1377 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1378 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001379 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001380 if (Subtarget->is64Bit())
1381 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001382
1383 // Turn FP extload into load/fextend
Owen Anderson9f944592009-08-11 20:47:22 +00001384 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001385 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1386
Chris Lattner0a1762e2008-03-17 03:21:36 +00001387 // Sparc doesn't have i1 sign extending load
Owen Anderson9f944592009-08-11 20:47:22 +00001388 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001389
Chris Lattner0a1762e2008-03-17 03:21:36 +00001390 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001391 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001392 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1393 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001394
1395 // Custom legalize GlobalAddress nodes into LO/HI parts.
Jakob Stoklund Olesen15b3e902013-04-13 19:02:23 +00001396 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1397 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1398 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001399 setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001400
Chris Lattner0a1762e2008-03-17 03:21:36 +00001401 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001402 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1403 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1404 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001405
1406 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001407 setOperationAction(ISD::UREM, MVT::i32, Expand);
1408 setOperationAction(ISD::SREM, MVT::i32, Expand);
1409 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1410 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001411
Roman Divacky2262cfa2013-10-31 19:22:33 +00001412 // ... nor does SparcV9.
1413 if (Subtarget->is64Bit()) {
1414 setOperationAction(ISD::UREM, MVT::i64, Expand);
1415 setOperationAction(ISD::SREM, MVT::i64, Expand);
1416 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1417 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1418 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001419
1420 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001421 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1422 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001423 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1424 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001425
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001426 // Custom Expand fp<->uint
1427 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1428 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001429 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1430 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001431
Wesley Peck527da1b2010-11-23 03:31:01 +00001432 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1433 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001434
Chris Lattner0a1762e2008-03-17 03:21:36 +00001435 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001436 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1437 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1438 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001439 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1440
Owen Anderson9f944592009-08-11 20:47:22 +00001441 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1442 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1443 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001444 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001445
Chris Lattner0a1762e2008-03-17 03:21:36 +00001446 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001447 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1448 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1449 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1450 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1451 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1452 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001453 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001454
Owen Anderson9f944592009-08-11 20:47:22 +00001455 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1456 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1457 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001458 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001459
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001460 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001461 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1462 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1463 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1464 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001465 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1466 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001467 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1468 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001469 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001470 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001471
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001472 setOperationAction(ISD::CTPOP, MVT::i64,
1473 Subtarget->usePopc() ? Legal : Expand);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001474 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1475 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1476 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1477 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1478 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001479 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1480 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001481 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001482 }
1483
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001484 // ATOMICs.
1485 // FIXME: We insert fences for each atomics and generate sub-optimal code
1486 // for PSO/TSO. Also, implement other atomicrmw operations.
1487
1488 setInsertFencesForAtomic(true);
1489
1490 setOperationAction(ISD::ATOMIC_SWAP, MVT::i32, Legal);
1491 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32,
1492 (Subtarget->isV9() ? Legal: Expand));
1493
1494
1495 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Legal);
1496
1497 // Custom Lower Atomic LOAD/STORE
1498 setOperationAction(ISD::ATOMIC_LOAD, MVT::i32, Custom);
1499 setOperationAction(ISD::ATOMIC_STORE, MVT::i32, Custom);
1500
1501 if (Subtarget->is64Bit()) {
1502 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Legal);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00001503 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Legal);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00001504 setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom);
1505 setOperationAction(ISD::ATOMIC_STORE, MVT::i64, Custom);
1506 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001507
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001508 if (!Subtarget->isV9()) {
1509 // SparcV8 does not have FNEGD and FABSD.
1510 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1511 setOperationAction(ISD::FABS, MVT::f64, Custom);
1512 }
1513
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001514 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1515 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1516 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1517 setOperationAction(ISD::FREM , MVT::f128, Expand);
1518 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001519 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1520 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001521 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001522 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001523 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001524 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1525 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001526 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001527 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001528 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001529 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001530 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001531 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001532 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001533 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1534 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1535 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001536 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001537 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1538 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001539 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001540 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1541 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001542
Owen Anderson9f944592009-08-11 20:47:22 +00001543 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1544 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1545 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001546
1547 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001548 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1549 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001550
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001551 if (Subtarget->is64Bit()) {
1552 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1553 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1554 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1555 setOperationAction(ISD::MULHS, MVT::i64, Expand);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00001556
1557 setOperationAction(ISD::UMULO, MVT::i64, Custom);
1558 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Roman Divacky37136c02014-02-19 21:35:39 +00001559
1560 setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
1561 setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
1562 setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001563 }
1564
Chris Lattner0a1762e2008-03-17 03:21:36 +00001565 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001566 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001567 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001568 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001569
Chris Lattner0a1762e2008-03-17 03:21:36 +00001570 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001571 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1572 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1573 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1574 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1575 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001576
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +00001577 setExceptionPointerRegister(SP::I0);
1578 setExceptionSelectorRegister(SP::I1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001579
Chris Lattner0a1762e2008-03-17 03:21:36 +00001580 setStackPointerRegisterToSaveRestore(SP::O6);
1581
Jakob Stoklund Olesen6f39ce42014-01-26 08:12:34 +00001582 setOperationAction(ISD::CTPOP, MVT::i32,
1583 Subtarget->usePopc() ? Legal : Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001584
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001585 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1586 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1587 setOperationAction(ISD::STORE, MVT::f128, Legal);
1588 } else {
1589 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1590 setOperationAction(ISD::STORE, MVT::f128, Custom);
1591 }
1592
1593 if (Subtarget->hasHardQuad()) {
1594 setOperationAction(ISD::FADD, MVT::f128, Legal);
1595 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1596 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1597 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1598 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1599 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1600 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1601 if (Subtarget->isV9()) {
1602 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1603 setOperationAction(ISD::FABS, MVT::f128, Legal);
1604 } else {
1605 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1606 setOperationAction(ISD::FABS, MVT::f128, Custom);
1607 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001608
1609 if (!Subtarget->is64Bit()) {
1610 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1611 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1612 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1613 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1614 }
1615
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001616 } else {
1617 // Custom legalize f128 operations.
1618
1619 setOperationAction(ISD::FADD, MVT::f128, Custom);
1620 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1621 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1622 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1623 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1624 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1625 setOperationAction(ISD::FABS, MVT::f128, Custom);
1626
1627 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1628 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1629 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1630
1631 // Setup Runtime library names.
1632 if (Subtarget->is64Bit()) {
1633 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1634 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1635 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1636 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1637 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1638 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001639 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001640 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001641 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001642 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1643 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1644 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1645 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001646 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1647 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1648 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1649 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1650 } else {
1651 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1652 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1653 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1654 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1655 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1656 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001657 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001658 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001659 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001660 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1661 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1662 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1663 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001664 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1665 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1666 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1667 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1668 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001669 }
1670
Eli Friedman2518f832011-05-06 20:34:06 +00001671 setMinFunctionAlignment(2);
1672
Chris Lattner0a1762e2008-03-17 03:21:36 +00001673 computeRegisterProperties();
1674}
1675
1676const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1677 switch (Opcode) {
1678 default: return 0;
1679 case SPISD::CMPICC: return "SPISD::CMPICC";
1680 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1681 case SPISD::BRICC: return "SPISD::BRICC";
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001682 case SPISD::BRXCC: return "SPISD::BRXCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001683 case SPISD::BRFCC: return "SPISD::BRFCC";
1684 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001685 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001686 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1687 case SPISD::Hi: return "SPISD::Hi";
1688 case SPISD::Lo: return "SPISD::Lo";
1689 case SPISD::FTOI: return "SPISD::FTOI";
1690 case SPISD::ITOF: return "SPISD::ITOF";
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001691 case SPISD::FTOX: return "SPISD::FTOX";
1692 case SPISD::XTOF: return "SPISD::XTOF";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001693 case SPISD::CALL: return "SPISD::CALL";
1694 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001695 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00001696 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001697 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1698 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1699 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001700 }
1701}
1702
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001703EVT SparcTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1704 if (!VT.isVector())
1705 return MVT::i32;
1706 return VT.changeVectorElementTypeToInteger();
1707}
1708
Chris Lattner0a1762e2008-03-17 03:21:36 +00001709/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1710/// be zero. Op is expected to be a target specific node. Used by DAG
1711/// combiner.
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001712void SparcTargetLowering::computeMaskedBitsForTargetNode
1713 (const SDValue Op,
1714 APInt &KnownZero,
1715 APInt &KnownOne,
1716 const SelectionDAG &DAG,
1717 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001718 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001719 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001720
Chris Lattner0a1762e2008-03-17 03:21:36 +00001721 switch (Op.getOpcode()) {
1722 default: break;
1723 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001724 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001725 case SPISD::SELECT_FCC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001726 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1727 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001728 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1729 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1730
Chris Lattner0a1762e2008-03-17 03:21:36 +00001731 // Only known if known in both the LHS and RHS.
1732 KnownOne &= KnownOne2;
1733 KnownZero &= KnownZero2;
1734 break;
1735 }
1736}
1737
Chris Lattner0a1762e2008-03-17 03:21:36 +00001738// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1739// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001740static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001741 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001742 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001743 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001744 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001745 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1746 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001747 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1748 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1749 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1750 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1751 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001752 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1753 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001754 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001755 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001756 LHS = CMPCC.getOperand(0);
1757 RHS = CMPCC.getOperand(1);
1758 }
1759}
1760
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001761// Convert to a target node and set target flags.
1762SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1763 SelectionDAG &DAG) const {
1764 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1765 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001766 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001767 GA->getValueType(0),
1768 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001769
1770 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1771 return DAG.getTargetConstantPool(CP->getConstVal(),
1772 CP->getValueType(0),
1773 CP->getAlignment(),
1774 CP->getOffset(), TF);
1775
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001776 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1777 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1778 Op.getValueType(),
1779 0,
1780 TF);
1781
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001782 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1783 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1784 ES->getValueType(0), TF);
1785
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001786 llvm_unreachable("Unhandled address SDNode");
1787}
1788
1789// Split Op into high and low parts according to HiTF and LoTF.
1790// Return an ADD node combining the parts.
1791SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1792 unsigned HiTF, unsigned LoTF,
1793 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001794 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001795 EVT VT = Op.getValueType();
1796 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1797 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1798 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1799}
1800
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001801// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1802// or ExternalSymbol SDNode.
1803SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001804 SDLoc DL(Op);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001805 EVT VT = getPointerTy();
1806
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001807 // Handle PIC mode first.
1808 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1809 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
Venkatraman Govindaraju104643d2014-02-07 04:24:35 +00001810 SDValue HiLo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_GOT22,
1811 SparcMCExpr::VK_Sparc_GOT10, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001812 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1813 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001814 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1815 // function has calls.
1816 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1817 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001818 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1819 MachinePointerInfo::getGOT(), false, false, false, 0);
1820 }
1821
1822 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001823 switch(getTargetMachine().getCodeModel()) {
1824 default:
1825 llvm_unreachable("Unsupported absolute code model");
1826 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001827 // abs32.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001828 return makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1829 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001830 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001831 // abs44.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001832 SDValue H44 = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_H44,
1833 SparcMCExpr::VK_Sparc_M44, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001834 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001835 SDValue L44 = withTargetFlags(Op, SparcMCExpr::VK_Sparc_L44, DAG);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001836 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1837 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1838 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001839 case CodeModel::Large: {
1840 // abs64.
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001841 SDValue Hi = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HH,
1842 SparcMCExpr::VK_Sparc_HM, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001843 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001844 SDValue Lo = makeHiLoPair(Op, SparcMCExpr::VK_Sparc_HI,
1845 SparcMCExpr::VK_Sparc_LO, DAG);
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001846 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1847 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001848 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001849}
1850
Wesley Peck527da1b2010-11-23 03:31:01 +00001851SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001852 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001853 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001854}
1855
Chris Lattner840c7002009-09-15 17:46:24 +00001856SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001857 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001858 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001859}
1860
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001861SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1862 SelectionDAG &DAG) const {
1863 return makeAddress(Op, DAG);
1864}
1865
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001866SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1867 SelectionDAG &DAG) const {
1868
1869 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1870 SDLoc DL(GA);
1871 const GlobalValue *GV = GA->getGlobal();
1872 EVT PtrVT = getPointerTy();
1873
1874 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1875
1876 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001877 unsigned HiTF = ((model == TLSModel::GeneralDynamic)
1878 ? SparcMCExpr::VK_Sparc_TLS_GD_HI22
1879 : SparcMCExpr::VK_Sparc_TLS_LDM_HI22);
1880 unsigned LoTF = ((model == TLSModel::GeneralDynamic)
1881 ? SparcMCExpr::VK_Sparc_TLS_GD_LO10
1882 : SparcMCExpr::VK_Sparc_TLS_LDM_LO10);
1883 unsigned addTF = ((model == TLSModel::GeneralDynamic)
1884 ? SparcMCExpr::VK_Sparc_TLS_GD_ADD
1885 : SparcMCExpr::VK_Sparc_TLS_LDM_ADD);
1886 unsigned callTF = ((model == TLSModel::GeneralDynamic)
1887 ? SparcMCExpr::VK_Sparc_TLS_GD_CALL
1888 : SparcMCExpr::VK_Sparc_TLS_LDM_CALL);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001889
1890 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1891 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1892 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1893 withTargetFlags(Op, addTF, DAG));
1894
1895 SDValue Chain = DAG.getEntryNode();
1896 SDValue InFlag;
1897
1898 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1899 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1900 InFlag = Chain.getValue(1);
1901 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1902 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1903
1904 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1905 SmallVector<SDValue, 4> Ops;
1906 Ops.push_back(Chain);
1907 Ops.push_back(Callee);
1908 Ops.push_back(Symbol);
1909 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1910 const uint32_t *Mask = getTargetMachine()
1911 .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1912 assert(Mask && "Missing call preserved mask for calling convention");
1913 Ops.push_back(DAG.getRegisterMask(Mask));
1914 Ops.push_back(InFlag);
1915 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1916 InFlag = Chain.getValue(1);
1917 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1918 DAG.getIntPtrConstant(0, true), InFlag, DL);
1919 InFlag = Chain.getValue(1);
1920 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1921
1922 if (model != TLSModel::LocalDynamic)
1923 return Ret;
1924
1925 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001926 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001927 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001928 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001929 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1930 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001931 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LDO_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001932 }
1933
1934 if (model == TLSModel::InitialExec) {
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001935 unsigned ldTF = ((PtrVT == MVT::i64)? SparcMCExpr::VK_Sparc_TLS_IE_LDX
1936 : SparcMCExpr::VK_Sparc_TLS_IE_LD);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001937
1938 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1939
1940 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1941 // function has calls.
1942 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1943 MFI->setHasCalls(true);
1944
1945 SDValue TGA = makeHiLoPair(Op,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001946 SparcMCExpr::VK_Sparc_TLS_IE_HI22,
1947 SparcMCExpr::VK_Sparc_TLS_IE_LO10, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001948 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1949 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1950 DL, PtrVT, Ptr,
1951 withTargetFlags(Op, ldTF, DAG));
1952 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1953 DAG.getRegister(SP::G7, PtrVT), Offset,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001954 withTargetFlags(Op,
1955 SparcMCExpr::VK_Sparc_TLS_IE_ADD, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001956 }
1957
1958 assert(model == TLSModel::LocalExec);
1959 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001960 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_HIX22, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001961 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
Venkatraman Govindarajudfe09b12014-02-07 02:36:06 +00001962 withTargetFlags(Op, SparcMCExpr::VK_Sparc_TLS_LE_LOX10, DAG));
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001963 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1964
1965 return DAG.getNode(ISD::ADD, DL, PtrVT,
1966 DAG.getRegister(SP::G7, PtrVT), Offset);
1967}
1968
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001969SDValue
1970SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1971 SDValue Arg, SDLoc DL,
1972 SelectionDAG &DAG) const {
1973 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1974 EVT ArgVT = Arg.getValueType();
1975 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1976
1977 ArgListEntry Entry;
1978 Entry.Node = Arg;
1979 Entry.Ty = ArgTy;
1980
1981 if (ArgTy->isFP128Ty()) {
1982 // Create a stack object and pass the pointer to the library function.
1983 int FI = MFI->CreateStackObject(16, 8, false);
1984 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1985 Chain = DAG.getStore(Chain,
1986 DL,
1987 Entry.Node,
1988 FIPtr,
1989 MachinePointerInfo(),
1990 false,
1991 false,
1992 8);
1993
1994 Entry.Node = FIPtr;
1995 Entry.Ty = PointerType::getUnqual(ArgTy);
1996 }
1997 Args.push_back(Entry);
1998 return Chain;
1999}
2000
2001SDValue
2002SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
2003 const char *LibFuncName,
2004 unsigned numArgs) const {
2005
2006 ArgListTy Args;
2007
2008 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2009
2010 SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
2011 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
2012 Type *RetTyABI = RetTy;
2013 SDValue Chain = DAG.getEntryNode();
2014 SDValue RetPtr;
2015
2016 if (RetTy->isFP128Ty()) {
2017 // Create a Stack Object to receive the return value of type f128.
2018 ArgListEntry Entry;
2019 int RetFI = MFI->CreateStackObject(16, 8, false);
2020 RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
2021 Entry.Node = RetPtr;
2022 Entry.Ty = PointerType::getUnqual(RetTy);
2023 if (!Subtarget->is64Bit())
2024 Entry.isSRet = true;
2025 Entry.isReturned = false;
2026 Args.push_back(Entry);
2027 RetTyABI = Type::getVoidTy(*DAG.getContext());
2028 }
2029
2030 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
2031 for (unsigned i = 0, e = numArgs; i != e; ++i) {
2032 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
2033 }
2034 TargetLowering::
2035 CallLoweringInfo CLI(Chain,
2036 RetTyABI,
2037 false, false, false, false,
2038 0, CallingConv::C,
2039 false, false, true,
2040 Callee, Args, DAG, SDLoc(Op));
2041 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2042
2043 // chain is in second result.
2044 if (RetTyABI == RetTy)
2045 return CallInfo.first;
2046
2047 assert (RetTy->isFP128Ty() && "Unexpected return type!");
2048
2049 Chain = CallInfo.second;
2050
2051 // Load RetPtr to get the return value.
2052 return DAG.getLoad(Op.getValueType(),
2053 SDLoc(Op),
2054 Chain,
2055 RetPtr,
2056 MachinePointerInfo(),
2057 false, false, false, 8);
2058}
2059
2060SDValue
2061SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2062 unsigned &SPCC,
2063 SDLoc DL,
2064 SelectionDAG &DAG) const {
2065
2066 const char *LibCall = 0;
2067 bool is64Bit = Subtarget->is64Bit();
2068 switch(SPCC) {
2069 default: llvm_unreachable("Unhandled conditional code!");
2070 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2071 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2072 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2073 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2074 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2075 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2076 case SPCC::FCC_UL :
2077 case SPCC::FCC_ULE:
2078 case SPCC::FCC_UG :
2079 case SPCC::FCC_UGE:
2080 case SPCC::FCC_U :
2081 case SPCC::FCC_O :
2082 case SPCC::FCC_LG :
2083 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2084 }
2085
2086 SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
2087 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2088 ArgListTy Args;
2089 SDValue Chain = DAG.getEntryNode();
2090 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2091 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2092
2093 TargetLowering::
2094 CallLoweringInfo CLI(Chain,
2095 RetTy,
2096 false, false, false, false,
2097 0, CallingConv::C,
2098 false, false, true,
2099 Callee, Args, DAG, DL);
2100
2101 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2102
2103 // result is in first, and chain is in second result.
2104 SDValue Result = CallInfo.first;
2105
2106 switch(SPCC) {
2107 default: {
2108 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2109 SPCC = SPCC::ICC_NE;
2110 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2111 }
2112 case SPCC::FCC_UL : {
2113 SDValue Mask = DAG.getTargetConstant(1, Result.getValueType());
2114 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2115 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2116 SPCC = SPCC::ICC_NE;
2117 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2118 }
2119 case SPCC::FCC_ULE: {
Venkatraman Govindarajub803cec2013-09-04 15:15:20 +00002120 SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002121 SPCC = SPCC::ICC_NE;
2122 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2123 }
2124 case SPCC::FCC_UG : {
2125 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2126 SPCC = SPCC::ICC_G;
2127 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2128 }
2129 case SPCC::FCC_UGE: {
2130 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2131 SPCC = SPCC::ICC_NE;
2132 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2133 }
2134
2135 case SPCC::FCC_U : {
2136 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2137 SPCC = SPCC::ICC_E;
2138 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2139 }
2140 case SPCC::FCC_O : {
2141 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2142 SPCC = SPCC::ICC_NE;
2143 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2144 }
2145 case SPCC::FCC_LG : {
2146 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2147 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2148 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2149 SPCC = SPCC::ICC_NE;
2150 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2151 }
2152 case SPCC::FCC_UE : {
2153 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2154 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2155 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2156 SPCC = SPCC::ICC_E;
2157 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2158 }
2159 }
2160}
2161
2162static SDValue
2163LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2164 const SparcTargetLowering &TLI) {
2165
2166 if (Op.getOperand(0).getValueType() == MVT::f64)
2167 return TLI.LowerF128Op(Op, DAG,
2168 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2169
2170 if (Op.getOperand(0).getValueType() == MVT::f32)
2171 return TLI.LowerF128Op(Op, DAG,
2172 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2173
2174 llvm_unreachable("fpextend with non-float operand!");
2175 return SDValue(0, 0);
2176}
2177
2178static SDValue
2179LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2180 const SparcTargetLowering &TLI) {
2181 // FP_ROUND on f64 and f32 are legal.
2182 if (Op.getOperand(0).getValueType() != MVT::f128)
2183 return Op;
2184
2185 if (Op.getValueType() == MVT::f64)
2186 return TLI.LowerF128Op(Op, DAG,
2187 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2188 if (Op.getValueType() == MVT::f32)
2189 return TLI.LowerF128Op(Op, DAG,
2190 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2191
2192 llvm_unreachable("fpround to non-float!");
2193 return SDValue(0, 0);
2194}
2195
2196static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2197 const SparcTargetLowering &TLI,
2198 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002199 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002200 EVT VT = Op.getValueType();
2201 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002202
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002203 // Expand f128 operations to fp128 abi calls.
2204 if (Op.getOperand(0).getValueType() == MVT::f128
2205 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2206 const char *libName = TLI.getLibcallName(VT == MVT::i32
2207 ? RTLIB::FPTOSINT_F128_I32
2208 : RTLIB::FPTOSINT_F128_I64);
2209 return TLI.LowerF128Op(Op, DAG, libName, 1);
2210 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002211
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002212 // Expand if the resulting type is illegal.
2213 if (!TLI.isTypeLegal(VT))
2214 return SDValue(0, 0);
2215
2216 // Otherwise, Convert the fp value to integer in an FP register.
2217 if (VT == MVT::i32)
2218 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2219 else
2220 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2221
2222 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002223}
2224
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002225static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2226 const SparcTargetLowering &TLI,
2227 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002228 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002229 EVT OpVT = Op.getOperand(0).getValueType();
2230 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2231
2232 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2233
2234 // Expand f128 operations to fp128 ABI calls.
2235 if (Op.getValueType() == MVT::f128
2236 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2237 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2238 ? RTLIB::SINTTOFP_I32_F128
2239 : RTLIB::SINTTOFP_I64_F128);
2240 return TLI.LowerF128Op(Op, DAG, libName, 1);
2241 }
2242
2243 // Expand if the operand type is illegal.
2244 if (!TLI.isTypeLegal(OpVT))
2245 return SDValue(0, 0);
2246
2247 // Otherwise, Convert the int value to FP in an FP register.
2248 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2249 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2250 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002251}
2252
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002253static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2254 const SparcTargetLowering &TLI,
2255 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002256 SDLoc dl(Op);
2257 EVT VT = Op.getValueType();
2258
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002259 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002260 // quad floating point instructions and the resulting type is legal.
2261 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2262 (hasHardQuad && TLI.isTypeLegal(VT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002263 return SDValue(0, 0);
2264
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002265 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002266
2267 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002268 TLI.getLibcallName(VT == MVT::i32
2269 ? RTLIB::FPTOUINT_F128_I32
2270 : RTLIB::FPTOUINT_F128_I64),
2271 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002272}
2273
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002274static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2275 const SparcTargetLowering &TLI,
2276 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002277 SDLoc dl(Op);
2278 EVT OpVT = Op.getOperand(0).getValueType();
2279 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2280
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002281 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002282 // quad floating point instructions and the operand type is legal.
2283 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002284 return SDValue(0, 0);
2285
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002286 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002287 TLI.getLibcallName(OpVT == MVT::i32
2288 ? RTLIB::UINTTOFP_I32_F128
2289 : RTLIB::UINTTOFP_I64_F128),
2290 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002291}
2292
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002293static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2294 const SparcTargetLowering &TLI,
2295 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002296 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002297 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002298 SDValue LHS = Op.getOperand(2);
2299 SDValue RHS = Op.getOperand(3);
2300 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002301 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002302 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002303
Chris Lattner0a1762e2008-03-17 03:21:36 +00002304 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2305 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2306 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002307
Chris Lattner0a1762e2008-03-17 03:21:36 +00002308 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002309 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002310 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002311 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002312 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002313 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2314 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002315 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002316 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2317 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2318 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2319 Opc = SPISD::BRICC;
2320 } else {
2321 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2322 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2323 Opc = SPISD::BRFCC;
2324 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002325 }
Owen Anderson9f944592009-08-11 20:47:22 +00002326 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2327 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002328}
2329
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002330static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2331 const SparcTargetLowering &TLI,
2332 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002333 SDValue LHS = Op.getOperand(0);
2334 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002335 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002336 SDValue TrueVal = Op.getOperand(2);
2337 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002338 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002339 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002340
Chris Lattner0a1762e2008-03-17 03:21:36 +00002341 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2342 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2343 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002344
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002345 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002346 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002347 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002348 Opc = LHS.getValueType() == MVT::i32 ?
2349 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002350 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2351 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002352 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2353 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2354 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2355 Opc = SPISD::SELECT_ICC;
2356 } else {
2357 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2358 Opc = SPISD::SELECT_FCC;
2359 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2360 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002361 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002362 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson9f944592009-08-11 20:47:22 +00002363 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002364}
2365
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002366static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002367 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002368 MachineFunction &MF = DAG.getMachineFunction();
2369 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2370
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002371 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002372 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2373
Chris Lattner0a1762e2008-03-17 03:21:36 +00002374 // vastart just stores the address of the VarArgsFrameIndex slot into the
2375 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002376 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002377 SDValue Offset =
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002378 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2379 DAG.getRegister(SP::I6, TLI.getPointerTy()),
2380 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002381 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002382 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002383 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002384}
2385
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002386static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002387 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002388 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002389 SDValue InChain = Node->getOperand(0);
2390 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002391 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002392 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002393 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002394 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002395 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002396 // Increment the pointer, VAList, to the next vaarg.
2397 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2398 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2399 // Store the incremented VAList to the legalized pointer.
2400 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002401 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002402 // Load the actual argument out of the pointer VAList.
2403 // We can't count on greater alignment than the word size.
2404 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2405 false, false, false,
2406 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002407}
2408
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002409static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002410 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002411 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2412 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002413 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002414 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002415
Chris Lattner0a1762e2008-03-17 03:21:36 +00002416 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002417 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2418 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002419 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002420
Chris Lattner0a1762e2008-03-17 03:21:36 +00002421 // The resultant pointer is actually 16 words from the bottom of the stack,
2422 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002423 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2424 regSpillArea += Subtarget->getStackPointerBias();
2425
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002426 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
2427 DAG.getConstant(regSpillArea, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002428 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002429 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002430}
2431
Chris Lattner0a1762e2008-03-17 03:21:36 +00002432
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002433static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002434 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002435 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002436 dl, MVT::Other, DAG.getEntryNode());
2437 return Chain;
2438}
2439
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002440static SDValue getFRAMEADDR(uint64_t depth, SDValue Op, SelectionDAG &DAG,
2441 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002442 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2443 MFI->setFrameAddressIsTaken(true);
2444
2445 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002446 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002447 unsigned FrameReg = SP::I6;
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002448 unsigned stackBias = Subtarget->getStackPointerBias();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002449
2450 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002451
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002452 if (depth == 0) {
2453 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2454 if (Subtarget->is64Bit())
2455 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2456 DAG.getIntPtrConstant(stackBias));
2457 return FrameAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002458 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002459
2460 // flush first to make sure the windowed registers' values are in stack
2461 SDValue Chain = getFLUSHW(Op, DAG);
2462 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
2463
2464 unsigned Offset = (Subtarget->is64Bit()) ? (stackBias + 112) : 56;
2465
2466 while (depth--) {
2467 SDValue Ptr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2468 DAG.getIntPtrConstant(Offset));
2469 FrameAddr = DAG.getLoad(VT, dl, Chain, Ptr, MachinePointerInfo(),
2470 false, false, false, 0);
2471 }
2472 if (Subtarget->is64Bit())
2473 FrameAddr = DAG.getNode(ISD::ADD, dl, VT, FrameAddr,
2474 DAG.getIntPtrConstant(stackBias));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002475 return FrameAddr;
2476}
2477
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002478
2479static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG,
2480 const SparcSubtarget *Subtarget) {
2481
2482 uint64_t depth = Op.getConstantOperandVal(0);
2483
2484 return getFRAMEADDR(depth, Op, DAG, Subtarget);
2485
2486}
2487
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002488static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002489 const SparcTargetLowering &TLI,
2490 const SparcSubtarget *Subtarget) {
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002491 MachineFunction &MF = DAG.getMachineFunction();
2492 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002493 MFI->setReturnAddressIsTaken(true);
2494
Bill Wendling908bf812014-01-06 00:43:20 +00002495 if (TLI.verifyReturnAddressArgumentIsConstant(Op, DAG))
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002496 return SDValue();
Bill Wendlingdf7dd282014-01-05 01:47:20 +00002497
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002498 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002499 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002500 uint64_t depth = Op.getConstantOperandVal(0);
2501
2502 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002503 if (depth == 0) {
2504 unsigned RetReg = MF.addLiveIn(SP::I7,
2505 TLI.getRegClassFor(TLI.getPointerTy()));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002506 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002507 return RetAddr;
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002508 }
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002509
2510 // Need frame address to find return address of the caller.
2511 SDValue FrameAddr = getFRAMEADDR(depth - 1, Op, DAG, Subtarget);
2512
2513 unsigned Offset = (Subtarget->is64Bit()) ? 120 : 60;
2514 SDValue Ptr = DAG.getNode(ISD::ADD,
2515 dl, VT,
2516 FrameAddr,
2517 DAG.getIntPtrConstant(Offset));
2518 RetAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), Ptr,
2519 MachinePointerInfo(), false, false, false, 0);
2520
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002521 return RetAddr;
2522}
2523
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002524static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002525{
2526 SDLoc dl(Op);
2527
2528 assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002529 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002530
2531 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2532 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2533 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2534
2535 SDValue SrcReg64 = Op.getOperand(0);
2536 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2537 SrcReg64);
2538 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2539 SrcReg64);
2540
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002541 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002542
2543 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2544 dl, MVT::f64), 0);
2545 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2546 DstReg64, Hi32);
2547 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2548 DstReg64, Lo32);
2549 return DstReg64;
2550}
2551
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002552// Lower a f128 load into two f64 loads.
2553static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2554{
2555 SDLoc dl(Op);
2556 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2557 assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2558 && "Unexpected node type");
2559
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002560 unsigned alignment = LdNode->getAlignment();
2561 if (alignment > 8)
2562 alignment = 8;
2563
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002564 SDValue Hi64 = DAG.getLoad(MVT::f64,
2565 dl,
2566 LdNode->getChain(),
2567 LdNode->getBasePtr(),
2568 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002569 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002570 EVT addrVT = LdNode->getBasePtr().getValueType();
2571 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2572 LdNode->getBasePtr(),
2573 DAG.getConstant(8, addrVT));
2574 SDValue Lo64 = DAG.getLoad(MVT::f64,
2575 dl,
2576 LdNode->getChain(),
2577 LoPtr,
2578 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002579 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002580
2581 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2582 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2583
2584 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2585 dl, MVT::f128);
2586 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2587 MVT::f128,
2588 SDValue(InFP128, 0),
2589 Hi64,
2590 SubRegEven);
2591 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2592 MVT::f128,
2593 SDValue(InFP128, 0),
2594 Lo64,
2595 SubRegOdd);
2596 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2597 SDValue(Lo64.getNode(), 1) };
2598 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2599 &OutChains[0], 2);
2600 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2601 return DAG.getMergeValues(Ops, 2, dl);
2602}
2603
2604// Lower a f128 store into two f64 stores.
2605static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2606 SDLoc dl(Op);
2607 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2608 assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2609 && "Unexpected node type");
2610 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2611 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2612
2613 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2614 dl,
2615 MVT::f64,
2616 StNode->getValue(),
2617 SubRegEven);
2618 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2619 dl,
2620 MVT::f64,
2621 StNode->getValue(),
2622 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002623
2624 unsigned alignment = StNode->getAlignment();
2625 if (alignment > 8)
2626 alignment = 8;
2627
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002628 SDValue OutChains[2];
2629 OutChains[0] = DAG.getStore(StNode->getChain(),
2630 dl,
2631 SDValue(Hi64, 0),
2632 StNode->getBasePtr(),
2633 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002634 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002635 EVT addrVT = StNode->getBasePtr().getValueType();
2636 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2637 StNode->getBasePtr(),
2638 DAG.getConstant(8, addrVT));
2639 OutChains[1] = DAG.getStore(StNode->getChain(),
2640 dl,
2641 SDValue(Lo64, 0),
2642 LoPtr,
2643 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002644 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002645 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2646 &OutChains[0], 2);
2647}
2648
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002649static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2650 const SparcTargetLowering &TLI,
2651 bool is64Bit) {
2652 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002653 return LowerF64Op(Op, DAG, ISD::FNEG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002654 if (Op.getValueType() == MVT::f128)
2655 return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2656 return Op;
2657}
2658
2659static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2660 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002661 return LowerF64Op(Op, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002662 if (Op.getValueType() != MVT::f128)
2663 return Op;
2664
2665 // Lower fabs on f128 to fabs on f64
2666 // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2667
2668 SDLoc dl(Op);
2669 SDValue SrcReg128 = Op.getOperand(0);
2670 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2671 SrcReg128);
2672 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2673 SrcReg128);
2674 if (isV9)
2675 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2676 else
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002677 Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002678
2679 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2680 dl, MVT::f128), 0);
2681 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2682 DstReg128, Hi64);
2683 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2684 DstReg128, Lo64);
2685 return DstReg128;
2686}
2687
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002688static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002689
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002690 if (Op.getValueType() != MVT::i64)
2691 return Op;
2692
2693 SDLoc dl(Op);
2694 SDValue Src1 = Op.getOperand(0);
2695 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2696 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2697 DAG.getConstant(32, MVT::i64));
2698 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2699
2700 SDValue Src2 = Op.getOperand(1);
2701 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2702 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2703 DAG.getConstant(32, MVT::i64));
2704 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2705
2706
2707 bool hasChain = false;
2708 unsigned hiOpc = Op.getOpcode();
2709 switch (Op.getOpcode()) {
2710 default: llvm_unreachable("Invalid opcode");
2711 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2712 case ISD::ADDE: hasChain = true; break;
2713 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2714 case ISD::SUBE: hasChain = true; break;
2715 }
2716 SDValue Lo;
2717 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2718 if (hasChain) {
2719 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2720 Op.getOperand(2));
2721 } else {
2722 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2723 }
2724 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2725 SDValue Carry = Hi.getValue(1);
2726
2727 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2728 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2729 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2730 DAG.getConstant(32, MVT::i64));
2731
2732 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2733 SDValue Ops[2] = { Dst, Carry };
2734 return DAG.getMergeValues(Ops, 2, dl);
2735}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002736
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002737// Custom lower UMULO/SMULO for SPARC. This code is similar to ExpandNode()
2738// in LegalizeDAG.cpp except the order of arguments to the library function.
2739static SDValue LowerUMULO_SMULO(SDValue Op, SelectionDAG &DAG,
2740 const SparcTargetLowering &TLI)
2741{
2742 unsigned opcode = Op.getOpcode();
2743 assert((opcode == ISD::UMULO || opcode == ISD::SMULO) && "Invalid Opcode.");
2744
2745 bool isSigned = (opcode == ISD::SMULO);
2746 EVT VT = MVT::i64;
2747 EVT WideVT = MVT::i128;
2748 SDLoc dl(Op);
2749 SDValue LHS = Op.getOperand(0);
2750
2751 if (LHS.getValueType() != VT)
2752 return Op;
2753
2754 SDValue ShiftAmt = DAG.getConstant(63, VT);
2755
2756 SDValue RHS = Op.getOperand(1);
2757 SDValue HiLHS = DAG.getNode(ISD::SRA, dl, VT, LHS, ShiftAmt);
2758 SDValue HiRHS = DAG.getNode(ISD::SRA, dl, MVT::i64, RHS, ShiftAmt);
2759 SDValue Args[] = { HiLHS, LHS, HiRHS, RHS };
2760
2761 SDValue MulResult = TLI.makeLibCall(DAG,
2762 RTLIB::MUL_I128, WideVT,
2763 Args, 4, isSigned, dl).first;
2764 SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
2765 MulResult, DAG.getIntPtrConstant(0));
2766 SDValue TopHalf = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, VT,
2767 MulResult, DAG.getIntPtrConstant(1));
2768 if (isSigned) {
2769 SDValue Tmp1 = DAG.getNode(ISD::SRA, dl, VT, BottomHalf, ShiftAmt);
2770 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, Tmp1, ISD::SETNE);
2771 } else {
2772 TopHalf = DAG.getSetCC(dl, MVT::i32, TopHalf, DAG.getConstant(0, VT),
2773 ISD::SETNE);
2774 }
2775 // MulResult is a node with an illegal type. Because such things are not
2776 // generally permitted during this phase of legalization, delete the
2777 // node. The above EXTRACT_ELEMENT nodes should have been folded.
2778 DAG.DeleteNode(MulResult.getNode());
2779
2780 SDValue Ops[2] = { BottomHalf, TopHalf } ;
2781 return DAG.getMergeValues(Ops, 2, dl);
2782}
2783
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002784static SDValue LowerATOMIC_LOAD_STORE(SDValue Op, SelectionDAG &DAG) {
2785 // Monotonic load/stores are legal.
2786 if (cast<AtomicSDNode>(Op)->getOrdering() <= Monotonic)
2787 return Op;
2788
2789 // Otherwise, expand with a fence.
2790 return SDValue();
2791}
2792
2793
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002794SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002795LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002796
2797 bool hasHardQuad = Subtarget->hasHardQuad();
2798 bool is64Bit = Subtarget->is64Bit();
2799 bool isV9 = Subtarget->isV9();
2800
Chris Lattner0a1762e2008-03-17 03:21:36 +00002801 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002802 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002803
Venkatraman Govindaraju96ab3bc2014-01-04 07:17:21 +00002804 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this,
2805 Subtarget);
2806 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG,
2807 Subtarget);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002808 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002809 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002810 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002811 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002812 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2813 hasHardQuad);
2814 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2815 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002816 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
2817 hasHardQuad);
2818 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
2819 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002820 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
2821 hasHardQuad);
2822 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
2823 hasHardQuad);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002824 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
2825 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002826 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002827 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002828
2829 case ISD::LOAD: return LowerF128Load(Op, DAG);
2830 case ISD::STORE: return LowerF128Store(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002831 case ISD::FADD: return LowerF128Op(Op, DAG,
2832 getLibcallName(RTLIB::ADD_F128), 2);
2833 case ISD::FSUB: return LowerF128Op(Op, DAG,
2834 getLibcallName(RTLIB::SUB_F128), 2);
2835 case ISD::FMUL: return LowerF128Op(Op, DAG,
2836 getLibcallName(RTLIB::MUL_F128), 2);
2837 case ISD::FDIV: return LowerF128Op(Op, DAG,
2838 getLibcallName(RTLIB::DIV_F128), 2);
2839 case ISD::FSQRT: return LowerF128Op(Op, DAG,
2840 getLibcallName(RTLIB::SQRT_F128),1);
2841 case ISD::FNEG: return LowerFNEG(Op, DAG, *this, is64Bit);
2842 case ISD::FABS: return LowerFABS(Op, DAG, isV9);
2843 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
2844 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002845 case ISD::ADDC:
2846 case ISD::ADDE:
2847 case ISD::SUBC:
2848 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Venkatraman Govindaraju77011e82014-01-01 20:22:45 +00002849 case ISD::UMULO:
2850 case ISD::SMULO: return LowerUMULO_SMULO(Op, DAG, *this);
Venkatraman Govindaraju9a3da522014-01-01 22:11:54 +00002851 case ISD::ATOMIC_LOAD:
2852 case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002853 }
2854}
2855
2856MachineBasicBlock *
2857SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00002858 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00002859 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002860 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00002861 case SP::SELECT_CC_Int_ICC:
2862 case SP::SELECT_CC_FP_ICC:
2863 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002864 case SP::SELECT_CC_QFP_ICC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00002865 return expandSelectCC(MI, BB, SP::BCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002866 case SP::SELECT_CC_Int_FCC:
2867 case SP::SELECT_CC_FP_FCC:
2868 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002869 case SP::SELECT_CC_QFP_FCC:
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00002870 return expandSelectCC(MI, BB, SP::FBCOND);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002871
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00002872 case SP::ATOMIC_LOAD_ADD_32:
2873 return expandAtomicRMW(MI, BB, SP::ADDrr);
2874 case SP::ATOMIC_LOAD_ADD_64:
2875 return expandAtomicRMW(MI, BB, SP::ADDXrr);
2876 case SP::ATOMIC_LOAD_SUB_32:
2877 return expandAtomicRMW(MI, BB, SP::SUBrr);
2878 case SP::ATOMIC_LOAD_SUB_64:
2879 return expandAtomicRMW(MI, BB, SP::SUBXrr);
2880 case SP::ATOMIC_LOAD_AND_32:
2881 return expandAtomicRMW(MI, BB, SP::ANDrr);
2882 case SP::ATOMIC_LOAD_AND_64:
2883 return expandAtomicRMW(MI, BB, SP::ANDXrr);
2884 case SP::ATOMIC_LOAD_OR_32:
2885 return expandAtomicRMW(MI, BB, SP::ORrr);
2886 case SP::ATOMIC_LOAD_OR_64:
2887 return expandAtomicRMW(MI, BB, SP::ORXrr);
2888 case SP::ATOMIC_LOAD_XOR_32:
2889 return expandAtomicRMW(MI, BB, SP::XORrr);
2890 case SP::ATOMIC_LOAD_XOR_64:
2891 return expandAtomicRMW(MI, BB, SP::XORXrr);
2892 case SP::ATOMIC_LOAD_NAND_32:
2893 return expandAtomicRMW(MI, BB, SP::ANDrr);
2894 case SP::ATOMIC_LOAD_NAND_64:
2895 return expandAtomicRMW(MI, BB, SP::ANDXrr);
2896
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00002897 case SP::ATOMIC_SWAP_64:
2898 return expandAtomicRMW(MI, BB, 0);
2899
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00002900 case SP::ATOMIC_LOAD_MAX_32:
2901 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_G);
2902 case SP::ATOMIC_LOAD_MAX_64:
2903 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_G);
2904 case SP::ATOMIC_LOAD_MIN_32:
2905 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_LE);
2906 case SP::ATOMIC_LOAD_MIN_64:
2907 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_LE);
2908 case SP::ATOMIC_LOAD_UMAX_32:
2909 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_GU);
2910 case SP::ATOMIC_LOAD_UMAX_64:
2911 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_GU);
2912 case SP::ATOMIC_LOAD_UMIN_32:
2913 return expandAtomicRMW(MI, BB, SP::MOVICCrr, SPCC::ICC_LEU);
2914 case SP::ATOMIC_LOAD_UMIN_64:
2915 return expandAtomicRMW(MI, BB, SP::MOVXCCrr, SPCC::ICC_LEU);
2916 }
2917}
2918
2919MachineBasicBlock*
2920SparcTargetLowering::expandSelectCC(MachineInstr *MI,
2921 MachineBasicBlock *BB,
2922 unsigned BROpcode) const {
2923 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2924 DebugLoc dl = MI->getDebugLoc();
2925 unsigned CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002926
Chris Lattner0a1762e2008-03-17 03:21:36 +00002927 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2928 // control-flow pattern. The incoming instruction knows the destination vreg
2929 // to set, the condition code register to branch on, the true/false values to
2930 // select between, and a branch opcode to use.
2931 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00002932 MachineFunction::iterator It = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002933 ++It;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002934
Chris Lattner0a1762e2008-03-17 03:21:36 +00002935 // thisMBB:
2936 // ...
2937 // TrueVal = ...
2938 // [f]bCC copy1MBB
2939 // fallthrough --> copy0MBB
2940 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002941 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00002942 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2943 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00002944 F->insert(It, copy0MBB);
2945 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00002946
2947 // Transfer the remainder of BB and its successor edges to sinkMBB.
2948 sinkMBB->splice(sinkMBB->begin(), BB,
2949 llvm::next(MachineBasicBlock::iterator(MI)),
2950 BB->end());
2951 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2952
2953 // Add the true and fallthrough blocks as its successors.
2954 BB->addSuccessor(copy0MBB);
2955 BB->addSuccessor(sinkMBB);
2956
Dale Johannesen215a9252009-02-13 02:31:35 +00002957 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002958
Chris Lattner0a1762e2008-03-17 03:21:36 +00002959 // copy0MBB:
2960 // %FalseValue = ...
2961 // # fallthrough to sinkMBB
2962 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002963
Chris Lattner0a1762e2008-03-17 03:21:36 +00002964 // Update machine-CFG edges
2965 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002966
Chris Lattner0a1762e2008-03-17 03:21:36 +00002967 // sinkMBB:
2968 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2969 // ...
2970 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00002971 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00002972 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2973 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002974
Dan Gohman34396292010-07-06 20:24:04 +00002975 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00002976 return BB;
2977}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002978
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00002979MachineBasicBlock*
2980SparcTargetLowering::expandAtomicRMW(MachineInstr *MI,
2981 MachineBasicBlock *MBB,
2982 unsigned Opcode,
2983 unsigned CondCode) const {
2984 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2985 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
2986 DebugLoc DL = MI->getDebugLoc();
2987
2988 // MI is an atomic read-modify-write instruction of the form:
2989 //
2990 // rd = atomicrmw<op> addr, rs2
2991 //
2992 // All three operands are registers.
2993 unsigned DestReg = MI->getOperand(0).getReg();
2994 unsigned AddrReg = MI->getOperand(1).getReg();
2995 unsigned Rs2Reg = MI->getOperand(2).getReg();
2996
2997 // SelectionDAG has already inserted memory barriers before and after MI, so
2998 // we simply have to implement the operatiuon in terms of compare-and-swap.
2999 //
3000 // %val0 = load %addr
3001 // loop:
3002 // %val = phi %val0, %dest
3003 // %upd = op %val, %rs2
Jakob Stoklund Olesen39f08332014-01-26 06:09:54 +00003004 // %dest = cas %addr, %val, %upd
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003005 // cmp %val, %dest
3006 // bne loop
3007 // done:
3008 //
3009 bool is64Bit = SP::I64RegsRegClass.hasSubClassEq(MRI.getRegClass(DestReg));
3010 const TargetRegisterClass *ValueRC =
3011 is64Bit ? &SP::I64RegsRegClass : &SP::IntRegsRegClass;
3012 unsigned Val0Reg = MRI.createVirtualRegister(ValueRC);
3013
3014 BuildMI(*MBB, MI, DL, TII.get(is64Bit ? SP::LDXri : SP::LDri), Val0Reg)
3015 .addReg(AddrReg).addImm(0);
3016
3017 // Split the basic block MBB before MI and insert the loop block in the hole.
3018 MachineFunction::iterator MFI = MBB;
3019 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
3020 MachineFunction *MF = MBB->getParent();
3021 MachineBasicBlock *LoopMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3022 MachineBasicBlock *DoneMBB = MF->CreateMachineBasicBlock(LLVM_BB);
3023 ++MFI;
3024 MF->insert(MFI, LoopMBB);
3025 MF->insert(MFI, DoneMBB);
3026
3027 // Move MI and following instructions to DoneMBB.
3028 DoneMBB->splice(DoneMBB->begin(), MBB, MI, MBB->end());
3029 DoneMBB->transferSuccessorsAndUpdatePHIs(MBB);
3030
3031 // Connect the CFG again.
3032 MBB->addSuccessor(LoopMBB);
3033 LoopMBB->addSuccessor(LoopMBB);
3034 LoopMBB->addSuccessor(DoneMBB);
3035
3036 // Build the loop block.
3037 unsigned ValReg = MRI.createVirtualRegister(ValueRC);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00003038 // Opcode == 0 means try to write Rs2Reg directly (ATOMIC_SWAP).
3039 unsigned UpdReg = (Opcode ? MRI.createVirtualRegister(ValueRC) : Rs2Reg);
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003040
3041 BuildMI(LoopMBB, DL, TII.get(SP::PHI), ValReg)
3042 .addReg(Val0Reg).addMBB(MBB)
3043 .addReg(DestReg).addMBB(LoopMBB);
3044
3045 if (CondCode) {
3046 // This is one of the min/max operations. We need a CMPrr followed by a
3047 // MOVXCC/MOVICC.
3048 BuildMI(LoopMBB, DL, TII.get(SP::CMPrr)).addReg(ValReg).addReg(Rs2Reg);
3049 BuildMI(LoopMBB, DL, TII.get(Opcode), UpdReg)
3050 .addReg(ValReg).addReg(Rs2Reg).addImm(CondCode);
Jakob Stoklund Olesenef1d59a2014-01-30 04:48:46 +00003051 } else if (Opcode) {
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003052 BuildMI(LoopMBB, DL, TII.get(Opcode), UpdReg)
3053 .addReg(ValReg).addReg(Rs2Reg);
3054 }
3055
3056 if (MI->getOpcode() == SP::ATOMIC_LOAD_NAND_32 ||
3057 MI->getOpcode() == SP::ATOMIC_LOAD_NAND_64) {
3058 unsigned TmpReg = UpdReg;
3059 UpdReg = MRI.createVirtualRegister(ValueRC);
3060 BuildMI(LoopMBB, DL, TII.get(SP::XORri), UpdReg).addReg(TmpReg).addImm(-1);
3061 }
3062
3063 BuildMI(LoopMBB, DL, TII.get(is64Bit ? SP::CASXrr : SP::CASrr), DestReg)
Jakob Stoklund Olesen39f08332014-01-26 06:09:54 +00003064 .addReg(AddrReg).addReg(ValReg).addReg(UpdReg)
Jakob Stoklund Olesen05ae2d62014-01-24 06:23:31 +00003065 .setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
3066 BuildMI(LoopMBB, DL, TII.get(SP::CMPrr)).addReg(ValReg).addReg(DestReg);
3067 BuildMI(LoopMBB, DL, TII.get(is64Bit ? SP::BPXCC : SP::BCOND))
3068 .addMBB(LoopMBB).addImm(SPCC::ICC_NE);
3069
3070 MI->eraseFromParent();
3071 return DoneMBB;
3072}
3073
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003074//===----------------------------------------------------------------------===//
3075// Sparc Inline Assembly Support
3076//===----------------------------------------------------------------------===//
3077
3078/// getConstraintType - Given a constraint letter, return the type of
3079/// constraint it is for this target.
3080SparcTargetLowering::ConstraintType
3081SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
3082 if (Constraint.size() == 1) {
3083 switch (Constraint[0]) {
3084 default: break;
3085 case 'r': return C_RegisterClass;
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003086 case 'I': // SIMM13
3087 return C_Other;
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003088 }
3089 }
3090
3091 return TargetLowering::getConstraintType(Constraint);
3092}
3093
Venkatraman Govindaraju407e4422014-01-22 01:29:51 +00003094TargetLowering::ConstraintWeight SparcTargetLowering::
3095getSingleConstraintMatchWeight(AsmOperandInfo &info,
3096 const char *constraint) const {
3097 ConstraintWeight weight = CW_Invalid;
3098 Value *CallOperandVal = info.CallOperandVal;
3099 // If we don't have a value, we can't do a match,
3100 // but allow it at the lowest weight.
3101 if (CallOperandVal == NULL)
3102 return CW_Default;
3103
3104 // Look at the constraint type.
3105 switch (*constraint) {
3106 default:
3107 weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
3108 break;
3109 case 'I': // SIMM13
3110 if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
3111 if (isInt<13>(C->getSExtValue()))
3112 weight = CW_Constant;
3113 }
3114 break;
3115 }
3116 return weight;
3117}
3118
3119/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
3120/// vector. If it is invalid, don't add anything to Ops.
3121void SparcTargetLowering::
3122LowerAsmOperandForConstraint(SDValue Op,
3123 std::string &Constraint,
3124 std::vector<SDValue> &Ops,
3125 SelectionDAG &DAG) const {
3126 SDValue Result(0, 0);
3127
3128 // Only support length 1 constraints for now.
3129 if (Constraint.length() > 1)
3130 return;
3131
3132 char ConstraintLetter = Constraint[0];
3133 switch (ConstraintLetter) {
3134 default: break;
3135 case 'I':
3136 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
3137 if (isInt<13>(C->getSExtValue())) {
3138 Result = DAG.getTargetConstant(C->getSExtValue(), Op.getValueType());
3139 break;
3140 }
3141 return;
3142 }
3143 }
3144
3145 if (Result.getNode()) {
3146 Ops.push_back(Result);
3147 return;
3148 }
3149 TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
3150}
3151
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003152std::pair<unsigned, const TargetRegisterClass*>
3153SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00003154 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003155 if (Constraint.size() == 1) {
3156 switch (Constraint[0]) {
3157 case 'r':
Craig Topperabadc662012-04-20 06:31:50 +00003158 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003159 }
Venkatraman Govindarajudd634ca2014-01-22 03:18:42 +00003160 } else if (!Constraint.empty() && Constraint.size() <= 5
3161 && Constraint[0] == '{' && *(Constraint.end()-1) == '}') {
3162 // constraint = '{r<d>}'
3163 // Remove the braces from around the name.
3164 StringRef name(Constraint.data()+1, Constraint.size()-2);
3165 // Handle register aliases:
3166 // r0-r7 -> g0-g7
3167 // r8-r15 -> o0-o7
3168 // r16-r23 -> l0-l7
3169 // r24-r31 -> i0-i7
3170 uint64_t intVal = 0;
3171 if (name.substr(0, 1).equals("r")
3172 && !name.substr(1).getAsInteger(10, intVal) && intVal <= 31) {
3173 const char regTypes[] = { 'g', 'o', 'l', 'i' };
3174 char regType = regTypes[intVal/8];
3175 char regIdx = '0' + (intVal % 8);
3176 char tmp[] = { '{', regType, regIdx, '}', 0 };
3177 std::string newConstraint = std::string(tmp);
3178 return TargetLowering::getRegForInlineAsmConstraint(newConstraint, VT);
3179 }
Anton Korobeynikov281cf242008-10-10 20:28:10 +00003180 }
3181
3182 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
3183}
3184
Dan Gohman2fe6bee2008-10-18 02:06:02 +00003185bool
3186SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
3187 // The Sparc target isn't yet aware of offsets.
3188 return false;
3189}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00003190
3191void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
3192 SmallVectorImpl<SDValue>& Results,
3193 SelectionDAG &DAG) const {
3194
3195 SDLoc dl(N);
3196
3197 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
3198
3199 switch (N->getOpcode()) {
3200 default:
3201 llvm_unreachable("Do not know how to custom type legalize this operation!");
3202
3203 case ISD::FP_TO_SINT:
3204 case ISD::FP_TO_UINT:
3205 // Custom lower only if it involves f128 or i64.
3206 if (N->getOperand(0).getValueType() != MVT::f128
3207 || N->getValueType(0) != MVT::i64)
3208 return;
3209 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
3210 ? RTLIB::FPTOSINT_F128_I64
3211 : RTLIB::FPTOUINT_F128_I64);
3212
3213 Results.push_back(LowerF128Op(SDValue(N, 0),
3214 DAG,
3215 getLibcallName(libCall),
3216 1));
3217 return;
3218
3219 case ISD::SINT_TO_FP:
3220 case ISD::UINT_TO_FP:
3221 // Custom lower only if it involves f128 or i64.
3222 if (N->getValueType(0) != MVT::f128
3223 || N->getOperand(0).getValueType() != MVT::i64)
3224 return;
3225
3226 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
3227 ? RTLIB::SINTTOFP_I64_F128
3228 : RTLIB::UINTTOFP_I64_F128);
3229
3230 Results.push_back(LowerF128Op(SDValue(N, 0),
3231 DAG,
3232 getLibcallName(libCall),
3233 1));
3234 return;
3235 }
3236}