blob: 1b3ca3ed1cd74ff280c311c327a972f78bb2e383 [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
253SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000254 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000255 bool isTailCall,
256 const SmallVectorImpl<ISD::OutputArg> &Outs,
257 const SmallVectorImpl<ISD::InputArg> &Ins,
258 DebugLoc dl, SelectionDAG &DAG,
259 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner98949a62008-03-17 06:01:07 +0000260
Chris Lattner315123f2008-03-17 06:58:37 +0000261#if 0
262 // Analyze operands of the call, assigning locations to each operand.
263 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000264 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
265 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000266
Chris Lattner315123f2008-03-17 06:58:37 +0000267 // Get the size of the outgoing arguments stack space requirement.
268 unsigned ArgsSize = CCInfo.getNextStackOffset();
269 // FIXME: We can't use this until f64 is known to take two GPRs.
270#else
271 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000272
Chris Lattner5a65b922008-03-17 05:41:48 +0000273 // Count the size of the outgoing arguments.
274 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000275 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 switch (Outs[i].Val.getValueType().getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000277 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000278 case MVT::i1:
279 case MVT::i8:
280 case MVT::i16:
281 case MVT::i32:
282 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000283 ArgsSize += 4;
284 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 case MVT::i64:
286 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000287 ArgsSize += 8;
288 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000289 }
290 }
291 if (ArgsSize > 4*6)
292 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
293 else
294 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000295#endif
296
Chris Lattner5a65b922008-03-17 05:41:48 +0000297 // Keep stack frames 8-byte aligned.
298 ArgsSize = (ArgsSize+7) & ~7;
299
Chris Lattnere563bbc2008-10-11 22:08:30 +0000300 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000301
Dan Gohman475871a2008-07-27 21:46:04 +0000302 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
303 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000304
Chris Lattner315123f2008-03-17 06:58:37 +0000305#if 0
306 // Walk the register/memloc assignments, inserting copies/loads.
307 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
308 CCValAssign &VA = ArgLocs[i];
Dan Gohman98ca4f22009-08-05 01:29:28 +0000309 SDValue Arg = Outs[i].Val;
Chris Lattner315123f2008-03-17 06:58:37 +0000310
311 // Promote the value if needed.
312 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000313 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000314 case CCValAssign::Full: break;
315 case CCValAssign::SExt:
316 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
317 break;
318 case CCValAssign::ZExt:
319 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
320 break;
321 case CCValAssign::AExt:
322 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
323 break;
324 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000325
326 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000327 // RegsToPass vector
328 if (VA.isRegLoc()) {
329 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
330 continue;
331 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000332
Chris Lattner315123f2008-03-17 06:58:37 +0000333 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000334
Chris Lattner315123f2008-03-17 06:58:37 +0000335 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000336 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000337 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000338 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000339 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner315123f2008-03-17 06:58:37 +0000340 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
341 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000342
343#else
Chris Lattner315123f2008-03-17 06:58:37 +0000344 static const unsigned ArgRegs[] = {
345 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
346 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000347 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000348
Dan Gohman98ca4f22009-08-05 01:29:28 +0000349 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
350 SDValue Val = Outs[i].Val;
Owen Andersone50ed302009-08-10 22:56:29 +0000351 EVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000352 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000353 unsigned ObjSize;
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000355 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000356 case MVT::i32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000357 ObjSize = 4;
358
Chris Lattner315123f2008-03-17 06:58:37 +0000359 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000360 ValToStore = Val;
361 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000362 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000363 }
364 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000365 case MVT::f32:
Chris Lattner5a65b922008-03-17 05:41:48 +0000366 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000367 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000368 ValToStore = Val;
369 } else {
370 // Convert this to a FP value in an int reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000372 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000373 }
374 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000375 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000376 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000377 if (RegsToPass.size() >= 6) {
378 ValToStore = Val; // Whole thing is passed in memory.
379 break;
380 }
381
382 // Break into top and bottom parts by storing to the stack and loading
383 // out the parts as integers. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000385 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
386 Val, StackPtr, NULL, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000387 // Sparc is big-endian, so the high part comes first.
Owen Anderson825b72b2009-08-11 20:47:22 +0000388 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000389 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000390 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000391 DAG.getIntPtrConstant(4));
392 // Load the low part.
Owen Anderson825b72b2009-08-11 20:47:22 +0000393 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000394
395 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
396
397 if (RegsToPass.size() >= 6) {
398 ValToStore = Lo;
399 ArgOffset += 4;
400 ObjSize = 4;
401 } else {
402 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
403 }
404 break;
405 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000407 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000408 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000409 ValToStore = Val; // Whole thing is passed in memory.
410 break;
411 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000412
Chris Lattner5a65b922008-03-17 05:41:48 +0000413 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000414 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
415 DAG.getConstant(1, MVT::i32));
416 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
417 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000418 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000419
Chris Lattner315123f2008-03-17 06:58:37 +0000420 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000421 ValToStore = Lo;
422 ArgOffset += 4;
423 ObjSize = 4;
424 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000425 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000426 }
427 break;
428 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000429 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000430
Gabor Greifba36cb52008-08-28 21:40:38 +0000431 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000432 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
433 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
434 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000435 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
436 PtrOff, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000437 }
438 ArgOffset += ObjSize;
439 }
Chris Lattner315123f2008-03-17 06:58:37 +0000440#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000441
Chris Lattner5a65b922008-03-17 05:41:48 +0000442 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000443 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000444 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000445 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000446
447 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000448 // chain and flag operands which copy the outgoing args into registers.
449 // The InFlag in necessary since all emited instructions must be
450 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000451 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000452 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
453 unsigned Reg = RegsToPass[i].first;
454 // Remap I0->I7 -> O0->O7.
455 if (Reg >= SP::I0 && Reg <= SP::I7)
456 Reg = Reg-SP::I0+SP::O0;
457
Dale Johannesen33c960f2009-02-04 20:06:27 +0000458 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000459 InFlag = Chain.getValue(1);
460 }
461
462 // If the callee is a GlobalAddress node (quite common, every direct call is)
463 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000464 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000465 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000466 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000467 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000468 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000469
Owen Andersone50ed302009-08-10 22:56:29 +0000470 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 NodeTys.push_back(MVT::Other); // Returns a chain
472 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000473 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000474 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000475 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000476
Chris Lattnere563bbc2008-10-11 22:08:30 +0000477 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
478 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000479 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000480
Chris Lattner98949a62008-03-17 06:01:07 +0000481 // Assign locations to each value returned by this call.
482 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000483 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000484 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000485
Dan Gohman98ca4f22009-08-05 01:29:28 +0000486 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000487
Chris Lattner98949a62008-03-17 06:01:07 +0000488 // Copy all of the result registers out of their specified physreg.
489 for (unsigned i = 0; i != RVLocs.size(); ++i) {
490 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000491
Chris Lattner98949a62008-03-17 06:01:07 +0000492 // Remap I0->I7 -> O0->O7.
493 if (Reg >= SP::I0 && Reg <= SP::I7)
494 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000495
Dale Johannesen33c960f2009-02-04 20:06:27 +0000496 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000497 RVLocs[i].getValVT(), InFlag).getValue(1);
498 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000499 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000500 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000501
Dan Gohman98ca4f22009-08-05 01:29:28 +0000502 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000503}
504
505
506
Chris Lattnerd23405e2008-03-17 03:21:36 +0000507//===----------------------------------------------------------------------===//
508// TargetLowering Implementation
509//===----------------------------------------------------------------------===//
510
511/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
512/// condition.
513static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
514 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000515 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000516 case ISD::SETEQ: return SPCC::ICC_E;
517 case ISD::SETNE: return SPCC::ICC_NE;
518 case ISD::SETLT: return SPCC::ICC_L;
519 case ISD::SETGT: return SPCC::ICC_G;
520 case ISD::SETLE: return SPCC::ICC_LE;
521 case ISD::SETGE: return SPCC::ICC_GE;
522 case ISD::SETULT: return SPCC::ICC_CS;
523 case ISD::SETULE: return SPCC::ICC_LEU;
524 case ISD::SETUGT: return SPCC::ICC_GU;
525 case ISD::SETUGE: return SPCC::ICC_CC;
526 }
527}
528
529/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
530/// FCC condition.
531static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
532 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000533 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000534 case ISD::SETEQ:
535 case ISD::SETOEQ: return SPCC::FCC_E;
536 case ISD::SETNE:
537 case ISD::SETUNE: return SPCC::FCC_NE;
538 case ISD::SETLT:
539 case ISD::SETOLT: return SPCC::FCC_L;
540 case ISD::SETGT:
541 case ISD::SETOGT: return SPCC::FCC_G;
542 case ISD::SETLE:
543 case ISD::SETOLE: return SPCC::FCC_LE;
544 case ISD::SETGE:
545 case ISD::SETOGE: return SPCC::FCC_GE;
546 case ISD::SETULT: return SPCC::FCC_UL;
547 case ISD::SETULE: return SPCC::FCC_ULE;
548 case ISD::SETUGT: return SPCC::FCC_UG;
549 case ISD::SETUGE: return SPCC::FCC_UGE;
550 case ISD::SETUO: return SPCC::FCC_U;
551 case ISD::SETO: return SPCC::FCC_O;
552 case ISD::SETONE: return SPCC::FCC_LG;
553 case ISD::SETUEQ: return SPCC::FCC_UE;
554 }
555}
556
Chris Lattnerd23405e2008-03-17 03:21:36 +0000557SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000558 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000559
Chris Lattnerd23405e2008-03-17 03:21:36 +0000560 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000561 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
562 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
563 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000564
565 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000566 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000567 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000568 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000569 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000571
572 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
574 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
575 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000576
Chris Lattnerd23405e2008-03-17 03:21:36 +0000577 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000578 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
579 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
580 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000581
582 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000583 setOperationAction(ISD::UREM, MVT::i32, Expand);
584 setOperationAction(ISD::SREM, MVT::i32, Expand);
585 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
586 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000587
588 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000589 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
590 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000591
592 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000593 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
594 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000595
Owen Anderson825b72b2009-08-11 20:47:22 +0000596 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
597 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000598
Chris Lattnerd23405e2008-03-17 03:21:36 +0000599 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 setOperationAction(ISD::SELECT, MVT::i32, Expand);
601 setOperationAction(ISD::SELECT, MVT::f32, Expand);
602 setOperationAction(ISD::SELECT, MVT::f64, Expand);
603 setOperationAction(ISD::SETCC, MVT::i32, Expand);
604 setOperationAction(ISD::SETCC, MVT::f32, Expand);
605 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000606
Chris Lattnerd23405e2008-03-17 03:21:36 +0000607 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
609 setOperationAction(ISD::BRIND, MVT::Other, Expand);
610 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
611 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
612 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
613 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000614
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
616 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
617 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000618
Chris Lattnerd23405e2008-03-17 03:21:36 +0000619 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000621
Owen Anderson825b72b2009-08-11 20:47:22 +0000622 setOperationAction(ISD::FSIN , MVT::f64, Expand);
623 setOperationAction(ISD::FCOS , MVT::f64, Expand);
624 setOperationAction(ISD::FREM , MVT::f64, Expand);
625 setOperationAction(ISD::FSIN , MVT::f32, Expand);
626 setOperationAction(ISD::FCOS , MVT::f32, Expand);
627 setOperationAction(ISD::FREM , MVT::f32, Expand);
628 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
629 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
630 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
631 setOperationAction(ISD::ROTL , MVT::i32, Expand);
632 setOperationAction(ISD::ROTR , MVT::i32, Expand);
633 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
634 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
635 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
636 setOperationAction(ISD::FPOW , MVT::f64, Expand);
637 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000638
Owen Anderson825b72b2009-08-11 20:47:22 +0000639 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
640 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
641 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000642
643 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000644 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
645 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000648
Chris Lattnerd23405e2008-03-17 03:21:36 +0000649 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000651 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000653
Chris Lattnerd23405e2008-03-17 03:21:36 +0000654 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
656 setOperationAction(ISD::VAEND , MVT::Other, Expand);
657 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
658 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
659 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000660
661 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000663
Chris Lattnerd23405e2008-03-17 03:21:36 +0000664 setStackPointerRegisterToSaveRestore(SP::O6);
665
666 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000668
Chris Lattnerd23405e2008-03-17 03:21:36 +0000669 computeRegisterProperties();
670}
671
672const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
673 switch (Opcode) {
674 default: return 0;
675 case SPISD::CMPICC: return "SPISD::CMPICC";
676 case SPISD::CMPFCC: return "SPISD::CMPFCC";
677 case SPISD::BRICC: return "SPISD::BRICC";
678 case SPISD::BRFCC: return "SPISD::BRFCC";
679 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
680 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
681 case SPISD::Hi: return "SPISD::Hi";
682 case SPISD::Lo: return "SPISD::Lo";
683 case SPISD::FTOI: return "SPISD::FTOI";
684 case SPISD::ITOF: return "SPISD::ITOF";
685 case SPISD::CALL: return "SPISD::CALL";
686 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
687 }
688}
689
690/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
691/// be zero. Op is expected to be a target specific node. Used by DAG
692/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000693void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000694 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000695 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000696 APInt &KnownOne,
697 const SelectionDAG &DAG,
698 unsigned Depth) const {
699 APInt KnownZero2, KnownOne2;
700 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000701
Chris Lattnerd23405e2008-03-17 03:21:36 +0000702 switch (Op.getOpcode()) {
703 default: break;
704 case SPISD::SELECT_ICC:
705 case SPISD::SELECT_FCC:
706 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
707 Depth+1);
708 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
709 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000710 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
711 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
712
Chris Lattnerd23405e2008-03-17 03:21:36 +0000713 // Only known if known in both the LHS and RHS.
714 KnownOne &= KnownOne2;
715 KnownZero &= KnownZero2;
716 break;
717 }
718}
719
Chris Lattnerd23405e2008-03-17 03:21:36 +0000720// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
721// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000722static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000723 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000724 if (isa<ConstantSDNode>(RHS) &&
725 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000726 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000727 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
728 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
729 (LHS.getOpcode() == SPISD::SELECT_FCC &&
730 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
731 isa<ConstantSDNode>(LHS.getOperand(0)) &&
732 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000733 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
734 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000735 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000736 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000737 LHS = CMPCC.getOperand(0);
738 RHS = CMPCC.getOperand(1);
739 }
740}
741
Chris Lattnerdb486a62009-09-15 17:46:24 +0000742SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
743 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000744 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000745 // FIXME there isn't really any debug info here
746 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000747 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
748 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
749 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000750
751 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
752 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
753
754 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
755 getPointerTy());
756 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
757 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
758 GlobalBase, RelAddr);
759 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
760 AbsAddr, NULL, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000761}
762
Chris Lattnerdb486a62009-09-15 17:46:24 +0000763SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
764 SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000765 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000766 // FIXME there isn't really any debug info here
767 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000768 Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000769 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
770 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
771 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000772 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
773 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
774
775 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
776 getPointerTy());
777 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
778 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
779 GlobalBase, RelAddr);
780 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
781 AbsAddr, NULL, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000782}
783
Dan Gohman475871a2008-07-27 21:46:04 +0000784static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000785 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000786 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000787 assert(Op.getValueType() == MVT::i32);
788 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
789 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000790}
791
Dan Gohman475871a2008-07-27 21:46:04 +0000792static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000793 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000794 assert(Op.getOperand(0).getValueType() == MVT::i32);
795 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000796 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000797 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000798}
799
Dan Gohman475871a2008-07-27 21:46:04 +0000800static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
801 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000802 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000803 SDValue LHS = Op.getOperand(2);
804 SDValue RHS = Op.getOperand(3);
805 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000806 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000807 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000808
Chris Lattnerd23405e2008-03-17 03:21:36 +0000809 // If this is a br_cc of a "setcc", and if the setcc got lowered into
810 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
811 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000812
Chris Lattnerd23405e2008-03-17 03:21:36 +0000813 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000814 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000815 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000816 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000817 VTs.push_back(MVT::i32);
818 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000820 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000821 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
822 Opc = SPISD::BRICC;
823 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000824 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000825 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
826 Opc = SPISD::BRFCC;
827 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
829 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000830}
831
Dan Gohman475871a2008-07-27 21:46:04 +0000832static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
833 SDValue LHS = Op.getOperand(0);
834 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000835 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000836 SDValue TrueVal = Op.getOperand(2);
837 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000838 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000839 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000840
Chris Lattnerd23405e2008-03-17 03:21:36 +0000841 // If this is a select_cc of a "setcc", and if the setcc got lowered into
842 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
843 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000844
Dan Gohman475871a2008-07-27 21:46:04 +0000845 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000847 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000848 VTs.push_back(LHS.getValueType()); // subcc returns a value
Owen Anderson825b72b2009-08-11 20:47:22 +0000849 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000851 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000852 Opc = SPISD::SELECT_ICC;
853 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
854 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000855 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Flag, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000856 Opc = SPISD::SELECT_FCC;
857 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
858 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000859 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000861}
862
Dan Gohman475871a2008-07-27 21:46:04 +0000863static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000864 SparcTargetLowering &TLI) {
865 // vastart just stores the address of the VarArgsFrameIndex slot into the
866 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000867 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000868 SDValue Offset = DAG.getNode(ISD::ADD, dl, MVT::i32,
869 DAG.getRegister(SP::I6, MVT::i32),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000870 DAG.getConstant(TLI.getVarArgsFrameOffset(),
Owen Anderson825b72b2009-08-11 20:47:22 +0000871 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000872 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000873 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000874}
875
Dan Gohman475871a2008-07-27 21:46:04 +0000876static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000877 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000878 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000879 SDValue InChain = Node->getOperand(0);
880 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000881 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000882 DebugLoc dl = Node->getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000883 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000884 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000886 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000888 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000889 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000890 VAListPtr, SV, 0);
891 // Load the actual argument out of the pointer VAList, unless this is an
892 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 if (VT != MVT::f64)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000894 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000895
Chris Lattnerd23405e2008-03-17 03:21:36 +0000896 // Otherwise, load it as i64, then do a bitconvert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000897 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000898
Chris Lattnerd23405e2008-03-17 03:21:36 +0000899 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000900 SDValue Ops[2] = {
Owen Anderson825b72b2009-08-11 20:47:22 +0000901 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000902 V.getValue(1)
903 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000904 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000905}
906
Dan Gohman475871a2008-07-27 21:46:04 +0000907static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
908 SDValue Chain = Op.getOperand(0); // Legalize the chain.
909 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000910 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000911
Chris Lattnerd23405e2008-03-17 03:21:36 +0000912 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000913 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
914 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000915 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000916
Chris Lattnerd23405e2008-03-17 03:21:36 +0000917 // The resultant pointer is actually 16 words from the bottom of the stack,
918 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
920 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000921 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000922 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000923}
924
Chris Lattnerd23405e2008-03-17 03:21:36 +0000925
Dan Gohman475871a2008-07-27 21:46:04 +0000926SDValue SparcTargetLowering::
927LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000928 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000929 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000930 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000931 case ISD::RETURNADDR: return SDValue();
932 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000933 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000934 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000935 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
936 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000937 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
938 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
939 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
940 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
941 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
942 case ISD::VAARG: return LowerVAARG(Op, DAG);
943 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000944 }
945}
946
947MachineBasicBlock *
948SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000949 MachineBasicBlock *BB,
950 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000951 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
952 unsigned BROpcode;
953 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +0000954 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000955 // Figure out the conditional branch opcode to use for this select_cc.
956 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000957 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000958 case SP::SELECT_CC_Int_ICC:
959 case SP::SELECT_CC_FP_ICC:
960 case SP::SELECT_CC_DFP_ICC:
961 BROpcode = SP::BCOND;
962 break;
963 case SP::SELECT_CC_Int_FCC:
964 case SP::SELECT_CC_FP_FCC:
965 case SP::SELECT_CC_DFP_FCC:
966 BROpcode = SP::FBCOND;
967 break;
968 }
969
970 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000971
Chris Lattnerd23405e2008-03-17 03:21:36 +0000972 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
973 // control-flow pattern. The incoming instruction knows the destination vreg
974 // to set, the condition code register to branch on, the true/false values to
975 // select between, and a branch opcode to use.
976 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000977 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000978 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000979
Chris Lattnerd23405e2008-03-17 03:21:36 +0000980 // thisMBB:
981 // ...
982 // TrueVal = ...
983 // [f]bCC copy1MBB
984 // fallthrough --> copy0MBB
985 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000986 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000987 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
988 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Dale Johannesend552eee2009-02-13 02:31:35 +0000989 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000990 F->insert(It, copy0MBB);
991 F->insert(It, sinkMBB);
Evan Chengce319102009-09-19 09:51:03 +0000992 // Update machine-CFG edges by first adding all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +0000993 // block to the new block which will contain the Phi node for the select.
Evan Chengce319102009-09-19 09:51:03 +0000994 // Also inform sdisel of the edge changes.
995 for (MachineBasicBlock::succ_iterator I = BB->succ_begin(),
996 E = BB->succ_end(); I != E; ++I) {
997 EM->insert(std::make_pair(*I, sinkMBB));
998 sinkMBB->addSuccessor(*I);
999 }
1000 // Next, remove all successors of the current block, and add the true
1001 // and fallthrough blocks as its successors.
1002 while (!BB->succ_empty())
1003 BB->removeSuccessor(BB->succ_begin());
Dan Gohman0011dc42008-06-21 20:21:19 +00001004 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001005 BB->addSuccessor(copy0MBB);
1006 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001007
Chris Lattnerd23405e2008-03-17 03:21:36 +00001008 // copy0MBB:
1009 // %FalseValue = ...
1010 // # fallthrough to sinkMBB
1011 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001012
Chris Lattnerd23405e2008-03-17 03:21:36 +00001013 // Update machine-CFG edges
1014 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001015
Chris Lattnerd23405e2008-03-17 03:21:36 +00001016 // sinkMBB:
1017 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1018 // ...
1019 BB = sinkMBB;
Dale Johannesend552eee2009-02-13 02:31:35 +00001020 BuildMI(BB, dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001021 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1022 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001023
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001024 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001025 return BB;
1026}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001027
1028//===----------------------------------------------------------------------===//
1029// Sparc Inline Assembly Support
1030//===----------------------------------------------------------------------===//
1031
1032/// getConstraintType - Given a constraint letter, return the type of
1033/// constraint it is for this target.
1034SparcTargetLowering::ConstraintType
1035SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1036 if (Constraint.size() == 1) {
1037 switch (Constraint[0]) {
1038 default: break;
1039 case 'r': return C_RegisterClass;
1040 }
1041 }
1042
1043 return TargetLowering::getConstraintType(Constraint);
1044}
1045
1046std::pair<unsigned, const TargetRegisterClass*>
1047SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001048 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001049 if (Constraint.size() == 1) {
1050 switch (Constraint[0]) {
1051 case 'r':
1052 return std::make_pair(0U, SP::IntRegsRegisterClass);
1053 }
1054 }
1055
1056 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1057}
1058
1059std::vector<unsigned> SparcTargetLowering::
1060getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001061 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001062 if (Constraint.size() != 1)
1063 return std::vector<unsigned>();
1064
1065 switch (Constraint[0]) {
1066 default: break;
1067 case 'r':
1068 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1069 SP::L4, SP::L5, SP::L6, SP::L7,
1070 SP::I0, SP::I1, SP::I2, SP::I3,
1071 SP::I4, SP::I5,
1072 SP::O0, SP::O1, SP::O2, SP::O3,
1073 SP::O4, SP::O5, SP::O7, 0);
1074 }
1075
1076 return std::vector<unsigned>();
1077}
Dan Gohman6520e202008-10-18 02:06:02 +00001078
1079bool
1080SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1081 // The Sparc target isn't yet aware of offsets.
1082 return false;
1083}
Bill Wendling20c568f2009-06-30 22:38:32 +00001084
Bill Wendlingb4202b82009-07-01 18:50:55 +00001085/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001086unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001087 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001088}