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