blob: ec8c7b4809727ac038652e24640d842913685e4a [file] [log] [blame]
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +00001
Chris Lattnerd23405e2008-03-17 03:21:36 +00002//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file implements the interfaces that Sparc uses to lower LLVM code into a
12// selection DAG.
13//
14//===----------------------------------------------------------------------===//
15
16#include "SparcISelLowering.h"
17#include "SparcTargetMachine.h"
Dan Gohman1e93df62010-04-17 14:41:14 +000018#include "SparcMachineFunctionInfo.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000020#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000021#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov362dd0b2010-02-15 22:37:53 +000026#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000027#include "llvm/ADT/VectorExtras.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000029using namespace llvm;
30
Chris Lattner5a65b922008-03-17 05:41:48 +000031
32//===----------------------------------------------------------------------===//
33// Calling Convention Implementation
34//===----------------------------------------------------------------------===//
35
36#include "SparcGenCallingConv.inc"
37
Dan Gohman98ca4f22009-08-05 01:29:28 +000038SDValue
39SparcTargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000040 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000041 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +000042 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohmand858e902010-04-17 15:26:15 +000043 DebugLoc dl, SelectionDAG &DAG) const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000044
Chris Lattner5a65b922008-03-17 05:41:48 +000045 // CCValAssign - represent the assignment of the return value to locations.
46 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikov53835702008-10-10 20:27:31 +000047
Chris Lattner5a65b922008-03-17 05:41:48 +000048 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +000049 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(),
50 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +000051
Dan Gohman98ca4f22009-08-05 01:29:28 +000052 // Analize return values.
53 CCInfo.AnalyzeReturn(Outs, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000054
Chris Lattner5a65b922008-03-17 05:41:48 +000055 // If this is the first return lowered for this function, add the regs to the
56 // liveout set for the function.
57 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
58 for (unsigned i = 0; i != RVLocs.size(); ++i)
59 if (RVLocs[i].isRegLoc())
60 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
61 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000062
Dan Gohman475871a2008-07-27 21:46:04 +000063 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000064
65 // Copy the result values into the output registers.
66 for (unsigned i = 0; i != RVLocs.size(); ++i) {
67 CCValAssign &VA = RVLocs[i];
68 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000069
Wesley Peckbf17cfa2010-11-23 03:31:01 +000070 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohmanc9403652010-07-07 15:54:55 +000071 OutVals[i], Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000072
Chris Lattner5a65b922008-03-17 05:41:48 +000073 // Guarantee that all emitted copies are stuck together with flags.
74 Flag = Chain.getValue(1);
75 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000076
Gabor Greifba36cb52008-08-28 21:40:38 +000077 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +000078 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
79 return DAG.getNode(SPISD::RET_FLAG, dl, MVT::Other, Chain);
Chris Lattner5a65b922008-03-17 05:41:48 +000080}
81
Dan Gohman98ca4f22009-08-05 01:29:28 +000082/// LowerFormalArguments - V8 uses a very simple ABI, where all values are
83/// passed in either one or two GPRs, including FP values. TODO: we should
84/// pass FP values in FP registers for fastcc functions.
Eli Friedmana786c7b2009-07-19 19:53:46 +000085SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000086SparcTargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +000087 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +000088 const SmallVectorImpl<ISD::InputArg>
89 &Ins,
90 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +000091 SmallVectorImpl<SDValue> &InVals)
92 const {
Dan Gohman98ca4f22009-08-05 01:29:28 +000093
Chris Lattner5a65b922008-03-17 05:41:48 +000094 MachineFunction &MF = DAG.getMachineFunction();
95 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Dan Gohman1e93df62010-04-17 14:41:14 +000096 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
Eli Friedmana786c7b2009-07-19 19:53:46 +000097
98 // Assign locations to all of the incoming arguments.
99 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000100 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
101 ArgLocs, *DAG.getContext());
102 CCInfo.AnalyzeFormalArguments(Ins, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000103
Chris Lattner5a65b922008-03-17 05:41:48 +0000104 static const unsigned ArgRegs[] = {
105 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
106 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000107 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
108 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000109
Eli Friedmana786c7b2009-07-19 19:53:46 +0000110 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
111 SDValue ArgValue;
112 CCValAssign &VA = ArgLocs[i];
113 // FIXME: We ignore the register assignments of AnalyzeFormalArguments
114 // because it doesn't know how to split a double into two i32 registers.
Owen Andersone50ed302009-08-10 22:56:29 +0000115 EVT ObjectVT = VA.getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000116 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000117 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000118 case MVT::i1:
119 case MVT::i8:
120 case MVT::i16:
121 case MVT::i32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000122 if (!Ins[i].Used) { // Argument is dead.
123 if (CurArgReg < ArgRegEnd) ++CurArgReg;
124 InVals.push_back(DAG.getUNDEF(ObjectVT));
125 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000126 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
127 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000128 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
129 if (ObjectVT != MVT::i32) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000130 unsigned AssertOp = ISD::AssertSext;
Owen Anderson825b72b2009-08-11 20:47:22 +0000131 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000132 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000133 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000134 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000135 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000136 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000137 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000138 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000139 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000140 SDValue Load;
Owen Anderson825b72b2009-08-11 20:47:22 +0000141 if (ObjectVT == MVT::i32) {
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000142 Load = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000143 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000144 } else {
145 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
146
147 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000148 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000149 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
150 DAG.getConstant(Offset, MVT::i32));
Evan Chengbcc80172010-07-07 22:15:37 +0000151 Load = DAG.getExtLoad(LoadOp, MVT::i32, dl, Chain, FIPtr,
Chris Lattner3d6ccfb2010-09-21 17:04:51 +0000152 MachinePointerInfo(), ObjectVT, false, false,0);
Dale Johannesen39355f92009-02-04 02:34:38 +0000153 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000154 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000156 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000157
Chris Lattner5a65b922008-03-17 05:41:48 +0000158 ArgOffset += 4;
159 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 case MVT::f32:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000161 if (!Ins[i].Used) { // Argument is dead.
162 if (CurArgReg < ArgRegEnd) ++CurArgReg;
163 InVals.push_back(DAG.getUNDEF(ObjectVT));
164 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
Chris Lattner5a65b922008-03-17 05:41:48 +0000165 // FP value is passed in an integer register.
166 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
167 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 SDValue Arg = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000169
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000170 Arg = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Arg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000171 InVals.push_back(Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000172 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000173 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000174 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000175 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000176 SDValue Load = DAG.getLoad(MVT::f32, dl, Chain, FIPtr,
177 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000178 false, false, 0);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000179 InVals.push_back(Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000180 }
181 ArgOffset += 4;
182 break;
183
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 case MVT::i64:
185 case MVT::f64:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000186 if (!Ins[i].Used) { // Argument is dead.
187 if (CurArgReg < ArgRegEnd) ++CurArgReg;
188 if (CurArgReg < ArgRegEnd) ++CurArgReg;
189 InVals.push_back(DAG.getUNDEF(ObjectVT));
190 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000191 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000192 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
193 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
194 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 HiVal = DAG.getCopyFromReg(Chain, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000196 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000197 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000198 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000200 HiVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000201 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000202 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000203
Dan Gohman475871a2008-07-27 21:46:04 +0000204 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
206 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
207 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Owen Anderson825b72b2009-08-11 20:47:22 +0000208 LoVal = DAG.getCopyFromReg(Chain, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000209 } else {
David Greene3f2bf852009-11-12 20:49:22 +0000210 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4,
Evan Chenged2ae132010-07-03 00:40:23 +0000211 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000212 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000213 LoVal = DAG.getLoad(MVT::i32, dl, Chain, FIPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000214 false, false, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000215 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000216
Chris Lattner5a65b922008-03-17 05:41:48 +0000217 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000218 SDValue WholeValue =
Owen Anderson825b72b2009-08-11 20:47:22 +0000219 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000220
Chris Lattner5a65b922008-03-17 05:41:48 +0000221 // If we want a double, do a bit convert.
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 if (ObjectVT == MVT::f64)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000223 WholeValue = DAG.getNode(ISD::BITCAST, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000224
Dan Gohman98ca4f22009-08-05 01:29:28 +0000225 InVals.push_back(WholeValue);
Chris Lattner5a65b922008-03-17 05:41:48 +0000226 }
227 ArgOffset += 8;
228 break;
229 }
230 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000231
Chris Lattner5a65b922008-03-17 05:41:48 +0000232 // Store remaining ArgRegs to the stack if this is a varargs function.
Eli Friedmana786c7b2009-07-19 19:53:46 +0000233 if (isVarArg) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000234 // Remember the vararg offset for the va_start implementation.
Dan Gohman1e93df62010-04-17 14:41:14 +0000235 FuncInfo->setVarArgsFrameOffset(ArgOffset);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000236
Eli Friedmana786c7b2009-07-19 19:53:46 +0000237 std::vector<SDValue> OutChains;
238
Chris Lattner5a65b922008-03-17 05:41:48 +0000239 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
240 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
241 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000243
David Greene3f2bf852009-11-12 20:49:22 +0000244 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset,
Evan Chenged2ae132010-07-03 00:40:23 +0000245 true);
Owen Anderson825b72b2009-08-11 20:47:22 +0000246 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000247
Chris Lattner6229d0a2010-09-21 18:41:36 +0000248 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr,
249 MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000250 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000251 ArgOffset += 4;
252 }
Eli Friedmana786c7b2009-07-19 19:53:46 +0000253
254 if (!OutChains.empty()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000255 OutChains.push_back(Chain);
Owen Anderson825b72b2009-08-11 20:47:22 +0000256 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000257 &OutChains[0], OutChains.size());
Eli Friedmana786c7b2009-07-19 19:53:46 +0000258 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000259 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000260
Dan Gohman98ca4f22009-08-05 01:29:28 +0000261 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000262}
263
Dan Gohman98ca4f22009-08-05 01:29:28 +0000264SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000265SparcTargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000266 CallingConv::ID CallConv, bool isVarArg,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000267 bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000268 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmanc9403652010-07-07 15:54:55 +0000269 const SmallVectorImpl<SDValue> &OutVals,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000270 const SmallVectorImpl<ISD::InputArg> &Ins,
271 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000272 SmallVectorImpl<SDValue> &InVals) const {
Evan Cheng0c439eb2010-01-27 00:07:07 +0000273 // Sparc target does not yet support tail call optimization.
274 isTailCall = false;
Chris Lattner98949a62008-03-17 06:01:07 +0000275
Chris Lattner315123f2008-03-17 06:58:37 +0000276#if 0
277 // Analyze operands of the call, assigning locations to each operand.
278 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000279 CCState CCInfo(CallConv, isVarArg, DAG.getTarget(), ArgLocs);
280 CCInfo.AnalyzeCallOperands(Outs, CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000281
Chris Lattner315123f2008-03-17 06:58:37 +0000282 // Get the size of the outgoing arguments stack space requirement.
283 unsigned ArgsSize = CCInfo.getNextStackOffset();
284 // FIXME: We can't use this until f64 is known to take two GPRs.
285#else
286 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000287
Chris Lattner5a65b922008-03-17 05:41:48 +0000288 // Count the size of the outgoing arguments.
289 unsigned ArgsSize = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000290 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000291 switch (Outs[i].VT.SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000292 default: llvm_unreachable("Unknown value type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000293 case MVT::i1:
294 case MVT::i8:
295 case MVT::i16:
296 case MVT::i32:
297 case MVT::f32:
Chris Lattner315123f2008-03-17 06:58:37 +0000298 ArgsSize += 4;
299 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000300 case MVT::i64:
301 case MVT::f64:
Chris Lattner315123f2008-03-17 06:58:37 +0000302 ArgsSize += 8;
303 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000304 }
305 }
306 if (ArgsSize > 4*6)
307 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
308 else
309 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000310#endif
311
Chris Lattner5a65b922008-03-17 05:41:48 +0000312 // Keep stack frames 8-byte aligned.
313 ArgsSize = (ArgsSize+7) & ~7;
314
Chris Lattnere563bbc2008-10-11 22:08:30 +0000315 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000316
Dan Gohman475871a2008-07-27 21:46:04 +0000317 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
318 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000319
Chris Lattner315123f2008-03-17 06:58:37 +0000320#if 0
321 // Walk the register/memloc assignments, inserting copies/loads.
322 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
323 CCValAssign &VA = ArgLocs[i];
Dan Gohmanc9403652010-07-07 15:54:55 +0000324 SDValue Arg = OutVals[i];
Chris Lattner315123f2008-03-17 06:58:37 +0000325
326 // Promote the value if needed.
327 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000328 default: llvm_unreachable("Unknown loc info!");
Chris Lattner315123f2008-03-17 06:58:37 +0000329 case CCValAssign::Full: break;
330 case CCValAssign::SExt:
331 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
332 break;
333 case CCValAssign::ZExt:
334 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
335 break;
336 case CCValAssign::AExt:
337 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
338 break;
339 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000340
341 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000342 // RegsToPass vector
343 if (VA.isRegLoc()) {
344 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
345 continue;
346 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000347
Chris Lattner315123f2008-03-17 06:58:37 +0000348 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000349
Chris Lattner315123f2008-03-17 06:58:37 +0000350 // Create a store off the stack pointer for this argument.
Owen Anderson825b72b2009-08-11 20:47:22 +0000351 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000352 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000353 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Owen Anderson825b72b2009-08-11 20:47:22 +0000354 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner6229d0a2010-09-21 18:41:36 +0000355 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000356 false, false, 0));
Chris Lattner315123f2008-03-17 06:58:37 +0000357 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000358
359#else
Chris Lattner315123f2008-03-17 06:58:37 +0000360 static const unsigned ArgRegs[] = {
361 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
362 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000363 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000364
Dan Gohman98ca4f22009-08-05 01:29:28 +0000365 for (unsigned i = 0, e = Outs.size(); i != e; ++i) {
Dan Gohmanc9403652010-07-07 15:54:55 +0000366 SDValue Val = OutVals[i];
367 EVT ObjectVT = Outs[i].VT;
Dan Gohman475871a2008-07-27 21:46:04 +0000368 SDValue ValToStore(0, 0);
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000369 SDValue ValToStore2(0, 0);
370 unsigned ArgOffset1 = 0, ArgOffset2 = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000371 switch (ObjectVT.getSimpleVT().SimpleTy) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000372 default: llvm_unreachable("Unhandled argument type!");
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 case MVT::i32:
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000374 ArgOffset1 = ArgOffset;
375 ArgOffset += 4;
Chris Lattner5a65b922008-03-17 05:41:48 +0000376
Chris Lattner315123f2008-03-17 06:58:37 +0000377 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000378 ValToStore = Val;
379 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000380 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000381 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000382
Chris Lattner5a65b922008-03-17 05:41:48 +0000383 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000384 case MVT::f32:
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000385 ArgOffset1 = ArgOffset;
386 ArgOffset += 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000387 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000388 ValToStore = Val;
389 } else {
390 // Convert this to a FP value in an int reg.
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000391 Val = DAG.getNode(ISD::BITCAST, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000392 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000393 }
394 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000395 case MVT::f64: {
Duncan Sands8c0f2442008-12-12 08:05:40 +0000396
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000397 if (RegsToPass.size() >= 6) {
398 if (ArgOffset % 8 == 0) {
399 ArgOffset1 = ArgOffset;
400 ArgOffset += 8;
401 ValToStore = Val; // Whole thing is passed in memory.
402 break;
403 }
404 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000405 // Break into top and bottom parts by storing to the stack and loading
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000406 // out the parts as integers.
Owen Anderson825b72b2009-08-11 20:47:22 +0000407 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000408 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000409 Val, StackPtr, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000410 false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000411 // Sparc is big-endian, so the high part comes first.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000412 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
413 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000414 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000415 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000416 DAG.getIntPtrConstant(4));
417 // Load the low part.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000418 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr,
419 MachinePointerInfo(), false, false, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000420
Duncan Sands8c0f2442008-12-12 08:05:40 +0000421 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000422 ArgOffset1 = ArgOffset;
423 ValToStore = Hi;
424 } else {
425 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
426 }
427 ArgOffset += 4;
428 if (RegsToPass.size() >= 6) {
429 ArgOffset2 = ArgOffset;
430 ValToStore2 = Lo;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000431 } else {
432 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
433 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000434 ArgOffset += 4;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000435 break;
436 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000437 case MVT::i64: {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000438
Chris Lattner315123f2008-03-17 06:58:37 +0000439 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000440 if (ArgOffset % 8 == 0) {
441 ArgOffset1 = ArgOffset;
442 ArgOffset += 8;
443 ValToStore = Val; // Whole thing is passed in memory.
444 break;
445 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000446 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000447
Chris Lattner5a65b922008-03-17 05:41:48 +0000448 // Split the value into top and bottom part. Top part goes in a reg.
Owen Anderson825b72b2009-08-11 20:47:22 +0000449 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
450 DAG.getConstant(1, MVT::i32));
451 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
452 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000453 if (RegsToPass.size() >= 6) {
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000454 ArgOffset1 = ArgOffset;
455 ValToStore = Hi;
456 } else {
457 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
458 }
459 ArgOffset += 4;
460 if (RegsToPass.size() >= 6) {
461 ArgOffset2 = ArgOffset;
462 ValToStore2 = Lo;
Chris Lattner5a65b922008-03-17 05:41:48 +0000463 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000464 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000465 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000466 ArgOffset += 4;
Chris Lattner5a65b922008-03-17 05:41:48 +0000467 break;
468 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000469 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000470
Gabor Greifba36cb52008-08-28 21:40:38 +0000471 if (ValToStore.getNode()) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000473 SDValue PtrOff = DAG.getConstant(ArgOffset1, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000474 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000475 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000476 PtrOff, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000477 false, false, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000478 }
Venkatraman Govindaraju12db7b62010-12-29 05:37:15 +0000479 if (ValToStore2.getNode()) {
480 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
481 SDValue PtrOff = DAG.getConstant(ArgOffset2, MVT::i32);
482 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
483 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore2,
484 PtrOff, MachinePointerInfo(),
485 false, false, 0));
486 }
Chris Lattner5a65b922008-03-17 05:41:48 +0000487 }
Chris Lattner315123f2008-03-17 06:58:37 +0000488#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000489
Chris Lattner5a65b922008-03-17 05:41:48 +0000490 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000491 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000492 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000493 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000494
495 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000496 // chain and flag operands which copy the outgoing args into registers.
497 // The InFlag in necessary since all emited instructions must be
498 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000499 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000500 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
501 unsigned Reg = RegsToPass[i].first;
502 // Remap I0->I7 -> O0->O7.
503 if (Reg >= SP::I0 && Reg <= SP::I7)
504 Reg = Reg-SP::I0+SP::O0;
505
Dale Johannesen33c960f2009-02-04 20:06:27 +0000506 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000507 InFlag = Chain.getValue(1);
508 }
509
510 // If the callee is a GlobalAddress node (quite common, every direct call is)
511 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000512 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000513 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Devang Patel0d881da2010-07-06 22:08:15 +0000514 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), dl, MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000515 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000516 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000517
Venkatraman Govindaraju7d29ffb2011-01-12 03:18:21 +0000518 // Returns a chain & a flag for retval copy to use
519 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
520 SmallVector<SDValue, 8> Ops;
521 Ops.push_back(Chain);
522 Ops.push_back(Callee);
523 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
524 unsigned Reg = RegsToPass[i].first;
525 if (Reg >= SP::I0 && Reg <= SP::I7)
526 Reg = Reg-SP::I0+SP::O0;
527
528 Ops.push_back(DAG.getRegister(Reg, RegsToPass[i].second.getValueType()));
529 }
530 if (InFlag.getNode())
531 Ops.push_back(InFlag);
532
533 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000534 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000535
Chris Lattnere563bbc2008-10-11 22:08:30 +0000536 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
537 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000538 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000539
Chris Lattner98949a62008-03-17 06:01:07 +0000540 // Assign locations to each value returned by this call.
541 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000542 CCState RVInfo(CallConv, isVarArg, DAG.getTarget(),
Owen Andersone922c022009-07-22 00:24:57 +0000543 RVLocs, *DAG.getContext());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000544
Dan Gohman98ca4f22009-08-05 01:29:28 +0000545 RVInfo.AnalyzeCallResult(Ins, RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000546
Chris Lattner98949a62008-03-17 06:01:07 +0000547 // Copy all of the result registers out of their specified physreg.
548 for (unsigned i = 0; i != RVLocs.size(); ++i) {
549 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000550
Chris Lattner98949a62008-03-17 06:01:07 +0000551 // Remap I0->I7 -> O0->O7.
552 if (Reg >= SP::I0 && Reg <= SP::I7)
553 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000554
Dale Johannesen33c960f2009-02-04 20:06:27 +0000555 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000556 RVLocs[i].getValVT(), InFlag).getValue(1);
557 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000558 InVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000559 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000560
Dan Gohman98ca4f22009-08-05 01:29:28 +0000561 return Chain;
Chris Lattner5a65b922008-03-17 05:41:48 +0000562}
563
564
565
Chris Lattnerd23405e2008-03-17 03:21:36 +0000566//===----------------------------------------------------------------------===//
567// TargetLowering Implementation
568//===----------------------------------------------------------------------===//
569
570/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
571/// condition.
572static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
573 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000574 default: llvm_unreachable("Unknown integer condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000575 case ISD::SETEQ: return SPCC::ICC_E;
576 case ISD::SETNE: return SPCC::ICC_NE;
577 case ISD::SETLT: return SPCC::ICC_L;
578 case ISD::SETGT: return SPCC::ICC_G;
579 case ISD::SETLE: return SPCC::ICC_LE;
580 case ISD::SETGE: return SPCC::ICC_GE;
581 case ISD::SETULT: return SPCC::ICC_CS;
582 case ISD::SETULE: return SPCC::ICC_LEU;
583 case ISD::SETUGT: return SPCC::ICC_GU;
584 case ISD::SETUGE: return SPCC::ICC_CC;
585 }
586}
587
588/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
589/// FCC condition.
590static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
591 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 default: llvm_unreachable("Unknown fp condition code!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000593 case ISD::SETEQ:
594 case ISD::SETOEQ: return SPCC::FCC_E;
595 case ISD::SETNE:
596 case ISD::SETUNE: return SPCC::FCC_NE;
597 case ISD::SETLT:
598 case ISD::SETOLT: return SPCC::FCC_L;
599 case ISD::SETGT:
600 case ISD::SETOGT: return SPCC::FCC_G;
601 case ISD::SETLE:
602 case ISD::SETOLE: return SPCC::FCC_LE;
603 case ISD::SETGE:
604 case ISD::SETOGE: return SPCC::FCC_GE;
605 case ISD::SETULT: return SPCC::FCC_UL;
606 case ISD::SETULE: return SPCC::FCC_ULE;
607 case ISD::SETUGT: return SPCC::FCC_UG;
608 case ISD::SETUGE: return SPCC::FCC_UGE;
609 case ISD::SETUO: return SPCC::FCC_U;
610 case ISD::SETO: return SPCC::FCC_O;
611 case ISD::SETONE: return SPCC::FCC_LG;
612 case ISD::SETUEQ: return SPCC::FCC_UE;
613 }
614}
615
Chris Lattnerd23405e2008-03-17 03:21:36 +0000616SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
Chris Lattner5277b222009-08-08 20:43:12 +0000617 : TargetLowering(TM, new TargetLoweringObjectFileELF()) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000618
Chris Lattnerd23405e2008-03-17 03:21:36 +0000619 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
621 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
622 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000623
624 // Turn FP extload into load/fextend
Owen Anderson825b72b2009-08-11 20:47:22 +0000625 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000626 // Sparc doesn't have i1 sign extending load
Owen Anderson825b72b2009-08-11 20:47:22 +0000627 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000628 // Turn FP truncstore into trunc + store.
Owen Anderson825b72b2009-08-11 20:47:22 +0000629 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000630
631 // Custom legalize GlobalAddress nodes into LO/HI parts.
Owen Anderson825b72b2009-08-11 20:47:22 +0000632 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
633 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
634 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000635
Chris Lattnerd23405e2008-03-17 03:21:36 +0000636 // Sparc doesn't have sext_inreg, replace them with shl/sra
Owen Anderson825b72b2009-08-11 20:47:22 +0000637 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
638 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
639 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000640
641 // Sparc has no REM or DIVREM operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000642 setOperationAction(ISD::UREM, MVT::i32, Expand);
643 setOperationAction(ISD::SREM, MVT::i32, Expand);
644 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
645 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000646
647 // Custom expand fp<->sint
Owen Anderson825b72b2009-08-11 20:47:22 +0000648 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
649 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000650
651 // Expand fp<->uint
Owen Anderson825b72b2009-08-11 20:47:22 +0000652 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
653 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000654
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000655 setOperationAction(ISD::BITCAST, MVT::f32, Expand);
656 setOperationAction(ISD::BITCAST, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000657
Chris Lattnerd23405e2008-03-17 03:21:36 +0000658 // Sparc has no select or setcc: expand to SELECT_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000659 setOperationAction(ISD::SELECT, MVT::i32, Expand);
660 setOperationAction(ISD::SELECT, MVT::f32, Expand);
661 setOperationAction(ISD::SELECT, MVT::f64, Expand);
662 setOperationAction(ISD::SETCC, MVT::i32, Expand);
663 setOperationAction(ISD::SETCC, MVT::f32, Expand);
664 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000665
Chris Lattnerd23405e2008-03-17 03:21:36 +0000666 // Sparc doesn't have BRCOND either, it has BR_CC.
Owen Anderson825b72b2009-08-11 20:47:22 +0000667 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
668 setOperationAction(ISD::BRIND, MVT::Other, Expand);
669 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
670 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
671 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
672 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000673
Owen Anderson825b72b2009-08-11 20:47:22 +0000674 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
675 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
676 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000677
Chris Lattnerd23405e2008-03-17 03:21:36 +0000678 // SPARC has no intrinsics for these particular operations.
Owen Anderson825b72b2009-08-11 20:47:22 +0000679 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000680
Owen Anderson825b72b2009-08-11 20:47:22 +0000681 setOperationAction(ISD::FSIN , MVT::f64, Expand);
682 setOperationAction(ISD::FCOS , MVT::f64, Expand);
683 setOperationAction(ISD::FREM , MVT::f64, Expand);
684 setOperationAction(ISD::FSIN , MVT::f32, Expand);
685 setOperationAction(ISD::FCOS , MVT::f32, Expand);
686 setOperationAction(ISD::FREM , MVT::f32, Expand);
687 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
688 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
689 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
690 setOperationAction(ISD::ROTL , MVT::i32, Expand);
691 setOperationAction(ISD::ROTR , MVT::i32, Expand);
692 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
693 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
694 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
695 setOperationAction(ISD::FPOW , MVT::f64, Expand);
696 setOperationAction(ISD::FPOW , MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000697
Owen Anderson825b72b2009-08-11 20:47:22 +0000698 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
699 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
700 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000701
702 // FIXME: Sparc provides these multiplies, but we don't have them yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000703 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
704 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000705
Owen Anderson825b72b2009-08-11 20:47:22 +0000706 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000707
Chris Lattnerd23405e2008-03-17 03:21:36 +0000708 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
Owen Anderson825b72b2009-08-11 20:47:22 +0000709 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000710 // VAARG needs to be lowered to not do unaligned accesses for doubles.
Owen Anderson825b72b2009-08-11 20:47:22 +0000711 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000712
Chris Lattnerd23405e2008-03-17 03:21:36 +0000713 // Use the default implementation.
Owen Anderson825b72b2009-08-11 20:47:22 +0000714 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
715 setOperationAction(ISD::VAEND , MVT::Other, Expand);
716 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
717 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
718 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000719
720 // No debug info support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000721 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000722
Chris Lattnerd23405e2008-03-17 03:21:36 +0000723 setStackPointerRegisterToSaveRestore(SP::O6);
724
725 if (TM.getSubtarget<SparcSubtarget>().isV9())
Owen Anderson825b72b2009-08-11 20:47:22 +0000726 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000727
Chris Lattnerd23405e2008-03-17 03:21:36 +0000728 computeRegisterProperties();
729}
730
731const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
732 switch (Opcode) {
733 default: return 0;
734 case SPISD::CMPICC: return "SPISD::CMPICC";
735 case SPISD::CMPFCC: return "SPISD::CMPFCC";
736 case SPISD::BRICC: return "SPISD::BRICC";
737 case SPISD::BRFCC: return "SPISD::BRFCC";
738 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
739 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
740 case SPISD::Hi: return "SPISD::Hi";
741 case SPISD::Lo: return "SPISD::Lo";
742 case SPISD::FTOI: return "SPISD::FTOI";
743 case SPISD::ITOF: return "SPISD::ITOF";
744 case SPISD::CALL: return "SPISD::CALL";
745 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
746 }
747}
748
749/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
750/// be zero. Op is expected to be a target specific node. Used by DAG
751/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000752void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000753 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000754 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000755 APInt &KnownOne,
756 const SelectionDAG &DAG,
757 unsigned Depth) const {
758 APInt KnownZero2, KnownOne2;
759 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000760
Chris Lattnerd23405e2008-03-17 03:21:36 +0000761 switch (Op.getOpcode()) {
762 default: break;
763 case SPISD::SELECT_ICC:
764 case SPISD::SELECT_FCC:
765 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
766 Depth+1);
767 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
768 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000769 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
770 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
771
Chris Lattnerd23405e2008-03-17 03:21:36 +0000772 // Only known if known in both the LHS and RHS.
773 KnownOne &= KnownOne2;
774 KnownZero &= KnownZero2;
775 break;
776 }
777}
778
Chris Lattnerd23405e2008-03-17 03:21:36 +0000779// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
780// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000781static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000782 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000783 if (isa<ConstantSDNode>(RHS) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000784 cast<ConstantSDNode>(RHS)->isNullValue() &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000785 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000786 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
787 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
788 (LHS.getOpcode() == SPISD::SELECT_FCC &&
789 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
790 isa<ConstantSDNode>(LHS.getOperand(0)) &&
791 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmane368b462010-06-18 14:22:04 +0000792 cast<ConstantSDNode>(LHS.getOperand(0))->isOne() &&
793 cast<ConstantSDNode>(LHS.getOperand(1))->isNullValue()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000794 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000795 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000796 LHS = CMPCC.getOperand(0);
797 RHS = CMPCC.getOperand(1);
798 }
799}
800
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000801SDValue SparcTargetLowering::LowerGlobalAddress(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000802 SelectionDAG &DAG) const {
Dan Gohman46510a72010-04-15 01:51:59 +0000803 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dale Johannesende064702009-02-06 21:50:26 +0000804 // FIXME there isn't really any debug info here
805 DebugLoc dl = Op.getDebugLoc();
Devang Patel0d881da2010-07-06 22:08:15 +0000806 SDValue GA = DAG.getTargetGlobalAddress(GV, dl, MVT::i32);
Owen Anderson825b72b2009-08-11 20:47:22 +0000807 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, GA);
808 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, GA);
Chris Lattnerdb486a62009-09-15 17:46:24 +0000809
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000810 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000811 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000812
Chris Lattnerdb486a62009-09-15 17:46:24 +0000813 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
814 getPointerTy());
815 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000816 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000817 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000818 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000819 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000820}
821
Chris Lattnerdb486a62009-09-15 17:46:24 +0000822SDValue SparcTargetLowering::LowerConstantPool(SDValue Op,
Dan Gohmand858e902010-04-17 15:26:15 +0000823 SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000824 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +0000825 // FIXME there isn't really any debug info here
826 DebugLoc dl = Op.getDebugLoc();
Dan Gohman46510a72010-04-15 01:51:59 +0000827 const Constant *C = N->getConstVal();
Owen Anderson825b72b2009-08-11 20:47:22 +0000828 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
829 SDValue Hi = DAG.getNode(SPISD::Hi, dl, MVT::i32, CP);
830 SDValue Lo = DAG.getNode(SPISD::Lo, dl, MVT::i32, CP);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000831 if (getTargetMachine().getRelocationModel() != Reloc::PIC_)
Chris Lattnerdb486a62009-09-15 17:46:24 +0000832 return DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
833
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000834 SDValue GlobalBase = DAG.getNode(SPISD::GLOBAL_BASE_REG, dl,
Chris Lattnerdb486a62009-09-15 17:46:24 +0000835 getPointerTy());
836 SDValue RelAddr = DAG.getNode(ISD::ADD, dl, MVT::i32, Lo, Hi);
837 SDValue AbsAddr = DAG.getNode(ISD::ADD, dl, MVT::i32,
838 GlobalBase, RelAddr);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000839 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000840 AbsAddr, MachinePointerInfo(), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000841}
842
Dan Gohman475871a2008-07-27 21:46:04 +0000843static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000844 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000845 // Convert the fp value to integer in an FP register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000846 assert(Op.getValueType() == MVT::i32);
847 Op = DAG.getNode(SPISD::FTOI, dl, MVT::f32, Op.getOperand(0));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000848 return DAG.getNode(ISD::BITCAST, dl, MVT::i32, Op);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000849}
850
Dan Gohman475871a2008-07-27 21:46:04 +0000851static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000852 DebugLoc dl = Op.getDebugLoc();
Owen Anderson825b72b2009-08-11 20:47:22 +0000853 assert(Op.getOperand(0).getValueType() == MVT::i32);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000854 SDValue Tmp = DAG.getNode(ISD::BITCAST, dl, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000855 // Convert the int value to FP in an FP register.
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000856 return DAG.getNode(SPISD::ITOF, dl, Op.getValueType(), Tmp);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000857}
858
Dan Gohman475871a2008-07-27 21:46:04 +0000859static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
860 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000861 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000862 SDValue LHS = Op.getOperand(2);
863 SDValue RHS = Op.getOperand(3);
864 SDValue Dest = Op.getOperand(4);
Dale Johannesen3484c092009-02-05 22:07:54 +0000865 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000866 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000867
Chris Lattnerd23405e2008-03-17 03:21:36 +0000868 // If this is a br_cc of a "setcc", and if the setcc got lowered into
869 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
870 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000871
Chris Lattnerd23405e2008-03-17 03:21:36 +0000872 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000873 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000874 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000875 std::vector<EVT> VTs;
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 VTs.push_back(MVT::i32);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000877 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000878 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000879 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000880 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
881 Opc = SPISD::BRICC;
882 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000883 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000884 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
885 Opc = SPISD::BRFCC;
886 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 return DAG.getNode(Opc, dl, MVT::Other, Chain, Dest,
888 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000889}
890
Dan Gohman475871a2008-07-27 21:46:04 +0000891static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
892 SDValue LHS = Op.getOperand(0);
893 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000894 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SDValue TrueVal = Op.getOperand(2);
896 SDValue FalseVal = Op.getOperand(3);
Dale Johannesen3484c092009-02-05 22:07:54 +0000897 DebugLoc dl = Op.getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000898 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000899
Chris Lattnerd23405e2008-03-17 03:21:36 +0000900 // If this is a select_cc of a "setcc", and if the setcc got lowered into
901 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
902 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000903
Dan Gohman475871a2008-07-27 21:46:04 +0000904 SDValue CompareFlag;
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 if (LHS.getValueType() == MVT::i32) {
Owen Andersone50ed302009-08-10 22:56:29 +0000906 std::vector<EVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000907 VTs.push_back(LHS.getValueType()); // subcc returns a value
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000908 VTs.push_back(MVT::Glue);
Dan Gohman475871a2008-07-27 21:46:04 +0000909 SDValue Ops[2] = { LHS, RHS };
Dale Johannesen3484c092009-02-05 22:07:54 +0000910 CompareFlag = DAG.getNode(SPISD::CMPICC, dl, VTs, Ops, 2).getValue(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000911 Opc = SPISD::SELECT_ICC;
912 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
913 } else {
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000914 CompareFlag = DAG.getNode(SPISD::CMPFCC, dl, MVT::Glue, LHS, RHS);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000915 Opc = SPISD::SELECT_FCC;
916 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
917 }
Dale Johannesen3484c092009-02-05 22:07:54 +0000918 return DAG.getNode(Opc, dl, TrueVal.getValueType(), TrueVal, FalseVal,
Owen Anderson825b72b2009-08-11 20:47:22 +0000919 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000920}
921
Dan Gohman475871a2008-07-27 21:46:04 +0000922static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000923 const SparcTargetLowering &TLI) {
Dan Gohman1e93df62010-04-17 14:41:14 +0000924 MachineFunction &MF = DAG.getMachineFunction();
925 SparcMachineFunctionInfo *FuncInfo = MF.getInfo<SparcMachineFunctionInfo>();
926
Chris Lattnerd23405e2008-03-17 03:21:36 +0000927 // vastart just stores the address of the VarArgsFrameIndex slot into the
928 // memory location argument.
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000929 DebugLoc dl = Op.getDebugLoc();
Dan Gohman1e93df62010-04-17 14:41:14 +0000930 SDValue Offset =
931 DAG.getNode(ISD::ADD, dl, MVT::i32,
932 DAG.getRegister(SP::I6, MVT::i32),
933 DAG.getConstant(FuncInfo->getVarArgsFrameOffset(),
934 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000935 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Chris Lattner6229d0a2010-09-21 18:41:36 +0000936 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1),
937 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000938}
939
Dan Gohman475871a2008-07-27 21:46:04 +0000940static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000941 SDNode *Node = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000942 EVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000943 SDValue InChain = Node->getOperand(0);
944 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000945 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000946 DebugLoc dl = Node->getDebugLoc();
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000947 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr,
948 MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000949 // Increment the pointer, VAList, to the next vaarg
Owen Anderson825b72b2009-08-11 20:47:22 +0000950 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000951 DAG.getConstant(VT.getSizeInBits()/8,
Owen Anderson825b72b2009-08-11 20:47:22 +0000952 MVT::i32));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000953 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000954 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattner6229d0a2010-09-21 18:41:36 +0000955 VAListPtr, MachinePointerInfo(SV), false, false, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000956 // Load the actual argument out of the pointer VAList, unless this is an
957 // f64 load.
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 if (VT != MVT::f64)
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000959 return DAG.getLoad(VT, dl, InChain, VAList, MachinePointerInfo(),
960 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000961
Chris Lattnerd23405e2008-03-17 03:21:36 +0000962 // Otherwise, load it as i64, then do a bitconvert.
Chris Lattnerd1c24ed2010-09-21 06:44:06 +0000963 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, MachinePointerInfo(),
David Greene54a7aa82010-02-15 16:57:02 +0000964 false, false, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000965
Chris Lattnerd23405e2008-03-17 03:21:36 +0000966 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000967 SDValue Ops[2] = {
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000968 DAG.getNode(ISD::BITCAST, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000969 V.getValue(1)
970 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000971 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000972}
973
Dan Gohman475871a2008-07-27 21:46:04 +0000974static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
975 SDValue Chain = Op.getOperand(0); // Legalize the chain.
976 SDValue Size = Op.getOperand(1); // Legalize the size.
Dale Johannesena05dca42009-02-04 23:02:30 +0000977 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000978
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979 unsigned SPReg = SP::O6;
Owen Anderson825b72b2009-08-11 20:47:22 +0000980 SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, MVT::i32);
981 SDValue NewSP = DAG.getNode(ISD::SUB, dl, MVT::i32, SP, Size); // Value
Dale Johannesena05dca42009-02-04 23:02:30 +0000982 Chain = DAG.getCopyToReg(SP.getValue(1), dl, SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000983
Chris Lattnerd23405e2008-03-17 03:21:36 +0000984 // The resultant pointer is actually 16 words from the bottom of the stack,
985 // to provide a register spill area.
Owen Anderson825b72b2009-08-11 20:47:22 +0000986 SDValue NewVal = DAG.getNode(ISD::ADD, dl, MVT::i32, NewSP,
987 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000988 SDValue Ops[2] = { NewVal, Chain };
Dale Johannesena05dca42009-02-04 23:02:30 +0000989 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000990}
991
Chris Lattnerd23405e2008-03-17 03:21:36 +0000992
Dan Gohman475871a2008-07-27 21:46:04 +0000993SDValue SparcTargetLowering::
Dan Gohmand858e902010-04-17 15:26:15 +0000994LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000995 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000996 default: llvm_unreachable("Should not custom lower this!");
Chris Lattnerd23405e2008-03-17 03:21:36 +0000997 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000998 case ISD::RETURNADDR: return SDValue();
999 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001000 case ISD::GlobalTLSAddress:
Torok Edwinc23197a2009-07-14 16:55:14 +00001001 llvm_unreachable("TLS not implemented for Sparc.");
Chris Lattnerdb486a62009-09-15 17:46:24 +00001002 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
1003 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001004 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
1005 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
1006 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
1007 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
1008 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
1009 case ISD::VAARG: return LowerVAARG(Op, DAG);
1010 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +00001011 }
1012}
1013
1014MachineBasicBlock *
1015SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohmanaf1d8ca2010-05-01 00:01:06 +00001016 MachineBasicBlock *BB) const {
Chris Lattnerd23405e2008-03-17 03:21:36 +00001017 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
1018 unsigned BROpcode;
1019 unsigned CC;
Dale Johannesend552eee2009-02-13 02:31:35 +00001020 DebugLoc dl = MI->getDebugLoc();
Chris Lattnerd23405e2008-03-17 03:21:36 +00001021 // Figure out the conditional branch opcode to use for this select_cc.
1022 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001023 default: llvm_unreachable("Unknown SELECT_CC!");
Chris Lattnerd23405e2008-03-17 03:21:36 +00001024 case SP::SELECT_CC_Int_ICC:
1025 case SP::SELECT_CC_FP_ICC:
1026 case SP::SELECT_CC_DFP_ICC:
1027 BROpcode = SP::BCOND;
1028 break;
1029 case SP::SELECT_CC_Int_FCC:
1030 case SP::SELECT_CC_FP_FCC:
1031 case SP::SELECT_CC_DFP_FCC:
1032 BROpcode = SP::FBCOND;
1033 break;
1034 }
1035
1036 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +00001037
Chris Lattnerd23405e2008-03-17 03:21:36 +00001038 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
1039 // control-flow pattern. The incoming instruction knows the destination vreg
1040 // to set, the condition code register to branch on, the true/false values to
1041 // select between, and a branch opcode to use.
1042 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001043 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001044 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001045
Chris Lattnerd23405e2008-03-17 03:21:36 +00001046 // thisMBB:
1047 // ...
1048 // TrueVal = ...
1049 // [f]bCC copy1MBB
1050 // fallthrough --> copy0MBB
1051 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +00001052 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001053 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
1054 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Venkatraman Govindarajuf6612772010-12-28 20:39:17 +00001055 F->insert(It, copy0MBB);
1056 F->insert(It, sinkMBB);
Dan Gohman14152b42010-07-06 20:24:04 +00001057
1058 // Transfer the remainder of BB and its successor edges to sinkMBB.
1059 sinkMBB->splice(sinkMBB->begin(), BB,
1060 llvm::next(MachineBasicBlock::iterator(MI)),
1061 BB->end());
1062 sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
1063
1064 // Add the true and fallthrough blocks as its successors.
1065 BB->addSuccessor(copy0MBB);
1066 BB->addSuccessor(sinkMBB);
1067
Dale Johannesend552eee2009-02-13 02:31:35 +00001068 BuildMI(BB, dl, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001069
Chris Lattnerd23405e2008-03-17 03:21:36 +00001070 // copy0MBB:
1071 // %FalseValue = ...
1072 // # fallthrough to sinkMBB
1073 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +00001074
Chris Lattnerd23405e2008-03-17 03:21:36 +00001075 // Update machine-CFG edges
1076 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001077
Chris Lattnerd23405e2008-03-17 03:21:36 +00001078 // sinkMBB:
1079 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1080 // ...
1081 BB = sinkMBB;
Dan Gohman14152b42010-07-06 20:24:04 +00001082 BuildMI(*BB, BB->begin(), dl, TII.get(SP::PHI), MI->getOperand(0).getReg())
Chris Lattnerd23405e2008-03-17 03:21:36 +00001083 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
1084 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +00001085
Dan Gohman14152b42010-07-06 20:24:04 +00001086 MI->eraseFromParent(); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +00001087 return BB;
1088}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001089
1090//===----------------------------------------------------------------------===//
1091// Sparc Inline Assembly Support
1092//===----------------------------------------------------------------------===//
1093
1094/// getConstraintType - Given a constraint letter, return the type of
1095/// constraint it is for this target.
1096SparcTargetLowering::ConstraintType
1097SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
1098 if (Constraint.size() == 1) {
1099 switch (Constraint[0]) {
1100 default: break;
1101 case 'r': return C_RegisterClass;
1102 }
1103 }
1104
1105 return TargetLowering::getConstraintType(Constraint);
1106}
1107
1108std::pair<unsigned, const TargetRegisterClass*>
1109SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001110 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001111 if (Constraint.size() == 1) {
1112 switch (Constraint[0]) {
1113 case 'r':
1114 return std::make_pair(0U, SP::IntRegsRegisterClass);
1115 }
1116 }
1117
1118 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1119}
1120
1121std::vector<unsigned> SparcTargetLowering::
1122getRegClassForInlineAsmConstraint(const std::string &Constraint,
Owen Andersone50ed302009-08-10 22:56:29 +00001123 EVT VT) const {
Anton Korobeynikov0eefda12008-10-10 20:28:10 +00001124 if (Constraint.size() != 1)
1125 return std::vector<unsigned>();
1126
1127 switch (Constraint[0]) {
1128 default: break;
1129 case 'r':
1130 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1131 SP::L4, SP::L5, SP::L6, SP::L7,
1132 SP::I0, SP::I1, SP::I2, SP::I3,
1133 SP::I4, SP::I5,
1134 SP::O0, SP::O1, SP::O2, SP::O3,
1135 SP::O4, SP::O5, SP::O7, 0);
1136 }
1137
1138 return std::vector<unsigned>();
1139}
Dan Gohman6520e202008-10-18 02:06:02 +00001140
1141bool
1142SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1143 // The Sparc target isn't yet aware of offsets.
1144 return false;
1145}
Bill Wendling20c568f2009-06-30 22:38:32 +00001146
Bill Wendlingb4202b82009-07-01 18:50:55 +00001147/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001148unsigned SparcTargetLowering::getFunctionAlignment(const Function *) const {
Chris Lattnerdb486a62009-09-15 17:46:24 +00001149 return 2;
Bill Wendling20c568f2009-06-30 22:38:32 +00001150}