1 //===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
13 //===----------------------------------------------------------------------===//
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"
28 #include "llvm/Support/CommandLine.h"
29 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
30 cl::desc("Enable fastcc on X86"));
32 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
33 : TargetLowering(TM) {
35 // Set up the TargetLowering object.
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
43 // Set up the register classes.
44 // FIXME: Eliminate these two classes when legalize can handle promotions
46 addRegisterClass(MVT::i1, X86::R8RegisterClass);
47 addRegisterClass(MVT::i8, X86::R8RegisterClass);
48 addRegisterClass(MVT::i16, X86::R16RegisterClass);
49 addRegisterClass(MVT::i32, X86::R32RegisterClass);
51 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
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);
58 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
60 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
61 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
64 // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
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);
72 // Handle FP_TO_UINT by promoting the destination to a larger signed
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);
79 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
81 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
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);
87 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
88 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
89 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
90 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
91 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
92 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
93 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
94 setOperationAction(ISD::FREM , MVT::f64 , Expand);
95 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
96 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
97 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
98 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
99 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
100 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
101 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
103 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
104 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
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);
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);
120 // Set up the FP register classes.
121 addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
122 addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
124 // SSE has no load+extend ops
125 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
126 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
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);
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);
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);
149 addLegalFPImmediate(+0.0); // xorps / xorpd
151 // Set up the FP register classes.
152 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
155 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
156 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
159 addLegalFPImmediate(+0.0); // FLD0
160 addLegalFPImmediate(+1.0); // FLD1
161 addLegalFPImmediate(-0.0); // FLD0/FCHS
162 addLegalFPImmediate(-1.0); // FLD1/FCHS
164 computeRegisterProperties();
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!
172 std::vector<SDOperand>
173 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
174 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
175 return LowerFastCCArguments(F, DAG);
176 return LowerCCCArguments(F, DAG);
179 std::pair<SDOperand, SDOperand>
180 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
181 bool isVarArg, unsigned CallingConv,
183 SDOperand Callee, ArgListTy &Args,
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);
192 //===----------------------------------------------------------------------===//
193 // C Calling Convention implementation
194 //===----------------------------------------------------------------------===//
196 std::vector<SDOperand>
197 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
198 std::vector<SDOperand> ArgValues;
200 MachineFunction &MF = DAG.getMachineFunction();
201 MachineFrameInfo *MFI = MF.getFrameInfo();
203 // Add DAG nodes to load the arguments... On entry to a function on the X86,
204 // the stack frame looks like this:
206 // [ESP] -- return address
207 // [ESP + 4] -- first argument (leftmost lexically)
208 // [ESP + 8] -- second argument, if first argument is four bytes in size
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;
217 default: assert(0 && "Unhandled argument type!");
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;
226 // Create the frame index object for this incoming parameter...
227 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
229 // Create the SelectionDAG nodes corresponding to a load from this parameter
230 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
232 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
236 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
237 DAG.getSrcValue(NULL));
239 if (MVT::isInteger(ObjectVT))
240 ArgValue = DAG.getConstant(0, ObjectVT);
242 ArgValue = DAG.getConstantFP(0, ObjectVT);
244 ArgValues.push_back(ArgValue);
246 ArgOffset += ArgIncrement; // Move on to the next argument...
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.
252 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
253 ReturnAddrIndex = 0; // No return address slot generated yet.
254 BytesToPopOnReturn = 0; // Callee pops nothing.
255 BytesCallerReserves = ArgOffset;
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;
265 MF.addLiveOut(X86::EAX);
268 MF.addLiveOut(X86::EAX);
269 MF.addLiveOut(X86::EDX);
273 MF.addLiveOut(X86::ST0);
279 std::pair<SDOperand, SDOperand>
280 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
281 bool isVarArg, bool isTailCall,
282 SDOperand Callee, ArgListTy &Args,
284 // Count how many bytes are to be pushed on the stack.
285 unsigned NumBytes = 0;
289 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
290 DAG.getConstant(0, getPointerTy()));
292 for (unsigned i = 0, e = Args.size(); i != e; ++i)
293 switch (getValueType(Args[i].second)) {
294 default: assert(0 && "Unknown value type!");
308 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
309 DAG.getConstant(NumBytes, getPointerTy()));
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(),
315 std::vector<SDOperand> Stores;
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);
321 switch (getValueType(Args[i].second)) {
322 default: assert(0 && "Unexpected ValueType for argument!");
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);
331 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
336 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
337 Args[i].first, PtrOff,
338 DAG.getSrcValue(NULL)));
343 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
344 Args[i].first, PtrOff,
345 DAG.getSrcValue(NULL)));
350 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
353 std::vector<MVT::ValueType> RetVals;
354 MVT::ValueType RetTyVT = getValueType(RetTy);
355 RetVals.push_back(MVT::Other);
357 // The result values produced have to be legal. Promote the result.
359 case MVT::isVoid: break;
361 RetVals.push_back(RetTyVT);
366 RetVals.push_back(MVT::i32);
370 RetVals.push_back(MVT::f32);
372 RetVals.push_back(MVT::f64);
375 RetVals.push_back(MVT::i32);
376 RetVals.push_back(MVT::i32);
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,
386 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
390 case MVT::isVoid: break;
392 ResultVal = TheCall.getValue(1);
397 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
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));
405 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
406 TheCall.getValue(2));
410 return std::make_pair(ResultVal, Chain);
414 X86TargetLowering::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));
423 std::pair<SDOperand,SDOperand>
424 X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
425 Value *VAListV, const Type *ArgTy,
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));
433 if (ArgVT == MVT::i32)
436 assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
437 "Other types should have been promoted for varargs!");
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);
447 //===----------------------------------------------------------------------===//
448 // Fast Calling Convention implementation
449 //===----------------------------------------------------------------------===//
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.
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
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.
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
467 static 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);
476 std::vector<SDOperand>
477 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
478 std::vector<SDOperand> ArgValues;
480 MachineFunction &MF = DAG.getMachineFunction();
481 MachineFrameInfo *MFI = MF.getFrameInfo();
483 // Add DAG nodes to load the arguments... On entry to a function the stack
484 // frame looks like this:
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
490 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
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
495 unsigned NumIntRegs = 0;
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;
504 default: assert(0 && "Unhandled argument type!");
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));
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));
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));
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);
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));
556 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
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));
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);
578 ObjSize = ArgIncrement = 8;
580 case MVT::f32: ObjSize = 4; break;
581 case MVT::f64: ObjSize = ArgIncrement = 8; break;
584 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
586 if (ObjSize && !I->use_empty()) {
587 // Create the frame index object for this incoming parameter...
588 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
590 // Create the SelectionDAG nodes corresponding to a load from this
592 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
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);
600 ArgValue = DAG.getConstantFP(0, ObjectVT);
602 ArgValues.push_back(ArgValue);
605 ArgOffset += ArgIncrement; // Move on to the next argument.
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)
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;
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;
626 MF.addLiveOut(X86::EAX);
629 MF.addLiveOut(X86::EAX);
630 MF.addLiveOut(X86::EDX);
634 MF.addLiveOut(X86::ST0);
640 std::pair<SDOperand, SDOperand>
641 X86TargetLowering::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;
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
650 unsigned NumIntRegs = 0;
652 for (unsigned i = 0, e = Args.size(); i != e; ++i)
653 switch (getValueType(Args[i].second)) {
654 default: assert(0 && "Unknown value type!");
659 if (NumIntRegs < 2) {
668 if (NumIntRegs == 0) {
671 } else if (NumIntRegs == 1) {
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)
688 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
689 DAG.getConstant(NumBytes, getPointerTy()));
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(),
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!");
705 if (NumIntRegs < 2) {
706 RegValuesToPass.push_back(Args[i].first);
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)));
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);
728 if (NumIntRegs < 2) { // Pass both parts in regs?
729 RegValuesToPass.push_back(Hi);
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)));
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)));
753 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
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)
760 std::vector<MVT::ValueType> RetVals;
761 MVT::ValueType RetTyVT = getValueType(RetTy);
763 RetVals.push_back(MVT::Other);
765 // The result values produced have to be legal. Promote the result.
767 case MVT::isVoid: break;
769 RetVals.push_back(RetTyVT);
774 RetVals.push_back(MVT::i32);
778 RetVals.push_back(MVT::f32);
780 RetVals.push_back(MVT::f64);
783 RetVals.push_back(MVT::i32);
784 RetVals.push_back(MVT::i32);
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()));
795 // Pass register arguments as needed.
796 Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
798 SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
800 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
804 case MVT::isVoid: break;
806 ResultVal = TheCall.getValue(1);
811 ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
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));
819 ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
820 TheCall.getValue(2));
824 return std::make_pair(ResultVal, Chain);
827 SDOperand 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);
834 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
839 std::pair<SDOperand, SDOperand> X86TargetLowering::
840 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
843 if (Depth) // Depths > 0 not supported yet!
844 Result = DAG.getConstant(0, getPointerTy());
846 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
848 // Just load the return address
849 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
850 DAG.getSrcValue(NULL));
852 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
853 DAG.getConstant(4, MVT::i32));
855 return std::make_pair(Result, Chain);
858 //===----------------------------------------------------------------------===//
859 // X86 Custom Lowering Hooks
860 //===----------------------------------------------------------------------===//
862 /// LowerOperation - Provide custom lowering hooks for some operations.
864 SDOperand 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
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);
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
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());
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;
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);
913 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
914 DAG.getSrcValue(NULL));
916 case ISD::READCYCLECOUNTER: {
917 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);
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);