blob: a0d6b2ffec6e64dd88525be856a3b4c2a3862c2d [file] [log] [blame]
Chris Lattner6c18b102005-12-17 07:47:01 +00001//===-- SparcV8ISelDAGToDAG.cpp - A dag to dag inst selector for SparcV8 --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the V8 target
11//
12//===----------------------------------------------------------------------===//
13
14#include "SparcV8.h"
15#include "SparcV8TargetMachine.h"
Chris Lattner384e5ef2005-12-18 13:33:06 +000016#include "llvm/DerivedTypes.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000017#include "llvm/Function.h"
Chris Lattner8fa54dc2005-12-18 06:59:57 +000018#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000019#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner33084492005-12-18 08:13:54 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattnera01b7572005-12-17 08:03:24 +000023#include "llvm/CodeGen/SSARegMap.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000024#include "llvm/Target/TargetLowering.h"
25#include "llvm/Support/Debug.h"
26#include <iostream>
27using namespace llvm;
28
29//===----------------------------------------------------------------------===//
30// TargetLowering Implementation
31//===----------------------------------------------------------------------===//
32
Chris Lattner4d55aca2005-12-18 01:20:35 +000033namespace V8ISD {
34 enum {
35 FIRST_NUMBER = ISD::BUILTIN_OP_END+V8::INSTRUCTION_LIST_END,
36 CMPICC, // Compare two GPR operands, set icc.
37 CMPFCC, // Compare two FP operands, set fcc.
38 BRICC, // Branch to dest on icc condition
39 BRFCC, // Branch to dest on fcc condition
Chris Lattnere3572462005-12-18 02:10:39 +000040
41 Hi, Lo, // Hi/Lo operations, typically on a global address.
Chris Lattner8fa54dc2005-12-18 06:59:57 +000042
43 FTOI, // FP to Int within a FP register.
44 ITOF, // Int to FP within a FP register.
Chris Lattner33084492005-12-18 08:13:54 +000045
46 SELECT_ICC, // Select between two values using the current ICC flags.
47 SELECT_FCC, // Select between two values using the current FCC flags.
Chris Lattnerdab05f02005-12-18 21:03:04 +000048
49 RET_FLAG, // Return with a flag operand.
Chris Lattner4d55aca2005-12-18 01:20:35 +000050 };
51}
52
Chris Lattner6c18b102005-12-17 07:47:01 +000053namespace {
54 class SparcV8TargetLowering : public TargetLowering {
Chris Lattner2db3ff62005-12-18 15:55:15 +000055 int VarArgsFrameOffset; // Frame offset to start of varargs area.
Chris Lattner6c18b102005-12-17 07:47:01 +000056 public:
57 SparcV8TargetLowering(TargetMachine &TM);
Chris Lattner4d55aca2005-12-18 01:20:35 +000058 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Chris Lattner6c18b102005-12-17 07:47:01 +000059 virtual std::vector<SDOperand>
60 LowerArguments(Function &F, SelectionDAG &DAG);
61 virtual std::pair<SDOperand, SDOperand>
62 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
63 unsigned CC,
64 bool isTailCall, SDOperand Callee, ArgListTy &Args,
65 SelectionDAG &DAG);
66
67 virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
68 SelectionDAG &DAG);
69 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
70 Value *VAListV, SelectionDAG &DAG);
71 virtual std::pair<SDOperand,SDOperand>
72 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
73 const Type *ArgTy, SelectionDAG &DAG);
74 virtual std::pair<SDOperand, SDOperand>
75 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
76 SelectionDAG &DAG);
Chris Lattner33084492005-12-18 08:13:54 +000077 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
78 MachineBasicBlock *MBB);
Chris Lattner6c18b102005-12-17 07:47:01 +000079 };
80}
81
82SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
83 : TargetLowering(TM) {
84
85 // Set up the register classes.
86 addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
87 addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
88 addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
Chris Lattner9a60ff62005-12-17 20:50:42 +000089
Chris Lattnere3572462005-12-18 02:10:39 +000090 // Custom legalize GlobalAddress nodes into LO/HI parts.
91 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner76acc872005-12-18 02:37:35 +000092 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Chris Lattnere3572462005-12-18 02:10:39 +000093
Chris Lattner9a60ff62005-12-17 20:50:42 +000094 // Sparc doesn't have sext_inreg, replace them with shl/sra
Chris Lattner33084492005-12-18 08:13:54 +000095 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
96 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
97 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner7087e572005-12-17 22:39:19 +000098
99 // Sparc has no REM operation.
100 setOperationAction(ISD::UREM, MVT::i32, Expand);
101 setOperationAction(ISD::SREM, MVT::i32, Expand);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000102
103 // Custom expand fp<->sint
104 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
105 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
106
107 // Expand fp<->uint
108 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
109 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Chris Lattner6c18b102005-12-17 07:47:01 +0000110
Chris Lattner53e88452005-12-23 05:13:35 +0000111 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
112 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
113
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000114 // Turn FP extload into load/fextend
Chris Lattner065c8962005-12-18 07:13:32 +0000115 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
116
Chris Lattner4d55aca2005-12-18 01:20:35 +0000117 // Sparc has no select or setcc: expand to SELECT_CC.
118 setOperationAction(ISD::SELECT, MVT::i32, Expand);
119 setOperationAction(ISD::SELECT, MVT::f32, Expand);
120 setOperationAction(ISD::SELECT, MVT::f64, Expand);
121 setOperationAction(ISD::SETCC, MVT::i32, Expand);
122 setOperationAction(ISD::SETCC, MVT::f32, Expand);
123 setOperationAction(ISD::SETCC, MVT::f64, Expand);
124
125 // Sparc doesn't have BRCOND either, it has BR_CC.
126 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
127 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
128 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
129 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
130 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
131 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
132
Chris Lattner33084492005-12-18 08:13:54 +0000133 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
134 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
135 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
136
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000137 // V8 has no intrinsics for these particular operations.
138 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
139 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
140 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
141
Chris Lattner61772c22005-12-19 01:39:40 +0000142 setOperationAction(ISD::FSIN , MVT::f64, Expand);
143 setOperationAction(ISD::FCOS , MVT::f64, Expand);
144 setOperationAction(ISD::FSIN , MVT::f32, Expand);
145 setOperationAction(ISD::FCOS , MVT::f32, Expand);
146 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
147 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
148 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
149
150 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
151 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
152 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000153
154 // We don't have line number support yet.
155 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000156 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
157 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000158
Chris Lattner6c18b102005-12-17 07:47:01 +0000159 computeRegisterProperties();
160}
161
Chris Lattner384e5ef2005-12-18 13:33:06 +0000162/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
163/// either one or two GPRs, including FP values. TODO: we should pass FP values
164/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000165std::vector<SDOperand>
166SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000167 MachineFunction &MF = DAG.getMachineFunction();
168 SSARegMap *RegMap = MF.getSSARegMap();
169 std::vector<SDOperand> ArgValues;
170
Chris Lattner384e5ef2005-12-18 13:33:06 +0000171 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000172 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
173 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000174
175 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
176 unsigned ArgOffset = 68;
177
178 SDOperand Root = DAG.getRoot();
179 std::vector<SDOperand> OutChains;
180
Chris Lattnera01b7572005-12-17 08:03:24 +0000181 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
182 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000183
184 switch (ObjectVT) {
185 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000186 case MVT::i1:
187 case MVT::i8:
188 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000189 case MVT::i32:
190 if (I->use_empty()) { // Argument is dead.
191 if (CurArgReg < ArgRegEnd) ++CurArgReg;
192 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
193 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
194 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
195 MF.addLiveIn(*CurArgReg++, VReg);
196 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
197 if (ObjectVT != MVT::i32) {
198 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
199 : ISD::AssertZext;
200 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
201 DAG.getValueType(ObjectVT));
202 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
203 }
204 ArgValues.push_back(Arg);
205 } else {
206 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
207 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
208 SDOperand Load;
209 if (ObjectVT == MVT::i32) {
210 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
211 } else {
212 unsigned LoadOp =
213 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
214
215 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
216 DAG.getSrcValue(0), ObjectVT);
217 }
218 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000219 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000220
221 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000222 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000223 case MVT::f32:
224 if (I->use_empty()) { // Argument is dead.
225 if (CurArgReg < ArgRegEnd) ++CurArgReg;
226 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
227 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
228 // FP value is passed in an integer register.
229 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
230 MF.addLiveIn(*CurArgReg++, VReg);
231 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
232
Chris Lattnera01874f2005-12-23 02:31:39 +0000233 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
234 ArgValues.push_back(Arg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000235 }
236 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000237 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000238
239 case MVT::i64:
240 case MVT::f64:
241 if (I->use_empty()) { // Argument is dead.
242 if (CurArgReg < ArgRegEnd) ++CurArgReg;
243 if (CurArgReg < ArgRegEnd) ++CurArgReg;
244 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
245 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
246 ((CurArgReg-ArgRegs) & 1) == 0) {
247 // If this is a double argument and the whole thing lives on the stack,
248 // and the argument is aligned, load the double straight from the stack.
249 // We can't do a load in cases like void foo([6ints], int,double),
250 // because the double wouldn't be aligned!
251 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
252 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
253 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
254 DAG.getSrcValue(0)));
255 } else {
256 SDOperand HiVal;
257 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
258 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
259 MF.addLiveIn(*CurArgReg++, VRegHi);
260 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
261 } else {
262 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
263 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
264 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
265 }
266
267 SDOperand LoVal;
268 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
269 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
270 MF.addLiveIn(*CurArgReg++, VRegLo);
271 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
272 } else {
273 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
274 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
275 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
276 }
277
278 // Compose the two halves together into an i64 unit.
279 SDOperand WholeValue =
280 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000281
282 // If we want a double, do a bit convert.
283 if (ObjectVT == MVT::f64)
284 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
285
286 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000287 }
288 ArgOffset += 8;
289 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000290 }
291 }
292
Chris Lattner384e5ef2005-12-18 13:33:06 +0000293 // Store remaining ArgRegs to the stack if this is a varargs function.
294 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000295 // Remember the vararg offset for the va_start implementation.
296 VarArgsFrameOffset = ArgOffset;
297
Chris Lattner384e5ef2005-12-18 13:33:06 +0000298 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
299 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
300 MF.addLiveIn(*CurArgReg, VReg);
301 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
302
303 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
304 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
305
306 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
307 Arg, FIPtr, DAG.getSrcValue(0)));
308 ArgOffset += 4;
309 }
310 }
311
312 if (!OutChains.empty())
313 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000314
315 // Finally, inform the code generator which regs we return values in.
316 switch (getValueType(F.getReturnType())) {
317 default: assert(0 && "Unknown type!");
318 case MVT::isVoid: break;
319 case MVT::i1:
320 case MVT::i8:
321 case MVT::i16:
322 case MVT::i32:
323 MF.addLiveOut(V8::I0);
324 break;
325 case MVT::i64:
326 MF.addLiveOut(V8::I0);
327 MF.addLiveOut(V8::I1);
328 break;
329 case MVT::f32:
330 MF.addLiveOut(V8::F0);
331 break;
332 case MVT::f64:
333 MF.addLiveOut(V8::D0);
334 break;
335 }
336
337 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000338}
339
340std::pair<SDOperand, SDOperand>
341SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
342 bool isVarArg, unsigned CC,
343 bool isTailCall, SDOperand Callee,
344 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000345 MachineFunction &MF = DAG.getMachineFunction();
346 // Count the size of the outgoing arguments.
347 unsigned ArgsSize = 0;
348 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
349 switch (getValueType(Args[i].second)) {
350 default: assert(0 && "Unknown value type!");
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 case MVT::i32:
355 case MVT::f32:
356 ArgsSize += 4;
357 break;
358 case MVT::i64:
359 case MVT::f64:
360 ArgsSize += 8;
361 break;
362 }
363 }
364 if (ArgsSize > 4*6)
365 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
366 else
367 ArgsSize = 0;
368
Chris Lattner6554bef2005-12-19 01:15:13 +0000369 // Keep stack frames 8-byte aligned.
370 ArgsSize = (ArgsSize+7) & ~7;
371
Chris Lattner2db3ff62005-12-18 15:55:15 +0000372 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
373 DAG.getConstant(ArgsSize, getPointerTy()));
374
375 SDOperand StackPtr, NullSV;
376 std::vector<SDOperand> Stores;
377 std::vector<SDOperand> RegValuesToPass;
378 unsigned ArgOffset = 68;
379 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
380 SDOperand Val = Args[i].first;
381 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000382 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000383 unsigned ObjSize;
384 switch (ObjectVT) {
385 default: assert(0 && "Unhandled argument type!");
386 case MVT::i1:
387 case MVT::i8:
388 case MVT::i16:
389 // Promote the integer to 32-bits. If the input type is signed, use a
390 // sign extend, otherwise use a zero extend.
391 if (Args[i].second->isSigned())
392 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
393 else
394 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
395 // FALL THROUGH
396 case MVT::i32:
397 ObjSize = 4;
398
399 if (RegValuesToPass.size() >= 6) {
400 ValToStore = Val;
401 } else {
402 RegValuesToPass.push_back(Val);
403 }
404 break;
405 case MVT::f32:
406 ObjSize = 4;
407 if (RegValuesToPass.size() >= 6) {
408 ValToStore = Val;
409 } else {
410 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000411 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000412 RegValuesToPass.push_back(Val);
413 }
414 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000415 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000416 ObjSize = 8;
417 // If we can store this directly into the outgoing slot, do so. We can
418 // do this when all ArgRegs are used and if the outgoing slot is aligned.
419 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
420 ValToStore = Val;
421 break;
422 }
423
424 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000425 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000426 // FALL THROUGH
427 case MVT::i64:
428 ObjSize = 8;
429 if (RegValuesToPass.size() >= 6) {
430 ValToStore = Val; // Whole thing is passed in memory.
431 break;
432 }
433
434 // Split the value into top and bottom part. Top part goes in a reg.
435 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
436 DAG.getConstant(1, MVT::i32));
437 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
438 DAG.getConstant(0, MVT::i32));
439 RegValuesToPass.push_back(Hi);
440
441 if (RegValuesToPass.size() >= 6) {
442 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000443 ArgOffset += 4;
444 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000445 } else {
446 RegValuesToPass.push_back(Lo);
447 }
448 break;
449 }
450
451 if (ValToStore.Val) {
452 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000453 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000454 NullSV = DAG.getSrcValue(NULL);
455 }
456 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
457 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
458 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
459 ValToStore, PtrOff, NullSV));
460 }
461 ArgOffset += ObjSize;
462 }
463
464 // Emit all stores, make sure the occur before any copies into physregs.
465 if (!Stores.empty())
466 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
467
468 static const unsigned ArgRegs[] = {
469 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
470 };
471
472 // Build a sequence of copy-to-reg nodes chained together with token chain
473 // and flag operands which copy the outgoing args into O[0-5].
474 SDOperand InFlag;
475 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
476 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
477 InFlag = Chain.getValue(1);
478 }
479
Chris Lattner2db3ff62005-12-18 15:55:15 +0000480 // If the callee is a GlobalAddress node (quite common, every direct call is)
481 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
482 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
483 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
484
485 std::vector<MVT::ValueType> NodeTys;
486 NodeTys.push_back(MVT::Other); // Returns a chain
487 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000488 if (InFlag.Val)
489 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
490 else
491 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000492 InFlag = Chain.getValue(1);
493
494 MVT::ValueType RetTyVT = getValueType(RetTy);
495 SDOperand RetVal;
496 if (RetTyVT != MVT::isVoid) {
497 switch (RetTyVT) {
498 default: assert(0 && "Unknown value type to return!");
499 case MVT::i1:
500 case MVT::i8:
501 case MVT::i16:
502 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
503 Chain = RetVal.getValue(1);
504
505 // Add a note to keep track of whether it is sign or zero extended.
506 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
507 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
508 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
509 break;
510 case MVT::i32:
511 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
512 Chain = RetVal.getValue(1);
513 break;
514 case MVT::f32:
515 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
516 Chain = RetVal.getValue(1);
517 break;
518 case MVT::f64:
519 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
520 Chain = RetVal.getValue(1);
521 break;
522 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000523 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000524 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
525 Lo.getValue(2));
526 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
527 Chain = Hi.getValue(1);
528 break;
529 }
530 }
531
532 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
533 DAG.getConstant(ArgsSize, getPointerTy()));
534
Chris Lattner2db3ff62005-12-18 15:55:15 +0000535 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000536}
537
538SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
539 SelectionDAG &DAG) {
Chris Lattnerdab05f02005-12-18 21:03:04 +0000540 SDOperand Copy;
541 switch (Op.getValueType()) {
542 default: assert(0 && "Unknown type to return!");
543 case MVT::i32:
544 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
545 break;
546 case MVT::f32:
547 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
548 break;
549 case MVT::f64:
550 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
551 break;
552 case MVT::i64:
Chris Lattner4b486312005-12-17 08:15:09 +0000553 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
554 DAG.getConstant(1, MVT::i32));
555 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
556 DAG.getConstant(0, MVT::i32));
Chris Lattnerdab05f02005-12-18 21:03:04 +0000557 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
558 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
559 break;
Chris Lattner4b486312005-12-17 08:15:09 +0000560 }
Chris Lattnerdab05f02005-12-18 21:03:04 +0000561 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Chris Lattner6c18b102005-12-17 07:47:01 +0000562}
563
Chris Lattner4d55aca2005-12-18 01:20:35 +0000564SDOperand SparcV8TargetLowering::
565LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
566 SelectionDAG &DAG) {
567
Chris Lattnerc4769bb2005-12-23 06:24:04 +0000568 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
569 DAG.getRegister(V8::I6, MVT::i32),
570 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
571 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
572 VAListP, DAG.getSrcValue(VAListV));
Chris Lattner6c18b102005-12-17 07:47:01 +0000573}
574
Chris Lattner4d55aca2005-12-18 01:20:35 +0000575std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
576LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
577 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner74fa64b2005-12-23 06:37:38 +0000578 // Load the pointer out of the valist.
579 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
580 VAListP, DAG.getSrcValue(VAListV));
581 MVT::ValueType ArgVT = getValueType(ArgTy);
582 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
583 Ptr, DAG.getSrcValue(NULL));
584 // Increment the pointer.
585 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
586 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
587 // Store it back to the valist.
588 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
589 VAListP, DAG.getSrcValue(VAListV));
590 return std::make_pair(Val, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000591}
592
Chris Lattner4d55aca2005-12-18 01:20:35 +0000593std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
594LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
595 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000596 assert(0 && "Unimp");
597 abort();
598}
599
Chris Lattner4d55aca2005-12-18 01:20:35 +0000600SDOperand SparcV8TargetLowering::
601LowerOperation(SDOperand Op, SelectionDAG &DAG) {
602 switch (Op.getOpcode()) {
603 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000604 case ISD::GlobalAddress: {
605 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
606 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
607 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
608 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
609 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
610 }
Chris Lattner76acc872005-12-18 02:37:35 +0000611 case ISD::ConstantPool: {
612 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
613 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
614 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
615 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
616 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
617 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000618 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000619 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000620 assert(Op.getValueType() == MVT::i32);
621 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
622 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000623 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000624 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner330ea122005-12-23 07:08:39 +0000625 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000626 // Convert the int value to FP in an FP register.
627 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Op);
628 }
Chris Lattner33084492005-12-18 08:13:54 +0000629 case ISD::BR_CC: {
630 SDOperand Chain = Op.getOperand(0);
631 SDOperand CC = Op.getOperand(1);
632 SDOperand LHS = Op.getOperand(2);
633 SDOperand RHS = Op.getOperand(3);
634 SDOperand Dest = Op.getOperand(4);
635
636 // Get the condition flag.
637 if (LHS.getValueType() == MVT::i32) {
638 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, MVT::Flag, LHS, RHS);
639 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
640 } else {
641 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
642 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
643 }
644 }
645 case ISD::SELECT_CC: {
646 SDOperand LHS = Op.getOperand(0);
647 SDOperand RHS = Op.getOperand(1);
648 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
649 SDOperand TrueVal = Op.getOperand(2);
650 SDOperand FalseVal = Op.getOperand(3);
651
652 unsigned Opc;
653 Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
654 SDOperand CompareFlag = DAG.getNode(Opc, MVT::Flag, LHS, RHS);
655
656 Opc = LHS.getValueType() == MVT::i32 ?
657 V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
658 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
659 DAG.getConstant(CC, MVT::i32), CompareFlag);
660 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000661 }
662}
663
Chris Lattner33084492005-12-18 08:13:54 +0000664MachineBasicBlock *
665SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
666 MachineBasicBlock *BB) {
667 unsigned BROpcode;
668 // Figure out the conditional branch opcode to use for this select_cc.
669 switch (MI->getOpcode()) {
670 default: assert(0 && "Unknown SELECT_CC!");
671 case V8::SELECT_CC_Int_ICC:
672 case V8::SELECT_CC_FP_ICC:
673 case V8::SELECT_CC_DFP_ICC:
674 // Integer compare.
675 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
676 default: assert(0 && "Unknown integer condition code!");
677 case ISD::SETEQ: BROpcode = V8::BE; break;
678 case ISD::SETNE: BROpcode = V8::BNE; break;
679 case ISD::SETLT: BROpcode = V8::BL; break;
680 case ISD::SETGT: BROpcode = V8::BG; break;
681 case ISD::SETLE: BROpcode = V8::BLE; break;
682 case ISD::SETGE: BROpcode = V8::BGE; break;
683 case ISD::SETULT: BROpcode = V8::BCS; break;
684 case ISD::SETULE: BROpcode = V8::BLEU; break;
685 case ISD::SETUGT: BROpcode = V8::BGU; break;
686 case ISD::SETUGE: BROpcode = V8::BCC; break;
687 }
688 break;
689 case V8::SELECT_CC_Int_FCC:
690 case V8::SELECT_CC_FP_FCC:
691 case V8::SELECT_CC_DFP_FCC:
692 // FP compare.
693 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
694 default: assert(0 && "Unknown fp condition code!");
695 case ISD::SETEQ: BROpcode = V8::FBE; break;
696 case ISD::SETNE: BROpcode = V8::FBNE; break;
697 case ISD::SETLT: BROpcode = V8::FBL; break;
698 case ISD::SETGT: BROpcode = V8::FBG; break;
699 case ISD::SETLE: BROpcode = V8::FBLE; break;
700 case ISD::SETGE: BROpcode = V8::FBGE; break;
701 case ISD::SETULT: BROpcode = V8::FBUL; break;
702 case ISD::SETULE: BROpcode = V8::FBULE; break;
703 case ISD::SETUGT: BROpcode = V8::FBUG; break;
704 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
705 case ISD::SETUO: BROpcode = V8::FBU; break;
706 case ISD::SETO: BROpcode = V8::FBO; break;
707 case ISD::SETONE: BROpcode = V8::FBLG; break;
708 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
709 }
710 break;
711 }
712
713 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
714 // control-flow pattern. The incoming instruction knows the destination vreg
715 // to set, the condition code register to branch on, the true/false values to
716 // select between, and a branch opcode to use.
717 const BasicBlock *LLVM_BB = BB->getBasicBlock();
718 ilist<MachineBasicBlock>::iterator It = BB;
719 ++It;
720
721 // thisMBB:
722 // ...
723 // TrueVal = ...
724 // [f]bCC copy1MBB
725 // fallthrough --> copy0MBB
726 MachineBasicBlock *thisMBB = BB;
727 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
728 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
729 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
730 MachineFunction *F = BB->getParent();
731 F->getBasicBlockList().insert(It, copy0MBB);
732 F->getBasicBlockList().insert(It, sinkMBB);
733 // Update machine-CFG edges
734 BB->addSuccessor(copy0MBB);
735 BB->addSuccessor(sinkMBB);
736
737 // copy0MBB:
738 // %FalseValue = ...
739 // # fallthrough to sinkMBB
740 BB = copy0MBB;
741
742 // Update machine-CFG edges
743 BB->addSuccessor(sinkMBB);
744
745 // sinkMBB:
746 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
747 // ...
748 BB = sinkMBB;
749 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
750 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
751 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
752
753 delete MI; // The pseudo instruction is gone now.
754 return BB;
755}
756
Chris Lattner6c18b102005-12-17 07:47:01 +0000757//===----------------------------------------------------------------------===//
758// Instruction Selector Implementation
759//===----------------------------------------------------------------------===//
760
761//===--------------------------------------------------------------------===//
762/// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
763/// instructions for SelectionDAG operations.
764///
765namespace {
766class SparcV8DAGToDAGISel : public SelectionDAGISel {
767 SparcV8TargetLowering V8Lowering;
768public:
769 SparcV8DAGToDAGISel(TargetMachine &TM)
770 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
771
772 SDOperand Select(SDOperand Op);
773
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000774 // Complex Pattern Selectors.
775 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
776 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
777
Chris Lattner6c18b102005-12-17 07:47:01 +0000778 /// InstructionSelectBasicBlock - This callback is invoked by
779 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
780 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
781
782 virtual const char *getPassName() const {
783 return "PowerPC DAG->DAG Pattern Instruction Selection";
784 }
785
786 // Include the pieces autogenerated from the target description.
787#include "SparcV8GenDAGISel.inc"
788};
789} // end anonymous namespace
790
791/// InstructionSelectBasicBlock - This callback is invoked by
792/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
793void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
794 DEBUG(BB->dump());
795
796 // Select target instructions for the DAG.
797 DAG.setRoot(Select(DAG.getRoot()));
798 CodeGenMap.clear();
799 DAG.RemoveDeadNodes();
800
801 // Emit machine code to BB.
802 ScheduleAndEmitDAG(DAG);
803}
804
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000805bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
806 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000807 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
808 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000809 Offset = CurDAG->getTargetConstant(0, MVT::i32);
810 return true;
811 }
812
813 if (Addr.getOpcode() == ISD::ADD) {
814 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
815 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000816 if (FrameIndexSDNode *FIN =
817 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000818 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +0000819 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000820 } else {
821 Base = Select(Addr.getOperand(0));
822 }
823 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
824 return true;
825 }
826 }
827 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
828 Base = Select(Addr.getOperand(1));
829 Offset = Addr.getOperand(0).getOperand(0);
830 return true;
831 }
832 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
833 Base = Select(Addr.getOperand(0));
834 Offset = Addr.getOperand(1).getOperand(0);
835 return true;
836 }
837 }
838 Base = Select(Addr);
839 Offset = CurDAG->getTargetConstant(0, MVT::i32);
840 return true;
841}
842
Chris Lattner9034b882005-12-17 21:25:27 +0000843bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000844 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000845 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +0000846 if (Addr.getOpcode() == ISD::ADD) {
847 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
848 Predicate_simm13(Addr.getOperand(1).Val))
849 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +0000850 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
851 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
852 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +0000853 R1 = Select(Addr.getOperand(0));
854 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +0000855 return true;
856 }
857
858 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000859 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
860 return true;
861}
862
Chris Lattner6c18b102005-12-17 07:47:01 +0000863SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
864 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +0000865 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
866 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +0000867 return Op; // Already selected.
868 // If this has already been converted, use it.
869 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
870 if (CGMI != CodeGenMap.end()) return CGMI->second;
871
872 switch (N->getOpcode()) {
873 default: break;
Chris Lattner7c423b42005-12-19 07:57:53 +0000874 case ISD::Register: return Op;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000875 case ISD::FrameIndex: {
876 int FI = cast<FrameIndexSDNode>(N)->getIndex();
877 if (N->hasOneUse())
878 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
879 CurDAG->getTargetFrameIndex(FI, MVT::i32),
880 CurDAG->getTargetConstant(0, MVT::i32));
881 return CodeGenMap[Op] =
882 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
883 CurDAG->getTargetFrameIndex(FI, MVT::i32),
884 CurDAG->getTargetConstant(0, MVT::i32));
885 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000886 case V8ISD::CMPICC: {
887 // FIXME: Handle compare with immediate.
888 SDOperand LHS = Select(N->getOperand(0));
889 SDOperand RHS = Select(N->getOperand(1));
890 SDOperand Result = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
891 LHS, RHS);
892 return CodeGenMap[Op] = Result.getValue(1);
893 }
Chris Lattnerd19fc652005-12-17 22:55:57 +0000894 case ISD::ADD_PARTS: {
895 SDOperand LHSL = Select(N->getOperand(0));
896 SDOperand LHSH = Select(N->getOperand(1));
897 SDOperand RHSL = Select(N->getOperand(2));
898 SDOperand RHSH = Select(N->getOperand(3));
899 // FIXME, handle immediate RHS.
900 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
901 LHSL, RHSL);
902 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
903 Low.getValue(1));
904 CodeGenMap[SDOperand(N, 0)] = Low;
905 CodeGenMap[SDOperand(N, 1)] = Hi;
906 return Op.ResNo ? Hi : Low;
907 }
908 case ISD::SUB_PARTS: {
909 SDOperand LHSL = Select(N->getOperand(0));
910 SDOperand LHSH = Select(N->getOperand(1));
911 SDOperand RHSL = Select(N->getOperand(2));
912 SDOperand RHSH = Select(N->getOperand(3));
913 // FIXME, handle immediate RHS.
914 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
915 LHSL, RHSL);
916 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
917 Low.getValue(1));
918 CodeGenMap[SDOperand(N, 0)] = Low;
919 CodeGenMap[SDOperand(N, 1)] = Hi;
920 return Op.ResNo ? Hi : Low;
921 }
Chris Lattner7087e572005-12-17 22:39:19 +0000922 case ISD::SDIV:
923 case ISD::UDIV: {
924 // FIXME: should use a custom expander to expose the SRA to the dag.
925 SDOperand DivLHS = Select(N->getOperand(0));
926 SDOperand DivRHS = Select(N->getOperand(1));
927
928 // Set the Y register to the high-part.
929 SDOperand TopPart;
930 if (N->getOpcode() == ISD::SDIV) {
931 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
932 CurDAG->getTargetConstant(31, MVT::i32));
933 } else {
934 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
935 }
936 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
937 CurDAG->getRegister(V8::G0, MVT::i32));
938
939 // FIXME: Handle div by immediate.
940 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
941 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
942 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000943 case ISD::MULHU:
944 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000945 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000946 SDOperand MulLHS = Select(N->getOperand(0));
947 SDOperand MulRHS = Select(N->getOperand(1));
948 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
949 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
950 MulLHS, MulRHS);
951 // The high part is in the Y register.
952 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
953 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000954 case ISD::CALL:
955 // FIXME: This is a workaround for a bug in tblgen.
956 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
957 // Emits: (CALL:void (tglobaladdr:i32):$dst)
958 // Pattern complexity = 2 cost = 1
959 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +0000960 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
961 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000962 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000963 SDOperand Chain = N->getOperand(0);
964 SDOperand Tmp0 = N1;
965 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000966 SDOperand Result;
967 if (N->getNumOperands() == 3) {
968 InFlag = Select(N->getOperand(2));
969 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
970 Chain, InFlag);
971 } else {
972 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
973 Chain);
974 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000975 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
976 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
977 return Result.getValue(Op.ResNo);
978 }
979 P47Fail:;
980
Chris Lattner6c18b102005-12-17 07:47:01 +0000981 }
982
983 return SelectCode(Op);
984}
985
986
987/// createPPCISelDag - This pass converts a legalized DAG into a
988/// PowerPC-specific DAG, ready for instruction scheduling.
989///
990FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
991 return new SparcV8DAGToDAGISel(TM);
992}