blob: a6d2f06a44cf1ebcb6f372e489bfbb5baf98303d [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 Lattner065c8962005-12-18 07:13:32 +0000111 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
112
Chris Lattner4d55aca2005-12-18 01:20:35 +0000113 // Sparc has no select or setcc: expand to SELECT_CC.
114 setOperationAction(ISD::SELECT, MVT::i32, Expand);
115 setOperationAction(ISD::SELECT, MVT::f32, Expand);
116 setOperationAction(ISD::SELECT, MVT::f64, Expand);
117 setOperationAction(ISD::SETCC, MVT::i32, Expand);
118 setOperationAction(ISD::SETCC, MVT::f32, Expand);
119 setOperationAction(ISD::SETCC, MVT::f64, Expand);
120
121 // Sparc doesn't have BRCOND either, it has BR_CC.
122 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
123 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
124 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
125 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
126 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
127 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
128
Chris Lattner33084492005-12-18 08:13:54 +0000129 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
130 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
131 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
132
Chris Lattner6c18b102005-12-17 07:47:01 +0000133 computeRegisterProperties();
134}
135
Chris Lattner384e5ef2005-12-18 13:33:06 +0000136/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
137/// either one or two GPRs, including FP values. TODO: we should pass FP values
138/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000139std::vector<SDOperand>
140SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000141 MachineFunction &MF = DAG.getMachineFunction();
142 SSARegMap *RegMap = MF.getSSARegMap();
143 std::vector<SDOperand> ArgValues;
144
Chris Lattner384e5ef2005-12-18 13:33:06 +0000145 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000146 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
147 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000148
149 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
150 unsigned ArgOffset = 68;
151
152 SDOperand Root = DAG.getRoot();
153 std::vector<SDOperand> OutChains;
154
Chris Lattnera01b7572005-12-17 08:03:24 +0000155 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
156 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000157
158 switch (ObjectVT) {
159 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000160 case MVT::i1:
161 case MVT::i8:
162 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000163 case MVT::i32:
164 if (I->use_empty()) { // Argument is dead.
165 if (CurArgReg < ArgRegEnd) ++CurArgReg;
166 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
167 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
168 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
169 MF.addLiveIn(*CurArgReg++, VReg);
170 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
171 if (ObjectVT != MVT::i32) {
172 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
173 : ISD::AssertZext;
174 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
175 DAG.getValueType(ObjectVT));
176 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
177 }
178 ArgValues.push_back(Arg);
179 } else {
180 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
181 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
182 SDOperand Load;
183 if (ObjectVT == MVT::i32) {
184 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
185 } else {
186 unsigned LoadOp =
187 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
188
189 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
190 DAG.getSrcValue(0), ObjectVT);
191 }
192 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000193 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000194
195 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000196 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000197 case MVT::f32:
198 if (I->use_empty()) { // Argument is dead.
199 if (CurArgReg < ArgRegEnd) ++CurArgReg;
200 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
201 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
202 // FP value is passed in an integer register.
203 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
204 MF.addLiveIn(*CurArgReg++, VReg);
205 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
206
207 // We use the stack space that is already reserved for this reg.
208 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
209 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
210
211 SDOperand SV = DAG.getSrcValue(0);
212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Root,
213 Arg, FIPtr, SV);
214 ArgValues.push_back(DAG.getLoad(MVT::f32, Store, FIPtr, SV));
215 }
216 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000217 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000218
219 case MVT::i64:
220 case MVT::f64:
221 if (I->use_empty()) { // Argument is dead.
222 if (CurArgReg < ArgRegEnd) ++CurArgReg;
223 if (CurArgReg < ArgRegEnd) ++CurArgReg;
224 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
225 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
226 ((CurArgReg-ArgRegs) & 1) == 0) {
227 // If this is a double argument and the whole thing lives on the stack,
228 // and the argument is aligned, load the double straight from the stack.
229 // We can't do a load in cases like void foo([6ints], int,double),
230 // because the double wouldn't be aligned!
231 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
232 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
233 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
234 DAG.getSrcValue(0)));
235 } else {
236 SDOperand HiVal;
237 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
238 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
239 MF.addLiveIn(*CurArgReg++, VRegHi);
240 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
241 } else {
242 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
243 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
244 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
245 }
246
247 SDOperand LoVal;
248 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
249 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
250 MF.addLiveIn(*CurArgReg++, VRegLo);
251 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
252 } else {
253 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
254 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
255 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
256 }
257
258 // Compose the two halves together into an i64 unit.
259 SDOperand WholeValue =
260 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
261
262 if (ObjectVT == MVT::i64) {
263 // If we are emitting an i64, this is what we want.
264 ArgValues.push_back(WholeValue);
265 } else {
266 assert(ObjectVT == MVT::f64);
267 // Otherwise, emit a store to the stack and reload into FPR.
268 int FrameIdx = MF.getFrameInfo()->CreateStackObject(8, 8);
269 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
270 SDOperand SV = DAG.getSrcValue(0);
271 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Root,
272 WholeValue, FIPtr, SV);
273 ArgValues.push_back(DAG.getLoad(MVT::f64, Store, FIPtr, SV));
274 }
275 }
276 ArgOffset += 8;
277 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000278 }
279 }
280
Chris Lattner384e5ef2005-12-18 13:33:06 +0000281 // Store remaining ArgRegs to the stack if this is a varargs function.
282 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000283 // Remember the vararg offset for the va_start implementation.
284 VarArgsFrameOffset = ArgOffset;
285
Chris Lattner384e5ef2005-12-18 13:33:06 +0000286 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
287 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
288 MF.addLiveIn(*CurArgReg, VReg);
289 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
290
291 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
292 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
293
294 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
295 Arg, FIPtr, DAG.getSrcValue(0)));
296 ArgOffset += 4;
297 }
298 }
299
300 if (!OutChains.empty())
301 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000302
303 // Finally, inform the code generator which regs we return values in.
304 switch (getValueType(F.getReturnType())) {
305 default: assert(0 && "Unknown type!");
306 case MVT::isVoid: break;
307 case MVT::i1:
308 case MVT::i8:
309 case MVT::i16:
310 case MVT::i32:
311 MF.addLiveOut(V8::I0);
312 break;
313 case MVT::i64:
314 MF.addLiveOut(V8::I0);
315 MF.addLiveOut(V8::I1);
316 break;
317 case MVT::f32:
318 MF.addLiveOut(V8::F0);
319 break;
320 case MVT::f64:
321 MF.addLiveOut(V8::D0);
322 break;
323 }
324
325 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000326}
327
328std::pair<SDOperand, SDOperand>
329SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
330 bool isVarArg, unsigned CC,
331 bool isTailCall, SDOperand Callee,
332 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000333 MachineFunction &MF = DAG.getMachineFunction();
334 // Count the size of the outgoing arguments.
335 unsigned ArgsSize = 0;
336 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
337 switch (getValueType(Args[i].second)) {
338 default: assert(0 && "Unknown value type!");
339 case MVT::i1:
340 case MVT::i8:
341 case MVT::i16:
342 case MVT::i32:
343 case MVT::f32:
344 ArgsSize += 4;
345 break;
346 case MVT::i64:
347 case MVT::f64:
348 ArgsSize += 8;
349 break;
350 }
351 }
352 if (ArgsSize > 4*6)
353 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
354 else
355 ArgsSize = 0;
356
357 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
358 DAG.getConstant(ArgsSize, getPointerTy()));
359
360 SDOperand StackPtr, NullSV;
361 std::vector<SDOperand> Stores;
362 std::vector<SDOperand> RegValuesToPass;
363 unsigned ArgOffset = 68;
364 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
365 SDOperand Val = Args[i].first;
366 MVT::ValueType ObjectVT = Val.getValueType();
367 SDOperand ValToStore;
368 unsigned ObjSize;
369 switch (ObjectVT) {
370 default: assert(0 && "Unhandled argument type!");
371 case MVT::i1:
372 case MVT::i8:
373 case MVT::i16:
374 // Promote the integer to 32-bits. If the input type is signed, use a
375 // sign extend, otherwise use a zero extend.
376 if (Args[i].second->isSigned())
377 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
378 else
379 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
380 // FALL THROUGH
381 case MVT::i32:
382 ObjSize = 4;
383
384 if (RegValuesToPass.size() >= 6) {
385 ValToStore = Val;
386 } else {
387 RegValuesToPass.push_back(Val);
388 }
389 break;
390 case MVT::f32:
391 ObjSize = 4;
392 if (RegValuesToPass.size() >= 6) {
393 ValToStore = Val;
394 } else {
395 // Convert this to a FP value in an int reg.
396 int FrameIdx = MF.getFrameInfo()->CreateStackObject(4, 4);
397 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
398 SDOperand SV = DAG.getSrcValue(0);
399 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
400 Val, FIPtr, SV);
401 Val = DAG.getLoad(MVT::i32, Store, FIPtr, SV);
402 RegValuesToPass.push_back(Val);
403 }
404 break;
405 case MVT::f64: {
406 ObjSize = 8;
407 // If we can store this directly into the outgoing slot, do so. We can
408 // do this when all ArgRegs are used and if the outgoing slot is aligned.
409 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
410 ValToStore = Val;
411 break;
412 }
413
414 // Otherwise, convert this to a FP value in int regs.
415 int FrameIdx = MF.getFrameInfo()->CreateStackObject(8, 8);
416 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
417 SDOperand SV = DAG.getSrcValue(0);
418 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, Chain,
419 Val, FIPtr, SV);
420 Val = DAG.getLoad(MVT::i64, Store, FIPtr, SV);
421 }
422 // FALL THROUGH
423 case MVT::i64:
424 ObjSize = 8;
425 if (RegValuesToPass.size() >= 6) {
426 ValToStore = Val; // Whole thing is passed in memory.
427 break;
428 }
429
430 // Split the value into top and bottom part. Top part goes in a reg.
431 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
432 DAG.getConstant(1, MVT::i32));
433 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
434 DAG.getConstant(0, MVT::i32));
435 RegValuesToPass.push_back(Hi);
436
437 if (RegValuesToPass.size() >= 6) {
438 ValToStore = Lo;
439 } else {
440 RegValuesToPass.push_back(Lo);
441 }
442 break;
443 }
444
445 if (ValToStore.Val) {
446 if (!StackPtr.Val) {
447 StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(), V8::SP, MVT::i32);
448 NullSV = DAG.getSrcValue(NULL);
449 }
450 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
451 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
452 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
453 ValToStore, PtrOff, NullSV));
454 }
455 ArgOffset += ObjSize;
456 }
457
458 // Emit all stores, make sure the occur before any copies into physregs.
459 if (!Stores.empty())
460 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
461
462 static const unsigned ArgRegs[] = {
463 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
464 };
465
466 // Build a sequence of copy-to-reg nodes chained together with token chain
467 // and flag operands which copy the outgoing args into O[0-5].
468 SDOperand InFlag;
469 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
470 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
471 InFlag = Chain.getValue(1);
472 }
473
474 std::vector<MVT::ValueType> RetVals;
475 RetVals.push_back(MVT::Other);
476 RetVals.push_back(MVT::Flag);
477
478 // If the callee is a GlobalAddress node (quite common, every direct call is)
479 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
480 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
481 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
482
483 std::vector<MVT::ValueType> NodeTys;
484 NodeTys.push_back(MVT::Other); // Returns a chain
485 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
486 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
487 InFlag = Chain.getValue(1);
488
489 MVT::ValueType RetTyVT = getValueType(RetTy);
490 SDOperand RetVal;
491 if (RetTyVT != MVT::isVoid) {
492 switch (RetTyVT) {
493 default: assert(0 && "Unknown value type to return!");
494 case MVT::i1:
495 case MVT::i8:
496 case MVT::i16:
497 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
498 Chain = RetVal.getValue(1);
499
500 // Add a note to keep track of whether it is sign or zero extended.
501 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
502 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
503 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
504 break;
505 case MVT::i32:
506 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
507 Chain = RetVal.getValue(1);
508 break;
509 case MVT::f32:
510 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
511 Chain = RetVal.getValue(1);
512 break;
513 case MVT::f64:
514 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
515 Chain = RetVal.getValue(1);
516 break;
517 case MVT::i64:
518 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
519 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
520 Lo.getValue(2));
521 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
522 Chain = Hi.getValue(1);
523 break;
524 }
525 }
526
527 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
528 DAG.getConstant(ArgsSize, getPointerTy()));
529
530 MVT::ValueType ActualRetTyVT = RetTyVT;
531 if (RetTyVT >= MVT::i1 && RetTyVT <= MVT::i16)
532 ActualRetTyVT = MVT::i32; // Promote result to i32.
533
534 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 Lattner6c18b102005-12-17 07:47:01 +0000567 assert(0 && "Unimp");
568 abort();
569}
570
Chris Lattner4d55aca2005-12-18 01:20:35 +0000571std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
572LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
573 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000574 assert(0 && "Unimp");
575 abort();
576}
577
Chris Lattner4d55aca2005-12-18 01:20:35 +0000578std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
579LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
580 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000581 assert(0 && "Unimp");
582 abort();
583}
584
Chris Lattner4d55aca2005-12-18 01:20:35 +0000585SDOperand SparcV8TargetLowering::
586LowerOperation(SDOperand Op, SelectionDAG &DAG) {
587 switch (Op.getOpcode()) {
588 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000589 case ISD::GlobalAddress: {
590 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
591 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
592 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
593 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
594 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
595 }
Chris Lattner76acc872005-12-18 02:37:35 +0000596 case ISD::ConstantPool: {
597 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
598 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
599 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
600 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
601 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
602 }
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000603 case ISD::FP_TO_SINT: {
604 // Convert the fp value to integer in an FP register.
605 Op = DAG.getNode(V8ISD::FTOI, Op.getOperand(0).getValueType(),
606 Op.getOperand(0));
607 int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
608 int FrameIdx =
609 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
610 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
611 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
612 Op, FI, DAG.getSrcValue(0));
613 return DAG.getLoad(MVT::i32, ST, FI, DAG.getSrcValue(0));
614 }
615 case ISD::SINT_TO_FP: {
616 int Size = Op.getOperand(0).getValueType() == MVT::f32 ? 4 : 8;
617 int FrameIdx =
618 DAG.getMachineFunction().getFrameInfo()->CreateStackObject(Size, Size);
619 SDOperand FI = DAG.getFrameIndex(FrameIdx, MVT::i32);
620 SDOperand ST = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
621 Op.getOperand(0), FI, DAG.getSrcValue(0));
622
623 Op = DAG.getLoad(Op.getValueType(), ST, FI, DAG.getSrcValue(0));
624
625 // 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 Lattner4d55aca2005-12-18 01:20:35 +0000873 case ISD::BasicBlock: return CodeGenMap[Op] = 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);
959 if (N1.getOpcode() != ISD::TargetGlobalAddress) goto P47Fail;
960 SDOperand N2 = N->getOperand(2);
961 SDOperand InFlag = SDOperand(0,0);
962 SDOperand Chain = N->getOperand(0);
963 SDOperand Tmp0 = N1;
964 Chain = Select(Chain);
965 InFlag = Select(N2);
966 SDOperand Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0, Chain, InFlag);
967 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
968 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
969 return Result.getValue(Op.ResNo);
970 }
971 P47Fail:;
972
Chris Lattner6c18b102005-12-17 07:47:01 +0000973 }
974
975 return SelectCode(Op);
976}
977
978
979/// createPPCISelDag - This pass converts a legalized DAG into a
980/// PowerPC-specific DAG, ready for instruction scheduling.
981///
982FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
983 return new SparcV8DAGToDAGISel(TM);
984}