blob: e67002a7dbe3c9de80d214f7c48fe361339947cd [file] [log] [blame]
Chris Lattnerd23405e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcISelLowering.h"
16#include "SparcTargetMachine.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000017#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000018#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000024#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000025#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000026#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000027using namespace llvm;
28
Chris Lattner5a65b922008-03-17 05:41:48 +000029
30//===----------------------------------------------------------------------===//
31// Calling Convention Implementation
32//===----------------------------------------------------------------------===//
33
34#include "SparcGenCallingConv.inc"
35
Dan Gohman98ca4f22009-08-05 01:29:28 +000036SDValue
37SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000038 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000039 const SmallVectorImpl<ISD::OutputArg> &Outs,
40 DebugLoc dl, SelectionDAG &DAG) {
41
Chris Lattner5a65b922008-03-17 05:41:48 +000042 // CCValAssign - represent the assignment of the return value to locations.
43 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000044
Chris Lattner5a65b922008-03-17 05:41:48 +000045 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000046 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
47 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000048
Dan Gohman98ca4f22009-08-05 01:29:28 +000049 // Analize return values.
50 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000051
Chris Lattner5a65b922008-03-17 05:41:48 +000052 // If this is the first return lowered for this function, add the regs to the
53 // liveout set for the function.
54 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
55 for (unsigned i = 0; i != RVLocs.size(); ++i)
56 if (RVLocs[i].isRegLoc())
57 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
58 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000059
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000061
62 // Copy the result values into the output registers.
63 for (unsigned i = 0; i != RVLocs.size(); ++i) {
64 CCValAssign &VA = RVLocs[i];
65 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000066
Dale Johannesena05dca42009-02-04 23:02:30 +000067 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +000068 Outs[i].Val, Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000069
Chris Lattner5a65b922008-03-17 05:41:48 +000070 // Guarantee that all emitted copies are stuck together with flags.
71 Flag = Chain.getValue(1);
72 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000073
Gabor Greifba36cb52008-08-28 21:40:38 +000074 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +000075 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
76 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000077}
78
Dan Gohman98ca4f22009-08-05 01:29:28 +000079/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
80/// passed in either one or two GPRs, including FP values. TODO: we should
81/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000082SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000083SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000084 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000085 const SmallVectorImpl<ISD::InputArg>
86 &Ins,
87 DebugLoc dl, SelectionDAG &DAG,
88 SmallVectorImpl<SDValue> &InVals) {
89
Chris Lattner5a65b922008-03-17 05:41:48 +000090 MachineFunction &MF = DAG.getMachineFunction();
91 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Eli Friedmana786c7b2009-07-19 19:53:46 +000092
93 // Assign locations to all of the incoming arguments.
94 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000095 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
96 ArgLocs, *DAG.getContext());
97 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000098
Chris Lattner5a65b922008-03-17 05:41:48 +000099 static const unsigned ArgRegs[] = {
100 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
101 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000102 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
103 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000104
Eli Friedmana786c7b2009-07-19 19:53:46 +0000105 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
106 SDValue ArgValue;
107 CCValAssign &VA = ArgLocs[i];
108 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
109 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000110 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000112 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000113 case MVT::i1:
114 case MVT::i8:
115 case MVT::i16:
116 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000117 if (!Ins[i].Used) { // Argument is dead.
118 if (CurArgReg < ArgRegEnd) ++CurArgReg;
119 InVals.push_back(DAG.getUNDEF(ObjectVT));
120 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000121 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
122 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
124 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000125 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000126 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000127 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000128 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000129 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000130 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000131 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000132 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
133 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000134 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000135 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000136 if (ObjectVT == MVT::i32) {
137 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000138 } else {
139 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
140
141 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000142 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000143 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
144 DAG.getConstant(Offset, MVT::i32));
145 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Chain, FIPtr,
Chris Lattner5a65b922008-03-17 05:41:48 +0000146 NULL, 0, ObjectVT);
Dale Johannesen39355f92009-02-04 02:34:38 +0000147 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000148 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000149 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000150 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000151
Chris Lattner5a65b922008-03-17 05:41:48 +0000152 ArgOffset += 4;
153 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000154 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155 if (!Ins[i].Used) { // Argument is dead.
156 if (CurArgReg < ArgRegEnd) ++CurArgReg;
157 InVals.push_back(DAG.getUNDEF(ObjectVT));
158 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000159 // FP value is passed in an integer register.
160 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
161 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000163
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000165 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000166 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000167 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
168 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
170 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr, NULL, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000171 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000172 }
173 ArgOffset += 4;
174 break;
175
Owen Anderson825b72b2009-08-11 20:47:22 +0000176 case MVT::i64:
177 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000178 if (!Ins[i].Used) { // Argument is dead.
179 if (CurArgReg < ArgRegEnd) ++CurArgReg;
180 if (CurArgReg < ArgRegEnd) ++CurArgReg;
181 InVals.push_back(DAG.getUNDEF(ObjectVT));
182 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000183 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000184 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
185 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
186 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000188 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000189 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
190 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
192 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000193 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000194
Dan Gohman475871a2008-07-27 21:46:04 +0000195 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000196 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
197 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
198 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000200 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000201 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
202 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000203 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
204 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000206
Chris Lattner5a65b922008-03-17 05:41:48 +0000207 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000208 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000209 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000210
Chris Lattner5a65b922008-03-17 05:41:48 +0000211 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 if (ObjectVT == MVT::f64)
213 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000214
Dan Gohman98ca4f22009-08-05 01:29:28 +0000215 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000216 }
217 ArgOffset += 8;
218 break;
219 }
220 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000221
Chris Lattner5a65b922008-03-17 05:41:48 +0000222 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000223 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000224 // Remember the vararg offset for the va_start implementation.
225 VarArgsFrameOffset = ArgOffset;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000226
Eli Friedmana786c7b2009-07-19 19:53:46 +0000227 std::vector<SDValue> OutChains;
228
Chris Lattner5a65b922008-03-17 05:41:48 +0000229 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
230 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
231 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000232 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000233
David Greene3f2bf852009-11-12 20:49:22 +0000234 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
235 true, false);
Owen Anderson825b72b2009-08-11 20:47:22 +0000236 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000237
Dale Johannesen39355f92009-02-04 02:34:38 +0000238 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000239 ArgOffset += 4;
240 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000241
242 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000243 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000245 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000246 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000247 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000248
Dan Gohman98ca4f22009-08-05 01:29:28 +0000249 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000250}
251
Dan Gohman98ca4f22009-08-05 01:29:28 +0000252SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000253SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000254 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000255 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000256 const SmallVectorImpl<ISD::OutputArg> &Outs,
257 const SmallVectorImpl<ISD::InputArg> &Ins,
258 DebugLoc dl, SelectionDAG &DAG,
259 SmallVectorImpl<SDValue> &InVals) {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000260 // Sparc target does not yet support tail call optimization.
261 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000262
Chris Lattner315123f2008-03-17 06:58:37 +0000263#if 0
264 // Analyze operands of the call, assigning locations to each operand.
265 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000266 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
267 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000268
Chris Lattner315123f2008-03-17 06:58:37 +0000269 // Get the size of the outgoing arguments stack space requirement.
270 unsigned ArgsSize = CCInfo.getNextStackOffset();
271 // FIXME: We can't use this until f64 is known to take two GPRs.
272#else
273 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000274
Chris Lattner5a65b922008-03-17 05:41:48 +0000275 // Count the size of the outgoing arguments.
276 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000277 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000279 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000280 case MVT::i1:
281 case MVT::i8:
282 case MVT::i16:
283 case MVT::i32:
284 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000285 ArgsSize += 4;
286 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000287 case MVT::i64:
288 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000289 ArgsSize += 8;
290 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000291 }
292 }
293 if (ArgsSize > 4*6)
294 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
295 else
296 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000297#endif
298
Chris Lattner5a65b922008-03-17 05:41:48 +0000299 // Keep stack frames 8-byte aligned.
300 ArgsSize = (ArgsSize+7) & ~7;
301
Chris Lattnere563bbc2008-10-11 22:08:30 +0000302 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000303
Dan Gohman475871a2008-07-27 21:46:04 +0000304 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
305 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000306
Chris Lattner315123f2008-03-17 06:58:37 +0000307#if 0
308 // Walk the register/memloc assignments, inserting copies/loads.
309 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
310 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000311 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000312
313 // Promote the value if needed.
314 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000315 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000316 case CCValAssign::Full: break;
317 case CCValAssign::SExt:
318 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
319 break;
320 case CCValAssign::ZExt:
321 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
322 break;
323 case CCValAssign::AExt:
324 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
325 break;
326 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000327
328 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000329 // RegsToPass vector
330 if (VA.isRegLoc()) {
331 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
332 continue;
333 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000334
Chris Lattner315123f2008-03-17 06:58:37 +0000335 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000336
Chris Lattner315123f2008-03-17 06:58:37 +0000337 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000338 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000339 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000340 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000341 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner315123f2008-03-17 06:58:37 +0000342 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
343 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000344
345#else
Chris Lattner315123f2008-03-17 06:58:37 +0000346 static const unsigned ArgRegs[] = {
347 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
348 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000349 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000350
Dan Gohman98ca4f22009-08-05 01:29:28 +0000351 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
352 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000353 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000354 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000355 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000357 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000358 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000359 ObjSize = 4;
360
Chris Lattner315123f2008-03-17 06:58:37 +0000361 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000362 ValToStore = Val;
363 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000364 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000365 }
366 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000367 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000369 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000370 ValToStore = Val;
371 } else {
372 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000374 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000375 }
376 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000377 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000378 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000379 if (RegsToPass.size() >= 6) {
380 ValToStore = Val; // Whole thing is passed in memory.
381 break;
382 }
383
384 // Break into top and bottom parts by storing to the stack and loading
385 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000386 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000387 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
388 Val, StackPtr, NULL, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000389 // Sparc is big-endian, so the high part comes first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000390 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000391 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000392 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000393 DAG.getIntPtrConstant(4));
394 // Load the low part.
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000396
397 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
398
399 if (RegsToPass.size() >= 6) {
400 ValToStore = Lo;
401 ArgOffset += 4;
402 ObjSize = 4;
403 } else {
404 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
405 }
406 break;
407 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000408 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000409 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000410 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000411 ValToStore = Val; // Whole thing is passed in memory.
412 break;
413 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000414
Chris Lattner5a65b922008-03-17 05:41:48 +0000415 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000416 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
417 DAG.getConstant(1, MVT::i32));
418 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
419 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000420 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000421
Chris Lattner315123f2008-03-17 06:58:37 +0000422 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000423 ValToStore = Lo;
424 ArgOffset += 4;
425 ObjSize = 4;
426 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000427 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000428 }
429 break;
430 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000431 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000432
Gabor Greifba36cb52008-08-28 21:40:38 +0000433 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000434 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
435 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
436 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000437 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
438 PtrOff, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000439 }
440 ArgOffset += ObjSize;
441 }
Chris Lattner315123f2008-03-17 06:58:37 +0000442#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000443
Chris Lattner5a65b922008-03-17 05:41:48 +0000444 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000445 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000446 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000447 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000448
449 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000450 // chain and flag operands which copy the outgoing args into registers.
451 // The InFlag in necessary since all emited instructions must be
452 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000453 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000454 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
455 unsigned Reg = RegsToPass[i].first;
456 // Remap I0->I7 -> O0->O7.
457 if (Reg >= SP::I0 && Reg <= SP::I7)
458 Reg = Reg-SP::I0+SP::O0;
459
Dale Johannesen33c960f2009-02-04 20:06:27 +0000460 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000461 InFlag = Chain.getValue(1);
462 }
463
464 // If the callee is a GlobalAddress node (quite common, every direct call is)
465 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000466 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000467 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000469 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000470 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000471
Owen Andersone50ed302009-08-10 22:56:29 +0000472 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 NodeTys.push_back(MVT::Other); // Returns a chain
474 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000475 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000476 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000477 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000478
Chris Lattnere563bbc2008-10-11 22:08:30 +0000479 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
480 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000481 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000482
Chris Lattner98949a62008-03-17 06:01:07 +0000483 // Assign locations to each value returned by this call.
484 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000485 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000486 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000487
Dan Gohman98ca4f22009-08-05 01:29:28 +0000488 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000489
Chris Lattner98949a62008-03-17 06:01:07 +0000490 // Copy all of the result registers out of their specified physreg.
491 for (unsigned i = 0; i != RVLocs.size(); ++i) {
492 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000493
Chris Lattner98949a62008-03-17 06:01:07 +0000494 // Remap I0->I7 -> O0->O7.
495 if (Reg >= SP::I0 && Reg <= SP::I7)
496 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000497
Dale Johannesen33c960f2009-02-04 20:06:27 +0000498 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000499 RVLocs[i].getValVT(), InFlag).getValue(1);
500 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000501 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000502 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000503
Dan Gohman98ca4f22009-08-05 01:29:28 +0000504 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000505}
506
507
508
Chris Lattnerd23405e2008-03-17 03:21:36 +0000509//===----------------------------------------------------------------------===//
510// TargetLowering Implementation
511//===----------------------------------------------------------------------===//
512
513/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
514/// condition.
515static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
516 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000517 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000518 case ISD::SETEQ: return SPCC::ICC_E;
519 case ISD::SETNE: return SPCC::ICC_NE;
520 case ISD::SETLT: return SPCC::ICC_L;
521 case ISD::SETGT: return SPCC::ICC_G;
522 case ISD::SETLE: return SPCC::ICC_LE;
523 case ISD::SETGE: return SPCC::ICC_GE;
524 case ISD::SETULT: return SPCC::ICC_CS;
525 case ISD::SETULE: return SPCC::ICC_LEU;
526 case ISD::SETUGT: return SPCC::ICC_GU;
527 case ISD::SETUGE: return SPCC::ICC_CC;
528 }
529}
530
531/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
532/// FCC condition.
533static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
534 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000535 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000536 case ISD::SETEQ:
537 case ISD::SETOEQ: return SPCC::FCC_E;
538 case ISD::SETNE:
539 case ISD::SETUNE: return SPCC::FCC_NE;
540 case ISD::SETLT:
541 case ISD::SETOLT: return SPCC::FCC_L;
542 case ISD::SETGT:
543 case ISD::SETOGT: return SPCC::FCC_G;
544 case ISD::SETLE:
545 case ISD::SETOLE: return SPCC::FCC_LE;
546 case ISD::SETGE:
547 case ISD::SETOGE: return SPCC::FCC_GE;
548 case ISD::SETULT: return SPCC::FCC_UL;
549 case ISD::SETULE: return SPCC::FCC_ULE;
550 case ISD::SETUGT: return SPCC::FCC_UG;
551 case ISD::SETUGE: return SPCC::FCC_UGE;
552 case ISD::SETUO: return SPCC::FCC_U;
553 case ISD::SETO: return SPCC::FCC_O;
554 case ISD::SETONE: return SPCC::FCC_LG;
555 case ISD::SETUEQ: return SPCC::FCC_UE;
556 }
557}
558
Chris Lattnerd23405e2008-03-17 03:21:36 +0000559SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000560 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000561
Chris Lattnerd23405e2008-03-17 03:21:36 +0000562 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000563 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
564 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
565 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000566
567 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000569 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000571 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000572 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000573
574 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000575 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
576 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
577 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000578
Chris Lattnerd23405e2008-03-17 03:21:36 +0000579 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000580 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
581 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
582 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000583
584 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000585 setOperationAction(ISD::UREM, MVT::i32, Expand);
586 setOperationAction(ISD::SREM, MVT::i32, Expand);
587 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
588 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000589
590 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000591 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
592 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000593
594 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000595 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
596 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000597
Owen Anderson825b72b2009-08-11 20:47:22 +0000598 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
599 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000600
Chris Lattnerd23405e2008-03-17 03:21:36 +0000601 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000602 setOperationAction(ISD::SELECT, MVT::i32, Expand);
603 setOperationAction(ISD::SELECT, MVT::f32, Expand);
604 setOperationAction(ISD::SELECT, MVT::f64, Expand);
605 setOperationAction(ISD::SETCC, MVT::i32, Expand);
606 setOperationAction(ISD::SETCC, MVT::f32, Expand);
607 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000608
Chris Lattnerd23405e2008-03-17 03:21:36 +0000609 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000610 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
611 setOperationAction(ISD::BRIND, MVT::Other, Expand);
612 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
613 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
614 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
615 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000616
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
618 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
619 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000620
Chris Lattnerd23405e2008-03-17 03:21:36 +0000621 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000623
Owen Anderson825b72b2009-08-11 20:47:22 +0000624 setOperationAction(ISD::FSIN , MVT::f64, Expand);
625 setOperationAction(ISD::FCOS , MVT::f64, Expand);
626 setOperationAction(ISD::FREM , MVT::f64, Expand);
627 setOperationAction(ISD::FSIN , MVT::f32, Expand);
628 setOperationAction(ISD::FCOS , MVT::f32, Expand);
629 setOperationAction(ISD::FREM , MVT::f32, Expand);
630 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
631 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
632 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
633 setOperationAction(ISD::ROTL , MVT::i32, Expand);
634 setOperationAction(ISD::ROTR , MVT::i32, Expand);
635 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
636 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
637 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
638 setOperationAction(ISD::FPOW , MVT::f64, Expand);
639 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000640
Owen Anderson825b72b2009-08-11 20:47:22 +0000641 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
642 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
643 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000644
645 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
647 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000650
Chris Lattnerd23405e2008-03-17 03:21:36 +0000651 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000653 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000654 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000655
Chris Lattnerd23405e2008-03-17 03:21:36 +0000656 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000657 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
658 setOperationAction(ISD::VAEND , MVT::Other, Expand);
659 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
660 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
661 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000662
663 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000664 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 setStackPointerRegisterToSaveRestore(SP::O6);
667
668 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000670
Chris Lattnerd23405e2008-03-17 03:21:36 +0000671 computeRegisterProperties();
672}
673
674const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
675 switch (Opcode) {
676 default: return 0;
677 case SPISD::CMPICC: return "SPISD::CMPICC";
678 case SPISD::CMPFCC: return "SPISD::CMPFCC";
679 case SPISD::BRICC: return "SPISD::BRICC";
680 case SPISD::BRFCC: return "SPISD::BRFCC";
681 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
682 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
683 case SPISD::Hi: return "SPISD::Hi";
684 case SPISD::Lo: return "SPISD::Lo";
685 case SPISD::FTOI: return "SPISD::FTOI";
686 case SPISD::ITOF: return "SPISD::ITOF";
687 case SPISD::CALL: return "SPISD::CALL";
688 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
689 }
690}
691
692/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
693/// be zero. Op is expected to be a target specific node. Used by DAG
694/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000695void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000696 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000697 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000698 APInt &KnownOne,
699 const SelectionDAG &DAG,
700 unsigned Depth) const {
701 APInt KnownZero2, KnownOne2;
702 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000703
Chris Lattnerd23405e2008-03-17 03:21:36 +0000704 switch (Op.getOpcode()) {
705 default: break;
706 case SPISD::SELECT_ICC:
707 case SPISD::SELECT_FCC:
708 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
709 Depth+1);
710 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
711 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000712 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
713 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
714
Chris Lattnerd23405e2008-03-17 03:21:36 +0000715 // Only known if known in both the LHS and RHS.
716 KnownOne &= KnownOne2;
717 KnownZero &= KnownZero2;
718 break;
719 }
720}
721
Chris Lattnerd23405e2008-03-17 03:21:36 +0000722// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
723// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000724static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000725 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000726 if (isa<ConstantSDNode>(RHS) &&
727 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000728 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000729 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
730 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
731 (LHS.getOpcode() == SPISD::SELECT_FCC &&
732 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
733 isa<ConstantSDNode>(LHS.getOperand(0)) &&
734 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000735 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
736 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000737 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000738 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000739 LHS = CMPCC.getOperand(0);
740 RHS = CMPCC.getOperand(1);
741 }
742}
743
Chris Lattnerdb486a62009-09-15 17:46:24 +0000744SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
745 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000746 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000747 // FIXME there isn't really any debug info here
748 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000749 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
750 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
751 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000752
753 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
754 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
755
756 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
757 getPointerTy());
758 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
759 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
760 GlobalBase, RelAddr);
761 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
762 AbsAddr, NULL, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000763}
764
Chris Lattnerdb486a62009-09-15 17:46:24 +0000765SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
766 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000767 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000768 // FIXME there isn't really any debug info here
769 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000770 Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000771 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
772 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
773 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000774 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
775 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
776
777 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
778 getPointerTy());
779 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
780 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
781 GlobalBase, RelAddr);
782 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
783 AbsAddr, NULL, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000784}
785
Dan Gohman475871a2008-07-27 21:46:04 +0000786static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000787 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000788 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000789 assert(Op.getValueType() == MVT::i32);
790 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
791 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000792}
793
Dan Gohman475871a2008-07-27 21:46:04 +0000794static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000795 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000796 assert(Op.getOperand(0).getValueType() == MVT::i32);
797 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000798 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000799 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000800}
801
Dan Gohman475871a2008-07-27 21:46:04 +0000802static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
803 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000804 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000805 SDValue LHS = Op.getOperand(2);
806 SDValue RHS = Op.getOperand(3);
807 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000808 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000809 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000810
Chris Lattnerd23405e2008-03-17 03:21:36 +0000811 // If this is a br_cc of a "setcc", and if the setcc got lowered into
812 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
813 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000814
Chris Lattnerd23405e2008-03-17 03:21:36 +0000815 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000816 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000818 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000819 VTs.push_back(MVT::i32);
820 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000821 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000822 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000823 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
824 Opc = SPISD::BRICC;
825 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000826 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000827 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
828 Opc = SPISD::BRFCC;
829 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000830 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
831 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000832}
833
Dan Gohman475871a2008-07-27 21:46:04 +0000834static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
835 SDValue LHS = Op.getOperand(0);
836 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000838 SDValue TrueVal = Op.getOperand(2);
839 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000840 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000841 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000842
Chris Lattnerd23405e2008-03-17 03:21:36 +0000843 // If this is a select_cc of a "setcc", and if the setcc got lowered into
844 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
845 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000846
Dan Gohman475871a2008-07-27 21:46:04 +0000847 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000848 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000849 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000850 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000851 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000852 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000853 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000854 Opc = SPISD::SELECT_ICC;
855 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
856 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000858 Opc = SPISD::SELECT_FCC;
859 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
860 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000861 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000863}
864
Dan Gohman475871a2008-07-27 21:46:04 +0000865static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000866 SparcTargetLowering &TLI) {
867 // vastart just stores the address of the VarArgsFrameIndex slot into the
868 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000869 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000870 SDValue Offset = DAG.getNode(ISD::ADD, dl, MVT::i32,
871 DAG.getRegister(SP::I6, MVT::i32),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000872 DAG.getConstant(TLI.getVarArgsFrameOffset(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000873 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000874 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000875 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000876}
877
Dan Gohman475871a2008-07-27 21:46:04 +0000878static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000879 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000880 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue InChain = Node->getOperand(0);
882 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000883 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000884 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000886 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000888 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000889 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000890 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000891 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000892 VAListPtr, SV, 0);
893 // Load the actual argument out of the pointer VAList, unless this is an
894 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000895 if (VT != MVT::f64)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000896 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000897
Chris Lattnerd23405e2008-03-17 03:21:36 +0000898 // Otherwise, load it as i64, then do a bitconvert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000899 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000900
Chris Lattnerd23405e2008-03-17 03:21:36 +0000901 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000902 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000903 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000904 V.getValue(1)
905 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000906 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000907}
908
Dan Gohman475871a2008-07-27 21:46:04 +0000909static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
910 SDValue Chain = Op.getOperand(0); // Legalize the chain.
911 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000912 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000913
Chris Lattnerd23405e2008-03-17 03:21:36 +0000914 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000915 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
916 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000917 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000918
Chris Lattnerd23405e2008-03-17 03:21:36 +0000919 // The resultant pointer is actually 16 words from the bottom of the stack,
920 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000921 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
922 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000923 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000924 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000925}
926
Chris Lattnerd23405e2008-03-17 03:21:36 +0000927
Dan Gohman475871a2008-07-27 21:46:04 +0000928SDValue SparcTargetLowering::
929LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000930 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000931 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000932 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000933 case ISD::RETURNADDR: return SDValue();
934 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000935 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000936 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000937 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
938 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000939 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
940 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
941 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
942 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
943 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
944 case ISD::VAARG: return LowerVAARG(Op, DAG);
945 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000946 }
947}
948
949MachineBasicBlock *
950SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000951 MachineBasicBlock *BB,
952 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000953 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
954 unsigned BROpcode;
955 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000956 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000957 // Figure out the conditional branch opcode to use for this select_cc.
958 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000959 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000960 case SP::SELECT_CC_Int_ICC:
961 case SP::SELECT_CC_FP_ICC:
962 case SP::SELECT_CC_DFP_ICC:
963 BROpcode = SP::BCOND;
964 break;
965 case SP::SELECT_CC_Int_FCC:
966 case SP::SELECT_CC_FP_FCC:
967 case SP::SELECT_CC_DFP_FCC:
968 BROpcode = SP::FBCOND;
969 break;
970 }
971
972 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000973
Chris Lattnerd23405e2008-03-17 03:21:36 +0000974 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
975 // control-flow pattern. The incoming instruction knows the destination vreg
976 // to set, the condition code register to branch on, the true/false values to
977 // select between, and a branch opcode to use.
978 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000979 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000980 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000981
Chris Lattnerd23405e2008-03-17 03:21:36 +0000982 // thisMBB:
983 // ...
984 // TrueVal = ...
985 // [f]bCC copy1MBB
986 // fallthrough --> copy0MBB
987 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000988 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000989 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
990 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesend552eee2009-02-13 02:31:35 +0000991 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000992 F->insert(It, copy0MBB);
993 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +0000994 // Update machine-CFG edges by first adding all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +0000995 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +0000996 // Also inform sdisel of the edge changes.
997 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
998 E = BB->succ_end(); I != E; ++I) {
999 EM->insert(std::make_pair(*I, sinkMBB));
1000 sinkMBB->addSuccessor(*I);
1001 }
1002 // Next, remove all successors of the current block, and add the true
1003 // and fallthrough blocks as its successors.
1004 while (!BB->succ_empty())
1005 BB->removeSuccessor(BB->succ_begin());
Dan Gohman0011dc42008-06-21 20:21:19 +00001006 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001007 BB->addSuccessor(copy0MBB);
1008 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001009
Chris Lattnerd23405e2008-03-17 03:21:36 +00001010 // copy0MBB:
1011 // %FalseValue = ...
1012 // # fallthrough to sinkMBB
1013 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001014
Chris Lattnerd23405e2008-03-17 03:21:36 +00001015 // Update machine-CFG edges
1016 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001017
Chris Lattnerd23405e2008-03-17 03:21:36 +00001018 // sinkMBB:
1019 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1020 // ...
1021 BB = sinkMBB;
Dale Johannesend552eee2009-02-13 02:31:35 +00001022 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001023 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1024 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001025
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001026 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001027 return BB;
1028}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001029
1030//===----------------------------------------------------------------------===//
1031// Sparc Inline Assembly Support
1032//===----------------------------------------------------------------------===//
1033
1034/// getConstraintType - Given a constraint letter, return the type of
1035/// constraint it is for this target.
1036SparcTargetLowering::ConstraintType
1037SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1038 if (Constraint.size() == 1) {
1039 switch (Constraint[0]) {
1040 default: break;
1041 case 'r': return C_RegisterClass;
1042 }
1043 }
1044
1045 return TargetLowering::getConstraintType(Constraint);
1046}
1047
1048std::pair<unsigned, const TargetRegisterClass*>
1049SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001050 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001051 if (Constraint.size() == 1) {
1052 switch (Constraint[0]) {
1053 case 'r':
1054 return std::make_pair(0U, SP::IntRegsRegisterClass);
1055 }
1056 }
1057
1058 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1059}
1060
1061std::vector<unsigned> SparcTargetLowering::
1062getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001063 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001064 if (Constraint.size() != 1)
1065 return std::vector<unsigned>();
1066
1067 switch (Constraint[0]) {
1068 default: break;
1069 case 'r':
1070 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1071 SP::L4, SP::L5, SP::L6, SP::L7,
1072 SP::I0, SP::I1, SP::I2, SP::I3,
1073 SP::I4, SP::I5,
1074 SP::O0, SP::O1, SP::O2, SP::O3,
1075 SP::O4, SP::O5, SP::O7, 0);
1076 }
1077
1078 return std::vector<unsigned>();
1079}
Dan Gohman6520e202008-10-18 02:06:02 +00001080
1081bool
1082SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1083 // The Sparc target isn't yet aware of offsets.
1084 return false;
1085}
Bill Wendling20c568f2009-06-30 22:38:32 +00001086
Bill Wendlingb4202b82009-07-01 18:50:55 +00001087/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001088unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001089 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001090}