blob: 1ac49bd230fc3628cbabb82ecbbcc0a463f51c81 [file] [log] [blame]
Chris Lattnerd23405e2008-03-17 03:21:36 +00001//===-- SparcISelLowering.cpp - Sparc DAG Lowering Implementation ---------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the interfaces that Sparc uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "SparcISelLowering.h"
16#include "SparcTargetMachine.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000017#include "llvm/Function.h"
Chris Lattner5a65b922008-03-17 05:41:48 +000018#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
Anton Korobeynikov0eefda12008-10-10 20:28:10 +000024#include "llvm/ADT/VectorExtras.h"
Chris Lattnerd23405e2008-03-17 03:21:36 +000025using namespace llvm;
26
Chris Lattner5a65b922008-03-17 05:41:48 +000027
28//===----------------------------------------------------------------------===//
29// Calling Convention Implementation
30//===----------------------------------------------------------------------===//
31
32#include "SparcGenCallingConv.inc"
33
Dan Gohman475871a2008-07-27 21:46:04 +000034static SDValue LowerRET(SDValue Op, SelectionDAG &DAG) {
Chris Lattner5a65b922008-03-17 05:41:48 +000035 // CCValAssign - represent the assignment of the return value to locations.
36 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner98949a62008-03-17 06:01:07 +000037 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner5a65b922008-03-17 05:41:48 +000038 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Anton Korobeynikov53835702008-10-10 20:27:31 +000039
Chris Lattner5a65b922008-03-17 05:41:48 +000040 // CCState - Info about the registers and stack slot.
41 CCState CCInfo(CC, isVarArg, DAG.getTarget(), RVLocs);
Anton Korobeynikov53835702008-10-10 20:27:31 +000042
Chris Lattner5a65b922008-03-17 05:41:48 +000043 // Analize return values of ISD::RET
Gabor Greifba36cb52008-08-28 21:40:38 +000044 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +000045
Chris Lattner5a65b922008-03-17 05:41:48 +000046 // If this is the first return lowered for this function, add the regs to the
47 // liveout set for the function.
48 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
49 for (unsigned i = 0; i != RVLocs.size(); ++i)
50 if (RVLocs[i].isRegLoc())
51 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
52 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000053
Dan Gohman475871a2008-07-27 21:46:04 +000054 SDValue Chain = Op.getOperand(0);
55 SDValue Flag;
Chris Lattner5a65b922008-03-17 05:41:48 +000056
57 // Copy the result values into the output registers.
58 for (unsigned i = 0; i != RVLocs.size(); ++i) {
59 CCValAssign &VA = RVLocs[i];
60 assert(VA.isRegLoc() && "Can only return in registers!");
Anton Korobeynikov53835702008-10-10 20:27:31 +000061
Chris Lattner5a65b922008-03-17 05:41:48 +000062 // ISD::RET => ret chain, (regnum1,val1), ...
63 // So i*2+1 index only the regnums.
64 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
Anton Korobeynikov53835702008-10-10 20:27:31 +000065
Chris Lattner5a65b922008-03-17 05:41:48 +000066 // Guarantee that all emitted copies are stuck together with flags.
67 Flag = Chain.getValue(1);
68 }
Anton Korobeynikov53835702008-10-10 20:27:31 +000069
Gabor Greifba36cb52008-08-28 21:40:38 +000070 if (Flag.getNode())
Chris Lattner5a65b922008-03-17 05:41:48 +000071 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain, Flag);
72 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain);
73}
74
75/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
76/// either one or two GPRs, including FP values. TODO: we should pass FP values
77/// in FP registers for fastcc functions.
Dan Gohmana44b6742008-06-30 20:31:15 +000078void
79SparcTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
Dale Johannesen7d2ad622009-01-30 23:10:59 +000080 SmallVectorImpl<SDValue> &ArgValues,
81 DebugLoc dl) {
Chris Lattner5a65b922008-03-17 05:41:48 +000082 MachineFunction &MF = DAG.getMachineFunction();
83 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikov53835702008-10-10 20:27:31 +000084
Chris Lattner5a65b922008-03-17 05:41:48 +000085 static const unsigned ArgRegs[] = {
86 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
87 };
Anton Korobeynikov53835702008-10-10 20:27:31 +000088
Chris Lattner5a65b922008-03-17 05:41:48 +000089 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
90 unsigned ArgOffset = 68;
Anton Korobeynikov53835702008-10-10 20:27:31 +000091
Dan Gohman475871a2008-07-27 21:46:04 +000092 SDValue Root = DAG.getRoot();
93 std::vector<SDValue> OutChains;
Chris Lattner5a65b922008-03-17 05:41:48 +000094
95 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
Duncan Sands83ec4b62008-06-06 12:08:01 +000096 MVT ObjectVT = getValueType(I->getType());
Anton Korobeynikov53835702008-10-10 20:27:31 +000097
Duncan Sands83ec4b62008-06-06 12:08:01 +000098 switch (ObjectVT.getSimpleVT()) {
Chris Lattner5a65b922008-03-17 05:41:48 +000099 default: assert(0 && "Unhandled argument type!");
100 case MVT::i1:
101 case MVT::i8:
102 case MVT::i16:
103 case MVT::i32:
104 if (I->use_empty()) { // Argument is dead.
105 if (CurArgReg < ArgRegEnd) ++CurArgReg;
Dale Johannesen39355f92009-02-04 02:34:38 +0000106 ArgValues.push_back(DAG.getNode(ISD::UNDEF, dl, ObjectVT));
Chris Lattner5a65b922008-03-17 05:41:48 +0000107 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
108 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
109 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Dale Johannesen39355f92009-02-04 02:34:38 +0000110 SDValue Arg = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000111 if (ObjectVT != MVT::i32) {
112 unsigned AssertOp = ISD::AssertSext;
Dale Johannesen39355f92009-02-04 02:34:38 +0000113 Arg = DAG.getNode(AssertOp, dl, MVT::i32, Arg,
Chris Lattner5a65b922008-03-17 05:41:48 +0000114 DAG.getValueType(ObjectVT));
Dale Johannesen39355f92009-02-04 02:34:38 +0000115 Arg = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000116 }
117 ArgValues.push_back(Arg);
118 } else {
119 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000120 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
121 SDValue Load;
Chris Lattner5a65b922008-03-17 05:41:48 +0000122 if (ObjectVT == MVT::i32) {
Dale Johannesen39355f92009-02-04 02:34:38 +0000123 Load = DAG.getLoad(MVT::i32, dl, Root, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000124 } else {
125 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
126
127 // Sparc is big endian, so add an offset based on the ObjectVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000128 unsigned Offset = 4-std::max(1U, ObjectVT.getSizeInBits()/8);
Dale Johannesen39355f92009-02-04 02:34:38 +0000129 FIPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, FIPtr,
Chris Lattner5a65b922008-03-17 05:41:48 +0000130 DAG.getConstant(Offset, MVT::i32));
Dale Johannesen39355f92009-02-04 02:34:38 +0000131 Load = DAG.getExtLoad(LoadOp, dl, MVT::i32, Root, FIPtr,
Chris Lattner5a65b922008-03-17 05:41:48 +0000132 NULL, 0, ObjectVT);
Dale Johannesen39355f92009-02-04 02:34:38 +0000133 Load = DAG.getNode(ISD::TRUNCATE, dl, ObjectVT, Load);
Chris Lattner5a65b922008-03-17 05:41:48 +0000134 }
135 ArgValues.push_back(Load);
136 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000137
Chris Lattner5a65b922008-03-17 05:41:48 +0000138 ArgOffset += 4;
139 break;
140 case MVT::f32:
141 if (I->use_empty()) { // Argument is dead.
142 if (CurArgReg < ArgRegEnd) ++CurArgReg;
Dale Johannesen39355f92009-02-04 02:34:38 +0000143 ArgValues.push_back(DAG.getNode(ISD::UNDEF, dl, ObjectVT));
Chris Lattner5a65b922008-03-17 05:41:48 +0000144 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
145 // FP value is passed in an integer register.
146 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
147 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
Dale Johannesen39355f92009-02-04 02:34:38 +0000148 SDValue Arg = DAG.getCopyFromReg(Root, dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000149
Dale Johannesen39355f92009-02-04 02:34:38 +0000150 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Arg);
Chris Lattner5a65b922008-03-17 05:41:48 +0000151 ArgValues.push_back(Arg);
152 } else {
153 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000154 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dale Johannesen39355f92009-02-04 02:34:38 +0000155 SDValue Load = DAG.getLoad(MVT::f32, dl, Root, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000156 ArgValues.push_back(Load);
157 }
158 ArgOffset += 4;
159 break;
160
161 case MVT::i64:
162 case MVT::f64:
163 if (I->use_empty()) { // Argument is dead.
164 if (CurArgReg < ArgRegEnd) ++CurArgReg;
165 if (CurArgReg < ArgRegEnd) ++CurArgReg;
Dale Johannesen39355f92009-02-04 02:34:38 +0000166 ArgValues.push_back(DAG.getNode(ISD::UNDEF, dl, ObjectVT));
Chris Lattner5a65b922008-03-17 05:41:48 +0000167 } else {
Dan Gohman475871a2008-07-27 21:46:04 +0000168 SDValue HiVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000169 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
170 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
171 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
Dale Johannesen39355f92009-02-04 02:34:38 +0000172 HiVal = DAG.getCopyFromReg(Root, dl, VRegHi, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000173 } else {
174 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000175 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dale Johannesen39355f92009-02-04 02:34:38 +0000176 HiVal = DAG.getLoad(MVT::i32, dl, Root, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000177 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000178
Dan Gohman475871a2008-07-27 21:46:04 +0000179 SDValue LoVal;
Chris Lattner5a65b922008-03-17 05:41:48 +0000180 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
181 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
182 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
Dale Johannesen39355f92009-02-04 02:34:38 +0000183 LoVal = DAG.getCopyFromReg(Root, dl, VRegLo, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000184 } else {
185 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
Dan Gohman475871a2008-07-27 21:46:04 +0000186 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Dale Johannesen39355f92009-02-04 02:34:38 +0000187 LoVal = DAG.getLoad(MVT::i32, dl, Root, FIPtr, NULL, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000188 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000189
Chris Lattner5a65b922008-03-17 05:41:48 +0000190 // Compose the two halves together into an i64 unit.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000191 SDValue WholeValue =
Dale Johannesen39355f92009-02-04 02:34:38 +0000192 DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, LoVal, HiVal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000193
Chris Lattner5a65b922008-03-17 05:41:48 +0000194 // If we want a double, do a bit convert.
195 if (ObjectVT == MVT::f64)
Dale Johannesen39355f92009-02-04 02:34:38 +0000196 WholeValue = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, WholeValue);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000197
Chris Lattner5a65b922008-03-17 05:41:48 +0000198 ArgValues.push_back(WholeValue);
199 }
200 ArgOffset += 8;
201 break;
202 }
203 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000204
Chris Lattner5a65b922008-03-17 05:41:48 +0000205 // Store remaining ArgRegs to the stack if this is a varargs function.
206 if (F.isVarArg()) {
207 // Remember the vararg offset for the va_start implementation.
208 VarArgsFrameOffset = ArgOffset;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000209
Chris Lattner5a65b922008-03-17 05:41:48 +0000210 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
211 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
212 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
Dale Johannesen39355f92009-02-04 02:34:38 +0000213 SDValue Arg = DAG.getCopyFromReg(DAG.getRoot(), dl, VReg, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000214
215 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
Dan Gohman475871a2008-07-27 21:46:04 +0000216 SDValue FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000217
Dale Johannesen39355f92009-02-04 02:34:38 +0000218 OutChains.push_back(DAG.getStore(DAG.getRoot(), dl, Arg, FIPtr, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000219 ArgOffset += 4;
220 }
221 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000222
Chris Lattner5a65b922008-03-17 05:41:48 +0000223 if (!OutChains.empty())
Dale Johannesen39355f92009-02-04 02:34:38 +0000224 DAG.setRoot(DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner5a65b922008-03-17 05:41:48 +0000225 &OutChains[0], OutChains.size()));
Chris Lattner5a65b922008-03-17 05:41:48 +0000226}
227
Dan Gohman475871a2008-07-27 21:46:04 +0000228static SDValue LowerCALL(SDValue Op, SelectionDAG &DAG) {
Dan Gohman095cc292008-09-13 01:54:27 +0000229 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
230 unsigned CallingConv = TheCall->getCallingConv();
231 SDValue Chain = TheCall->getChain();
232 SDValue Callee = TheCall->getCallee();
233 bool isVarArg = TheCall->isVarArg();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000234 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattner98949a62008-03-17 06:01:07 +0000235
Chris Lattner315123f2008-03-17 06:58:37 +0000236#if 0
237 // Analyze operands of the call, assigning locations to each operand.
238 SmallVector<CCValAssign, 16> ArgLocs;
239 CCState CCInfo(CallingConv, isVarArg, DAG.getTarget(), ArgLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000240 CCInfo.AnalyzeCallOperands(Op.getNode(), CC_Sparc32);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000241
Chris Lattner315123f2008-03-17 06:58:37 +0000242 // Get the size of the outgoing arguments stack space requirement.
243 unsigned ArgsSize = CCInfo.getNextStackOffset();
244 // FIXME: We can't use this until f64 is known to take two GPRs.
245#else
246 (void)CC_Sparc32;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000247
Chris Lattner5a65b922008-03-17 05:41:48 +0000248 // Count the size of the outgoing arguments.
249 unsigned ArgsSize = 0;
Dan Gohman095cc292008-09-13 01:54:27 +0000250 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; ++i) {
251 switch (TheCall->getArg(i).getValueType().getSimpleVT()) {
Chris Lattner315123f2008-03-17 06:58:37 +0000252 default: assert(0 && "Unknown value type!");
253 case MVT::i1:
254 case MVT::i8:
255 case MVT::i16:
256 case MVT::i32:
257 case MVT::f32:
258 ArgsSize += 4;
259 break;
260 case MVT::i64:
261 case MVT::f64:
262 ArgsSize += 8;
263 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000264 }
265 }
266 if (ArgsSize > 4*6)
267 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
268 else
269 ArgsSize = 0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000270#endif
271
Chris Lattner5a65b922008-03-17 05:41:48 +0000272 // Keep stack frames 8-byte aligned.
273 ArgsSize = (ArgsSize+7) & ~7;
274
Chris Lattnere563bbc2008-10-11 22:08:30 +0000275 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize, true));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000276
Dan Gohman475871a2008-07-27 21:46:04 +0000277 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
278 SmallVector<SDValue, 8> MemOpChains;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000279
Chris Lattner315123f2008-03-17 06:58:37 +0000280#if 0
281 // Walk the register/memloc assignments, inserting copies/loads.
282 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
283 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikov53835702008-10-10 20:27:31 +0000284
Chris Lattner315123f2008-03-17 06:58:37 +0000285 // Arguments start after the 5 first operands of ISD::CALL
Dan Gohman095cc292008-09-13 01:54:27 +0000286 SDValue Arg = TheCall->getArg(i);
Chris Lattner315123f2008-03-17 06:58:37 +0000287
288 // Promote the value if needed.
289 switch (VA.getLocInfo()) {
290 default: assert(0 && "Unknown loc info!");
291 case CCValAssign::Full: break;
292 case CCValAssign::SExt:
293 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
294 break;
295 case CCValAssign::ZExt:
296 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
297 break;
298 case CCValAssign::AExt:
299 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
300 break;
301 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000302
303 // Arguments that can be passed on register must be kept at
Chris Lattner315123f2008-03-17 06:58:37 +0000304 // RegsToPass vector
305 if (VA.isRegLoc()) {
306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
307 continue;
308 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000309
Chris Lattner315123f2008-03-17 06:58:37 +0000310 assert(VA.isMemLoc());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000311
Chris Lattner315123f2008-03-17 06:58:37 +0000312 // Create a store off the stack pointer for this argument.
Dan Gohman475871a2008-07-27 21:46:04 +0000313 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner315123f2008-03-17 06:58:37 +0000314 // FIXME: VERIFY THAT 68 IS RIGHT.
Dan Gohman475871a2008-07-27 21:46:04 +0000315 SDValue PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
Chris Lattner315123f2008-03-17 06:58:37 +0000316 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
317 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
318 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000319
320#else
Chris Lattner315123f2008-03-17 06:58:37 +0000321 static const unsigned ArgRegs[] = {
322 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
323 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000324 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000325
Dan Gohman095cc292008-09-13 01:54:27 +0000326 for (unsigned i = 0, e = TheCall->getNumArgs(); i != e; ++i) {
327 SDValue Val = TheCall->getArg(i);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000328 MVT ObjectVT = Val.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +0000329 SDValue ValToStore(0, 0);
Chris Lattner5a65b922008-03-17 05:41:48 +0000330 unsigned ObjSize;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000331 switch (ObjectVT.getSimpleVT()) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000332 default: assert(0 && "Unhandled argument type!");
Chris Lattner5a65b922008-03-17 05:41:48 +0000333 case MVT::i32:
334 ObjSize = 4;
335
Chris Lattner315123f2008-03-17 06:58:37 +0000336 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000337 ValToStore = Val;
338 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000339 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000340 }
341 break;
342 case MVT::f32:
343 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000344 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000345 ValToStore = Val;
346 } else {
347 // Convert this to a FP value in an int reg.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000348 Val = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000349 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000350 }
351 break;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000352 case MVT::f64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000353 ObjSize = 8;
Duncan Sands8c0f2442008-12-12 08:05:40 +0000354 if (RegsToPass.size() >= 6) {
355 ValToStore = Val; // Whole thing is passed in memory.
356 break;
357 }
358
359 // Break into top and bottom parts by storing to the stack and loading
360 // out the parts as integers. Top part goes in a reg.
361 SDValue StackPtr = DAG.CreateStackTemporary(MVT::f64, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000362 SDValue Store = DAG.getStore(DAG.getEntryNode(), dl,
363 Val, StackPtr, NULL, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000364 // Sparc is big-endian, so the high part comes first.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000365 SDValue Hi = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000366 // Increment the pointer to the other half.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000367 StackPtr = DAG.getNode(ISD::ADD, dl, StackPtr.getValueType(), StackPtr,
Duncan Sands8c0f2442008-12-12 08:05:40 +0000368 DAG.getIntPtrConstant(4));
369 // Load the low part.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000370 SDValue Lo = DAG.getLoad(MVT::i32, dl, Store, StackPtr, NULL, 0, 0);
Duncan Sands8c0f2442008-12-12 08:05:40 +0000371
372 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
373
374 if (RegsToPass.size() >= 6) {
375 ValToStore = Lo;
376 ArgOffset += 4;
377 ObjSize = 4;
378 } else {
379 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
380 }
381 break;
382 }
383 case MVT::i64: {
Chris Lattner5a65b922008-03-17 05:41:48 +0000384 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000385 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000386 ValToStore = Val; // Whole thing is passed in memory.
387 break;
388 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000389
Chris Lattner5a65b922008-03-17 05:41:48 +0000390 // Split the value into top and bottom part. Top part goes in a reg.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000391 SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
Chris Lattner5a65b922008-03-17 05:41:48 +0000392 DAG.getConstant(1, MVT::i32));
Dale Johannesen33c960f2009-02-04 20:06:27 +0000393 SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Val,
Chris Lattner5a65b922008-03-17 05:41:48 +0000394 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000395 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Anton Korobeynikov53835702008-10-10 20:27:31 +0000396
Chris Lattner315123f2008-03-17 06:58:37 +0000397 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000398 ValToStore = Lo;
399 ArgOffset += 4;
400 ObjSize = 4;
401 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000402 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000403 }
404 break;
405 }
Duncan Sands8c0f2442008-12-12 08:05:40 +0000406 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000407
Gabor Greifba36cb52008-08-28 21:40:38 +0000408 if (ValToStore.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +0000409 SDValue StackPtr = DAG.getRegister(SP::O6, MVT::i32);
410 SDValue PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
Dale Johannesen33c960f2009-02-04 20:06:27 +0000411 PtrOff = DAG.getNode(ISD::ADD, dl, MVT::i32, StackPtr, PtrOff);
412 MemOpChains.push_back(DAG.getStore(Chain, dl, ValToStore,
413 PtrOff, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000414 }
415 ArgOffset += ObjSize;
416 }
Chris Lattner315123f2008-03-17 06:58:37 +0000417#endif
Anton Korobeynikov53835702008-10-10 20:27:31 +0000418
Chris Lattner5a65b922008-03-17 05:41:48 +0000419 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000420 if (!MemOpChains.empty())
Dale Johannesen33c960f2009-02-04 20:06:27 +0000421 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattner315123f2008-03-17 06:58:37 +0000422 &MemOpChains[0], MemOpChains.size());
Anton Korobeynikov53835702008-10-10 20:27:31 +0000423
424 // Build a sequence of copy-to-reg nodes chained together with token
Chris Lattner315123f2008-03-17 06:58:37 +0000425 // chain and flag operands which copy the outgoing args into registers.
426 // The InFlag in necessary since all emited instructions must be
427 // stuck together.
Dan Gohman475871a2008-07-27 21:46:04 +0000428 SDValue InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000429 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
430 unsigned Reg = RegsToPass[i].first;
431 // Remap I0->I7 -> O0->O7.
432 if (Reg >= SP::I0 && Reg <= SP::I7)
433 Reg = Reg-SP::I0+SP::O0;
434
Dale Johannesen33c960f2009-02-04 20:06:27 +0000435 Chain = DAG.getCopyToReg(Chain, dl, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000436 InFlag = Chain.getValue(1);
437 }
438
439 // If the callee is a GlobalAddress node (quite common, every direct call is)
440 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Bill Wendling056292f2008-09-16 21:48:12 +0000441 // Likewise ExternalSymbol -> TargetExternalSymbol.
Chris Lattner5a65b922008-03-17 05:41:48 +0000442 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
443 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
Bill Wendling056292f2008-09-16 21:48:12 +0000444 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
445 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000446
Duncan Sands83ec4b62008-06-06 12:08:01 +0000447 std::vector<MVT> NodeTys;
Chris Lattner5a65b922008-03-17 05:41:48 +0000448 NodeTys.push_back(MVT::Other); // Returns a chain
449 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Dan Gohman475871a2008-07-27 21:46:04 +0000450 SDValue Ops[] = { Chain, Callee, InFlag };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000451 Chain = DAG.getNode(SPISD::CALL, dl, NodeTys, Ops, InFlag.getNode() ? 3 : 2);
Chris Lattner5a65b922008-03-17 05:41:48 +0000452 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000453
Chris Lattnere563bbc2008-10-11 22:08:30 +0000454 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(ArgsSize, true),
455 DAG.getIntPtrConstant(0, true), InFlag);
Chris Lattner98949a62008-03-17 06:01:07 +0000456 InFlag = Chain.getValue(1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000457
Chris Lattner98949a62008-03-17 06:01:07 +0000458 // Assign locations to each value returned by this call.
459 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner315123f2008-03-17 06:58:37 +0000460 CCState RVInfo(CallingConv, isVarArg, DAG.getTarget(), RVLocs);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000461
Dan Gohman095cc292008-09-13 01:54:27 +0000462 RVInfo.AnalyzeCallResult(TheCall, RetCC_Sparc32);
Dan Gohman475871a2008-07-27 21:46:04 +0000463 SmallVector<SDValue, 8> ResultVals;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000464
Chris Lattner98949a62008-03-17 06:01:07 +0000465 // Copy all of the result registers out of their specified physreg.
466 for (unsigned i = 0; i != RVLocs.size(); ++i) {
467 unsigned Reg = RVLocs[i].getLocReg();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000468
Chris Lattner98949a62008-03-17 06:01:07 +0000469 // Remap I0->I7 -> O0->O7.
470 if (Reg >= SP::I0 && Reg <= SP::I7)
471 Reg = Reg-SP::I0+SP::O0;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000472
Dale Johannesen33c960f2009-02-04 20:06:27 +0000473 Chain = DAG.getCopyFromReg(Chain, dl, Reg,
Chris Lattner98949a62008-03-17 06:01:07 +0000474 RVLocs[i].getValVT(), InFlag).getValue(1);
475 InFlag = Chain.getValue(2);
476 ResultVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000477 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000478
Chris Lattner98949a62008-03-17 06:01:07 +0000479 ResultVals.push_back(Chain);
Duncan Sands4bdcb612008-07-02 17:40:58 +0000480
Chris Lattner98949a62008-03-17 06:01:07 +0000481 // Merge everything together with a MERGE_VALUES node.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000482 return DAG.getNode(ISD::MERGE_VALUES, dl,
483 TheCall->getVTList(), &ResultVals[0],
Duncan Sandsaaffa052008-12-01 11:41:29 +0000484 ResultVals.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000485}
486
487
488
Chris Lattnerd23405e2008-03-17 03:21:36 +0000489//===----------------------------------------------------------------------===//
490// TargetLowering Implementation
491//===----------------------------------------------------------------------===//
492
493/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
494/// condition.
495static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
496 switch (CC) {
497 default: assert(0 && "Unknown integer condition code!");
498 case ISD::SETEQ: return SPCC::ICC_E;
499 case ISD::SETNE: return SPCC::ICC_NE;
500 case ISD::SETLT: return SPCC::ICC_L;
501 case ISD::SETGT: return SPCC::ICC_G;
502 case ISD::SETLE: return SPCC::ICC_LE;
503 case ISD::SETGE: return SPCC::ICC_GE;
504 case ISD::SETULT: return SPCC::ICC_CS;
505 case ISD::SETULE: return SPCC::ICC_LEU;
506 case ISD::SETUGT: return SPCC::ICC_GU;
507 case ISD::SETUGE: return SPCC::ICC_CC;
508 }
509}
510
511/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
512/// FCC condition.
513static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
514 switch (CC) {
515 default: assert(0 && "Unknown fp condition code!");
516 case ISD::SETEQ:
517 case ISD::SETOEQ: return SPCC::FCC_E;
518 case ISD::SETNE:
519 case ISD::SETUNE: return SPCC::FCC_NE;
520 case ISD::SETLT:
521 case ISD::SETOLT: return SPCC::FCC_L;
522 case ISD::SETGT:
523 case ISD::SETOGT: return SPCC::FCC_G;
524 case ISD::SETLE:
525 case ISD::SETOLE: return SPCC::FCC_LE;
526 case ISD::SETGE:
527 case ISD::SETOGE: return SPCC::FCC_GE;
528 case ISD::SETULT: return SPCC::FCC_UL;
529 case ISD::SETULE: return SPCC::FCC_ULE;
530 case ISD::SETUGT: return SPCC::FCC_UG;
531 case ISD::SETUGE: return SPCC::FCC_UGE;
532 case ISD::SETUO: return SPCC::FCC_U;
533 case ISD::SETO: return SPCC::FCC_O;
534 case ISD::SETONE: return SPCC::FCC_LG;
535 case ISD::SETUEQ: return SPCC::FCC_UE;
536 }
537}
538
539
540SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
541 : TargetLowering(TM) {
Anton Korobeynikov53835702008-10-10 20:27:31 +0000542
Chris Lattnerd23405e2008-03-17 03:21:36 +0000543 // Set up the register classes.
544 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
545 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
546 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
547
548 // Turn FP extload into load/fextend
Evan Cheng03294662008-10-14 21:26:46 +0000549 setLoadExtAction(ISD::EXTLOAD, MVT::f32, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000550 // Sparc doesn't have i1 sign extending load
Evan Cheng03294662008-10-14 21:26:46 +0000551 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000552 // Turn FP truncstore into trunc + store.
553 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
554
555 // Custom legalize GlobalAddress nodes into LO/HI parts.
556 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
557 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
558 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000559
Chris Lattnerd23405e2008-03-17 03:21:36 +0000560 // Sparc doesn't have sext_inreg, replace them with shl/sra
561 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
562 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
563 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
564
565 // Sparc has no REM or DIVREM operations.
566 setOperationAction(ISD::UREM, MVT::i32, Expand);
567 setOperationAction(ISD::SREM, MVT::i32, Expand);
568 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
569 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
570
571 // Custom expand fp<->sint
572 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
573 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
574
575 // Expand fp<->uint
576 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
577 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000578
Chris Lattnerd23405e2008-03-17 03:21:36 +0000579 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
580 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000581
Chris Lattnerd23405e2008-03-17 03:21:36 +0000582 // Sparc has no select or setcc: expand to SELECT_CC.
583 setOperationAction(ISD::SELECT, MVT::i32, Expand);
584 setOperationAction(ISD::SELECT, MVT::f32, Expand);
585 setOperationAction(ISD::SELECT, MVT::f64, Expand);
586 setOperationAction(ISD::SETCC, MVT::i32, Expand);
587 setOperationAction(ISD::SETCC, MVT::f32, Expand);
588 setOperationAction(ISD::SETCC, MVT::f64, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000589
Chris Lattnerd23405e2008-03-17 03:21:36 +0000590 // Sparc doesn't have BRCOND either, it has BR_CC.
591 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
592 setOperationAction(ISD::BRIND, MVT::Other, Expand);
593 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
594 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
595 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
596 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000597
Chris Lattnerd23405e2008-03-17 03:21:36 +0000598 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
599 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
600 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000601
Chris Lattnerd23405e2008-03-17 03:21:36 +0000602 // SPARC has no intrinsics for these particular operations.
Chris Lattnerd23405e2008-03-17 03:21:36 +0000603 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
604
605 setOperationAction(ISD::FSIN , MVT::f64, Expand);
606 setOperationAction(ISD::FCOS , MVT::f64, Expand);
607 setOperationAction(ISD::FREM , MVT::f64, Expand);
608 setOperationAction(ISD::FSIN , MVT::f32, Expand);
609 setOperationAction(ISD::FCOS , MVT::f32, Expand);
610 setOperationAction(ISD::FREM , MVT::f32, Expand);
611 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
612 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
613 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
614 setOperationAction(ISD::ROTL , MVT::i32, Expand);
615 setOperationAction(ISD::ROTR , MVT::i32, Expand);
616 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
617 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
618 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
619 setOperationAction(ISD::FPOW , MVT::f64, Expand);
620 setOperationAction(ISD::FPOW , MVT::f32, Expand);
621
622 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
623 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
624 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
625
626 // FIXME: Sparc provides these multiplies, but we don't have them yet.
627 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov4b58b6a2008-10-10 20:29:31 +0000628 setOperationAction(ISD::SMUL_LOHI, MVT::i32, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000629
Chris Lattnerd23405e2008-03-17 03:21:36 +0000630 // We don't have line number support yet.
Dan Gohman7f460202008-06-30 20:59:49 +0000631 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000632 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000633 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
634 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000635
636 // RET must be custom lowered, to meet ABI requirements
637 setOperationAction(ISD::RET , MVT::Other, Custom);
638
639 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
640 setOperationAction(ISD::VASTART , MVT::Other, Custom);
641 // VAARG needs to be lowered to not do unaligned accesses for doubles.
642 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000643
Chris Lattnerd23405e2008-03-17 03:21:36 +0000644 // Use the default implementation.
645 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
646 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000647 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000648 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
649 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
650
651 // No debug info support yet.
Dan Gohman7f460202008-06-30 20:59:49 +0000652 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Dan Gohman44066042008-07-01 00:05:16 +0000653 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
654 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000655 setOperationAction(ISD::DECLARE, MVT::Other, Expand);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000656
Chris Lattnerd23405e2008-03-17 03:21:36 +0000657 setStackPointerRegisterToSaveRestore(SP::O6);
658
659 if (TM.getSubtarget<SparcSubtarget>().isV9())
660 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000661
Chris Lattnerd23405e2008-03-17 03:21:36 +0000662 computeRegisterProperties();
663}
664
665const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
666 switch (Opcode) {
667 default: return 0;
668 case SPISD::CMPICC: return "SPISD::CMPICC";
669 case SPISD::CMPFCC: return "SPISD::CMPFCC";
670 case SPISD::BRICC: return "SPISD::BRICC";
671 case SPISD::BRFCC: return "SPISD::BRFCC";
672 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
673 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
674 case SPISD::Hi: return "SPISD::Hi";
675 case SPISD::Lo: return "SPISD::Lo";
676 case SPISD::FTOI: return "SPISD::FTOI";
677 case SPISD::ITOF: return "SPISD::ITOF";
678 case SPISD::CALL: return "SPISD::CALL";
679 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
680 }
681}
682
683/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
684/// be zero. Op is expected to be a target specific node. Used by DAG
685/// combiner.
Dan Gohman475871a2008-07-27 21:46:04 +0000686void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000687 const APInt &Mask,
Anton Korobeynikov53835702008-10-10 20:27:31 +0000688 APInt &KnownZero,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000689 APInt &KnownOne,
690 const SelectionDAG &DAG,
691 unsigned Depth) const {
692 APInt KnownZero2, KnownOne2;
693 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000694
Chris Lattnerd23405e2008-03-17 03:21:36 +0000695 switch (Op.getOpcode()) {
696 default: break;
697 case SPISD::SELECT_ICC:
698 case SPISD::SELECT_FCC:
699 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
700 Depth+1);
701 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
702 Depth+1);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000703 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
704 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
705
Chris Lattnerd23405e2008-03-17 03:21:36 +0000706 // Only known if known in both the LHS and RHS.
707 KnownOne &= KnownOne2;
708 KnownZero &= KnownZero2;
709 break;
710 }
711}
712
Chris Lattnerd23405e2008-03-17 03:21:36 +0000713// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
714// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
Dan Gohman475871a2008-07-27 21:46:04 +0000715static void LookThroughSetCC(SDValue &LHS, SDValue &RHS,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000716 ISD::CondCode CC, unsigned &SPCC) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000717 if (isa<ConstantSDNode>(RHS) &&
718 cast<ConstantSDNode>(RHS)->getZExtValue() == 0 &&
Anton Korobeynikov53835702008-10-10 20:27:31 +0000719 CC == ISD::SETNE &&
Chris Lattnerd23405e2008-03-17 03:21:36 +0000720 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
721 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
722 (LHS.getOpcode() == SPISD::SELECT_FCC &&
723 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
724 isa<ConstantSDNode>(LHS.getOperand(0)) &&
725 isa<ConstantSDNode>(LHS.getOperand(1)) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000726 cast<ConstantSDNode>(LHS.getOperand(0))->getZExtValue() == 1 &&
727 cast<ConstantSDNode>(LHS.getOperand(1))->getZExtValue() == 0) {
Dan Gohman475871a2008-07-27 21:46:04 +0000728 SDValue CMPCC = LHS.getOperand(3);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000729 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getZExtValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000730 LHS = CMPCC.getOperand(0);
731 RHS = CMPCC.getOperand(1);
732 }
733}
734
Dan Gohman475871a2008-07-27 21:46:04 +0000735static SDValue LowerGLOBALADDRESS(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000736 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +0000737 SDValue GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
738 SDValue Hi = DAG.getNode(SPISD::Hi, MVT::i32, GA);
739 SDValue Lo = DAG.getNode(SPISD::Lo, MVT::i32, GA);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000740 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
741}
742
Dan Gohman475871a2008-07-27 21:46:04 +0000743static SDValue LowerCONSTANTPOOL(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000744 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
745 Constant *C = N->getConstVal();
Dan Gohman475871a2008-07-27 21:46:04 +0000746 SDValue CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
747 SDValue Hi = DAG.getNode(SPISD::Hi, MVT::i32, CP);
748 SDValue Lo = DAG.getNode(SPISD::Lo, MVT::i32, CP);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000749 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
750}
751
Dan Gohman475871a2008-07-27 21:46:04 +0000752static SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000753 // Convert the fp value to integer in an FP register.
754 assert(Op.getValueType() == MVT::i32);
755 Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
756 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
757}
758
Dan Gohman475871a2008-07-27 21:46:04 +0000759static SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000760 assert(Op.getOperand(0).getValueType() == MVT::i32);
Dan Gohman475871a2008-07-27 21:46:04 +0000761 SDValue Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattnerd23405e2008-03-17 03:21:36 +0000762 // Convert the int value to FP in an FP register.
763 return DAG.getNode(SPISD::ITOF, Op.getValueType(), Tmp);
764}
765
Dan Gohman475871a2008-07-27 21:46:04 +0000766static SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
767 SDValue Chain = Op.getOperand(0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000768 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000769 SDValue LHS = Op.getOperand(2);
770 SDValue RHS = Op.getOperand(3);
771 SDValue Dest = Op.getOperand(4);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000772 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000773
Chris Lattnerd23405e2008-03-17 03:21:36 +0000774 // If this is a br_cc of a "setcc", and if the setcc got lowered into
775 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
776 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000777
Chris Lattnerd23405e2008-03-17 03:21:36 +0000778 // Get the condition flag.
Dan Gohman475871a2008-07-27 21:46:04 +0000779 SDValue CompareFlag;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000780 if (LHS.getValueType() == MVT::i32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000781 std::vector<MVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000782 VTs.push_back(MVT::i32);
783 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000784 SDValue Ops[2] = { LHS, RHS };
Chris Lattnerd23405e2008-03-17 03:21:36 +0000785 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
786 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
787 Opc = SPISD::BRICC;
788 } else {
789 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
790 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
791 Opc = SPISD::BRFCC;
792 }
793 return DAG.getNode(Opc, MVT::Other, Chain, Dest,
794 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
795}
796
Dan Gohman475871a2008-07-27 21:46:04 +0000797static SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
798 SDValue LHS = Op.getOperand(0);
799 SDValue RHS = Op.getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000800 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Dan Gohman475871a2008-07-27 21:46:04 +0000801 SDValue TrueVal = Op.getOperand(2);
802 SDValue FalseVal = Op.getOperand(3);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000803 unsigned Opc, SPCC = ~0U;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000804
Chris Lattnerd23405e2008-03-17 03:21:36 +0000805 // If this is a select_cc of a "setcc", and if the setcc got lowered into
806 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
807 LookThroughSetCC(LHS, RHS, CC, SPCC);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000808
Dan Gohman475871a2008-07-27 21:46:04 +0000809 SDValue CompareFlag;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000810 if (LHS.getValueType() == MVT::i32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811 std::vector<MVT> VTs;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000812 VTs.push_back(LHS.getValueType()); // subcc returns a value
813 VTs.push_back(MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +0000814 SDValue Ops[2] = { LHS, RHS };
Chris Lattnerd23405e2008-03-17 03:21:36 +0000815 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
816 Opc = SPISD::SELECT_ICC;
817 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
818 } else {
819 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
820 Opc = SPISD::SELECT_FCC;
821 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
822 }
Anton Korobeynikov53835702008-10-10 20:27:31 +0000823 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000824 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
825}
826
Dan Gohman475871a2008-07-27 21:46:04 +0000827static SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000828 SparcTargetLowering &TLI) {
829 // vastart just stores the address of the VarArgsFrameIndex slot into the
830 // memory location argument.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000831 DebugLoc dl = Op.getNode()->getDebugLoc();
832 SDValue Offset = DAG.getNode(ISD::ADD, dl, MVT::i32,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000833 DAG.getRegister(SP::I6, MVT::i32),
834 DAG.getConstant(TLI.getVarArgsFrameOffset(),
835 MVT::i32));
836 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000837 return DAG.getStore(Op.getOperand(0), dl, Offset, Op.getOperand(1), SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000838}
839
Dan Gohman475871a2008-07-27 21:46:04 +0000840static SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000841 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842 MVT VT = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +0000843 SDValue InChain = Node->getOperand(0);
844 SDValue VAListPtr = Node->getOperand(1);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000845 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dale Johannesen33c960f2009-02-04 20:06:27 +0000846 DebugLoc dl = Node->getDebugLoc();
847 SDValue VAList = DAG.getLoad(MVT::i32, dl, InChain, VAListPtr, SV, 0);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000848 // Increment the pointer, VAList, to the next vaarg
Dale Johannesen33c960f2009-02-04 20:06:27 +0000849 SDValue NextPtr = DAG.getNode(ISD::ADD, dl, MVT::i32, VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +0000850 DAG.getConstant(VT.getSizeInBits()/8,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000851 MVT::i32));
852 // Store the incremented VAList to the legalized pointer
Dale Johannesen33c960f2009-02-04 20:06:27 +0000853 InChain = DAG.getStore(VAList.getValue(1), dl, NextPtr,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000854 VAListPtr, SV, 0);
855 // Load the actual argument out of the pointer VAList, unless this is an
856 // f64 load.
857 if (VT != MVT::f64)
Dale Johannesen33c960f2009-02-04 20:06:27 +0000858 return DAG.getLoad(VT, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000859
Chris Lattnerd23405e2008-03-17 03:21:36 +0000860 // Otherwise, load it as i64, then do a bitconvert.
Dale Johannesen33c960f2009-02-04 20:06:27 +0000861 SDValue V = DAG.getLoad(MVT::i64, dl, InChain, VAList, NULL, 0);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000862
Chris Lattnerd23405e2008-03-17 03:21:36 +0000863 // Bit-Convert the value to f64.
Dan Gohman475871a2008-07-27 21:46:04 +0000864 SDValue Ops[2] = {
Dale Johannesen33c960f2009-02-04 20:06:27 +0000865 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f64, V),
Chris Lattnerd23405e2008-03-17 03:21:36 +0000866 V.getValue(1)
867 };
Dale Johannesen33c960f2009-02-04 20:06:27 +0000868 return DAG.getMergeValues(Ops, 2, dl);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000869}
870
Dan Gohman475871a2008-07-27 21:46:04 +0000871static SDValue LowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) {
872 SDValue Chain = Op.getOperand(0); // Legalize the chain.
873 SDValue Size = Op.getOperand(1); // Legalize the size.
Anton Korobeynikov53835702008-10-10 20:27:31 +0000874
Chris Lattnerd23405e2008-03-17 03:21:36 +0000875 unsigned SPReg = SP::O6;
Dan Gohman475871a2008-07-27 21:46:04 +0000876 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, MVT::i32);
877 SDValue NewSP = DAG.getNode(ISD::SUB, MVT::i32, SP, Size); // Value
Chris Lattnerd23405e2008-03-17 03:21:36 +0000878 Chain = DAG.getCopyToReg(SP.getValue(1), SPReg, NewSP); // Output chain
Anton Korobeynikov53835702008-10-10 20:27:31 +0000879
Chris Lattnerd23405e2008-03-17 03:21:36 +0000880 // The resultant pointer is actually 16 words from the bottom of the stack,
881 // to provide a register spill area.
Dan Gohman475871a2008-07-27 21:46:04 +0000882 SDValue NewVal = DAG.getNode(ISD::ADD, MVT::i32, NewSP,
Chris Lattnerd23405e2008-03-17 03:21:36 +0000883 DAG.getConstant(96, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +0000884 SDValue Ops[2] = { NewVal, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000885 return DAG.getMergeValues(Ops, 2);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000886}
887
Chris Lattnerd23405e2008-03-17 03:21:36 +0000888
Dan Gohman475871a2008-07-27 21:46:04 +0000889SDValue SparcTargetLowering::
890LowerOperation(SDValue Op, SelectionDAG &DAG) {
Chris Lattnerd23405e2008-03-17 03:21:36 +0000891 switch (Op.getOpcode()) {
892 default: assert(0 && "Should not custom lower this!");
893 // Frame & Return address. Currently unimplemented
Dan Gohman475871a2008-07-27 21:46:04 +0000894 case ISD::RETURNADDR: return SDValue();
895 case ISD::FRAMEADDR: return SDValue();
Chris Lattnerd23405e2008-03-17 03:21:36 +0000896 case ISD::GlobalTLSAddress:
897 assert(0 && "TLS not implemented for Sparc.");
898 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
899 case ISD::ConstantPool: return LowerCONSTANTPOOL(Op, DAG);
900 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
901 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
902 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
903 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
904 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
905 case ISD::VAARG: return LowerVAARG(Op, DAG);
906 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattner98949a62008-03-17 06:01:07 +0000907 case ISD::CALL: return LowerCALL(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000908 case ISD::RET: return LowerRET(Op, DAG);
909 }
910}
911
912MachineBasicBlock *
913SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
914 MachineBasicBlock *BB) {
915 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
916 unsigned BROpcode;
917 unsigned CC;
918 // Figure out the conditional branch opcode to use for this select_cc.
919 switch (MI->getOpcode()) {
920 default: assert(0 && "Unknown SELECT_CC!");
921 case SP::SELECT_CC_Int_ICC:
922 case SP::SELECT_CC_FP_ICC:
923 case SP::SELECT_CC_DFP_ICC:
924 BROpcode = SP::BCOND;
925 break;
926 case SP::SELECT_CC_Int_FCC:
927 case SP::SELECT_CC_FP_FCC:
928 case SP::SELECT_CC_DFP_FCC:
929 BROpcode = SP::FBCOND;
930 break;
931 }
932
933 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
Anton Korobeynikov53835702008-10-10 20:27:31 +0000934
Chris Lattnerd23405e2008-03-17 03:21:36 +0000935 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
936 // control-flow pattern. The incoming instruction knows the destination vreg
937 // to set, the condition code register to branch on, the true/false values to
938 // select between, and a branch opcode to use.
939 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000940 MachineFunction::iterator It = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000941 ++It;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000942
Chris Lattnerd23405e2008-03-17 03:21:36 +0000943 // thisMBB:
944 // ...
945 // TrueVal = ...
946 // [f]bCC copy1MBB
947 // fallthrough --> copy0MBB
948 MachineBasicBlock *thisMBB = BB;
Chris Lattnerd23405e2008-03-17 03:21:36 +0000949 MachineFunction *F = BB->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000950 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
951 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
952 BuildMI(BB, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
953 F->insert(It, copy0MBB);
954 F->insert(It, sinkMBB);
Dan Gohman0011dc42008-06-21 20:21:19 +0000955 // Update machine-CFG edges by transferring all successors of the current
Chris Lattnerd23405e2008-03-17 03:21:36 +0000956 // block to the new block which will contain the Phi node for the select.
Dan Gohman0011dc42008-06-21 20:21:19 +0000957 sinkMBB->transferSuccessors(BB);
958 // Next, add the true and fallthrough blocks as its successors.
Chris Lattnerd23405e2008-03-17 03:21:36 +0000959 BB->addSuccessor(copy0MBB);
960 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000961
Chris Lattnerd23405e2008-03-17 03:21:36 +0000962 // copy0MBB:
963 // %FalseValue = ...
964 // # fallthrough to sinkMBB
965 BB = copy0MBB;
Anton Korobeynikov53835702008-10-10 20:27:31 +0000966
Chris Lattnerd23405e2008-03-17 03:21:36 +0000967 // Update machine-CFG edges
968 BB->addSuccessor(sinkMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000969
Chris Lattnerd23405e2008-03-17 03:21:36 +0000970 // sinkMBB:
971 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
972 // ...
973 BB = sinkMBB;
974 BuildMI(BB, TII.get(SP::PHI), MI->getOperand(0).getReg())
975 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
976 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
Anton Korobeynikov53835702008-10-10 20:27:31 +0000977
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000978 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Chris Lattnerd23405e2008-03-17 03:21:36 +0000979 return BB;
980}
Anton Korobeynikov0eefda12008-10-10 20:28:10 +0000981
982//===----------------------------------------------------------------------===//
983// Sparc Inline Assembly Support
984//===----------------------------------------------------------------------===//
985
986/// getConstraintType - Given a constraint letter, return the type of
987/// constraint it is for this target.
988SparcTargetLowering::ConstraintType
989SparcTargetLowering::getConstraintType(const std::string &Constraint) const {
990 if (Constraint.size() == 1) {
991 switch (Constraint[0]) {
992 default: break;
993 case 'r': return C_RegisterClass;
994 }
995 }
996
997 return TargetLowering::getConstraintType(Constraint);
998}
999
1000std::pair<unsigned, const TargetRegisterClass*>
1001SparcTargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
1002 MVT VT) const {
1003 if (Constraint.size() == 1) {
1004 switch (Constraint[0]) {
1005 case 'r':
1006 return std::make_pair(0U, SP::IntRegsRegisterClass);
1007 }
1008 }
1009
1010 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
1011}
1012
1013std::vector<unsigned> SparcTargetLowering::
1014getRegClassForInlineAsmConstraint(const std::string &Constraint,
1015 MVT VT) const {
1016 if (Constraint.size() != 1)
1017 return std::vector<unsigned>();
1018
1019 switch (Constraint[0]) {
1020 default: break;
1021 case 'r':
1022 return make_vector<unsigned>(SP::L0, SP::L1, SP::L2, SP::L3,
1023 SP::L4, SP::L5, SP::L6, SP::L7,
1024 SP::I0, SP::I1, SP::I2, SP::I3,
1025 SP::I4, SP::I5,
1026 SP::O0, SP::O1, SP::O2, SP::O3,
1027 SP::O4, SP::O5, SP::O7, 0);
1028 }
1029
1030 return std::vector<unsigned>();
1031}
Dan Gohman6520e202008-10-18 02:06:02 +00001032
1033bool
1034SparcTargetLowering::isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const {
1035 // The Sparc target isn't yet aware of offsets.
1036 return false;
1037}