blob: e33bf956860c338c12043697b61ddac9c390d7c5 [file] [log] [blame]
Chris Lattner76ac0682005-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) {
34
35 // Set up the TargetLowering object.
36
37 // X86 is weird, it always uses i8 for shift amounts and setcc results.
38 setShiftAmountType(MVT::i8);
39 setSetCCResultType(MVT::i8);
40 setSetCCResultContents(ZeroOrOneSetCCResult);
41 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
42
43 // Set up the register classes.
44 // FIXME: Eliminate these two classes when legalize can handle promotions
45 // well.
46 addRegisterClass(MVT::i1, X86::R8RegisterClass);
47 addRegisterClass(MVT::i8, X86::R8RegisterClass);
48 addRegisterClass(MVT::i16, X86::R16RegisterClass);
49 addRegisterClass(MVT::i32, X86::R32RegisterClass);
50
51 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
52 // operation.
53 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
54 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
55 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
56 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
57
58 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
59 // this operation.
60 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
61 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
62
63 if (!X86ScalarSSE) {
64 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
65 // isn't legal.
66 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
67 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
68 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
69 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
70 }
71
72 // Handle FP_TO_UINT by promoting the destination to a larger signed
73 // conversion.
74 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
75 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
76 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
77
78 if (!X86ScalarSSE)
79 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
80
81 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
82 // this operation.
83 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
85 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
86
87 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
88 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
89 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
90 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
91 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
92 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
93 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
94 setOperationAction(ISD::FREM , MVT::f64 , Expand);
95 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
96 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
97 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
98 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
99 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
100 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
104
105 setOperationAction(ISD::READIO , MVT::i1 , Expand);
106 setOperationAction(ISD::READIO , MVT::i8 , Expand);
107 setOperationAction(ISD::READIO , MVT::i16 , Expand);
108 setOperationAction(ISD::READIO , MVT::i32 , Expand);
109 setOperationAction(ISD::WRITEIO , MVT::i1 , Expand);
110 setOperationAction(ISD::WRITEIO , MVT::i8 , Expand);
111 setOperationAction(ISD::WRITEIO , MVT::i16 , Expand);
112 setOperationAction(ISD::WRITEIO , MVT::i32 , Expand);
113
114 // These should be promoted to a larger select which is supported.
115 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
116 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
117
118 if (X86ScalarSSE) {
119 // Set up the FP register classes.
120 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
121 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
122
123 // SSE has no load+extend ops
124 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
125 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
126
127 // SSE has no i16 to fp conversion, only i32
128 setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
129 setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
130
131 // Expand FP_TO_UINT into a select.
132 // FIXME: We would like to use a Custom expander here eventually to do
133 // the optimal thing for SSE vs. the default expansion in the legalizer.
134 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
135
136 // We don't support sin/cos/sqrt/fmod
137 setOperationAction(ISD::FSIN , MVT::f64, Expand);
138 setOperationAction(ISD::FCOS , MVT::f64, Expand);
139 setOperationAction(ISD::FABS , MVT::f64, Expand);
140 setOperationAction(ISD::FNEG , MVT::f64, Expand);
141 setOperationAction(ISD::FREM , MVT::f64, Expand);
142 setOperationAction(ISD::FSIN , MVT::f32, Expand);
143 setOperationAction(ISD::FCOS , MVT::f32, Expand);
144 setOperationAction(ISD::FABS , MVT::f32, Expand);
145 setOperationAction(ISD::FNEG , MVT::f32, Expand);
146 setOperationAction(ISD::FREM , MVT::f32, Expand);
147
148 addLegalFPImmediate(+0.0); // xorps / xorpd
149 } else {
150 // Set up the FP register classes.
151 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
152
153 if (!UnsafeFPMath) {
154 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
155 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
156 }
157
158 addLegalFPImmediate(+0.0); // FLD0
159 addLegalFPImmediate(+1.0); // FLD1
160 addLegalFPImmediate(-0.0); // FLD0/FCHS
161 addLegalFPImmediate(-1.0); // FLD1/FCHS
162 }
163 computeRegisterProperties();
164
165 maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
166 maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
167 maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
168 allowUnalignedMemoryAccesses = true; // x86 supports it!
169}
170
171std::vector<SDOperand>
172X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
173 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
174 return LowerFastCCArguments(F, DAG);
175 return LowerCCCArguments(F, DAG);
176}
177
178std::pair<SDOperand, SDOperand>
179X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
180 bool isVarArg, unsigned CallingConv,
181 bool isTailCall,
182 SDOperand Callee, ArgListTy &Args,
183 SelectionDAG &DAG) {
184 assert((!isVarArg || CallingConv == CallingConv::C) &&
185 "Only C takes varargs!");
186 if (CallingConv == CallingConv::Fast && EnableFastCC)
187 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
188 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
189}
190
191//===----------------------------------------------------------------------===//
192// C Calling Convention implementation
193//===----------------------------------------------------------------------===//
194
195std::vector<SDOperand>
196X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
197 std::vector<SDOperand> ArgValues;
198
199 MachineFunction &MF = DAG.getMachineFunction();
200 MachineFrameInfo *MFI = MF.getFrameInfo();
201
202 // Add DAG nodes to load the arguments... On entry to a function on the X86,
203 // the stack frame looks like this:
204 //
205 // [ESP] -- return address
206 // [ESP + 4] -- first argument (leftmost lexically)
207 // [ESP + 8] -- second argument, if first argument is four bytes in size
208 // ...
209 //
210 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
211 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
212 MVT::ValueType ObjectVT = getValueType(I->getType());
213 unsigned ArgIncrement = 4;
214 unsigned ObjSize;
215 switch (ObjectVT) {
216 default: assert(0 && "Unhandled argument type!");
217 case MVT::i1:
218 case MVT::i8: ObjSize = 1; break;
219 case MVT::i16: ObjSize = 2; break;
220 case MVT::i32: ObjSize = 4; break;
221 case MVT::i64: ObjSize = ArgIncrement = 8; break;
222 case MVT::f32: ObjSize = 4; break;
223 case MVT::f64: ObjSize = ArgIncrement = 8; break;
224 }
225 // Create the frame index object for this incoming parameter...
226 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
227
228 // Create the SelectionDAG nodes corresponding to a load from this parameter
229 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
230
231 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
232 // dead loads.
233 SDOperand ArgValue;
234 if (!I->use_empty())
235 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
236 DAG.getSrcValue(NULL));
237 else {
238 if (MVT::isInteger(ObjectVT))
239 ArgValue = DAG.getConstant(0, ObjectVT);
240 else
241 ArgValue = DAG.getConstantFP(0, ObjectVT);
242 }
243 ArgValues.push_back(ArgValue);
244
245 ArgOffset += ArgIncrement; // Move on to the next argument...
246 }
247
248 // If the function takes variable number of arguments, make a frame index for
249 // the start of the first vararg value... for expansion of llvm.va_start.
250 if (F.isVarArg())
251 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
252 ReturnAddrIndex = 0; // No return address slot generated yet.
253 BytesToPopOnReturn = 0; // Callee pops nothing.
254 BytesCallerReserves = ArgOffset;
255
256 // Finally, inform the code generator which regs we return values in.
257 switch (getValueType(F.getReturnType())) {
258 default: assert(0 && "Unknown type!");
259 case MVT::isVoid: break;
260 case MVT::i1:
261 case MVT::i8:
262 case MVT::i16:
263 case MVT::i32:
264 MF.addLiveOut(X86::EAX);
265 break;
266 case MVT::i64:
267 MF.addLiveOut(X86::EAX);
268 MF.addLiveOut(X86::EDX);
269 break;
270 case MVT::f32:
271 case MVT::f64:
272 MF.addLiveOut(X86::ST0);
273 break;
274 }
275 return ArgValues;
276}
277
278std::pair<SDOperand, SDOperand>
279X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
280 bool isVarArg, bool isTailCall,
281 SDOperand Callee, ArgListTy &Args,
282 SelectionDAG &DAG) {
283 // Count how many bytes are to be pushed on the stack.
284 unsigned NumBytes = 0;
285
286 if (Args.empty()) {
287 // Save zero bytes.
288 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
289 DAG.getConstant(0, getPointerTy()));
290 } else {
291 for (unsigned i = 0, e = Args.size(); i != e; ++i)
292 switch (getValueType(Args[i].second)) {
293 default: assert(0 && "Unknown value type!");
294 case MVT::i1:
295 case MVT::i8:
296 case MVT::i16:
297 case MVT::i32:
298 case MVT::f32:
299 NumBytes += 4;
300 break;
301 case MVT::i64:
302 case MVT::f64:
303 NumBytes += 8;
304 break;
305 }
306
307 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
308 DAG.getConstant(NumBytes, getPointerTy()));
309
310 // Arguments go on the stack in reverse order, as specified by the ABI.
311 unsigned ArgOffset = 0;
312 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
313 X86::ESP, MVT::i32);
314 std::vector<SDOperand> Stores;
315
316 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
317 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
318 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
319
320 switch (getValueType(Args[i].second)) {
321 default: assert(0 && "Unexpected ValueType for argument!");
322 case MVT::i1:
323 case MVT::i8:
324 case MVT::i16:
325 // Promote the integer to 32 bits. If the input type is signed use a
326 // sign extend, otherwise use a zero extend.
327 if (Args[i].second->isSigned())
328 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
329 else
330 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
331
332 // FALL THROUGH
333 case MVT::i32:
334 case MVT::f32:
335 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
336 Args[i].first, PtrOff,
337 DAG.getSrcValue(NULL)));
338 ArgOffset += 4;
339 break;
340 case MVT::i64:
341 case MVT::f64:
342 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
343 Args[i].first, PtrOff,
344 DAG.getSrcValue(NULL)));
345 ArgOffset += 8;
346 break;
347 }
348 }
349 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
350 }
351
352 std::vector<MVT::ValueType> RetVals;
353 MVT::ValueType RetTyVT = getValueType(RetTy);
354 RetVals.push_back(MVT::Other);
355
356 // The result values produced have to be legal. Promote the result.
357 switch (RetTyVT) {
358 case MVT::isVoid: break;
359 default:
360 RetVals.push_back(RetTyVT);
361 break;
362 case MVT::i1:
363 case MVT::i8:
364 case MVT::i16:
365 RetVals.push_back(MVT::i32);
366 break;
367 case MVT::f32:
368 if (X86ScalarSSE)
369 RetVals.push_back(MVT::f32);
370 else
371 RetVals.push_back(MVT::f64);
372 break;
373 case MVT::i64:
374 RetVals.push_back(MVT::i32);
375 RetVals.push_back(MVT::i32);
376 break;
377 }
378 std::vector<SDOperand> Ops;
379 Ops.push_back(Chain);
380 Ops.push_back(Callee);
381 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
382 Ops.push_back(DAG.getConstant(0, getPointerTy()));
383 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
384 RetVals, Ops);
385 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
386
387 SDOperand ResultVal;
388 switch (RetTyVT) {
389 case MVT::isVoid: break;
390 default:
391 ResultVal = TheCall.getValue(1);
392 break;
393 case MVT::i1:
394 case MVT::i8:
395 case MVT::i16:
396 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
397 break;
398 case MVT::f32:
399 // FIXME: we would really like to remember that this FP_ROUND operation is
400 // okay to eliminate if we allow excess FP precision.
401 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
402 break;
403 case MVT::i64:
404 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
405 TheCall.getValue(2));
406 break;
407 }
408
409 return std::make_pair(ResultVal, Chain);
410}
411
412SDOperand
413X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
414 Value *VAListV, SelectionDAG &DAG) {
415 // vastart just stores the address of the VarArgsFrameIndex slot.
416 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
417 return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
418 DAG.getSrcValue(VAListV));
419}
420
421
422std::pair<SDOperand,SDOperand>
423X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
424 Value *VAListV, const Type *ArgTy,
425 SelectionDAG &DAG) {
426 MVT::ValueType ArgVT = getValueType(ArgTy);
427 SDOperand Val = DAG.getLoad(MVT::i32, Chain,
428 VAListP, DAG.getSrcValue(VAListV));
429 SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
430 DAG.getSrcValue(NULL));
431 unsigned Amt;
432 if (ArgVT == MVT::i32)
433 Amt = 4;
434 else {
435 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
436 "Other types should have been promoted for varargs!");
437 Amt = 8;
438 }
439 Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
440 DAG.getConstant(Amt, Val.getValueType()));
441 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
442 Val, VAListP, DAG.getSrcValue(VAListV));
443 return std::make_pair(Result, Chain);
444}
445
446//===----------------------------------------------------------------------===//
447// Fast Calling Convention implementation
448//===----------------------------------------------------------------------===//
449//
450// The X86 'fast' calling convention passes up to two integer arguments in
451// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
452// and requires that the callee pop its arguments off the stack (allowing proper
453// tail calls), and has the same return value conventions as C calling convs.
454//
455// This calling convention always arranges for the callee pop value to be 8n+4
456// bytes, which is needed for tail recursion elimination and stack alignment
457// reasons.
458//
459// Note that this can be enhanced in the future to pass fp vals in registers
460// (when we have a global fp allocator) and do other tricks.
461//
462
463/// AddLiveIn - This helper function adds the specified physical register to the
464/// MachineFunction as a live in value. It also creates a corresponding virtual
465/// register for it.
466static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
467 TargetRegisterClass *RC) {
468 assert(RC->contains(PReg) && "Not the correct regclass!");
469 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
470 MF.addLiveIn(PReg, VReg);
471 return VReg;
472}
473
474
475std::vector<SDOperand>
476X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
477 std::vector<SDOperand> ArgValues;
478
479 MachineFunction &MF = DAG.getMachineFunction();
480 MachineFrameInfo *MFI = MF.getFrameInfo();
481
482 // Add DAG nodes to load the arguments... On entry to a function the stack
483 // frame looks like this:
484 //
485 // [ESP] -- return address
486 // [ESP + 4] -- first nonreg argument (leftmost lexically)
487 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
488 // ...
489 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
490
491 // Keep track of the number of integer regs passed so far. This can be either
492 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
493 // used).
494 unsigned NumIntRegs = 0;
495
496 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
497 MVT::ValueType ObjectVT = getValueType(I->getType());
498 unsigned ArgIncrement = 4;
499 unsigned ObjSize = 0;
500 SDOperand ArgValue;
501
502 switch (ObjectVT) {
503 default: assert(0 && "Unhandled argument type!");
504 case MVT::i1:
505 case MVT::i8:
506 if (NumIntRegs < 2) {
507 if (!I->use_empty()) {
508 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
509 X86::R8RegisterClass);
510 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
511 DAG.setRoot(ArgValue.getValue(1));
512 }
513 ++NumIntRegs;
514 break;
515 }
516
517 ObjSize = 1;
518 break;
519 case MVT::i16:
520 if (NumIntRegs < 2) {
521 if (!I->use_empty()) {
522 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
523 X86::R16RegisterClass);
524 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
525 DAG.setRoot(ArgValue.getValue(1));
526 }
527 ++NumIntRegs;
528 break;
529 }
530 ObjSize = 2;
531 break;
532 case MVT::i32:
533 if (NumIntRegs < 2) {
534 if (!I->use_empty()) {
535 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
536 X86::R32RegisterClass);
537 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
538 DAG.setRoot(ArgValue.getValue(1));
539 }
540 ++NumIntRegs;
541 break;
542 }
543 ObjSize = 4;
544 break;
545 case MVT::i64:
546 if (NumIntRegs == 0) {
547 if (!I->use_empty()) {
548 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
549 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
550
551 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
552 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
553 DAG.setRoot(Hi.getValue(1));
554
555 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
556 }
557 NumIntRegs = 2;
558 break;
559 } else if (NumIntRegs == 1) {
560 if (!I->use_empty()) {
561 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
562 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
563 DAG.setRoot(Low.getValue(1));
564
565 // Load the high part from memory.
566 // Create the frame index object for this incoming parameter...
567 int FI = MFI->CreateFixedObject(4, ArgOffset);
568 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
569 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
570 DAG.getSrcValue(NULL));
571 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
572 }
573 ArgOffset += 4;
574 NumIntRegs = 2;
575 break;
576 }
577 ObjSize = ArgIncrement = 8;
578 break;
579 case MVT::f32: ObjSize = 4; break;
580 case MVT::f64: ObjSize = ArgIncrement = 8; break;
581 }
582
583 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
584 // dead loads.
585 if (ObjSize && !I->use_empty()) {
586 // Create the frame index object for this incoming parameter...
587 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
588
589 // Create the SelectionDAG nodes corresponding to a load from this
590 // parameter.
591 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
592
593 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
594 DAG.getSrcValue(NULL));
595 } else if (ArgValue.Val == 0) {
596 if (MVT::isInteger(ObjectVT))
597 ArgValue = DAG.getConstant(0, ObjectVT);
598 else
599 ArgValue = DAG.getConstantFP(0, ObjectVT);
600 }
601 ArgValues.push_back(ArgValue);
602
603 if (ObjSize)
604 ArgOffset += ArgIncrement; // Move on to the next argument.
605 }
606
607 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
608 // arguments and the arguments after the retaddr has been pushed are aligned.
609 if ((ArgOffset & 7) == 0)
610 ArgOffset += 4;
611
612 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
613 ReturnAddrIndex = 0; // No return address slot generated yet.
614 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
615 BytesCallerReserves = 0;
616
617 // Finally, inform the code generator which regs we return values in.
618 switch (getValueType(F.getReturnType())) {
619 default: assert(0 && "Unknown type!");
620 case MVT::isVoid: break;
621 case MVT::i1:
622 case MVT::i8:
623 case MVT::i16:
624 case MVT::i32:
625 MF.addLiveOut(X86::EAX);
626 break;
627 case MVT::i64:
628 MF.addLiveOut(X86::EAX);
629 MF.addLiveOut(X86::EDX);
630 break;
631 case MVT::f32:
632 case MVT::f64:
633 MF.addLiveOut(X86::ST0);
634 break;
635 }
636 return ArgValues;
637}
638
639std::pair<SDOperand, SDOperand>
640X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
641 bool isTailCall, SDOperand Callee,
642 ArgListTy &Args, SelectionDAG &DAG) {
643 // Count how many bytes are to be pushed on the stack.
644 unsigned NumBytes = 0;
645
646 // Keep track of the number of integer regs passed so far. This can be either
647 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
648 // used).
649 unsigned NumIntRegs = 0;
650
651 for (unsigned i = 0, e = Args.size(); i != e; ++i)
652 switch (getValueType(Args[i].second)) {
653 default: assert(0 && "Unknown value type!");
654 case MVT::i1:
655 case MVT::i8:
656 case MVT::i16:
657 case MVT::i32:
658 if (NumIntRegs < 2) {
659 ++NumIntRegs;
660 break;
661 }
662 // fall through
663 case MVT::f32:
664 NumBytes += 4;
665 break;
666 case MVT::i64:
667 if (NumIntRegs == 0) {
668 NumIntRegs = 2;
669 break;
670 } else if (NumIntRegs == 1) {
671 NumIntRegs = 2;
672 NumBytes += 4;
673 break;
674 }
675
676 // fall through
677 case MVT::f64:
678 NumBytes += 8;
679 break;
680 }
681
682 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
683 // arguments and the arguments after the retaddr has been pushed are aligned.
684 if ((NumBytes & 7) == 0)
685 NumBytes += 4;
686
687 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
688 DAG.getConstant(NumBytes, getPointerTy()));
689
690 // Arguments go on the stack in reverse order, as specified by the ABI.
691 unsigned ArgOffset = 0;
692 SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
693 X86::ESP, MVT::i32);
694 NumIntRegs = 0;
695 std::vector<SDOperand> Stores;
696 std::vector<SDOperand> RegValuesToPass;
697 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
698 switch (getValueType(Args[i].second)) {
699 default: assert(0 && "Unexpected ValueType for argument!");
700 case MVT::i1:
701 case MVT::i8:
702 case MVT::i16:
703 case MVT::i32:
704 if (NumIntRegs < 2) {
705 RegValuesToPass.push_back(Args[i].first);
706 ++NumIntRegs;
707 break;
708 }
709 // Fall through
710 case MVT::f32: {
711 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
712 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
713 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
714 Args[i].first, PtrOff,
715 DAG.getSrcValue(NULL)));
716 ArgOffset += 4;
717 break;
718 }
719 case MVT::i64:
720 if (NumIntRegs < 2) { // Can pass part of it in regs?
721 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
722 Args[i].first, DAG.getConstant(1, MVT::i32));
723 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
724 Args[i].first, DAG.getConstant(0, MVT::i32));
725 RegValuesToPass.push_back(Lo);
726 ++NumIntRegs;
727 if (NumIntRegs < 2) { // Pass both parts in regs?
728 RegValuesToPass.push_back(Hi);
729 ++NumIntRegs;
730 } else {
731 // Pass the high part in memory.
732 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
733 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
734 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
735 Hi, PtrOff, DAG.getSrcValue(NULL)));
736 ArgOffset += 4;
737 }
738 break;
739 }
740 // Fall through
741 case MVT::f64:
742 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
743 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
744 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
745 Args[i].first, PtrOff,
746 DAG.getSrcValue(NULL)));
747 ArgOffset += 8;
748 break;
749 }
750 }
751 if (!Stores.empty())
752 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
753
754 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
755 // arguments and the arguments after the retaddr has been pushed are aligned.
756 if ((ArgOffset & 7) == 0)
757 ArgOffset += 4;
758
759 std::vector<MVT::ValueType> RetVals;
760 MVT::ValueType RetTyVT = getValueType(RetTy);
761
762 RetVals.push_back(MVT::Other);
763
764 // The result values produced have to be legal. Promote the result.
765 switch (RetTyVT) {
766 case MVT::isVoid: break;
767 default:
768 RetVals.push_back(RetTyVT);
769 break;
770 case MVT::i1:
771 case MVT::i8:
772 case MVT::i16:
773 RetVals.push_back(MVT::i32);
774 break;
775 case MVT::f32:
776 if (X86ScalarSSE)
777 RetVals.push_back(MVT::f32);
778 else
779 RetVals.push_back(MVT::f64);
780 break;
781 case MVT::i64:
782 RetVals.push_back(MVT::i32);
783 RetVals.push_back(MVT::i32);
784 break;
785 }
786
787 std::vector<SDOperand> Ops;
788 Ops.push_back(Chain);
789 Ops.push_back(Callee);
790 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
791 // Callee pops all arg values on the stack.
792 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
793
794 // Pass register arguments as needed.
795 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
796
797 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
798 RetVals, Ops);
799 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
800
801 SDOperand ResultVal;
802 switch (RetTyVT) {
803 case MVT::isVoid: break;
804 default:
805 ResultVal = TheCall.getValue(1);
806 break;
807 case MVT::i1:
808 case MVT::i8:
809 case MVT::i16:
810 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
811 break;
812 case MVT::f32:
813 // FIXME: we would really like to remember that this FP_ROUND operation is
814 // okay to eliminate if we allow excess FP precision.
815 ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
816 break;
817 case MVT::i64:
818 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
819 TheCall.getValue(2));
820 break;
821 }
822
823 return std::make_pair(ResultVal, Chain);
824}
825
826SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
827 if (ReturnAddrIndex == 0) {
828 // Set up a frame object for the return address.
829 MachineFunction &MF = DAG.getMachineFunction();
830 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
831 }
832
833 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
834}
835
836
837
838std::pair<SDOperand, SDOperand> X86TargetLowering::
839LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
840 SelectionDAG &DAG) {
841 SDOperand Result;
842 if (Depth) // Depths > 0 not supported yet!
843 Result = DAG.getConstant(0, getPointerTy());
844 else {
845 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
846 if (!isFrameAddress)
847 // Just load the return address
848 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
849 DAG.getSrcValue(NULL));
850 else
851 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
852 DAG.getConstant(4, MVT::i32));
853 }
854 return std::make_pair(Result, Chain);
855}
856
857//===----------------------------------------------------------------------===//
858// X86 Custom Lowering Hooks
859//===----------------------------------------------------------------------===//
860
861/// LowerOperation - Provide custom lowering hooks for some operations.
862///
863SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
864 switch (Op.getOpcode()) {
865 default: assert(0 && "Should not custom lower this!");
866 case ISD::SINT_TO_FP: {
867 assert(Op.getValueType() == MVT::f64 &&
868 Op.getOperand(0).getValueType() == MVT::i64 &&
869 "Unknown SINT_TO_FP to lower!");
870 // We lower sint64->FP into a store to a temporary stack slot, followed by a
871 // FILD64m node.
872 MachineFunction &MF = DAG.getMachineFunction();
873 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
874 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
875 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
876 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
877 std::vector<MVT::ValueType> RTs;
878 RTs.push_back(MVT::f64);
879 RTs.push_back(MVT::Other);
880 std::vector<SDOperand> Ops;
881 Ops.push_back(Store);
882 Ops.push_back(StackSlot);
883 return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
884 }
885 case ISD::FP_TO_SINT: {
886 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
887 Op.getOperand(0).getValueType() == MVT::f64 &&
888 "Unknown FP_TO_SINT to lower!");
889 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
890 // stack slot.
891 MachineFunction &MF = DAG.getMachineFunction();
892 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
893 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
894 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
895
896 unsigned Opc;
897 switch (Op.getValueType()) {
898 default: assert(0 && "Invalid FP_TO_SINT to lower!");
899 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
900 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
901 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
902 }
903
904 // Build the FP_TO_INT*_IN_MEM
905 std::vector<SDOperand> Ops;
906 Ops.push_back(DAG.getEntryNode());
907 Ops.push_back(Op.getOperand(0));
908 Ops.push_back(StackSlot);
909 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
910
911 // Load the result.
912 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
913 DAG.getSrcValue(NULL));
914 }
915 }
916}