blob: 7e017402a8ec2ffe31ade27da911db2a21303283 [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 Lattner72878a42006-01-12 07:31:15 +000079
80 virtual const char *getTargetNodeName(unsigned Opcode) const;
Chris Lattner6c18b102005-12-17 07:47:01 +000081 };
82}
83
84SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
85 : TargetLowering(TM) {
86
87 // Set up the register classes.
88 addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
89 addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
90 addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
Chris Lattner9a60ff62005-12-17 20:50:42 +000091
Chris Lattnere3572462005-12-18 02:10:39 +000092 // Custom legalize GlobalAddress nodes into LO/HI parts.
93 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner76acc872005-12-18 02:37:35 +000094 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Chris Lattnere3572462005-12-18 02:10:39 +000095
Chris Lattner9a60ff62005-12-17 20:50:42 +000096 // Sparc doesn't have sext_inreg, replace them with shl/sra
Chris Lattner33084492005-12-18 08:13:54 +000097 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
98 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
99 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner7087e572005-12-17 22:39:19 +0000100
101 // Sparc has no REM operation.
102 setOperationAction(ISD::UREM, MVT::i32, Expand);
103 setOperationAction(ISD::SREM, MVT::i32, Expand);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000104
105 // Custom expand fp<->sint
106 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
107 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
108
109 // Expand fp<->uint
110 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
111 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Chris Lattner6c18b102005-12-17 07:47:01 +0000112
Chris Lattner53e88452005-12-23 05:13:35 +0000113 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
114 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
115
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000116 // Turn FP extload into load/fextend
Chris Lattner065c8962005-12-18 07:13:32 +0000117 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
118
Chris Lattner4d55aca2005-12-18 01:20:35 +0000119 // Sparc has no select or setcc: expand to SELECT_CC.
120 setOperationAction(ISD::SELECT, MVT::i32, Expand);
121 setOperationAction(ISD::SELECT, MVT::f32, Expand);
122 setOperationAction(ISD::SELECT, MVT::f64, Expand);
123 setOperationAction(ISD::SETCC, MVT::i32, Expand);
124 setOperationAction(ISD::SETCC, MVT::f32, Expand);
125 setOperationAction(ISD::SETCC, MVT::f64, Expand);
126
127 // Sparc doesn't have BRCOND either, it has BR_CC.
128 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
129 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
130 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
131 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
132 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
133 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
134
Chris Lattner33084492005-12-18 08:13:54 +0000135 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
136 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
137 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
138
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000139 // V8 has no intrinsics for these particular operations.
140 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
141 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
142 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
143
Chris Lattner61772c22005-12-19 01:39:40 +0000144 setOperationAction(ISD::FSIN , MVT::f64, Expand);
145 setOperationAction(ISD::FCOS , MVT::f64, Expand);
146 setOperationAction(ISD::FSIN , MVT::f32, Expand);
147 setOperationAction(ISD::FCOS , MVT::f32, Expand);
148 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000151 setOperationAction(ISD::ROTL , MVT::i32, Expand);
152 setOperationAction(ISD::ROTR , MVT::i32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000153
154 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
155 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
156 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000157
158 // We don't have line number support yet.
159 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000160 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
161 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000162
Chris Lattner6c18b102005-12-17 07:47:01 +0000163 computeRegisterProperties();
164}
165
Chris Lattner72878a42006-01-12 07:31:15 +0000166const char *SparcV8TargetLowering::getTargetNodeName(unsigned Opcode) const {
167 switch (Opcode) {
168 case V8ISD::CMPICC: return "V8ISD::CMPICC";
169 case V8ISD::CMPFCC: return "V8ISD::CMPFCC";
170 case V8ISD::BRICC: return "V8ISD::BRICC";
171 case V8ISD::BRFCC: return "V8ISD::BRFCC";
172 case V8ISD::Hi: return "V8ISD::Hi";
173 case V8ISD::Lo: return "V8ISD::Lo";
174 case V8ISD::FTOI: return "V8ISD::FTOI";
175 case V8ISD::ITOF: return "V8ISD::ITOF";
176 case V8ISD::SELECT_ICC: return "V8ISD::SELECT_ICC";
177 case V8ISD::SELECT_FCC: return "V8ISD::SELECT_FCC";
178 case V8ISD::RET_FLAG: return "V8ISD::RET_FLAG";
179 }
180}
181
Chris Lattner384e5ef2005-12-18 13:33:06 +0000182/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
183/// either one or two GPRs, including FP values. TODO: we should pass FP values
184/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000185std::vector<SDOperand>
186SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000187 MachineFunction &MF = DAG.getMachineFunction();
188 SSARegMap *RegMap = MF.getSSARegMap();
189 std::vector<SDOperand> ArgValues;
190
Chris Lattner384e5ef2005-12-18 13:33:06 +0000191 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000192 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
193 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000194
195 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
196 unsigned ArgOffset = 68;
197
198 SDOperand Root = DAG.getRoot();
199 std::vector<SDOperand> OutChains;
200
Chris Lattnera01b7572005-12-17 08:03:24 +0000201 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
202 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000203
204 switch (ObjectVT) {
205 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000206 case MVT::i1:
207 case MVT::i8:
208 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000209 case MVT::i32:
210 if (I->use_empty()) { // Argument is dead.
211 if (CurArgReg < ArgRegEnd) ++CurArgReg;
212 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
213 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
214 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
215 MF.addLiveIn(*CurArgReg++, VReg);
216 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
217 if (ObjectVT != MVT::i32) {
218 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
219 : ISD::AssertZext;
220 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
221 DAG.getValueType(ObjectVT));
222 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
223 }
224 ArgValues.push_back(Arg);
225 } else {
226 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
227 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
228 SDOperand Load;
229 if (ObjectVT == MVT::i32) {
230 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
231 } else {
232 unsigned LoadOp =
233 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
234
235 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
236 DAG.getSrcValue(0), ObjectVT);
237 }
238 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000239 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000240
241 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000242 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000243 case MVT::f32:
244 if (I->use_empty()) { // Argument is dead.
245 if (CurArgReg < ArgRegEnd) ++CurArgReg;
246 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
247 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
248 // FP value is passed in an integer register.
249 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
250 MF.addLiveIn(*CurArgReg++, VReg);
251 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
252
Chris Lattnera01874f2005-12-23 02:31:39 +0000253 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
254 ArgValues.push_back(Arg);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000255 }
256 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000257 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000258
259 case MVT::i64:
260 case MVT::f64:
261 if (I->use_empty()) { // Argument is dead.
262 if (CurArgReg < ArgRegEnd) ++CurArgReg;
263 if (CurArgReg < ArgRegEnd) ++CurArgReg;
264 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
265 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
266 ((CurArgReg-ArgRegs) & 1) == 0) {
267 // If this is a double argument and the whole thing lives on the stack,
268 // and the argument is aligned, load the double straight from the stack.
269 // We can't do a load in cases like void foo([6ints], int,double),
270 // because the double wouldn't be aligned!
271 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
272 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
273 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
274 DAG.getSrcValue(0)));
275 } else {
276 SDOperand HiVal;
277 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
278 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
279 MF.addLiveIn(*CurArgReg++, VRegHi);
280 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
281 } else {
282 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
283 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
284 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
285 }
286
287 SDOperand LoVal;
288 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
289 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
290 MF.addLiveIn(*CurArgReg++, VRegLo);
291 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
292 } else {
293 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
294 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
295 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
296 }
297
298 // Compose the two halves together into an i64 unit.
299 SDOperand WholeValue =
300 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000301
302 // If we want a double, do a bit convert.
303 if (ObjectVT == MVT::f64)
304 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
305
306 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000307 }
308 ArgOffset += 8;
309 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000310 }
311 }
312
Chris Lattner384e5ef2005-12-18 13:33:06 +0000313 // Store remaining ArgRegs to the stack if this is a varargs function.
314 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000315 // Remember the vararg offset for the va_start implementation.
316 VarArgsFrameOffset = ArgOffset;
317
Chris Lattner384e5ef2005-12-18 13:33:06 +0000318 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
319 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
320 MF.addLiveIn(*CurArgReg, VReg);
321 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
322
323 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
324 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
325
326 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
327 Arg, FIPtr, DAG.getSrcValue(0)));
328 ArgOffset += 4;
329 }
330 }
331
332 if (!OutChains.empty())
333 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000334
335 // Finally, inform the code generator which regs we return values in.
336 switch (getValueType(F.getReturnType())) {
337 default: assert(0 && "Unknown type!");
338 case MVT::isVoid: break;
339 case MVT::i1:
340 case MVT::i8:
341 case MVT::i16:
342 case MVT::i32:
343 MF.addLiveOut(V8::I0);
344 break;
345 case MVT::i64:
346 MF.addLiveOut(V8::I0);
347 MF.addLiveOut(V8::I1);
348 break;
349 case MVT::f32:
350 MF.addLiveOut(V8::F0);
351 break;
352 case MVT::f64:
353 MF.addLiveOut(V8::D0);
354 break;
355 }
356
357 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000358}
359
360std::pair<SDOperand, SDOperand>
361SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
362 bool isVarArg, unsigned CC,
363 bool isTailCall, SDOperand Callee,
364 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000365 MachineFunction &MF = DAG.getMachineFunction();
366 // Count the size of the outgoing arguments.
367 unsigned ArgsSize = 0;
368 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
369 switch (getValueType(Args[i].second)) {
370 default: assert(0 && "Unknown value type!");
371 case MVT::i1:
372 case MVT::i8:
373 case MVT::i16:
374 case MVT::i32:
375 case MVT::f32:
376 ArgsSize += 4;
377 break;
378 case MVT::i64:
379 case MVT::f64:
380 ArgsSize += 8;
381 break;
382 }
383 }
384 if (ArgsSize > 4*6)
385 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
386 else
387 ArgsSize = 0;
388
Chris Lattner6554bef2005-12-19 01:15:13 +0000389 // Keep stack frames 8-byte aligned.
390 ArgsSize = (ArgsSize+7) & ~7;
391
Chris Lattner2db3ff62005-12-18 15:55:15 +0000392 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
393 DAG.getConstant(ArgsSize, getPointerTy()));
394
395 SDOperand StackPtr, NullSV;
396 std::vector<SDOperand> Stores;
397 std::vector<SDOperand> RegValuesToPass;
398 unsigned ArgOffset = 68;
399 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
400 SDOperand Val = Args[i].first;
401 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000402 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000403 unsigned ObjSize;
404 switch (ObjectVT) {
405 default: assert(0 && "Unhandled argument type!");
406 case MVT::i1:
407 case MVT::i8:
408 case MVT::i16:
409 // Promote the integer to 32-bits. If the input type is signed, use a
410 // sign extend, otherwise use a zero extend.
411 if (Args[i].second->isSigned())
412 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
413 else
414 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
415 // FALL THROUGH
416 case MVT::i32:
417 ObjSize = 4;
418
419 if (RegValuesToPass.size() >= 6) {
420 ValToStore = Val;
421 } else {
422 RegValuesToPass.push_back(Val);
423 }
424 break;
425 case MVT::f32:
426 ObjSize = 4;
427 if (RegValuesToPass.size() >= 6) {
428 ValToStore = Val;
429 } else {
430 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000431 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000432 RegValuesToPass.push_back(Val);
433 }
434 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000435 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000436 ObjSize = 8;
437 // If we can store this directly into the outgoing slot, do so. We can
438 // do this when all ArgRegs are used and if the outgoing slot is aligned.
439 if (RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
440 ValToStore = Val;
441 break;
442 }
443
444 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000445 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000446 // FALL THROUGH
447 case MVT::i64:
448 ObjSize = 8;
449 if (RegValuesToPass.size() >= 6) {
450 ValToStore = Val; // Whole thing is passed in memory.
451 break;
452 }
453
454 // Split the value into top and bottom part. Top part goes in a reg.
455 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
456 DAG.getConstant(1, MVT::i32));
457 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
458 DAG.getConstant(0, MVT::i32));
459 RegValuesToPass.push_back(Hi);
460
461 if (RegValuesToPass.size() >= 6) {
462 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000463 ArgOffset += 4;
464 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000465 } else {
466 RegValuesToPass.push_back(Lo);
467 }
468 break;
469 }
470
471 if (ValToStore.Val) {
472 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000473 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000474 NullSV = DAG.getSrcValue(NULL);
475 }
476 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
477 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
478 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
479 ValToStore, PtrOff, NullSV));
480 }
481 ArgOffset += ObjSize;
482 }
483
484 // Emit all stores, make sure the occur before any copies into physregs.
485 if (!Stores.empty())
486 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
487
488 static const unsigned ArgRegs[] = {
489 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
490 };
491
492 // Build a sequence of copy-to-reg nodes chained together with token chain
493 // and flag operands which copy the outgoing args into O[0-5].
494 SDOperand InFlag;
495 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
496 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
497 InFlag = Chain.getValue(1);
498 }
499
Chris Lattner2db3ff62005-12-18 15:55:15 +0000500 // If the callee is a GlobalAddress node (quite common, every direct call is)
501 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
502 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
503 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
504
505 std::vector<MVT::ValueType> NodeTys;
506 NodeTys.push_back(MVT::Other); // Returns a chain
507 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000508 if (InFlag.Val)
509 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee, InFlag), 0);
510 else
511 Chain = SDOperand(DAG.getCall(NodeTys, Chain, Callee), 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000512 InFlag = Chain.getValue(1);
513
514 MVT::ValueType RetTyVT = getValueType(RetTy);
515 SDOperand RetVal;
516 if (RetTyVT != MVT::isVoid) {
517 switch (RetTyVT) {
518 default: assert(0 && "Unknown value type to return!");
519 case MVT::i1:
520 case MVT::i8:
521 case MVT::i16:
522 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
523 Chain = RetVal.getValue(1);
524
525 // Add a note to keep track of whether it is sign or zero extended.
526 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
527 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
528 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
529 break;
530 case MVT::i32:
531 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
532 Chain = RetVal.getValue(1);
533 break;
534 case MVT::f32:
535 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
536 Chain = RetVal.getValue(1);
537 break;
538 case MVT::f64:
539 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
540 Chain = RetVal.getValue(1);
541 break;
542 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000543 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000544 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
545 Lo.getValue(2));
546 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
547 Chain = Hi.getValue(1);
548 break;
549 }
550 }
551
552 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
553 DAG.getConstant(ArgsSize, getPointerTy()));
554
Chris Lattner2db3ff62005-12-18 15:55:15 +0000555 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000556}
557
558SDOperand SparcV8TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
559 SelectionDAG &DAG) {
Chris Lattnerdab05f02005-12-18 21:03:04 +0000560 SDOperand Copy;
561 switch (Op.getValueType()) {
562 default: assert(0 && "Unknown type to return!");
563 case MVT::i32:
564 Copy = DAG.getCopyToReg(Chain, V8::I0, Op, SDOperand());
565 break;
566 case MVT::f32:
567 Copy = DAG.getCopyToReg(Chain, V8::F0, Op, SDOperand());
568 break;
569 case MVT::f64:
570 Copy = DAG.getCopyToReg(Chain, V8::D0, Op, SDOperand());
571 break;
572 case MVT::i64:
Chris Lattner4b486312005-12-17 08:15:09 +0000573 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
574 DAG.getConstant(1, MVT::i32));
575 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
576 DAG.getConstant(0, MVT::i32));
Chris Lattnerdab05f02005-12-18 21:03:04 +0000577 Copy = DAG.getCopyToReg(Chain, V8::I0, Hi, SDOperand());
578 Copy = DAG.getCopyToReg(Copy, V8::I1, Lo, Copy.getValue(1));
579 break;
Chris Lattner4b486312005-12-17 08:15:09 +0000580 }
Chris Lattnerdab05f02005-12-18 21:03:04 +0000581 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
Chris Lattner6c18b102005-12-17 07:47:01 +0000582}
583
Chris Lattner4d55aca2005-12-18 01:20:35 +0000584SDOperand SparcV8TargetLowering::
585LowerVAStart(SDOperand Chain, SDOperand VAListP, Value *VAListV,
586 SelectionDAG &DAG) {
587
Chris Lattnerc4769bb2005-12-23 06:24:04 +0000588 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
589 DAG.getRegister(V8::I6, MVT::i32),
590 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
591 return DAG.getNode(ISD::STORE, MVT::Other, Chain, Offset,
592 VAListP, DAG.getSrcValue(VAListV));
Chris Lattner6c18b102005-12-17 07:47:01 +0000593}
594
Chris Lattner4d55aca2005-12-18 01:20:35 +0000595std::pair<SDOperand,SDOperand> SparcV8TargetLowering::
596LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
597 const Type *ArgTy, SelectionDAG &DAG) {
Chris Lattner74fa64b2005-12-23 06:37:38 +0000598 // Load the pointer out of the valist.
599 SDOperand Ptr = DAG.getLoad(MVT::i32, Chain,
600 VAListP, DAG.getSrcValue(VAListV));
601 MVT::ValueType ArgVT = getValueType(ArgTy);
602 SDOperand Val = DAG.getLoad(ArgVT, Ptr.getValue(1),
603 Ptr, DAG.getSrcValue(NULL));
604 // Increment the pointer.
605 Ptr = DAG.getNode(ISD::ADD, MVT::i32, Ptr,
606 DAG.getConstant(MVT::getSizeInBits(ArgVT)/8, MVT::i32));
607 // Store it back to the valist.
608 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Ptr,
609 VAListP, DAG.getSrcValue(VAListV));
610 return std::make_pair(Val, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000611}
612
Chris Lattner4d55aca2005-12-18 01:20:35 +0000613std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
614LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
615 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000616 assert(0 && "Unimp");
617 abort();
618}
619
Chris Lattner4d55aca2005-12-18 01:20:35 +0000620SDOperand SparcV8TargetLowering::
621LowerOperation(SDOperand Op, SelectionDAG &DAG) {
622 switch (Op.getOpcode()) {
623 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000624 case ISD::GlobalAddress: {
625 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
626 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
627 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
628 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
629 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
630 }
Chris Lattner76acc872005-12-18 02:37:35 +0000631 case ISD::ConstantPool: {
632 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
633 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
634 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
635 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
636 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
637 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000638 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000639 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000640 assert(Op.getValueType() == MVT::i32);
641 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
642 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000643 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000644 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner3fbb7262006-01-11 07:27:40 +0000645 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000646 // Convert the int value to FP in an FP register.
Chris Lattner3fbb7262006-01-11 07:27:40 +0000647 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000648 }
Chris Lattner33084492005-12-18 08:13:54 +0000649 case ISD::BR_CC: {
650 SDOperand Chain = Op.getOperand(0);
651 SDOperand CC = Op.getOperand(1);
652 SDOperand LHS = Op.getOperand(2);
653 SDOperand RHS = Op.getOperand(3);
654 SDOperand Dest = Op.getOperand(4);
655
656 // Get the condition flag.
657 if (LHS.getValueType() == MVT::i32) {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000658 std::vector<MVT::ValueType> VTs;
659 VTs.push_back(MVT::i32);
660 VTs.push_back(MVT::Flag);
661 std::vector<SDOperand> Ops;
662 Ops.push_back(LHS);
663 Ops.push_back(RHS);
664 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops);
Chris Lattner33084492005-12-18 08:13:54 +0000665 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
666 } else {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000667 std::vector<MVT::ValueType> VTs;
668 VTs.push_back(MVT::i32);
669 VTs.push_back(MVT::Flag);
670 std::vector<SDOperand> Ops;
671 Ops.push_back(LHS);
672 Ops.push_back(RHS);
673 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, VTs, Ops);
Chris Lattner33084492005-12-18 08:13:54 +0000674 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
675 }
676 }
677 case ISD::SELECT_CC: {
678 SDOperand LHS = Op.getOperand(0);
679 SDOperand RHS = Op.getOperand(1);
680 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
681 SDOperand TrueVal = Op.getOperand(2);
682 SDOperand FalseVal = Op.getOperand(3);
683
684 unsigned Opc;
685 Opc = LHS.getValueType() == MVT::i32 ? V8ISD::CMPICC : V8ISD::CMPFCC;
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000686 std::vector<MVT::ValueType> VTs;
687 VTs.push_back(LHS.getValueType());
688 VTs.push_back(MVT::Flag);
689 std::vector<SDOperand> Ops;
690 Ops.push_back(LHS);
691 Ops.push_back(RHS);
692 SDOperand CompareFlag = DAG.getNode(Opc, VTs, Ops).getValue(1);
Chris Lattner33084492005-12-18 08:13:54 +0000693
694 Opc = LHS.getValueType() == MVT::i32 ?
695 V8ISD::SELECT_ICC : V8ISD::SELECT_FCC;
696 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
697 DAG.getConstant(CC, MVT::i32), CompareFlag);
698 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000699 }
700}
701
Chris Lattner33084492005-12-18 08:13:54 +0000702MachineBasicBlock *
703SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
704 MachineBasicBlock *BB) {
705 unsigned BROpcode;
706 // Figure out the conditional branch opcode to use for this select_cc.
707 switch (MI->getOpcode()) {
708 default: assert(0 && "Unknown SELECT_CC!");
709 case V8::SELECT_CC_Int_ICC:
710 case V8::SELECT_CC_FP_ICC:
711 case V8::SELECT_CC_DFP_ICC:
712 // Integer compare.
713 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
714 default: assert(0 && "Unknown integer condition code!");
715 case ISD::SETEQ: BROpcode = V8::BE; break;
716 case ISD::SETNE: BROpcode = V8::BNE; break;
717 case ISD::SETLT: BROpcode = V8::BL; break;
718 case ISD::SETGT: BROpcode = V8::BG; break;
719 case ISD::SETLE: BROpcode = V8::BLE; break;
720 case ISD::SETGE: BROpcode = V8::BGE; break;
721 case ISD::SETULT: BROpcode = V8::BCS; break;
722 case ISD::SETULE: BROpcode = V8::BLEU; break;
723 case ISD::SETUGT: BROpcode = V8::BGU; break;
724 case ISD::SETUGE: BROpcode = V8::BCC; break;
725 }
726 break;
727 case V8::SELECT_CC_Int_FCC:
728 case V8::SELECT_CC_FP_FCC:
729 case V8::SELECT_CC_DFP_FCC:
730 // FP compare.
731 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
732 default: assert(0 && "Unknown fp condition code!");
733 case ISD::SETEQ: BROpcode = V8::FBE; break;
734 case ISD::SETNE: BROpcode = V8::FBNE; break;
735 case ISD::SETLT: BROpcode = V8::FBL; break;
736 case ISD::SETGT: BROpcode = V8::FBG; break;
737 case ISD::SETLE: BROpcode = V8::FBLE; break;
738 case ISD::SETGE: BROpcode = V8::FBGE; break;
739 case ISD::SETULT: BROpcode = V8::FBUL; break;
740 case ISD::SETULE: BROpcode = V8::FBULE; break;
741 case ISD::SETUGT: BROpcode = V8::FBUG; break;
742 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
743 case ISD::SETUO: BROpcode = V8::FBU; break;
744 case ISD::SETO: BROpcode = V8::FBO; break;
745 case ISD::SETONE: BROpcode = V8::FBLG; break;
746 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
747 }
748 break;
749 }
750
751 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
752 // control-flow pattern. The incoming instruction knows the destination vreg
753 // to set, the condition code register to branch on, the true/false values to
754 // select between, and a branch opcode to use.
755 const BasicBlock *LLVM_BB = BB->getBasicBlock();
756 ilist<MachineBasicBlock>::iterator It = BB;
757 ++It;
758
759 // thisMBB:
760 // ...
761 // TrueVal = ...
762 // [f]bCC copy1MBB
763 // fallthrough --> copy0MBB
764 MachineBasicBlock *thisMBB = BB;
765 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
766 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
767 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
768 MachineFunction *F = BB->getParent();
769 F->getBasicBlockList().insert(It, copy0MBB);
770 F->getBasicBlockList().insert(It, sinkMBB);
771 // Update machine-CFG edges
772 BB->addSuccessor(copy0MBB);
773 BB->addSuccessor(sinkMBB);
774
775 // copy0MBB:
776 // %FalseValue = ...
777 // # fallthrough to sinkMBB
778 BB = copy0MBB;
779
780 // Update machine-CFG edges
781 BB->addSuccessor(sinkMBB);
782
783 // sinkMBB:
784 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
785 // ...
786 BB = sinkMBB;
787 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
788 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
789 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
790
791 delete MI; // The pseudo instruction is gone now.
792 return BB;
793}
794
Chris Lattner6c18b102005-12-17 07:47:01 +0000795//===----------------------------------------------------------------------===//
796// Instruction Selector Implementation
797//===----------------------------------------------------------------------===//
798
799//===--------------------------------------------------------------------===//
800/// SparcV8DAGToDAGISel - PPC specific code to select Sparc V8 machine
801/// instructions for SelectionDAG operations.
802///
803namespace {
804class SparcV8DAGToDAGISel : public SelectionDAGISel {
805 SparcV8TargetLowering V8Lowering;
806public:
807 SparcV8DAGToDAGISel(TargetMachine &TM)
808 : SelectionDAGISel(V8Lowering), V8Lowering(TM) {}
809
810 SDOperand Select(SDOperand Op);
811
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000812 // Complex Pattern Selectors.
813 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
814 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
815
Chris Lattner6c18b102005-12-17 07:47:01 +0000816 /// InstructionSelectBasicBlock - This callback is invoked by
817 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
818 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
819
820 virtual const char *getPassName() const {
821 return "PowerPC DAG->DAG Pattern Instruction Selection";
822 }
823
824 // Include the pieces autogenerated from the target description.
825#include "SparcV8GenDAGISel.inc"
826};
827} // end anonymous namespace
828
829/// InstructionSelectBasicBlock - This callback is invoked by
830/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
831void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
832 DEBUG(BB->dump());
833
834 // Select target instructions for the DAG.
835 DAG.setRoot(Select(DAG.getRoot()));
836 CodeGenMap.clear();
837 DAG.RemoveDeadNodes();
838
839 // Emit machine code to BB.
840 ScheduleAndEmitDAG(DAG);
841}
842
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000843bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
844 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000845 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
846 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000847 Offset = CurDAG->getTargetConstant(0, MVT::i32);
848 return true;
849 }
850
851 if (Addr.getOpcode() == ISD::ADD) {
852 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
853 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000854 if (FrameIndexSDNode *FIN =
855 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000856 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +0000857 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000858 } else {
859 Base = Select(Addr.getOperand(0));
860 }
861 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
862 return true;
863 }
864 }
865 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
866 Base = Select(Addr.getOperand(1));
867 Offset = Addr.getOperand(0).getOperand(0);
868 return true;
869 }
870 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
871 Base = Select(Addr.getOperand(0));
872 Offset = Addr.getOperand(1).getOperand(0);
873 return true;
874 }
875 }
876 Base = Select(Addr);
877 Offset = CurDAG->getTargetConstant(0, MVT::i32);
878 return true;
879}
880
Chris Lattner9034b882005-12-17 21:25:27 +0000881bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000882 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000883 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +0000884 if (Addr.getOpcode() == ISD::ADD) {
885 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
886 Predicate_simm13(Addr.getOperand(1).Val))
887 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +0000888 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
889 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
890 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +0000891 R1 = Select(Addr.getOperand(0));
892 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +0000893 return true;
894 }
895
896 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000897 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
898 return true;
899}
900
Chris Lattner6c18b102005-12-17 07:47:01 +0000901SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
902 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +0000903 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
904 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +0000905 return Op; // Already selected.
906 // If this has already been converted, use it.
907 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
908 if (CGMI != CodeGenMap.end()) return CGMI->second;
909
910 switch (N->getOpcode()) {
911 default: break;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000912 case ISD::FrameIndex: {
913 int FI = cast<FrameIndexSDNode>(N)->getIndex();
914 if (N->hasOneUse())
915 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
916 CurDAG->getTargetFrameIndex(FI, MVT::i32),
917 CurDAG->getTargetConstant(0, MVT::i32));
918 return CodeGenMap[Op] =
919 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
920 CurDAG->getTargetFrameIndex(FI, MVT::i32),
921 CurDAG->getTargetConstant(0, MVT::i32));
922 }
Chris Lattnerd19fc652005-12-17 22:55:57 +0000923 case ISD::ADD_PARTS: {
924 SDOperand LHSL = Select(N->getOperand(0));
925 SDOperand LHSH = Select(N->getOperand(1));
926 SDOperand RHSL = Select(N->getOperand(2));
927 SDOperand RHSH = Select(N->getOperand(3));
928 // FIXME, handle immediate RHS.
929 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
930 LHSL, RHSL);
931 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
932 Low.getValue(1));
933 CodeGenMap[SDOperand(N, 0)] = Low;
934 CodeGenMap[SDOperand(N, 1)] = Hi;
935 return Op.ResNo ? Hi : Low;
936 }
937 case ISD::SUB_PARTS: {
938 SDOperand LHSL = Select(N->getOperand(0));
939 SDOperand LHSH = Select(N->getOperand(1));
940 SDOperand RHSL = Select(N->getOperand(2));
941 SDOperand RHSH = Select(N->getOperand(3));
942 // FIXME, handle immediate RHS.
943 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
944 LHSL, RHSL);
945 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
946 Low.getValue(1));
947 CodeGenMap[SDOperand(N, 0)] = Low;
948 CodeGenMap[SDOperand(N, 1)] = Hi;
949 return Op.ResNo ? Hi : Low;
950 }
Chris Lattner7087e572005-12-17 22:39:19 +0000951 case ISD::SDIV:
952 case ISD::UDIV: {
953 // FIXME: should use a custom expander to expose the SRA to the dag.
954 SDOperand DivLHS = Select(N->getOperand(0));
955 SDOperand DivRHS = Select(N->getOperand(1));
956
957 // Set the Y register to the high-part.
958 SDOperand TopPart;
959 if (N->getOpcode() == ISD::SDIV) {
960 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
961 CurDAG->getTargetConstant(31, MVT::i32));
962 } else {
963 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
964 }
965 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
966 CurDAG->getRegister(V8::G0, MVT::i32));
967
968 // FIXME: Handle div by immediate.
969 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
970 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
971 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000972 case ISD::MULHU:
973 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000974 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000975 SDOperand MulLHS = Select(N->getOperand(0));
976 SDOperand MulRHS = Select(N->getOperand(1));
977 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
978 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
979 MulLHS, MulRHS);
980 // The high part is in the Y register.
981 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
982 }
Chris Lattner2db3ff62005-12-18 15:55:15 +0000983 case ISD::CALL:
984 // FIXME: This is a workaround for a bug in tblgen.
985 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
986 // Emits: (CALL:void (tglobaladdr:i32):$dst)
987 // Pattern complexity = 2 cost = 1
988 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +0000989 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
990 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000991 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000992 SDOperand Chain = N->getOperand(0);
993 SDOperand Tmp0 = N1;
994 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000995 SDOperand Result;
996 if (N->getNumOperands() == 3) {
997 InFlag = Select(N->getOperand(2));
998 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
999 Chain, InFlag);
1000 } else {
1001 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1002 Chain);
1003 }
Chris Lattner2db3ff62005-12-18 15:55:15 +00001004 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
1005 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
1006 return Result.getValue(Op.ResNo);
1007 }
1008 P47Fail:;
1009
Chris Lattner6c18b102005-12-17 07:47:01 +00001010 }
1011
1012 return SelectCode(Op);
1013}
1014
1015
1016/// createPPCISelDag - This pass converts a legalized DAG into a
1017/// PowerPC-specific DAG, ready for instruction scheduling.
1018///
1019FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1020 return new SparcV8DAGToDAGISel(TM);
1021}