blob: 6c93a26f24e4b514f000607f925aeab0d7a28b6a [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);
156 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
157
Chris Lattner6c18b102005-12-17 07:47:01 +0000158 computeRegisterProperties();
159}
160
Chris Lattner384e5ef2005-12-18 13:33:06 +0000161/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
162/// either one or two GPRs, including FP values. TODO: we should pass FP values
163/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000164std::vector<SDOperand>
165SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000166 MachineFunction &MF = DAG.getMachineFunction();
167 SSARegMap *RegMap = MF.getSSARegMap();
168 std::vector<SDOperand> ArgValues;
169
Chris Lattner384e5ef2005-12-18 13:33:06 +0000170 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000171 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
172 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000173
174 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
175 unsigned ArgOffset = 68;
176
177 SDOperand Root = DAG.getRoot();
178 std::vector<SDOperand> OutChains;
179
Chris Lattnera01b7572005-12-17 08:03:24 +0000180 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
181 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000182
183 switch (ObjectVT) {
184 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000185 case MVT::i1:
186 case MVT::i8:
187 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000188 case MVT::i32:
189 if (I->use_empty()) { // Argument is dead.
190 if (CurArgReg < ArgRegEnd) ++CurArgReg;
191 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
192 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
193 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
194 MF.addLiveIn(*CurArgReg++, VReg);
195 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
196 if (ObjectVT != MVT::i32) {
197 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
198 : ISD::AssertZext;
199 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
200 DAG.getValueType(ObjectVT));
201 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
202 }
203 ArgValues.push_back(Arg);
204 } else {
205 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
206 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
207 SDOperand Load;
208 if (ObjectVT == MVT::i32) {
209 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
210 } else {
211 unsigned LoadOp =
212 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
213
214 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
215 DAG.getSrcValue(0), ObjectVT);
216 }
217 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000218 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000219
220 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000221 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000222 case MVT::f32:
223 if (I->use_empty()) { // Argument is dead.
224 if (CurArgReg < ArgRegEnd) ++CurArgReg;
225 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
226 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
227 // FP value is passed in an integer register.
228 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
229 MF.addLiveIn(*CurArgReg++, VReg);
230 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
231
Chris Lattnera01874f2005-12-23 02:31:39 +0000232 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
233 ArgValues.push_back(Arg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000234 }
235 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000236 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000237
238 case MVT::i64:
239 case MVT::f64:
240 if (I->use_empty()) { // Argument is dead.
241 if (CurArgReg < ArgRegEnd) ++CurArgReg;
242 if (CurArgReg < ArgRegEnd) ++CurArgReg;
243 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
244 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
245 ((CurArgReg-ArgRegs) & 1) == 0) {
246 // If this is a double argument and the whole thing lives on the stack,
247 // and the argument is aligned, load the double straight from the stack.
248 // We can't do a load in cases like void foo([6ints], int,double),
249 // because the double wouldn't be aligned!
250 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
251 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
252 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
253 DAG.getSrcValue(0)));
254 } else {
255 SDOperand HiVal;
256 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
257 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
258 MF.addLiveIn(*CurArgReg++, VRegHi);
259 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
260 } else {
261 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
262 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
263 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
264 }
265
266 SDOperand LoVal;
267 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
268 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
269 MF.addLiveIn(*CurArgReg++, VRegLo);
270 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
271 } else {
272 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
273 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
274 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
275 }
276
277 // Compose the two halves together into an i64 unit.
278 SDOperand WholeValue =
279 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000280
281 // If we want a double, do a bit convert.
282 if (ObjectVT == MVT::f64)
283 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
284
285 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000286 }
287 ArgOffset += 8;
288 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000289 }
290 }
291
Chris Lattner384e5ef2005-12-18 13:33:06 +0000292 // Store remaining ArgRegs to the stack if this is a varargs function.
293 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000294 // Remember the vararg offset for the va_start implementation.
295 VarArgsFrameOffset = ArgOffset;
296
Chris Lattner384e5ef2005-12-18 13:33:06 +0000297 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
298 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
299 MF.addLiveIn(*CurArgReg, VReg);
300 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
301
302 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
303 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
304
305 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
306 Arg, FIPtr, DAG.getSrcValue(0)));
307 ArgOffset += 4;
308 }
309 }
310
311 if (!OutChains.empty())
312 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000313
314 // Finally, inform the code generator which regs we return values in.
315 switch (getValueType(F.getReturnType())) {
316 default: assert(0 && "Unknown type!");
317 case MVT::isVoid: break;
318 case MVT::i1:
319 case MVT::i8:
320 case MVT::i16:
321 case MVT::i32:
322 MF.addLiveOut(V8::I0);
323 break;
324 case MVT::i64:
325 MF.addLiveOut(V8::I0);
326 MF.addLiveOut(V8::I1);
327 break;
328 case MVT::f32:
329 MF.addLiveOut(V8::F0);
330 break;
331 case MVT::f64:
332 MF.addLiveOut(V8::D0);
333 break;
334 }
335
336 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000337}
338
339std::pair<SDOperand, SDOperand>
340SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
341 bool isVarArg, unsigned CC,
342 bool isTailCall, SDOperand Callee,
343 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000344 MachineFunction &MF = DAG.getMachineFunction();
345 // Count the size of the outgoing arguments.
346 unsigned ArgsSize = 0;
347 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
348 switch (getValueType(Args[i].second)) {
349 default: assert(0 && "Unknown value type!");
350 case MVT::i1:
351 case MVT::i8:
352 case MVT::i16:
353 case MVT::i32:
354 case MVT::f32:
355 ArgsSize += 4;
356 break;
357 case MVT::i64:
358 case MVT::f64:
359 ArgsSize += 8;
360 break;
361 }
362 }
363 if (ArgsSize > 4*6)
364 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
365 else
366 ArgsSize = 0;
367
Chris Lattner6554bef2005-12-19 01:15:13 +0000368 // Keep stack frames 8-byte aligned.
369 ArgsSize = (ArgsSize+7) & ~7;
370
Chris Lattner2db3ff62005-12-18 15:55:15 +0000371 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
372 DAG.getConstant(ArgsSize, getPointerTy()));
373
374 SDOperand StackPtr, NullSV;
375 std::vector<SDOperand> Stores;
376 std::vector<SDOperand> RegValuesToPass;
377 unsigned ArgOffset = 68;
378 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
379 SDOperand Val = Args[i].first;
380 MVT::ValueType ObjectVT = Val.getValueType();
381 SDOperand ValToStore;
382 unsigned ObjSize;
383 switch (ObjectVT) {
384 default: assert(0 && "Unhandled argument type!");
385 case MVT::i1:
386 case MVT::i8:
387 case MVT::i16:
388 // Promote the integer to 32-bits. If the input type is signed, use a
389 // sign extend, otherwise use a zero extend.
390 if (Args[i].second->isSigned())
391 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
392 else
393 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
394 // FALL THROUGH
395 case MVT::i32:
396 ObjSize = 4;
397
398 if (RegValuesToPass.size() >= 6) {
399 ValToStore = Val;
400 } else {
401 RegValuesToPass.push_back(Val);
402 }
403 break;
404 case MVT::f32:
405 ObjSize = 4;
406 if (RegValuesToPass.size() >= 6) {
407 ValToStore = Val;
408 } else {
409 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000410 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000411 RegValuesToPass.push_back(Val);
412 }
413 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000414 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000415 ObjSize = 8;
416 // If we can store this directly into the outgoing slot, do so. We can
417 // do this when all ArgRegs are used and if the outgoing slot is aligned.
418 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
419 ValToStore = Val;
420 break;
421 }
422
423 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000424 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000425 // FALL THROUGH
426 case MVT::i64:
427 ObjSize = 8;
428 if (RegValuesToPass.size() >= 6) {
429 ValToStore = Val; // Whole thing is passed in memory.
430 break;
431 }
432
433 // Split the value into top and bottom part. Top part goes in a reg.
434 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
435 DAG.getConstant(1, MVT::i32));
436 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
437 DAG.getConstant(0, MVT::i32));
438 RegValuesToPass.push_back(Hi);
439
440 if (RegValuesToPass.size() >= 6) {
441 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000442 ArgOffset += 4;
443 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000444 } else {
445 RegValuesToPass.push_back(Lo);
446 }
447 break;
448 }
449
450 if (ValToStore.Val) {
451 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000452 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000453 NullSV = DAG.getSrcValue(NULL);
454 }
455 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
456 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
457 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
458 ValToStore, PtrOff, NullSV));
459 }
460 ArgOffset += ObjSize;
461 }
462
463 // Emit all stores, make sure the occur before any copies into physregs.
464 if (!Stores.empty())
465 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
466
467 static const unsigned ArgRegs[] = {
468 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
469 };
470
471 // Build a sequence of copy-to-reg nodes chained together with token chain
472 // and flag operands which copy the outgoing args into O[0-5].
473 SDOperand InFlag;
474 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
475 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
476 InFlag = Chain.getValue(1);
477 }
478
Chris Lattner2db3ff62005-12-18 15:55:15 +0000479 // If the callee is a GlobalAddress node (quite common, every direct call is)
480 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
481 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
482 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
483
484 std::vector<MVT::ValueType> NodeTys;
485 NodeTys.push_back(MVT::Other); // Returns a chain
486 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000487 if (InFlag.Val)
488 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
489 else
490 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000491 InFlag = Chain.getValue(1);
492
493 MVT::ValueType RetTyVT = getValueType(RetTy);
494 SDOperand RetVal;
495 if (RetTyVT != MVT::isVoid) {
496 switch (RetTyVT) {
497 default: assert(0 && "Unknown value type to return!");
498 case MVT::i1:
499 case MVT::i8:
500 case MVT::i16:
501 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
502 Chain = RetVal.getValue(1);
503
504 // Add a note to keep track of whether it is sign or zero extended.
505 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
506 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
507 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
508 break;
509 case MVT::i32:
510 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
511 Chain = RetVal.getValue(1);
512 break;
513 case MVT::f32:
514 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
515 Chain = RetVal.getValue(1);
516 break;
517 case MVT::f64:
518 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
519 Chain = RetVal.getValue(1);
520 break;
521 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000522 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000523 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
524 Lo.getValue(2));
525 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
526 Chain = Hi.getValue(1);
527 break;
528 }
529 }
530
531 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
532 DAG.getConstant(ArgsSize, getPointerTy()));
533
Chris Lattner2db3ff62005-12-18 15:55:15 +0000534 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000535}
536
537SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
538 SelectionDAG &DAG) {
Chris Lattnerdab05f02005-12-18 21:03:04 +0000539 SDOperand Copy;
540 switch (Op.getValueType()) {
541 default: assert(0 && "Unknown type to return!");
542 case MVT::i32:
543 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
544 break;
545 case MVT::f32:
546 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
547 break;
548 case MVT::f64:
549 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
550 break;
551 case MVT::i64:
Chris Lattner4b486312005-12-17 08:15:09 +0000552 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
553 DAG.getConstant(1, MVT::i32));
554 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
555 DAG.getConstant(0, MVT::i32));
Chris Lattnerdab05f02005-12-18 21:03:04 +0000556 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
557 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
558 break;
Chris Lattner4b486312005-12-17 08:15:09 +0000559 }
Chris Lattnerdab05f02005-12-18 21:03:04 +0000560 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Chris Lattner6c18b102005-12-17 07:47:01 +0000561}
562
Chris Lattner4d55aca2005-12-18 01:20:35 +0000563SDOperand SparcV8TargetLowering::
564LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
565 SelectionDAG &DAG) {
566
Chris Lattnerc4769bb2005-12-23 06:24:04 +0000567 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
568 DAG.getRegister(V8::I6, MVT::i32),
569 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
570 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
571 VAListP, DAG.getSrcValue(VAListV));
Chris Lattner6c18b102005-12-17 07:47:01 +0000572}
573
Chris Lattner4d55aca2005-12-18 01:20:35 +0000574std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
575LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
576 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner74fa64b2005-12-23 06:37:38 +0000577 // Load the pointer out of the valist.
578 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
579 VAListP, DAG.getSrcValue(VAListV));
580 MVT::ValueType ArgVT = getValueType(ArgTy);
581 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
582 Ptr, DAG.getSrcValue(NULL));
583 // Increment the pointer.
584 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
585 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
586 // Store it back to the valist.
587 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
588 VAListP, DAG.getSrcValue(VAListV));
589 return std::make_pair(Val, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000590}
591
Chris Lattner4d55aca2005-12-18 01:20:35 +0000592std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
593LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
594 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000595 assert(0 && "Unimp");
596 abort();
597}
598
Chris Lattner4d55aca2005-12-18 01:20:35 +0000599SDOperand SparcV8TargetLowering::
600LowerOperation(SDOperand Op, SelectionDAG &DAG) {
601 switch (Op.getOpcode()) {
602 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000603 case ISD::GlobalAddress: {
604 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
605 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
606 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
607 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
608 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
609 }
Chris Lattner76acc872005-12-18 02:37:35 +0000610 case ISD::ConstantPool: {
611 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
612 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
613 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
614 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
615 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
616 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000617 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000618 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000619 assert(Op.getValueType() == MVT::i32);
620 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
621 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000622 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000623 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner330ea122005-12-23 07:08:39 +0000624 Op = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000625 // Convert the int value to FP in an FP register.
626 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Op);
627 }
Chris Lattner33084492005-12-18 08:13:54 +0000628 case ISD::BR_CC: {
629 SDOperand Chain = Op.getOperand(0);
630 SDOperand CC = Op.getOperand(1);
631 SDOperand LHS = Op.getOperand(2);
632 SDOperand RHS = Op.getOperand(3);
633 SDOperand Dest = Op.getOperand(4);
634
635 // Get the condition flag.
636 if (LHS.getValueType() == MVT::i32) {
637 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, MVT::Flag, LHS, RHS);
638 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
639 } else {
640 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
641 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
642 }
643 }
644 case ISD::SELECT_CC: {
645 SDOperand LHS = Op.getOperand(0);
646 SDOperand RHS = Op.getOperand(1);
647 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
648 SDOperand TrueVal = Op.getOperand(2);
649 SDOperand FalseVal = Op.getOperand(3);
650
651 unsigned Opc;
652 Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
653 SDOperand CompareFlag = DAG.getNode(Opc, MVT::Flag, LHS, RHS);
654
655 Opc = LHS.getValueType() == MVT::i32 ?
656 V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
657 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
658 DAG.getConstant(CC, MVT::i32), CompareFlag);
659 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000660 }
661}
662
Chris Lattner33084492005-12-18 08:13:54 +0000663MachineBasicBlock *
664SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
665 MachineBasicBlock *BB) {
666 unsigned BROpcode;
667 // Figure out the conditional branch opcode to use for this select_cc.
668 switch (MI->getOpcode()) {
669 default: assert(0 && "Unknown SELECT_CC!");
670 case V8::SELECT_CC_Int_ICC:
671 case V8::SELECT_CC_FP_ICC:
672 case V8::SELECT_CC_DFP_ICC:
673 // Integer compare.
674 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
675 default: assert(0 && "Unknown integer condition code!");
676 case ISD::SETEQ: BROpcode = V8::BE; break;
677 case ISD::SETNE: BROpcode = V8::BNE; break;
678 case ISD::SETLT: BROpcode = V8::BL; break;
679 case ISD::SETGT: BROpcode = V8::BG; break;
680 case ISD::SETLE: BROpcode = V8::BLE; break;
681 case ISD::SETGE: BROpcode = V8::BGE; break;
682 case ISD::SETULT: BROpcode = V8::BCS; break;
683 case ISD::SETULE: BROpcode = V8::BLEU; break;
684 case ISD::SETUGT: BROpcode = V8::BGU; break;
685 case ISD::SETUGE: BROpcode = V8::BCC; break;
686 }
687 break;
688 case V8::SELECT_CC_Int_FCC:
689 case V8::SELECT_CC_FP_FCC:
690 case V8::SELECT_CC_DFP_FCC:
691 // FP compare.
692 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
693 default: assert(0 && "Unknown fp condition code!");
694 case ISD::SETEQ: BROpcode = V8::FBE; break;
695 case ISD::SETNE: BROpcode = V8::FBNE; break;
696 case ISD::SETLT: BROpcode = V8::FBL; break;
697 case ISD::SETGT: BROpcode = V8::FBG; break;
698 case ISD::SETLE: BROpcode = V8::FBLE; break;
699 case ISD::SETGE: BROpcode = V8::FBGE; break;
700 case ISD::SETULT: BROpcode = V8::FBUL; break;
701 case ISD::SETULE: BROpcode = V8::FBULE; break;
702 case ISD::SETUGT: BROpcode = V8::FBUG; break;
703 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
704 case ISD::SETUO: BROpcode = V8::FBU; break;
705 case ISD::SETO: BROpcode = V8::FBO; break;
706 case ISD::SETONE: BROpcode = V8::FBLG; break;
707 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
708 }
709 break;
710 }
711
712 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
713 // control-flow pattern. The incoming instruction knows the destination vreg
714 // to set, the condition code register to branch on, the true/false values to
715 // select between, and a branch opcode to use.
716 const BasicBlock *LLVM_BB = BB->getBasicBlock();
717 ilist<MachineBasicBlock>::iterator It = BB;
718 ++It;
719
720 // thisMBB:
721 // ...
722 // TrueVal = ...
723 // [f]bCC copy1MBB
724 // fallthrough --> copy0MBB
725 MachineBasicBlock *thisMBB = BB;
726 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
727 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
728 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
729 MachineFunction *F = BB->getParent();
730 F->getBasicBlockList().insert(It, copy0MBB);
731 F->getBasicBlockList().insert(It, sinkMBB);
732 // Update machine-CFG edges
733 BB->addSuccessor(copy0MBB);
734 BB->addSuccessor(sinkMBB);
735
736 // copy0MBB:
737 // %FalseValue = ...
738 // # fallthrough to sinkMBB
739 BB = copy0MBB;
740
741 // Update machine-CFG edges
742 BB->addSuccessor(sinkMBB);
743
744 // sinkMBB:
745 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
746 // ...
747 BB = sinkMBB;
748 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
749 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
750 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
751
752 delete MI; // The pseudo instruction is gone now.
753 return BB;
754}
755
Chris Lattner6c18b102005-12-17 07:47:01 +0000756//===----------------------------------------------------------------------===//
757// Instruction Selector Implementation
758//===----------------------------------------------------------------------===//
759
760//===--------------------------------------------------------------------===//
761/// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
762/// instructions for SelectionDAG operations.
763///
764namespace {
765class SparcV8DAGToDAGISel : public SelectionDAGISel {
766 SparcV8TargetLowering V8Lowering;
767public:
768 SparcV8DAGToDAGISel(TargetMachine &TM)
769 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
770
771 SDOperand Select(SDOperand Op);
772
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000773 // Complex Pattern Selectors.
774 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
775 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
776
Chris Lattner6c18b102005-12-17 07:47:01 +0000777 /// InstructionSelectBasicBlock - This callback is invoked by
778 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
779 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
780
781 virtual const char *getPassName() const {
782 return "PowerPC DAG->DAG Pattern Instruction Selection";
783 }
784
785 // Include the pieces autogenerated from the target description.
786#include "SparcV8GenDAGISel.inc"
787};
788} // end anonymous namespace
789
790/// InstructionSelectBasicBlock - This callback is invoked by
791/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
792void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
793 DEBUG(BB->dump());
794
795 // Select target instructions for the DAG.
796 DAG.setRoot(Select(DAG.getRoot()));
797 CodeGenMap.clear();
798 DAG.RemoveDeadNodes();
799
800 // Emit machine code to BB.
801 ScheduleAndEmitDAG(DAG);
802}
803
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000804bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
805 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000806 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
807 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000808 Offset = CurDAG->getTargetConstant(0, MVT::i32);
809 return true;
810 }
811
812 if (Addr.getOpcode() == ISD::ADD) {
813 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
814 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000815 if (FrameIndexSDNode *FIN =
816 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000817 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +0000818 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000819 } else {
820 Base = Select(Addr.getOperand(0));
821 }
822 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
823 return true;
824 }
825 }
826 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
827 Base = Select(Addr.getOperand(1));
828 Offset = Addr.getOperand(0).getOperand(0);
829 return true;
830 }
831 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
832 Base = Select(Addr.getOperand(0));
833 Offset = Addr.getOperand(1).getOperand(0);
834 return true;
835 }
836 }
837 Base = Select(Addr);
838 Offset = CurDAG->getTargetConstant(0, MVT::i32);
839 return true;
840}
841
Chris Lattner9034b882005-12-17 21:25:27 +0000842bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000843 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000844 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +0000845 if (Addr.getOpcode() == ISD::ADD) {
846 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
847 Predicate_simm13(Addr.getOperand(1).Val))
848 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +0000849 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
850 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
851 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +0000852 R1 = Select(Addr.getOperand(0));
853 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +0000854 return true;
855 }
856
857 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000858 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
859 return true;
860}
861
Chris Lattner6c18b102005-12-17 07:47:01 +0000862SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
863 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +0000864 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
865 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +0000866 return Op; // Already selected.
867 // If this has already been converted, use it.
868 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
869 if (CGMI != CodeGenMap.end()) return CGMI->second;
870
871 switch (N->getOpcode()) {
872 default: break;
Chris Lattner7c423b42005-12-19 07:57:53 +0000873 case ISD::Register: return Op;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000874 case ISD::FrameIndex: {
875 int FI = cast<FrameIndexSDNode>(N)->getIndex();
876 if (N->hasOneUse())
877 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
878 CurDAG->getTargetFrameIndex(FI, MVT::i32),
879 CurDAG->getTargetConstant(0, MVT::i32));
880 return CodeGenMap[Op] =
881 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
882 CurDAG->getTargetFrameIndex(FI, MVT::i32),
883 CurDAG->getTargetConstant(0, MVT::i32));
884 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000885 case V8ISD::CMPICC: {
886 // FIXME: Handle compare with immediate.
887 SDOperand LHS = Select(N->getOperand(0));
888 SDOperand RHS = Select(N->getOperand(1));
889 SDOperand Result = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
890 LHS, RHS);
891 return CodeGenMap[Op] = Result.getValue(1);
892 }
Chris Lattnerd19fc652005-12-17 22:55:57 +0000893 case ISD::ADD_PARTS: {
894 SDOperand LHSL = Select(N->getOperand(0));
895 SDOperand LHSH = Select(N->getOperand(1));
896 SDOperand RHSL = Select(N->getOperand(2));
897 SDOperand RHSH = Select(N->getOperand(3));
898 // FIXME, handle immediate RHS.
899 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
900 LHSL, RHSL);
901 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
902 Low.getValue(1));
903 CodeGenMap[SDOperand(N, 0)] = Low;
904 CodeGenMap[SDOperand(N, 1)] = Hi;
905 return Op.ResNo ? Hi : Low;
906 }
907 case ISD::SUB_PARTS: {
908 SDOperand LHSL = Select(N->getOperand(0));
909 SDOperand LHSH = Select(N->getOperand(1));
910 SDOperand RHSL = Select(N->getOperand(2));
911 SDOperand RHSH = Select(N->getOperand(3));
912 // FIXME, handle immediate RHS.
913 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
914 LHSL, RHSL);
915 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
916 Low.getValue(1));
917 CodeGenMap[SDOperand(N, 0)] = Low;
918 CodeGenMap[SDOperand(N, 1)] = Hi;
919 return Op.ResNo ? Hi : Low;
920 }
Chris Lattner7087e572005-12-17 22:39:19 +0000921 case ISD::SDIV:
922 case ISD::UDIV: {
923 // FIXME: should use a custom expander to expose the SRA to the dag.
924 SDOperand DivLHS = Select(N->getOperand(0));
925 SDOperand DivRHS = Select(N->getOperand(1));
926
927 // Set the Y register to the high-part.
928 SDOperand TopPart;
929 if (N->getOpcode() == ISD::SDIV) {
930 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
931 CurDAG->getTargetConstant(31, MVT::i32));
932 } else {
933 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
934 }
935 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
936 CurDAG->getRegister(V8::G0, MVT::i32));
937
938 // FIXME: Handle div by immediate.
939 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
940 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
941 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000942 case ISD::MULHU:
943 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000944 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000945 SDOperand MulLHS = Select(N->getOperand(0));
946 SDOperand MulRHS = Select(N->getOperand(1));
947 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
948 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
949 MulLHS, MulRHS);
950 // The high part is in the Y register.
951 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
952 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000953 case ISD::CALL:
954 // FIXME: This is a workaround for a bug in tblgen.
955 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
956 // Emits: (CALL:void (tglobaladdr:i32):$dst)
957 // Pattern complexity = 2 cost = 1
958 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +0000959 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
960 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000961 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000962 SDOperand Chain = N->getOperand(0);
963 SDOperand Tmp0 = N1;
964 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000965 SDOperand Result;
966 if (N->getNumOperands() == 3) {
967 InFlag = Select(N->getOperand(2));
968 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
969 Chain, InFlag);
970 } else {
971 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
972 Chain);
973 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000974 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
975 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
976 return Result.getValue(Op.ResNo);
977 }
978 P47Fail:;
979
Chris Lattner6c18b102005-12-17 07:47:01 +0000980 }
981
982 return SelectCode(Op);
983}
984
985
986/// createPPCISelDag - This pass converts a legalized DAG into a
987/// PowerPC-specific DAG, ready for instruction scheduling.
988///
989FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
990 return new SparcV8DAGToDAGISel(TM);
991}