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