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