DAG based isel call support.
[oota-llvm.git] / lib / Target / X86 / X86ISelLowering.cpp
1 //===-- 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"
25 using namespace llvm;
26
27 // FIXME: temporary.
28 #include "llvm/Support/CommandLine.h"
29 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
30                                   cl::desc("Enable fastcc on X86"));
31
32 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
33   : TargetLowering(TM) {
34   // Set up the TargetLowering object.
35
36   // X86 is weird, it always uses i8 for shift amounts and setcc results.
37   setShiftAmountType(MVT::i8);
38   setSetCCResultType(MVT::i8);
39   setSetCCResultContents(ZeroOrOneSetCCResult);
40   setShiftAmountFlavor(Mask);   // shl X, 32 == shl X, 0
41
42   // Set up the register classes.
43   addRegisterClass(MVT::i8, X86::R8RegisterClass);
44   addRegisterClass(MVT::i16, X86::R16RegisterClass);
45   addRegisterClass(MVT::i32, X86::R32RegisterClass);
46
47   // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
48   // operation.
49   setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
50   setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
51   setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
52   setOperationAction(ISD::UINT_TO_FP       , MVT::i32  , Promote);
53
54   // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
55   // this operation.
56   setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
57   setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
58
59   if (!X86ScalarSSE) {
60     // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64
61     // isn't legal.
62     setOperationAction(ISD::SINT_TO_FP     , MVT::i64  , Custom);
63     setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Custom);
64     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
65     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
66   }
67
68   // Handle FP_TO_UINT by promoting the destination to a larger signed
69   // conversion.
70   setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
71   setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
72   setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
73
74   if (!X86ScalarSSE)
75     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
76
77   // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
78   // this operation.
79   setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
80   setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
81   setOperationAction(ISD::FP_TO_SINT       , MVT::i16  , Promote);
82
83   setOperationAction(ISD::BIT_CONVERT, MVT::f32, Expand);
84   setOperationAction(ISD::BIT_CONVERT, MVT::i32, Expand);
85
86   if (X86DAGIsel) {
87     setOperationAction(ISD::BRCOND         , MVT::Other, Custom);
88   }
89   setOperationAction(ISD::BRCONDTWOWAY     , MVT::Other, Expand);
90   setOperationAction(ISD::BRTWOWAY_CC      , MVT::Other, Expand);
91   setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
92   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Expand);
93   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Expand);
94   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
95   setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
96   setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
97   setOperationAction(ISD::FREM             , MVT::f64  , Expand);
98   setOperationAction(ISD::CTPOP            , MVT::i8   , Expand);
99   setOperationAction(ISD::CTTZ             , MVT::i8   , Expand);
100   setOperationAction(ISD::CTLZ             , MVT::i8   , Expand);
101   setOperationAction(ISD::CTPOP            , MVT::i16  , Expand);
102   setOperationAction(ISD::CTTZ             , MVT::i16  , Expand);
103   setOperationAction(ISD::CTLZ             , MVT::i16  , Expand);
104   setOperationAction(ISD::CTPOP            , MVT::i32  , Expand);
105   setOperationAction(ISD::CTTZ             , MVT::i32  , Expand);
106   setOperationAction(ISD::CTLZ             , MVT::i32  , Expand);
107   setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
108
109   setOperationAction(ISD::READIO           , MVT::i1   , Expand);
110   setOperationAction(ISD::READIO           , MVT::i8   , Expand);
111   setOperationAction(ISD::READIO           , MVT::i16  , Expand);
112   setOperationAction(ISD::READIO           , MVT::i32  , Expand);
113   setOperationAction(ISD::WRITEIO          , MVT::i1   , Expand);
114   setOperationAction(ISD::WRITEIO          , MVT::i8   , Expand);
115   setOperationAction(ISD::WRITEIO          , MVT::i16  , Expand);
116   setOperationAction(ISD::WRITEIO          , MVT::i32  , Expand);
117
118   // These should be promoted to a larger select which is supported.
119   setOperationAction(ISD::SELECT           , MVT::i1   , Promote);
120   setOperationAction(ISD::SELECT           , MVT::i8   , Promote);
121   // X86 wants to expand cmov itself.
122   if (X86DAGIsel) {
123     setOperationAction(ISD::SELECT         , MVT::i16  , Custom);
124     setOperationAction(ISD::SELECT         , MVT::i32  , Custom);
125     setOperationAction(ISD::SETCC          , MVT::i8   , Custom);
126     setOperationAction(ISD::SETCC          , MVT::i16  , Custom);
127     setOperationAction(ISD::SETCC          , MVT::i32  , Custom);
128     setOperationAction(ISD::GlobalAddress  , MVT::i32  , Custom);
129   }
130
131   // We don't have line number support yet.
132   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
133   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
134
135   if (X86ScalarSSE) {
136     // Set up the FP register classes.
137     addRegisterClass(MVT::f32, X86::V4F4RegisterClass);
138     addRegisterClass(MVT::f64, X86::V2F8RegisterClass);
139
140     // SSE has no load+extend ops
141     setOperationAction(ISD::EXTLOAD,  MVT::f32, Expand);
142     setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
143
144     // SSE has no i16 to fp conversion, only i32
145     setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote);
146     setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote);
147
148     // Expand FP_TO_UINT into a select.
149     // FIXME: We would like to use a Custom expander here eventually to do
150     // the optimal thing for SSE vs. the default expansion in the legalizer.
151     setOperationAction(ISD::FP_TO_UINT       , MVT::i32  , Expand);
152         
153     // We don't support sin/cos/sqrt/fmod
154     setOperationAction(ISD::FSIN , MVT::f64, Expand);
155     setOperationAction(ISD::FCOS , MVT::f64, Expand);
156     setOperationAction(ISD::FABS , MVT::f64, Expand);
157     setOperationAction(ISD::FNEG , MVT::f64, Expand);
158     setOperationAction(ISD::FREM , MVT::f64, Expand);
159     setOperationAction(ISD::FSIN , MVT::f32, Expand);
160     setOperationAction(ISD::FCOS , MVT::f32, Expand);
161     setOperationAction(ISD::FABS , MVT::f32, Expand);
162     setOperationAction(ISD::FNEG , MVT::f32, Expand);
163     setOperationAction(ISD::FREM , MVT::f32, Expand);
164
165     addLegalFPImmediate(+0.0); // xorps / xorpd
166   } else {
167     // Set up the FP register classes.
168     addRegisterClass(MVT::f64, X86::RFPRegisterClass);
169
170     if (!UnsafeFPMath) {
171       setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
172       setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
173     }
174
175     addLegalFPImmediate(+0.0); // FLD0
176     addLegalFPImmediate(+1.0); // FLD1
177     addLegalFPImmediate(-0.0); // FLD0/FCHS
178     addLegalFPImmediate(-1.0); // FLD1/FCHS
179   }
180   computeRegisterProperties();
181
182   maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores
183   maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores
184   maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores
185   allowUnalignedMemoryAccesses = true; // x86 supports it!
186 }
187
188 std::vector<SDOperand>
189 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
190   if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
191     return LowerFastCCArguments(F, DAG);
192   return LowerCCCArguments(F, DAG);
193 }
194
195 std::pair<SDOperand, SDOperand>
196 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
197                                bool isVarArg, unsigned CallingConv,
198                                bool isTailCall,
199                                SDOperand Callee, ArgListTy &Args,
200                                SelectionDAG &DAG) {
201   assert((!isVarArg || CallingConv == CallingConv::C) &&
202          "Only C takes varargs!");
203   if (CallingConv == CallingConv::Fast && EnableFastCC)
204     return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
205   return  LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
206 }
207
208 SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op,
209                                            SelectionDAG &DAG) {
210   if (!X86DAGIsel)
211     return DAG.getNode(ISD::RET, MVT::Other, Chain, Op);
212
213   SDOperand Copy;
214   MVT::ValueType OpVT = Op.getValueType();
215   switch (OpVT) {
216     default: assert(0 && "Unknown type to return!");
217     case MVT::i32:
218       Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand());
219       break;
220     case MVT::i64: {
221       SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, 
222                                  DAG.getConstant(1, MVT::i32));
223       SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op,
224                                  DAG.getConstant(0, MVT::i32));
225       Copy = DAG.getCopyToReg(Chain, X86::EAX, Hi, SDOperand());
226       Copy = DAG.getCopyToReg(Copy,  X86::EDX, Lo, Copy.getValue(1));
227       break;
228     }
229     case MVT::f32:
230     case MVT::f64:
231       if (!X86ScalarSSE) {
232         std::vector<MVT::ValueType> Tys;
233         Tys.push_back(MVT::Other);
234         Tys.push_back(MVT::Flag);
235         std::vector<SDOperand> Ops;
236         Ops.push_back(Chain);
237         if (OpVT == MVT::f32)
238           Op = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Op);
239         Ops.push_back(Op);
240         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
241       } else {
242         // Spill the value to memory and reload it into top of stack.
243         unsigned Size = MVT::getSizeInBits(OpVT)/8;
244         MachineFunction &MF = DAG.getMachineFunction();
245         int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
246         SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
247         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op,
248                             StackSlot, DAG.getSrcValue(NULL));
249         std::vector<MVT::ValueType> Tys;
250         Tys.push_back(MVT::f64);
251         Tys.push_back(MVT::Other);
252         std::vector<SDOperand> Ops;
253         Ops.push_back(Chain);
254         Ops.push_back(StackSlot);
255         Ops.push_back(DAG.getValueType(OpVT));
256         Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
257         Tys.clear();
258         Tys.push_back(MVT::Other);
259         Tys.push_back(MVT::Flag);
260         Ops.clear();
261         Ops.push_back(Copy.getValue(1));
262         Ops.push_back(Copy);
263         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
264       }
265       break;
266   }
267
268   return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
269                      Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
270                      Copy.getValue(1));
271 }
272
273 //===----------------------------------------------------------------------===//
274 //                    C Calling Convention implementation
275 //===----------------------------------------------------------------------===//
276
277 std::vector<SDOperand>
278 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
279   std::vector<SDOperand> ArgValues;
280
281   MachineFunction &MF = DAG.getMachineFunction();
282   MachineFrameInfo *MFI = MF.getFrameInfo();
283
284   // Add DAG nodes to load the arguments...  On entry to a function on the X86,
285   // the stack frame looks like this:
286   //
287   // [ESP] -- return address
288   // [ESP + 4] -- first argument (leftmost lexically)
289   // [ESP + 8] -- second argument, if first argument is four bytes in size
290   //    ...
291   //
292   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
293   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
294     MVT::ValueType ObjectVT = getValueType(I->getType());
295     unsigned ArgIncrement = 4;
296     unsigned ObjSize;
297     switch (ObjectVT) {
298     default: assert(0 && "Unhandled argument type!");
299     case MVT::i1:
300     case MVT::i8:  ObjSize = 1;                break;
301     case MVT::i16: ObjSize = 2;                break;
302     case MVT::i32: ObjSize = 4;                break;
303     case MVT::i64: ObjSize = ArgIncrement = 8; break;
304     case MVT::f32: ObjSize = 4;                break;
305     case MVT::f64: ObjSize = ArgIncrement = 8; break;
306     }
307     // Create the frame index object for this incoming parameter...
308     int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
309
310     // Create the SelectionDAG nodes corresponding to a load from this parameter
311     SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
312
313     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
314     // dead loads.
315     SDOperand ArgValue;
316     if (!I->use_empty())
317       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
318                              DAG.getSrcValue(NULL));
319     else {
320       if (MVT::isInteger(ObjectVT))
321         ArgValue = DAG.getConstant(0, ObjectVT);
322       else
323         ArgValue = DAG.getConstantFP(0, ObjectVT);
324     }
325     ArgValues.push_back(ArgValue);
326
327     ArgOffset += ArgIncrement;   // Move on to the next argument...
328   }
329
330   // If the function takes variable number of arguments, make a frame index for
331   // the start of the first vararg value... for expansion of llvm.va_start.
332   if (F.isVarArg())
333     VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
334   ReturnAddrIndex = 0;     // No return address slot generated yet.
335   BytesToPopOnReturn = 0;  // Callee pops nothing.
336   BytesCallerReserves = ArgOffset;
337
338   // Finally, inform the code generator which regs we return values in.
339   switch (getValueType(F.getReturnType())) {
340   default: assert(0 && "Unknown type!");
341   case MVT::isVoid: break;
342   case MVT::i1:
343   case MVT::i8:
344   case MVT::i16:
345   case MVT::i32:
346     MF.addLiveOut(X86::EAX);
347     break;
348   case MVT::i64:
349     MF.addLiveOut(X86::EAX);
350     MF.addLiveOut(X86::EDX);
351     break;
352   case MVT::f32:
353   case MVT::f64:
354     MF.addLiveOut(X86::ST0);
355     break;
356   }
357   return ArgValues;
358 }
359
360 std::pair<SDOperand, SDOperand>
361 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
362                                   bool isVarArg, bool isTailCall,
363                                   SDOperand Callee, ArgListTy &Args,
364                                   SelectionDAG &DAG) {
365   // Count how many bytes are to be pushed on the stack.
366   unsigned NumBytes = 0;
367
368   if (Args.empty()) {
369     // Save zero bytes.
370     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
371                         DAG.getConstant(0, getPointerTy()));
372   } else {
373     for (unsigned i = 0, e = Args.size(); i != e; ++i)
374       switch (getValueType(Args[i].second)) {
375       default: assert(0 && "Unknown value type!");
376       case MVT::i1:
377       case MVT::i8:
378       case MVT::i16:
379       case MVT::i32:
380       case MVT::f32:
381         NumBytes += 4;
382         break;
383       case MVT::i64:
384       case MVT::f64:
385         NumBytes += 8;
386         break;
387       }
388
389     Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
390                         DAG.getConstant(NumBytes, getPointerTy()));
391
392     // Arguments go on the stack in reverse order, as specified by the ABI.
393     unsigned ArgOffset = 0;
394     SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
395                                             X86::ESP, MVT::i32);
396     std::vector<SDOperand> Stores;
397
398     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
399       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
400       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
401
402       switch (getValueType(Args[i].second)) {
403       default: assert(0 && "Unexpected ValueType for argument!");
404       case MVT::i1:
405       case MVT::i8:
406       case MVT::i16:
407         // Promote the integer to 32 bits.  If the input type is signed use a
408         // sign extend, otherwise use a zero extend.
409         if (Args[i].second->isSigned())
410           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
411         else
412           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
413
414         // FALL THROUGH
415       case MVT::i32:
416       case MVT::f32:
417         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
418                                      Args[i].first, PtrOff,
419                                      DAG.getSrcValue(NULL)));
420         ArgOffset += 4;
421         break;
422       case MVT::i64:
423       case MVT::f64:
424         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
425                                      Args[i].first, PtrOff,
426                                      DAG.getSrcValue(NULL)));
427         ArgOffset += 8;
428         break;
429       }
430     }
431     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
432   }
433
434   std::vector<MVT::ValueType> RetVals;
435   MVT::ValueType RetTyVT = getValueType(RetTy);
436   RetVals.push_back(MVT::Other);
437
438   // The result values produced have to be legal.  Promote the result.
439   switch (RetTyVT) {
440   case MVT::isVoid: break;
441   default:
442     RetVals.push_back(RetTyVT);
443     break;
444   case MVT::i1:
445   case MVT::i8:
446   case MVT::i16:
447     RetVals.push_back(MVT::i32);
448     break;
449   case MVT::f32:
450     if (X86ScalarSSE)
451       RetVals.push_back(MVT::f32);
452     else
453       RetVals.push_back(MVT::f64);
454     break;
455   case MVT::i64:
456     RetVals.push_back(MVT::i32);
457     RetVals.push_back(MVT::i32);
458     break;
459   }
460
461   if (X86DAGIsel) {
462     std::vector<MVT::ValueType> NodeTys;
463     NodeTys.push_back(MVT::Other);   // Returns a chain
464     NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
465
466     std::vector<SDOperand> Ops;
467     Ops.push_back(Chain);
468     Ops.push_back(Callee);
469
470     Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
471                         NodeTys, Ops);
472     SDOperand InFlag = Chain.getValue(1);
473
474     SDOperand RetVal;
475     if (RetTyVT != MVT::isVoid) {
476       switch (RetTyVT) {
477       default: assert(0 && "Unknown value type to return!");
478       case MVT::i1:
479       case MVT::i8:
480         RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
481         Chain = RetVal.getValue(1);
482         break;
483       case MVT::i16:
484         RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
485         Chain = RetVal.getValue(1);
486         break;
487       case MVT::i32:
488         RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
489         Chain = RetVal.getValue(1);
490         break;
491       case MVT::i64: {
492         SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
493         SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, 
494                                           Lo.getValue(2));
495         RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
496         Chain = Hi.getValue(1);
497         break;
498       }
499       case MVT::f32:
500       case MVT::f64: {
501         std::vector<MVT::ValueType> Tys;
502         Tys.push_back(MVT::f64);
503         Tys.push_back(MVT::Other);
504         std::vector<SDOperand> Ops;
505         Ops.push_back(Chain);
506         Ops.push_back(InFlag);
507         RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
508         Chain = RetVal.getValue(1);
509         if (X86ScalarSSE) {
510           unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
511           MachineFunction &MF = DAG.getMachineFunction();
512           int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
513           SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
514           Tys.clear();
515           Tys.push_back(MVT::Other);
516           Ops.clear();
517           Ops.push_back(Chain);
518           Ops.push_back(RetVal);
519           Ops.push_back(StackSlot);
520           Ops.push_back(DAG.getValueType(RetTyVT));
521           Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
522           RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
523                                DAG.getSrcValue(NULL));
524           Chain = RetVal.getValue(1);
525         } else if (RetTyVT == MVT::f32)
526           RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
527         break;
528       }
529       }
530     }
531
532     Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
533                         DAG.getConstant(NumBytes, getPointerTy()),
534                         DAG.getConstant(0, getPointerTy()));
535     return std::make_pair(RetVal, Chain);
536   } else {
537     std::vector<SDOperand> Ops;
538     Ops.push_back(Chain);
539     Ops.push_back(Callee);
540     Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
541     Ops.push_back(DAG.getConstant(0, getPointerTy()));
542
543     SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
544                                     RetVals, Ops);
545
546     SDOperand ResultVal;
547     switch (RetTyVT) {
548     case MVT::isVoid: break;
549     default:
550       ResultVal = TheCall.getValue(1);
551       break;
552     case MVT::i1:
553     case MVT::i8:
554     case MVT::i16:
555       ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
556       break;
557     case MVT::f32:
558       // FIXME: we would really like to remember that this FP_ROUND operation is
559       // okay to eliminate if we allow excess FP precision.
560       ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
561       break;
562     case MVT::i64:
563       ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
564                               TheCall.getValue(2));
565       break;
566     }
567
568     Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
569     return std::make_pair(ResultVal, Chain);
570   }
571 }
572
573 SDOperand
574 X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
575                                 Value *VAListV, SelectionDAG &DAG) {
576   // vastart just stores the address of the VarArgsFrameIndex slot.
577   SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
578   return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
579                      DAG.getSrcValue(VAListV));
580 }
581
582
583 std::pair<SDOperand,SDOperand>
584 X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP,
585                               Value *VAListV, const Type *ArgTy,
586                               SelectionDAG &DAG) {
587   MVT::ValueType ArgVT = getValueType(ArgTy);
588   SDOperand Val = DAG.getLoad(MVT::i32, Chain,
589                               VAListP, DAG.getSrcValue(VAListV));
590   SDOperand Result = DAG.getLoad(ArgVT, Chain, Val,
591                                  DAG.getSrcValue(NULL));
592   unsigned Amt;
593   if (ArgVT == MVT::i32)
594     Amt = 4;
595   else {
596     assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) &&
597            "Other types should have been promoted for varargs!");
598     Amt = 8;
599   }
600   Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val,
601                     DAG.getConstant(Amt, Val.getValueType()));
602   Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain,
603                       Val, VAListP, DAG.getSrcValue(VAListV));
604   return std::make_pair(Result, Chain);
605 }
606
607 //===----------------------------------------------------------------------===//
608 //                    Fast Calling Convention implementation
609 //===----------------------------------------------------------------------===//
610 //
611 // The X86 'fast' calling convention passes up to two integer arguments in
612 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
613 // and requires that the callee pop its arguments off the stack (allowing proper
614 // tail calls), and has the same return value conventions as C calling convs.
615 //
616 // This calling convention always arranges for the callee pop value to be 8n+4
617 // bytes, which is needed for tail recursion elimination and stack alignment
618 // reasons.
619 //
620 // Note that this can be enhanced in the future to pass fp vals in registers
621 // (when we have a global fp allocator) and do other tricks.
622 //
623
624 /// AddLiveIn - This helper function adds the specified physical register to the
625 /// MachineFunction as a live in value.  It also creates a corresponding virtual
626 /// register for it.
627 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
628                           TargetRegisterClass *RC) {
629   assert(RC->contains(PReg) && "Not the correct regclass!");
630   unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
631   MF.addLiveIn(PReg, VReg);
632   return VReg;
633 }
634
635
636 std::vector<SDOperand>
637 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
638   std::vector<SDOperand> ArgValues;
639
640   MachineFunction &MF = DAG.getMachineFunction();
641   MachineFrameInfo *MFI = MF.getFrameInfo();
642
643   // Add DAG nodes to load the arguments...  On entry to a function the stack
644   // frame looks like this:
645   //
646   // [ESP] -- return address
647   // [ESP + 4] -- first nonreg argument (leftmost lexically)
648   // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
649   //    ...
650   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
651
652   // Keep track of the number of integer regs passed so far.  This can be either
653   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
654   // used).
655   unsigned NumIntRegs = 0;
656
657   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
658     MVT::ValueType ObjectVT = getValueType(I->getType());
659     unsigned ArgIncrement = 4;
660     unsigned ObjSize = 0;
661     SDOperand ArgValue;
662
663     switch (ObjectVT) {
664     default: assert(0 && "Unhandled argument type!");
665     case MVT::i1:
666     case MVT::i8:
667       if (NumIntRegs < 2) {
668         if (!I->use_empty()) {
669           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
670                                     X86::R8RegisterClass);
671           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
672           DAG.setRoot(ArgValue.getValue(1));
673           if (ObjectVT == MVT::i1)
674             // FIXME: Should insert a assertzext here.
675             ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
676         }
677         ++NumIntRegs;
678         break;
679       }
680
681       ObjSize = 1;
682       break;
683     case MVT::i16:
684       if (NumIntRegs < 2) {
685         if (!I->use_empty()) {
686           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
687                                     X86::R16RegisterClass);
688           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
689           DAG.setRoot(ArgValue.getValue(1));
690         }
691         ++NumIntRegs;
692         break;
693       }
694       ObjSize = 2;
695       break;
696     case MVT::i32:
697       if (NumIntRegs < 2) {
698         if (!I->use_empty()) {
699           unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
700                                     X86::R32RegisterClass);
701           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
702           DAG.setRoot(ArgValue.getValue(1));
703         }
704         ++NumIntRegs;
705         break;
706       }
707       ObjSize = 4;
708       break;
709     case MVT::i64:
710       if (NumIntRegs == 0) {
711         if (!I->use_empty()) {
712           unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
713           unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
714
715           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
716           SDOperand Hi  = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
717           DAG.setRoot(Hi.getValue(1));
718
719           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
720         }
721         NumIntRegs = 2;
722         break;
723       } else if (NumIntRegs == 1) {
724         if (!I->use_empty()) {
725           unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
726           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
727           DAG.setRoot(Low.getValue(1));
728
729           // Load the high part from memory.
730           // Create the frame index object for this incoming parameter...
731           int FI = MFI->CreateFixedObject(4, ArgOffset);
732           SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
733           SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
734                                      DAG.getSrcValue(NULL));
735           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
736         }
737         ArgOffset += 4;
738         NumIntRegs = 2;
739         break;
740       }
741       ObjSize = ArgIncrement = 8;
742       break;
743     case MVT::f32: ObjSize = 4;                break;
744     case MVT::f64: ObjSize = ArgIncrement = 8; break;
745     }
746
747     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
748     // dead loads.
749     if (ObjSize && !I->use_empty()) {
750       // Create the frame index object for this incoming parameter...
751       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
752
753       // Create the SelectionDAG nodes corresponding to a load from this
754       // parameter.
755       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
756
757       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
758                              DAG.getSrcValue(NULL));
759     } else if (ArgValue.Val == 0) {
760       if (MVT::isInteger(ObjectVT))
761         ArgValue = DAG.getConstant(0, ObjectVT);
762       else
763         ArgValue = DAG.getConstantFP(0, ObjectVT);
764     }
765     ArgValues.push_back(ArgValue);
766
767     if (ObjSize)
768       ArgOffset += ArgIncrement;   // Move on to the next argument.
769   }
770
771   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
772   // arguments and the arguments after the retaddr has been pushed are aligned.
773   if ((ArgOffset & 7) == 0)
774     ArgOffset += 4;
775
776   VarArgsFrameIndex = 0xAAAAAAA;   // fastcc functions can't have varargs.
777   ReturnAddrIndex = 0;             // No return address slot generated yet.
778   BytesToPopOnReturn = ArgOffset;  // Callee pops all stack arguments.
779   BytesCallerReserves = 0;
780
781   // Finally, inform the code generator which regs we return values in.
782   switch (getValueType(F.getReturnType())) {
783   default: assert(0 && "Unknown type!");
784   case MVT::isVoid: break;
785   case MVT::i1:
786   case MVT::i8:
787   case MVT::i16:
788   case MVT::i32:
789     MF.addLiveOut(X86::EAX);
790     break;
791   case MVT::i64:
792     MF.addLiveOut(X86::EAX);
793     MF.addLiveOut(X86::EDX);
794     break;
795   case MVT::f32:
796   case MVT::f64:
797     MF.addLiveOut(X86::ST0);
798     break;
799   }
800   return ArgValues;
801 }
802
803 std::pair<SDOperand, SDOperand>
804 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
805                                      bool isTailCall, SDOperand Callee,
806                                      ArgListTy &Args, SelectionDAG &DAG) {
807   // Count how many bytes are to be pushed on the stack.
808   unsigned NumBytes = 0;
809
810   // Keep track of the number of integer regs passed so far.  This can be either
811   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
812   // used).
813   unsigned NumIntRegs = 0;
814
815   for (unsigned i = 0, e = Args.size(); i != e; ++i)
816     switch (getValueType(Args[i].second)) {
817     default: assert(0 && "Unknown value type!");
818     case MVT::i1:
819     case MVT::i8:
820     case MVT::i16:
821     case MVT::i32:
822       if (NumIntRegs < 2) {
823         ++NumIntRegs;
824         break;
825       }
826       // fall through
827     case MVT::f32:
828       NumBytes += 4;
829       break;
830     case MVT::i64:
831       if (NumIntRegs == 0) {
832         NumIntRegs = 2;
833         break;
834       } else if (NumIntRegs == 1) {
835         NumIntRegs = 2;
836         NumBytes += 4;
837         break;
838       }
839
840       // fall through
841     case MVT::f64:
842       NumBytes += 8;
843       break;
844     }
845
846   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
847   // arguments and the arguments after the retaddr has been pushed are aligned.
848   if ((NumBytes & 7) == 0)
849     NumBytes += 4;
850
851   Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
852                       DAG.getConstant(NumBytes, getPointerTy()));
853
854   // Arguments go on the stack in reverse order, as specified by the ABI.
855   unsigned ArgOffset = 0;
856   SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(),
857                                           X86::ESP, MVT::i32);
858   NumIntRegs = 0;
859   std::vector<SDOperand> Stores;
860   std::vector<SDOperand> RegValuesToPass;
861   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
862     switch (getValueType(Args[i].second)) {
863     default: assert(0 && "Unexpected ValueType for argument!");
864     case MVT::i1:
865       Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
866       // Fall through.
867     case MVT::i8:
868     case MVT::i16:
869     case MVT::i32:
870       if (NumIntRegs < 2) {
871         RegValuesToPass.push_back(Args[i].first);
872         ++NumIntRegs;
873         break;
874       }
875       // Fall through
876     case MVT::f32: {
877       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
878       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
879       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
880                                    Args[i].first, PtrOff,
881                                    DAG.getSrcValue(NULL)));
882       ArgOffset += 4;
883       break;
884     }
885     case MVT::i64:
886       if (NumIntRegs < 2) {    // Can pass part of it in regs?
887         SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
888                                    Args[i].first, DAG.getConstant(1, MVT::i32));
889         SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
890                                    Args[i].first, DAG.getConstant(0, MVT::i32));
891         RegValuesToPass.push_back(Lo);
892         ++NumIntRegs;
893         if (NumIntRegs < 2) {   // Pass both parts in regs?
894           RegValuesToPass.push_back(Hi);
895           ++NumIntRegs;
896         } else {
897           // Pass the high part in memory.
898           SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
899           PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
900           Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
901                                        Hi, PtrOff, DAG.getSrcValue(NULL)));
902           ArgOffset += 4;
903         }
904         break;
905       }
906       // Fall through
907     case MVT::f64:
908       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
909       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
910       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
911                                    Args[i].first, PtrOff,
912                                    DAG.getSrcValue(NULL)));
913       ArgOffset += 8;
914       break;
915     }
916   }
917   if (!Stores.empty())
918     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
919
920   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
921   // arguments and the arguments after the retaddr has been pushed are aligned.
922   if ((ArgOffset & 7) == 0)
923     ArgOffset += 4;
924
925   std::vector<MVT::ValueType> RetVals;
926   MVT::ValueType RetTyVT = getValueType(RetTy);
927
928   RetVals.push_back(MVT::Other);
929
930   // The result values produced have to be legal.  Promote the result.
931   switch (RetTyVT) {
932   case MVT::isVoid: break;
933   default:
934     RetVals.push_back(RetTyVT);
935     break;
936   case MVT::i1:
937   case MVT::i8:
938   case MVT::i16:
939     RetVals.push_back(MVT::i32);
940     break;
941   case MVT::f32:
942     if (X86ScalarSSE)
943       RetVals.push_back(MVT::f32);
944     else
945       RetVals.push_back(MVT::f64);
946     break;
947   case MVT::i64:
948     RetVals.push_back(MVT::i32);
949     RetVals.push_back(MVT::i32);
950     break;
951   }
952
953   std::vector<SDOperand> Ops;
954   Ops.push_back(Chain);
955   Ops.push_back(Callee);
956   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
957   // Callee pops all arg values on the stack.
958   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
959
960   // Pass register arguments as needed.
961   Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end());
962
963   SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
964                                   RetVals, Ops);
965   Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall);
966
967   SDOperand ResultVal;
968   switch (RetTyVT) {
969   case MVT::isVoid: break;
970   default:
971     ResultVal = TheCall.getValue(1);
972     break;
973   case MVT::i1:
974   case MVT::i8:
975   case MVT::i16:
976     ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1));
977     break;
978   case MVT::f32:
979     // FIXME: we would really like to remember that this FP_ROUND operation is
980     // okay to eliminate if we allow excess FP precision.
981     ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1));
982     break;
983   case MVT::i64:
984     ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1),
985                             TheCall.getValue(2));
986     break;
987   }
988
989   return std::make_pair(ResultVal, Chain);
990 }
991
992 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
993   if (ReturnAddrIndex == 0) {
994     // Set up a frame object for the return address.
995     MachineFunction &MF = DAG.getMachineFunction();
996     ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
997   }
998
999   return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1000 }
1001
1002
1003
1004 std::pair<SDOperand, SDOperand> X86TargetLowering::
1005 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1006                         SelectionDAG &DAG) {
1007   SDOperand Result;
1008   if (Depth)        // Depths > 0 not supported yet!
1009     Result = DAG.getConstant(0, getPointerTy());
1010   else {
1011     SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1012     if (!isFrameAddress)
1013       // Just load the return address
1014       Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1015                            DAG.getSrcValue(NULL));
1016     else
1017       Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1018                            DAG.getConstant(4, MVT::i32));
1019   }
1020   return std::make_pair(Result, Chain);
1021 }
1022
1023 //===----------------------------------------------------------------------===//
1024 //                           X86 Custom Lowering Hooks
1025 //===----------------------------------------------------------------------===//
1026
1027 /// LowerOperation - Provide custom lowering hooks for some operations.
1028 ///
1029 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1030   switch (Op.getOpcode()) {
1031   default: assert(0 && "Should not custom lower this!");
1032   case ISD::SINT_TO_FP: {
1033     assert(Op.getValueType() == MVT::f64 &&
1034            Op.getOperand(0).getValueType() == MVT::i64 &&
1035            "Unknown SINT_TO_FP to lower!");
1036     // We lower sint64->FP into a store to a temporary stack slot, followed by a
1037     // FILD64m node.
1038     MachineFunction &MF = DAG.getMachineFunction();
1039     int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1040     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1041     SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
1042                            Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
1043     std::vector<MVT::ValueType> RTs;
1044     RTs.push_back(MVT::f64);
1045     RTs.push_back(MVT::Other);
1046     std::vector<SDOperand> Ops;
1047     Ops.push_back(Store);
1048     Ops.push_back(StackSlot);
1049     return DAG.getNode(X86ISD::FILD64m, RTs, Ops);
1050   }
1051   case ISD::FP_TO_SINT: {
1052     assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1053            Op.getOperand(0).getValueType() == MVT::f64 &&
1054            "Unknown FP_TO_SINT to lower!");
1055     // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1056     // stack slot.
1057     MachineFunction &MF = DAG.getMachineFunction();
1058     unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1059     int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1060     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1061
1062     unsigned Opc;
1063     switch (Op.getValueType()) {
1064     default: assert(0 && "Invalid FP_TO_SINT to lower!");
1065     case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1066     case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1067     case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1068     }
1069
1070     // Build the FP_TO_INT*_IN_MEM
1071     std::vector<SDOperand> Ops;
1072     Ops.push_back(DAG.getEntryNode());
1073     Ops.push_back(Op.getOperand(0));
1074     Ops.push_back(StackSlot);
1075     SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1076
1077     // Load the result.
1078     return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1079                        DAG.getSrcValue(NULL));
1080   }
1081   case ISD::READCYCLECOUNTER: {
1082     std::vector<MVT::ValueType> Tys;
1083     Tys.push_back(MVT::Other);
1084     Tys.push_back(MVT::Flag);
1085     std::vector<SDOperand> Ops;
1086     Ops.push_back(Op.getOperand(0));
1087     SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1088     Ops.clear();
1089     Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1090     Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, 
1091                                      MVT::i32, Ops[0].getValue(2)));
1092     Ops.push_back(Ops[1].getValue(1));
1093     Tys[0] = Tys[1] = MVT::i32;
1094     Tys.push_back(MVT::Other);
1095     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1096   }
1097   case ISD::SETCC: {
1098     assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1099     SDOperand CC   = Op.getOperand(2);
1100     SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1101                                  Op.getOperand(0), Op.getOperand(1));
1102     return DAG.getNode(X86ISD::SETCC, MVT::i8, CC, Cond);
1103   }
1104   case ISD::SELECT: {
1105     SDOperand Cond  = Op.getOperand(0);
1106     SDOperand CC;
1107     if (Cond.getOpcode() == X86ISD::SETCC) {
1108       CC = Cond.getOperand(0);
1109       Cond = Cond.getOperand(1);
1110     } else if (Cond.getOpcode() == ISD::SETCC) {
1111       CC = Cond.getOperand(2);
1112       Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1113                          Cond.getOperand(0), Cond.getOperand(1));
1114     } else {
1115       CC = DAG.getCondCode(ISD::SETEQ);
1116       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1117     }
1118     return DAG.getNode(X86ISD::CMOV, Op.getValueType(),
1119                        Op.getOperand(1), Op.getOperand(2), CC, Cond);
1120   }
1121   case ISD::BRCOND: {
1122     SDOperand Cond  = Op.getOperand(1);
1123     SDOperand Dest  = Op.getOperand(2);
1124     SDOperand CC;
1125     // TODO: handle Cond == OR / AND / XOR
1126     if (Cond.getOpcode() == X86ISD::SETCC) {
1127       CC = Cond.getOperand(0);
1128       Cond = Cond.getOperand(1);
1129     } else if (Cond.getOpcode() == ISD::SETCC) {
1130       CC = Cond.getOperand(2);
1131       Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1132                          Cond.getOperand(0), Cond.getOperand(1));
1133     } else {
1134       CC = DAG.getCondCode(ISD::SETNE);
1135       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1136     }
1137     return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1138                        Op.getOperand(0), Op.getOperand(2), CC, Cond);
1139   }
1140   case ISD::GlobalAddress: {
1141     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1142     SDOperand GVOp = DAG.getTargetGlobalAddress(GV, getPointerTy());
1143     // For Darwin, external and weak symbols are indirect, so we want to load
1144     // the value at address GV, not the value of GV itself.  This means that
1145     // the GlobalAddress must be in the base or index register of the address,
1146     // not the GV offset field.
1147     if (getTargetMachine().
1148         getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() &&
1149         (GV->hasWeakLinkage() || GV->isExternal()))
1150       return DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1151                          GVOp, DAG.getSrcValue(NULL));
1152     else
1153       return GVOp;
1154     break;
1155   }
1156   }
1157 }
1158
1159 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1160   switch (Opcode) {
1161   default: return NULL;
1162   case X86ISD::FILD64m:            return "X86ISD::FILD64m";
1163   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1164   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1165   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
1166   case X86ISD::FLD:                return "X86ISD::FLD";
1167   case X86ISD::FST:                return "X86ISD::FST";
1168   case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
1169   case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
1170   case X86ISD::CALL:               return "X86ISD::CALL";
1171   case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
1172   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
1173   case X86ISD::CMP:                return "X86ISD::CMP";
1174   case X86ISD::TEST:               return "X86ISD::TEST";
1175   case X86ISD::SETCC:              return "X86ISD::SETCC";
1176   case X86ISD::CMOV:               return "X86ISD::CMOV";
1177   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
1178   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
1179   }
1180 }
1181
1182 bool X86TargetLowering::isMaskedValueZeroForTargetNode(const SDOperand &Op,
1183                                                        uint64_t Mask) const {
1184
1185   unsigned Opc = Op.getOpcode();
1186
1187   switch (Opc) {
1188   default:
1189     assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
1190     break;
1191   case X86ISD::SETCC: return (Mask & 1) == 0;
1192   }
1193
1194   return false;
1195 }