blob: bddf0986ebd2fffe1aa9c23bcb8254950854a0fb [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,
Chris Lattner9072c052006-01-30 06:14:02 +000036 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
40 SELECT_ICC, // Select between two values using the current ICC flags.
41 SELECT_FCC, // Select between two values using the current FCC flags.
Chris Lattnere3572462005-12-18 02:10:39 +000042
Chris Lattner9072c052006-01-30 06:14:02 +000043 Hi, Lo, // Hi/Lo operations, typically on a global address.
Chris Lattner8fa54dc2005-12-18 06:59:57 +000044
Chris Lattner9072c052006-01-30 06:14:02 +000045 FTOI, // FP to Int within a FP register.
46 ITOF, // Int to FP within a FP register.
47
48 CALL, // A V8 call instruction.
49 RET_FLAG, // Return with a flag operand.
Chris Lattner4d55aca2005-12-18 01:20:35 +000050 };
51}
52
Chris Lattner3772bcb2006-01-30 07:43:04 +000053// Enums corresponding to SparcV8 condition codes, both icc's and fcc's. These
54// values must be kept in sync with the ones in the .td file.
55namespace V8CC {
56 enum CondCodes {
57 //ICC_A = 8 , // Always
58 //ICC_N = 0 , // Never
59 ICC_NE = 9 , // Not Equal
60 ICC_E = 1 , // Equal
61 ICC_G = 10 , // Greater
62 ICC_LE = 2 , // Less or Equal
63 ICC_GE = 11 , // Greater or Equal
64 ICC_L = 3 , // Less
65 ICC_GU = 12 , // Greater Unsigned
66 ICC_LEU = 4 , // Less or Equal Unsigned
67 ICC_CC = 13 , // Carry Clear/Great or Equal Unsigned
68 ICC_CS = 5 , // Carry Set/Less Unsigned
69 ICC_POS = 14 , // Positive
70 ICC_NEG = 6 , // Negative
71 ICC_VC = 15 , // Overflow Clear
72 ICC_VS = 7 , // Overflow Set
73
74 //FCC_A = 8+16, // Always
75 //FCC_N = 0+16, // Never
76 FCC_U = 7+16, // Unordered
77 FCC_G = 6+16, // Greater
78 FCC_UG = 5+16, // Unordered or Greater
79 FCC_L = 4+16, // Less
80 FCC_UL = 3+16, // Unordered or Less
81 FCC_LG = 2+16, // Less or Greater
82 FCC_NE = 1+16, // Not Equal
83 FCC_E = 9+16, // Equal
84 FCC_UE = 10+16, // Unordered or Equal
85 FCC_GE = 11+16, // Greater or Equal
86 FCC_UGE = 12+16, // Unordered or Greater or Equal
87 FCC_LE = 13+16, // Less or Equal
88 FCC_ULE = 14+16, // Unordered or Less or Equal
89 FCC_O = 15+16, // Ordered
90 };
91}
92
93
94/// IntCondCCodeToICC - Convert a DAG integer condition code to a SPARC ICC
95/// condition.
96static V8CC::CondCodes IntCondCCodeToICC(ISD::CondCode CC) {
97 switch (CC) {
98 default: assert(0 && "Unknown integer condition code!");
99 case ISD::SETEQ: return V8CC::ICC_E;
100 case ISD::SETNE: return V8CC::ICC_NE;
101 case ISD::SETLT: return V8CC::ICC_L;
102 case ISD::SETGT: return V8CC::ICC_G;
103 case ISD::SETLE: return V8CC::ICC_LE;
104 case ISD::SETGE: return V8CC::ICC_GE;
105 case ISD::SETULT: return V8CC::ICC_CS;
106 case ISD::SETULE: return V8CC::ICC_LEU;
107 case ISD::SETUGT: return V8CC::ICC_GU;
108 case ISD::SETUGE: return V8CC::ICC_CC;
109 }
110}
111
112/// FPCondCCodeToFCC - Convert a DAG floatingp oint condition code to a SPARC
113/// FCC condition.
114static V8CC::CondCodes FPCondCCodeToFCC(ISD::CondCode CC) {
115 switch (CC) {
116 default: assert(0 && "Unknown fp condition code!");
117 case ISD::SETEQ: return V8CC::FCC_E;
118 case ISD::SETNE: return V8CC::FCC_NE;
119 case ISD::SETLT: return V8CC::FCC_L;
120 case ISD::SETGT: return V8CC::FCC_G;
121 case ISD::SETLE: return V8CC::FCC_LE;
122 case ISD::SETGE: return V8CC::FCC_GE;
123 case ISD::SETULT: return V8CC::FCC_UL;
124 case ISD::SETULE: return V8CC::FCC_ULE;
125 case ISD::SETUGT: return V8CC::FCC_UG;
126 case ISD::SETUGE: return V8CC::FCC_UGE;
127 case ISD::SETUO: return V8CC::FCC_U;
128 case ISD::SETO: return V8CC::FCC_O;
129 case ISD::SETONE: return V8CC::FCC_LG;
130 case ISD::SETUEQ: return V8CC::FCC_UE;
131 }
132}
133
134
135static unsigned SPARCCondCodeToBranchInstr(V8CC::CondCodes CC) {
136 switch (CC) {
137 default: assert(0 && "Unknown condition code");
138 case V8CC::ICC_NE: return V8::BNE;
139 case V8CC::ICC_E: return V8::BE;
140 case V8CC::ICC_G: return V8::BG;
141 case V8CC::ICC_LE: return V8::BLE;
142 case V8CC::ICC_GE: return V8::BGE;
143 case V8CC::ICC_L: return V8::BL;
144 case V8CC::ICC_GU: return V8::BGU;
145 case V8CC::ICC_LEU: return V8::BLEU;
146 case V8CC::ICC_CC: return V8::BCC;
147 case V8CC::ICC_CS: return V8::BCS;
148 case V8CC::ICC_POS: return V8::BPOS;
149 case V8CC::ICC_NEG: return V8::BNEG;
150 case V8CC::ICC_VC: return V8::BVC;
151 case V8CC::ICC_VS: return V8::BVS;
152 case V8CC::FCC_U: return V8::FBU;
153 case V8CC::FCC_G: return V8::FBG;
154 case V8CC::FCC_UG: return V8::FBUG;
155 case V8CC::FCC_L: return V8::FBL;
156 case V8CC::FCC_UL: return V8::FBUL;
157 case V8CC::FCC_LG: return V8::FBLG;
158 case V8CC::FCC_NE: return V8::FBNE;
159 case V8CC::FCC_E: return V8::FBE;
160 case V8CC::FCC_UE: return V8::FBUE;
161 case V8CC::FCC_GE: return V8::FBGE;
162 case V8CC::FCC_UGE: return V8::FBUGE;
163 case V8CC::FCC_LE: return V8::FBLE;
164 case V8CC::FCC_ULE: return V8::FBULE;
165 case V8CC::FCC_O: return V8::FBO;
166 }
167}
168
169
Chris Lattner6c18b102005-12-17 07:47:01 +0000170namespace {
171 class SparcV8TargetLowering : public TargetLowering {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000172 int VarArgsFrameOffset; // Frame offset to start of varargs area.
Chris Lattner6c18b102005-12-17 07:47:01 +0000173 public:
174 SparcV8TargetLowering(TargetMachine &TM);
Chris Lattner4d55aca2005-12-18 01:20:35 +0000175 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
Chris Lattner4a397e02006-01-30 03:51:45 +0000176
177 /// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
178 /// be zero. Op is expected to be a target specific node. Used by DAG
179 /// combiner.
180 virtual bool isMaskedValueZeroForTargetNode(const SDOperand &Op,
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000181 uint64_t Mask) const;
Chris Lattner4a397e02006-01-30 03:51:45 +0000182
Chris Lattner6c18b102005-12-17 07:47:01 +0000183 virtual std::vector<SDOperand>
184 LowerArguments(Function &F, SelectionDAG &DAG);
185 virtual std::pair<SDOperand, SDOperand>
186 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
187 unsigned CC,
188 bool isTailCall, SDOperand Callee, ArgListTy &Args,
189 SelectionDAG &DAG);
Chris Lattner6c18b102005-12-17 07:47:01 +0000190 virtual std::pair<SDOperand, SDOperand>
191 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
192 SelectionDAG &DAG);
Chris Lattner33084492005-12-18 08:13:54 +0000193 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
194 MachineBasicBlock *MBB);
Chris Lattner72878a42006-01-12 07:31:15 +0000195
196 virtual const char *getTargetNodeName(unsigned Opcode) const;
Chris Lattner6c18b102005-12-17 07:47:01 +0000197 };
198}
199
200SparcV8TargetLowering::SparcV8TargetLowering(TargetMachine &TM)
201 : TargetLowering(TM) {
202
203 // Set up the register classes.
204 addRegisterClass(MVT::i32, V8::IntRegsRegisterClass);
205 addRegisterClass(MVT::f32, V8::FPRegsRegisterClass);
206 addRegisterClass(MVT::f64, V8::DFPRegsRegisterClass);
Chris Lattner9a60ff62005-12-17 20:50:42 +0000207
Chris Lattnere3572462005-12-18 02:10:39 +0000208 // Custom legalize GlobalAddress nodes into LO/HI parts.
209 setOperationAction(ISD::GlobalAddress, MVT::i32, Custom);
Chris Lattner76acc872005-12-18 02:37:35 +0000210 setOperationAction(ISD::ConstantPool , MVT::i32, Custom);
Chris Lattnere3572462005-12-18 02:10:39 +0000211
Chris Lattner9a60ff62005-12-17 20:50:42 +0000212 // Sparc doesn't have sext_inreg, replace them with shl/sra
Chris Lattner33084492005-12-18 08:13:54 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16, Expand);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
Chris Lattner7087e572005-12-17 22:39:19 +0000216
217 // Sparc has no REM operation.
218 setOperationAction(ISD::UREM, MVT::i32, Expand);
219 setOperationAction(ISD::SREM, MVT::i32, Expand);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000220
221 // Custom expand fp<->sint
222 setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom);
223 setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom);
224
225 // Expand fp<->uint
226 setOperationAction(ISD::FP_TO_UINT, MVT::i32, Expand);
227 setOperationAction(ISD::UINT_TO_FP, MVT::i32, Expand);
Chris Lattner6c18b102005-12-17 07:47:01 +0000228
Chris Lattner53e88452005-12-23 05:13:35 +0000229 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
230 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
231
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000232 // Turn FP extload into load/fextend
Chris Lattner065c8962005-12-18 07:13:32 +0000233 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
234
Chris Lattner4d55aca2005-12-18 01:20:35 +0000235 // Sparc has no select or setcc: expand to SELECT_CC.
236 setOperationAction(ISD::SELECT, MVT::i32, Expand);
237 setOperationAction(ISD::SELECT, MVT::f32, Expand);
238 setOperationAction(ISD::SELECT, MVT::f64, Expand);
239 setOperationAction(ISD::SETCC, MVT::i32, Expand);
240 setOperationAction(ISD::SETCC, MVT::f32, Expand);
241 setOperationAction(ISD::SETCC, MVT::f64, Expand);
242
243 // Sparc doesn't have BRCOND either, it has BR_CC.
244 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
245 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
246 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
247 setOperationAction(ISD::BR_CC, MVT::i32, Custom);
248 setOperationAction(ISD::BR_CC, MVT::f32, Custom);
249 setOperationAction(ISD::BR_CC, MVT::f64, Custom);
250
Chris Lattner33084492005-12-18 08:13:54 +0000251 setOperationAction(ISD::SELECT_CC, MVT::i32, Custom);
252 setOperationAction(ISD::SELECT_CC, MVT::f32, Custom);
253 setOperationAction(ISD::SELECT_CC, MVT::f64, Custom);
254
Chris Lattnere90ac3a2005-12-18 23:00:27 +0000255 // V8 has no intrinsics for these particular operations.
256 setOperationAction(ISD::MEMMOVE, MVT::Other, Expand);
257 setOperationAction(ISD::MEMSET, MVT::Other, Expand);
258 setOperationAction(ISD::MEMCPY, MVT::Other, Expand);
259
Chris Lattner61772c22005-12-19 01:39:40 +0000260 setOperationAction(ISD::FSIN , MVT::f64, Expand);
261 setOperationAction(ISD::FCOS , MVT::f64, Expand);
262 setOperationAction(ISD::FSIN , MVT::f32, Expand);
263 setOperationAction(ISD::FCOS , MVT::f32, Expand);
264 setOperationAction(ISD::CTPOP, MVT::i32, Expand);
265 setOperationAction(ISD::CTTZ , MVT::i32, Expand);
266 setOperationAction(ISD::CTLZ , MVT::i32, Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000267 setOperationAction(ISD::ROTL , MVT::i32, Expand);
268 setOperationAction(ISD::ROTR , MVT::i32, Expand);
Nate Begemand88fc032006-01-14 03:14:10 +0000269 setOperationAction(ISD::BSWAP, MVT::i32, Expand);
Chris Lattner61772c22005-12-19 01:39:40 +0000270
271 setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand);
272 setOperationAction(ISD::SRA_PARTS, MVT::i32, Expand);
273 setOperationAction(ISD::SRL_PARTS, MVT::i32, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000274
275 // We don't have line number support yet.
276 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000277 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
278 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Jim Laskeye81aecb2005-12-21 20:51:37 +0000279
Nate Begemanee625572006-01-27 21:09:22 +0000280 // RET must be custom lowered, to meet ABI requirements
281 setOperationAction(ISD::RET , MVT::Other, Custom);
282
Nate Begemanacc398c2006-01-25 18:21:52 +0000283 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
284 setOperationAction(ISD::VASTART , MVT::Other, Custom);
285
286 // Use the default implementation.
287 setOperationAction(ISD::VAARG , MVT::Other, Expand);
288 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
289 setOperationAction(ISD::VAEND , MVT::Other, Expand);
290 setOperationAction(ISD::STACKSAVE , MVT::Other, Expand);
291 setOperationAction(ISD::STACKRESTORE , MVT::Other, Expand);
Chris Lattner9072c052006-01-30 06:14:02 +0000292 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner934ea492006-01-15 08:55:25 +0000293
294 setStackPointerRegisterToSaveRestore(V8::O6);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000295
Chris Lattner9072c052006-01-30 06:14:02 +0000296 if (TM.getSubtarget<SparcV8Subtarget>().isV9()) {
297 setOperationAction(ISD::CTPOP, MVT::i32, Legal);
298 }
299
Chris Lattner6c18b102005-12-17 07:47:01 +0000300 computeRegisterProperties();
301}
302
Chris Lattner72878a42006-01-12 07:31:15 +0000303const char *SparcV8TargetLowering::getTargetNodeName(unsigned Opcode) const {
304 switch (Opcode) {
Chris Lattner138d3222006-01-12 07:38:04 +0000305 default: return 0;
Chris Lattner72878a42006-01-12 07:31:15 +0000306 case V8ISD::CMPICC: return "V8ISD::CMPICC";
307 case V8ISD::CMPFCC: return "V8ISD::CMPFCC";
308 case V8ISD::BRICC: return "V8ISD::BRICC";
309 case V8ISD::BRFCC: return "V8ISD::BRFCC";
Chris Lattner9072c052006-01-30 06:14:02 +0000310 case V8ISD::SELECT_ICC: return "V8ISD::SELECT_ICC";
311 case V8ISD::SELECT_FCC: return "V8ISD::SELECT_FCC";
Chris Lattner72878a42006-01-12 07:31:15 +0000312 case V8ISD::Hi: return "V8ISD::Hi";
313 case V8ISD::Lo: return "V8ISD::Lo";
314 case V8ISD::FTOI: return "V8ISD::FTOI";
315 case V8ISD::ITOF: return "V8ISD::ITOF";
Chris Lattner44ea7b12006-01-27 23:30:03 +0000316 case V8ISD::CALL: return "V8ISD::CALL";
Chris Lattner72878a42006-01-12 07:31:15 +0000317 case V8ISD::RET_FLAG: return "V8ISD::RET_FLAG";
318 }
319}
320
Chris Lattner4a397e02006-01-30 03:51:45 +0000321/// isMaskedValueZeroForTargetNode - Return true if 'Op & Mask' is known to
322/// be zero. Op is expected to be a target specific node. Used by DAG
323/// combiner.
324bool SparcV8TargetLowering::
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000325isMaskedValueZeroForTargetNode(const SDOperand &Op, uint64_t Mask) const {
Chris Lattner4a397e02006-01-30 03:51:45 +0000326 switch (Op.getOpcode()) {
327 default: return false;
328 case V8ISD::SELECT_ICC:
329 case V8ISD::SELECT_FCC:
330 assert(MVT::isInteger(Op.getValueType()) && "Not an integer select!");
331 // These operations are masked zero if both the left and the right are zero.
Chris Lattnerc6fd6cd2006-01-30 04:09:27 +0000332 return MaskedValueIsZero(Op.getOperand(0), Mask) &&
333 MaskedValueIsZero(Op.getOperand(1), Mask);
Chris Lattner4a397e02006-01-30 03:51:45 +0000334 }
335}
336
337
Chris Lattner384e5ef2005-12-18 13:33:06 +0000338/// LowerArguments - V8 uses a very simple ABI, where all values are passed in
339/// either one or two GPRs, including FP values. TODO: we should pass FP values
340/// in FP registers for fastcc functions.
Chris Lattner6c18b102005-12-17 07:47:01 +0000341std::vector<SDOperand>
342SparcV8TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Chris Lattnera01b7572005-12-17 08:03:24 +0000343 MachineFunction &MF = DAG.getMachineFunction();
344 SSARegMap *RegMap = MF.getSSARegMap();
345 std::vector<SDOperand> ArgValues;
346
Chris Lattner384e5ef2005-12-18 13:33:06 +0000347 static const unsigned ArgRegs[] = {
Chris Lattnera01b7572005-12-17 08:03:24 +0000348 V8::I0, V8::I1, V8::I2, V8::I3, V8::I4, V8::I5
349 };
Chris Lattner384e5ef2005-12-18 13:33:06 +0000350
351 const unsigned *CurArgReg = ArgRegs, *ArgRegEnd = ArgRegs+6;
352 unsigned ArgOffset = 68;
353
354 SDOperand Root = DAG.getRoot();
355 std::vector<SDOperand> OutChains;
356
Chris Lattnera01b7572005-12-17 08:03:24 +0000357 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
358 MVT::ValueType ObjectVT = getValueType(I->getType());
Chris Lattnera01b7572005-12-17 08:03:24 +0000359
360 switch (ObjectVT) {
361 default: assert(0 && "Unhandled argument type!");
Chris Lattnera01b7572005-12-17 08:03:24 +0000362 case MVT::i1:
363 case MVT::i8:
364 case MVT::i16:
Chris Lattner384e5ef2005-12-18 13:33:06 +0000365 case MVT::i32:
366 if (I->use_empty()) { // Argument is dead.
367 if (CurArgReg < ArgRegEnd) ++CurArgReg;
368 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
369 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
370 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
371 MF.addLiveIn(*CurArgReg++, VReg);
372 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
373 if (ObjectVT != MVT::i32) {
374 unsigned AssertOp = I->getType()->isSigned() ? ISD::AssertSext
375 : ISD::AssertZext;
376 Arg = DAG.getNode(AssertOp, MVT::i32, Arg,
377 DAG.getValueType(ObjectVT));
378 Arg = DAG.getNode(ISD::TRUNCATE, ObjectVT, Arg);
379 }
380 ArgValues.push_back(Arg);
381 } else {
382 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
383 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
384 SDOperand Load;
385 if (ObjectVT == MVT::i32) {
386 Load = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
387 } else {
388 unsigned LoadOp =
389 I->getType()->isSigned() ? ISD::SEXTLOAD : ISD::ZEXTLOAD;
390
Chris Lattner99cf5092006-01-16 01:40:00 +0000391 // Sparc is big endian, so add an offset based on the ObjectVT.
392 unsigned Offset = 4-std::max(1U, MVT::getSizeInBits(ObjectVT)/8);
393 FIPtr = DAG.getNode(ISD::ADD, MVT::i32, FIPtr,
394 DAG.getConstant(Offset, MVT::i32));
Chris Lattner384e5ef2005-12-18 13:33:06 +0000395 Load = DAG.getExtLoad(LoadOp, MVT::i32, Root, FIPtr,
396 DAG.getSrcValue(0), ObjectVT);
Chris Lattnerf7511b42006-01-15 22:22:01 +0000397 Load = DAG.getNode(ISD::TRUNCATE, ObjectVT, Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000398 }
399 ArgValues.push_back(Load);
Chris Lattnera01b7572005-12-17 08:03:24 +0000400 }
Chris Lattner384e5ef2005-12-18 13:33:06 +0000401
402 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000403 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000404 case MVT::f32:
405 if (I->use_empty()) { // Argument is dead.
406 if (CurArgReg < ArgRegEnd) ++CurArgReg;
407 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
408 } else if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
409 // FP value is passed in an integer register.
410 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
411 MF.addLiveIn(*CurArgReg++, VReg);
412 SDOperand Arg = DAG.getCopyFromReg(Root, VReg, MVT::i32);
413
Chris Lattnera01874f2005-12-23 02:31:39 +0000414 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Arg);
415 ArgValues.push_back(Arg);
Chris Lattner46030a62006-01-19 07:22:29 +0000416 } else {
417 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
418 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
419 SDOperand Load = DAG.getLoad(MVT::f32, Root, FIPtr, DAG.getSrcValue(0));
420 ArgValues.push_back(Load);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000421 }
422 ArgOffset += 4;
Chris Lattner217aabf2005-12-17 20:59:06 +0000423 break;
Chris Lattner384e5ef2005-12-18 13:33:06 +0000424
425 case MVT::i64:
426 case MVT::f64:
427 if (I->use_empty()) { // Argument is dead.
428 if (CurArgReg < ArgRegEnd) ++CurArgReg;
429 if (CurArgReg < ArgRegEnd) ++CurArgReg;
430 ArgValues.push_back(DAG.getNode(ISD::UNDEF, ObjectVT));
431 } else if (CurArgReg == ArgRegEnd && ObjectVT == MVT::f64 &&
432 ((CurArgReg-ArgRegs) & 1) == 0) {
433 // If this is a double argument and the whole thing lives on the stack,
434 // and the argument is aligned, load the double straight from the stack.
435 // We can't do a load in cases like void foo([6ints], int,double),
436 // because the double wouldn't be aligned!
437 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(8, ArgOffset);
438 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
439 ArgValues.push_back(DAG.getLoad(MVT::f64, Root, FIPtr,
440 DAG.getSrcValue(0)));
441 } else {
442 SDOperand HiVal;
443 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
444 unsigned VRegHi = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
445 MF.addLiveIn(*CurArgReg++, VRegHi);
446 HiVal = DAG.getCopyFromReg(Root, VRegHi, MVT::i32);
447 } else {
448 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
449 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
450 HiVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
451 }
452
453 SDOperand LoVal;
454 if (CurArgReg < ArgRegEnd) { // Lives in an incoming GPR
455 unsigned VRegLo = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
456 MF.addLiveIn(*CurArgReg++, VRegLo);
457 LoVal = DAG.getCopyFromReg(Root, VRegLo, MVT::i32);
458 } else {
459 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset+4);
460 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
461 LoVal = DAG.getLoad(MVT::i32, Root, FIPtr, DAG.getSrcValue(0));
462 }
463
464 // Compose the two halves together into an i64 unit.
465 SDOperand WholeValue =
466 DAG.getNode(ISD::BUILD_PAIR, MVT::i64, LoVal, HiVal);
Chris Lattnera01874f2005-12-23 02:31:39 +0000467
468 // If we want a double, do a bit convert.
469 if (ObjectVT == MVT::f64)
470 WholeValue = DAG.getNode(ISD::BIT_CONVERT, MVT::f64, WholeValue);
471
472 ArgValues.push_back(WholeValue);
Chris Lattner384e5ef2005-12-18 13:33:06 +0000473 }
474 ArgOffset += 8;
475 break;
Chris Lattnera01b7572005-12-17 08:03:24 +0000476 }
477 }
478
Chris Lattner384e5ef2005-12-18 13:33:06 +0000479 // Store remaining ArgRegs to the stack if this is a varargs function.
480 if (F.getFunctionType()->isVarArg()) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000481 // Remember the vararg offset for the va_start implementation.
482 VarArgsFrameOffset = ArgOffset;
483
Chris Lattner384e5ef2005-12-18 13:33:06 +0000484 for (; CurArgReg != ArgRegEnd; ++CurArgReg) {
485 unsigned VReg = RegMap->createVirtualRegister(&V8::IntRegsRegClass);
486 MF.addLiveIn(*CurArgReg, VReg);
487 SDOperand Arg = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
488
489 int FrameIdx = MF.getFrameInfo()->CreateFixedObject(4, ArgOffset);
490 SDOperand FIPtr = DAG.getFrameIndex(FrameIdx, MVT::i32);
491
492 OutChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(),
493 Arg, FIPtr, DAG.getSrcValue(0)));
494 ArgOffset += 4;
495 }
496 }
497
498 if (!OutChains.empty())
499 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains));
Chris Lattnera01b7572005-12-17 08:03:24 +0000500
501 // Finally, inform the code generator which regs we return values in.
502 switch (getValueType(F.getReturnType())) {
503 default: assert(0 && "Unknown type!");
504 case MVT::isVoid: break;
505 case MVT::i1:
506 case MVT::i8:
507 case MVT::i16:
508 case MVT::i32:
509 MF.addLiveOut(V8::I0);
510 break;
511 case MVT::i64:
512 MF.addLiveOut(V8::I0);
513 MF.addLiveOut(V8::I1);
514 break;
515 case MVT::f32:
516 MF.addLiveOut(V8::F0);
517 break;
518 case MVT::f64:
519 MF.addLiveOut(V8::D0);
520 break;
521 }
522
523 return ArgValues;
Chris Lattner6c18b102005-12-17 07:47:01 +0000524}
525
526std::pair<SDOperand, SDOperand>
527SparcV8TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
528 bool isVarArg, unsigned CC,
529 bool isTailCall, SDOperand Callee,
530 ArgListTy &Args, SelectionDAG &DAG) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000531 MachineFunction &MF = DAG.getMachineFunction();
532 // Count the size of the outgoing arguments.
533 unsigned ArgsSize = 0;
534 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
535 switch (getValueType(Args[i].second)) {
536 default: assert(0 && "Unknown value type!");
537 case MVT::i1:
538 case MVT::i8:
539 case MVT::i16:
540 case MVT::i32:
541 case MVT::f32:
542 ArgsSize += 4;
543 break;
544 case MVT::i64:
545 case MVT::f64:
546 ArgsSize += 8;
547 break;
548 }
549 }
550 if (ArgsSize > 4*6)
551 ArgsSize -= 4*6; // Space for first 6 arguments is prereserved.
552 else
553 ArgsSize = 0;
554
Chris Lattner6554bef2005-12-19 01:15:13 +0000555 // Keep stack frames 8-byte aligned.
556 ArgsSize = (ArgsSize+7) & ~7;
557
Chris Lattner2db3ff62005-12-18 15:55:15 +0000558 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
559 DAG.getConstant(ArgsSize, getPointerTy()));
560
561 SDOperand StackPtr, NullSV;
562 std::vector<SDOperand> Stores;
563 std::vector<SDOperand> RegValuesToPass;
564 unsigned ArgOffset = 68;
565 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
566 SDOperand Val = Args[i].first;
567 MVT::ValueType ObjectVT = Val.getValueType();
Chris Lattnercb833742006-01-06 17:56:38 +0000568 SDOperand ValToStore(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000569 unsigned ObjSize;
570 switch (ObjectVT) {
571 default: assert(0 && "Unhandled argument type!");
572 case MVT::i1:
573 case MVT::i8:
574 case MVT::i16:
575 // Promote the integer to 32-bits. If the input type is signed, use a
576 // sign extend, otherwise use a zero extend.
577 if (Args[i].second->isSigned())
578 Val = DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Val);
579 else
580 Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Val);
581 // FALL THROUGH
582 case MVT::i32:
583 ObjSize = 4;
584
585 if (RegValuesToPass.size() >= 6) {
586 ValToStore = Val;
587 } else {
588 RegValuesToPass.push_back(Val);
589 }
590 break;
591 case MVT::f32:
592 ObjSize = 4;
593 if (RegValuesToPass.size() >= 6) {
594 ValToStore = Val;
595 } else {
596 // Convert this to a FP value in an int reg.
Chris Lattnera01874f2005-12-23 02:31:39 +0000597 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000598 RegValuesToPass.push_back(Val);
599 }
600 break;
Chris Lattnera01874f2005-12-23 02:31:39 +0000601 case MVT::f64:
Chris Lattner2db3ff62005-12-18 15:55:15 +0000602 ObjSize = 8;
603 // If we can store this directly into the outgoing slot, do so. We can
604 // do this when all ArgRegs are used and if the outgoing slot is aligned.
Chris Lattner7f9975a2006-01-15 19:15:46 +0000605 // FIXME: McGill/misr fails with this.
606 if (0 && RegValuesToPass.size() >= 6 && ((ArgOffset-68) & 7) == 0) {
Chris Lattner2db3ff62005-12-18 15:55:15 +0000607 ValToStore = Val;
608 break;
609 }
610
611 // Otherwise, convert this to a FP value in int regs.
Chris Lattnera01874f2005-12-23 02:31:39 +0000612 Val = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Val);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000613 // FALL THROUGH
614 case MVT::i64:
615 ObjSize = 8;
616 if (RegValuesToPass.size() >= 6) {
617 ValToStore = Val; // Whole thing is passed in memory.
618 break;
619 }
620
621 // Split the value into top and bottom part. Top part goes in a reg.
622 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
623 DAG.getConstant(1, MVT::i32));
624 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Val,
625 DAG.getConstant(0, MVT::i32));
626 RegValuesToPass.push_back(Hi);
627
628 if (RegValuesToPass.size() >= 6) {
629 ValToStore = Lo;
Chris Lattner7c423b42005-12-19 07:57:53 +0000630 ArgOffset += 4;
631 ObjSize = 4;
Chris Lattner2db3ff62005-12-18 15:55:15 +0000632 } else {
633 RegValuesToPass.push_back(Lo);
634 }
635 break;
636 }
637
638 if (ValToStore.Val) {
639 if (!StackPtr.Val) {
Chris Lattner7c423b42005-12-19 07:57:53 +0000640 StackPtr = DAG.getRegister(V8::O6, MVT::i32);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000641 NullSV = DAG.getSrcValue(NULL);
642 }
643 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
644 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
645 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
646 ValToStore, PtrOff, NullSV));
647 }
648 ArgOffset += ObjSize;
649 }
650
651 // Emit all stores, make sure the occur before any copies into physregs.
652 if (!Stores.empty())
653 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
654
655 static const unsigned ArgRegs[] = {
656 V8::O0, V8::O1, V8::O2, V8::O3, V8::O4, V8::O5
657 };
658
659 // Build a sequence of copy-to-reg nodes chained together with token chain
660 // and flag operands which copy the outgoing args into O[0-5].
661 SDOperand InFlag;
662 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
663 Chain = DAG.getCopyToReg(Chain, ArgRegs[i], RegValuesToPass[i], InFlag);
664 InFlag = Chain.getValue(1);
665 }
666
Chris Lattner2db3ff62005-12-18 15:55:15 +0000667 // If the callee is a GlobalAddress node (quite common, every direct call is)
668 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
669 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
670 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i32);
671
672 std::vector<MVT::ValueType> NodeTys;
673 NodeTys.push_back(MVT::Other); // Returns a chain
674 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Chris Lattner44ea7b12006-01-27 23:30:03 +0000675 std::vector<SDOperand> Ops;
676 Ops.push_back(Chain);
677 Ops.push_back(Callee);
Chris Lattnerb4d899e2005-12-18 22:57:47 +0000678 if (InFlag.Val)
Chris Lattner44ea7b12006-01-27 23:30:03 +0000679 Ops.push_back(InFlag);
680 Chain = DAG.getNode(V8ISD::CALL, NodeTys, Ops);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000681 InFlag = Chain.getValue(1);
682
683 MVT::ValueType RetTyVT = getValueType(RetTy);
684 SDOperand RetVal;
685 if (RetTyVT != MVT::isVoid) {
686 switch (RetTyVT) {
687 default: assert(0 && "Unknown value type to return!");
688 case MVT::i1:
689 case MVT::i8:
690 case MVT::i16:
691 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
692 Chain = RetVal.getValue(1);
693
694 // Add a note to keep track of whether it is sign or zero extended.
695 RetVal = DAG.getNode(RetTy->isSigned() ? ISD::AssertSext :ISD::AssertZext,
696 MVT::i32, RetVal, DAG.getValueType(RetTyVT));
697 RetVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, RetVal);
698 break;
699 case MVT::i32:
700 RetVal = DAG.getCopyFromReg(Chain, V8::O0, MVT::i32, InFlag);
701 Chain = RetVal.getValue(1);
702 break;
703 case MVT::f32:
704 RetVal = DAG.getCopyFromReg(Chain, V8::F0, MVT::f32, InFlag);
705 Chain = RetVal.getValue(1);
706 break;
707 case MVT::f64:
708 RetVal = DAG.getCopyFromReg(Chain, V8::D0, MVT::f64, InFlag);
709 Chain = RetVal.getValue(1);
710 break;
711 case MVT::i64:
Chris Lattnereb096662005-12-19 02:15:51 +0000712 SDOperand Lo = DAG.getCopyFromReg(Chain, V8::O1, MVT::i32, InFlag);
Chris Lattner2db3ff62005-12-18 15:55:15 +0000713 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), V8::O0, MVT::i32,
714 Lo.getValue(2));
715 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
716 Chain = Hi.getValue(1);
717 break;
718 }
719 }
720
721 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
722 DAG.getConstant(ArgsSize, getPointerTy()));
723
Chris Lattner2db3ff62005-12-18 15:55:15 +0000724 return std::make_pair(RetVal, Chain);
Chris Lattner6c18b102005-12-17 07:47:01 +0000725}
726
Chris Lattner4d55aca2005-12-18 01:20:35 +0000727std::pair<SDOperand, SDOperand> SparcV8TargetLowering::
728LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
729 SelectionDAG &DAG) {
Chris Lattner6c18b102005-12-17 07:47:01 +0000730 assert(0 && "Unimp");
731 abort();
732}
733
Chris Lattner4d55aca2005-12-18 01:20:35 +0000734SDOperand SparcV8TargetLowering::
735LowerOperation(SDOperand Op, SelectionDAG &DAG) {
736 switch (Op.getOpcode()) {
737 default: assert(0 && "Should not custom lower this!");
Chris Lattnere3572462005-12-18 02:10:39 +0000738 case ISD::GlobalAddress: {
739 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
740 SDOperand GA = DAG.getTargetGlobalAddress(GV, MVT::i32);
741 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, GA);
742 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, GA);
743 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
744 }
Chris Lattner76acc872005-12-18 02:37:35 +0000745 case ISD::ConstantPool: {
746 Constant *C = cast<ConstantPoolSDNode>(Op)->get();
747 SDOperand CP = DAG.getTargetConstantPool(C, MVT::i32);
748 SDOperand Hi = DAG.getNode(V8ISD::Hi, MVT::i32, CP);
749 SDOperand Lo = DAG.getNode(V8ISD::Lo, MVT::i32, CP);
750 return DAG.getNode(ISD::ADD, MVT::i32, Lo, Hi);
751 }
Chris Lattner3cb71872005-12-23 05:00:16 +0000752 case ISD::FP_TO_SINT:
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000753 // Convert the fp value to integer in an FP register.
Chris Lattner3cb71872005-12-23 05:00:16 +0000754 assert(Op.getValueType() == MVT::i32);
755 Op = DAG.getNode(V8ISD::FTOI, MVT::f32, Op.getOperand(0));
756 return DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000757 case ISD::SINT_TO_FP: {
Chris Lattner3cb71872005-12-23 05:00:16 +0000758 assert(Op.getOperand(0).getValueType() == MVT::i32);
Chris Lattner3fbb7262006-01-11 07:27:40 +0000759 SDOperand Tmp = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Op.getOperand(0));
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000760 // Convert the int value to FP in an FP register.
Chris Lattner3fbb7262006-01-11 07:27:40 +0000761 return DAG.getNode(V8ISD::ITOF, Op.getValueType(), Tmp);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000762 }
Chris Lattner33084492005-12-18 08:13:54 +0000763 case ISD::BR_CC: {
764 SDOperand Chain = Op.getOperand(0);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000765 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
Chris Lattner33084492005-12-18 08:13:54 +0000766 SDOperand LHS = Op.getOperand(2);
767 SDOperand RHS = Op.getOperand(3);
768 SDOperand Dest = Op.getOperand(4);
769
770 // Get the condition flag.
771 if (LHS.getValueType() == MVT::i32) {
Chris Lattnerb9169ce2006-01-11 07:49:38 +0000772 std::vector<MVT::ValueType> VTs;
773 VTs.push_back(MVT::i32);
774 VTs.push_back(MVT::Flag);
775 std::vector<SDOperand> Ops;
776 Ops.push_back(LHS);
777 Ops.push_back(RHS);
Chris Lattner138d3222006-01-12 07:38:04 +0000778 SDOperand Cond = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000779 SDOperand CCN = DAG.getConstant(IntCondCCodeToICC(CC), MVT::i32);
780 return DAG.getNode(V8ISD::BRICC, MVT::Other, Chain, Dest, CCN, Cond);
Chris Lattner33084492005-12-18 08:13:54 +0000781 } else {
Chris Lattner4bb91022006-01-12 17:05:32 +0000782 SDOperand Cond = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000783 SDOperand CCN = DAG.getConstant(FPCondCCodeToFCC(CC), MVT::i32);
784 return DAG.getNode(V8ISD::BRFCC, MVT::Other, Chain, Dest, CCN, Cond);
Chris Lattner33084492005-12-18 08:13:54 +0000785 }
786 }
787 case ISD::SELECT_CC: {
788 SDOperand LHS = Op.getOperand(0);
789 SDOperand RHS = Op.getOperand(1);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000790 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Chris Lattner33084492005-12-18 08:13:54 +0000791 SDOperand TrueVal = Op.getOperand(2);
792 SDOperand FalseVal = Op.getOperand(3);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000793 unsigned Opc, V8CC = ~0U;
794
Chris Lattnerdea95282006-01-30 04:34:44 +0000795 // If this is a select_cc of a "setcc", and if the setcc got lowered into
796 // an CMP[IF]CC/SELECT_[IF]CC pair, find the original compared values.
797 if (isa<ConstantSDNode>(RHS) && cast<ConstantSDNode>(RHS)->getValue() == 0&&
798 CC == ISD::SETNE &&
799 ((LHS.getOpcode() == V8ISD::SELECT_ICC &&
800 LHS.getOperand(3).getOpcode() == V8ISD::CMPICC) ||
801 (LHS.getOpcode() == V8ISD::SELECT_FCC &&
802 LHS.getOperand(3).getOpcode() == V8ISD::CMPFCC)) &&
803 isa<ConstantSDNode>(LHS.getOperand(0)) &&
804 isa<ConstantSDNode>(LHS.getOperand(1)) &&
805 cast<ConstantSDNode>(LHS.getOperand(0))->getValue() == 1 &&
806 cast<ConstantSDNode>(LHS.getOperand(1))->getValue() == 0) {
807 SDOperand CMPCC = LHS.getOperand(3);
Chris Lattner3772bcb2006-01-30 07:43:04 +0000808 V8CC = cast<ConstantSDNode>(LHS.getOperand(2))->getValue();
Chris Lattnerdea95282006-01-30 04:34:44 +0000809 LHS = CMPCC.getOperand(0);
810 RHS = CMPCC.getOperand(1);
811 }
812
Chris Lattner4bb91022006-01-12 17:05:32 +0000813 SDOperand CompareFlag;
Chris Lattner4bb91022006-01-12 17:05:32 +0000814 if (LHS.getValueType() == MVT::i32) {
815 std::vector<MVT::ValueType> VTs;
816 VTs.push_back(LHS.getValueType()); // subcc returns a value
817 VTs.push_back(MVT::Flag);
818 std::vector<SDOperand> Ops;
819 Ops.push_back(LHS);
820 Ops.push_back(RHS);
821 CompareFlag = DAG.getNode(V8ISD::CMPICC, VTs, Ops).getValue(1);
822 Opc = V8ISD::SELECT_ICC;
Chris Lattner3772bcb2006-01-30 07:43:04 +0000823 if (V8CC == ~0U) V8CC = IntCondCCodeToICC(CC);
Chris Lattner4bb91022006-01-12 17:05:32 +0000824 } else {
825 CompareFlag = DAG.getNode(V8ISD::CMPFCC, MVT::Flag, LHS, RHS);
826 Opc = V8ISD::SELECT_FCC;
Chris Lattner3772bcb2006-01-30 07:43:04 +0000827 if (V8CC == ~0U) V8CC = FPCondCCodeToFCC(CC);
Chris Lattner4bb91022006-01-12 17:05:32 +0000828 }
Chris Lattner33084492005-12-18 08:13:54 +0000829 return DAG.getNode(Opc, TrueVal.getValueType(), TrueVal, FalseVal,
Chris Lattner3772bcb2006-01-30 07:43:04 +0000830 DAG.getConstant(V8CC, MVT::i32), CompareFlag);
Chris Lattner33084492005-12-18 08:13:54 +0000831 }
Nate Begemanacc398c2006-01-25 18:21:52 +0000832 case ISD::VASTART: {
833 // vastart just stores the address of the VarArgsFrameIndex slot into the
834 // memory location argument.
835 SDOperand Offset = DAG.getNode(ISD::ADD, MVT::i32,
836 DAG.getRegister(V8::I6, MVT::i32),
837 DAG.getConstant(VarArgsFrameOffset, MVT::i32));
838 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), Offset,
839 Op.getOperand(1), Op.getOperand(2));
840 }
Nate Begemanee625572006-01-27 21:09:22 +0000841 case ISD::RET: {
842 SDOperand Copy;
843
844 switch(Op.getNumOperands()) {
845 default:
846 assert(0 && "Do not know how to return this many arguments!");
847 abort();
848 case 1:
849 return SDOperand(); // ret void is legal
850 case 2: {
851 unsigned ArgReg;
852 switch(Op.getOperand(1).getValueType()) {
853 default: assert(0 && "Unknown type to return!");
854 case MVT::i32: ArgReg = V8::I0; break;
855 case MVT::f32: ArgReg = V8::F0; break;
856 case MVT::f64: ArgReg = V8::D0; break;
857 }
858 Copy = DAG.getCopyToReg(Op.getOperand(0), ArgReg, Op.getOperand(1),
859 SDOperand());
860 break;
861 }
862 case 3:
863 Copy = DAG.getCopyToReg(Op.getOperand(0), V8::I0, Op.getOperand(2),
864 SDOperand());
865 Copy = DAG.getCopyToReg(Copy, V8::I1, Op.getOperand(1), Copy.getValue(1));
866 break;
867 }
868 return DAG.getNode(V8ISD::RET_FLAG, MVT::Other, Copy, Copy.getValue(1));
869 }
Chris Lattnerbce88872006-01-15 08:43:57 +0000870 }
Chris Lattner4d55aca2005-12-18 01:20:35 +0000871}
872
Chris Lattner33084492005-12-18 08:13:54 +0000873MachineBasicBlock *
874SparcV8TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
875 MachineBasicBlock *BB) {
876 unsigned BROpcode;
877 // Figure out the conditional branch opcode to use for this select_cc.
878 switch (MI->getOpcode()) {
879 default: assert(0 && "Unknown SELECT_CC!");
880 case V8::SELECT_CC_Int_ICC:
881 case V8::SELECT_CC_FP_ICC:
882 case V8::SELECT_CC_DFP_ICC:
Chris Lattner33084492005-12-18 08:13:54 +0000883 case V8::SELECT_CC_Int_FCC:
884 case V8::SELECT_CC_FP_FCC:
885 case V8::SELECT_CC_DFP_FCC:
Chris Lattner3772bcb2006-01-30 07:43:04 +0000886 V8CC::CondCodes CC = (V8CC::CondCodes)MI->getOperand(3).getImmedValue();
887 BROpcode = SPARCCondCodeToBranchInstr(CC);
Chris Lattner33084492005-12-18 08:13:54 +0000888 break;
889 }
890
891 // To "insert" a SELECT_CC instruction, we actually have to insert the diamond
892 // control-flow pattern. The incoming instruction knows the destination vreg
893 // to set, the condition code register to branch on, the true/false values to
894 // select between, and a branch opcode to use.
895 const BasicBlock *LLVM_BB = BB->getBasicBlock();
896 ilist<MachineBasicBlock>::iterator It = BB;
897 ++It;
898
899 // thisMBB:
900 // ...
901 // TrueVal = ...
902 // [f]bCC copy1MBB
903 // fallthrough --> copy0MBB
904 MachineBasicBlock *thisMBB = BB;
905 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
906 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
907 BuildMI(BB, BROpcode, 1).addMBB(sinkMBB);
908 MachineFunction *F = BB->getParent();
909 F->getBasicBlockList().insert(It, copy0MBB);
910 F->getBasicBlockList().insert(It, sinkMBB);
911 // Update machine-CFG edges
912 BB->addSuccessor(copy0MBB);
913 BB->addSuccessor(sinkMBB);
914
915 // copy0MBB:
916 // %FalseValue = ...
917 // # fallthrough to sinkMBB
918 BB = copy0MBB;
919
920 // Update machine-CFG edges
921 BB->addSuccessor(sinkMBB);
922
923 // sinkMBB:
924 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
925 // ...
926 BB = sinkMBB;
927 BuildMI(BB, V8::PHI, 4, MI->getOperand(0).getReg())
928 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
929 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
930
931 delete MI; // The pseudo instruction is gone now.
932 return BB;
933}
934
Chris Lattner6c18b102005-12-17 07:47:01 +0000935//===----------------------------------------------------------------------===//
936// Instruction Selector Implementation
937//===----------------------------------------------------------------------===//
938
939//===--------------------------------------------------------------------===//
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000940/// SparcV8DAGToDAGISel - SPARC specific code to select Sparc V8 machine
Chris Lattner6c18b102005-12-17 07:47:01 +0000941/// instructions for SelectionDAG operations.
942///
943namespace {
944class SparcV8DAGToDAGISel : public SelectionDAGISel {
945 SparcV8TargetLowering V8Lowering;
Chris Lattner76afdc92006-01-30 05:35:57 +0000946
947 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
948 /// make the right decision when generating code for different targets.
949 const SparcV8Subtarget &Subtarget;
Chris Lattner6c18b102005-12-17 07:47:01 +0000950public:
951 SparcV8DAGToDAGISel(TargetMachine &TM)
Chris Lattner76afdc92006-01-30 05:35:57 +0000952 : SelectionDAGISel(V8Lowering), V8Lowering(TM),
953 Subtarget(TM.getSubtarget<SparcV8Subtarget>()) {
954 }
Chris Lattner6c18b102005-12-17 07:47:01 +0000955
956 SDOperand Select(SDOperand Op);
957
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000958 // Complex Pattern Selectors.
959 bool SelectADDRrr(SDOperand N, SDOperand &R1, SDOperand &R2);
960 bool SelectADDRri(SDOperand N, SDOperand &Base, SDOperand &Offset);
961
Chris Lattner6c18b102005-12-17 07:47:01 +0000962 /// InstructionSelectBasicBlock - This callback is invoked by
963 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
964 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
965
966 virtual const char *getPassName() const {
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000967 return "SparcV8 DAG->DAG Pattern Instruction Selection";
Chris Lattner6c18b102005-12-17 07:47:01 +0000968 }
969
970 // Include the pieces autogenerated from the target description.
971#include "SparcV8GenDAGISel.inc"
972};
973} // end anonymous namespace
974
975/// InstructionSelectBasicBlock - This callback is invoked by
976/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
977void SparcV8DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
978 DEBUG(BB->dump());
979
980 // Select target instructions for the DAG.
981 DAG.setRoot(Select(DAG.getRoot()));
982 CodeGenMap.clear();
983 DAG.RemoveDeadNodes();
984
985 // Emit machine code to BB.
986 ScheduleAndEmitDAG(DAG);
987}
988
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000989bool SparcV8DAGToDAGISel::SelectADDRri(SDOperand Addr, SDOperand &Base,
990 SDOperand &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +0000991 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
992 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000993 Offset = CurDAG->getTargetConstant(0, MVT::i32);
994 return true;
995 }
996
997 if (Addr.getOpcode() == ISD::ADD) {
998 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
999 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +00001000 if (FrameIndexSDNode *FIN =
1001 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001002 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +00001003 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001004 } else {
1005 Base = Select(Addr.getOperand(0));
1006 }
1007 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
1008 return true;
1009 }
1010 }
1011 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo) {
1012 Base = Select(Addr.getOperand(1));
1013 Offset = Addr.getOperand(0).getOperand(0);
1014 return true;
1015 }
1016 if (Addr.getOperand(1).getOpcode() == V8ISD::Lo) {
1017 Base = Select(Addr.getOperand(0));
1018 Offset = Addr.getOperand(1).getOperand(0);
1019 return true;
1020 }
1021 }
1022 Base = Select(Addr);
1023 Offset = CurDAG->getTargetConstant(0, MVT::i32);
1024 return true;
1025}
1026
Chris Lattner9034b882005-12-17 21:25:27 +00001027bool SparcV8DAGToDAGISel::SelectADDRrr(SDOperand Addr, SDOperand &R1,
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001028 SDOperand &R2) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001029 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Chris Lattner9034b882005-12-17 21:25:27 +00001030 if (Addr.getOpcode() == ISD::ADD) {
1031 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
1032 Predicate_simm13(Addr.getOperand(1).Val))
1033 return false; // Let the reg+imm pattern catch this!
Chris Lattnere1389ad2005-12-18 02:27:00 +00001034 if (Addr.getOperand(0).getOpcode() == V8ISD::Lo ||
1035 Addr.getOperand(1).getOpcode() == V8ISD::Lo)
1036 return false; // Let the reg+imm pattern catch this!
Chris Lattnere3572462005-12-18 02:10:39 +00001037 R1 = Select(Addr.getOperand(0));
1038 R2 = Select(Addr.getOperand(1));
Chris Lattner9034b882005-12-17 21:25:27 +00001039 return true;
1040 }
1041
1042 R1 = Select(Addr);
Chris Lattnerbc83fd92005-12-17 20:04:49 +00001043 R2 = CurDAG->getRegister(V8::G0, MVT::i32);
1044 return true;
1045}
1046
Chris Lattner6c18b102005-12-17 07:47:01 +00001047SDOperand SparcV8DAGToDAGISel::Select(SDOperand Op) {
1048 SDNode *N = Op.Val;
Chris Lattner4d55aca2005-12-18 01:20:35 +00001049 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
1050 N->getOpcode() < V8ISD::FIRST_NUMBER)
Chris Lattner6c18b102005-12-17 07:47:01 +00001051 return Op; // Already selected.
1052 // If this has already been converted, use it.
1053 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
1054 if (CGMI != CodeGenMap.end()) return CGMI->second;
1055
1056 switch (N->getOpcode()) {
1057 default: break;
Chris Lattner8fa54dc2005-12-18 06:59:57 +00001058 case ISD::FrameIndex: {
1059 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1060 if (N->hasOneUse())
1061 return CurDAG->SelectNodeTo(N, V8::ADDri, MVT::i32,
1062 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1063 CurDAG->getTargetConstant(0, MVT::i32));
1064 return CodeGenMap[Op] =
1065 CurDAG->getTargetNode(V8::ADDri, MVT::i32,
1066 CurDAG->getTargetFrameIndex(FI, MVT::i32),
1067 CurDAG->getTargetConstant(0, MVT::i32));
1068 }
Chris Lattnerd19fc652005-12-17 22:55:57 +00001069 case ISD::ADD_PARTS: {
1070 SDOperand LHSL = Select(N->getOperand(0));
1071 SDOperand LHSH = Select(N->getOperand(1));
1072 SDOperand RHSL = Select(N->getOperand(2));
1073 SDOperand RHSH = Select(N->getOperand(3));
1074 // FIXME, handle immediate RHS.
1075 SDOperand Low = CurDAG->getTargetNode(V8::ADDCCrr, MVT::i32, MVT::Flag,
1076 LHSL, RHSL);
1077 SDOperand Hi = CurDAG->getTargetNode(V8::ADDXrr, MVT::i32, LHSH, RHSH,
1078 Low.getValue(1));
1079 CodeGenMap[SDOperand(N, 0)] = Low;
1080 CodeGenMap[SDOperand(N, 1)] = Hi;
1081 return Op.ResNo ? Hi : Low;
1082 }
1083 case ISD::SUB_PARTS: {
1084 SDOperand LHSL = Select(N->getOperand(0));
1085 SDOperand LHSH = Select(N->getOperand(1));
1086 SDOperand RHSL = Select(N->getOperand(2));
1087 SDOperand RHSH = Select(N->getOperand(3));
1088 // FIXME, handle immediate RHS.
1089 SDOperand Low = CurDAG->getTargetNode(V8::SUBCCrr, MVT::i32, MVT::Flag,
1090 LHSL, RHSL);
1091 SDOperand Hi = CurDAG->getTargetNode(V8::SUBXrr, MVT::i32, LHSH, RHSH,
1092 Low.getValue(1));
1093 CodeGenMap[SDOperand(N, 0)] = Low;
1094 CodeGenMap[SDOperand(N, 1)] = Hi;
1095 return Op.ResNo ? Hi : Low;
1096 }
Chris Lattner7087e572005-12-17 22:39:19 +00001097 case ISD::SDIV:
1098 case ISD::UDIV: {
1099 // FIXME: should use a custom expander to expose the SRA to the dag.
1100 SDOperand DivLHS = Select(N->getOperand(0));
1101 SDOperand DivRHS = Select(N->getOperand(1));
1102
1103 // Set the Y register to the high-part.
1104 SDOperand TopPart;
1105 if (N->getOpcode() == ISD::SDIV) {
1106 TopPart = CurDAG->getTargetNode(V8::SRAri, MVT::i32, DivLHS,
1107 CurDAG->getTargetConstant(31, MVT::i32));
1108 } else {
1109 TopPart = CurDAG->getRegister(V8::G0, MVT::i32);
1110 }
1111 TopPart = CurDAG->getTargetNode(V8::WRYrr, MVT::Flag, TopPart,
1112 CurDAG->getRegister(V8::G0, MVT::i32));
1113
1114 // FIXME: Handle div by immediate.
1115 unsigned Opcode = N->getOpcode() == ISD::SDIV ? V8::SDIVrr : V8::UDIVrr;
1116 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, TopPart);
1117 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001118 case ISD::MULHU:
1119 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +00001120 // FIXME: Handle mul by immediate.
Chris Lattneree3d5fb2005-12-17 22:30:00 +00001121 SDOperand MulLHS = Select(N->getOperand(0));
1122 SDOperand MulRHS = Select(N->getOperand(1));
1123 unsigned Opcode = N->getOpcode() == ISD::MULHU ? V8::UMULrr : V8::SMULrr;
1124 SDOperand Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
1125 MulLHS, MulRHS);
1126 // The high part is in the Y register.
1127 return CurDAG->SelectNodeTo(N, V8::RDY, MVT::i32, Mul.getValue(1));
1128 }
Chris Lattner44ea7b12006-01-27 23:30:03 +00001129 case V8ISD::CALL:
Chris Lattner2db3ff62005-12-18 15:55:15 +00001130 // FIXME: This is a workaround for a bug in tblgen.
1131 { // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
1132 // Emits: (CALL:void (tglobaladdr:i32):$dst)
1133 // Pattern complexity = 2 cost = 1
1134 SDOperand N1 = N->getOperand(1);
Chris Lattner311f8c22005-12-18 23:07:11 +00001135 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
1136 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
Chris Lattnerb4d899e2005-12-18 22:57:47 +00001137 SDOperand InFlag = SDOperand(0, 0);
Chris Lattner2db3ff62005-12-18 15:55:15 +00001138 SDOperand Chain = N->getOperand(0);
1139 SDOperand Tmp0 = N1;
1140 Chain = Select(Chain);
Chris Lattnerb4d899e2005-12-18 22:57:47 +00001141 SDOperand Result;
1142 if (N->getNumOperands() == 3) {
1143 InFlag = Select(N->getOperand(2));
1144 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1145 Chain, InFlag);
1146 } else {
1147 Result = CurDAG->getTargetNode(V8::CALL, MVT::Other, MVT::Flag, Tmp0,
1148 Chain);
1149 }
Chris Lattner2db3ff62005-12-18 15:55:15 +00001150 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
1151 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
1152 return Result.getValue(Op.ResNo);
1153 }
1154 P47Fail:;
1155
Chris Lattner6c18b102005-12-17 07:47:01 +00001156 }
1157
1158 return SelectCode(Op);
1159}
1160
1161
Chris Lattner4dcfaac2006-01-26 07:22:22 +00001162/// createSparcV8ISelDag - This pass converts a legalized DAG into a
1163/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +00001164///
1165FunctionPass *llvm::createSparcV8ISelDag(TargetMachine &TM) {
1166 return new SparcV8DAGToDAGISel(TM);
1167}