blob: 1d4fe0bc8cba877f198eeaf00d8bb99b17f936b8 [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"
24using namespace llvm;
25
Chris Lattner5a65b922008-03-17 05:41:48 +000026
27//===----------------------------------------------------------------------===//
28// Calling Convention Implementation
29//===----------------------------------------------------------------------===//
30
31#include "SparcGenCallingConv.inc"
32
33static SDOperand LowerRET(SDOperand Op, SelectionDAG &DAG) {
34 // CCValAssign - represent the assignment of the return value to locations.
35 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner98949a62008-03-17 06:01:07 +000036 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner5a65b922008-03-17 05:41:48 +000037 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
38
39 // CCState - Info about the registers and stack slot.
40 CCState CCInfo(CC, isVarArg, DAG.getTarget(), RVLocs);
41
42 // Analize return values of ISD::RET
43 CCInfo.AnalyzeReturn(Op.Val, RetCC_Sparc32);
44
45 // If this is the first return lowered for this function, add the regs to the
46 // liveout set for the function.
47 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
48 for (unsigned i = 0; i != RVLocs.size(); ++i)
49 if (RVLocs[i].isRegLoc())
50 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
51 }
52
53 SDOperand Chain = Op.getOperand(0);
54 SDOperand Flag;
55
56 // Copy the result values into the output registers.
57 for (unsigned i = 0; i != RVLocs.size(); ++i) {
58 CCValAssign &VA = RVLocs[i];
59 assert(VA.isRegLoc() && "Can only return in registers!");
60
61 // ISD::RET => ret chain, (regnum1,val1), ...
62 // So i*2+1 index only the regnums.
63 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1), Flag);
64
65 // Guarantee that all emitted copies are stuck together with flags.
66 Flag = Chain.getValue(1);
67 }
68
69 if (Flag.Val)
70 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain, Flag);
71 return DAG.getNode(SPISD::RET_FLAG, MVT::Other, Chain);
72}
73
74/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
75/// either one or two GPRs, including FP values. TODO: we should pass FP values
76/// in FP registers for fastcc functions.
77std::vector<SDOperand>
78SparcTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
79 MachineFunction &MF = DAG.getMachineFunction();
80 MachineRegisterInfo &RegInfo = MF.getRegInfo();
81 std::vector<SDOperand> ArgValues;
82
83 static const unsigned ArgRegs[] = {
84 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
85 };
86
87 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
88 unsigned ArgOffset = 68;
89
90 SDOperand Root = DAG.getRoot();
91 std::vector<SDOperand> OutChains;
92
93 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
94 MVT::ValueType ObjectVT = getValueType(I->getType());
95
96 switch (ObjectVT) {
97 default: assert(0 && "Unhandled argument type!");
98 case MVT::i1:
99 case MVT::i8:
100 case MVT::i16:
101 case MVT::i32:
102 if (I->use_empty()) { // Argument is dead.
103 if (CurArgReg < ArgRegEnd) ++CurArgReg;
104 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
105 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
106 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
107 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
108 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
109 if (ObjectVT != MVT::i32) {
110 unsigned AssertOp = ISD::AssertSext;
111 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
112 DAG.getValueType(ObjectVT));
113 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
114 }
115 ArgValues.push_back(Arg);
116 } else {
117 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
118 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
119 SDOperand Load;
120 if (ObjectVT == MVT::i32) {
121 Load = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
122 } else {
123 ISD::LoadExtType LoadOp = ISD::SEXTLOAD;
124
125 // Sparc is big endian, so add an offset based on the ObjectVT.
126 unsigned Offset = 4-std::max(1U, MVT::getSizeInBits(ObjectVT)/8);
127 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
128 DAG.getConstant(Offset, MVT::i32));
129 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
130 NULL, 0, ObjectVT);
131 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
132 }
133 ArgValues.push_back(Load);
134 }
135
136 ArgOffset += 4;
137 break;
138 case MVT::f32:
139 if (I->use_empty()) { // Argument is dead.
140 if (CurArgReg < ArgRegEnd) ++CurArgReg;
141 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
142 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
143 // FP value is passed in an integer register.
144 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
145 MF.getRegInfo().addLiveIn(*CurArgReg++, VReg);
146 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
147
148 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
149 ArgValues.push_back(Arg);
150 } else {
151 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
152 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
153 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, NULL, 0);
154 ArgValues.push_back(Load);
155 }
156 ArgOffset += 4;
157 break;
158
159 case MVT::i64:
160 case MVT::f64:
161 if (I->use_empty()) { // Argument is dead.
162 if (CurArgReg < ArgRegEnd) ++CurArgReg;
163 if (CurArgReg < ArgRegEnd) ++CurArgReg;
164 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
Chris Lattner5a65b922008-03-17 05:41:48 +0000165 } else {
166 SDOperand HiVal;
167 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
168 unsigned VRegHi = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
169 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegHi);
170 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
171 } else {
172 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
173 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
174 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
175 }
176
177 SDOperand LoVal;
178 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
179 unsigned VRegLo = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
180 MF.getRegInfo().addLiveIn(*CurArgReg++, VRegLo);
181 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
182 } else {
183 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
184 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
185 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, NULL, 0);
186 }
187
188 // Compose the two halves together into an i64 unit.
189 SDOperand WholeValue =
190 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
191
192 // If we want a double, do a bit convert.
193 if (ObjectVT == MVT::f64)
194 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
195
196 ArgValues.push_back(WholeValue);
197 }
198 ArgOffset += 8;
199 break;
200 }
201 }
202
203 // Store remaining ArgRegs to the stack if this is a varargs function.
204 if (F.isVarArg()) {
205 // Remember the vararg offset for the va_start implementation.
206 VarArgsFrameOffset = ArgOffset;
207
208 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
209 unsigned VReg = RegInfo.createVirtualRegister(&SP::IntRegsRegClass);
210 MF.getRegInfo().addLiveIn(*CurArgReg, VReg);
211 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
212
213 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
214 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
215
216 OutChains.push_back(DAG.getStore(DAG.getRoot(), Arg, FIPtr, NULL, 0));
217 ArgOffset += 4;
218 }
219 }
220
221 if (!OutChains.empty())
222 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other,
223 &OutChains[0], OutChains.size()));
224
225 return ArgValues;
226}
227
Chris Lattner98949a62008-03-17 06:01:07 +0000228static SDOperand LowerCALL(SDOperand Op, SelectionDAG &DAG) {
229 unsigned CallingConv = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
230 SDOperand Chain = Op.getOperand(0);
231 SDOperand Callee = Op.getOperand(4);
232 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
233
Chris Lattner315123f2008-03-17 06:58:37 +0000234#if 0
235 // Analyze operands of the call, assigning locations to each operand.
236 SmallVector<CCValAssign, 16> ArgLocs;
237 CCState CCInfo(CallingConv, isVarArg, DAG.getTarget(), ArgLocs);
238 CCInfo.AnalyzeCallOperands(Op.Val, CC_Sparc32);
239
240 // Get the size of the outgoing arguments stack space requirement.
241 unsigned ArgsSize = CCInfo.getNextStackOffset();
242 // FIXME: We can't use this until f64 is known to take two GPRs.
243#else
244 (void)CC_Sparc32;
245
Chris Lattner5a65b922008-03-17 05:41:48 +0000246 // Count the size of the outgoing arguments.
247 unsigned ArgsSize = 0;
Chris Lattner98949a62008-03-17 06:01:07 +0000248 for (unsigned i = 5, e = Op.getNumOperands(); i != e; i += 2) {
249 switch (Op.getOperand(i).getValueType()) {
Chris Lattner315123f2008-03-17 06:58:37 +0000250 default: assert(0 && "Unknown value type!");
251 case MVT::i1:
252 case MVT::i8:
253 case MVT::i16:
254 case MVT::i32:
255 case MVT::f32:
256 ArgsSize += 4;
257 break;
258 case MVT::i64:
259 case MVT::f64:
260 ArgsSize += 8;
261 break;
Chris Lattner5a65b922008-03-17 05:41:48 +0000262 }
263 }
264 if (ArgsSize > 4*6)
265 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
266 else
267 ArgsSize = 0;
Chris Lattner315123f2008-03-17 06:58:37 +0000268#endif
269
Chris Lattner5a65b922008-03-17 05:41:48 +0000270 // Keep stack frames 8-byte aligned.
271 ArgsSize = (ArgsSize+7) & ~7;
272
Chris Lattner315123f2008-03-17 06:58:37 +0000273 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(ArgsSize));
Chris Lattner5a65b922008-03-17 05:41:48 +0000274
Chris Lattner315123f2008-03-17 06:58:37 +0000275 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
276 SmallVector<SDOperand, 8> MemOpChains;
277
278#if 0
279 // Walk the register/memloc assignments, inserting copies/loads.
280 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
281 CCValAssign &VA = ArgLocs[i];
282
283 // Arguments start after the 5 first operands of ISD::CALL
284 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
285
286 // Promote the value if needed.
287 switch (VA.getLocInfo()) {
288 default: assert(0 && "Unknown loc info!");
289 case CCValAssign::Full: break;
290 case CCValAssign::SExt:
291 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
292 break;
293 case CCValAssign::ZExt:
294 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
295 break;
296 case CCValAssign::AExt:
297 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
298 break;
299 }
300
301 // Arguments that can be passed on register must be kept at
302 // RegsToPass vector
303 if (VA.isRegLoc()) {
304 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
305 continue;
306 }
307
308 assert(VA.isMemLoc());
309
310 // Create a store off the stack pointer for this argument.
311 SDOperand StackPtr = DAG.getRegister(SP::O6, MVT::i32);
312 // FIXME: VERIFY THAT 68 IS RIGHT.
313 SDOperand PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset()+68);
314 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
315 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
316 }
317
318#else
319 static const unsigned ArgRegs[] = {
320 SP::I0, SP::I1, SP::I2, SP::I3, SP::I4, SP::I5
321 };
Chris Lattner5a65b922008-03-17 05:41:48 +0000322 unsigned ArgOffset = 68;
Chris Lattner315123f2008-03-17 06:58:37 +0000323
Chris Lattner98949a62008-03-17 06:01:07 +0000324 for (unsigned i = 5, e = Op.getNumOperands(); i != e; i += 2) {
325 SDOperand Val = Op.getOperand(i);
Chris Lattner5a65b922008-03-17 05:41:48 +0000326 MVT::ValueType ObjectVT = Val.getValueType();
327 SDOperand ValToStore(0, 0);
328 unsigned ObjSize;
329 switch (ObjectVT) {
330 default: assert(0 && "Unhandled argument type!");
Chris Lattner5a65b922008-03-17 05:41:48 +0000331 case MVT::i32:
332 ObjSize = 4;
333
Chris Lattner315123f2008-03-17 06:58:37 +0000334 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000335 ValToStore = Val;
336 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000337 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000338 }
339 break;
340 case MVT::f32:
341 ObjSize = 4;
Chris Lattner315123f2008-03-17 06:58:37 +0000342 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000343 ValToStore = Val;
344 } else {
345 // Convert this to a FP value in an int reg.
346 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner315123f2008-03-17 06:58:37 +0000347 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Val));
Chris Lattner5a65b922008-03-17 05:41:48 +0000348 }
349 break;
350 case MVT::f64:
351 ObjSize = 8;
Chris Lattner5a65b922008-03-17 05:41:48 +0000352 // Otherwise, convert this to a FP value in int regs.
353 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
354 // FALL THROUGH
355 case MVT::i64:
356 ObjSize = 8;
Chris Lattner315123f2008-03-17 06:58:37 +0000357 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000358 ValToStore = Val; // Whole thing is passed in memory.
359 break;
360 }
361
362 // Split the value into top and bottom part. Top part goes in a reg.
Chris Lattner98949a62008-03-17 06:01:07 +0000363 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
Chris Lattner5a65b922008-03-17 05:41:48 +0000364 DAG.getConstant(1, MVT::i32));
Chris Lattner98949a62008-03-17 06:01:07 +0000365 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
Chris Lattner5a65b922008-03-17 05:41:48 +0000366 DAG.getConstant(0, MVT::i32));
Chris Lattner315123f2008-03-17 06:58:37 +0000367 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Hi));
Chris Lattner5a65b922008-03-17 05:41:48 +0000368
Chris Lattner315123f2008-03-17 06:58:37 +0000369 if (RegsToPass.size() >= 6) {
Chris Lattner5a65b922008-03-17 05:41:48 +0000370 ValToStore = Lo;
371 ArgOffset += 4;
372 ObjSize = 4;
373 } else {
Chris Lattner315123f2008-03-17 06:58:37 +0000374 RegsToPass.push_back(std::make_pair(ArgRegs[RegsToPass.size()], Lo));
Chris Lattner5a65b922008-03-17 05:41:48 +0000375 }
376 break;
377 }
378
379 if (ValToStore.Val) {
Chris Lattner315123f2008-03-17 06:58:37 +0000380 SDOperand StackPtr = DAG.getRegister(SP::O6, MVT::i32);
Chris Lattner98949a62008-03-17 06:01:07 +0000381 SDOperand PtrOff = DAG.getConstant(ArgOffset, MVT::i32);
Chris Lattner5a65b922008-03-17 05:41:48 +0000382 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
Chris Lattner315123f2008-03-17 06:58:37 +0000383 MemOpChains.push_back(DAG.getStore(Chain, ValToStore, PtrOff, NULL, 0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000384 }
385 ArgOffset += ObjSize;
386 }
Chris Lattner315123f2008-03-17 06:58:37 +0000387#endif
Chris Lattner5a65b922008-03-17 05:41:48 +0000388
389 // Emit all stores, make sure the occur before any copies into physregs.
Chris Lattner315123f2008-03-17 06:58:37 +0000390 if (!MemOpChains.empty())
391 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
392 &MemOpChains[0], MemOpChains.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000393
Chris Lattner315123f2008-03-17 06:58:37 +0000394 // Build a sequence of copy-to-reg nodes chained together with token
395 // chain and flag operands which copy the outgoing args into registers.
396 // The InFlag in necessary since all emited instructions must be
397 // stuck together.
Chris Lattner5a65b922008-03-17 05:41:48 +0000398 SDOperand InFlag;
Chris Lattner315123f2008-03-17 06:58:37 +0000399 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
400 unsigned Reg = RegsToPass[i].first;
401 // Remap I0->I7 -> O0->O7.
402 if (Reg >= SP::I0 && Reg <= SP::I7)
403 Reg = Reg-SP::I0+SP::O0;
404
405 Chain = DAG.getCopyToReg(Chain, Reg, RegsToPass[i].second, InFlag);
Chris Lattner5a65b922008-03-17 05:41:48 +0000406 InFlag = Chain.getValue(1);
407 }
408
409 // If the callee is a GlobalAddress node (quite common, every direct call is)
410 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
411 // Likewise ExternalSymbol -> TargetExternalSymbol.
412 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
413 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
414 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
415 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i32);
416
417 std::vector<MVT::ValueType> NodeTys;
418 NodeTys.push_back(MVT::Other); // Returns a chain
419 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
420 SDOperand Ops[] = { Chain, Callee, InFlag };
421 Chain = DAG.getNode(SPISD::CALL, NodeTys, Ops, InFlag.Val ? 3 : 2);
422 InFlag = Chain.getValue(1);
423
Chris Lattner98949a62008-03-17 06:01:07 +0000424 Chain = DAG.getCALLSEQ_END(Chain,
425 DAG.getConstant(ArgsSize, MVT::i32),
426 DAG.getConstant(0, MVT::i32), InFlag);
427 InFlag = Chain.getValue(1);
Chris Lattner5a65b922008-03-17 05:41:48 +0000428
Chris Lattner98949a62008-03-17 06:01:07 +0000429 // Assign locations to each value returned by this call.
430 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner315123f2008-03-17 06:58:37 +0000431 CCState RVInfo(CallingConv, isVarArg, DAG.getTarget(), RVLocs);
Chris Lattner98949a62008-03-17 06:01:07 +0000432
Chris Lattner315123f2008-03-17 06:58:37 +0000433 RVInfo.AnalyzeCallResult(Op.Val, RetCC_Sparc32);
Chris Lattner98949a62008-03-17 06:01:07 +0000434 SmallVector<SDOperand, 8> ResultVals;
435
436 // Copy all of the result registers out of their specified physreg.
437 for (unsigned i = 0; i != RVLocs.size(); ++i) {
438 unsigned Reg = RVLocs[i].getLocReg();
439
440 // Remap I0->I7 -> O0->O7.
441 if (Reg >= SP::I0 && Reg <= SP::I7)
442 Reg = Reg-SP::I0+SP::O0;
443
444 Chain = DAG.getCopyFromReg(Chain, Reg,
445 RVLocs[i].getValVT(), InFlag).getValue(1);
446 InFlag = Chain.getValue(2);
447 ResultVals.push_back(Chain.getValue(0));
Chris Lattner5a65b922008-03-17 05:41:48 +0000448 }
449
Chris Lattner98949a62008-03-17 06:01:07 +0000450 ResultVals.push_back(Chain);
451
452 // Merge everything together with a MERGE_VALUES node.
453 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
454 &ResultVals[0], ResultVals.size());
Chris Lattner5a65b922008-03-17 05:41:48 +0000455}
456
457
458
Chris Lattnerd23405e2008-03-17 03:21:36 +0000459//===----------------------------------------------------------------------===//
460// TargetLowering Implementation
461//===----------------------------------------------------------------------===//
462
463/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
464/// condition.
465static SPCC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
466 switch (CC) {
467 default: assert(0 && "Unknown integer condition code!");
468 case ISD::SETEQ: return SPCC::ICC_E;
469 case ISD::SETNE: return SPCC::ICC_NE;
470 case ISD::SETLT: return SPCC::ICC_L;
471 case ISD::SETGT: return SPCC::ICC_G;
472 case ISD::SETLE: return SPCC::ICC_LE;
473 case ISD::SETGE: return SPCC::ICC_GE;
474 case ISD::SETULT: return SPCC::ICC_CS;
475 case ISD::SETULE: return SPCC::ICC_LEU;
476 case ISD::SETUGT: return SPCC::ICC_GU;
477 case ISD::SETUGE: return SPCC::ICC_CC;
478 }
479}
480
481/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
482/// FCC condition.
483static SPCC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
484 switch (CC) {
485 default: assert(0 && "Unknown fp condition code!");
486 case ISD::SETEQ:
487 case ISD::SETOEQ: return SPCC::FCC_E;
488 case ISD::SETNE:
489 case ISD::SETUNE: return SPCC::FCC_NE;
490 case ISD::SETLT:
491 case ISD::SETOLT: return SPCC::FCC_L;
492 case ISD::SETGT:
493 case ISD::SETOGT: return SPCC::FCC_G;
494 case ISD::SETLE:
495 case ISD::SETOLE: return SPCC::FCC_LE;
496 case ISD::SETGE:
497 case ISD::SETOGE: return SPCC::FCC_GE;
498 case ISD::SETULT: return SPCC::FCC_UL;
499 case ISD::SETULE: return SPCC::FCC_ULE;
500 case ISD::SETUGT: return SPCC::FCC_UG;
501 case ISD::SETUGE: return SPCC::FCC_UGE;
502 case ISD::SETUO: return SPCC::FCC_U;
503 case ISD::SETO: return SPCC::FCC_O;
504 case ISD::SETONE: return SPCC::FCC_LG;
505 case ISD::SETUEQ: return SPCC::FCC_UE;
506 }
507}
508
509
510SparcTargetLowering::SparcTargetLowering(TargetMachine &TM)
511 : TargetLowering(TM) {
512
513 // Set up the register classes.
514 addRegisterClass(MVT::i32, SP::IntRegsRegisterClass);
515 addRegisterClass(MVT::f32, SP::FPRegsRegisterClass);
516 addRegisterClass(MVT::f64, SP::DFPRegsRegisterClass);
517
518 // Turn FP extload into load/fextend
519 setLoadXAction(ISD::EXTLOAD, MVT::f32, Expand);
520 // Sparc doesn't have i1 sign extending load
521 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
522 // Turn FP truncstore into trunc + store.
523 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
524
525 // Custom legalize GlobalAddress nodes into LO/HI parts.
526 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
527 setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
528 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
529
530 // Sparc doesn't have sext_inreg, replace them with shl/sra
531 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
532 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
533 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
534
535 // Sparc has no REM or DIVREM operations.
536 setOperationAction(ISD::UREM, MVT::i32, Expand);
537 setOperationAction(ISD::SREM, MVT::i32, Expand);
538 setOperationAction(ISD::SDIVREM, MVT::i32, Expand);
539 setOperationAction(ISD::UDIVREM, MVT::i32, Expand);
540
541 // Custom expand fp<->sint
542 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
543 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
544
545 // Expand fp<->uint
546 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
547 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
548
549 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
550 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
551
552 // Sparc has no select or setcc: expand to SELECT_CC.
553 setOperationAction(ISD::SELECT, MVT::i32, Expand);
554 setOperationAction(ISD::SELECT, MVT::f32, Expand);
555 setOperationAction(ISD::SELECT, MVT::f64, Expand);
556 setOperationAction(ISD::SETCC, MVT::i32, Expand);
557 setOperationAction(ISD::SETCC, MVT::f32, Expand);
558 setOperationAction(ISD::SETCC, MVT::f64, Expand);
559
560 // Sparc doesn't have BRCOND either, it has BR_CC.
561 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
562 setOperationAction(ISD::BRIND, MVT::Other, Expand);
563 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
564 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
565 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
566 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
567
568 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
569 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
570 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
571
572 // SPARC has no intrinsics for these particular operations.
573 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
574 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
575 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
576 setOperationAction(ISD::MEMBARRIER, MVT::Other, Expand);
577
578 setOperationAction(ISD::FSIN , MVT::f64, Expand);
579 setOperationAction(ISD::FCOS , MVT::f64, Expand);
580 setOperationAction(ISD::FREM , MVT::f64, Expand);
581 setOperationAction(ISD::FSIN , MVT::f32, Expand);
582 setOperationAction(ISD::FCOS , MVT::f32, Expand);
583 setOperationAction(ISD::FREM , MVT::f32, Expand);
584 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
585 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
586 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
587 setOperationAction(ISD::ROTL , MVT::i32, Expand);
588 setOperationAction(ISD::ROTR , MVT::i32, Expand);
589 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
590 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
591 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
592 setOperationAction(ISD::FPOW , MVT::f64, Expand);
593 setOperationAction(ISD::FPOW , MVT::f32, Expand);
594
595 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
596 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
597 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
598
599 // FIXME: Sparc provides these multiplies, but we don't have them yet.
600 setOperationAction(ISD::UMUL_LOHI, MVT::i32, Expand);
601
602 // We don't have line number support yet.
603 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
604 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
605 setOperationAction(ISD::LABEL, MVT::Other, Expand);
606
607 // RET must be custom lowered, to meet ABI requirements
608 setOperationAction(ISD::RET , MVT::Other, Custom);
609
610 // VASTART needs to be custom lowered to use the VarArgsFrameIndex.
611 setOperationAction(ISD::VASTART , MVT::Other, Custom);
612 // VAARG needs to be lowered to not do unaligned accesses for doubles.
613 setOperationAction(ISD::VAARG , MVT::Other, Custom);
614
615 // Use the default implementation.
616 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
617 setOperationAction(ISD::VAEND , MVT::Other, Expand);
618 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
619 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
620 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Custom);
621
622 // No debug info support yet.
623 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
624 setOperationAction(ISD::LABEL, MVT::Other, Expand);
625 setOperationAction(ISD::DECLARE, MVT::Other, Expand);
626
627 setStackPointerRegisterToSaveRestore(SP::O6);
628
629 if (TM.getSubtarget<SparcSubtarget>().isV9())
630 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
631
632 computeRegisterProperties();
633}
634
635const char *SparcTargetLowering::getTargetNodeName(unsigned Opcode) const {
636 switch (Opcode) {
637 default: return 0;
638 case SPISD::CMPICC: return "SPISD::CMPICC";
639 case SPISD::CMPFCC: return "SPISD::CMPFCC";
640 case SPISD::BRICC: return "SPISD::BRICC";
641 case SPISD::BRFCC: return "SPISD::BRFCC";
642 case SPISD::SELECT_ICC: return "SPISD::SELECT_ICC";
643 case SPISD::SELECT_FCC: return "SPISD::SELECT_FCC";
644 case SPISD::Hi: return "SPISD::Hi";
645 case SPISD::Lo: return "SPISD::Lo";
646 case SPISD::FTOI: return "SPISD::FTOI";
647 case SPISD::ITOF: return "SPISD::ITOF";
648 case SPISD::CALL: return "SPISD::CALL";
649 case SPISD::RET_FLAG: return "SPISD::RET_FLAG";
650 }
651}
652
653/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
654/// be zero. Op is expected to be a target specific node. Used by DAG
655/// combiner.
656void SparcTargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
657 const APInt &Mask,
658 APInt &KnownZero,
659 APInt &KnownOne,
660 const SelectionDAG &DAG,
661 unsigned Depth) const {
662 APInt KnownZero2, KnownOne2;
663 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
664
665 switch (Op.getOpcode()) {
666 default: break;
667 case SPISD::SELECT_ICC:
668 case SPISD::SELECT_FCC:
669 DAG.ComputeMaskedBits(Op.getOperand(1), Mask, KnownZero, KnownOne,
670 Depth+1);
671 DAG.ComputeMaskedBits(Op.getOperand(0), Mask, KnownZero2, KnownOne2,
672 Depth+1);
673 assert((KnownZero & KnownOne) == 0 && "Bits known to be one AND zero?");
674 assert((KnownZero2 & KnownOne2) == 0 && "Bits known to be one AND zero?");
675
676 // Only known if known in both the LHS and RHS.
677 KnownOne &= KnownOne2;
678 KnownZero &= KnownZero2;
679 break;
680 }
681}
682
Chris Lattnerd23405e2008-03-17 03:21:36 +0000683// Look at LHS/RHS/CC and see if they are a lowered setcc instruction. If so
684// set LHS/RHS and SPCC to the LHS/RHS of the setcc and SPCC to the condition.
685static void LookThroughSetCC(SDOperand &LHS, SDOperand &RHS,
686 ISD::CondCode CC, unsigned &SPCC) {
687 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0 &&
688 CC == ISD::SETNE &&
689 ((LHS.getOpcode() == SPISD::SELECT_ICC &&
690 LHS.getOperand(3).getOpcode() == SPISD::CMPICC) ||
691 (LHS.getOpcode() == SPISD::SELECT_FCC &&
692 LHS.getOperand(3).getOpcode() == SPISD::CMPFCC)) &&
693 isa<ConstantSDNode>(LHS.getOperand(0)) &&
694 isa<ConstantSDNode>(LHS.getOperand(1)) &&
695 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
696 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
697 SDOperand CMPCC = LHS.getOperand(3);
698 SPCC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
699 LHS = CMPCC.getOperand(0);
700 RHS = CMPCC.getOperand(1);
701 }
702}
703
704static SDOperand LowerGLOBALADDRESS(SDOperand Op, SelectionDAG &DAG) {
705 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
706 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
707 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, GA);
708 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, GA);
709 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
710}
711
712static SDOperand LowerCONSTANTPOOL(SDOperand Op, SelectionDAG &DAG) {
713 ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
714 Constant *C = N->getConstVal();
715 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32, N->getAlignment());
716 SDOperand Hi = DAG.getNode(SPISD::Hi, MVT::i32, CP);
717 SDOperand Lo = DAG.getNode(SPISD::Lo, MVT::i32, CP);
718 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
719}
720
721static SDOperand LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
722 // Convert the fp value to integer in an FP register.
723 assert(Op.getValueType() == MVT::i32);
724 Op = DAG.getNode(SPISD::FTOI, MVT::f32, Op.getOperand(0));
725 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
726}
727
728static SDOperand LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
729 assert(Op.getOperand(0).getValueType() == MVT::i32);
730 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
731 // Convert the int value to FP in an FP register.
732 return DAG.getNode(SPISD::ITOF, Op.getValueType(), Tmp);
733}
734
735static SDOperand LowerBR_CC(SDOperand Op, SelectionDAG &DAG) {
736 SDOperand Chain = Op.getOperand(0);
737 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
738 SDOperand LHS = Op.getOperand(2);
739 SDOperand RHS = Op.getOperand(3);
740 SDOperand Dest = Op.getOperand(4);
741 unsigned Opc, SPCC = ~0U;
742
743 // If this is a br_cc of a "setcc", and if the setcc got lowered into
744 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
745 LookThroughSetCC(LHS, RHS, CC, SPCC);
746
747 // Get the condition flag.
748 SDOperand CompareFlag;
749 if (LHS.getValueType() == MVT::i32) {
750 std::vector<MVT::ValueType> VTs;
751 VTs.push_back(MVT::i32);
752 VTs.push_back(MVT::Flag);
753 SDOperand Ops[2] = { LHS, RHS };
754 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
755 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
756 Opc = SPISD::BRICC;
757 } else {
758 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
759 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
760 Opc = SPISD::BRFCC;
761 }
762 return DAG.getNode(Opc, MVT::Other, Chain, Dest,
763 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
764}
765
766static SDOperand LowerSELECT_CC(SDOperand Op, SelectionDAG &DAG) {
767 SDOperand LHS = Op.getOperand(0);
768 SDOperand RHS = Op.getOperand(1);
769 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
770 SDOperand TrueVal = Op.getOperand(2);
771 SDOperand FalseVal = Op.getOperand(3);
772 unsigned Opc, SPCC = ~0U;
773
774 // If this is a select_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);
777
778 SDOperand CompareFlag;
779 if (LHS.getValueType() == MVT::i32) {
780 std::vector<MVT::ValueType> VTs;
781 VTs.push_back(LHS.getValueType()); // subcc returns a value
782 VTs.push_back(MVT::Flag);
783 SDOperand Ops[2] = { LHS, RHS };
784 CompareFlag = DAG.getNode(SPISD::CMPICC, VTs, Ops, 2).getValue(1);
785 Opc = SPISD::SELECT_ICC;
786 if (SPCC == ~0U) SPCC = IntCondCCodeToICC(CC);
787 } else {
788 CompareFlag = DAG.getNode(SPISD::CMPFCC, MVT::Flag, LHS, RHS);
789 Opc = SPISD::SELECT_FCC;
790 if (SPCC == ~0U) SPCC = FPCondCCodeToFCC(CC);
791 }
792 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
793 DAG.getConstant(SPCC, MVT::i32), CompareFlag);
794}
795
796static SDOperand LowerVASTART(SDOperand Op, SelectionDAG &DAG,
797 SparcTargetLowering &TLI) {
798 // vastart just stores the address of the VarArgsFrameIndex slot into the
799 // memory location argument.
800 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
801 DAG.getRegister(SP::I6, MVT::i32),
802 DAG.getConstant(TLI.getVarArgsFrameOffset(),
803 MVT::i32));
804 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
805 return DAG.getStore(Op.getOperand(0), Offset, Op.getOperand(1), SV, 0);
806}
807
808static SDOperand LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
809 SDNode *Node = Op.Val;
810 MVT::ValueType VT = Node->getValueType(0);
811 SDOperand InChain = Node->getOperand(0);
812 SDOperand VAListPtr = Node->getOperand(1);
813 const Value *SV = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
814 SDOperand VAList = DAG.getLoad(MVT::i32, InChain, VAListPtr, SV, 0);
815 // Increment the pointer, VAList, to the next vaarg
816 SDOperand NextPtr = DAG.getNode(ISD::ADD, MVT::i32, VAList,
817 DAG.getConstant(MVT::getSizeInBits(VT)/8,
818 MVT::i32));
819 // Store the incremented VAList to the legalized pointer
820 InChain = DAG.getStore(VAList.getValue(1), NextPtr,
821 VAListPtr, SV, 0);
822 // Load the actual argument out of the pointer VAList, unless this is an
823 // f64 load.
824 if (VT != MVT::f64)
825 return DAG.getLoad(VT, InChain, VAList, NULL, 0);
826
827 // Otherwise, load it as i64, then do a bitconvert.
828 SDOperand V = DAG.getLoad(MVT::i64, InChain, VAList, NULL, 0);
829
830 // Bit-Convert the value to f64.
831 SDOperand Ops[2] = {
832 DAG.getNode(ISD::BIT_CONVERT, MVT::f64, V),
833 V.getValue(1)
834 };
835 return DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(MVT::f64, MVT::Other),
836 Ops, 2);
837}
838
839static SDOperand LowerDYNAMIC_STACKALLOC(SDOperand Op, SelectionDAG &DAG) {
840 SDOperand Chain = Op.getOperand(0); // Legalize the chain.
841 SDOperand Size = Op.getOperand(1); // Legalize the size.
842
843 unsigned SPReg = SP::O6;
844 SDOperand SP = DAG.getCopyFromReg(Chain, SPReg, MVT::i32);
845 SDOperand NewSP = DAG.getNode(ISD::SUB, MVT::i32, SP, Size); // Value
846 Chain = DAG.getCopyToReg(SP.getValue(1), SPReg, NewSP); // Output chain
847
848 // The resultant pointer is actually 16 words from the bottom of the stack,
849 // to provide a register spill area.
850 SDOperand NewVal = DAG.getNode(ISD::ADD, MVT::i32, NewSP,
851 DAG.getConstant(96, MVT::i32));
852 std::vector<MVT::ValueType> Tys;
853 Tys.push_back(MVT::i32);
854 Tys.push_back(MVT::Other);
855 SDOperand Ops[2] = { NewVal, Chain };
856 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
857}
858
Chris Lattnerd23405e2008-03-17 03:21:36 +0000859
860SDOperand SparcTargetLowering::
861LowerOperation(SDOperand Op, SelectionDAG &DAG) {
862 switch (Op.getOpcode()) {
863 default: assert(0 && "Should not custom lower this!");
864 // Frame & Return address. Currently unimplemented
865 case ISD::RETURNADDR: return SDOperand();
866 case ISD::FRAMEADDR: return SDOperand();
867 case ISD::GlobalTLSAddress:
868 assert(0 && "TLS not implemented for Sparc.");
869 case ISD::GlobalAddress: return LowerGLOBALADDRESS(Op, DAG);
870 case ISD::ConstantPool: return LowerCONSTANTPOOL(Op, DAG);
871 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
872 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
873 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
874 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
875 case ISD::VASTART: return LowerVASTART(Op, DAG, *this);
876 case ISD::VAARG: return LowerVAARG(Op, DAG);
877 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Chris Lattner98949a62008-03-17 06:01:07 +0000878 case ISD::CALL: return LowerCALL(Op, DAG);
Chris Lattnerd23405e2008-03-17 03:21:36 +0000879 case ISD::RET: return LowerRET(Op, DAG);
880 }
881}
882
883MachineBasicBlock *
884SparcTargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
885 MachineBasicBlock *BB) {
886 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
887 unsigned BROpcode;
888 unsigned CC;
889 // Figure out the conditional branch opcode to use for this select_cc.
890 switch (MI->getOpcode()) {
891 default: assert(0 && "Unknown SELECT_CC!");
892 case SP::SELECT_CC_Int_ICC:
893 case SP::SELECT_CC_FP_ICC:
894 case SP::SELECT_CC_DFP_ICC:
895 BROpcode = SP::BCOND;
896 break;
897 case SP::SELECT_CC_Int_FCC:
898 case SP::SELECT_CC_FP_FCC:
899 case SP::SELECT_CC_DFP_FCC:
900 BROpcode = SP::FBCOND;
901 break;
902 }
903
904 CC = (SPCC::CondCodes)MI->getOperand(3).getImm();
905
906 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
907 // control-flow pattern. The incoming instruction knows the destination vreg
908 // to set, the condition code register to branch on, the true/false values to
909 // select between, and a branch opcode to use.
910 const BasicBlock *LLVM_BB = BB->getBasicBlock();
911 ilist<MachineBasicBlock>::iterator It = BB;
912 ++It;
913
914 // thisMBB:
915 // ...
916 // TrueVal = ...
917 // [f]bCC copy1MBB
918 // fallthrough --> copy0MBB
919 MachineBasicBlock *thisMBB = BB;
920 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
921 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
922 BuildMI(BB, TII.get(BROpcode)).addMBB(sinkMBB).addImm(CC);
923 MachineFunction *F = BB->getParent();
924 F->getBasicBlockList().insert(It, copy0MBB);
925 F->getBasicBlockList().insert(It, sinkMBB);
926 // Update machine-CFG edges by first adding all successors of the current
927 // block to the new block which will contain the Phi node for the select.
928 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
929 e = BB->succ_end(); i != e; ++i)
930 sinkMBB->addSuccessor(*i);
931 // Next, remove all successors of the current block, and add the true
932 // and fallthrough blocks as its successors.
933 while(!BB->succ_empty())
934 BB->removeSuccessor(BB->succ_begin());
935 BB->addSuccessor(copy0MBB);
936 BB->addSuccessor(sinkMBB);
937
938 // copy0MBB:
939 // %FalseValue = ...
940 // # fallthrough to sinkMBB
941 BB = copy0MBB;
942
943 // Update machine-CFG edges
944 BB->addSuccessor(sinkMBB);
945
946 // sinkMBB:
947 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
948 // ...
949 BB = sinkMBB;
950 BuildMI(BB, TII.get(SP::PHI), MI->getOperand(0).getReg())
951 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
952 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
953
954 delete MI; // The pseudo instruction is gone now.
955 return BB;
956}
Chris Lattner5a65b922008-03-17 05:41:48 +0000957