blob: 9718801a8a0001a679b46bad625e8fd5e56fb8df [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
Chris Lattner44ea7b12006-01-27 23:30:03 +000049 CALL, // A V8 call instruction.
Chris Lattnerdab05f02005-12-18 21:03:04 +000050 RET_FLAG, // Return with a flag operand.
Chris Lattner4d55aca2005-12-18 01:20:35 +000051 };
52}
53
Chris Lattner6c18b102005-12-17 07:47:01 +000054namespace {
55 class SparcV8TargetLowering : public TargetLowering {
Chris Lattner2db3ff62005-12-18 15:55:15 +000056 int VarArgsFrameOffset; // Frame offset to start of varargs area.
Chris Lattner6c18b102005-12-17 07:47:01 +000057 public:
58 SparcV8TargetLowering(TargetMachine &TM);
Chris Lattner4d55aca2005-12-18 01:20:35 +000059 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Chris Lattner4a397e02006-01-30 03:51:45 +000060
61 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
62 /// be zero. Op is expected to be a target specific node. Used by DAG
63 /// combiner.
64 virtual bool isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +000065 uint64_t Mask) const;
Chris Lattner4a397e02006-01-30 03:51:45 +000066
Chris Lattner6c18b102005-12-17 07:47:01 +000067 virtual std::vector<SDOperand>
68 LowerArguments(Function &F, SelectionDAG &DAG);
69 virtual std::pair<SDOperand, SDOperand>
70 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
71 unsigned CC,
72 bool isTailCall, SDOperand Callee, ArgListTy &Args,
73 SelectionDAG &DAG);
Chris Lattner6c18b102005-12-17 07:47:01 +000074 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);
Nate Begemand88fc032006-01-14 03:14:10 +0000153 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000154
155 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
156 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
157 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000158
159 // We don't have line number support yet.
160 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000161 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
162 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000163
Nate Begemanee625572006-01-27 21:09:22 +0000164 // RET must be custom lowered, to meet ABI requirements
165 setOperationAction(ISD::RET , MVT::Other, Custom);
166
Nate Begemanacc398c2006-01-25 18:21:52 +0000167 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
168 setOperationAction(ISD::VASTART , MVT::Other, Custom);
169
170 // Use the default implementation.
171 setOperationAction(ISD::VAARG , MVT::Other, Expand);
172 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
173 setOperationAction(ISD::VAEND , MVT::Other, Expand);
174 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
175 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
Chris Lattner934ea492006-01-15 08:55:25 +0000176 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
177
178 setStackPointerRegisterToSaveRestore(V8::O6);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000179
Chris Lattner6c18b102005-12-17 07:47:01 +0000180 computeRegisterProperties();
181}
182
Chris Lattner72878a42006-01-12 07:31:15 +0000183const char *SparcV8TargetLowering::getTargetNodeName(unsigned Opcode) const {
184 switch (Opcode) {
Chris Lattner138d3222006-01-12 07:38:04 +0000185 default: return 0;
Chris Lattner72878a42006-01-12 07:31:15 +0000186 case V8ISD::CMPICC: return "V8ISD::CMPICC";
187 case V8ISD::CMPFCC: return "V8ISD::CMPFCC";
188 case V8ISD::BRICC: return "V8ISD::BRICC";
189 case V8ISD::BRFCC: return "V8ISD::BRFCC";
190 case V8ISD::Hi: return "V8ISD::Hi";
191 case V8ISD::Lo: return "V8ISD::Lo";
192 case V8ISD::FTOI: return "V8ISD::FTOI";
193 case V8ISD::ITOF: return "V8ISD::ITOF";
194 case V8ISD::SELECT_ICC: return "V8ISD::SELECT_ICC";
195 case V8ISD::SELECT_FCC: return "V8ISD::SELECT_FCC";
Chris Lattner44ea7b12006-01-27 23:30:03 +0000196 case V8ISD::CALL: return "V8ISD::CALL";
Chris Lattner72878a42006-01-12 07:31:15 +0000197 case V8ISD::RET_FLAG: return "V8ISD::RET_FLAG";
198 }
199}
200
Chris Lattner4a397e02006-01-30 03:51:45 +0000201/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
202/// be zero. Op is expected to be a target specific node. Used by DAG
203/// combiner.
204bool SparcV8TargetLowering::
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000205isMaskedValueZeroForTargetNode(const SDOperand &Op, uint64_t Mask) const {
Chris Lattner4a397e02006-01-30 03:51:45 +0000206 switch (Op.getOpcode()) {
207 default: return false;
208 case V8ISD::SELECT_ICC:
209 case V8ISD::SELECT_FCC:
210 assert(MVT::isInteger(Op.getValueType()) && "Not an integer select!");
211 // These operations are masked zero if both the left and the right are zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000212 return MaskedValueIsZero(Op.getOperand(0), Mask) &&
213 MaskedValueIsZero(Op.getOperand(1), Mask);
Chris Lattner4a397e02006-01-30 03:51:45 +0000214 }
215}
216
217
Chris Lattner384e5ef2005-12-18 13:33:06 +0000218/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
219/// either one or two GPRs, including FP values. TODO: we should pass FP values
220/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000221std::vector<SDOperand>
222SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000223 MachineFunction &MF = DAG.getMachineFunction();
224 SSARegMap *RegMap = MF.getSSARegMap();
225 std::vector<SDOperand> ArgValues;
226
Chris Lattner384e5ef2005-12-18 13:33:06 +0000227 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000228 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
229 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000230
231 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
232 unsigned ArgOffset = 68;
233
234 SDOperand Root = DAG.getRoot();
235 std::vector<SDOperand> OutChains;
236
Chris Lattnera01b7572005-12-17 08:03:24 +0000237 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
238 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000239
240 switch (ObjectVT) {
241 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000242 case MVT::i1:
243 case MVT::i8:
244 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000245 case MVT::i32:
246 if (I->use_empty()) { // Argument is dead.
247 if (CurArgReg < ArgRegEnd) ++CurArgReg;
248 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
249 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
250 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
251 MF.addLiveIn(*CurArgReg++, VReg);
252 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
253 if (ObjectVT != MVT::i32) {
254 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
255 : ISD::AssertZext;
256 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
257 DAG.getValueType(ObjectVT));
258 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
259 }
260 ArgValues.push_back(Arg);
261 } else {
262 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
263 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
264 SDOperand Load;
265 if (ObjectVT == MVT::i32) {
266 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
267 } else {
268 unsigned LoadOp =
269 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
270
Chris Lattner99cf5092006-01-16 01:40:00 +0000271 // Sparc is big endian, so add an offset based on the ObjectVT.
272 unsigned Offset = 4-std::max(1U, MVT::getSizeInBits(ObjectVT)/8);
273 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
274 DAG.getConstant(Offset, MVT::i32));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000275 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
276 DAG.getSrcValue(0), ObjectVT);
Chris Lattnerf7511b42006-01-15 22:22:01 +0000277 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000278 }
279 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000280 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000281
282 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000283 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000284 case MVT::f32:
285 if (I->use_empty()) { // Argument is dead.
286 if (CurArgReg < ArgRegEnd) ++CurArgReg;
287 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
288 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
289 // FP value is passed in an integer register.
290 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
291 MF.addLiveIn(*CurArgReg++, VReg);
292 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
293
Chris Lattnera01874f2005-12-23 02:31:39 +0000294 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
295 ArgValues.push_back(Arg);
Chris Lattner46030a62006-01-19 07:22:29 +0000296 } else {
297 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
298 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
299 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, DAG.getSrcValue(0));
300 ArgValues.push_back(Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000301 }
302 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000303 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000304
305 case MVT::i64:
306 case MVT::f64:
307 if (I->use_empty()) { // Argument is dead.
308 if (CurArgReg < ArgRegEnd) ++CurArgReg;
309 if (CurArgReg < ArgRegEnd) ++CurArgReg;
310 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
311 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
312 ((CurArgReg-ArgRegs) & 1) == 0) {
313 // If this is a double argument and the whole thing lives on the stack,
314 // and the argument is aligned, load the double straight from the stack.
315 // We can't do a load in cases like void foo([6ints], int,double),
316 // because the double wouldn't be aligned!
317 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
318 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
319 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
320 DAG.getSrcValue(0)));
321 } else {
322 SDOperand HiVal;
323 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
324 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
325 MF.addLiveIn(*CurArgReg++, VRegHi);
326 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
327 } else {
328 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
329 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
330 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
331 }
332
333 SDOperand LoVal;
334 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
335 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
336 MF.addLiveIn(*CurArgReg++, VRegLo);
337 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
338 } else {
339 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
340 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
341 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
342 }
343
344 // Compose the two halves together into an i64 unit.
345 SDOperand WholeValue =
346 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000347
348 // If we want a double, do a bit convert.
349 if (ObjectVT == MVT::f64)
350 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
351
352 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000353 }
354 ArgOffset += 8;
355 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000356 }
357 }
358
Chris Lattner384e5ef2005-12-18 13:33:06 +0000359 // Store remaining ArgRegs to the stack if this is a varargs function.
360 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000361 // Remember the vararg offset for the va_start implementation.
362 VarArgsFrameOffset = ArgOffset;
363
Chris Lattner384e5ef2005-12-18 13:33:06 +0000364 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
365 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
366 MF.addLiveIn(*CurArgReg, VReg);
367 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
368
369 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
370 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
371
372 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
373 Arg, FIPtr, DAG.getSrcValue(0)));
374 ArgOffset += 4;
375 }
376 }
377
378 if (!OutChains.empty())
379 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000380
381 // Finally, inform the code generator which regs we return values in.
382 switch (getValueType(F.getReturnType())) {
383 default: assert(0 && "Unknown type!");
384 case MVT::isVoid: break;
385 case MVT::i1:
386 case MVT::i8:
387 case MVT::i16:
388 case MVT::i32:
389 MF.addLiveOut(V8::I0);
390 break;
391 case MVT::i64:
392 MF.addLiveOut(V8::I0);
393 MF.addLiveOut(V8::I1);
394 break;
395 case MVT::f32:
396 MF.addLiveOut(V8::F0);
397 break;
398 case MVT::f64:
399 MF.addLiveOut(V8::D0);
400 break;
401 }
402
403 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000404}
405
406std::pair<SDOperand, SDOperand>
407SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
408 bool isVarArg, unsigned CC,
409 bool isTailCall, SDOperand Callee,
410 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000411 MachineFunction &MF = DAG.getMachineFunction();
412 // Count the size of the outgoing arguments.
413 unsigned ArgsSize = 0;
414 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
415 switch (getValueType(Args[i].second)) {
416 default: assert(0 && "Unknown value type!");
417 case MVT::i1:
418 case MVT::i8:
419 case MVT::i16:
420 case MVT::i32:
421 case MVT::f32:
422 ArgsSize += 4;
423 break;
424 case MVT::i64:
425 case MVT::f64:
426 ArgsSize += 8;
427 break;
428 }
429 }
430 if (ArgsSize > 4*6)
431 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
432 else
433 ArgsSize = 0;
434
Chris Lattner6554bef2005-12-19 01:15:13 +0000435 // Keep stack frames 8-byte aligned.
436 ArgsSize = (ArgsSize+7) & ~7;
437
Chris Lattner2db3ff62005-12-18 15:55:15 +0000438 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
439 DAG.getConstant(ArgsSize, getPointerTy()));
440
441 SDOperand StackPtr, NullSV;
442 std::vector<SDOperand> Stores;
443 std::vector<SDOperand> RegValuesToPass;
444 unsigned ArgOffset = 68;
445 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
446 SDOperand Val = Args[i].first;
447 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000448 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000449 unsigned ObjSize;
450 switch (ObjectVT) {
451 default: assert(0 && "Unhandled argument type!");
452 case MVT::i1:
453 case MVT::i8:
454 case MVT::i16:
455 // Promote the integer to 32-bits. If the input type is signed, use a
456 // sign extend, otherwise use a zero extend.
457 if (Args[i].second->isSigned())
458 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
459 else
460 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
461 // FALL THROUGH
462 case MVT::i32:
463 ObjSize = 4;
464
465 if (RegValuesToPass.size() >= 6) {
466 ValToStore = Val;
467 } else {
468 RegValuesToPass.push_back(Val);
469 }
470 break;
471 case MVT::f32:
472 ObjSize = 4;
473 if (RegValuesToPass.size() >= 6) {
474 ValToStore = Val;
475 } else {
476 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000477 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000478 RegValuesToPass.push_back(Val);
479 }
480 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000481 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000482 ObjSize = 8;
483 // If we can store this directly into the outgoing slot, do so. We can
484 // do this when all ArgRegs are used and if the outgoing slot is aligned.
Chris Lattner7f9975a2006-01-15 19:15:46 +0000485 // FIXME: McGill/misr fails with this.
486 if (0 && RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000487 ValToStore = Val;
488 break;
489 }
490
491 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000492 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000493 // FALL THROUGH
494 case MVT::i64:
495 ObjSize = 8;
496 if (RegValuesToPass.size() >= 6) {
497 ValToStore = Val; // Whole thing is passed in memory.
498 break;
499 }
500
501 // Split the value into top and bottom part. Top part goes in a reg.
502 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
503 DAG.getConstant(1, MVT::i32));
504 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
505 DAG.getConstant(0, MVT::i32));
506 RegValuesToPass.push_back(Hi);
507
508 if (RegValuesToPass.size() >= 6) {
509 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000510 ArgOffset += 4;
511 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000512 } else {
513 RegValuesToPass.push_back(Lo);
514 }
515 break;
516 }
517
518 if (ValToStore.Val) {
519 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000520 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000521 NullSV = DAG.getSrcValue(NULL);
522 }
523 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
524 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
525 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
526 ValToStore, PtrOff, NullSV));
527 }
528 ArgOffset += ObjSize;
529 }
530
531 // Emit all stores, make sure the occur before any copies into physregs.
532 if (!Stores.empty())
533 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
534
535 static const unsigned ArgRegs[] = {
536 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
537 };
538
539 // Build a sequence of copy-to-reg nodes chained together with token chain
540 // and flag operands which copy the outgoing args into O[0-5].
541 SDOperand InFlag;
542 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
543 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
544 InFlag = Chain.getValue(1);
545 }
546
Chris Lattner2db3ff62005-12-18 15:55:15 +0000547 // If the callee is a GlobalAddress node (quite common, every direct call is)
548 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
549 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
550 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
551
552 std::vector<MVT::ValueType> NodeTys;
553 NodeTys.push_back(MVT::Other); // Returns a chain
554 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattner44ea7b12006-01-27 23:30:03 +0000555 std::vector<SDOperand> Ops;
556 Ops.push_back(Chain);
557 Ops.push_back(Callee);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000558 if (InFlag.Val)
Chris Lattner44ea7b12006-01-27 23:30:03 +0000559 Ops.push_back(InFlag);
560 Chain = DAG.getNode(V8ISD::CALL, NodeTys, Ops);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000561 InFlag = Chain.getValue(1);
562
563 MVT::ValueType RetTyVT = getValueType(RetTy);
564 SDOperand RetVal;
565 if (RetTyVT != MVT::isVoid) {
566 switch (RetTyVT) {
567 default: assert(0 && "Unknown value type to return!");
568 case MVT::i1:
569 case MVT::i8:
570 case MVT::i16:
571 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
572 Chain = RetVal.getValue(1);
573
574 // Add a note to keep track of whether it is sign or zero extended.
575 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
576 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
577 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
578 break;
579 case MVT::i32:
580 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
581 Chain = RetVal.getValue(1);
582 break;
583 case MVT::f32:
584 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
585 Chain = RetVal.getValue(1);
586 break;
587 case MVT::f64:
588 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
589 Chain = RetVal.getValue(1);
590 break;
591 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000592 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000593 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
594 Lo.getValue(2));
595 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
596 Chain = Hi.getValue(1);
597 break;
598 }
599 }
600
601 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
602 DAG.getConstant(ArgsSize, getPointerTy()));
603
Chris Lattner2db3ff62005-12-18 15:55:15 +0000604 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000605}
606
Chris Lattner4d55aca2005-12-18 01:20:35 +0000607std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
608LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
609 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000610 assert(0 && "Unimp");
611 abort();
612}
613
Chris Lattner4d55aca2005-12-18 01:20:35 +0000614SDOperand SparcV8TargetLowering::
615LowerOperation(SDOperand Op, SelectionDAG &DAG) {
616 switch (Op.getOpcode()) {
617 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000618 case ISD::GlobalAddress: {
619 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
620 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
621 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
622 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
623 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
624 }
Chris Lattner76acc872005-12-18 02:37:35 +0000625 case ISD::ConstantPool: {
626 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
627 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
628 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
629 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
630 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
631 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000632 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000633 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000634 assert(Op.getValueType() == MVT::i32);
635 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
636 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000637 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000638 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner3fbb7262006-01-11 07:27:40 +0000639 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000640 // Convert the int value to FP in an FP register.
Chris Lattner3fbb7262006-01-11 07:27:40 +0000641 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000642 }
Chris Lattner33084492005-12-18 08:13:54 +0000643 case ISD::BR_CC: {
644 SDOperand Chain = Op.getOperand(0);
645 SDOperand CC = Op.getOperand(1);
646 SDOperand LHS = Op.getOperand(2);
647 SDOperand RHS = Op.getOperand(3);
648 SDOperand Dest = Op.getOperand(4);
649
650 // Get the condition flag.
651 if (LHS.getValueType() == MVT::i32) {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000652 std::vector<MVT::ValueType> VTs;
653 VTs.push_back(MVT::i32);
654 VTs.push_back(MVT::Flag);
655 std::vector<SDOperand> Ops;
656 Ops.push_back(LHS);
657 Ops.push_back(RHS);
Chris Lattner138d3222006-01-12 07:38:04 +0000658 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
Chris Lattner33084492005-12-18 08:13:54 +0000659 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CC, Cond);
660 } else {
Chris Lattner4bb91022006-01-12 17:05:32 +0000661 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
Chris Lattner33084492005-12-18 08:13:54 +0000662 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CC, Cond);
663 }
664 }
665 case ISD::SELECT_CC: {
666 SDOperand LHS = Op.getOperand(0);
667 SDOperand RHS = Op.getOperand(1);
668 unsigned CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
669 SDOperand TrueVal = Op.getOperand(2);
670 SDOperand FalseVal = Op.getOperand(3);
671
Chris Lattnerdea95282006-01-30 04:34:44 +0000672 // If this is a select_cc of a "setcc", and if the setcc got lowered into
673 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
674 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0&&
675 CC == ISD::SETNE &&
676 ((LHS.getOpcode() == V8ISD::SELECT_ICC &&
677 LHS.getOperand(3).getOpcode() == V8ISD::CMPICC) ||
678 (LHS.getOpcode() == V8ISD::SELECT_FCC &&
679 LHS.getOperand(3).getOpcode() == V8ISD::CMPFCC)) &&
680 isa<ConstantSDNode>(LHS.getOperand(0)) &&
681 isa<ConstantSDNode>(LHS.getOperand(1)) &&
682 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
683 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
684 SDOperand CMPCC = LHS.getOperand(3);
685 CC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
686 LHS = CMPCC.getOperand(0);
687 RHS = CMPCC.getOperand(1);
688 }
689
Chris Lattner4bb91022006-01-12 17:05:32 +0000690 SDOperand CompareFlag;
Chris Lattner33084492005-12-18 08:13:54 +0000691 unsigned Opc;
Chris Lattner4bb91022006-01-12 17:05:32 +0000692 if (LHS.getValueType() == MVT::i32) {
693 std::vector<MVT::ValueType> VTs;
694 VTs.push_back(LHS.getValueType()); // subcc returns a value
695 VTs.push_back(MVT::Flag);
696 std::vector<SDOperand> Ops;
697 Ops.push_back(LHS);
698 Ops.push_back(RHS);
699 CompareFlag = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
700 Opc = V8ISD::SELECT_ICC;
701 } else {
702 CompareFlag = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
703 Opc = V8ISD::SELECT_FCC;
704 }
Chris Lattner33084492005-12-18 08:13:54 +0000705 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
706 DAG.getConstant(CC, MVT::i32), CompareFlag);
707 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000708 case ISD::VASTART: {
709 // vastart just stores the address of the VarArgsFrameIndex slot into the
710 // memory location argument.
711 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
712 DAG.getRegister(V8::I6, MVT::i32),
713 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
714 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), Offset,
715 Op.getOperand(1), Op.getOperand(2));
716 }
Nate Begemanee625572006-01-27 21:09:22 +0000717 case ISD::RET: {
718 SDOperand Copy;
719
720 switch(Op.getNumOperands()) {
721 default:
722 assert(0 && "Do not know how to return this many arguments!");
723 abort();
724 case 1:
725 return SDOperand(); // ret void is legal
726 case 2: {
727 unsigned ArgReg;
728 switch(Op.getOperand(1).getValueType()) {
729 default: assert(0 && "Unknown type to return!");
730 case MVT::i32: ArgReg = V8::I0; break;
731 case MVT::f32: ArgReg = V8::F0; break;
732 case MVT::f64: ArgReg = V8::D0; break;
733 }
734 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
735 SDOperand());
736 break;
737 }
738 case 3:
739 Copy = DAG.getCopyToReg(Op.getOperand(0), V8::I0, Op.getOperand(2),
740 SDOperand());
741 Copy = DAG.getCopyToReg(Copy, V8::I1, Op.getOperand(1), Copy.getValue(1));
742 break;
743 }
744 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
745 }
Chris Lattnerbce88872006-01-15 08:43:57 +0000746 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000747}
748
Chris Lattner33084492005-12-18 08:13:54 +0000749MachineBasicBlock *
750SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
751 MachineBasicBlock *BB) {
752 unsigned BROpcode;
753 // Figure out the conditional branch opcode to use for this select_cc.
754 switch (MI->getOpcode()) {
755 default: assert(0 && "Unknown SELECT_CC!");
756 case V8::SELECT_CC_Int_ICC:
757 case V8::SELECT_CC_FP_ICC:
758 case V8::SELECT_CC_DFP_ICC:
759 // Integer compare.
760 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
761 default: assert(0 && "Unknown integer condition code!");
762 case ISD::SETEQ: BROpcode = V8::BE; break;
763 case ISD::SETNE: BROpcode = V8::BNE; break;
764 case ISD::SETLT: BROpcode = V8::BL; break;
765 case ISD::SETGT: BROpcode = V8::BG; break;
766 case ISD::SETLE: BROpcode = V8::BLE; break;
767 case ISD::SETGE: BROpcode = V8::BGE; break;
768 case ISD::SETULT: BROpcode = V8::BCS; break;
769 case ISD::SETULE: BROpcode = V8::BLEU; break;
770 case ISD::SETUGT: BROpcode = V8::BGU; break;
771 case ISD::SETUGE: BROpcode = V8::BCC; break;
772 }
773 break;
774 case V8::SELECT_CC_Int_FCC:
775 case V8::SELECT_CC_FP_FCC:
776 case V8::SELECT_CC_DFP_FCC:
777 // FP compare.
778 switch ((ISD::CondCode)MI->getOperand(3).getImmedValue()) {
779 default: assert(0 && "Unknown fp condition code!");
780 case ISD::SETEQ: BROpcode = V8::FBE; break;
781 case ISD::SETNE: BROpcode = V8::FBNE; break;
782 case ISD::SETLT: BROpcode = V8::FBL; break;
783 case ISD::SETGT: BROpcode = V8::FBG; break;
784 case ISD::SETLE: BROpcode = V8::FBLE; break;
785 case ISD::SETGE: BROpcode = V8::FBGE; break;
786 case ISD::SETULT: BROpcode = V8::FBUL; break;
787 case ISD::SETULE: BROpcode = V8::FBULE; break;
788 case ISD::SETUGT: BROpcode = V8::FBUG; break;
789 case ISD::SETUGE: BROpcode = V8::FBUGE; break;
790 case ISD::SETUO: BROpcode = V8::FBU; break;
791 case ISD::SETO: BROpcode = V8::FBO; break;
792 case ISD::SETONE: BROpcode = V8::FBLG; break;
793 case ISD::SETUEQ: BROpcode = V8::FBUE; break;
794 }
795 break;
796 }
797
798 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
799 // control-flow pattern. The incoming instruction knows the destination vreg
800 // to set, the condition code register to branch on, the true/false values to
801 // select between, and a branch opcode to use.
802 const BasicBlock *LLVM_BB = BB->getBasicBlock();
803 ilist<MachineBasicBlock>::iterator It = BB;
804 ++It;
805
806 // thisMBB:
807 // ...
808 // TrueVal = ...
809 // [f]bCC copy1MBB
810 // fallthrough --> copy0MBB
811 MachineBasicBlock *thisMBB = BB;
812 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
813 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
814 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
815 MachineFunction *F = BB->getParent();
816 F->getBasicBlockList().insert(It, copy0MBB);
817 F->getBasicBlockList().insert(It, sinkMBB);
818 // Update machine-CFG edges
819 BB->addSuccessor(copy0MBB);
820 BB->addSuccessor(sinkMBB);
821
822 // copy0MBB:
823 // %FalseValue = ...
824 // # fallthrough to sinkMBB
825 BB = copy0MBB;
826
827 // Update machine-CFG edges
828 BB->addSuccessor(sinkMBB);
829
830 // sinkMBB:
831 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
832 // ...
833 BB = sinkMBB;
834 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
835 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
836 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
837
838 delete MI; // The pseudo instruction is gone now.
839 return BB;
840}
841
Chris Lattner6c18b102005-12-17 07:47:01 +0000842//===----------------------------------------------------------------------===//
843// Instruction Selector Implementation
844//===----------------------------------------------------------------------===//
845
846//===--------------------------------------------------------------------===//
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000847/// SparcV8DAGToDAGISel - SPARC specific code to select Sparc V8 machine
Chris Lattner6c18b102005-12-17 07:47:01 +0000848/// instructions for SelectionDAG operations.
849///
850namespace {
851class SparcV8DAGToDAGISel : public SelectionDAGISel {
852 SparcV8TargetLowering V8Lowering;
Chris Lattner76afdc92006-01-30 05:35:57 +0000853
854 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
855 /// make the right decision when generating code for different targets.
856 const SparcV8Subtarget &Subtarget;
Chris Lattner6c18b102005-12-17 07:47:01 +0000857public:
858 SparcV8DAGToDAGISel(TargetMachine &TM)
Chris Lattner76afdc92006-01-30 05:35:57 +0000859 : SelectionDAGISel(V8Lowering), V8Lowering(TM),
860 Subtarget(TM.getSubtarget<SparcV8Subtarget>()) {
861 }
Chris Lattner6c18b102005-12-17 07:47:01 +0000862
863 SDOperand Select(SDOperand Op);
864
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000865 // Complex Pattern Selectors.
866 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
867 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
868
Chris Lattner6c18b102005-12-17 07:47:01 +0000869 /// InstructionSelectBasicBlock - This callback is invoked by
870 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
871 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
872
873 virtual const char *getPassName() const {
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000874 return "SparcV8 DAG->DAG Pattern Instruction Selection";
Chris Lattner6c18b102005-12-17 07:47:01 +0000875 }
876
877 // Include the pieces autogenerated from the target description.
878#include "SparcV8GenDAGISel.inc"
879};
880} // end anonymous namespace
881
882/// InstructionSelectBasicBlock - This callback is invoked by
883/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
884void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
885 DEBUG(BB->dump());
886
887 // Select target instructions for the DAG.
888 DAG.setRoot(Select(DAG.getRoot()));
889 CodeGenMap.clear();
890 DAG.RemoveDeadNodes();
891
892 // Emit machine code to BB.
893 ScheduleAndEmitDAG(DAG);
894}
895
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000896bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
897 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000898 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
899 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000900 Offset = CurDAG->getTargetConstant(0, MVT::i32);
901 return true;
902 }
903
904 if (Addr.getOpcode() == ISD::ADD) {
905 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
906 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000907 if (FrameIndexSDNode *FIN =
908 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000909 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +0000910 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000911 } else {
912 Base = Select(Addr.getOperand(0));
913 }
914 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
915 return true;
916 }
917 }
918 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
919 Base = Select(Addr.getOperand(1));
920 Offset = Addr.getOperand(0).getOperand(0);
921 return true;
922 }
923 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
924 Base = Select(Addr.getOperand(0));
925 Offset = Addr.getOperand(1).getOperand(0);
926 return true;
927 }
928 }
929 Base = Select(Addr);
930 Offset = CurDAG->getTargetConstant(0, MVT::i32);
931 return true;
932}
933
Chris Lattner9034b882005-12-17 21:25:27 +0000934bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000935 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000936 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +0000937 if (Addr.getOpcode() == ISD::ADD) {
938 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
939 Predicate_simm13(Addr.getOperand(1).Val))
940 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +0000941 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
942 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
943 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +0000944 R1 = Select(Addr.getOperand(0));
945 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +0000946 return true;
947 }
948
949 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000950 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
951 return true;
952}
953
Chris Lattner6c18b102005-12-17 07:47:01 +0000954SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
955 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +0000956 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
957 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +0000958 return Op; // Already selected.
959 // If this has already been converted, use it.
960 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
961 if (CGMI != CodeGenMap.end()) return CGMI->second;
962
963 switch (N->getOpcode()) {
964 default: break;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000965 case ISD::FrameIndex: {
966 int FI = cast<FrameIndexSDNode>(N)->getIndex();
967 if (N->hasOneUse())
968 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
969 CurDAG->getTargetFrameIndex(FI, MVT::i32),
970 CurDAG->getTargetConstant(0, MVT::i32));
971 return CodeGenMap[Op] =
972 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
973 CurDAG->getTargetFrameIndex(FI, MVT::i32),
974 CurDAG->getTargetConstant(0, MVT::i32));
975 }
Chris Lattnerd19fc652005-12-17 22:55:57 +0000976 case ISD::ADD_PARTS: {
977 SDOperand LHSL = Select(N->getOperand(0));
978 SDOperand LHSH = Select(N->getOperand(1));
979 SDOperand RHSL = Select(N->getOperand(2));
980 SDOperand RHSH = Select(N->getOperand(3));
981 // FIXME, handle immediate RHS.
982 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
983 LHSL, RHSL);
984 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
985 Low.getValue(1));
986 CodeGenMap[SDOperand(N, 0)] = Low;
987 CodeGenMap[SDOperand(N, 1)] = Hi;
988 return Op.ResNo ? Hi : Low;
989 }
990 case ISD::SUB_PARTS: {
991 SDOperand LHSL = Select(N->getOperand(0));
992 SDOperand LHSH = Select(N->getOperand(1));
993 SDOperand RHSL = Select(N->getOperand(2));
994 SDOperand RHSH = Select(N->getOperand(3));
995 // FIXME, handle immediate RHS.
996 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
997 LHSL, RHSL);
998 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
999 Low.getValue(1));
1000 CodeGenMap[SDOperand(N, 0)] = Low;
1001 CodeGenMap[SDOperand(N, 1)] = Hi;
1002 return Op.ResNo ? Hi : Low;
1003 }
Chris Lattner7087e572005-12-17 22:39:19 +00001004 case ISD::SDIV:
1005 case ISD::UDIV: {
1006 // FIXME: should use a custom expander to expose the SRA to the dag.
1007 SDOperand DivLHS = Select(N->getOperand(0));
1008 SDOperand DivRHS = Select(N->getOperand(1));
1009
1010 // Set the Y register to the high-part.
1011 SDOperand TopPart;
1012 if (N->getOpcode() == ISD::SDIV) {
1013 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
1014 CurDAG->getTargetConstant(31, MVT::i32));
1015 } else {
1016 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
1017 }
1018 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
1019 CurDAG->getRegister(V8::G0, MVT::i32));
1020
1021 // FIXME: Handle div by immediate.
1022 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
1023 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
1024 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001025 case ISD::MULHU:
1026 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +00001027 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001028 SDOperand MulLHS = Select(N->getOperand(0));
1029 SDOperand MulRHS = Select(N->getOperand(1));
1030 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
1031 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
1032 MulLHS, MulRHS);
1033 // The high part is in the Y register.
1034 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
1035 }
Chris Lattner44ea7b12006-01-27 23:30:03 +00001036 case V8ISD::CALL:
Chris Lattner2db3ff62005-12-18 15:55:15 +00001037 // FIXME: This is a workaround for a bug in tblgen.
1038 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
1039 // Emits: (CALL:void (tglobaladdr:i32):$dst)
1040 // Pattern complexity = 2 cost = 1
1041 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +00001042 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
1043 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +00001044 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +00001045 SDOperand Chain = N->getOperand(0);
1046 SDOperand Tmp0 = N1;
1047 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +00001048 SDOperand Result;
1049 if (N->getNumOperands() == 3) {
1050 InFlag = Select(N->getOperand(2));
1051 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1052 Chain, InFlag);
1053 } else {
1054 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1055 Chain);
1056 }
Chris Lattner2db3ff62005-12-18 15:55:15 +00001057 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
1058 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
1059 return Result.getValue(Op.ResNo);
1060 }
1061 P47Fail:;
1062
Chris Lattner6c18b102005-12-17 07:47:01 +00001063 }
1064
1065 return SelectCode(Op);
1066}
1067
1068
Chris Lattner4dcfaac2006-01-26 07:22:22 +00001069/// createSparcV8ISelDag - This pass converts a legalized DAG into a
1070/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +00001071///
1072FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1073 return new SparcV8DAGToDAGISel(TM);
1074}