blob: b418f575f17e647689d9dd06e12f1410990406d8 [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);
Nate Begeman35ef9132006-01-11 21:21:00 +0000149 setOperationAction(ISD::ROTL , MVT::i32, Expand);
150 setOperationAction(ISD::ROTR , MVT::i32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000151
152 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
153 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
154 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000155
156 // We don't have line number support yet.
157 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000158 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
159 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000160
Chris Lattner6c18b102005-12-17 07:47:01 +0000161 computeRegisterProperties();
162}
163
Chris Lattner384e5ef2005-12-18 13:33:06 +0000164/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
165/// either one or two GPRs, including FP values. TODO: we should pass FP values
166/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000167std::vector<SDOperand>
168SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000169 MachineFunction &MF = DAG.getMachineFunction();
170 SSARegMap *RegMap = MF.getSSARegMap();
171 std::vector<SDOperand> ArgValues;
172
Chris Lattner384e5ef2005-12-18 13:33:06 +0000173 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000174 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
175 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000176
177 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
178 unsigned ArgOffset = 68;
179
180 SDOperand Root = DAG.getRoot();
181 std::vector<SDOperand> OutChains;
182
Chris Lattnera01b7572005-12-17 08:03:24 +0000183 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
184 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000185
186 switch (ObjectVT) {
187 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000188 case MVT::i1:
189 case MVT::i8:
190 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000191 case MVT::i32:
192 if (I->use_empty()) { // Argument is dead.
193 if (CurArgReg < ArgRegEnd) ++CurArgReg;
194 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
195 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
196 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
197 MF.addLiveIn(*CurArgReg++, VReg);
198 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
199 if (ObjectVT != MVT::i32) {
200 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
201 : ISD::AssertZext;
202 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
203 DAG.getValueType(ObjectVT));
204 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
205 }
206 ArgValues.push_back(Arg);
207 } else {
208 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
209 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
210 SDOperand Load;
211 if (ObjectVT == MVT::i32) {
212 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
213 } else {
214 unsigned LoadOp =
215 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
216
217 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
218 DAG.getSrcValue(0), ObjectVT);
219 }
220 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000221 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000222
223 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000224 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000225 case MVT::f32:
226 if (I->use_empty()) { // Argument is dead.
227 if (CurArgReg < ArgRegEnd) ++CurArgReg;
228 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
229 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
230 // FP value is passed in an integer register.
231 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
232 MF.addLiveIn(*CurArgReg++, VReg);
233 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
234
Chris Lattnera01874f2005-12-23 02:31:39 +0000235 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
236 ArgValues.push_back(Arg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000237 }
238 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000239 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000240
241 case MVT::i64:
242 case MVT::f64:
243 if (I->use_empty()) { // Argument is dead.
244 if (CurArgReg < ArgRegEnd) ++CurArgReg;
245 if (CurArgReg < ArgRegEnd) ++CurArgReg;
246 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
247 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
248 ((CurArgReg-ArgRegs) & 1) == 0) {
249 // If this is a double argument and the whole thing lives on the stack,
250 // and the argument is aligned, load the double straight from the stack.
251 // We can't do a load in cases like void foo([6ints], int,double),
252 // because the double wouldn't be aligned!
253 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
254 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
255 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
256 DAG.getSrcValue(0)));
257 } else {
258 SDOperand HiVal;
259 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
260 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
261 MF.addLiveIn(*CurArgReg++, VRegHi);
262 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
263 } else {
264 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
265 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
266 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
267 }
268
269 SDOperand LoVal;
270 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
271 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
272 MF.addLiveIn(*CurArgReg++, VRegLo);
273 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
274 } else {
275 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
276 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
277 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
278 }
279
280 // Compose the two halves together into an i64 unit.
281 SDOperand WholeValue =
282 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000283
284 // If we want a double, do a bit convert.
285 if (ObjectVT == MVT::f64)
286 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
287
288 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000289 }
290 ArgOffset += 8;
291 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000292 }
293 }
294
Chris Lattner384e5ef2005-12-18 13:33:06 +0000295 // Store remaining ArgRegs to the stack if this is a varargs function.
296 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000297 // Remember the vararg offset for the va_start implementation.
298 VarArgsFrameOffset = ArgOffset;
299
Chris Lattner384e5ef2005-12-18 13:33:06 +0000300 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
301 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
302 MF.addLiveIn(*CurArgReg, VReg);
303 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
304
305 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
306 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
307
308 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
309 Arg, FIPtr, DAG.getSrcValue(0)));
310 ArgOffset += 4;
311 }
312 }
313
314 if (!OutChains.empty())
315 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000316
317 // Finally, inform the code generator which regs we return values in.
318 switch (getValueType(F.getReturnType())) {
319 default: assert(0 && "Unknown type!");
320 case MVT::isVoid: break;
321 case MVT::i1:
322 case MVT::i8:
323 case MVT::i16:
324 case MVT::i32:
325 MF.addLiveOut(V8::I0);
326 break;
327 case MVT::i64:
328 MF.addLiveOut(V8::I0);
329 MF.addLiveOut(V8::I1);
330 break;
331 case MVT::f32:
332 MF.addLiveOut(V8::F0);
333 break;
334 case MVT::f64:
335 MF.addLiveOut(V8::D0);
336 break;
337 }
338
339 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000340}
341
342std::pair<SDOperand, SDOperand>
343SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
344 bool isVarArg, unsigned CC,
345 bool isTailCall, SDOperand Callee,
346 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000347 MachineFunction &MF = DAG.getMachineFunction();
348 // Count the size of the outgoing arguments.
349 unsigned ArgsSize = 0;
350 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
351 switch (getValueType(Args[i].second)) {
352 default: assert(0 && "Unknown value type!");
353 case MVT::i1:
354 case MVT::i8:
355 case MVT::i16:
356 case MVT::i32:
357 case MVT::f32:
358 ArgsSize += 4;
359 break;
360 case MVT::i64:
361 case MVT::f64:
362 ArgsSize += 8;
363 break;
364 }
365 }
366 if (ArgsSize > 4*6)
367 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
368 else
369 ArgsSize = 0;
370
Chris Lattner6554bef2005-12-19 01:15:13 +0000371 // Keep stack frames 8-byte aligned.
372 ArgsSize = (ArgsSize+7) & ~7;
373
Chris Lattner2db3ff62005-12-18 15:55:15 +0000374 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
375 DAG.getConstant(ArgsSize, getPointerTy()));
376
377 SDOperand StackPtr, NullSV;
378 std::vector<SDOperand> Stores;
379 std::vector<SDOperand> RegValuesToPass;
380 unsigned ArgOffset = 68;
381 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
382 SDOperand Val = Args[i].first;
383 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000384 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000385 unsigned ObjSize;
386 switch (ObjectVT) {
387 default: assert(0 && "Unhandled argument type!");
388 case MVT::i1:
389 case MVT::i8:
390 case MVT::i16:
391 // Promote the integer to 32-bits. If the input type is signed, use a
392 // sign extend, otherwise use a zero extend.
393 if (Args[i].second->isSigned())
394 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
395 else
396 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
397 // FALL THROUGH
398 case MVT::i32:
399 ObjSize = 4;
400
401 if (RegValuesToPass.size() >= 6) {
402 ValToStore = Val;
403 } else {
404 RegValuesToPass.push_back(Val);
405 }
406 break;
407 case MVT::f32:
408 ObjSize = 4;
409 if (RegValuesToPass.size() >= 6) {
410 ValToStore = Val;
411 } else {
412 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000413 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000414 RegValuesToPass.push_back(Val);
415 }
416 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000417 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000418 ObjSize = 8;
419 // If we can store this directly into the outgoing slot, do so. We can
420 // do this when all ArgRegs are used and if the outgoing slot is aligned.
421 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
422 ValToStore = Val;
423 break;
424 }
425
426 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000427 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000428 // FALL THROUGH
429 case MVT::i64:
430 ObjSize = 8;
431 if (RegValuesToPass.size() >= 6) {
432 ValToStore = Val; // Whole thing is passed in memory.
433 break;
434 }
435
436 // Split the value into top and bottom part. Top part goes in a reg.
437 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
438 DAG.getConstant(1, MVT::i32));
439 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
440 DAG.getConstant(0, MVT::i32));
441 RegValuesToPass.push_back(Hi);
442
443 if (RegValuesToPass.size() >= 6) {
444 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000445 ArgOffset += 4;
446 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000447 } else {
448 RegValuesToPass.push_back(Lo);
449 }
450 break;
451 }
452
453 if (ValToStore.Val) {
454 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000455 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000456 NullSV = DAG.getSrcValue(NULL);
457 }
458 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
459 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
460 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
461 ValToStore, PtrOff, NullSV));
462 }
463 ArgOffset += ObjSize;
464 }
465
466 // Emit all stores, make sure the occur before any copies into physregs.
467 if (!Stores.empty())
468 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
469
470 static const unsigned ArgRegs[] = {
471 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
472 };
473
474 // Build a sequence of copy-to-reg nodes chained together with token chain
475 // and flag operands which copy the outgoing args into O[0-5].
476 SDOperand InFlag;
477 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
478 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
479 InFlag = Chain.getValue(1);
480 }
481
Chris Lattner2db3ff62005-12-18 15:55:15 +0000482 // If the callee is a GlobalAddress node (quite common, every direct call is)
483 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
484 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
485 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
486
487 std::vector<MVT::ValueType> NodeTys;
488 NodeTys.push_back(MVT::Other); // Returns a chain
489 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000490 if (InFlag.Val)
491 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
492 else
493 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000494 InFlag = Chain.getValue(1);
495
496 MVT::ValueType RetTyVT = getValueType(RetTy);
497 SDOperand RetVal;
498 if (RetTyVT != MVT::isVoid) {
499 switch (RetTyVT) {
500 default: assert(0 && "Unknown value type to return!");
501 case MVT::i1:
502 case MVT::i8:
503 case MVT::i16:
504 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
505 Chain = RetVal.getValue(1);
506
507 // Add a note to keep track of whether it is sign or zero extended.
508 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
509 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
510 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
511 break;
512 case MVT::i32:
513 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
514 Chain = RetVal.getValue(1);
515 break;
516 case MVT::f32:
517 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
518 Chain = RetVal.getValue(1);
519 break;
520 case MVT::f64:
521 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
522 Chain = RetVal.getValue(1);
523 break;
524 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000525 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000526 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
527 Lo.getValue(2));
528 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
529 Chain = Hi.getValue(1);
530 break;
531 }
532 }
533
534 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
535 DAG.getConstant(ArgsSize, getPointerTy()));
536
Chris Lattner2db3ff62005-12-18 15:55:15 +0000537 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000538}
539
540SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
541 SelectionDAG &DAG) {
Chris Lattnerdab05f02005-12-18 21:03:04 +0000542 SDOperand Copy;
543 switch (Op.getValueType()) {
544 default: assert(0 && "Unknown type to return!");
545 case MVT::i32:
546 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
547 break;
548 case MVT::f32:
549 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
550 break;
551 case MVT::f64:
552 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
553 break;
554 case MVT::i64:
Chris Lattner4b486312005-12-17 08:15:09 +0000555 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
556 DAG.getConstant(1, MVT::i32));
557 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
558 DAG.getConstant(0, MVT::i32));
Chris Lattnerdab05f02005-12-18 21:03:04 +0000559 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
560 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
561 break;
Chris Lattner4b486312005-12-17 08:15:09 +0000562 }
Chris Lattnerdab05f02005-12-18 21:03:04 +0000563 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Chris Lattner6c18b102005-12-17 07:47:01 +0000564}
565
Chris Lattner4d55aca2005-12-18 01:20:35 +0000566SDOperand SparcV8TargetLowering::
567LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
568 SelectionDAG &DAG) {
569
Chris Lattnerc4769bb2005-12-23 06:24:04 +0000570 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
571 DAG.getRegister(V8::I6, MVT::i32),
572 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
573 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
574 VAListP, DAG.getSrcValue(VAListV));
Chris Lattner6c18b102005-12-17 07:47:01 +0000575}
576
Chris Lattner4d55aca2005-12-18 01:20:35 +0000577std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
578LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
579 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner74fa64b2005-12-23 06:37:38 +0000580 // Load the pointer out of the valist.
581 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
582 VAListP, DAG.getSrcValue(VAListV));
583 MVT::ValueType ArgVT = getValueType(ArgTy);
584 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
585 Ptr, DAG.getSrcValue(NULL));
586 // Increment the pointer.
587 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
588 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
589 // Store it back to the valist.
590 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
591 VAListP, DAG.getSrcValue(VAListV));
592 return std::make_pair(Val, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000593}
594
Chris Lattner4d55aca2005-12-18 01:20:35 +0000595std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
596LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
597 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000598 assert(0 && "Unimp");
599 abort();
600}
601
Chris Lattner4d55aca2005-12-18 01:20:35 +0000602SDOperand SparcV8TargetLowering::
603LowerOperation(SDOperand Op, SelectionDAG &DAG) {
604 switch (Op.getOpcode()) {
605 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000606 case ISD::GlobalAddress: {
607 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
608 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
609 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
610 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
611 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
612 }
Chris Lattner76acc872005-12-18 02:37:35 +0000613 case ISD::ConstantPool: {
614 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
615 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
616 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
617 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
618 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
619 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000620 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000621 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000622 assert(Op.getValueType() == MVT::i32);
623 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
624 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000625 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000626 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner3fbb7262006-01-11 07:27:40 +0000627 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000628 // Convert the int value to FP in an FP register.
Chris Lattner3fbb7262006-01-11 07:27:40 +0000629 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000630 }
Chris Lattner33084492005-12-18 08:13:54 +0000631 case ISD::BR_CC: {
632 SDOperand Chain = Op.getOperand(0);
633 SDOperand CC = Op.getOperand(1);
634 SDOperand LHS = Op.getOperand(2);
635 SDOperand RHS = Op.getOperand(3);
636 SDOperand Dest = Op.getOperand(4);
637
638 // Get the condition flag.
639 if (LHS.getValueType() == MVT::i32) {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000640 std::vector<MVT::ValueType> VTs;
641 VTs.push_back(MVT::i32);
642 VTs.push_back(MVT::Flag);
643 std::vector<SDOperand> Ops;
644 Ops.push_back(LHS);
645 Ops.push_back(RHS);
646 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops);
Chris Lattner33084492005-12-18 08:13:54 +0000647 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
648 } else {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000649 std::vector<MVT::ValueType> VTs;
650 VTs.push_back(MVT::i32);
651 VTs.push_back(MVT::Flag);
652 std::vector<SDOperand> Ops;
653 Ops.push_back(LHS);
654 Ops.push_back(RHS);
655 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, VTs, Ops);
Chris Lattner33084492005-12-18 08:13:54 +0000656 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
657 }
658 }
659 case ISD::SELECT_CC: {
660 SDOperand LHS = Op.getOperand(0);
661 SDOperand RHS = Op.getOperand(1);
662 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
663 SDOperand TrueVal = Op.getOperand(2);
664 SDOperand FalseVal = Op.getOperand(3);
665
666 unsigned Opc;
667 Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000668 std::vector<MVT::ValueType> VTs;
669 VTs.push_back(LHS.getValueType());
670 VTs.push_back(MVT::Flag);
671 std::vector<SDOperand> Ops;
672 Ops.push_back(LHS);
673 Ops.push_back(RHS);
674 SDOperand CompareFlag = DAG.getNode(Opc, VTs, Ops).getValue(1);
Chris Lattner33084492005-12-18 08:13:54 +0000675
676 Opc = LHS.getValueType() == MVT::i32 ?
677 V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
678 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
679 DAG.getConstant(CC, MVT::i32), CompareFlag);
680 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000681 }
682}
683
Chris Lattner33084492005-12-18 08:13:54 +0000684MachineBasicBlock *
685SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
686 MachineBasicBlock *BB) {
687 unsigned BROpcode;
688 // Figure out the conditional branch opcode to use for this select_cc.
689 switch (MI->getOpcode()) {
690 default: assert(0 && "Unknown SELECT_CC!");
691 case V8::SELECT_CC_Int_ICC:
692 case V8::SELECT_CC_FP_ICC:
693 case V8::SELECT_CC_DFP_ICC:
694 // Integer compare.
695 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
696 default: assert(0 && "Unknown integer condition code!");
697 case ISD::SETEQ: BROpcode = V8::BE; break;
698 case ISD::SETNE: BROpcode = V8::BNE; break;
699 case ISD::SETLT: BROpcode = V8::BL; break;
700 case ISD::SETGT: BROpcode = V8::BG; break;
701 case ISD::SETLE: BROpcode = V8::BLE; break;
702 case ISD::SETGE: BROpcode = V8::BGE; break;
703 case ISD::SETULT: BROpcode = V8::BCS; break;
704 case ISD::SETULE: BROpcode = V8::BLEU; break;
705 case ISD::SETUGT: BROpcode = V8::BGU; break;
706 case ISD::SETUGE: BROpcode = V8::BCC; break;
707 }
708 break;
709 case V8::SELECT_CC_Int_FCC:
710 case V8::SELECT_CC_FP_FCC:
711 case V8::SELECT_CC_DFP_FCC:
712 // FP compare.
713 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
714 default: assert(0 && "Unknown fp condition code!");
715 case ISD::SETEQ: BROpcode = V8::FBE; break;
716 case ISD::SETNE: BROpcode = V8::FBNE; break;
717 case ISD::SETLT: BROpcode = V8::FBL; break;
718 case ISD::SETGT: BROpcode = V8::FBG; break;
719 case ISD::SETLE: BROpcode = V8::FBLE; break;
720 case ISD::SETGE: BROpcode = V8::FBGE; break;
721 case ISD::SETULT: BROpcode = V8::FBUL; break;
722 case ISD::SETULE: BROpcode = V8::FBULE; break;
723 case ISD::SETUGT: BROpcode = V8::FBUG; break;
724 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
725 case ISD::SETUO: BROpcode = V8::FBU; break;
726 case ISD::SETO: BROpcode = V8::FBO; break;
727 case ISD::SETONE: BROpcode = V8::FBLG; break;
728 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
729 }
730 break;
731 }
732
733 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
734 // control-flow pattern. The incoming instruction knows the destination vreg
735 // to set, the condition code register to branch on, the true/false values to
736 // select between, and a branch opcode to use.
737 const BasicBlock *LLVM_BB = BB->getBasicBlock();
738 ilist<MachineBasicBlock>::iterator It = BB;
739 ++It;
740
741 // thisMBB:
742 // ...
743 // TrueVal = ...
744 // [f]bCC copy1MBB
745 // fallthrough --> copy0MBB
746 MachineBasicBlock *thisMBB = BB;
747 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
748 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
749 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
750 MachineFunction *F = BB->getParent();
751 F->getBasicBlockList().insert(It, copy0MBB);
752 F->getBasicBlockList().insert(It, sinkMBB);
753 // Update machine-CFG edges
754 BB->addSuccessor(copy0MBB);
755 BB->addSuccessor(sinkMBB);
756
757 // copy0MBB:
758 // %FalseValue = ...
759 // # fallthrough to sinkMBB
760 BB = copy0MBB;
761
762 // Update machine-CFG edges
763 BB->addSuccessor(sinkMBB);
764
765 // sinkMBB:
766 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
767 // ...
768 BB = sinkMBB;
769 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
770 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
771 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
772
773 delete MI; // The pseudo instruction is gone now.
774 return BB;
775}
776
Chris Lattner6c18b102005-12-17 07:47:01 +0000777//===----------------------------------------------------------------------===//
778// Instruction Selector Implementation
779//===----------------------------------------------------------------------===//
780
781//===--------------------------------------------------------------------===//
782/// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
783/// instructions for SelectionDAG operations.
784///
785namespace {
786class SparcV8DAGToDAGISel : public SelectionDAGISel {
787 SparcV8TargetLowering V8Lowering;
788public:
789 SparcV8DAGToDAGISel(TargetMachine &TM)
790 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
791
792 SDOperand Select(SDOperand Op);
793
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000794 // Complex Pattern Selectors.
795 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
796 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
797
Chris Lattner6c18b102005-12-17 07:47:01 +0000798 /// InstructionSelectBasicBlock - This callback is invoked by
799 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
800 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
801
802 virtual const char *getPassName() const {
803 return "PowerPC DAG->DAG Pattern Instruction Selection";
804 }
805
806 // Include the pieces autogenerated from the target description.
807#include "SparcV8GenDAGISel.inc"
808};
809} // end anonymous namespace
810
811/// InstructionSelectBasicBlock - This callback is invoked by
812/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
813void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
814 DEBUG(BB->dump());
815
816 // Select target instructions for the DAG.
817 DAG.setRoot(Select(DAG.getRoot()));
818 CodeGenMap.clear();
819 DAG.RemoveDeadNodes();
820
821 // Emit machine code to BB.
822 ScheduleAndEmitDAG(DAG);
823}
824
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000825bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
826 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000827 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
828 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000829 Offset = CurDAG->getTargetConstant(0, MVT::i32);
830 return true;
831 }
832
833 if (Addr.getOpcode() == ISD::ADD) {
834 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
835 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000836 if (FrameIndexSDNode *FIN =
837 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000838 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +0000839 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000840 } else {
841 Base = Select(Addr.getOperand(0));
842 }
843 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
844 return true;
845 }
846 }
847 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
848 Base = Select(Addr.getOperand(1));
849 Offset = Addr.getOperand(0).getOperand(0);
850 return true;
851 }
852 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
853 Base = Select(Addr.getOperand(0));
854 Offset = Addr.getOperand(1).getOperand(0);
855 return true;
856 }
857 }
858 Base = Select(Addr);
859 Offset = CurDAG->getTargetConstant(0, MVT::i32);
860 return true;
861}
862
Chris Lattner9034b882005-12-17 21:25:27 +0000863bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000864 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000865 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +0000866 if (Addr.getOpcode() == ISD::ADD) {
867 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
868 Predicate_simm13(Addr.getOperand(1).Val))
869 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +0000870 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
871 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
872 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +0000873 R1 = Select(Addr.getOperand(0));
874 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +0000875 return true;
876 }
877
878 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000879 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
880 return true;
881}
882
Chris Lattner6c18b102005-12-17 07:47:01 +0000883SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
884 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +0000885 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
886 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +0000887 return Op; // Already selected.
888 // If this has already been converted, use it.
889 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
890 if (CGMI != CodeGenMap.end()) return CGMI->second;
891
892 switch (N->getOpcode()) {
893 default: break;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000894 case ISD::FrameIndex: {
895 int FI = cast<FrameIndexSDNode>(N)->getIndex();
896 if (N->hasOneUse())
897 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
898 CurDAG->getTargetFrameIndex(FI, MVT::i32),
899 CurDAG->getTargetConstant(0, MVT::i32));
900 return CodeGenMap[Op] =
901 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
902 CurDAG->getTargetFrameIndex(FI, MVT::i32),
903 CurDAG->getTargetConstant(0, MVT::i32));
904 }
Chris Lattnerd19fc652005-12-17 22:55:57 +0000905 case ISD::ADD_PARTS: {
906 SDOperand LHSL = Select(N->getOperand(0));
907 SDOperand LHSH = Select(N->getOperand(1));
908 SDOperand RHSL = Select(N->getOperand(2));
909 SDOperand RHSH = Select(N->getOperand(3));
910 // FIXME, handle immediate RHS.
911 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
912 LHSL, RHSL);
913 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
914 Low.getValue(1));
915 CodeGenMap[SDOperand(N, 0)] = Low;
916 CodeGenMap[SDOperand(N, 1)] = Hi;
917 return Op.ResNo ? Hi : Low;
918 }
919 case ISD::SUB_PARTS: {
920 SDOperand LHSL = Select(N->getOperand(0));
921 SDOperand LHSH = Select(N->getOperand(1));
922 SDOperand RHSL = Select(N->getOperand(2));
923 SDOperand RHSH = Select(N->getOperand(3));
924 // FIXME, handle immediate RHS.
925 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
926 LHSL, RHSL);
927 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
928 Low.getValue(1));
929 CodeGenMap[SDOperand(N, 0)] = Low;
930 CodeGenMap[SDOperand(N, 1)] = Hi;
931 return Op.ResNo ? Hi : Low;
932 }
Chris Lattner7087e572005-12-17 22:39:19 +0000933 case ISD::SDIV:
934 case ISD::UDIV: {
935 // FIXME: should use a custom expander to expose the SRA to the dag.
936 SDOperand DivLHS = Select(N->getOperand(0));
937 SDOperand DivRHS = Select(N->getOperand(1));
938
939 // Set the Y register to the high-part.
940 SDOperand TopPart;
941 if (N->getOpcode() == ISD::SDIV) {
942 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
943 CurDAG->getTargetConstant(31, MVT::i32));
944 } else {
945 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
946 }
947 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
948 CurDAG->getRegister(V8::G0, MVT::i32));
949
950 // FIXME: Handle div by immediate.
951 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
952 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
953 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000954 case ISD::MULHU:
955 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000956 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000957 SDOperand MulLHS = Select(N->getOperand(0));
958 SDOperand MulRHS = Select(N->getOperand(1));
959 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
960 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
961 MulLHS, MulRHS);
962 // The high part is in the Y register.
963 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
964 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000965 case ISD::CALL:
966 // FIXME: This is a workaround for a bug in tblgen.
967 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
968 // Emits: (CALL:void (tglobaladdr:i32):$dst)
969 // Pattern complexity = 2 cost = 1
970 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +0000971 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
972 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000973 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000974 SDOperand Chain = N->getOperand(0);
975 SDOperand Tmp0 = N1;
976 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000977 SDOperand Result;
978 if (N->getNumOperands() == 3) {
979 InFlag = Select(N->getOperand(2));
980 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
981 Chain, InFlag);
982 } else {
983 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
984 Chain);
985 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000986 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
987 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
988 return Result.getValue(Op.ResNo);
989 }
990 P47Fail:;
991
Chris Lattner6c18b102005-12-17 07:47:01 +0000992 }
993
994 return SelectCode(Op);
995}
996
997
998/// createPPCISelDag - This pass converts a legalized DAG into a
999/// PowerPC-specific DAG, ready for instruction scheduling.
1000///
1001FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1002 return new SparcV8DAGToDAGISel(TM);
1003}