blob: c96049d440fa4fa9f3b1589f765b720a59b4b7bf [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
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 the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86ISelLowering.h"
17#include "X86TargetMachine.h"
18#include "llvm/CallingConv.h"
19#include "llvm/Function.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SSARegMap.h"
24#include "llvm/Target/TargetOptions.h"
25using namespace llvm;
26
27// FIXME: temporary.
28#include "llvm/Support/CommandLine.h"
29static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
30 cl::desc("Enable fastcc on X86"));
31
32X86TargetLowering::X86TargetLowering(TargetMachine &TM)
33 : TargetLowering(TM) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000034 // Set up the TargetLowering object.
35
36 // X86 is weird, it always uses i8 for shift amounts and setcc results.
37 setShiftAmountType(MVT::i8);
38 setSetCCResultType(MVT::i8);
39 setSetCCResultContents(ZeroOrOneSetCCResult);
40 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
41
42 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043 addRegisterClass(MVT::i8, X86::R8RegisterClass);
44 addRegisterClass(MVT::i16, X86::R16RegisterClass);
45 addRegisterClass(MVT::i32, X86::R32RegisterClass);
46
47 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
48 // operation.
49 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
50 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
51 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
52 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
53
54 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
55 // this operation.
56 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
57 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
58
59 if (!X86ScalarSSE) {
60 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
61 // isn't legal.
62 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
63 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
64 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
65 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
66 }
67
68 // Handle FP_TO_UINT by promoting the destination to a larger signed
69 // conversion.
70 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
71 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
72 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
73
74 if (!X86ScalarSSE)
75 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
76
77 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
78 // this operation.
79 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
80 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
81 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
82
Chris Lattner21f66852005-12-23 05:15:23 +000083 setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
84 setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
85
Evan Cheng898101c2005-12-19 23:12:38 +000086 if (X86DAGIsel) {
87 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
88 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000089 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
90 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
91 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
92 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +000093 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
95 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
96 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
97 setOperationAction(ISD::FREM , MVT::f64 , Expand);
98 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
99 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
100 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
104 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
105 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
106 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000107 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000108
109 setOperationAction(ISD::READIO , MVT::i1 , Expand);
110 setOperationAction(ISD::READIO , MVT::i8 , Expand);
111 setOperationAction(ISD::READIO , MVT::i16 , Expand);
112 setOperationAction(ISD::READIO , MVT::i32 , Expand);
113 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
114 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
115 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
116 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
117
118 // These should be promoted to a larger select which is supported.
119 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
120 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Evan Cheng7df96d62005-12-17 01:21:05 +0000121 // X86 wants to expand cmov itself.
122 if (X86DAGIsel) {
123 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
124 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
Evan Chengd5781fc2005-12-21 20:21:51 +0000125 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
126 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
127 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
Evan Cheng3a03ebb2005-12-21 23:05:39 +0000128 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng7df96d62005-12-17 01:21:05 +0000129 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130
Chris Lattnerf73bae12005-11-29 06:16:21 +0000131 // We don't have line number support yet.
132 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000133 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
134 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000135
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000136 if (X86ScalarSSE) {
137 // Set up the FP register classes.
138 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
139 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
140
141 // SSE has no load+extend ops
142 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
143 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
144
145 // SSE has no i16 to fp conversion, only i32
146 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
147 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
148
149 // Expand FP_TO_UINT into a select.
150 // FIXME: We would like to use a Custom expander here eventually to do
151 // the optimal thing for SSE vs. the default expansion in the legalizer.
152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
153
154 // We don't support sin/cos/sqrt/fmod
155 setOperationAction(ISD::FSIN , MVT::f64, Expand);
156 setOperationAction(ISD::FCOS , MVT::f64, Expand);
157 setOperationAction(ISD::FABS , MVT::f64, Expand);
158 setOperationAction(ISD::FNEG , MVT::f64, Expand);
159 setOperationAction(ISD::FREM , MVT::f64, Expand);
160 setOperationAction(ISD::FSIN , MVT::f32, Expand);
161 setOperationAction(ISD::FCOS , MVT::f32, Expand);
162 setOperationAction(ISD::FABS , MVT::f32, Expand);
163 setOperationAction(ISD::FNEG , MVT::f32, Expand);
164 setOperationAction(ISD::FREM , MVT::f32, Expand);
165
166 addLegalFPImmediate(+0.0); // xorps / xorpd
167 } else {
168 // Set up the FP register classes.
169 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
170
171 if (!UnsafeFPMath) {
172 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
173 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
174 }
175
176 addLegalFPImmediate(+0.0); // FLD0
177 addLegalFPImmediate(+1.0); // FLD1
178 addLegalFPImmediate(-0.0); // FLD0/FCHS
179 addLegalFPImmediate(-1.0); // FLD1/FCHS
180 }
181 computeRegisterProperties();
182
183 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
184 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
185 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
186 allowUnalignedMemoryAccesses = true; // x86 supports it!
187}
188
189std::vector<SDOperand>
190X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
191 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
192 return LowerFastCCArguments(F, DAG);
193 return LowerCCCArguments(F, DAG);
194}
195
196std::pair<SDOperand, SDOperand>
197X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
198 bool isVarArg, unsigned CallingConv,
199 bool isTailCall,
200 SDOperand Callee, ArgListTy &Args,
201 SelectionDAG &DAG) {
202 assert((!isVarArg || CallingConv == CallingConv::C) &&
203 "Only C takes varargs!");
204 if (CallingConv == CallingConv::Fast && EnableFastCC)
205 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
206 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
207}
208
Evan Chengb077b842005-12-21 02:39:21 +0000209SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
210 SelectionDAG &DAG) {
211 if (!X86DAGIsel)
212 return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
213
214 SDOperand Copy;
215 MVT::ValueType OpVT = Op.getValueType();
216 switch (OpVT) {
217 default: assert(0 && "Unknown type to return!");
218 case MVT::i32:
219 Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
220 break;
221 case MVT::i64: {
222 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
223 DAG.getConstant(1, MVT::i32));
224 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
225 DAG.getConstant(0, MVT::i32));
226 Copy = DAG.getCopyToReg(Chain, X86::EAX, Hi, SDOperand());
227 Copy = DAG.getCopyToReg(Copy, X86::EDX, Lo, Copy.getValue(1));
228 break;
229 }
230 case MVT::f32:
Evan Chengb077b842005-12-21 02:39:21 +0000231 case MVT::f64:
232 if (!X86ScalarSSE) {
233 std::vector<MVT::ValueType> Tys;
234 Tys.push_back(MVT::Other);
235 Tys.push_back(MVT::Flag);
236 std::vector<SDOperand> Ops;
237 Ops.push_back(Chain);
Evan Cheng38bcbaf2005-12-23 07:31:11 +0000238 if (OpVT == MVT::f32)
239 Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
Evan Chengb077b842005-12-21 02:39:21 +0000240 Ops.push_back(Op);
241 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
242 } else {
243 // Spill the value to memory and reload it into top of stack.
244 unsigned Size = MVT::getSizeInBits(OpVT)/8;
245 MachineFunction &MF = DAG.getMachineFunction();
246 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
247 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
248 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
249 StackSlot, DAG.getSrcValue(NULL));
250 std::vector<MVT::ValueType> Tys;
251 Tys.push_back(MVT::f64);
252 Tys.push_back(MVT::Other);
253 std::vector<SDOperand> Ops;
254 Ops.push_back(Chain);
255 Ops.push_back(StackSlot);
256 Ops.push_back(DAG.getValueType(OpVT));
257 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
258 Tys.clear();
259 Tys.push_back(MVT::Other);
260 Tys.push_back(MVT::Flag);
261 Ops.clear();
262 Ops.push_back(Copy.getValue(1));
263 Ops.push_back(Copy);
264 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
265 }
266 break;
267 }
Evan Chengd5781fc2005-12-21 20:21:51 +0000268
269 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
270 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
271 Copy.getValue(1));
Evan Chengb077b842005-12-21 02:39:21 +0000272}
273
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000274//===----------------------------------------------------------------------===//
275// C Calling Convention implementation
276//===----------------------------------------------------------------------===//
277
278std::vector<SDOperand>
279X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
280 std::vector<SDOperand> ArgValues;
281
282 MachineFunction &MF = DAG.getMachineFunction();
283 MachineFrameInfo *MFI = MF.getFrameInfo();
284
285 // Add DAG nodes to load the arguments... On entry to a function on the X86,
286 // the stack frame looks like this:
287 //
288 // [ESP] -- return address
289 // [ESP + 4] -- first argument (leftmost lexically)
290 // [ESP + 8] -- second argument, if first argument is four bytes in size
291 // ...
292 //
293 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
294 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
295 MVT::ValueType ObjectVT = getValueType(I->getType());
296 unsigned ArgIncrement = 4;
297 unsigned ObjSize;
298 switch (ObjectVT) {
299 default: assert(0 && "Unhandled argument type!");
300 case MVT::i1:
301 case MVT::i8: ObjSize = 1; break;
302 case MVT::i16: ObjSize = 2; break;
303 case MVT::i32: ObjSize = 4; break;
304 case MVT::i64: ObjSize = ArgIncrement = 8; break;
305 case MVT::f32: ObjSize = 4; break;
306 case MVT::f64: ObjSize = ArgIncrement = 8; break;
307 }
308 // Create the frame index object for this incoming parameter...
309 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
310
311 // Create the SelectionDAG nodes corresponding to a load from this parameter
312 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
313
314 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
315 // dead loads.
316 SDOperand ArgValue;
317 if (!I->use_empty())
318 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
319 DAG.getSrcValue(NULL));
320 else {
321 if (MVT::isInteger(ObjectVT))
322 ArgValue = DAG.getConstant(0, ObjectVT);
323 else
324 ArgValue = DAG.getConstantFP(0, ObjectVT);
325 }
326 ArgValues.push_back(ArgValue);
327
328 ArgOffset += ArgIncrement; // Move on to the next argument...
329 }
330
331 // If the function takes variable number of arguments, make a frame index for
332 // the start of the first vararg value... for expansion of llvm.va_start.
333 if (F.isVarArg())
334 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
335 ReturnAddrIndex = 0; // No return address slot generated yet.
336 BytesToPopOnReturn = 0; // Callee pops nothing.
337 BytesCallerReserves = ArgOffset;
338
339 // Finally, inform the code generator which regs we return values in.
340 switch (getValueType(F.getReturnType())) {
341 default: assert(0 && "Unknown type!");
342 case MVT::isVoid: break;
343 case MVT::i1:
344 case MVT::i8:
345 case MVT::i16:
346 case MVT::i32:
347 MF.addLiveOut(X86::EAX);
348 break;
349 case MVT::i64:
350 MF.addLiveOut(X86::EAX);
351 MF.addLiveOut(X86::EDX);
352 break;
353 case MVT::f32:
354 case MVT::f64:
355 MF.addLiveOut(X86::ST0);
356 break;
357 }
358 return ArgValues;
359}
360
361std::pair<SDOperand, SDOperand>
362X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
363 bool isVarArg, bool isTailCall,
364 SDOperand Callee, ArgListTy &Args,
365 SelectionDAG &DAG) {
366 // Count how many bytes are to be pushed on the stack.
367 unsigned NumBytes = 0;
368
369 if (Args.empty()) {
370 // Save zero bytes.
371 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
372 DAG.getConstant(0, getPointerTy()));
373 } else {
374 for (unsigned i = 0, e = Args.size(); i != e; ++i)
375 switch (getValueType(Args[i].second)) {
376 default: assert(0 && "Unknown value type!");
377 case MVT::i1:
378 case MVT::i8:
379 case MVT::i16:
380 case MVT::i32:
381 case MVT::f32:
382 NumBytes += 4;
383 break;
384 case MVT::i64:
385 case MVT::f64:
386 NumBytes += 8;
387 break;
388 }
389
390 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
391 DAG.getConstant(NumBytes, getPointerTy()));
392
393 // Arguments go on the stack in reverse order, as specified by the ABI.
394 unsigned ArgOffset = 0;
395 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
396 X86::ESP, MVT::i32);
397 std::vector<SDOperand> Stores;
398
399 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
400 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
401 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
402
403 switch (getValueType(Args[i].second)) {
404 default: assert(0 && "Unexpected ValueType for argument!");
405 case MVT::i1:
406 case MVT::i8:
407 case MVT::i16:
408 // Promote the integer to 32 bits. If the input type is signed use a
409 // sign extend, otherwise use a zero extend.
410 if (Args[i].second->isSigned())
411 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
412 else
413 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
414
415 // FALL THROUGH
416 case MVT::i32:
417 case MVT::f32:
418 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
419 Args[i].first, PtrOff,
420 DAG.getSrcValue(NULL)));
421 ArgOffset += 4;
422 break;
423 case MVT::i64:
424 case MVT::f64:
425 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
426 Args[i].first, PtrOff,
427 DAG.getSrcValue(NULL)));
428 ArgOffset += 8;
429 break;
430 }
431 }
432 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
433 }
434
435 std::vector<MVT::ValueType> RetVals;
436 MVT::ValueType RetTyVT = getValueType(RetTy);
437 RetVals.push_back(MVT::Other);
438
439 // The result values produced have to be legal. Promote the result.
440 switch (RetTyVT) {
441 case MVT::isVoid: break;
442 default:
443 RetVals.push_back(RetTyVT);
444 break;
445 case MVT::i1:
446 case MVT::i8:
447 case MVT::i16:
448 RetVals.push_back(MVT::i32);
449 break;
450 case MVT::f32:
451 if (X86ScalarSSE)
452 RetVals.push_back(MVT::f32);
453 else
454 RetVals.push_back(MVT::f64);
455 break;
456 case MVT::i64:
457 RetVals.push_back(MVT::i32);
458 RetVals.push_back(MVT::i32);
459 break;
460 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
Evan Chengd90eb7f2006-01-05 00:27:02 +0000462 if (X86DAGIsel) {
463 std::vector<MVT::ValueType> NodeTys;
464 NodeTys.push_back(MVT::Other); // Returns a chain
465 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
466
467 std::vector<SDOperand> Ops;
468 Ops.push_back(Chain);
469 Ops.push_back(Callee);
470
471 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
472 NodeTys, Ops);
473 SDOperand InFlag = Chain.getValue(1);
474
475 SDOperand RetVal;
476 if (RetTyVT != MVT::isVoid) {
477 switch (RetTyVT) {
478 default: assert(0 && "Unknown value type to return!");
479 case MVT::i1:
480 case MVT::i8:
481 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
482 Chain = RetVal.getValue(1);
483 break;
484 case MVT::i16:
485 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
486 Chain = RetVal.getValue(1);
487 break;
488 case MVT::i32:
489 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
490 Chain = RetVal.getValue(1);
491 break;
492 case MVT::i64: {
493 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
494 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
495 Lo.getValue(2));
496 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
497 Chain = Hi.getValue(1);
498 break;
499 }
500 case MVT::f32:
501 case MVT::f64: {
502 std::vector<MVT::ValueType> Tys;
503 Tys.push_back(MVT::f64);
504 Tys.push_back(MVT::Other);
505 std::vector<SDOperand> Ops;
506 Ops.push_back(Chain);
507 Ops.push_back(InFlag);
508 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
509 Chain = RetVal.getValue(1);
510 if (X86ScalarSSE) {
511 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
512 MachineFunction &MF = DAG.getMachineFunction();
513 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
514 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
515 Tys.clear();
516 Tys.push_back(MVT::Other);
517 Ops.clear();
518 Ops.push_back(Chain);
519 Ops.push_back(RetVal);
520 Ops.push_back(StackSlot);
521 Ops.push_back(DAG.getValueType(RetTyVT));
522 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
523 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
524 DAG.getSrcValue(NULL));
525 Chain = RetVal.getValue(1);
526 } else if (RetTyVT == MVT::f32)
527 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
528 break;
529 }
530 }
531 }
532
533 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
534 DAG.getConstant(NumBytes, getPointerTy()),
535 DAG.getConstant(0, getPointerTy()));
536 return std::make_pair(RetVal, Chain);
537 } else {
538 std::vector<SDOperand> Ops;
539 Ops.push_back(Chain);
540 Ops.push_back(Callee);
541 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
542 Ops.push_back(DAG.getConstant(0, getPointerTy()));
543
544 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
545 RetVals, Ops);
546
547 SDOperand ResultVal;
548 switch (RetTyVT) {
549 case MVT::isVoid: break;
550 default:
551 ResultVal = TheCall.getValue(1);
552 break;
553 case MVT::i1:
554 case MVT::i8:
555 case MVT::i16:
556 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
557 break;
558 case MVT::f32:
559 // FIXME: we would really like to remember that this FP_ROUND operation is
560 // okay to eliminate if we allow excess FP precision.
561 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
562 break;
563 case MVT::i64:
564 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
565 TheCall.getValue(2));
566 break;
567 }
568
569 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
570 return std::make_pair(ResultVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000571 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000572}
573
574SDOperand
575X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
576 Value *VAListV, SelectionDAG &DAG) {
577 // vastart just stores the address of the VarArgsFrameIndex slot.
578 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
579 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
580 DAG.getSrcValue(VAListV));
581}
582
583
584std::pair<SDOperand,SDOperand>
585X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
586 Value *VAListV, const Type *ArgTy,
587 SelectionDAG &DAG) {
588 MVT::ValueType ArgVT = getValueType(ArgTy);
589 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
590 VAListP, DAG.getSrcValue(VAListV));
591 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
592 DAG.getSrcValue(NULL));
593 unsigned Amt;
594 if (ArgVT == MVT::i32)
595 Amt = 4;
596 else {
597 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
598 "Other types should have been promoted for varargs!");
599 Amt = 8;
600 }
601 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
602 DAG.getConstant(Amt, Val.getValueType()));
603 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
604 Val, VAListP, DAG.getSrcValue(VAListV));
605 return std::make_pair(Result, Chain);
606}
607
608//===----------------------------------------------------------------------===//
609// Fast Calling Convention implementation
610//===----------------------------------------------------------------------===//
611//
612// The X86 'fast' calling convention passes up to two integer arguments in
613// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
614// and requires that the callee pop its arguments off the stack (allowing proper
615// tail calls), and has the same return value conventions as C calling convs.
616//
617// This calling convention always arranges for the callee pop value to be 8n+4
618// bytes, which is needed for tail recursion elimination and stack alignment
619// reasons.
620//
621// Note that this can be enhanced in the future to pass fp vals in registers
622// (when we have a global fp allocator) and do other tricks.
623//
624
625/// AddLiveIn - This helper function adds the specified physical register to the
626/// MachineFunction as a live in value. It also creates a corresponding virtual
627/// register for it.
628static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
629 TargetRegisterClass *RC) {
630 assert(RC->contains(PReg) && "Not the correct regclass!");
631 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
632 MF.addLiveIn(PReg, VReg);
633 return VReg;
634}
635
636
637std::vector<SDOperand>
638X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
639 std::vector<SDOperand> ArgValues;
640
641 MachineFunction &MF = DAG.getMachineFunction();
642 MachineFrameInfo *MFI = MF.getFrameInfo();
643
644 // Add DAG nodes to load the arguments... On entry to a function the stack
645 // frame looks like this:
646 //
647 // [ESP] -- return address
648 // [ESP + 4] -- first nonreg argument (leftmost lexically)
649 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
650 // ...
651 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
652
653 // Keep track of the number of integer regs passed so far. This can be either
654 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
655 // used).
656 unsigned NumIntRegs = 0;
657
658 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
659 MVT::ValueType ObjectVT = getValueType(I->getType());
660 unsigned ArgIncrement = 4;
661 unsigned ObjSize = 0;
662 SDOperand ArgValue;
663
664 switch (ObjectVT) {
665 default: assert(0 && "Unhandled argument type!");
666 case MVT::i1:
667 case MVT::i8:
668 if (NumIntRegs < 2) {
669 if (!I->use_empty()) {
670 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
671 X86::R8RegisterClass);
672 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
673 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000674 if (ObjectVT == MVT::i1)
675 // FIXME: Should insert a assertzext here.
676 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000677 }
678 ++NumIntRegs;
679 break;
680 }
681
682 ObjSize = 1;
683 break;
684 case MVT::i16:
685 if (NumIntRegs < 2) {
686 if (!I->use_empty()) {
687 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
688 X86::R16RegisterClass);
689 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
690 DAG.setRoot(ArgValue.getValue(1));
691 }
692 ++NumIntRegs;
693 break;
694 }
695 ObjSize = 2;
696 break;
697 case MVT::i32:
698 if (NumIntRegs < 2) {
699 if (!I->use_empty()) {
700 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
701 X86::R32RegisterClass);
702 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
703 DAG.setRoot(ArgValue.getValue(1));
704 }
705 ++NumIntRegs;
706 break;
707 }
708 ObjSize = 4;
709 break;
710 case MVT::i64:
711 if (NumIntRegs == 0) {
712 if (!I->use_empty()) {
713 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
714 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
715
716 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
717 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
718 DAG.setRoot(Hi.getValue(1));
719
720 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
721 }
722 NumIntRegs = 2;
723 break;
724 } else if (NumIntRegs == 1) {
725 if (!I->use_empty()) {
726 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
727 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
728 DAG.setRoot(Low.getValue(1));
729
730 // Load the high part from memory.
731 // Create the frame index object for this incoming parameter...
732 int FI = MFI->CreateFixedObject(4, ArgOffset);
733 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
734 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
735 DAG.getSrcValue(NULL));
736 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
737 }
738 ArgOffset += 4;
739 NumIntRegs = 2;
740 break;
741 }
742 ObjSize = ArgIncrement = 8;
743 break;
744 case MVT::f32: ObjSize = 4; break;
745 case MVT::f64: ObjSize = ArgIncrement = 8; break;
746 }
747
748 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
749 // dead loads.
750 if (ObjSize && !I->use_empty()) {
751 // Create the frame index object for this incoming parameter...
752 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
753
754 // Create the SelectionDAG nodes corresponding to a load from this
755 // parameter.
756 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
757
758 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
759 DAG.getSrcValue(NULL));
760 } else if (ArgValue.Val == 0) {
761 if (MVT::isInteger(ObjectVT))
762 ArgValue = DAG.getConstant(0, ObjectVT);
763 else
764 ArgValue = DAG.getConstantFP(0, ObjectVT);
765 }
766 ArgValues.push_back(ArgValue);
767
768 if (ObjSize)
769 ArgOffset += ArgIncrement; // Move on to the next argument.
770 }
771
772 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
773 // arguments and the arguments after the retaddr has been pushed are aligned.
774 if ((ArgOffset & 7) == 0)
775 ArgOffset += 4;
776
777 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
778 ReturnAddrIndex = 0; // No return address slot generated yet.
779 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
780 BytesCallerReserves = 0;
781
782 // Finally, inform the code generator which regs we return values in.
783 switch (getValueType(F.getReturnType())) {
784 default: assert(0 && "Unknown type!");
785 case MVT::isVoid: break;
786 case MVT::i1:
787 case MVT::i8:
788 case MVT::i16:
789 case MVT::i32:
790 MF.addLiveOut(X86::EAX);
791 break;
792 case MVT::i64:
793 MF.addLiveOut(X86::EAX);
794 MF.addLiveOut(X86::EDX);
795 break;
796 case MVT::f32:
797 case MVT::f64:
798 MF.addLiveOut(X86::ST0);
799 break;
800 }
801 return ArgValues;
802}
803
804std::pair<SDOperand, SDOperand>
805X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
806 bool isTailCall, SDOperand Callee,
807 ArgListTy &Args, SelectionDAG &DAG) {
808 // Count how many bytes are to be pushed on the stack.
809 unsigned NumBytes = 0;
810
811 // Keep track of the number of integer regs passed so far. This can be either
812 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
813 // used).
814 unsigned NumIntRegs = 0;
815
816 for (unsigned i = 0, e = Args.size(); i != e; ++i)
817 switch (getValueType(Args[i].second)) {
818 default: assert(0 && "Unknown value type!");
819 case MVT::i1:
820 case MVT::i8:
821 case MVT::i16:
822 case MVT::i32:
823 if (NumIntRegs < 2) {
824 ++NumIntRegs;
825 break;
826 }
827 // fall through
828 case MVT::f32:
829 NumBytes += 4;
830 break;
831 case MVT::i64:
832 if (NumIntRegs == 0) {
833 NumIntRegs = 2;
834 break;
835 } else if (NumIntRegs == 1) {
836 NumIntRegs = 2;
837 NumBytes += 4;
838 break;
839 }
840
841 // fall through
842 case MVT::f64:
843 NumBytes += 8;
844 break;
845 }
846
847 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
848 // arguments and the arguments after the retaddr has been pushed are aligned.
849 if ((NumBytes & 7) == 0)
850 NumBytes += 4;
851
852 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
853 DAG.getConstant(NumBytes, getPointerTy()));
854
855 // Arguments go on the stack in reverse order, as specified by the ABI.
856 unsigned ArgOffset = 0;
857 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
858 X86::ESP, MVT::i32);
859 NumIntRegs = 0;
860 std::vector<SDOperand> Stores;
861 std::vector<SDOperand> RegValuesToPass;
862 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
863 switch (getValueType(Args[i].second)) {
864 default: assert(0 && "Unexpected ValueType for argument!");
865 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000866 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
867 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000868 case MVT::i8:
869 case MVT::i16:
870 case MVT::i32:
871 if (NumIntRegs < 2) {
872 RegValuesToPass.push_back(Args[i].first);
873 ++NumIntRegs;
874 break;
875 }
876 // Fall through
877 case MVT::f32: {
878 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
879 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
880 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
881 Args[i].first, PtrOff,
882 DAG.getSrcValue(NULL)));
883 ArgOffset += 4;
884 break;
885 }
886 case MVT::i64:
887 if (NumIntRegs < 2) { // Can pass part of it in regs?
888 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
889 Args[i].first, DAG.getConstant(1, MVT::i32));
890 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
891 Args[i].first, DAG.getConstant(0, MVT::i32));
892 RegValuesToPass.push_back(Lo);
893 ++NumIntRegs;
894 if (NumIntRegs < 2) { // Pass both parts in regs?
895 RegValuesToPass.push_back(Hi);
896 ++NumIntRegs;
897 } else {
898 // Pass the high part in memory.
899 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
900 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
901 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
902 Hi, PtrOff, DAG.getSrcValue(NULL)));
903 ArgOffset += 4;
904 }
905 break;
906 }
907 // Fall through
908 case MVT::f64:
909 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
910 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
911 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
912 Args[i].first, PtrOff,
913 DAG.getSrcValue(NULL)));
914 ArgOffset += 8;
915 break;
916 }
917 }
918 if (!Stores.empty())
919 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
920
921 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
922 // arguments and the arguments after the retaddr has been pushed are aligned.
923 if ((ArgOffset & 7) == 0)
924 ArgOffset += 4;
925
926 std::vector<MVT::ValueType> RetVals;
927 MVT::ValueType RetTyVT = getValueType(RetTy);
928
929 RetVals.push_back(MVT::Other);
930
931 // The result values produced have to be legal. Promote the result.
932 switch (RetTyVT) {
933 case MVT::isVoid: break;
934 default:
935 RetVals.push_back(RetTyVT);
936 break;
937 case MVT::i1:
938 case MVT::i8:
939 case MVT::i16:
940 RetVals.push_back(MVT::i32);
941 break;
942 case MVT::f32:
943 if (X86ScalarSSE)
944 RetVals.push_back(MVT::f32);
945 else
946 RetVals.push_back(MVT::f64);
947 break;
948 case MVT::i64:
949 RetVals.push_back(MVT::i32);
950 RetVals.push_back(MVT::i32);
951 break;
952 }
953
954 std::vector<SDOperand> Ops;
955 Ops.push_back(Chain);
956 Ops.push_back(Callee);
957 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
958 // Callee pops all arg values on the stack.
959 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
960
961 // Pass register arguments as needed.
962 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
963
964 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
965 RetVals, Ops);
966 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
967
968 SDOperand ResultVal;
969 switch (RetTyVT) {
970 case MVT::isVoid: break;
971 default:
972 ResultVal = TheCall.getValue(1);
973 break;
974 case MVT::i1:
975 case MVT::i8:
976 case MVT::i16:
977 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
978 break;
979 case MVT::f32:
980 // FIXME: we would really like to remember that this FP_ROUND operation is
981 // okay to eliminate if we allow excess FP precision.
982 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
983 break;
984 case MVT::i64:
985 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
986 TheCall.getValue(2));
987 break;
988 }
989
990 return std::make_pair(ResultVal, Chain);
991}
992
993SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
994 if (ReturnAddrIndex == 0) {
995 // Set up a frame object for the return address.
996 MachineFunction &MF = DAG.getMachineFunction();
997 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
998 }
999
1000 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1001}
1002
1003
1004
1005std::pair<SDOperand, SDOperand> X86TargetLowering::
1006LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1007 SelectionDAG &DAG) {
1008 SDOperand Result;
1009 if (Depth) // Depths > 0 not supported yet!
1010 Result = DAG.getConstant(0, getPointerTy());
1011 else {
1012 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1013 if (!isFrameAddress)
1014 // Just load the return address
1015 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1016 DAG.getSrcValue(NULL));
1017 else
1018 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1019 DAG.getConstant(4, MVT::i32));
1020 }
1021 return std::make_pair(Result, Chain);
1022}
1023
1024//===----------------------------------------------------------------------===//
1025// X86 Custom Lowering Hooks
1026//===----------------------------------------------------------------------===//
1027
1028/// LowerOperation - Provide custom lowering hooks for some operations.
1029///
1030SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1031 switch (Op.getOpcode()) {
1032 default: assert(0 && "Should not custom lower this!");
1033 case ISD::SINT_TO_FP: {
1034 assert(Op.getValueType() == MVT::f64 &&
1035 Op.getOperand(0).getValueType() == MVT::i64 &&
1036 "Unknown SINT_TO_FP to lower!");
1037 // We lower sint64->FP into a store to a temporary stack slot, followed by a
1038 // FILD64m node.
1039 MachineFunction &MF = DAG.getMachineFunction();
1040 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1041 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1042 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1043 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1044 std::vector<MVT::ValueType> RTs;
1045 RTs.push_back(MVT::f64);
1046 RTs.push_back(MVT::Other);
1047 std::vector<SDOperand> Ops;
1048 Ops.push_back(Store);
1049 Ops.push_back(StackSlot);
1050 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1051 }
1052 case ISD::FP_TO_SINT: {
1053 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1054 Op.getOperand(0).getValueType() == MVT::f64 &&
1055 "Unknown FP_TO_SINT to lower!");
1056 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1057 // stack slot.
1058 MachineFunction &MF = DAG.getMachineFunction();
1059 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1060 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1061 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1062
1063 unsigned Opc;
1064 switch (Op.getValueType()) {
1065 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1066 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1067 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1068 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1069 }
1070
1071 // Build the FP_TO_INT*_IN_MEM
1072 std::vector<SDOperand> Ops;
1073 Ops.push_back(DAG.getEntryNode());
1074 Ops.push_back(Op.getOperand(0));
1075 Ops.push_back(StackSlot);
1076 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1077
1078 // Load the result.
1079 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1080 DAG.getSrcValue(NULL));
1081 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001082 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001083 std::vector<MVT::ValueType> Tys;
1084 Tys.push_back(MVT::Other);
1085 Tys.push_back(MVT::Flag);
1086 std::vector<SDOperand> Ops;
1087 Ops.push_back(Op.getOperand(0));
1088 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001089 Ops.clear();
1090 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1091 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1092 MVT::i32, Ops[0].getValue(2)));
1093 Ops.push_back(Ops[1].getValue(1));
1094 Tys[0] = Tys[1] = MVT::i32;
1095 Tys.push_back(MVT::Other);
1096 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001097 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001098 case ISD::SETCC: {
1099 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1100 SDOperand CC = Op.getOperand(2);
1101 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1102 Op.getOperand(0), Op.getOperand(1));
1103 return DAG.getNode(X86ISD::SETCC, MVT::i8, CC, Cond);
1104 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001105 case ISD::SELECT: {
Evan Cheng7df96d62005-12-17 01:21:05 +00001106 SDOperand Cond = Op.getOperand(0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001107 SDOperand CC;
Evan Chengd5781fc2005-12-21 20:21:51 +00001108 if (Cond.getOpcode() == X86ISD::SETCC) {
1109 CC = Cond.getOperand(0);
1110 Cond = Cond.getOperand(1);
1111 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng7df96d62005-12-17 01:21:05 +00001112 CC = Cond.getOperand(2);
1113 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1114 Cond.getOperand(0), Cond.getOperand(1));
1115 } else {
1116 CC = DAG.getCondCode(ISD::SETEQ);
1117 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1118 }
1119 return DAG.getNode(X86ISD::CMOV, Op.getValueType(),
1120 Op.getOperand(1), Op.getOperand(2), CC, Cond);
1121 }
Evan Cheng898101c2005-12-19 23:12:38 +00001122 case ISD::BRCOND: {
Evan Cheng898101c2005-12-19 23:12:38 +00001123 SDOperand Cond = Op.getOperand(1);
1124 SDOperand Dest = Op.getOperand(2);
1125 SDOperand CC;
1126 // TODO: handle Cond == OR / AND / XOR
Evan Chengd5781fc2005-12-21 20:21:51 +00001127 if (Cond.getOpcode() == X86ISD::SETCC) {
1128 CC = Cond.getOperand(0);
1129 Cond = Cond.getOperand(1);
1130 } else if (Cond.getOpcode() == ISD::SETCC) {
Evan Cheng898101c2005-12-19 23:12:38 +00001131 CC = Cond.getOperand(2);
1132 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1133 Cond.getOperand(0), Cond.getOperand(1));
1134 } else {
1135 CC = DAG.getCondCode(ISD::SETNE);
1136 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1137 }
1138 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1139 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1140 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001141 case ISD::GlobalAddress: {
Evan Chengb077b842005-12-21 02:39:21 +00001142 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001143 SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Chengb077b842005-12-21 02:39:21 +00001144 // For Darwin, external and weak symbols are indirect, so we want to load
1145 // the value at address GV, not the value of GV itself. This means that
1146 // the GlobalAddress must be in the base or index register of the address,
1147 // not the GV offset field.
1148 if (getTargetMachine().
1149 getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1150 (GV->hasWeakLinkage() || GV->isExternal()))
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001151 return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1152 GVOp, DAG.getSrcValue(NULL));
Evan Chengb077b842005-12-21 02:39:21 +00001153 else
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001154 return GVOp;
Evan Chengb077b842005-12-21 02:39:21 +00001155 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001156 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001157 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001158}
Evan Cheng72261582005-12-20 06:22:03 +00001159
1160const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1161 switch (Opcode) {
1162 default: return NULL;
1163 case X86ISD::FILD64m: return "X86ISD::FILD64m";
1164 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1165 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1166 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001167 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001168 case X86ISD::FST: return "X86ISD::FST";
1169 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001170 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001171 case X86ISD::CALL: return "X86ISD::CALL";
1172 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1173 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1174 case X86ISD::CMP: return "X86ISD::CMP";
1175 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001176 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001177 case X86ISD::CMOV: return "X86ISD::CMOV";
1178 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001179 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00001180 }
1181}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00001182
1183bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1184 uint64_t Mask) const {
1185
1186 unsigned Opc = Op.getOpcode();
1187
1188 switch (Opc) {
1189 default:
1190 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1191 break;
1192 case X86ISD::SETCC: return (Mask & 1) == 0;
1193 }
1194
1195 return false;
1196}