blob: b794f8702bef1dc05f9b1817dc7f2a1732661936 [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"
Dan Gohman31ae5862010-04-17 14:41:14 +000016#include "SparcMachineFunctionInfo.h"
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +000017#include "SparcRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "SparcTargetMachine.h"
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +000019#include "MCTargetDesc/SparcBaseInfo.h"
Chris Lattner49b269d2008-03-17 05:41:48 +000020#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikovab663a02010-02-15 22:37:53 +000026#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/Module.h"
Torok Edwin56d06592009-07-11 20:10:48 +000030#include "llvm/Support/ErrorHandling.h"
Chris Lattner0a1762e2008-03-17 03:21:36 +000031using namespace llvm;
32
Chris Lattner49b269d2008-03-17 05:41:48 +000033
34//===----------------------------------------------------------------------===//
35// Calling Convention Implementation
36//===----------------------------------------------------------------------===//
37
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000038static bool CC_Sparc_Assign_SRet(unsigned &ValNo, MVT &ValVT,
39 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
40 ISD::ArgFlagsTy &ArgFlags, CCState &State)
41{
42 assert (ArgFlags.isSRet());
43
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000044 // Assign SRet argument.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +000045 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
46 0,
47 LocVT, LocInfo));
48 return true;
49}
50
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000051static bool CC_Sparc_Assign_f64(unsigned &ValNo, MVT &ValVT,
52 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
53 ISD::ArgFlagsTy &ArgFlags, CCState &State)
54{
Craig Topperbef78fc2012-03-11 07:57:25 +000055 static const uint16_t RegList[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000056 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
57 };
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000058 // Try to get first reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000059 if (unsigned Reg = State.AllocateReg(RegList, 6)) {
60 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
61 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000062 // Assign whole thing in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000063 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
64 State.AllocateStack(8,4),
65 LocVT, LocInfo));
66 return true;
67 }
68
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +000069 // Try to get second reg.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +000070 if (unsigned Reg = State.AllocateReg(RegList, 6))
71 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg, LocVT, LocInfo));
72 else
73 State.addLoc(CCValAssign::getCustomMem(ValNo, ValVT,
74 State.AllocateStack(4,4),
75 LocVT, LocInfo));
76 return true;
77}
78
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000079// Allocate a full-sized argument for the 64-bit ABI.
80static bool CC_Sparc64_Full(unsigned &ValNo, MVT &ValVT,
81 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
82 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +000083 assert((LocVT == MVT::f32 || LocVT == MVT::f128
84 || LocVT.getSizeInBits() == 64) &&
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000085 "Can't handle non-64 bits locations");
86
87 // Stack space is allocated for all arguments starting from [%fp+BIAS+128].
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +000088 unsigned size = (LocVT == MVT::f128) ? 16 : 8;
89 unsigned alignment = (LocVT == MVT::f128) ? 16 : 8;
90 unsigned Offset = State.AllocateStack(size, alignment);
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +000091 unsigned Reg = 0;
92
93 if (LocVT == MVT::i64 && Offset < 6*8)
94 // Promote integers to %i0-%i5.
95 Reg = SP::I0 + Offset/8;
96 else if (LocVT == MVT::f64 && Offset < 16*8)
97 // Promote doubles to %d0-%d30. (Which LLVM calls D0-D15).
98 Reg = SP::D0 + Offset/8;
99 else if (LocVT == MVT::f32 && Offset < 16*8)
100 // Promote floats to %f1, %f3, ...
101 Reg = SP::F1 + Offset/4;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +0000102 else if (LocVT == MVT::f128 && Offset < 16*8)
103 // Promote long doubles to %q0-%q28. (Which LLVM calls Q0-Q7).
104 Reg = SP::Q0 + Offset/16;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000105
106 // Promote to register when possible, otherwise use the stack slot.
107 if (Reg) {
108 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
109 return true;
110 }
111
112 // This argument goes on the stack in an 8-byte slot.
113 // When passing floats, LocVT is smaller than 8 bytes. Adjust the offset to
114 // the right-aligned float. The first 4 bytes of the stack slot are undefined.
115 if (LocVT == MVT::f32)
116 Offset += 4;
117
118 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
119 return true;
120}
121
122// Allocate a half-sized argument for the 64-bit ABI.
123//
124// This is used when passing { float, int } structs by value in registers.
125static bool CC_Sparc64_Half(unsigned &ValNo, MVT &ValVT,
126 MVT &LocVT, CCValAssign::LocInfo &LocInfo,
127 ISD::ArgFlagsTy &ArgFlags, CCState &State) {
128 assert(LocVT.getSizeInBits() == 32 && "Can't handle non-32 bits locations");
129 unsigned Offset = State.AllocateStack(4, 4);
130
131 if (LocVT == MVT::f32 && Offset < 16*8) {
132 // Promote floats to %f0-%f31.
133 State.addLoc(CCValAssign::getReg(ValNo, ValVT, SP::F0 + Offset/4,
134 LocVT, LocInfo));
135 return true;
136 }
137
138 if (LocVT == MVT::i32 && Offset < 6*8) {
139 // Promote integers to %i0-%i5, using half the register.
140 unsigned Reg = SP::I0 + Offset/8;
141 LocVT = MVT::i64;
142 LocInfo = CCValAssign::AExt;
143
144 // Set the Custom bit if this i32 goes in the high bits of a register.
145 if (Offset % 8 == 0)
146 State.addLoc(CCValAssign::getCustomReg(ValNo, ValVT, Reg,
147 LocVT, LocInfo));
148 else
149 State.addLoc(CCValAssign::getReg(ValNo, ValVT, Reg, LocVT, LocInfo));
150 return true;
151 }
152
153 State.addLoc(CCValAssign::getMem(ValNo, ValVT, Offset, LocVT, LocInfo));
154 return true;
155}
156
Chris Lattner49b269d2008-03-17 05:41:48 +0000157#include "SparcGenCallingConv.inc"
158
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000159// The calling conventions in SparcCallingConv.td are described in terms of the
160// callee's register window. This function translates registers to the
161// corresponding caller window %o register.
162static unsigned toCallerWindow(unsigned Reg) {
163 assert(SP::I0 + 7 == SP::I7 && SP::O0 + 7 == SP::O7 && "Unexpected enum");
164 if (Reg >= SP::I0 && Reg <= SP::I7)
165 return Reg - SP::I0 + SP::O0;
166 return Reg;
167}
168
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000169SDValue
170SparcTargetLowering::LowerReturn(SDValue Chain,
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000171 CallingConv::ID CallConv, bool IsVarArg,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000172 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000173 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000174 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000175 if (Subtarget->is64Bit())
176 return LowerReturn_64(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
177 return LowerReturn_32(Chain, CallConv, IsVarArg, Outs, OutVals, DL, DAG);
178}
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000179
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000180SDValue
181SparcTargetLowering::LowerReturn_32(SDValue Chain,
182 CallingConv::ID CallConv, bool IsVarArg,
183 const SmallVectorImpl<ISD::OutputArg> &Outs,
184 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000185 SDLoc DL, SelectionDAG &DAG) const {
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000186 MachineFunction &MF = DAG.getMachineFunction();
187
Chris Lattner49b269d2008-03-17 05:41:48 +0000188 // CCValAssign - represent the assignment of the return value to locations.
189 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000190
Chris Lattner49b269d2008-03-17 05:41:48 +0000191 // CCState - Info about the registers and stack slot.
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000192 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000193 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000194
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000195 // Analyze return values.
196 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000197
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000198 SDValue Flag;
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000199 SmallVector<SDValue, 4> RetOps(1, Chain);
200 // Make room for the return address offset.
201 RetOps.push_back(SDValue());
Chris Lattner49b269d2008-03-17 05:41:48 +0000202
203 // Copy the result values into the output registers.
204 for (unsigned i = 0; i != RVLocs.size(); ++i) {
205 CCValAssign &VA = RVLocs[i];
206 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000207
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000208 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(),
Dan Gohmanfe7532a2010-07-07 15:54:55 +0000209 OutVals[i], Flag);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000210
Chris Lattner49b269d2008-03-17 05:41:48 +0000211 // Guarantee that all emitted copies are stuck together with flags.
212 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000213 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
Chris Lattner49b269d2008-03-17 05:41:48 +0000214 }
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000215
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000216 unsigned RetAddrOffset = 8; // Call Inst + Delay Slot
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000217 // If the function returns a struct, copy the SRetReturnReg to I0
218 if (MF.getFunction()->hasStructRetAttr()) {
219 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
220 unsigned Reg = SFI->getSRetReturnReg();
221 if (!Reg)
222 llvm_unreachable("sret virtual register not created in the entry block");
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000223 SDValue Val = DAG.getCopyFromReg(Chain, DL, Reg, getPointerTy());
224 Chain = DAG.getCopyToReg(Chain, DL, SP::I0, Val, Flag);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000225 Flag = Chain.getValue(1);
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000226 RetOps.push_back(DAG.getRegister(SP::I0, getPointerTy()));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000227 RetAddrOffset = 12; // CallInst + Delay Slot + Unimp
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000228 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000229
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000230 RetOps[0] = Chain; // Update chain.
231 RetOps[1] = DAG.getConstant(RetAddrOffset, MVT::i32);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000232
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000233 // Add the flag if we have it.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000234 if (Flag.getNode())
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000235 RetOps.push_back(Flag);
236
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000237 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
238 &RetOps[0], RetOps.size());
239}
240
241// Lower return values for the 64-bit ABI.
242// Return values are passed the exactly the same way as function arguments.
243SDValue
244SparcTargetLowering::LowerReturn_64(SDValue Chain,
245 CallingConv::ID CallConv, bool IsVarArg,
246 const SmallVectorImpl<ISD::OutputArg> &Outs,
247 const SmallVectorImpl<SDValue> &OutVals,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000248 SDLoc DL, SelectionDAG &DAG) const {
Jakob Stoklund Olesenedaf66b2013-04-06 23:57:33 +0000249 // CCValAssign - represent the assignment of the return value to locations.
250 SmallVector<CCValAssign, 16> RVLocs;
251
252 // CCState - Info about the registers and stack slot.
253 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
254 DAG.getTarget(), RVLocs, *DAG.getContext());
255
256 // Analyze return values.
257 CCInfo.AnalyzeReturn(Outs, CC_Sparc64);
258
259 SDValue Flag;
260 SmallVector<SDValue, 4> RetOps(1, Chain);
261
262 // The second operand on the return instruction is the return address offset.
263 // The return address is always %i7+8 with the 64-bit ABI.
264 RetOps.push_back(DAG.getConstant(8, MVT::i32));
265
266 // Copy the result values into the output registers.
267 for (unsigned i = 0; i != RVLocs.size(); ++i) {
268 CCValAssign &VA = RVLocs[i];
269 assert(VA.isRegLoc() && "Can only return in registers!");
270 SDValue OutVal = OutVals[i];
271
272 // Integer return values must be sign or zero extended by the callee.
273 switch (VA.getLocInfo()) {
274 case CCValAssign::SExt:
275 OutVal = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), OutVal);
276 break;
277 case CCValAssign::ZExt:
278 OutVal = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), OutVal);
279 break;
280 case CCValAssign::AExt:
281 OutVal = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), OutVal);
282 default:
283 break;
284 }
285
286 // The custom bit on an i32 return value indicates that it should be passed
287 // in the high bits of the register.
288 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
289 OutVal = DAG.getNode(ISD::SHL, DL, MVT::i64, OutVal,
290 DAG.getConstant(32, MVT::i32));
291
292 // The next value may go in the low bits of the same register.
293 // Handle both at once.
294 if (i+1 < RVLocs.size() && RVLocs[i+1].getLocReg() == VA.getLocReg()) {
295 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, OutVals[i+1]);
296 OutVal = DAG.getNode(ISD::OR, DL, MVT::i64, OutVal, NV);
297 // Skip the next value, it's already done.
298 ++i;
299 }
300 }
301
302 Chain = DAG.getCopyToReg(Chain, DL, VA.getLocReg(), OutVal, Flag);
303
304 // Guarantee that all emitted copies are stuck together with flags.
305 Flag = Chain.getValue(1);
306 RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
307 }
308
309 RetOps[0] = Chain; // Update chain.
310
311 // Add the flag if we have it.
312 if (Flag.getNode())
313 RetOps.push_back(Flag);
314
315 return DAG.getNode(SPISD::RET_FLAG, DL, MVT::Other,
Jakob Stoklund Olesenef8bf3c2013-02-05 18:16:58 +0000316 &RetOps[0], RetOps.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000317}
318
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000319SDValue SparcTargetLowering::
320LowerFormalArguments(SDValue Chain,
321 CallingConv::ID CallConv,
322 bool IsVarArg,
323 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000324 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000325 SelectionDAG &DAG,
326 SmallVectorImpl<SDValue> &InVals) const {
327 if (Subtarget->is64Bit())
328 return LowerFormalArguments_64(Chain, CallConv, IsVarArg, Ins,
329 DL, DAG, InVals);
330 return LowerFormalArguments_32(Chain, CallConv, IsVarArg, Ins,
331 DL, DAG, InVals);
332}
333
334/// LowerFormalArguments32 - V8 uses a very simple ABI, where all values are
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000335/// passed in either one or two GPRs, including FP values. TODO: we should
336/// pass FP values in FP registers for fastcc functions.
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000337SDValue SparcTargetLowering::
338LowerFormalArguments_32(SDValue Chain,
339 CallingConv::ID CallConv,
340 bool isVarArg,
341 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000342 SDLoc dl,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000343 SelectionDAG &DAG,
344 SmallVectorImpl<SDValue> &InVals) const {
Chris Lattner49b269d2008-03-17 05:41:48 +0000345 MachineFunction &MF = DAG.getMachineFunction();
346 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman31ae5862010-04-17 14:41:14 +0000347 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmanbe853b72009-07-19 19:53:46 +0000348
349 // Assign locations to all of the incoming arguments.
350 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000351 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000352 getTargetMachine(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000353 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000354
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000355 const unsigned StackOffset = 92;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000356
Eli Friedmanbe853b72009-07-19 19:53:46 +0000357 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
Eli Friedmanbe853b72009-07-19 19:53:46 +0000358 CCValAssign &VA = ArgLocs[i];
Chris Lattner49b269d2008-03-17 05:41:48 +0000359
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000360 if (i == 0 && Ins[i].Flags.isSRet()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000361 // Get SRet from [%fp+64].
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000362 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, 64, true);
363 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
364 SDValue Arg = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
365 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000366 false, false, false, 0);
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000367 InVals.push_back(Arg);
368 continue;
369 }
370
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000371 if (VA.isRegLoc()) {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000372 if (VA.needsCustom()) {
373 assert(VA.getLocVT() == MVT::f64);
374 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
375 MF.getRegInfo().addLiveIn(VA.getLocReg(), VRegHi);
376 SDValue HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000377
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000378 assert(i+1 < e);
379 CCValAssign &NextVA = ArgLocs[++i];
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000380
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000381 SDValue LoVal;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000382 if (NextVA.isMemLoc()) {
383 int FrameIdx = MF.getFrameInfo()->
384 CreateFixedObject(4, StackOffset+NextVA.getLocMemOffset(),true);
Owen Anderson9f944592009-08-11 20:47:22 +0000385 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000386 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
387 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000388 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000389 } else {
390 unsigned loReg = MF.addLiveIn(NextVA.getLocReg(),
Devang Patelf3292b22011-02-21 23:21:26 +0000391 &SP::IntRegsRegClass);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000392 LoVal = DAG.getCopyFromReg(Chain, dl, loReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000393 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000394 SDValue WholeValue =
Owen Anderson9f944592009-08-11 20:47:22 +0000395 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000396 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000397 InVals.push_back(WholeValue);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000398 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000399 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000400 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
401 MF.getRegInfo().addLiveIn(VA.getLocReg(), VReg);
402 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
403 if (VA.getLocVT() == MVT::f32)
404 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
405 else if (VA.getLocVT() != MVT::i32) {
406 Arg = DAG.getNode(ISD::AssertSext, dl, MVT::i32, Arg,
407 DAG.getValueType(VA.getLocVT()));
408 Arg = DAG.getNode(ISD::TRUNCATE, dl, VA.getLocVT(), Arg);
409 }
410 InVals.push_back(Arg);
411 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000412 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000413
414 assert(VA.isMemLoc());
415
416 unsigned Offset = VA.getLocMemOffset()+StackOffset;
417
418 if (VA.needsCustom()) {
419 assert(VA.getValVT() == MVT::f64);
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000420 // If it is double-word aligned, just load.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000421 if (Offset % 8 == 0) {
422 int FI = MF.getFrameInfo()->CreateFixedObject(8,
423 Offset,
424 true);
425 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
426 SDValue Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
427 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000428 false,false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000429 InVals.push_back(Load);
430 continue;
431 }
432
433 int FI = MF.getFrameInfo()->CreateFixedObject(4,
434 Offset,
435 true);
436 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
437 SDValue HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr,
438 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000439 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000440 int FI2 = MF.getFrameInfo()->CreateFixedObject(4,
441 Offset+4,
442 true);
443 SDValue FIPtr2 = DAG.getFrameIndex(FI2, getPointerTy());
444
445 SDValue LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr2,
446 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000447 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000448
449 SDValue WholeValue =
450 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
451 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
452 InVals.push_back(WholeValue);
453 continue;
454 }
455
456 int FI = MF.getFrameInfo()->CreateFixedObject(4,
457 Offset,
458 true);
459 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
460 SDValue Load ;
461 if (VA.getValVT() == MVT::i32 || VA.getValVT() == MVT::f32) {
462 Load = DAG.getLoad(VA.getValVT(), dl, Chain, FIPtr,
463 MachinePointerInfo(),
Pete Cooper82cd9e82011-11-08 18:42:53 +0000464 false, false, false, 0);
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000465 } else {
466 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
467 // Sparc is big endian, so add an offset based on the ObjectVT.
468 unsigned Offset = 4-std::max(1U, VA.getValVT().getSizeInBits()/8);
469 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
470 DAG.getConstant(Offset, MVT::i32));
Stuart Hastings81c43062011-02-16 16:23:55 +0000471 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000472 MachinePointerInfo(),
473 VA.getValVT(), false, false,0);
474 Load = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Load);
475 }
476 InVals.push_back(Load);
Chris Lattner49b269d2008-03-17 05:41:48 +0000477 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000478
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000479 if (MF.getFunction()->hasStructRetAttr()) {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000480 // Copy the SRet Argument to SRetReturnReg.
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000481 SparcMachineFunctionInfo *SFI = MF.getInfo<SparcMachineFunctionInfo>();
482 unsigned Reg = SFI->getSRetReturnReg();
483 if (!Reg) {
484 Reg = MF.getRegInfo().createVirtualRegister(&SP::IntRegsRegClass);
485 SFI->setSRetReturnReg(Reg);
486 }
487 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
488 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
489 }
490
Chris Lattner49b269d2008-03-17 05:41:48 +0000491 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmanbe853b72009-07-19 19:53:46 +0000492 if (isVarArg) {
Craig Topperbef78fc2012-03-11 07:57:25 +0000493 static const uint16_t ArgRegs[] = {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000494 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
495 };
496 unsigned NumAllocated = CCInfo.getFirstUnallocated(ArgRegs, 6);
Craig Topperbef78fc2012-03-11 07:57:25 +0000497 const uint16_t *CurArgReg = ArgRegs+NumAllocated, *ArgRegEnd = ArgRegs+6;
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000498 unsigned ArgOffset = CCInfo.getNextStackOffset();
499 if (NumAllocated == 6)
500 ArgOffset += StackOffset;
501 else {
502 assert(!ArgOffset);
503 ArgOffset = 68+4*NumAllocated;
504 }
505
Chris Lattner49b269d2008-03-17 05:41:48 +0000506 // Remember the vararg offset for the va_start implementation.
Dan Gohman31ae5862010-04-17 14:41:14 +0000507 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000508
Eli Friedmanbe853b72009-07-19 19:53:46 +0000509 std::vector<SDValue> OutChains;
510
Chris Lattner49b269d2008-03-17 05:41:48 +0000511 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
512 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
513 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson9f944592009-08-11 20:47:22 +0000514 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000515
David Greene1fbe0542009-11-12 20:49:22 +0000516 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Cheng0664a672010-07-03 00:40:23 +0000517 true);
Owen Anderson9f944592009-08-11 20:47:22 +0000518 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000519
Chris Lattner676c61d2010-09-21 18:41:36 +0000520 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
521 MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000522 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000523 ArgOffset += 4;
524 }
Eli Friedmanbe853b72009-07-19 19:53:46 +0000525
526 if (!OutChains.empty()) {
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000527 OutChains.push_back(Chain);
Owen Anderson9f944592009-08-11 20:47:22 +0000528 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000529 &OutChains[0], OutChains.size());
Eli Friedmanbe853b72009-07-19 19:53:46 +0000530 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000531 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000532
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000533 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000534}
535
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000536// Lower formal arguments for the 64 bit ABI.
537SDValue SparcTargetLowering::
538LowerFormalArguments_64(SDValue Chain,
539 CallingConv::ID CallConv,
540 bool IsVarArg,
541 const SmallVectorImpl<ISD::InputArg> &Ins,
Andrew Trickef9de2a2013-05-25 02:42:55 +0000542 SDLoc DL,
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000543 SelectionDAG &DAG,
544 SmallVectorImpl<SDValue> &InVals) const {
545 MachineFunction &MF = DAG.getMachineFunction();
546
547 // Analyze arguments according to CC_Sparc64.
548 SmallVector<CCValAssign, 16> ArgLocs;
549 CCState CCInfo(CallConv, IsVarArg, DAG.getMachineFunction(),
550 getTargetMachine(), ArgLocs, *DAG.getContext());
551 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc64);
552
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000553 // The argument array begins at %fp+BIAS+128, after the register save area.
554 const unsigned ArgArea = 128;
555
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000556 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
557 CCValAssign &VA = ArgLocs[i];
558 if (VA.isRegLoc()) {
559 // This argument is passed in a register.
560 // All integer register arguments are promoted by the caller to i64.
561
562 // Create a virtual register for the promoted live-in value.
563 unsigned VReg = MF.addLiveIn(VA.getLocReg(),
564 getRegClassFor(VA.getLocVT()));
565 SDValue Arg = DAG.getCopyFromReg(Chain, DL, VReg, VA.getLocVT());
566
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000567 // Get the high bits for i32 struct elements.
568 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
569 Arg = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), Arg,
570 DAG.getConstant(32, MVT::i32));
571
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000572 // The caller promoted the argument, so insert an Assert?ext SDNode so we
573 // won't promote the value again in this function.
574 switch (VA.getLocInfo()) {
575 case CCValAssign::SExt:
576 Arg = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Arg,
577 DAG.getValueType(VA.getValVT()));
578 break;
579 case CCValAssign::ZExt:
580 Arg = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Arg,
581 DAG.getValueType(VA.getValVT()));
582 break;
583 default:
584 break;
585 }
586
587 // Truncate the register down to the argument type.
588 if (VA.isExtInLoc())
589 Arg = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Arg);
590
591 InVals.push_back(Arg);
592 continue;
593 }
594
595 // The registers are exhausted. This argument was passed on the stack.
596 assert(VA.isMemLoc());
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000597 // The CC_Sparc64_Full/Half functions compute stack offsets relative to the
598 // beginning of the arguments area at %fp+BIAS+128.
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000599 unsigned Offset = VA.getLocMemOffset() + ArgArea;
Jakob Stoklund Olesen1c9a95a2013-04-06 18:32:12 +0000600 unsigned ValSize = VA.getValVT().getSizeInBits() / 8;
601 // Adjust offset for extended arguments, SPARC is big-endian.
602 // The caller will have written the full slot with extended bytes, but we
603 // prefer our own extending loads.
604 if (VA.isExtInLoc())
605 Offset += 8 - ValSize;
606 int FI = MF.getFrameInfo()->CreateFixedObject(ValSize, Offset, true);
607 InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
608 DAG.getFrameIndex(FI, getPointerTy()),
609 MachinePointerInfo::getFixedStack(FI),
610 false, false, false, 0));
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000611 }
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +0000612
613 if (!IsVarArg)
614 return Chain;
615
616 // This function takes variable arguments, some of which may have been passed
617 // in registers %i0-%i5. Variable floating point arguments are never passed
618 // in floating point registers. They go on %i0-%i5 or on the stack like
619 // integer arguments.
620 //
621 // The va_start intrinsic needs to know the offset to the first variable
622 // argument.
623 unsigned ArgOffset = CCInfo.getNextStackOffset();
624 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
625 // Skip the 128 bytes of register save area.
626 FuncInfo->setVarArgsFrameOffset(ArgOffset + ArgArea +
627 Subtarget->getStackPointerBias());
628
629 // Save the variable arguments that were passed in registers.
630 // The caller is required to reserve stack space for 6 arguments regardless
631 // of how many arguments were actually passed.
632 SmallVector<SDValue, 8> OutChains;
633 for (; ArgOffset < 6*8; ArgOffset += 8) {
634 unsigned VReg = MF.addLiveIn(SP::I0 + ArgOffset/8, &SP::I64RegsRegClass);
635 SDValue VArg = DAG.getCopyFromReg(Chain, DL, VReg, MVT::i64);
636 int FI = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset + ArgArea, true);
637 OutChains.push_back(DAG.getStore(Chain, DL, VArg,
638 DAG.getFrameIndex(FI, getPointerTy()),
639 MachinePointerInfo::getFixedStack(FI),
640 false, false, 0));
641 }
642
643 if (!OutChains.empty())
644 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
645 &OutChains[0], OutChains.size());
646
Jakob Stoklund Olesen0b21f352013-04-02 04:09:02 +0000647 return Chain;
648}
649
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000650SDValue
Justin Holewinskiaa583972012-05-25 16:35:28 +0000651SparcTargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
Dan Gohman21cea8a2010-04-17 15:26:15 +0000652 SmallVectorImpl<SDValue> &InVals) const {
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000653 if (Subtarget->is64Bit())
654 return LowerCall_64(CLI, InVals);
655 return LowerCall_32(CLI, InVals);
656}
657
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000658static bool hasReturnsTwiceAttr(SelectionDAG &DAG, SDValue Callee,
659 ImmutableCallSite *CS) {
660 if (CS)
661 return CS->hasFnAttr(Attribute::ReturnsTwice);
662
663 const Function *CalleeFn = 0;
664 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
665 CalleeFn = dyn_cast<Function>(G->getGlobal());
666 } else if (ExternalSymbolSDNode *E =
667 dyn_cast<ExternalSymbolSDNode>(Callee)) {
668 const Function *Fn = DAG.getMachineFunction().getFunction();
669 const Module *M = Fn->getParent();
670 const char *CalleeName = E->getSymbol();
671 CalleeFn = M->getFunction(CalleeName);
672 }
673
674 if (!CalleeFn)
675 return false;
676 return CalleeFn->hasFnAttribute(Attribute::ReturnsTwice);
677}
678
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +0000679// Lower a call for the 32-bit ABI.
680SDValue
681SparcTargetLowering::LowerCall_32(TargetLowering::CallLoweringInfo &CLI,
682 SmallVectorImpl<SDValue> &InVals) const {
Justin Holewinskiaa583972012-05-25 16:35:28 +0000683 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +0000684 SDLoc &dl = CLI.DL;
Craig Topperb94011f2013-07-14 04:42:23 +0000685 SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
686 SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
687 SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
Justin Holewinskiaa583972012-05-25 16:35:28 +0000688 SDValue Chain = CLI.Chain;
689 SDValue Callee = CLI.Callee;
690 bool &isTailCall = CLI.IsTailCall;
691 CallingConv::ID CallConv = CLI.CallConv;
692 bool isVarArg = CLI.IsVarArg;
693
Evan Cheng67a69dd2010-01-27 00:07:07 +0000694 // Sparc target does not yet support tail call optimization.
695 isTailCall = false;
Chris Lattnerdb26db22008-03-17 06:01:07 +0000696
Chris Lattner7d4152b2008-03-17 06:58:37 +0000697 // Analyze operands of the call, assigning locations to each operand.
698 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000699 CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000700 DAG.getTarget(), ArgLocs, *DAG.getContext());
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000701 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000702
Chris Lattner7d4152b2008-03-17 06:58:37 +0000703 // Get the size of the outgoing arguments stack space requirement.
704 unsigned ArgsSize = CCInfo.getNextStackOffset();
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000705
Chris Lattner49b269d2008-03-17 05:41:48 +0000706 // Keep stack frames 8-byte aligned.
707 ArgsSize = (ArgsSize+7) & ~7;
708
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000709 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
710
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000711 // Create local copies for byval args.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000712 SmallVector<SDValue, 8> ByValArgs;
713 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
714 ISD::ArgFlagsTy Flags = Outs[i].Flags;
715 if (!Flags.isByVal())
716 continue;
717
718 SDValue Arg = OutVals[i];
719 unsigned Size = Flags.getByValSize();
720 unsigned Align = Flags.getByValAlign();
721
722 int FI = MFI->CreateStackObject(Size, Align, false);
723 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
724 SDValue SizeNode = DAG.getConstant(Size, MVT::i32);
725
726 Chain = DAG.getMemcpy(Chain, dl, FIPtr, Arg, SizeNode, Align,
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000727 false, // isVolatile,
728 (Size <= 32), // AlwaysInline if size <= 32
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000729 MachinePointerInfo(), MachinePointerInfo());
730 ByValArgs.push_back(FIPtr);
731 }
732
Andrew Trickad6d08a2013-05-29 22:03:55 +0000733 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
734 dl);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000735
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000736 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
737 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000738
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000739 const unsigned StackOffset = 92;
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000740 bool hasStructRetAttr = false;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000741 // Walk the register/memloc assignments, inserting copies/loads.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000742 for (unsigned i = 0, realArgIdx = 0, byvalArgIdx = 0, e = ArgLocs.size();
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000743 i != e;
744 ++i, ++realArgIdx) {
Chris Lattner7d4152b2008-03-17 06:58:37 +0000745 CCValAssign &VA = ArgLocs[i];
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000746 SDValue Arg = OutVals[realArgIdx];
Chris Lattner7d4152b2008-03-17 06:58:37 +0000747
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000748 ISD::ArgFlagsTy Flags = Outs[realArgIdx].Flags;
749
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000750 // Use local copy if it is a byval arg.
Venkatraman Govindaraju05947892011-01-21 14:00:01 +0000751 if (Flags.isByVal())
752 Arg = ByValArgs[byvalArgIdx++];
753
Chris Lattner7d4152b2008-03-17 06:58:37 +0000754 // Promote the value if needed.
755 switch (VA.getLocInfo()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +0000756 default: llvm_unreachable("Unknown loc info!");
Chris Lattner7d4152b2008-03-17 06:58:37 +0000757 case CCValAssign::Full: break;
758 case CCValAssign::SExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000759 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000760 break;
761 case CCValAssign::ZExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000762 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000763 break;
764 case CCValAssign::AExt:
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000765 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
766 break;
767 case CCValAssign::BCvt:
768 Arg = DAG.getNode(ISD::BITCAST, dl, VA.getLocVT(), Arg);
Chris Lattner7d4152b2008-03-17 06:58:37 +0000769 break;
770 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000771
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000772 if (Flags.isSRet()) {
773 assert(VA.needsCustom());
774 // store SRet argument in %sp+64
775 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
776 SDValue PtrOff = DAG.getIntPtrConstant(64);
777 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
778 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
779 MachinePointerInfo(),
780 false, false, 0));
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000781 hasStructRetAttr = true;
Venkatraman Govindarajucc91b7a2011-01-22 13:05:16 +0000782 continue;
783 }
784
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000785 if (VA.needsCustom()) {
786 assert(VA.getLocVT() == MVT::f64);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000787
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000788 if (VA.isMemLoc()) {
789 unsigned Offset = VA.getLocMemOffset() + StackOffset;
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000790 // if it is double-word aligned, just store.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000791 if (Offset % 8 == 0) {
792 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
793 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
794 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
795 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
796 MachinePointerInfo(),
797 false, false, 0));
798 continue;
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000799 }
800 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000801
Owen Anderson9f944592009-08-11 20:47:22 +0000802 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peck527da1b2010-11-23 03:31:01 +0000803 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000804 Arg, StackPtr, MachinePointerInfo(),
David Greene772fc342010-02-15 16:57:02 +0000805 false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000806 // Sparc is big-endian, so the high part comes first.
Chris Lattner7727d052010-09-21 06:44:06 +0000807 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000808 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000809 // Increment the pointer to the other half.
Dale Johannesen021052a2009-02-04 20:06:27 +0000810 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000811 DAG.getIntPtrConstant(4));
812 // Load the low part.
Chris Lattner7727d052010-09-21 06:44:06 +0000813 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +0000814 MachinePointerInfo(), false, false, false, 0);
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000815
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000816 if (VA.isRegLoc()) {
817 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Hi));
818 assert(i+1 != e);
819 CCValAssign &NextVA = ArgLocs[++i];
820 if (NextVA.isRegLoc()) {
821 RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Lo));
822 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +0000823 // Store the low part in stack.
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000824 unsigned Offset = NextVA.getLocMemOffset() + StackOffset;
825 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
826 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
827 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
828 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
829 MachinePointerInfo(),
830 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000831 }
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000832 } else {
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000833 unsigned Offset = VA.getLocMemOffset() + StackOffset;
834 // Store the high part.
835 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
836 SDValue PtrOff = DAG.getIntPtrConstant(Offset);
837 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
838 MemOpChains.push_back(DAG.getStore(Chain, dl, Hi, PtrOff,
839 MachinePointerInfo(),
840 false, false, 0));
841 // Store the low part.
842 PtrOff = DAG.getIntPtrConstant(Offset+4);
843 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
844 MemOpChains.push_back(DAG.getStore(Chain, dl, Lo, PtrOff,
845 MachinePointerInfo(),
846 false, false, 0));
Venkatraman Govindaraju0a091602010-12-29 05:37:15 +0000847 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000848 continue;
Duncan Sandsdd6f3db2008-12-12 08:05:40 +0000849 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000850
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000851 // Arguments that can be passed on register must be kept at
852 // RegsToPass vector
853 if (VA.isRegLoc()) {
854 if (VA.getLocVT() != MVT::f32) {
855 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
856 continue;
857 }
858 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Arg);
859 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
860 continue;
Chris Lattner49b269d2008-03-17 05:41:48 +0000861 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000862
863 assert(VA.isMemLoc());
864
865 // Create a store off the stack pointer for this argument.
866 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
867 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+StackOffset);
868 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
869 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
870 MachinePointerInfo(),
871 false, false, 0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000872 }
Venkatraman Govindarajuc386f8a2011-01-18 06:09:55 +0000873
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000874
Chris Lattner49b269d2008-03-17 05:41:48 +0000875 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner7d4152b2008-03-17 06:58:37 +0000876 if (!MemOpChains.empty())
Owen Anderson9f944592009-08-11 20:47:22 +0000877 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner7d4152b2008-03-17 06:58:37 +0000878 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000879
880 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner7d4152b2008-03-17 06:58:37 +0000881 // chain and flag operands which copy the outgoing args into registers.
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000882 // The InFlag in necessary since all emitted instructions must be
Chris Lattner7d4152b2008-03-17 06:58:37 +0000883 // stuck together.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000884 SDValue InFlag;
Chris Lattner7d4152b2008-03-17 06:58:37 +0000885 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000886 unsigned Reg = toCallerWindow(RegsToPass[i].first);
Dale Johannesen021052a2009-02-04 20:06:27 +0000887 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner49b269d2008-03-17 05:41:48 +0000888 InFlag = Chain.getValue(1);
889 }
890
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000891 unsigned SRetArgSize = (hasStructRetAttr)? getSRetArgSize(DAG, Callee):0;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000892 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000893
Chris Lattner49b269d2008-03-17 05:41:48 +0000894 // If the callee is a GlobalAddress node (quite common, every direct call is)
895 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling24c79f22008-09-16 21:48:12 +0000896 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner49b269d2008-03-17 05:41:48 +0000897 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patela3ca21b2010-07-06 22:08:15 +0000898 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling24c79f22008-09-16 21:48:12 +0000899 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson9f944592009-08-11 20:47:22 +0000900 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner49b269d2008-03-17 05:41:48 +0000901
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000902 // Returns a chain & a flag for retval copy to use
903 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
904 SmallVector<SDValue, 8> Ops;
905 Ops.push_back(Chain);
906 Ops.push_back(Callee);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000907 if (hasStructRetAttr)
908 Ops.push_back(DAG.getTargetConstant(SRetArgSize, MVT::i32));
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000909 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
910 Ops.push_back(DAG.getRegister(toCallerWindow(RegsToPass[i].first),
911 RegsToPass[i].second.getValueType()));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000912
913 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +0000914 const SparcRegisterInfo *TRI =
915 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
916 const uint32_t *Mask = ((hasReturnsTwice)
917 ? TRI->getRTCallPreservedMask(CallConv)
918 : TRI->getCallPreservedMask(CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +0000919 assert(Mask && "Missing call preserved mask for calling convention");
920 Ops.push_back(DAG.getRegisterMask(Mask));
921
Venkatraman Govindaraju3b71b0a2011-01-12 03:18:21 +0000922 if (InFlag.getNode())
923 Ops.push_back(InFlag);
924
925 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner49b269d2008-03-17 05:41:48 +0000926 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000927
Chris Lattner27539552008-10-11 22:08:30 +0000928 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +0000929 DAG.getIntPtrConstant(0, true), InFlag, dl);
Chris Lattnerdb26db22008-03-17 06:01:07 +0000930 InFlag = Chain.getValue(1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000931
Chris Lattnerdb26db22008-03-17 06:01:07 +0000932 // Assign locations to each value returned by this call.
933 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000934 CCState RVInfo(CallConv, isVarArg, DAG.getMachineFunction(),
Bill Wendlingea6397f2012-07-19 00:11:40 +0000935 DAG.getTarget(), RVLocs, *DAG.getContext());
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000936
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000937 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000938
Chris Lattnerdb26db22008-03-17 06:01:07 +0000939 // Copy all of the result registers out of their specified physreg.
940 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +0000941 Chain = DAG.getCopyFromReg(Chain, dl, toCallerWindow(RVLocs[i].getLocReg()),
Chris Lattnerdb26db22008-03-17 06:01:07 +0000942 RVLocs[i].getValVT(), InFlag).getValue(1);
943 InFlag = Chain.getValue(2);
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000944 InVals.push_back(Chain.getValue(0));
Chris Lattner49b269d2008-03-17 05:41:48 +0000945 }
Anton Korobeynikovb8736562008-10-10 20:27:31 +0000946
Dan Gohmanf9bbcd12009-08-05 01:29:28 +0000947 return Chain;
Chris Lattner49b269d2008-03-17 05:41:48 +0000948}
949
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000950// This functions returns true if CalleeName is a ABI function that returns
951// a long double (fp128).
952static bool isFP128ABICall(const char *CalleeName)
953{
954 static const char *const ABICalls[] =
955 { "_Q_add", "_Q_sub", "_Q_mul", "_Q_div",
956 "_Q_sqrt", "_Q_neg",
957 "_Q_itoq", "_Q_stoq", "_Q_dtoq", "_Q_utoq",
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000958 "_Q_lltoq", "_Q_ulltoq",
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000959 0
960 };
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +0000961 for (const char * const *I = ABICalls; *I != 0; ++I)
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000962 if (strcmp(CalleeName, *I) == 0)
963 return true;
964 return false;
965}
966
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000967unsigned
968SparcTargetLowering::getSRetArgSize(SelectionDAG &DAG, SDValue Callee) const
969{
970 const Function *CalleeFn = 0;
971 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
972 CalleeFn = dyn_cast<Function>(G->getGlobal());
973 } else if (ExternalSymbolSDNode *E =
974 dyn_cast<ExternalSymbolSDNode>(Callee)) {
975 const Function *Fn = DAG.getMachineFunction().getFunction();
976 const Module *M = Fn->getParent();
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +0000977 const char *CalleeName = E->getSymbol();
978 CalleeFn = M->getFunction(CalleeName);
979 if (!CalleeFn && isFP128ABICall(CalleeName))
980 return 16; // Return sizeof(fp128)
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000981 }
Chris Lattner49b269d2008-03-17 05:41:48 +0000982
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000983 if (!CalleeFn)
984 return 0;
985
986 assert(CalleeFn->hasStructRetAttr() &&
987 "Callee does not have the StructRet attribute.");
988
Chris Lattner229907c2011-07-18 04:54:35 +0000989 PointerType *Ty = cast<PointerType>(CalleeFn->arg_begin()->getType());
990 Type *ElementTy = Ty->getElementType();
Micah Villmowcdfe20b2012-10-08 16:38:25 +0000991 return getDataLayout()->getTypeAllocSize(ElementTy);
Venkatraman Govindarajua82203f2011-02-21 03:42:44 +0000992}
Chris Lattner49b269d2008-03-17 05:41:48 +0000993
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +0000994
995// Fixup floating point arguments in the ... part of a varargs call.
996//
997// The SPARC v9 ABI requires that floating point arguments are treated the same
998// as integers when calling a varargs function. This does not apply to the
999// fixed arguments that are part of the function's prototype.
1000//
1001// This function post-processes a CCValAssign array created by
1002// AnalyzeCallOperands().
1003static void fixupVariableFloatArgs(SmallVectorImpl<CCValAssign> &ArgLocs,
1004 ArrayRef<ISD::OutputArg> Outs) {
1005 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1006 const CCValAssign &VA = ArgLocs[i];
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001007 MVT ValTy = VA.getLocVT();
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001008 // FIXME: What about f32 arguments? C promotes them to f64 when calling
1009 // varargs functions.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001010 if (!VA.isRegLoc() || (ValTy != MVT::f64 && ValTy != MVT::f128))
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001011 continue;
1012 // The fixed arguments to a varargs function still go in FP registers.
1013 if (Outs[VA.getValNo()].IsFixed)
1014 continue;
1015
1016 // This floating point argument should be reassigned.
1017 CCValAssign NewVA;
1018
1019 // Determine the offset into the argument array.
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001020 unsigned firstReg = (ValTy == MVT::f64) ? SP::D0 : SP::Q0;
1021 unsigned argSize = (ValTy == MVT::f64) ? 8 : 16;
1022 unsigned Offset = argSize * (VA.getLocReg() - firstReg);
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001023 assert(Offset < 16*8 && "Offset out of range, bad register enum?");
1024
1025 if (Offset < 6*8) {
1026 // This argument should go in %i0-%i5.
1027 unsigned IReg = SP::I0 + Offset/8;
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001028 if (ValTy == MVT::f64)
1029 // Full register, just bitconvert into i64.
1030 NewVA = CCValAssign::getReg(VA.getValNo(), VA.getValVT(),
1031 IReg, MVT::i64, CCValAssign::BCvt);
1032 else {
1033 assert(ValTy == MVT::f128 && "Unexpected type!");
1034 // Full register, just bitconvert into i128 -- We will lower this into
1035 // two i64s in LowerCall_64.
1036 NewVA = CCValAssign::getCustomReg(VA.getValNo(), VA.getValVT(),
1037 IReg, MVT::i128, CCValAssign::BCvt);
1038 }
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001039 } else {
1040 // This needs to go to memory, we're out of integer registers.
1041 NewVA = CCValAssign::getMem(VA.getValNo(), VA.getValVT(),
1042 Offset, VA.getLocVT(), VA.getLocInfo());
1043 }
1044 ArgLocs[i] = NewVA;
1045 }
1046}
1047
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001048// Lower a call for the 64-bit ABI.
1049SDValue
1050SparcTargetLowering::LowerCall_64(TargetLowering::CallLoweringInfo &CLI,
1051 SmallVectorImpl<SDValue> &InVals) const {
1052 SelectionDAG &DAG = CLI.DAG;
Andrew Trickef9de2a2013-05-25 02:42:55 +00001053 SDLoc DL = CLI.DL;
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001054 SDValue Chain = CLI.Chain;
1055
Venkatraman Govindaraju88124852013-10-09 12:50:39 +00001056 // Sparc target does not yet support tail call optimization.
1057 CLI.IsTailCall = false;
1058
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001059 // Analyze operands of the call, assigning locations to each operand.
1060 SmallVector<CCValAssign, 16> ArgLocs;
1061 CCState CCInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1062 DAG.getTarget(), ArgLocs, *DAG.getContext());
1063 CCInfo.AnalyzeCallOperands(CLI.Outs, CC_Sparc64);
1064
1065 // Get the size of the outgoing arguments stack space requirement.
1066 // The stack offset computed by CC_Sparc64 includes all arguments.
Jakob Stoklund Olesen2cfe46f2013-04-09 04:37:47 +00001067 // Called functions expect 6 argument words to exist in the stack frame, used
1068 // or not.
1069 unsigned ArgsSize = std::max(6*8u, CCInfo.getNextStackOffset());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001070
1071 // Keep stack frames 16-byte aligned.
1072 ArgsSize = RoundUpToAlignment(ArgsSize, 16);
1073
Jakob Stoklund Olesen84ebe252013-04-21 21:36:49 +00001074 // Varargs calls require special treatment.
1075 if (CLI.IsVarArg)
1076 fixupVariableFloatArgs(ArgLocs, CLI.Outs);
1077
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001078 // Adjust the stack pointer to make room for the arguments.
1079 // FIXME: Use hasReservedCallFrame to avoid %sp adjustments around all calls
1080 // with more than 6 arguments.
Andrew Trickad6d08a2013-05-29 22:03:55 +00001081 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true),
1082 DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001083
1084 // Collect the set of registers to pass to the function and their values.
1085 // This will be emitted as a sequence of CopyToReg nodes glued to the call
1086 // instruction.
1087 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1088
1089 // Collect chains from all the memory opeations that copy arguments to the
1090 // stack. They must follow the stack pointer adjustment above and precede the
1091 // call instruction itself.
1092 SmallVector<SDValue, 8> MemOpChains;
1093
1094 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1095 const CCValAssign &VA = ArgLocs[i];
1096 SDValue Arg = CLI.OutVals[i];
1097
1098 // Promote the value if needed.
1099 switch (VA.getLocInfo()) {
1100 default:
1101 llvm_unreachable("Unknown location info!");
1102 case CCValAssign::Full:
1103 break;
1104 case CCValAssign::SExt:
1105 Arg = DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Arg);
1106 break;
1107 case CCValAssign::ZExt:
1108 Arg = DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Arg);
1109 break;
1110 case CCValAssign::AExt:
1111 Arg = DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Arg);
1112 break;
1113 case CCValAssign::BCvt:
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001114 // fixupVariableFloatArgs() may create bitcasts from f128 to i128. But
1115 // SPARC does not support i128 natively. Lower it into two i64, see below.
1116 if (!VA.needsCustom() || VA.getValVT() != MVT::f128
1117 || VA.getLocVT() != MVT::i128)
1118 Arg = DAG.getNode(ISD::BITCAST, DL, VA.getLocVT(), Arg);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001119 break;
1120 }
1121
1122 if (VA.isRegLoc()) {
Venkatraman Govindaraju0776cc02013-12-29 01:20:36 +00001123 if (VA.needsCustom() && VA.getValVT() == MVT::f128
1124 && VA.getLocVT() == MVT::i128) {
1125 // Store and reload into the interger register reg and reg+1.
1126 unsigned Offset = 8 * (VA.getLocReg() - SP::I0);
1127 unsigned StackOffset = Offset + Subtarget->getStackPointerBias() + 128;
1128 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1129 SDValue HiPtrOff = DAG.getIntPtrConstant(StackOffset);
1130 HiPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1131 HiPtrOff);
1132 SDValue LoPtrOff = DAG.getIntPtrConstant(StackOffset + 8);
1133 LoPtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr,
1134 LoPtrOff);
1135
1136 // Store to %sp+BIAS+128+Offset
1137 SDValue Store = DAG.getStore(Chain, DL, Arg, HiPtrOff,
1138 MachinePointerInfo(),
1139 false, false, 0);
1140 // Load into Reg and Reg+1
1141 SDValue Hi64 = DAG.getLoad(MVT::i64, DL, Store, HiPtrOff,
1142 MachinePointerInfo(),
1143 false, false, false, 0);
1144 SDValue Lo64 = DAG.getLoad(MVT::i64, DL, Store, LoPtrOff,
1145 MachinePointerInfo(),
1146 false, false, false, 0);
1147 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()),
1148 Hi64));
1149 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()+1),
1150 Lo64));
1151 continue;
1152 }
1153
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001154 // The custom bit on an i32 return value indicates that it should be
1155 // passed in the high bits of the register.
1156 if (VA.getValVT() == MVT::i32 && VA.needsCustom()) {
1157 Arg = DAG.getNode(ISD::SHL, DL, MVT::i64, Arg,
1158 DAG.getConstant(32, MVT::i32));
1159
1160 // The next value may go in the low bits of the same register.
1161 // Handle both at once.
1162 if (i+1 < ArgLocs.size() && ArgLocs[i+1].isRegLoc() &&
1163 ArgLocs[i+1].getLocReg() == VA.getLocReg()) {
1164 SDValue NV = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64,
1165 CLI.OutVals[i+1]);
1166 Arg = DAG.getNode(ISD::OR, DL, MVT::i64, Arg, NV);
1167 // Skip the next value, it's already done.
1168 ++i;
1169 }
1170 }
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001171 RegsToPass.push_back(std::make_pair(toCallerWindow(VA.getLocReg()), Arg));
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001172 continue;
1173 }
1174
1175 assert(VA.isMemLoc());
1176
1177 // Create a store off the stack pointer for this argument.
1178 SDValue StackPtr = DAG.getRegister(SP::O6, getPointerTy());
1179 // The argument area starts at %fp+BIAS+128 in the callee frame,
1180 // %sp+BIAS+128 in ours.
1181 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset() +
1182 Subtarget->getStackPointerBias() +
1183 128);
1184 PtrOff = DAG.getNode(ISD::ADD, DL, getPointerTy(), StackPtr, PtrOff);
1185 MemOpChains.push_back(DAG.getStore(Chain, DL, Arg, PtrOff,
1186 MachinePointerInfo(),
1187 false, false, 0));
1188 }
1189
1190 // Emit all stores, make sure they occur before the call.
1191 if (!MemOpChains.empty())
1192 Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1193 &MemOpChains[0], MemOpChains.size());
1194
1195 // Build a sequence of CopyToReg nodes glued together with token chain and
1196 // glue operands which copy the outgoing args into registers. The InGlue is
1197 // necessary since all emitted instructions must be stuck together in order
1198 // to pass the live physical registers.
1199 SDValue InGlue;
1200 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1201 Chain = DAG.getCopyToReg(Chain, DL,
1202 RegsToPass[i].first, RegsToPass[i].second, InGlue);
1203 InGlue = Chain.getValue(1);
1204 }
1205
1206 // If the callee is a GlobalAddress node (quite common, every direct call is)
1207 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1208 // Likewise ExternalSymbol -> TargetExternalSymbol.
1209 SDValue Callee = CLI.Callee;
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001210 bool hasReturnsTwice = hasReturnsTwiceAttr(DAG, Callee, CLI.CS);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001211 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1212 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, getPointerTy());
1213 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
1214 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), getPointerTy());
1215
1216 // Build the operands for the call instruction itself.
1217 SmallVector<SDValue, 8> Ops;
1218 Ops.push_back(Chain);
1219 Ops.push_back(Callee);
1220 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1221 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1222 RegsToPass[i].second.getValueType()));
1223
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001224 // Add a register mask operand representing the call-preserved registers.
Venkatraman Govindaraju55ecb102013-09-05 05:32:16 +00001225 const SparcRegisterInfo *TRI =
1226 ((const SparcTargetMachine&)getTargetMachine()).getRegisterInfo();
1227 const uint32_t *Mask = ((hasReturnsTwice)
1228 ? TRI->getRTCallPreservedMask(CLI.CallConv)
1229 : TRI->getCallPreservedMask(CLI.CallConv));
Jakob Stoklund Olesen0c007042013-08-23 02:33:47 +00001230 assert(Mask && "Missing call preserved mask for calling convention");
1231 Ops.push_back(DAG.getRegisterMask(Mask));
1232
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001233 // Make sure the CopyToReg nodes are glued to the call instruction which
1234 // consumes the registers.
1235 if (InGlue.getNode())
1236 Ops.push_back(InGlue);
1237
1238 // Now the call itself.
1239 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1240 Chain = DAG.getNode(SPISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
1241 InGlue = Chain.getValue(1);
1242
1243 // Revert the stack pointer immediately after the call.
1244 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
Andrew Trickad6d08a2013-05-29 22:03:55 +00001245 DAG.getIntPtrConstant(0, true), InGlue, DL);
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001246 InGlue = Chain.getValue(1);
1247
1248 // Now extract the return values. This is more or less the same as
1249 // LowerFormalArguments_64.
1250
1251 // Assign locations to each value returned by this call.
1252 SmallVector<CCValAssign, 16> RVLocs;
1253 CCState RVInfo(CLI.CallConv, CLI.IsVarArg, DAG.getMachineFunction(),
1254 DAG.getTarget(), RVLocs, *DAG.getContext());
1255 RVInfo.AnalyzeCallResult(CLI.Ins, CC_Sparc64);
1256
1257 // Copy all of the result registers out of their specified physreg.
1258 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1259 CCValAssign &VA = RVLocs[i];
Jakob Stoklund Olesenc910feb2013-04-09 05:11:52 +00001260 unsigned Reg = toCallerWindow(VA.getLocReg());
Jakob Stoklund Olesena30f4832013-04-07 19:10:57 +00001261
1262 // When returning 'inreg {i32, i32 }', two consecutive i32 arguments can
1263 // reside in the same register in the high and low bits. Reuse the
1264 // CopyFromReg previous node to avoid duplicate copies.
1265 SDValue RV;
1266 if (RegisterSDNode *SrcReg = dyn_cast<RegisterSDNode>(Chain.getOperand(1)))
1267 if (SrcReg->getReg() == Reg && Chain->getOpcode() == ISD::CopyFromReg)
1268 RV = Chain.getValue(0);
1269
1270 // But usually we'll create a new CopyFromReg for a different register.
1271 if (!RV.getNode()) {
1272 RV = DAG.getCopyFromReg(Chain, DL, Reg, RVLocs[i].getLocVT(), InGlue);
1273 Chain = RV.getValue(1);
1274 InGlue = Chain.getValue(2);
1275 }
1276
1277 // Get the high bits for i32 struct elements.
1278 if (VA.getValVT() == MVT::i32 && VA.needsCustom())
1279 RV = DAG.getNode(ISD::SRL, DL, VA.getLocVT(), RV,
1280 DAG.getConstant(32, MVT::i32));
1281
1282 // The callee promoted the return value, so insert an Assert?ext SDNode so
1283 // we won't promote the value again in this function.
1284 switch (VA.getLocInfo()) {
1285 case CCValAssign::SExt:
1286 RV = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), RV,
1287 DAG.getValueType(VA.getValVT()));
1288 break;
1289 case CCValAssign::ZExt:
1290 RV = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), RV,
1291 DAG.getValueType(VA.getValVT()));
1292 break;
1293 default:
1294 break;
1295 }
1296
1297 // Truncate the register down to the return value type.
1298 if (VA.isExtInLoc())
1299 RV = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), RV);
1300
1301 InVals.push_back(RV);
1302 }
1303
1304 return Chain;
1305}
1306
Chris Lattner0a1762e2008-03-17 03:21:36 +00001307//===----------------------------------------------------------------------===//
1308// TargetLowering Implementation
1309//===----------------------------------------------------------------------===//
1310
1311/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
1312/// condition.
1313static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
1314 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001315 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001316 case ISD::SETEQ: return SPCC::ICC_E;
1317 case ISD::SETNE: return SPCC::ICC_NE;
1318 case ISD::SETLT: return SPCC::ICC_L;
1319 case ISD::SETGT: return SPCC::ICC_G;
1320 case ISD::SETLE: return SPCC::ICC_LE;
1321 case ISD::SETGE: return SPCC::ICC_GE;
1322 case ISD::SETULT: return SPCC::ICC_CS;
1323 case ISD::SETULE: return SPCC::ICC_LEU;
1324 case ISD::SETUGT: return SPCC::ICC_GU;
1325 case ISD::SETUGE: return SPCC::ICC_CC;
1326 }
1327}
1328
1329/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
1330/// FCC condition.
1331static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
1332 switch (CC) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00001333 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00001334 case ISD::SETEQ:
1335 case ISD::SETOEQ: return SPCC::FCC_E;
1336 case ISD::SETNE:
1337 case ISD::SETUNE: return SPCC::FCC_NE;
1338 case ISD::SETLT:
1339 case ISD::SETOLT: return SPCC::FCC_L;
1340 case ISD::SETGT:
1341 case ISD::SETOGT: return SPCC::FCC_G;
1342 case ISD::SETLE:
1343 case ISD::SETOLE: return SPCC::FCC_LE;
1344 case ISD::SETGE:
1345 case ISD::SETOGE: return SPCC::FCC_GE;
1346 case ISD::SETULT: return SPCC::FCC_UL;
1347 case ISD::SETULE: return SPCC::FCC_ULE;
1348 case ISD::SETUGT: return SPCC::FCC_UG;
1349 case ISD::SETUGE: return SPCC::FCC_UGE;
1350 case ISD::SETUO: return SPCC::FCC_U;
1351 case ISD::SETO: return SPCC::FCC_O;
1352 case ISD::SETONE: return SPCC::FCC_LG;
1353 case ISD::SETUEQ: return SPCC::FCC_UE;
1354 }
1355}
1356
Chris Lattner0a1762e2008-03-17 03:21:36 +00001357SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattnerc9ea8fd2009-08-08 20:43:12 +00001358 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001359 Subtarget = &TM.getSubtarget<SparcSubtarget>();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001360
Chris Lattner0a1762e2008-03-17 03:21:36 +00001361 // Set up the register classes.
Craig Topperabadc662012-04-20 06:31:50 +00001362 addRegisterClass(MVT::i32, &SP::IntRegsRegClass);
1363 addRegisterClass(MVT::f32, &SP::FPRegsRegClass);
1364 addRegisterClass(MVT::f64, &SP::DFPRegsRegClass);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001365 addRegisterClass(MVT::f128, &SP::QFPRegsRegClass);
Jakob Stoklund Olesen5ad3b352013-04-02 04:08:54 +00001366 if (Subtarget->is64Bit())
1367 addRegisterClass(MVT::i64, &SP::I64RegsRegClass);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001368
1369 // Turn FP extload into load/fextend
Owen Anderson9f944592009-08-11 20:47:22 +00001370 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001371 setLoadExtAction(ISD::EXTLOAD, MVT::f64, Expand);
1372
Chris Lattner0a1762e2008-03-17 03:21:36 +00001373 // Sparc doesn't have i1 sign extending load
Owen Anderson9f944592009-08-11 20:47:22 +00001374 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001375
Chris Lattner0a1762e2008-03-17 03:21:36 +00001376 // Turn FP truncstore into trunc + store.
Owen Anderson9f944592009-08-11 20:47:22 +00001377 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001378 setTruncStoreAction(MVT::f128, MVT::f32, Expand);
1379 setTruncStoreAction(MVT::f128, MVT::f64, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001380
1381 // Custom legalize GlobalAddress nodes into LO/HI parts.
Jakob Stoklund Olesen15b3e902013-04-13 19:02:23 +00001382 setOperationAction(ISD::GlobalAddress, getPointerTy(), Custom);
1383 setOperationAction(ISD::GlobalTLSAddress, getPointerTy(), Custom);
1384 setOperationAction(ISD::ConstantPool, getPointerTy(), Custom);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001385 setOperationAction(ISD::BlockAddress, getPointerTy(), Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001386
Chris Lattner0a1762e2008-03-17 03:21:36 +00001387 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson9f944592009-08-11 20:47:22 +00001388 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
1389 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
1390 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001391
1392 // Sparc has no REM or DIVREM operations.
Owen Anderson9f944592009-08-11 20:47:22 +00001393 setOperationAction(ISD::UREM, MVT::i32, Expand);
1394 setOperationAction(ISD::SREM, MVT::i32, Expand);
1395 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
1396 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001397
Roman Divacky2262cfa2013-10-31 19:22:33 +00001398 // ... nor does SparcV9.
1399 if (Subtarget->is64Bit()) {
1400 setOperationAction(ISD::UREM, MVT::i64, Expand);
1401 setOperationAction(ISD::SREM, MVT::i64, Expand);
1402 setOperationAction(ISD::SDIVREM, MVT::i64, Expand);
1403 setOperationAction(ISD::UDIVREM, MVT::i64, Expand);
1404 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00001405
1406 // Custom expand fp<->sint
Owen Anderson9f944592009-08-11 20:47:22 +00001407 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
1408 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001409 setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom);
1410 setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001411
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001412 // Custom Expand fp<->uint
1413 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom);
1414 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001415 setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom);
1416 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001417
Wesley Peck527da1b2010-11-23 03:31:01 +00001418 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
1419 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001420
Chris Lattner0a1762e2008-03-17 03:21:36 +00001421 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001422 setOperationAction(ISD::SELECT, MVT::i32, Expand);
1423 setOperationAction(ISD::SELECT, MVT::f32, Expand);
1424 setOperationAction(ISD::SELECT, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001425 setOperationAction(ISD::SELECT, MVT::f128, Expand);
1426
Owen Anderson9f944592009-08-11 20:47:22 +00001427 setOperationAction(ISD::SETCC, MVT::i32, Expand);
1428 setOperationAction(ISD::SETCC, MVT::f32, Expand);
1429 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001430 setOperationAction(ISD::SETCC, MVT::f128, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001431
Chris Lattner0a1762e2008-03-17 03:21:36 +00001432 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson9f944592009-08-11 20:47:22 +00001433 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1434 setOperationAction(ISD::BRIND, MVT::Other, Expand);
1435 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1436 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
1437 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
1438 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001439 setOperationAction(ISD::BR_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001440
Owen Anderson9f944592009-08-11 20:47:22 +00001441 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
1442 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
1443 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001444 setOperationAction(ISD::SELECT_CC, MVT::f128, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001445
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001446 if (Subtarget->is64Bit()) {
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00001447 setOperationAction(ISD::ADDC, MVT::i64, Custom);
1448 setOperationAction(ISD::ADDE, MVT::i64, Custom);
1449 setOperationAction(ISD::SUBC, MVT::i64, Custom);
1450 setOperationAction(ISD::SUBE, MVT::i64, Custom);
Jakob Stoklund Olesenf9278002013-05-20 01:01:43 +00001451 setOperationAction(ISD::BITCAST, MVT::f64, Expand);
1452 setOperationAction(ISD::BITCAST, MVT::i64, Expand);
Jakob Stoklund Olesen751e9b82013-05-20 00:28:36 +00001453 setOperationAction(ISD::SELECT, MVT::i64, Expand);
1454 setOperationAction(ISD::SETCC, MVT::i64, Expand);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001455 setOperationAction(ISD::BR_CC, MVT::i64, Custom);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001456 setOperationAction(ISD::SELECT_CC, MVT::i64, Custom);
Venkatraman Govindaraju5615aca2013-11-03 05:59:07 +00001457
1458 setOperationAction(ISD::CTPOP, MVT::i64, Legal);
1459 setOperationAction(ISD::CTTZ , MVT::i64, Expand);
1460 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
1461 setOperationAction(ISD::CTLZ , MVT::i64, Expand);
1462 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
1463 setOperationAction(ISD::BSWAP, MVT::i64, Expand);
Roman Divackyb6517852013-11-12 19:04:45 +00001464 setOperationAction(ISD::ROTL , MVT::i64, Expand);
1465 setOperationAction(ISD::ROTR , MVT::i64, Expand);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00001466 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Custom);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001467 }
1468
Eli Friedman26a48482011-07-27 22:21:52 +00001469 // FIXME: There are instructions available for ATOMIC_FENCE
1470 // on SparcV8 and later.
Eli Friedman26a48482011-07-27 22:21:52 +00001471 setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001472
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001473 if (!Subtarget->isV9()) {
1474 // SparcV8 does not have FNEGD and FABSD.
1475 setOperationAction(ISD::FNEG, MVT::f64, Custom);
1476 setOperationAction(ISD::FABS, MVT::f64, Custom);
1477 }
1478
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001479 setOperationAction(ISD::FSIN , MVT::f128, Expand);
1480 setOperationAction(ISD::FCOS , MVT::f128, Expand);
1481 setOperationAction(ISD::FSINCOS, MVT::f128, Expand);
1482 setOperationAction(ISD::FREM , MVT::f128, Expand);
1483 setOperationAction(ISD::FMA , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001484 setOperationAction(ISD::FSIN , MVT::f64, Expand);
1485 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001486 setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001487 setOperationAction(ISD::FREM , MVT::f64, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001488 setOperationAction(ISD::FMA , MVT::f64, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001489 setOperationAction(ISD::FSIN , MVT::f32, Expand);
1490 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Evan Cheng0e88c7d2013-01-29 02:32:37 +00001491 setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001492 setOperationAction(ISD::FREM , MVT::f32, Expand);
Cameron Zwarichf03fa182011-07-08 21:39:21 +00001493 setOperationAction(ISD::FMA , MVT::f32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001494 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
1495 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001496 setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001497 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Chandler Carruth637cc6a2011-12-13 01:56:10 +00001498 setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001499 setOperationAction(ISD::ROTL , MVT::i32, Expand);
1500 setOperationAction(ISD::ROTR , MVT::i32, Expand);
1501 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001502 setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001503 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
1504 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001505 setOperationAction(ISD::FPOW , MVT::f128, Expand);
Owen Anderson9f944592009-08-11 20:47:22 +00001506 setOperationAction(ISD::FPOW , MVT::f64, Expand);
1507 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001508
Owen Anderson9f944592009-08-11 20:47:22 +00001509 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
1510 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
1511 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001512
1513 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson9f944592009-08-11 20:47:22 +00001514 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
1515 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001516
Venkatraman Govindaraju72cc2482013-12-08 22:06:07 +00001517 if (Subtarget->is64Bit()) {
1518 setOperationAction(ISD::UMUL_LOHI, MVT::i64, Expand);
1519 setOperationAction(ISD::SMUL_LOHI, MVT::i64, Expand);
1520 setOperationAction(ISD::MULHU, MVT::i64, Expand);
1521 setOperationAction(ISD::MULHS, MVT::i64, Expand);
1522 }
1523
Chris Lattner0a1762e2008-03-17 03:21:36 +00001524 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson9f944592009-08-11 20:47:22 +00001525 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001526 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson9f944592009-08-11 20:47:22 +00001527 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001528
Chris Lattner0a1762e2008-03-17 03:21:36 +00001529 // Use the default implementation.
Owen Anderson9f944592009-08-11 20:47:22 +00001530 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
1531 setOperationAction(ISD::VAEND , MVT::Other, Expand);
1532 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
1533 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
1534 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001535
Venkatraman Govindaraju4c0cdd72013-09-26 15:11:00 +00001536 setExceptionPointerRegister(SP::I0);
1537 setExceptionSelectorRegister(SP::I1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001538
Chris Lattner0a1762e2008-03-17 03:21:36 +00001539 setStackPointerRegisterToSaveRestore(SP::O6);
1540
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00001541 if (Subtarget->isV9())
Owen Anderson9f944592009-08-11 20:47:22 +00001542 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001543
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001544 if (Subtarget->isV9() && Subtarget->hasHardQuad()) {
1545 setOperationAction(ISD::LOAD, MVT::f128, Legal);
1546 setOperationAction(ISD::STORE, MVT::f128, Legal);
1547 } else {
1548 setOperationAction(ISD::LOAD, MVT::f128, Custom);
1549 setOperationAction(ISD::STORE, MVT::f128, Custom);
1550 }
1551
1552 if (Subtarget->hasHardQuad()) {
1553 setOperationAction(ISD::FADD, MVT::f128, Legal);
1554 setOperationAction(ISD::FSUB, MVT::f128, Legal);
1555 setOperationAction(ISD::FMUL, MVT::f128, Legal);
1556 setOperationAction(ISD::FDIV, MVT::f128, Legal);
1557 setOperationAction(ISD::FSQRT, MVT::f128, Legal);
1558 setOperationAction(ISD::FP_EXTEND, MVT::f128, Legal);
1559 setOperationAction(ISD::FP_ROUND, MVT::f64, Legal);
1560 if (Subtarget->isV9()) {
1561 setOperationAction(ISD::FNEG, MVT::f128, Legal);
1562 setOperationAction(ISD::FABS, MVT::f128, Legal);
1563 } else {
1564 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1565 setOperationAction(ISD::FABS, MVT::f128, Custom);
1566 }
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001567
1568 if (!Subtarget->is64Bit()) {
1569 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1570 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1571 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1572 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
1573 }
1574
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001575 } else {
1576 // Custom legalize f128 operations.
1577
1578 setOperationAction(ISD::FADD, MVT::f128, Custom);
1579 setOperationAction(ISD::FSUB, MVT::f128, Custom);
1580 setOperationAction(ISD::FMUL, MVT::f128, Custom);
1581 setOperationAction(ISD::FDIV, MVT::f128, Custom);
1582 setOperationAction(ISD::FSQRT, MVT::f128, Custom);
1583 setOperationAction(ISD::FNEG, MVT::f128, Custom);
1584 setOperationAction(ISD::FABS, MVT::f128, Custom);
1585
1586 setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom);
1587 setOperationAction(ISD::FP_ROUND, MVT::f64, Custom);
1588 setOperationAction(ISD::FP_ROUND, MVT::f32, Custom);
1589
1590 // Setup Runtime library names.
1591 if (Subtarget->is64Bit()) {
1592 setLibcallName(RTLIB::ADD_F128, "_Qp_add");
1593 setLibcallName(RTLIB::SUB_F128, "_Qp_sub");
1594 setLibcallName(RTLIB::MUL_F128, "_Qp_mul");
1595 setLibcallName(RTLIB::DIV_F128, "_Qp_div");
1596 setLibcallName(RTLIB::SQRT_F128, "_Qp_sqrt");
1597 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Qp_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001598 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Qp_qtoui");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001599 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Qp_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001600 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Qp_uitoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001601 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Qp_qtox");
1602 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Qp_qtoux");
1603 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Qp_xtoq");
1604 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Qp_uxtoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001605 setLibcallName(RTLIB::FPEXT_F32_F128, "_Qp_stoq");
1606 setLibcallName(RTLIB::FPEXT_F64_F128, "_Qp_dtoq");
1607 setLibcallName(RTLIB::FPROUND_F128_F32, "_Qp_qtos");
1608 setLibcallName(RTLIB::FPROUND_F128_F64, "_Qp_qtod");
1609 } else {
1610 setLibcallName(RTLIB::ADD_F128, "_Q_add");
1611 setLibcallName(RTLIB::SUB_F128, "_Q_sub");
1612 setLibcallName(RTLIB::MUL_F128, "_Q_mul");
1613 setLibcallName(RTLIB::DIV_F128, "_Q_div");
1614 setLibcallName(RTLIB::SQRT_F128, "_Q_sqrt");
1615 setLibcallName(RTLIB::FPTOSINT_F128_I32, "_Q_qtoi");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001616 setLibcallName(RTLIB::FPTOUINT_F128_I32, "_Q_qtou");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001617 setLibcallName(RTLIB::SINTTOFP_I32_F128, "_Q_itoq");
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00001618 setLibcallName(RTLIB::UINTTOFP_I32_F128, "_Q_utoq");
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001619 setLibcallName(RTLIB::FPTOSINT_F128_I64, "_Q_qtoll");
1620 setLibcallName(RTLIB::FPTOUINT_F128_I64, "_Q_qtoull");
1621 setLibcallName(RTLIB::SINTTOFP_I64_F128, "_Q_lltoq");
1622 setLibcallName(RTLIB::UINTTOFP_I64_F128, "_Q_ulltoq");
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001623 setLibcallName(RTLIB::FPEXT_F32_F128, "_Q_stoq");
1624 setLibcallName(RTLIB::FPEXT_F64_F128, "_Q_dtoq");
1625 setLibcallName(RTLIB::FPROUND_F128_F32, "_Q_qtos");
1626 setLibcallName(RTLIB::FPROUND_F128_F64, "_Q_qtod");
1627 }
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00001628 }
1629
Eli Friedman2518f832011-05-06 20:34:06 +00001630 setMinFunctionAlignment(2);
1631
Chris Lattner0a1762e2008-03-17 03:21:36 +00001632 computeRegisterProperties();
1633}
1634
1635const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
1636 switch (Opcode) {
1637 default: return 0;
1638 case SPISD::CMPICC: return "SPISD::CMPICC";
1639 case SPISD::CMPFCC: return "SPISD::CMPFCC";
1640 case SPISD::BRICC: return "SPISD::BRICC";
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00001641 case SPISD::BRXCC: return "SPISD::BRXCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001642 case SPISD::BRFCC: return "SPISD::BRFCC";
1643 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001644 case SPISD::SELECT_XCC: return "SPISD::SELECT_XCC";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001645 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
1646 case SPISD::Hi: return "SPISD::Hi";
1647 case SPISD::Lo: return "SPISD::Lo";
1648 case SPISD::FTOI: return "SPISD::FTOI";
1649 case SPISD::ITOF: return "SPISD::ITOF";
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00001650 case SPISD::FTOX: return "SPISD::FTOX";
1651 case SPISD::XTOF: return "SPISD::XTOF";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001652 case SPISD::CALL: return "SPISD::CALL";
1653 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00001654 case SPISD::GLOBAL_BASE_REG: return "SPISD::GLOBAL_BASE_REG";
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00001655 case SPISD::FLUSHW: return "SPISD::FLUSHW";
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001656 case SPISD::TLS_ADD: return "SPISD::TLS_ADD";
1657 case SPISD::TLS_LD: return "SPISD::TLS_LD";
1658 case SPISD::TLS_CALL: return "SPISD::TLS_CALL";
Chris Lattner0a1762e2008-03-17 03:21:36 +00001659 }
1660}
1661
Venkatraman Govindarajuf6c8fe92013-12-09 04:02:15 +00001662EVT SparcTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
1663 if (!VT.isVector())
1664 return MVT::i32;
1665 return VT.changeVectorElementTypeToInteger();
1666}
1667
Chris Lattner0a1762e2008-03-17 03:21:36 +00001668/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
1669/// be zero. Op is expected to be a target specific node. Used by DAG
1670/// combiner.
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00001671void SparcTargetLowering::computeMaskedBitsForTargetNode
1672 (const SDValue Op,
1673 APInt &KnownZero,
1674 APInt &KnownOne,
1675 const SelectionDAG &DAG,
1676 unsigned Depth) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00001677 APInt KnownZero2, KnownOne2;
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001678 KnownZero = KnownOne = APInt(KnownZero.getBitWidth(), 0);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001679
Chris Lattner0a1762e2008-03-17 03:21:36 +00001680 switch (Op.getOpcode()) {
1681 default: break;
1682 case SPISD::SELECT_ICC:
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001683 case SPISD::SELECT_XCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00001684 case SPISD::SELECT_FCC:
Rafael Espindolaba0a6ca2012-04-04 12:51:34 +00001685 DAG.ComputeMaskedBits(Op.getOperand(1), KnownZero, KnownOne, Depth+1);
1686 DAG.ComputeMaskedBits(Op.getOperand(0), KnownZero2, KnownOne2, Depth+1);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001687 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
1688 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
1689
Chris Lattner0a1762e2008-03-17 03:21:36 +00001690 // Only known if known in both the LHS and RHS.
1691 KnownOne &= KnownOne2;
1692 KnownZero &= KnownZero2;
1693 break;
1694 }
1695}
1696
Chris Lattner0a1762e2008-03-17 03:21:36 +00001697// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
1698// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001699static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattner0a1762e2008-03-17 03:21:36 +00001700 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001701 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001702 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikovb8736562008-10-10 20:27:31 +00001703 CC == ISD::SETNE &&
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00001704 (((LHS.getOpcode() == SPISD::SELECT_ICC ||
1705 LHS.getOpcode() == SPISD::SELECT_XCC) &&
Chris Lattner0a1762e2008-03-17 03:21:36 +00001706 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
1707 (LHS.getOpcode() == SPISD::SELECT_FCC &&
1708 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
1709 isa<ConstantSDNode>(LHS.getOperand(0)) &&
1710 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf1d83042010-06-18 14:22:04 +00001711 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
1712 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001713 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmaneffb8942008-09-12 16:56:44 +00001714 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattner0a1762e2008-03-17 03:21:36 +00001715 LHS = CMPCC.getOperand(0);
1716 RHS = CMPCC.getOperand(1);
1717 }
1718}
1719
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001720// Convert to a target node and set target flags.
1721SDValue SparcTargetLowering::withTargetFlags(SDValue Op, unsigned TF,
1722 SelectionDAG &DAG) const {
1723 if (const GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op))
1724 return DAG.getTargetGlobalAddress(GA->getGlobal(),
Andrew Trickef9de2a2013-05-25 02:42:55 +00001725 SDLoc(GA),
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001726 GA->getValueType(0),
1727 GA->getOffset(), TF);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001728
1729 if (const ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Op))
1730 return DAG.getTargetConstantPool(CP->getConstVal(),
1731 CP->getValueType(0),
1732 CP->getAlignment(),
1733 CP->getOffset(), TF);
1734
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001735 if (const BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(Op))
1736 return DAG.getTargetBlockAddress(BA->getBlockAddress(),
1737 Op.getValueType(),
1738 0,
1739 TF);
1740
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001741 if (const ExternalSymbolSDNode *ES = dyn_cast<ExternalSymbolSDNode>(Op))
1742 return DAG.getTargetExternalSymbol(ES->getSymbol(),
1743 ES->getValueType(0), TF);
1744
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001745 llvm_unreachable("Unhandled address SDNode");
1746}
1747
1748// Split Op into high and low parts according to HiTF and LoTF.
1749// Return an ADD node combining the parts.
1750SDValue SparcTargetLowering::makeHiLoPair(SDValue Op,
1751 unsigned HiTF, unsigned LoTF,
1752 SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001753 SDLoc DL(Op);
Jakob Stoklund Olesen1fb08a82013-04-14 01:33:32 +00001754 EVT VT = Op.getValueType();
1755 SDValue Hi = DAG.getNode(SPISD::Hi, DL, VT, withTargetFlags(Op, HiTF, DAG));
1756 SDValue Lo = DAG.getNode(SPISD::Lo, DL, VT, withTargetFlags(Op, LoTF, DAG));
1757 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1758}
1759
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001760// Build SDNodes for producing an address from a GlobalAddress, ConstantPool,
1761// or ExternalSymbol SDNode.
1762SDValue SparcTargetLowering::makeAddress(SDValue Op, SelectionDAG &DAG) const {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001763 SDLoc DL(Op);
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001764 EVT VT = getPointerTy();
1765
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001766 // Handle PIC mode first.
1767 if (getTargetMachine().getRelocationModel() == Reloc::PIC_) {
1768 // This is the pic32 code model, the GOT is known to be smaller than 4GB.
1769 SDValue HiLo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001770 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, VT);
1771 SDValue AbsAddr = DAG.getNode(ISD::ADD, DL, VT, GlobalBase, HiLo);
Venkatraman Govindaraju7e7eb8c2013-09-22 01:40:24 +00001772 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1773 // function has calls.
1774 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1775 MFI->setHasCalls(true);
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001776 return DAG.getLoad(VT, DL, DAG.getEntryNode(), AbsAddr,
1777 MachinePointerInfo::getGOT(), false, false, false, 0);
1778 }
1779
1780 // This is one of the absolute code models.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001781 switch(getTargetMachine().getCodeModel()) {
1782 default:
1783 llvm_unreachable("Unsupported absolute code model");
Venkatraman Govindaraju2ea4c282013-10-08 07:15:22 +00001784 case CodeModel::JITDefault:
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001785 case CodeModel::Small:
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001786 // abs32.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001787 return makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1788 case CodeModel::Medium: {
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001789 // abs44.
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001790 SDValue H44 = makeHiLoPair(Op, SPII::MO_H44, SPII::MO_M44, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001791 H44 = DAG.getNode(ISD::SHL, DL, VT, H44, DAG.getConstant(12, MVT::i32));
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001792 SDValue L44 = withTargetFlags(Op, SPII::MO_L44, DAG);
1793 L44 = DAG.getNode(SPISD::Lo, DL, VT, L44);
1794 return DAG.getNode(ISD::ADD, DL, VT, H44, L44);
1795 }
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001796 case CodeModel::Large: {
1797 // abs64.
1798 SDValue Hi = makeHiLoPair(Op, SPII::MO_HH, SPII::MO_HM, DAG);
Jakob Stoklund Oleseneed10722013-04-14 05:48:50 +00001799 Hi = DAG.getNode(ISD::SHL, DL, VT, Hi, DAG.getConstant(32, MVT::i32));
Jakob Stoklund Olesenc3c28f82013-04-14 05:10:36 +00001800 SDValue Lo = makeHiLoPair(Op, SPII::MO_HI, SPII::MO_LO, DAG);
1801 return DAG.getNode(ISD::ADD, DL, VT, Hi, Lo);
1802 }
Jakob Stoklund Olesenc8fc76b2013-04-14 04:57:51 +00001803 }
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001804}
1805
Wesley Peck527da1b2010-11-23 03:31:01 +00001806SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001807 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001808 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001809}
1810
Chris Lattner840c7002009-09-15 17:46:24 +00001811SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohman21cea8a2010-04-17 15:26:15 +00001812 SelectionDAG &DAG) const {
Jakob Stoklund Olesene0fc8322013-04-14 04:35:16 +00001813 return makeAddress(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00001814}
1815
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00001816SDValue SparcTargetLowering::LowerBlockAddress(SDValue Op,
1817 SelectionDAG &DAG) const {
1818 return makeAddress(Op, DAG);
1819}
1820
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00001821SDValue SparcTargetLowering::LowerGlobalTLSAddress(SDValue Op,
1822 SelectionDAG &DAG) const {
1823
1824 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
1825 SDLoc DL(GA);
1826 const GlobalValue *GV = GA->getGlobal();
1827 EVT PtrVT = getPointerTy();
1828
1829 TLSModel::Model model = getTargetMachine().getTLSModel(GV);
1830
1831 if (model == TLSModel::GeneralDynamic || model == TLSModel::LocalDynamic) {
1832 unsigned HiTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_HI22
1833 : SPII::MO_TLS_LDM_HI22);
1834 unsigned LoTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_LO10
1835 : SPII::MO_TLS_LDM_LO10);
1836 unsigned addTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_ADD
1837 : SPII::MO_TLS_LDM_ADD);
1838 unsigned callTF = ((model == TLSModel::GeneralDynamic)? SPII::MO_TLS_GD_CALL
1839 : SPII::MO_TLS_LDM_CALL);
1840
1841 SDValue HiLo = makeHiLoPair(Op, HiTF, LoTF, DAG);
1842 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1843 SDValue Argument = DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Base, HiLo,
1844 withTargetFlags(Op, addTF, DAG));
1845
1846 SDValue Chain = DAG.getEntryNode();
1847 SDValue InFlag;
1848
1849 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(1, true), DL);
1850 Chain = DAG.getCopyToReg(Chain, DL, SP::O0, Argument, InFlag);
1851 InFlag = Chain.getValue(1);
1852 SDValue Callee = DAG.getTargetExternalSymbol("__tls_get_addr", PtrVT);
1853 SDValue Symbol = withTargetFlags(Op, callTF, DAG);
1854
1855 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1856 SmallVector<SDValue, 4> Ops;
1857 Ops.push_back(Chain);
1858 Ops.push_back(Callee);
1859 Ops.push_back(Symbol);
1860 Ops.push_back(DAG.getRegister(SP::O0, PtrVT));
1861 const uint32_t *Mask = getTargetMachine()
1862 .getRegisterInfo()->getCallPreservedMask(CallingConv::C);
1863 assert(Mask && "Missing call preserved mask for calling convention");
1864 Ops.push_back(DAG.getRegisterMask(Mask));
1865 Ops.push_back(InFlag);
1866 Chain = DAG.getNode(SPISD::TLS_CALL, DL, NodeTys, &Ops[0], Ops.size());
1867 InFlag = Chain.getValue(1);
1868 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(1, true),
1869 DAG.getIntPtrConstant(0, true), InFlag, DL);
1870 InFlag = Chain.getValue(1);
1871 SDValue Ret = DAG.getCopyFromReg(Chain, DL, SP::O0, PtrVT, InFlag);
1872
1873 if (model != TLSModel::LocalDynamic)
1874 return Ret;
1875
1876 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1877 withTargetFlags(Op, SPII::MO_TLS_LDO_HIX22, DAG));
1878 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1879 withTargetFlags(Op, SPII::MO_TLS_LDO_LOX10, DAG));
1880 HiLo = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1881 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT, Ret, HiLo,
1882 withTargetFlags(Op, SPII::MO_TLS_LDO_ADD, DAG));
1883 }
1884
1885 if (model == TLSModel::InitialExec) {
1886 unsigned ldTF = ((PtrVT == MVT::i64)? SPII::MO_TLS_IE_LDX
1887 : SPII::MO_TLS_IE_LD);
1888
1889 SDValue Base = DAG.getNode(SPISD::GLOBAL_BASE_REG, DL, PtrVT);
1890
1891 // GLOBAL_BASE_REG codegen'ed with call. Inform MFI that this
1892 // function has calls.
1893 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1894 MFI->setHasCalls(true);
1895
1896 SDValue TGA = makeHiLoPair(Op,
1897 SPII::MO_TLS_IE_HI22, SPII::MO_TLS_IE_LO10, DAG);
1898 SDValue Ptr = DAG.getNode(ISD::ADD, DL, PtrVT, Base, TGA);
1899 SDValue Offset = DAG.getNode(SPISD::TLS_LD,
1900 DL, PtrVT, Ptr,
1901 withTargetFlags(Op, ldTF, DAG));
1902 return DAG.getNode(SPISD::TLS_ADD, DL, PtrVT,
1903 DAG.getRegister(SP::G7, PtrVT), Offset,
1904 withTargetFlags(Op, SPII::MO_TLS_IE_ADD, DAG));
1905 }
1906
1907 assert(model == TLSModel::LocalExec);
1908 SDValue Hi = DAG.getNode(SPISD::Hi, DL, PtrVT,
1909 withTargetFlags(Op, SPII::MO_TLS_LE_HIX22, DAG));
1910 SDValue Lo = DAG.getNode(SPISD::Lo, DL, PtrVT,
1911 withTargetFlags(Op, SPII::MO_TLS_LE_LOX10, DAG));
1912 SDValue Offset = DAG.getNode(ISD::XOR, DL, PtrVT, Hi, Lo);
1913
1914 return DAG.getNode(ISD::ADD, DL, PtrVT,
1915 DAG.getRegister(SP::G7, PtrVT), Offset);
1916}
1917
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00001918SDValue
1919SparcTargetLowering::LowerF128_LibCallArg(SDValue Chain, ArgListTy &Args,
1920 SDValue Arg, SDLoc DL,
1921 SelectionDAG &DAG) const {
1922 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1923 EVT ArgVT = Arg.getValueType();
1924 Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
1925
1926 ArgListEntry Entry;
1927 Entry.Node = Arg;
1928 Entry.Ty = ArgTy;
1929
1930 if (ArgTy->isFP128Ty()) {
1931 // Create a stack object and pass the pointer to the library function.
1932 int FI = MFI->CreateStackObject(16, 8, false);
1933 SDValue FIPtr = DAG.getFrameIndex(FI, getPointerTy());
1934 Chain = DAG.getStore(Chain,
1935 DL,
1936 Entry.Node,
1937 FIPtr,
1938 MachinePointerInfo(),
1939 false,
1940 false,
1941 8);
1942
1943 Entry.Node = FIPtr;
1944 Entry.Ty = PointerType::getUnqual(ArgTy);
1945 }
1946 Args.push_back(Entry);
1947 return Chain;
1948}
1949
1950SDValue
1951SparcTargetLowering::LowerF128Op(SDValue Op, SelectionDAG &DAG,
1952 const char *LibFuncName,
1953 unsigned numArgs) const {
1954
1955 ArgListTy Args;
1956
1957 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
1958
1959 SDValue Callee = DAG.getExternalSymbol(LibFuncName, getPointerTy());
1960 Type *RetTy = Op.getValueType().getTypeForEVT(*DAG.getContext());
1961 Type *RetTyABI = RetTy;
1962 SDValue Chain = DAG.getEntryNode();
1963 SDValue RetPtr;
1964
1965 if (RetTy->isFP128Ty()) {
1966 // Create a Stack Object to receive the return value of type f128.
1967 ArgListEntry Entry;
1968 int RetFI = MFI->CreateStackObject(16, 8, false);
1969 RetPtr = DAG.getFrameIndex(RetFI, getPointerTy());
1970 Entry.Node = RetPtr;
1971 Entry.Ty = PointerType::getUnqual(RetTy);
1972 if (!Subtarget->is64Bit())
1973 Entry.isSRet = true;
1974 Entry.isReturned = false;
1975 Args.push_back(Entry);
1976 RetTyABI = Type::getVoidTy(*DAG.getContext());
1977 }
1978
1979 assert(Op->getNumOperands() >= numArgs && "Not enough operands!");
1980 for (unsigned i = 0, e = numArgs; i != e; ++i) {
1981 Chain = LowerF128_LibCallArg(Chain, Args, Op.getOperand(i), SDLoc(Op), DAG);
1982 }
1983 TargetLowering::
1984 CallLoweringInfo CLI(Chain,
1985 RetTyABI,
1986 false, false, false, false,
1987 0, CallingConv::C,
1988 false, false, true,
1989 Callee, Args, DAG, SDLoc(Op));
1990 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
1991
1992 // chain is in second result.
1993 if (RetTyABI == RetTy)
1994 return CallInfo.first;
1995
1996 assert (RetTy->isFP128Ty() && "Unexpected return type!");
1997
1998 Chain = CallInfo.second;
1999
2000 // Load RetPtr to get the return value.
2001 return DAG.getLoad(Op.getValueType(),
2002 SDLoc(Op),
2003 Chain,
2004 RetPtr,
2005 MachinePointerInfo(),
2006 false, false, false, 8);
2007}
2008
2009SDValue
2010SparcTargetLowering::LowerF128Compare(SDValue LHS, SDValue RHS,
2011 unsigned &SPCC,
2012 SDLoc DL,
2013 SelectionDAG &DAG) const {
2014
2015 const char *LibCall = 0;
2016 bool is64Bit = Subtarget->is64Bit();
2017 switch(SPCC) {
2018 default: llvm_unreachable("Unhandled conditional code!");
2019 case SPCC::FCC_E : LibCall = is64Bit? "_Qp_feq" : "_Q_feq"; break;
2020 case SPCC::FCC_NE : LibCall = is64Bit? "_Qp_fne" : "_Q_fne"; break;
2021 case SPCC::FCC_L : LibCall = is64Bit? "_Qp_flt" : "_Q_flt"; break;
2022 case SPCC::FCC_G : LibCall = is64Bit? "_Qp_fgt" : "_Q_fgt"; break;
2023 case SPCC::FCC_LE : LibCall = is64Bit? "_Qp_fle" : "_Q_fle"; break;
2024 case SPCC::FCC_GE : LibCall = is64Bit? "_Qp_fge" : "_Q_fge"; break;
2025 case SPCC::FCC_UL :
2026 case SPCC::FCC_ULE:
2027 case SPCC::FCC_UG :
2028 case SPCC::FCC_UGE:
2029 case SPCC::FCC_U :
2030 case SPCC::FCC_O :
2031 case SPCC::FCC_LG :
2032 case SPCC::FCC_UE : LibCall = is64Bit? "_Qp_cmp" : "_Q_cmp"; break;
2033 }
2034
2035 SDValue Callee = DAG.getExternalSymbol(LibCall, getPointerTy());
2036 Type *RetTy = Type::getInt32Ty(*DAG.getContext());
2037 ArgListTy Args;
2038 SDValue Chain = DAG.getEntryNode();
2039 Chain = LowerF128_LibCallArg(Chain, Args, LHS, DL, DAG);
2040 Chain = LowerF128_LibCallArg(Chain, Args, RHS, DL, DAG);
2041
2042 TargetLowering::
2043 CallLoweringInfo CLI(Chain,
2044 RetTy,
2045 false, false, false, false,
2046 0, CallingConv::C,
2047 false, false, true,
2048 Callee, Args, DAG, DL);
2049
2050 std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
2051
2052 // result is in first, and chain is in second result.
2053 SDValue Result = CallInfo.first;
2054
2055 switch(SPCC) {
2056 default: {
2057 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2058 SPCC = SPCC::ICC_NE;
2059 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2060 }
2061 case SPCC::FCC_UL : {
2062 SDValue Mask = DAG.getTargetConstant(1, Result.getValueType());
2063 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2064 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2065 SPCC = SPCC::ICC_NE;
2066 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2067 }
2068 case SPCC::FCC_ULE: {
Venkatraman Govindarajub803cec2013-09-04 15:15:20 +00002069 SDValue RHS = DAG.getTargetConstant(2, Result.getValueType());
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002070 SPCC = SPCC::ICC_NE;
2071 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2072 }
2073 case SPCC::FCC_UG : {
2074 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2075 SPCC = SPCC::ICC_G;
2076 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2077 }
2078 case SPCC::FCC_UGE: {
2079 SDValue RHS = DAG.getTargetConstant(1, Result.getValueType());
2080 SPCC = SPCC::ICC_NE;
2081 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2082 }
2083
2084 case SPCC::FCC_U : {
2085 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2086 SPCC = SPCC::ICC_E;
2087 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2088 }
2089 case SPCC::FCC_O : {
2090 SDValue RHS = DAG.getTargetConstant(3, Result.getValueType());
2091 SPCC = SPCC::ICC_NE;
2092 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2093 }
2094 case SPCC::FCC_LG : {
2095 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2096 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2097 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2098 SPCC = SPCC::ICC_NE;
2099 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2100 }
2101 case SPCC::FCC_UE : {
2102 SDValue Mask = DAG.getTargetConstant(3, Result.getValueType());
2103 Result = DAG.getNode(ISD::AND, DL, Result.getValueType(), Result, Mask);
2104 SDValue RHS = DAG.getTargetConstant(0, Result.getValueType());
2105 SPCC = SPCC::ICC_E;
2106 return DAG.getNode(SPISD::CMPICC, DL, MVT::Glue, Result, RHS);
2107 }
2108 }
2109}
2110
2111static SDValue
2112LowerF128_FPEXTEND(SDValue Op, SelectionDAG &DAG,
2113 const SparcTargetLowering &TLI) {
2114
2115 if (Op.getOperand(0).getValueType() == MVT::f64)
2116 return TLI.LowerF128Op(Op, DAG,
2117 TLI.getLibcallName(RTLIB::FPEXT_F64_F128), 1);
2118
2119 if (Op.getOperand(0).getValueType() == MVT::f32)
2120 return TLI.LowerF128Op(Op, DAG,
2121 TLI.getLibcallName(RTLIB::FPEXT_F32_F128), 1);
2122
2123 llvm_unreachable("fpextend with non-float operand!");
2124 return SDValue(0, 0);
2125}
2126
2127static SDValue
2128LowerF128_FPROUND(SDValue Op, SelectionDAG &DAG,
2129 const SparcTargetLowering &TLI) {
2130 // FP_ROUND on f64 and f32 are legal.
2131 if (Op.getOperand(0).getValueType() != MVT::f128)
2132 return Op;
2133
2134 if (Op.getValueType() == MVT::f64)
2135 return TLI.LowerF128Op(Op, DAG,
2136 TLI.getLibcallName(RTLIB::FPROUND_F128_F64), 1);
2137 if (Op.getValueType() == MVT::f32)
2138 return TLI.LowerF128Op(Op, DAG,
2139 TLI.getLibcallName(RTLIB::FPROUND_F128_F32), 1);
2140
2141 llvm_unreachable("fpround to non-float!");
2142 return SDValue(0, 0);
2143}
2144
2145static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG,
2146 const SparcTargetLowering &TLI,
2147 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002148 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002149 EVT VT = Op.getValueType();
2150 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002151
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002152 // Expand f128 operations to fp128 abi calls.
2153 if (Op.getOperand(0).getValueType() == MVT::f128
2154 && (!hasHardQuad || !TLI.isTypeLegal(VT))) {
2155 const char *libName = TLI.getLibcallName(VT == MVT::i32
2156 ? RTLIB::FPTOSINT_F128_I32
2157 : RTLIB::FPTOSINT_F128_I64);
2158 return TLI.LowerF128Op(Op, DAG, libName, 1);
2159 }
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002160
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002161 // Expand if the resulting type is illegal.
2162 if (!TLI.isTypeLegal(VT))
2163 return SDValue(0, 0);
2164
2165 // Otherwise, Convert the fp value to integer in an FP register.
2166 if (VT == MVT::i32)
2167 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
2168 else
2169 Op = DAG.getNode(SPISD::FTOX, dl, MVT::f64, Op.getOperand(0));
2170
2171 return DAG.getNode(ISD::BITCAST, dl, VT, Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002172}
2173
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002174static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2175 const SparcTargetLowering &TLI,
2176 bool hasHardQuad) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002177 SDLoc dl(Op);
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002178 EVT OpVT = Op.getOperand(0).getValueType();
2179 assert(OpVT == MVT::i32 || (OpVT == MVT::i64));
2180
2181 EVT floatVT = (OpVT == MVT::i32) ? MVT::f32 : MVT::f64;
2182
2183 // Expand f128 operations to fp128 ABI calls.
2184 if (Op.getValueType() == MVT::f128
2185 && (!hasHardQuad || !TLI.isTypeLegal(OpVT))) {
2186 const char *libName = TLI.getLibcallName(OpVT == MVT::i32
2187 ? RTLIB::SINTTOFP_I32_F128
2188 : RTLIB::SINTTOFP_I64_F128);
2189 return TLI.LowerF128Op(Op, DAG, libName, 1);
2190 }
2191
2192 // Expand if the operand type is illegal.
2193 if (!TLI.isTypeLegal(OpVT))
2194 return SDValue(0, 0);
2195
2196 // Otherwise, Convert the int value to FP in an FP register.
2197 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, floatVT, Op.getOperand(0));
2198 unsigned opcode = (OpVT == MVT::i32)? SPISD::ITOF : SPISD::XTOF;
2199 return DAG.getNode(opcode, dl, Op.getValueType(), Tmp);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002200}
2201
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002202static SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG,
2203 const SparcTargetLowering &TLI,
2204 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002205 SDLoc dl(Op);
2206 EVT VT = Op.getValueType();
2207
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002208 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002209 // quad floating point instructions and the resulting type is legal.
2210 if (Op.getOperand(0).getValueType() != MVT::f128 ||
2211 (hasHardQuad && TLI.isTypeLegal(VT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002212 return SDValue(0, 0);
2213
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002214 assert(VT == MVT::i32 || VT == MVT::i64);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002215
2216 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002217 TLI.getLibcallName(VT == MVT::i32
2218 ? RTLIB::FPTOUINT_F128_I32
2219 : RTLIB::FPTOUINT_F128_I64),
2220 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002221}
2222
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002223static SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG,
2224 const SparcTargetLowering &TLI,
2225 bool hasHardQuad) {
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002226 SDLoc dl(Op);
2227 EVT OpVT = Op.getOperand(0).getValueType();
2228 assert(OpVT == MVT::i32 || OpVT == MVT::i64);
2229
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002230 // Expand if it does not involve f128 or the target has support for
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002231 // quad floating point instructions and the operand type is legal.
2232 if (Op.getValueType() != MVT::f128 || (hasHardQuad && TLI.isTypeLegal(OpVT)))
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002233 return SDValue(0, 0);
2234
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002235 return TLI.LowerF128Op(Op, DAG,
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002236 TLI.getLibcallName(OpVT == MVT::i32
2237 ? RTLIB::UINTTOFP_I32_F128
2238 : RTLIB::UINTTOFP_I64_F128),
2239 1);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002240}
2241
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002242static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG,
2243 const SparcTargetLowering &TLI,
2244 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002245 SDValue Chain = Op.getOperand(0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002246 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002247 SDValue LHS = Op.getOperand(2);
2248 SDValue RHS = Op.getOperand(3);
2249 SDValue Dest = Op.getOperand(4);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002250 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002251 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002252
Chris Lattner0a1762e2008-03-17 03:21:36 +00002253 // If this is a br_cc of a "setcc", and if the setcc got lowered into
2254 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2255 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002256
Chris Lattner0a1762e2008-03-17 03:21:36 +00002257 // Get the condition flag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002258 SDValue CompareFlag;
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002259 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002260 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002261 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
Jakob Stoklund Olesend9bbdfd2013-04-03 04:41:44 +00002262 // 32-bit compares use the icc flags, 64-bit uses the xcc flags.
2263 Opc = LHS.getValueType() == MVT::i32 ? SPISD::BRICC : SPISD::BRXCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002264 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002265 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2266 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2267 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2268 Opc = SPISD::BRICC;
2269 } else {
2270 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2271 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2272 Opc = SPISD::BRFCC;
2273 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002274 }
Owen Anderson9f944592009-08-11 20:47:22 +00002275 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
2276 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002277}
2278
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002279static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG,
2280 const SparcTargetLowering &TLI,
2281 bool hasHardQuad) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002282 SDValue LHS = Op.getOperand(0);
2283 SDValue RHS = Op.getOperand(1);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002284 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002285 SDValue TrueVal = Op.getOperand(2);
2286 SDValue FalseVal = Op.getOperand(3);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002287 SDLoc dl(Op);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002288 unsigned Opc, SPCC = ~0U;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002289
Chris Lattner0a1762e2008-03-17 03:21:36 +00002290 // If this is a select_cc of a "setcc", and if the setcc got lowered into
2291 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
2292 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002293
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002294 SDValue CompareFlag;
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002295 if (LHS.getValueType().isInteger()) {
Venkatraman Govindarajudc82ac02013-06-07 00:03:36 +00002296 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, MVT::Glue, LHS, RHS);
Jakob Stoklund Olesen8cfaffa2013-04-04 03:08:00 +00002297 Opc = LHS.getValueType() == MVT::i32 ?
2298 SPISD::SELECT_ICC : SPISD::SELECT_XCC;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002299 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
2300 } else {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002301 if (!hasHardQuad && LHS.getValueType() == MVT::f128) {
2302 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2303 CompareFlag = TLI.LowerF128Compare(LHS, RHS, SPCC, dl, DAG);
2304 Opc = SPISD::SELECT_ICC;
2305 } else {
2306 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
2307 Opc = SPISD::SELECT_FCC;
2308 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
2309 }
Chris Lattner0a1762e2008-03-17 03:21:36 +00002310 }
Dale Johannesenf80493b2009-02-05 22:07:54 +00002311 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson9f944592009-08-11 20:47:22 +00002312 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002313}
2314
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002315static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohman21cea8a2010-04-17 15:26:15 +00002316 const SparcTargetLowering &TLI) {
Dan Gohman31ae5862010-04-17 14:41:14 +00002317 MachineFunction &MF = DAG.getMachineFunction();
2318 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
2319
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002320 // Need frame address to find the address of VarArgsFrameIndex.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002321 MF.getFrameInfo()->setFrameAddressIsTaken(true);
2322
Chris Lattner0a1762e2008-03-17 03:21:36 +00002323 // vastart just stores the address of the VarArgsFrameIndex slot into the
2324 // memory location argument.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002325 SDLoc DL(Op);
Dan Gohman31ae5862010-04-17 14:41:14 +00002326 SDValue Offset =
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002327 DAG.getNode(ISD::ADD, DL, TLI.getPointerTy(),
2328 DAG.getRegister(SP::I6, TLI.getPointerTy()),
2329 DAG.getIntPtrConstant(FuncInfo->getVarArgsFrameOffset()));
Chris Lattner0a1762e2008-03-17 03:21:36 +00002330 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002331 return DAG.getStore(Op.getOperand(0), DL, Offset, Op.getOperand(1),
Chris Lattner676c61d2010-09-21 18:41:36 +00002332 MachinePointerInfo(SV), false, false, 0);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002333}
2334
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002335static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00002336 SDNode *Node = Op.getNode();
Owen Anderson53aa7a92009-08-10 22:56:29 +00002337 EVT VT = Node->getValueType(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002338 SDValue InChain = Node->getOperand(0);
2339 SDValue VAListPtr = Node->getOperand(1);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002340 EVT PtrVT = VAListPtr.getValueType();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002341 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002342 SDLoc DL(Node);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002343 SDValue VAList = DAG.getLoad(PtrVT, DL, InChain, VAListPtr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002344 MachinePointerInfo(SV), false, false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002345 // Increment the pointer, VAList, to the next vaarg.
2346 SDValue NextPtr = DAG.getNode(ISD::ADD, DL, PtrVT, VAList,
2347 DAG.getIntPtrConstant(VT.getSizeInBits()/8));
2348 // Store the incremented VAList to the legalized pointer.
2349 InChain = DAG.getStore(VAList.getValue(1), DL, NextPtr,
Chris Lattner676c61d2010-09-21 18:41:36 +00002350 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Jakob Stoklund Olesena41f91e2013-04-20 22:49:16 +00002351 // Load the actual argument out of the pointer VAList.
2352 // We can't count on greater alignment than the word size.
2353 return DAG.getLoad(VT, DL, InChain, VAList, MachinePointerInfo(),
2354 false, false, false,
2355 std::min(PtrVT.getSizeInBits(), VT.getSizeInBits())/8);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002356}
2357
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002358static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002359 const SparcSubtarget *Subtarget) {
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002360 SDValue Chain = Op.getOperand(0); // Legalize the chain.
2361 SDValue Size = Op.getOperand(1); // Legalize the size.
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002362 EVT VT = Size->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00002363 SDLoc dl(Op);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002364
Chris Lattner0a1762e2008-03-17 03:21:36 +00002365 unsigned SPReg = SP::O6;
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002366 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
2367 SDValue NewSP = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002368 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002369
Chris Lattner0a1762e2008-03-17 03:21:36 +00002370 // The resultant pointer is actually 16 words from the bottom of the stack,
2371 // to provide a register spill area.
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002372 unsigned regSpillArea = Subtarget->is64Bit() ? 128 : 96;
2373 regSpillArea += Subtarget->getStackPointerBias();
2374
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002375 SDValue NewVal = DAG.getNode(ISD::ADD, dl, VT, NewSP,
2376 DAG.getConstant(regSpillArea, VT));
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002377 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesenf08a47b2009-02-04 23:02:30 +00002378 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002379}
2380
Chris Lattner0a1762e2008-03-17 03:21:36 +00002381
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002382static SDValue getFLUSHW(SDValue Op, SelectionDAG &DAG) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00002383 SDLoc dl(Op);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002384 SDValue Chain = DAG.getNode(SPISD::FLUSHW,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002385 dl, MVT::Other, DAG.getEntryNode());
2386 return Chain;
2387}
2388
2389static SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
2390 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
2391 MFI->setFrameAddressIsTaken(true);
2392
2393 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002394 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002395 unsigned FrameReg = SP::I6;
2396
2397 uint64_t depth = Op.getConstantOperandVal(0);
2398
2399 SDValue FrameAddr;
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002400 if (depth == 0)
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002401 FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
2402 else {
2403 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002404 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002405 FrameAddr = DAG.getCopyFromReg(Chain, dl, FrameReg, VT);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002406
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002407 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002408 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002409 dl, MVT::i32,
2410 FrameAddr, DAG.getIntPtrConstant(56));
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002411 FrameAddr = DAG.getLoad(MVT::i32, dl,
2412 Chain,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002413 Ptr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002414 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002415 }
2416 }
2417 return FrameAddr;
2418}
2419
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002420static SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG,
2421 const SparcTargetLowering &TLI) {
2422 MachineFunction &MF = DAG.getMachineFunction();
2423 MachineFrameInfo *MFI = MF.getFrameInfo();
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002424 MFI->setReturnAddressIsTaken(true);
2425
2426 EVT VT = Op.getValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002427 SDLoc dl(Op);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002428 uint64_t depth = Op.getConstantOperandVal(0);
2429
2430 SDValue RetAddr;
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002431 if (depth == 0) {
2432 unsigned RetReg = MF.addLiveIn(SP::I7,
2433 TLI.getRegClassFor(TLI.getPointerTy()));
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002434 RetAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, RetReg, VT);
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002435 } else {
Venkatraman Govindarajua54533ed2013-06-04 18:33:25 +00002436 // Need frame address to find return address of the caller.
Venkatraman Govindaraju28e2cd02013-06-01 20:42:48 +00002437 MFI->setFrameAddressIsTaken(true);
2438
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002439 // flush first to make sure the windowed registers' values are in stack
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002440 SDValue Chain = getFLUSHW(Op, DAG);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002441 RetAddr = DAG.getCopyFromReg(Chain, dl, SP::I6, VT);
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002442
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002443 for (uint64_t i = 0; i != depth; ++i) {
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002444 SDValue Ptr = DAG.getNode(ISD::ADD,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002445 dl, MVT::i32,
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002446 RetAddr,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002447 DAG.getIntPtrConstant((i == depth-1)?60:56));
Venkatraman Govindarajuef8cf452011-01-21 22:00:00 +00002448 RetAddr = DAG.getLoad(MVT::i32, dl,
2449 Chain,
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002450 Ptr,
Pete Cooper82cd9e82011-11-08 18:42:53 +00002451 MachinePointerInfo(), false, false, false, 0);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002452 }
2453 }
2454 return RetAddr;
2455}
2456
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002457static SDValue LowerF64Op(SDValue Op, SelectionDAG &DAG, unsigned opcode)
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002458{
2459 SDLoc dl(Op);
2460
2461 assert(Op.getValueType() == MVT::f64 && "LowerF64Op called on non-double!");
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002462 assert(opcode == ISD::FNEG || opcode == ISD::FABS);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002463
2464 // Lower fneg/fabs on f64 to fneg/fabs on f32.
2465 // fneg f64 => fneg f32:sub_even, fmov f32:sub_odd.
2466 // fabs f64 => fabs f32:sub_even, fmov f32:sub_odd.
2467
2468 SDValue SrcReg64 = Op.getOperand(0);
2469 SDValue Hi32 = DAG.getTargetExtractSubreg(SP::sub_even, dl, MVT::f32,
2470 SrcReg64);
2471 SDValue Lo32 = DAG.getTargetExtractSubreg(SP::sub_odd, dl, MVT::f32,
2472 SrcReg64);
2473
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002474 Hi32 = DAG.getNode(opcode, dl, MVT::f32, Hi32);
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002475
2476 SDValue DstReg64 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2477 dl, MVT::f64), 0);
2478 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_even, dl, MVT::f64,
2479 DstReg64, Hi32);
2480 DstReg64 = DAG.getTargetInsertSubreg(SP::sub_odd, dl, MVT::f64,
2481 DstReg64, Lo32);
2482 return DstReg64;
2483}
2484
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002485// Lower a f128 load into two f64 loads.
2486static SDValue LowerF128Load(SDValue Op, SelectionDAG &DAG)
2487{
2488 SDLoc dl(Op);
2489 LoadSDNode *LdNode = dyn_cast<LoadSDNode>(Op.getNode());
2490 assert(LdNode && LdNode->getOffset().getOpcode() == ISD::UNDEF
2491 && "Unexpected node type");
2492
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002493 unsigned alignment = LdNode->getAlignment();
2494 if (alignment > 8)
2495 alignment = 8;
2496
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002497 SDValue Hi64 = DAG.getLoad(MVT::f64,
2498 dl,
2499 LdNode->getChain(),
2500 LdNode->getBasePtr(),
2501 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002502 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002503 EVT addrVT = LdNode->getBasePtr().getValueType();
2504 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2505 LdNode->getBasePtr(),
2506 DAG.getConstant(8, addrVT));
2507 SDValue Lo64 = DAG.getLoad(MVT::f64,
2508 dl,
2509 LdNode->getChain(),
2510 LoPtr,
2511 LdNode->getPointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002512 false, false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002513
2514 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2515 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2516
2517 SDNode *InFP128 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2518 dl, MVT::f128);
2519 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2520 MVT::f128,
2521 SDValue(InFP128, 0),
2522 Hi64,
2523 SubRegEven);
2524 InFP128 = DAG.getMachineNode(TargetOpcode::INSERT_SUBREG, dl,
2525 MVT::f128,
2526 SDValue(InFP128, 0),
2527 Lo64,
2528 SubRegOdd);
2529 SDValue OutChains[2] = { SDValue(Hi64.getNode(), 1),
2530 SDValue(Lo64.getNode(), 1) };
2531 SDValue OutChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2532 &OutChains[0], 2);
2533 SDValue Ops[2] = {SDValue(InFP128,0), OutChain};
2534 return DAG.getMergeValues(Ops, 2, dl);
2535}
2536
2537// Lower a f128 store into two f64 stores.
2538static SDValue LowerF128Store(SDValue Op, SelectionDAG &DAG) {
2539 SDLoc dl(Op);
2540 StoreSDNode *StNode = dyn_cast<StoreSDNode>(Op.getNode());
2541 assert(StNode && StNode->getOffset().getOpcode() == ISD::UNDEF
2542 && "Unexpected node type");
2543 SDValue SubRegEven = DAG.getTargetConstant(SP::sub_even64, MVT::i32);
2544 SDValue SubRegOdd = DAG.getTargetConstant(SP::sub_odd64, MVT::i32);
2545
2546 SDNode *Hi64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2547 dl,
2548 MVT::f64,
2549 StNode->getValue(),
2550 SubRegEven);
2551 SDNode *Lo64 = DAG.getMachineNode(TargetOpcode::EXTRACT_SUBREG,
2552 dl,
2553 MVT::f64,
2554 StNode->getValue(),
2555 SubRegOdd);
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002556
2557 unsigned alignment = StNode->getAlignment();
2558 if (alignment > 8)
2559 alignment = 8;
2560
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002561 SDValue OutChains[2];
2562 OutChains[0] = DAG.getStore(StNode->getChain(),
2563 dl,
2564 SDValue(Hi64, 0),
2565 StNode->getBasePtr(),
2566 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002567 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002568 EVT addrVT = StNode->getBasePtr().getValueType();
2569 SDValue LoPtr = DAG.getNode(ISD::ADD, dl, addrVT,
2570 StNode->getBasePtr(),
2571 DAG.getConstant(8, addrVT));
2572 OutChains[1] = DAG.getStore(StNode->getChain(),
2573 dl,
2574 SDValue(Lo64, 0),
2575 LoPtr,
2576 MachinePointerInfo(),
Venkatraman Govindarajuece63db2013-10-05 02:29:47 +00002577 false, false, alignment);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002578 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
2579 &OutChains[0], 2);
2580}
2581
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002582static SDValue LowerFNEG(SDValue Op, SelectionDAG &DAG,
2583 const SparcTargetLowering &TLI,
2584 bool is64Bit) {
2585 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002586 return LowerF64Op(Op, DAG, ISD::FNEG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002587 if (Op.getValueType() == MVT::f128)
2588 return TLI.LowerF128Op(Op, DAG, ((is64Bit) ? "_Qp_neg" : "_Q_neg"), 1);
2589 return Op;
2590}
2591
2592static SDValue LowerFABS(SDValue Op, SelectionDAG &DAG, bool isV9) {
2593 if (Op.getValueType() == MVT::f64)
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002594 return LowerF64Op(Op, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002595 if (Op.getValueType() != MVT::f128)
2596 return Op;
2597
2598 // Lower fabs on f128 to fabs on f64
2599 // fabs f128 => fabs f64:sub_even64, fmov f64:sub_odd64
2600
2601 SDLoc dl(Op);
2602 SDValue SrcReg128 = Op.getOperand(0);
2603 SDValue Hi64 = DAG.getTargetExtractSubreg(SP::sub_even64, dl, MVT::f64,
2604 SrcReg128);
2605 SDValue Lo64 = DAG.getTargetExtractSubreg(SP::sub_odd64, dl, MVT::f64,
2606 SrcReg128);
2607 if (isV9)
2608 Hi64 = DAG.getNode(Op.getOpcode(), dl, MVT::f64, Hi64);
2609 else
Venkatraman Govindaraju829aec52013-09-21 23:51:08 +00002610 Hi64 = LowerF64Op(Hi64, DAG, ISD::FABS);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002611
2612 SDValue DstReg128 = SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF,
2613 dl, MVT::f128), 0);
2614 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_even64, dl, MVT::f128,
2615 DstReg128, Hi64);
2616 DstReg128 = DAG.getTargetInsertSubreg(SP::sub_odd64, dl, MVT::f128,
2617 DstReg128, Lo64);
2618 return DstReg128;
2619}
2620
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002621static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002622
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002623 if (Op.getValueType() != MVT::i64)
2624 return Op;
2625
2626 SDLoc dl(Op);
2627 SDValue Src1 = Op.getOperand(0);
2628 SDValue Src1Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1);
2629 SDValue Src1Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src1,
2630 DAG.getConstant(32, MVT::i64));
2631 Src1Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src1Hi);
2632
2633 SDValue Src2 = Op.getOperand(1);
2634 SDValue Src2Lo = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2);
2635 SDValue Src2Hi = DAG.getNode(ISD::SRL, dl, MVT::i64, Src2,
2636 DAG.getConstant(32, MVT::i64));
2637 Src2Hi = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src2Hi);
2638
2639
2640 bool hasChain = false;
2641 unsigned hiOpc = Op.getOpcode();
2642 switch (Op.getOpcode()) {
2643 default: llvm_unreachable("Invalid opcode");
2644 case ISD::ADDC: hiOpc = ISD::ADDE; break;
2645 case ISD::ADDE: hasChain = true; break;
2646 case ISD::SUBC: hiOpc = ISD::SUBE; break;
2647 case ISD::SUBE: hasChain = true; break;
2648 }
2649 SDValue Lo;
2650 SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Glue);
2651 if (hasChain) {
2652 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo,
2653 Op.getOperand(2));
2654 } else {
2655 Lo = DAG.getNode(Op.getOpcode(), dl, VTs, Src1Lo, Src2Lo);
2656 }
2657 SDValue Hi = DAG.getNode(hiOpc, dl, VTs, Src1Hi, Src2Hi, Lo.getValue(1));
2658 SDValue Carry = Hi.getValue(1);
2659
2660 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Lo);
2661 Hi = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Hi);
2662 Hi = DAG.getNode(ISD::SHL, dl, MVT::i64, Hi,
2663 DAG.getConstant(32, MVT::i64));
2664
2665 SDValue Dst = DAG.getNode(ISD::OR, dl, MVT::i64, Hi, Lo);
2666 SDValue Ops[2] = { Dst, Carry };
2667 return DAG.getMergeValues(Ops, 2, dl);
2668}
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002669
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00002670SDValue SparcTargetLowering::
Dan Gohman21cea8a2010-04-17 15:26:15 +00002671LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002672
2673 bool hasHardQuad = Subtarget->hasHardQuad();
2674 bool is64Bit = Subtarget->is64Bit();
2675 bool isV9 = Subtarget->isV9();
2676
Chris Lattner0a1762e2008-03-17 03:21:36 +00002677 switch (Op.getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002678 default: llvm_unreachable("Should not custom lower this!");
Venkatraman Govindaraju7dae9ce2013-06-08 15:32:59 +00002679
Venkatraman Govindarajufee76fa2013-07-30 19:53:10 +00002680 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG, *this);
Venkatraman Govindarajud9645802011-01-12 05:08:36 +00002681 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +00002682 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002683 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Venkatraman Govindarajuf80d72f2013-06-03 05:58:33 +00002684 case ISD::BlockAddress: return LowerBlockAddress(Op, DAG);
Chris Lattner840c7002009-09-15 17:46:24 +00002685 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002686 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG, *this,
2687 hasHardQuad);
2688 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG, *this,
2689 hasHardQuad);
Venkatraman Govindarajuf1d807e2013-11-03 08:00:19 +00002690 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG, *this,
2691 hasHardQuad);
2692 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG, *this,
2693 hasHardQuad);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002694 case ISD::BR_CC: return LowerBR_CC(Op, DAG, *this,
2695 hasHardQuad);
2696 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG, *this,
2697 hasHardQuad);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002698 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
2699 case ISD::VAARG: return LowerVAARG(Op, DAG);
Venkatraman Govindaraju0510db02013-11-24 17:41:41 +00002700 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG,
Venkatraman Govindaraju61116e72013-12-09 05:13:25 +00002701 Subtarget);
Venkatraman Govindaraju35e0c382013-08-25 18:30:06 +00002702
2703 case ISD::LOAD: return LowerF128Load(Op, DAG);
2704 case ISD::STORE: return LowerF128Store(Op, DAG);
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002705 case ISD::FADD: return LowerF128Op(Op, DAG,
2706 getLibcallName(RTLIB::ADD_F128), 2);
2707 case ISD::FSUB: return LowerF128Op(Op, DAG,
2708 getLibcallName(RTLIB::SUB_F128), 2);
2709 case ISD::FMUL: return LowerF128Op(Op, DAG,
2710 getLibcallName(RTLIB::MUL_F128), 2);
2711 case ISD::FDIV: return LowerF128Op(Op, DAG,
2712 getLibcallName(RTLIB::DIV_F128), 2);
2713 case ISD::FSQRT: return LowerF128Op(Op, DAG,
2714 getLibcallName(RTLIB::SQRT_F128),1);
2715 case ISD::FNEG: return LowerFNEG(Op, DAG, *this, is64Bit);
2716 case ISD::FABS: return LowerFABS(Op, DAG, isV9);
2717 case ISD::FP_EXTEND: return LowerF128_FPEXTEND(Op, DAG, *this);
2718 case ISD::FP_ROUND: return LowerF128_FPROUND(Op, DAG, *this);
Venkatraman Govindaraju572d5052013-10-06 03:36:18 +00002719 case ISD::ADDC:
2720 case ISD::ADDE:
2721 case ISD::SUBC:
2722 case ISD::SUBE: return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
Chris Lattner0a1762e2008-03-17 03:21:36 +00002723 }
2724}
2725
2726MachineBasicBlock *
2727SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman25c16532010-05-01 00:01:06 +00002728 MachineBasicBlock *BB) const {
Chris Lattner0a1762e2008-03-17 03:21:36 +00002729 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
2730 unsigned BROpcode;
2731 unsigned CC;
Dale Johannesen215a9252009-02-13 02:31:35 +00002732 DebugLoc dl = MI->getDebugLoc();
Chris Lattner0a1762e2008-03-17 03:21:36 +00002733 // Figure out the conditional branch opcode to use for this select_cc.
2734 switch (MI->getOpcode()) {
Torok Edwinfbcc6632009-07-14 16:55:14 +00002735 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattner0a1762e2008-03-17 03:21:36 +00002736 case SP::SELECT_CC_Int_ICC:
2737 case SP::SELECT_CC_FP_ICC:
2738 case SP::SELECT_CC_DFP_ICC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002739 case SP::SELECT_CC_QFP_ICC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00002740 BROpcode = SP::BCOND;
2741 break;
2742 case SP::SELECT_CC_Int_FCC:
2743 case SP::SELECT_CC_FP_FCC:
2744 case SP::SELECT_CC_DFP_FCC:
Venkatraman Govindaraju59039dc2013-09-03 04:11:59 +00002745 case SP::SELECT_CC_QFP_FCC:
Chris Lattner0a1762e2008-03-17 03:21:36 +00002746 BROpcode = SP::FBCOND;
2747 break;
2748 }
2749
2750 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002751
Chris Lattner0a1762e2008-03-17 03:21:36 +00002752 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
2753 // control-flow pattern. The incoming instruction knows the destination vreg
2754 // to set, the condition code register to branch on, the true/false values to
2755 // select between, and a branch opcode to use.
2756 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman3b460302008-07-07 23:14:23 +00002757 MachineFunction::iterator It = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002758 ++It;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002759
Chris Lattner0a1762e2008-03-17 03:21:36 +00002760 // thisMBB:
2761 // ...
2762 // TrueVal = ...
2763 // [f]bCC copy1MBB
2764 // fallthrough --> copy0MBB
2765 MachineBasicBlock *thisMBB = BB;
Chris Lattner0a1762e2008-03-17 03:21:36 +00002766 MachineFunction *F = BB->getParent();
Dan Gohman3b460302008-07-07 23:14:23 +00002767 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
2768 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindaraju2f155032010-12-28 20:39:17 +00002769 F->insert(It, copy0MBB);
2770 F->insert(It, sinkMBB);
Dan Gohman34396292010-07-06 20:24:04 +00002771
2772 // Transfer the remainder of BB and its successor edges to sinkMBB.
2773 sinkMBB->splice(sinkMBB->begin(), BB,
2774 llvm::next(MachineBasicBlock::iterator(MI)),
2775 BB->end());
2776 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
2777
2778 // Add the true and fallthrough blocks as its successors.
2779 BB->addSuccessor(copy0MBB);
2780 BB->addSuccessor(sinkMBB);
2781
Dale Johannesen215a9252009-02-13 02:31:35 +00002782 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002783
Chris Lattner0a1762e2008-03-17 03:21:36 +00002784 // copy0MBB:
2785 // %FalseValue = ...
2786 // # fallthrough to sinkMBB
2787 BB = copy0MBB;
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002788
Chris Lattner0a1762e2008-03-17 03:21:36 +00002789 // Update machine-CFG edges
2790 BB->addSuccessor(sinkMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002791
Chris Lattner0a1762e2008-03-17 03:21:36 +00002792 // sinkMBB:
2793 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
2794 // ...
2795 BB = sinkMBB;
Dan Gohman34396292010-07-06 20:24:04 +00002796 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattner0a1762e2008-03-17 03:21:36 +00002797 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
2798 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikovb8736562008-10-10 20:27:31 +00002799
Dan Gohman34396292010-07-06 20:24:04 +00002800 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattner0a1762e2008-03-17 03:21:36 +00002801 return BB;
2802}
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002803
2804//===----------------------------------------------------------------------===//
2805// Sparc Inline Assembly Support
2806//===----------------------------------------------------------------------===//
2807
2808/// getConstraintType - Given a constraint letter, return the type of
2809/// constraint it is for this target.
2810SparcTargetLowering::ConstraintType
2811SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
2812 if (Constraint.size() == 1) {
2813 switch (Constraint[0]) {
2814 default: break;
2815 case 'r': return C_RegisterClass;
2816 }
2817 }
2818
2819 return TargetLowering::getConstraintType(Constraint);
2820}
2821
2822std::pair<unsigned, const TargetRegisterClass*>
2823SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Chad Rosier295bd432013-06-22 18:37:38 +00002824 MVT VT) const {
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002825 if (Constraint.size() == 1) {
2826 switch (Constraint[0]) {
2827 case 'r':
Craig Topperabadc662012-04-20 06:31:50 +00002828 return std::make_pair(0U, &SP::IntRegsRegClass);
Anton Korobeynikov281cf242008-10-10 20:28:10 +00002829 }
2830 }
2831
2832 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
2833}
2834
Dan Gohman2fe6bee2008-10-18 02:06:02 +00002835bool
2836SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
2837 // The Sparc target isn't yet aware of offsets.
2838 return false;
2839}
Venkatraman Govindaraju5ae77f72013-11-03 12:28:40 +00002840
2841void SparcTargetLowering::ReplaceNodeResults(SDNode *N,
2842 SmallVectorImpl<SDValue>& Results,
2843 SelectionDAG &DAG) const {
2844
2845 SDLoc dl(N);
2846
2847 RTLIB::Libcall libCall = RTLIB::UNKNOWN_LIBCALL;
2848
2849 switch (N->getOpcode()) {
2850 default:
2851 llvm_unreachable("Do not know how to custom type legalize this operation!");
2852
2853 case ISD::FP_TO_SINT:
2854 case ISD::FP_TO_UINT:
2855 // Custom lower only if it involves f128 or i64.
2856 if (N->getOperand(0).getValueType() != MVT::f128
2857 || N->getValueType(0) != MVT::i64)
2858 return;
2859 libCall = ((N->getOpcode() == ISD::FP_TO_SINT)
2860 ? RTLIB::FPTOSINT_F128_I64
2861 : RTLIB::FPTOUINT_F128_I64);
2862
2863 Results.push_back(LowerF128Op(SDValue(N, 0),
2864 DAG,
2865 getLibcallName(libCall),
2866 1));
2867 return;
2868
2869 case ISD::SINT_TO_FP:
2870 case ISD::UINT_TO_FP:
2871 // Custom lower only if it involves f128 or i64.
2872 if (N->getValueType(0) != MVT::f128
2873 || N->getOperand(0).getValueType() != MVT::i64)
2874 return;
2875
2876 libCall = ((N->getOpcode() == ISD::SINT_TO_FP)
2877 ? RTLIB::SINTTOFP_I64_F128
2878 : RTLIB::UINTTOFP_I64_F128);
2879
2880 Results.push_back(LowerF128Op(SDValue(N, 0),
2881 DAG,
2882 getLibcallName(libCall),
2883 1));
2884 return;
2885 }
2886}