blob: 27e78f07da569459d6ed5c22a3485363f9370054 [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"
Dan Gohman1e93df62010-04-17 14:41:14 +000017#include "SparcMachineFunctionInfo.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000018#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000019#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000020#include "llvm/CodeGen/MachineFrameInfo.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000025#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000026#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000028using namespace llvm;
29
Chris Lattner5a65b922008-03-17 05:41:48 +000030
31//===----------------------------------------------------------------------===//
32// Calling Convention Implementation
33//===----------------------------------------------------------------------===//
34
35#include "SparcGenCallingConv.inc"
36
Dan Gohman98ca4f22009-08-05 01:29:28 +000037SDValue
38SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000039 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000040 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +000041 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000042 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000043
Chris Lattner5a65b922008-03-17 05:41:48 +000044 // CCValAssign - represent the assignment of the return value to locations.
45 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000046
Chris Lattner5a65b922008-03-17 05:41:48 +000047 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000048 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
49 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000050
Dan Gohman98ca4f22009-08-05 01:29:28 +000051 // Analize return values.
52 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000053
Chris Lattner5a65b922008-03-17 05:41:48 +000054 // If this is the first return lowered for this function, add the regs to the
55 // liveout set for the function.
56 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
57 for (unsigned i = 0; i != RVLocs.size(); ++i)
58 if (RVLocs[i].isRegLoc())
59 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
60 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000061
Dan Gohman475871a2008-07-27 21:46:04 +000062 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000063
64 // Copy the result values into the output registers.
65 for (unsigned i = 0; i != RVLocs.size(); ++i) {
66 CCValAssign &VA = RVLocs[i];
67 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000068
Wesley Peckbf17cfa2010-11-23 03:31:01 +000069 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +000070 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000071
Chris Lattner5a65b922008-03-17 05:41:48 +000072 // Guarantee that all emitted copies are stuck together with flags.
73 Flag = Chain.getValue(1);
74 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000075
Gabor Greifba36cb52008-08-28 21:40:38 +000076 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +000077 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
78 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000079}
80
Dan Gohman98ca4f22009-08-05 01:29:28 +000081/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
82/// passed in either one or two GPRs, including FP values. TODO: we should
83/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000084SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000085SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000086 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000087 const SmallVectorImpl<ISD::InputArg>
88 &Ins,
89 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +000090 SmallVectorImpl<SDValue> &InVals)
91 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000092
Chris Lattner5a65b922008-03-17 05:41:48 +000093 MachineFunction &MF = DAG.getMachineFunction();
94 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +000095 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +000096
97 // Assign locations to all of the incoming arguments.
98 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +000099 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
100 ArgLocs, *DAG.getContext());
101 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000102
Chris Lattner5a65b922008-03-17 05:41:48 +0000103 static const unsigned ArgRegs[] = {
104 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
105 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000106 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
107 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000108
Eli Friedmana786c7b2009-07-19 19:53:46 +0000109 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
110 SDValue ArgValue;
111 CCValAssign &VA = ArgLocs[i];
112 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
113 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000114 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000116 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000117 case MVT::i1:
118 case MVT::i8:
119 case MVT::i16:
120 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000121 if (!Ins[i].Used) { // Argument is dead.
122 if (CurArgReg < ArgRegEnd) ++CurArgReg;
123 InVals.push_back(DAG.getUNDEF(ObjectVT));
124 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000125 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
126 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000127 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
128 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000129 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000130 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000131 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000132 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000133 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000134 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000135 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000136 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000137 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000138 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000139 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 if (ObjectVT == MVT::i32) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000141 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000142 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000143 } else {
144 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
145
146 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000147 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000148 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
149 DAG.getConstant(Offset, MVT::i32));
Evan Chengbcc80172010-07-07 22:15:37 +0000150 Load = DAG.getExtLoad(LoadOp, MVT::i32, dl, Chain, FIPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000151 MachinePointerInfo(), ObjectVT, false, false,0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000152 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000153 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000154 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000155 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000156
Chris Lattner5a65b922008-03-17 05:41:48 +0000157 ArgOffset += 4;
158 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000160 if (!Ins[i].Used) { // Argument is dead.
161 if (CurArgReg < ArgRegEnd) ++CurArgReg;
162 InVals.push_back(DAG.getUNDEF(ObjectVT));
163 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000164 // FP value is passed in an integer register.
165 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
166 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000167 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000168
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000169 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000170 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000171 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000172 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000173 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000175 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr,
176 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000177 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000178 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000179 }
180 ArgOffset += 4;
181 break;
182
Owen Anderson825b72b2009-08-11 20:47:22 +0000183 case MVT::i64:
184 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000185 if (!Ins[i].Used) { // Argument is dead.
186 if (CurArgReg < ArgRegEnd) ++CurArgReg;
187 if (CurArgReg < ArgRegEnd) ++CurArgReg;
188 InVals.push_back(DAG.getUNDEF(ObjectVT));
189 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000190 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000191 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
192 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
193 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000195 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000196 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000197 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000199 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000200 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000201 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000202
Dan Gohman475871a2008-07-27 21:46:04 +0000203 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000204 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
205 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
206 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000208 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000209 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
Evan Chenged2ae132010-07-03 00:40:23 +0000210 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000212 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000213 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000214 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000215
Chris Lattner5a65b922008-03-17 05:41:48 +0000216 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000217 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000218 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000219
Chris Lattner5a65b922008-03-17 05:41:48 +0000220 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000221 if (ObjectVT == MVT::f64)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000222 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000223
Dan Gohman98ca4f22009-08-05 01:29:28 +0000224 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000225 }
226 ArgOffset += 8;
227 break;
228 }
229 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000230
Chris Lattner5a65b922008-03-17 05:41:48 +0000231 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000232 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000233 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000234 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000235
Eli Friedmana786c7b2009-07-19 19:53:46 +0000236 std::vector<SDValue> OutChains;
237
Chris Lattner5a65b922008-03-17 05:41:48 +0000238 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
239 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
240 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000241 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000242
David Greene3f2bf852009-11-12 20:49:22 +0000243 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000244 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000245 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000246
Chris Lattner6229d0a2010-09-21 18:41:36 +0000247 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
248 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000249 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000250 ArgOffset += 4;
251 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000252
253 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000254 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000256 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000257 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000258 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000259
Dan Gohman98ca4f22009-08-05 01:29:28 +0000260 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000261}
262
Dan Gohman98ca4f22009-08-05 01:29:28 +0000263SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000264SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000265 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000266 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000267 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000268 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000269 const SmallVectorImpl<ISD::InputArg> &Ins,
270 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000271 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000272 // Sparc target does not yet support tail call optimization.
273 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000274
Chris Lattner315123f2008-03-17 06:58:37 +0000275#if 0
276 // Analyze operands of the call, assigning locations to each operand.
277 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000278 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
279 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000280
Chris Lattner315123f2008-03-17 06:58:37 +0000281 // Get the size of the outgoing arguments stack space requirement.
282 unsigned ArgsSize = CCInfo.getNextStackOffset();
283 // FIXME: We can't use this until f64 is known to take two GPRs.
284#else
285 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000286
Chris Lattner5a65b922008-03-17 05:41:48 +0000287 // Count the size of the outgoing arguments.
288 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000289 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000290 switch (Outs[i].VT.SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000291 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000292 case MVT::i1:
293 case MVT::i8:
294 case MVT::i16:
295 case MVT::i32:
296 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000297 ArgsSize += 4;
298 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 case MVT::i64:
300 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000301 ArgsSize += 8;
302 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000303 }
304 }
305 if (ArgsSize > 4*6)
306 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
307 else
308 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000309#endif
310
Chris Lattner5a65b922008-03-17 05:41:48 +0000311 // Keep stack frames 8-byte aligned.
312 ArgsSize = (ArgsSize+7) & ~7;
313
Chris Lattnere563bbc2008-10-11 22:08:30 +0000314 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000315
Dan Gohman475871a2008-07-27 21:46:04 +0000316 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
317 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000318
Chris Lattner315123f2008-03-17 06:58:37 +0000319#if 0
320 // Walk the register/memloc assignments, inserting copies/loads.
321 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
322 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000323 SDValue Arg = OutVals[i];
Chris Lattner315123f2008-03-17 06:58:37 +0000324
325 // Promote the value if needed.
326 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000327 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000328 case CCValAssign::Full: break;
329 case CCValAssign::SExt:
330 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
331 break;
332 case CCValAssign::ZExt:
333 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
334 break;
335 case CCValAssign::AExt:
336 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
337 break;
338 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000339
340 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000341 // RegsToPass vector
342 if (VA.isRegLoc()) {
343 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
344 continue;
345 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000346
Chris Lattner315123f2008-03-17 06:58:37 +0000347 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000348
Chris Lattner315123f2008-03-17 06:58:37 +0000349 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000350 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000351 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000352 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000353 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner6229d0a2010-09-21 18:41:36 +0000354 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000355 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000356 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000357
358#else
Chris Lattner315123f2008-03-17 06:58:37 +0000359 static const unsigned ArgRegs[] = {
360 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
361 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000362 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000363
Dan Gohman98ca4f22009-08-05 01:29:28 +0000364 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000365 SDValue Val = OutVals[i];
366 EVT ObjectVT = Outs[i].VT;
Dan Gohman475871a2008-07-27 21:46:04 +0000367 SDValue ValToStore(0, 0);
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000368 SDValue ValToStore2(0, 0);
369 unsigned ArgOffset1 = 0, ArgOffset2 = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000371 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000372 case MVT::i32:
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000373 ArgOffset1 = ArgOffset;
374 ArgOffset += 4;
Chris Lattner5a65b922008-03-17 05:41:48 +0000375
Chris Lattner315123f2008-03-17 06:58:37 +0000376 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000377 ValToStore = Val;
378 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000379 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000380 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000381
Chris Lattner5a65b922008-03-17 05:41:48 +0000382 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000383 case MVT::f32:
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000384 ArgOffset1 = ArgOffset;
385 ArgOffset += 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000386 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000387 ValToStore = Val;
388 } else {
389 // Convert this to a FP value in an int reg.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000390 Val = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000391 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000392 }
393 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000394 case MVT::f64: {
Duncan Sands8c0f2442008-12-12 08:05:40 +0000395
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000396 if (RegsToPass.size() >= 6) {
397 if (ArgOffset % 8 == 0) {
398 ArgOffset1 = ArgOffset;
399 ArgOffset += 8;
400 ValToStore = Val; // Whole thing is passed in memory.
401 break;
402 }
403 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000404 // Break into top and bottom parts by storing to the stack and loading
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000405 // out the parts as integers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000406 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000407 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000408 Val, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000409 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000410 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000411 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
412 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000413 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000414 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000415 DAG.getIntPtrConstant(4));
416 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000417 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
418 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000419
Duncan Sands8c0f2442008-12-12 08:05:40 +0000420 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000421 ArgOffset1 = ArgOffset;
422 ValToStore = Hi;
423 } else {
424 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
425 }
426 ArgOffset += 4;
427 if (RegsToPass.size() >= 6) {
428 ArgOffset2 = ArgOffset;
429 ValToStore2 = Lo;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000430 } else {
431 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
432 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000433 ArgOffset += 4;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000434 break;
435 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000436 case MVT::i64: {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000437
Chris Lattner315123f2008-03-17 06:58:37 +0000438 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000439 if (ArgOffset % 8 == 0) {
440 ArgOffset1 = ArgOffset;
441 ArgOffset += 8;
442 ValToStore = Val; // Whole thing is passed in memory.
443 break;
444 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000445 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000446
Chris Lattner5a65b922008-03-17 05:41:48 +0000447 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000448 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
449 DAG.getConstant(1, MVT::i32));
450 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
451 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000452 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000453 ArgOffset1 = ArgOffset;
454 ValToStore = Hi;
455 } else {
456 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
457 }
458 ArgOffset += 4;
459 if (RegsToPass.size() >= 6) {
460 ArgOffset2 = ArgOffset;
461 ValToStore2 = Lo;
Chris Lattner5a65b922008-03-17 05:41:48 +0000462 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000463 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000464 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000465 ArgOffset += 4;
Chris Lattner5a65b922008-03-17 05:41:48 +0000466 break;
467 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000468 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000469
Gabor Greifba36cb52008-08-28 21:40:38 +0000470 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000471 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000472 SDValue PtrOff = DAG.getConstant(ArgOffset1, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000473 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000474 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000475 PtrOff, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000476 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000477 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000478 if (ValToStore2.getNode()) {
479 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
480 SDValue PtrOff = DAG.getConstant(ArgOffset2, MVT::i32);
481 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
482 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore2,
483 PtrOff, MachinePointerInfo(),
484 false, false, 0));
485 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000486 }
Chris Lattner315123f2008-03-17 06:58:37 +0000487#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000488
Chris Lattner5a65b922008-03-17 05:41:48 +0000489 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000490 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000491 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000492 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000493
494 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000495 // chain and flag operands which copy the outgoing args into registers.
496 // The InFlag in necessary since all emited instructions must be
497 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000498 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000499 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
500 unsigned Reg = RegsToPass[i].first;
501 // Remap I0->I7 -> O0->O7.
502 if (Reg >= SP::I0 && Reg <= SP::I7)
503 Reg = Reg-SP::I0+SP::O0;
504
Dale Johannesen33c960f2009-02-04 20:06:27 +0000505 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000506 InFlag = Chain.getValue(1);
507 }
508
509 // If the callee is a GlobalAddress node (quite common, every direct call is)
510 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000511 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000512 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000513 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000514 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000515 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000516
Owen Andersone50ed302009-08-10 22:56:29 +0000517 std::vector<EVT> NodeTys;
Owen Anderson825b72b2009-08-11 20:47:22 +0000518 NodeTys.push_back(MVT::Other); // Returns a chain
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000519 NodeTys.push_back(MVT::Glue); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000520 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000521 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000522 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000523
Chris Lattnere563bbc2008-10-11 22:08:30 +0000524 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
525 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000526 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000527
Chris Lattner98949a62008-03-17 06:01:07 +0000528 // Assign locations to each value returned by this call.
529 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000530 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000531 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000532
Dan Gohman98ca4f22009-08-05 01:29:28 +0000533 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000534
Chris Lattner98949a62008-03-17 06:01:07 +0000535 // Copy all of the result registers out of their specified physreg.
536 for (unsigned i = 0; i != RVLocs.size(); ++i) {
537 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000538
Chris Lattner98949a62008-03-17 06:01:07 +0000539 // Remap I0->I7 -> O0->O7.
540 if (Reg >= SP::I0 && Reg <= SP::I7)
541 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000542
Dale Johannesen33c960f2009-02-04 20:06:27 +0000543 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000544 RVLocs[i].getValVT(), InFlag).getValue(1);
545 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000546 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000547 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000548
Dan Gohman98ca4f22009-08-05 01:29:28 +0000549 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000550}
551
552
553
Chris Lattnerd23405e2008-03-17 03:21:36 +0000554//===----------------------------------------------------------------------===//
555// TargetLowering Implementation
556//===----------------------------------------------------------------------===//
557
558/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
559/// condition.
560static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
561 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000562 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000563 case ISD::SETEQ: return SPCC::ICC_E;
564 case ISD::SETNE: return SPCC::ICC_NE;
565 case ISD::SETLT: return SPCC::ICC_L;
566 case ISD::SETGT: return SPCC::ICC_G;
567 case ISD::SETLE: return SPCC::ICC_LE;
568 case ISD::SETGE: return SPCC::ICC_GE;
569 case ISD::SETULT: return SPCC::ICC_CS;
570 case ISD::SETULE: return SPCC::ICC_LEU;
571 case ISD::SETUGT: return SPCC::ICC_GU;
572 case ISD::SETUGE: return SPCC::ICC_CC;
573 }
574}
575
576/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
577/// FCC condition.
578static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
579 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000580 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000581 case ISD::SETEQ:
582 case ISD::SETOEQ: return SPCC::FCC_E;
583 case ISD::SETNE:
584 case ISD::SETUNE: return SPCC::FCC_NE;
585 case ISD::SETLT:
586 case ISD::SETOLT: return SPCC::FCC_L;
587 case ISD::SETGT:
588 case ISD::SETOGT: return SPCC::FCC_G;
589 case ISD::SETLE:
590 case ISD::SETOLE: return SPCC::FCC_LE;
591 case ISD::SETGE:
592 case ISD::SETOGE: return SPCC::FCC_GE;
593 case ISD::SETULT: return SPCC::FCC_UL;
594 case ISD::SETULE: return SPCC::FCC_ULE;
595 case ISD::SETUGT: return SPCC::FCC_UG;
596 case ISD::SETUGE: return SPCC::FCC_UGE;
597 case ISD::SETUO: return SPCC::FCC_U;
598 case ISD::SETO: return SPCC::FCC_O;
599 case ISD::SETONE: return SPCC::FCC_LG;
600 case ISD::SETUEQ: return SPCC::FCC_UE;
601 }
602}
603
Chris Lattnerd23405e2008-03-17 03:21:36 +0000604SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000605 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000606
Chris Lattnerd23405e2008-03-17 03:21:36 +0000607 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000608 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
609 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
610 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000611
612 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000613 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000614 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000615 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000616 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000618
619 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
621 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
622 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000623
Chris Lattnerd23405e2008-03-17 03:21:36 +0000624 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
626 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
627 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000628
629 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000630 setOperationAction(ISD::UREM, MVT::i32, Expand);
631 setOperationAction(ISD::SREM, MVT::i32, Expand);
632 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
633 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000634
635 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000636 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
637 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000638
639 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000640 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
641 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000642
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000643 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
644 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000645
Chris Lattnerd23405e2008-03-17 03:21:36 +0000646 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 setOperationAction(ISD::SELECT, MVT::i32, Expand);
648 setOperationAction(ISD::SELECT, MVT::f32, Expand);
649 setOperationAction(ISD::SELECT, MVT::f64, Expand);
650 setOperationAction(ISD::SETCC, MVT::i32, Expand);
651 setOperationAction(ISD::SETCC, MVT::f32, Expand);
652 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000653
Chris Lattnerd23405e2008-03-17 03:21:36 +0000654 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000655 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
656 setOperationAction(ISD::BRIND, MVT::Other, Expand);
657 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
658 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
659 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
660 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000661
Owen Anderson825b72b2009-08-11 20:47:22 +0000662 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
663 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
664 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000668
Owen Anderson825b72b2009-08-11 20:47:22 +0000669 setOperationAction(ISD::FSIN , MVT::f64, Expand);
670 setOperationAction(ISD::FCOS , MVT::f64, Expand);
671 setOperationAction(ISD::FREM , MVT::f64, Expand);
672 setOperationAction(ISD::FSIN , MVT::f32, Expand);
673 setOperationAction(ISD::FCOS , MVT::f32, Expand);
674 setOperationAction(ISD::FREM , MVT::f32, Expand);
675 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
676 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
677 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
678 setOperationAction(ISD::ROTL , MVT::i32, Expand);
679 setOperationAction(ISD::ROTR , MVT::i32, Expand);
680 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
681 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
682 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
683 setOperationAction(ISD::FPOW , MVT::f64, Expand);
684 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000685
Owen Anderson825b72b2009-08-11 20:47:22 +0000686 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
687 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
688 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000689
690 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000691 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
692 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000693
Owen Anderson825b72b2009-08-11 20:47:22 +0000694 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000695
Chris Lattnerd23405e2008-03-17 03:21:36 +0000696 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000697 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000698 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000699 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000700
Chris Lattnerd23405e2008-03-17 03:21:36 +0000701 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000702 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
703 setOperationAction(ISD::VAEND , MVT::Other, Expand);
704 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
705 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
706 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000707
708 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000710
Chris Lattnerd23405e2008-03-17 03:21:36 +0000711 setStackPointerRegisterToSaveRestore(SP::O6);
712
713 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000715
Chris Lattnerd23405e2008-03-17 03:21:36 +0000716 computeRegisterProperties();
717}
718
719const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
720 switch (Opcode) {
721 default: return 0;
722 case SPISD::CMPICC: return "SPISD::CMPICC";
723 case SPISD::CMPFCC: return "SPISD::CMPFCC";
724 case SPISD::BRICC: return "SPISD::BRICC";
725 case SPISD::BRFCC: return "SPISD::BRFCC";
726 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
727 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
728 case SPISD::Hi: return "SPISD::Hi";
729 case SPISD::Lo: return "SPISD::Lo";
730 case SPISD::FTOI: return "SPISD::FTOI";
731 case SPISD::ITOF: return "SPISD::ITOF";
732 case SPISD::CALL: return "SPISD::CALL";
733 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
734 }
735}
736
737/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
738/// be zero. Op is expected to be a target specific node. Used by DAG
739/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000740void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000741 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000742 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000743 APInt &KnownOne,
744 const SelectionDAG &DAG,
745 unsigned Depth) const {
746 APInt KnownZero2, KnownOne2;
747 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000748
Chris Lattnerd23405e2008-03-17 03:21:36 +0000749 switch (Op.getOpcode()) {
750 default: break;
751 case SPISD::SELECT_ICC:
752 case SPISD::SELECT_FCC:
753 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
754 Depth+1);
755 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
756 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000757 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
758 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
759
Chris Lattnerd23405e2008-03-17 03:21:36 +0000760 // Only known if known in both the LHS and RHS.
761 KnownOne &= KnownOne2;
762 KnownZero &= KnownZero2;
763 break;
764 }
765}
766
Chris Lattnerd23405e2008-03-17 03:21:36 +0000767// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
768// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000769static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000770 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000771 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000772 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000773 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000774 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
775 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
776 (LHS.getOpcode() == SPISD::SELECT_FCC &&
777 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
778 isa<ConstantSDNode>(LHS.getOperand(0)) &&
779 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000780 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
781 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000782 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000783 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000784 LHS = CMPCC.getOperand(0);
785 RHS = CMPCC.getOperand(1);
786 }
787}
788
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000789SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000790 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000791 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000792 // FIXME there isn't really any debug info here
793 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000794 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000795 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
796 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000797
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000798 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000799 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000800
Chris Lattnerdb486a62009-09-15 17:46:24 +0000801 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
802 getPointerTy());
803 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000804 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000805 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000806 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000807 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000808}
809
Chris Lattnerdb486a62009-09-15 17:46:24 +0000810SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000811 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000812 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000813 // FIXME there isn't really any debug info here
814 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000815 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000816 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
817 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
818 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000819 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000820 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
821
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000822 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000823 getPointerTy());
824 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
825 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
826 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000827 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000828 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000829}
830
Dan Gohman475871a2008-07-27 21:46:04 +0000831static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000832 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000833 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000834 assert(Op.getValueType() == MVT::i32);
835 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000836 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000837}
838
Dan Gohman475871a2008-07-27 21:46:04 +0000839static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000840 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000841 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000842 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000843 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000844 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000845}
846
Dan Gohman475871a2008-07-27 21:46:04 +0000847static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
848 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000849 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000850 SDValue LHS = Op.getOperand(2);
851 SDValue RHS = Op.getOperand(3);
852 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000853 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000854 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000855
Chris Lattnerd23405e2008-03-17 03:21:36 +0000856 // If this is a br_cc of a "setcc", and if the setcc got lowered into
857 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
858 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000859
Chris Lattnerd23405e2008-03-17 03:21:36 +0000860 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000861 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000862 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000863 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 VTs.push_back(MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000865 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000866 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000867 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000868 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
869 Opc = SPISD::BRICC;
870 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000871 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000872 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
873 Opc = SPISD::BRFCC;
874 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000875 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
876 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000877}
878
Dan Gohman475871a2008-07-27 21:46:04 +0000879static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
880 SDValue LHS = Op.getOperand(0);
881 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000882 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000883 SDValue TrueVal = Op.getOperand(2);
884 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000885 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000886 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000887
Chris Lattnerd23405e2008-03-17 03:21:36 +0000888 // If this is a select_cc of a "setcc", and if the setcc got lowered into
889 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
890 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000891
Dan Gohman475871a2008-07-27 21:46:04 +0000892 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000893 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000894 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000895 VTs.push_back(LHS.getValueType()); // subcc returns a value
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000896 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000898 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000899 Opc = SPISD::SELECT_ICC;
900 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
901 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000902 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000903 Opc = SPISD::SELECT_FCC;
904 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
905 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000906 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000907 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000908}
909
Dan Gohman475871a2008-07-27 21:46:04 +0000910static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000911 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000912 MachineFunction &MF = DAG.getMachineFunction();
913 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
914
Chris Lattnerd23405e2008-03-17 03:21:36 +0000915 // vastart just stores the address of the VarArgsFrameIndex slot into the
916 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000917 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000918 SDValue Offset =
919 DAG.getNode(ISD::ADD, dl, MVT::i32,
920 DAG.getRegister(SP::I6, MVT::i32),
921 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
922 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000923 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +0000924 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
925 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000926}
927
Dan Gohman475871a2008-07-27 21:46:04 +0000928static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000929 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000930 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000931 SDValue InChain = Node->getOperand(0);
932 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000933 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000934 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000935 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
936 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000937 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000938 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000939 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000940 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000941 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000942 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000943 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000944 // Load the actual argument out of the pointer VAList, unless this is an
945 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000947 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
948 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000949
Chris Lattnerd23405e2008-03-17 03:21:36 +0000950 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000951 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000952 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000953
Chris Lattnerd23405e2008-03-17 03:21:36 +0000954 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000955 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000956 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000957 V.getValue(1)
958 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000959 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000960}
961
Dan Gohman475871a2008-07-27 21:46:04 +0000962static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
963 SDValue Chain = Op.getOperand(0); // Legalize the chain.
964 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000965 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000966
Chris Lattnerd23405e2008-03-17 03:21:36 +0000967 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000968 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
969 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000970 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000971
Chris Lattnerd23405e2008-03-17 03:21:36 +0000972 // The resultant pointer is actually 16 words from the bottom of the stack,
973 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000974 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
975 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000977 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000978}
979
Chris Lattnerd23405e2008-03-17 03:21:36 +0000980
Dan Gohman475871a2008-07-27 21:46:04 +0000981SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000982LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000983 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000984 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000985 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000986 case ISD::RETURNADDR: return SDValue();
987 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000988 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +0000989 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +0000990 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
991 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000992 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
993 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
994 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
995 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
996 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
997 case ISD::VAARG: return LowerVAARG(Op, DAG);
998 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000999 }
1000}
1001
1002MachineBasicBlock *
1003SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001004 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001005 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1006 unsigned BROpcode;
1007 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001008 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001009 // Figure out the conditional branch opcode to use for this select_cc.
1010 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001011 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001012 case SP::SELECT_CC_Int_ICC:
1013 case SP::SELECT_CC_FP_ICC:
1014 case SP::SELECT_CC_DFP_ICC:
1015 BROpcode = SP::BCOND;
1016 break;
1017 case SP::SELECT_CC_Int_FCC:
1018 case SP::SELECT_CC_FP_FCC:
1019 case SP::SELECT_CC_DFP_FCC:
1020 BROpcode = SP::FBCOND;
1021 break;
1022 }
1023
1024 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001025
Chris Lattnerd23405e2008-03-17 03:21:36 +00001026 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1027 // control-flow pattern. The incoming instruction knows the destination vreg
1028 // to set, the condition code register to branch on, the true/false values to
1029 // select between, and a branch opcode to use.
1030 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001031 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001032 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001033
Chris Lattnerd23405e2008-03-17 03:21:36 +00001034 // thisMBB:
1035 // ...
1036 // TrueVal = ...
1037 // [f]bCC copy1MBB
1038 // fallthrough --> copy0MBB
1039 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001040 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001041 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1042 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001043 F->insert(It, copy0MBB);
1044 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001045
1046 // Transfer the remainder of BB and its successor edges to sinkMBB.
1047 sinkMBB->splice(sinkMBB->begin(), BB,
1048 llvm::next(MachineBasicBlock::iterator(MI)),
1049 BB->end());
1050 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1051
1052 // Add the true and fallthrough blocks as its successors.
1053 BB->addSuccessor(copy0MBB);
1054 BB->addSuccessor(sinkMBB);
1055
Dale Johannesend552eee2009-02-13 02:31:35 +00001056 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001057
Chris Lattnerd23405e2008-03-17 03:21:36 +00001058 // copy0MBB:
1059 // %FalseValue = ...
1060 // # fallthrough to sinkMBB
1061 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001062
Chris Lattnerd23405e2008-03-17 03:21:36 +00001063 // Update machine-CFG edges
1064 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001065
Chris Lattnerd23405e2008-03-17 03:21:36 +00001066 // sinkMBB:
1067 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1068 // ...
1069 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001070 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001071 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1072 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001073
Dan Gohman14152b42010-07-06 20:24:04 +00001074 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001075 return BB;
1076}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001077
1078//===----------------------------------------------------------------------===//
1079// Sparc Inline Assembly Support
1080//===----------------------------------------------------------------------===//
1081
1082/// getConstraintType - Given a constraint letter, return the type of
1083/// constraint it is for this target.
1084SparcTargetLowering::ConstraintType
1085SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1086 if (Constraint.size() == 1) {
1087 switch (Constraint[0]) {
1088 default: break;
1089 case 'r': return C_RegisterClass;
1090 }
1091 }
1092
1093 return TargetLowering::getConstraintType(Constraint);
1094}
1095
1096std::pair<unsigned, const TargetRegisterClass*>
1097SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001098 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001099 if (Constraint.size() == 1) {
1100 switch (Constraint[0]) {
1101 case 'r':
1102 return std::make_pair(0U, SP::IntRegsRegisterClass);
1103 }
1104 }
1105
1106 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1107}
1108
1109std::vector<unsigned> SparcTargetLowering::
1110getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001111 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001112 if (Constraint.size() != 1)
1113 return std::vector<unsigned>();
1114
1115 switch (Constraint[0]) {
1116 default: break;
1117 case 'r':
1118 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1119 SP::L4, SP::L5, SP::L6, SP::L7,
1120 SP::I0, SP::I1, SP::I2, SP::I3,
1121 SP::I4, SP::I5,
1122 SP::O0, SP::O1, SP::O2, SP::O3,
1123 SP::O4, SP::O5, SP::O7, 0);
1124 }
1125
1126 return std::vector<unsigned>();
1127}
Dan Gohman6520e202008-10-18 02:06:02 +00001128
1129bool
1130SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1131 // The Sparc target isn't yet aware of offsets.
1132 return false;
1133}
Bill Wendling20c568f2009-06-30 22:38:32 +00001134
Bill Wendlingb4202b82009-07-01 18:50:55 +00001135/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001136unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001137 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001138}