Enable Dwarf debugging info.
[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 "X86InstrBuilder.h"
17 #include "X86ISelLowering.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/Function.h"
22 #include "llvm/CodeGen/MachineFrameInfo.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SSARegMap.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Target/TargetOptions.h"
29 #include "llvm/ADT/VectorExtras.h"
30 using namespace llvm;
31
32 // FIXME: temporary.
33 #include "llvm/Support/CommandLine.h"
34 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35                                   cl::desc("Enable fastcc on X86"));
36
37 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38   : TargetLowering(TM) {
39   Subtarget = &TM.getSubtarget<X86Subtarget>();
40   X86ScalarSSE = Subtarget->hasSSE2();
41
42   // Set up the TargetLowering object.
43
44   // X86 is weird, it always uses i8 for shift amounts and setcc results.
45   setShiftAmountType(MVT::i8);
46   setSetCCResultType(MVT::i8);
47   setSetCCResultContents(ZeroOrOneSetCCResult);
48   setSchedulingPreference(SchedulingForRegPressure);
49   setShiftAmountFlavor(Mask);   // shl X, 32 == shl X, 0
50   setStackPointerRegisterToSaveRestore(X86::ESP);
51   
52   // Set up the register classes.
53   addRegisterClass(MVT::i8, X86::R8RegisterClass);
54   addRegisterClass(MVT::i16, X86::R16RegisterClass);
55   addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57   // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58   // operation.
59   setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
60   setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
61   setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
62
63   if (X86ScalarSSE)
64     // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Expand);
66   else
67     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Promote);
68
69   // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70   // this operation.
71   setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
72   setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
73   // SSE has no i16 to fp conversion, only i32
74   if (X86ScalarSSE)
75     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
76   else {
77     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Custom);
78     setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
79   }
80
81   // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82   // isn't legal.
83   setOperationAction(ISD::SINT_TO_FP       , MVT::i64  , Custom);
84   setOperationAction(ISD::FP_TO_SINT       , MVT::i64  , Custom);
85
86   // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87   // this operation.
88   setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
89   setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
90
91   if (X86ScalarSSE) {
92     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
93   } else {
94     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
95     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
96   }
97
98   // Handle FP_TO_UINT by promoting the destination to a larger signed
99   // conversion.
100   setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
101   setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
102   setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
103
104   if (X86ScalarSSE && !Subtarget->hasSSE3())
105     // Expand FP_TO_UINT into a select.
106     // FIXME: We would like to use a Custom expander here eventually to do
107     // the optimal thing for SSE vs. the default expansion in the legalizer.
108     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Expand);
109   else
110     // With SSE3 we can use fisttpll to convert to a signed i64.
111     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
112
113   setOperationAction(ISD::BIT_CONVERT      , MVT::f32  , Expand);
114   setOperationAction(ISD::BIT_CONVERT      , MVT::i32  , Expand);
115
116   setOperationAction(ISD::BRCOND           , MVT::Other, Custom);
117   setOperationAction(ISD::BRCONDTWOWAY     , MVT::Other, Expand);
118   setOperationAction(ISD::BRTWOWAY_CC      , MVT::Other, Expand);
119   setOperationAction(ISD::BR_CC            , MVT::Other, Expand);
120   setOperationAction(ISD::SELECT_CC        , MVT::Other, Expand);
121   setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
122   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Expand);
123   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Expand);
124   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
125   setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
126   setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
127   setOperationAction(ISD::FREM             , MVT::f64  , Expand);
128   setOperationAction(ISD::CTPOP            , MVT::i8   , Expand);
129   setOperationAction(ISD::CTTZ             , MVT::i8   , Expand);
130   setOperationAction(ISD::CTLZ             , MVT::i8   , Expand);
131   setOperationAction(ISD::CTPOP            , MVT::i16  , Expand);
132   setOperationAction(ISD::CTTZ             , MVT::i16  , Expand);
133   setOperationAction(ISD::CTLZ             , MVT::i16  , Expand);
134   setOperationAction(ISD::CTPOP            , MVT::i32  , Expand);
135   setOperationAction(ISD::CTTZ             , MVT::i32  , Expand);
136   setOperationAction(ISD::CTLZ             , MVT::i32  , Expand);
137   setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
138   setOperationAction(ISD::BSWAP            , MVT::i16  , Expand);
139
140   // These should be promoted to a larger select which is supported.
141   setOperationAction(ISD::SELECT           , MVT::i1   , Promote);
142   setOperationAction(ISD::SELECT           , MVT::i8   , Promote);
143
144   // X86 wants to expand cmov itself.
145   setOperationAction(ISD::SELECT          , MVT::i16  , Custom);
146   setOperationAction(ISD::SELECT          , MVT::i32  , Custom);
147   setOperationAction(ISD::SELECT          , MVT::f32  , Custom);
148   setOperationAction(ISD::SELECT          , MVT::f64  , Custom);
149   setOperationAction(ISD::SETCC           , MVT::i8   , Custom);
150   setOperationAction(ISD::SETCC           , MVT::i16  , Custom);
151   setOperationAction(ISD::SETCC           , MVT::i32  , Custom);
152   setOperationAction(ISD::SETCC           , MVT::f32  , Custom);
153   setOperationAction(ISD::SETCC           , MVT::f64  , Custom);
154   // X86 ret instruction may pop stack.
155   setOperationAction(ISD::RET             , MVT::Other, Custom);
156   // Darwin ABI issue.
157   setOperationAction(ISD::ConstantPool    , MVT::i32  , Custom);
158   setOperationAction(ISD::GlobalAddress   , MVT::i32  , Custom);
159   setOperationAction(ISD::ExternalSymbol  , MVT::i32  , Custom);
160   // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
161   setOperationAction(ISD::SHL_PARTS       , MVT::i32  , Custom);
162   setOperationAction(ISD::SRA_PARTS       , MVT::i32  , Custom);
163   setOperationAction(ISD::SRL_PARTS       , MVT::i32  , Custom);
164   // X86 wants to expand memset / memcpy itself.
165   setOperationAction(ISD::MEMSET          , MVT::Other, Custom);
166   setOperationAction(ISD::MEMCPY          , MVT::Other, Custom);
167
168   // We don't have line number support yet.
169   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
170   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
171   // FIXME - use subtarget debug flags
172   if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
173     setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
174
175   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
176   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
177   
178   // Use the default implementation.
179   setOperationAction(ISD::VAARG             , MVT::Other, Expand);
180   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
181   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
182   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand); 
183   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
184   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Expand);
185
186   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
187   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
188
189   if (X86ScalarSSE) {
190     // Set up the FP register classes.
191     addRegisterClass(MVT::f32, X86::FR32RegisterClass);
192     addRegisterClass(MVT::f64, X86::FR64RegisterClass);
193
194     // SSE has no load+extend ops
195     setOperationAction(ISD::EXTLOAD,  MVT::f32, Expand);
196     setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
197
198     // Use ANDPD to simulate FABS.
199     setOperationAction(ISD::FABS , MVT::f64, Custom);
200     setOperationAction(ISD::FABS , MVT::f32, Custom);
201
202     // Use XORP to simulate FNEG.
203     setOperationAction(ISD::FNEG , MVT::f64, Custom);
204     setOperationAction(ISD::FNEG , MVT::f32, Custom);
205
206     // We don't support sin/cos/fmod
207     setOperationAction(ISD::FSIN , MVT::f64, Expand);
208     setOperationAction(ISD::FCOS , MVT::f64, Expand);
209     setOperationAction(ISD::FREM , MVT::f64, Expand);
210     setOperationAction(ISD::FSIN , MVT::f32, Expand);
211     setOperationAction(ISD::FCOS , MVT::f32, Expand);
212     setOperationAction(ISD::FREM , MVT::f32, Expand);
213
214     // Expand FP immediates into loads from the stack, except for the special
215     // cases we handle.
216     setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
217     setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
218     addLegalFPImmediate(+0.0); // xorps / xorpd
219   } else {
220     // Set up the FP register classes.
221     addRegisterClass(MVT::f64, X86::RFPRegisterClass);
222     
223     setOperationAction(ISD::UNDEF, MVT::f64, Expand);
224     
225     if (!UnsafeFPMath) {
226       setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
227       setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
228     }
229
230     setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
231     addLegalFPImmediate(+0.0); // FLD0
232     addLegalFPImmediate(+1.0); // FLD1
233     addLegalFPImmediate(-0.0); // FLD0/FCHS
234     addLegalFPImmediate(-1.0); // FLD1/FCHS
235   }
236
237   // First set operation action for all vector types to expand. Then we
238   // will selectively turn on ones that can be effectively codegen'd.
239   for (unsigned VT = (unsigned)MVT::Vector + 1;
240        VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
241     setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
242     setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
243     setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
244     setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
245   }
246
247   if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
248     addRegisterClass(MVT::v8i8,  X86::VR64RegisterClass);
249     addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
250     addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
251
252     // FIXME: add MMX packed arithmetics
253     setOperationAction(ISD::ConstantVec, MVT::v8i8,  Expand);
254     setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
255     setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
256   }
257
258   if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
259     addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
260
261     setOperationAction(ISD::ADD        , MVT::v4f32, Legal);
262     setOperationAction(ISD::SUB        , MVT::v4f32, Legal);
263     setOperationAction(ISD::MUL        , MVT::v4f32, Legal);
264     setOperationAction(ISD::LOAD       , MVT::v4f32, Legal);
265     setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
266   }
267
268   if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
269     addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
270     addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
271     addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
272     addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
273     addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
274
275
276     setOperationAction(ISD::ADD        , MVT::v2f64, Legal);
277     setOperationAction(ISD::SUB        , MVT::v2f64, Legal);
278     setOperationAction(ISD::MUL        , MVT::v2f64, Legal);
279     setOperationAction(ISD::LOAD       , MVT::v2f64, Legal);
280     setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
281     setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
282     setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
283     setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
284     setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
285   }
286
287   computeRegisterProperties();
288
289   // FIXME: These should be based on subtarget info. Plus, the values should
290   // be smaller when we are in optimizing for size mode.
291   maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
292   maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
293   maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
294   allowUnalignedMemoryAccesses = true; // x86 supports it!
295 }
296
297 std::vector<SDOperand>
298 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
299   if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
300     return LowerFastCCArguments(F, DAG);
301   return LowerCCCArguments(F, DAG);
302 }
303
304 std::pair<SDOperand, SDOperand>
305 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
306                                bool isVarArg, unsigned CallingConv,
307                                bool isTailCall,
308                                SDOperand Callee, ArgListTy &Args,
309                                SelectionDAG &DAG) {
310   assert((!isVarArg || CallingConv == CallingConv::C) &&
311          "Only C takes varargs!");
312
313   // If the callee is a GlobalAddress node (quite common, every direct call is)
314   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
315   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
316     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
317   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
318     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
319
320   if (CallingConv == CallingConv::Fast && EnableFastCC)
321     return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
322   return  LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
323 }
324
325 //===----------------------------------------------------------------------===//
326 //                    C Calling Convention implementation
327 //===----------------------------------------------------------------------===//
328
329 std::vector<SDOperand>
330 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
331   std::vector<SDOperand> ArgValues;
332
333   MachineFunction &MF = DAG.getMachineFunction();
334   MachineFrameInfo *MFI = MF.getFrameInfo();
335
336   // Add DAG nodes to load the arguments...  On entry to a function on the X86,
337   // the stack frame looks like this:
338   //
339   // [ESP] -- return address
340   // [ESP + 4] -- first argument (leftmost lexically)
341   // [ESP + 8] -- second argument, if first argument is four bytes in size
342   //    ...
343   //
344   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
345   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
346     MVT::ValueType ObjectVT = getValueType(I->getType());
347     unsigned ArgIncrement = 4;
348     unsigned ObjSize;
349     switch (ObjectVT) {
350     default: assert(0 && "Unhandled argument type!");
351     case MVT::i1:
352     case MVT::i8:  ObjSize = 1;                break;
353     case MVT::i16: ObjSize = 2;                break;
354     case MVT::i32: ObjSize = 4;                break;
355     case MVT::i64: ObjSize = ArgIncrement = 8; break;
356     case MVT::f32: ObjSize = 4;                break;
357     case MVT::f64: ObjSize = ArgIncrement = 8; break;
358     }
359     // Create the frame index object for this incoming parameter...
360     int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
361
362     // Create the SelectionDAG nodes corresponding to a load from this parameter
363     SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
364
365     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
366     // dead loads.
367     SDOperand ArgValue;
368     if (!I->use_empty())
369       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
370                              DAG.getSrcValue(NULL));
371     else {
372       if (MVT::isInteger(ObjectVT))
373         ArgValue = DAG.getConstant(0, ObjectVT);
374       else
375         ArgValue = DAG.getConstantFP(0, ObjectVT);
376     }
377     ArgValues.push_back(ArgValue);
378
379     ArgOffset += ArgIncrement;   // Move on to the next argument...
380   }
381
382   // If the function takes variable number of arguments, make a frame index for
383   // the start of the first vararg value... for expansion of llvm.va_start.
384   if (F.isVarArg())
385     VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
386   ReturnAddrIndex = 0;     // No return address slot generated yet.
387   BytesToPopOnReturn = 0;  // Callee pops nothing.
388   BytesCallerReserves = ArgOffset;
389
390   // Finally, inform the code generator which regs we return values in.
391   switch (getValueType(F.getReturnType())) {
392   default: assert(0 && "Unknown type!");
393   case MVT::isVoid: break;
394   case MVT::i1:
395   case MVT::i8:
396   case MVT::i16:
397   case MVT::i32:
398     MF.addLiveOut(X86::EAX);
399     break;
400   case MVT::i64:
401     MF.addLiveOut(X86::EAX);
402     MF.addLiveOut(X86::EDX);
403     break;
404   case MVT::f32:
405   case MVT::f64:
406     MF.addLiveOut(X86::ST0);
407     break;
408   }
409   return ArgValues;
410 }
411
412 std::pair<SDOperand, SDOperand>
413 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
414                                   bool isVarArg, bool isTailCall,
415                                   SDOperand Callee, ArgListTy &Args,
416                                   SelectionDAG &DAG) {
417   // Count how many bytes are to be pushed on the stack.
418   unsigned NumBytes = 0;
419
420   if (Args.empty()) {
421     // Save zero bytes.
422     Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
423   } else {
424     for (unsigned i = 0, e = Args.size(); i != e; ++i)
425       switch (getValueType(Args[i].second)) {
426       default: assert(0 && "Unknown value type!");
427       case MVT::i1:
428       case MVT::i8:
429       case MVT::i16:
430       case MVT::i32:
431       case MVT::f32:
432         NumBytes += 4;
433         break;
434       case MVT::i64:
435       case MVT::f64:
436         NumBytes += 8;
437         break;
438       }
439
440     Chain = DAG.getCALLSEQ_START(Chain,
441                                  DAG.getConstant(NumBytes, getPointerTy()));
442
443     // Arguments go on the stack in reverse order, as specified by the ABI.
444     unsigned ArgOffset = 0;
445     SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
446     std::vector<SDOperand> Stores;
447
448     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
449       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
450       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
451
452       switch (getValueType(Args[i].second)) {
453       default: assert(0 && "Unexpected ValueType for argument!");
454       case MVT::i1:
455       case MVT::i8:
456       case MVT::i16:
457         // Promote the integer to 32 bits.  If the input type is signed use a
458         // sign extend, otherwise use a zero extend.
459         if (Args[i].second->isSigned())
460           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
461         else
462           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
463
464         // FALL THROUGH
465       case MVT::i32:
466       case MVT::f32:
467         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
468                                      Args[i].first, PtrOff,
469                                      DAG.getSrcValue(NULL)));
470         ArgOffset += 4;
471         break;
472       case MVT::i64:
473       case MVT::f64:
474         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
475                                      Args[i].first, PtrOff,
476                                      DAG.getSrcValue(NULL)));
477         ArgOffset += 8;
478         break;
479       }
480     }
481     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
482   }
483
484   std::vector<MVT::ValueType> RetVals;
485   MVT::ValueType RetTyVT = getValueType(RetTy);
486   RetVals.push_back(MVT::Other);
487
488   // The result values produced have to be legal.  Promote the result.
489   switch (RetTyVT) {
490   case MVT::isVoid: break;
491   default:
492     RetVals.push_back(RetTyVT);
493     break;
494   case MVT::i1:
495   case MVT::i8:
496   case MVT::i16:
497     RetVals.push_back(MVT::i32);
498     break;
499   case MVT::f32:
500     if (X86ScalarSSE)
501       RetVals.push_back(MVT::f32);
502     else
503       RetVals.push_back(MVT::f64);
504     break;
505   case MVT::i64:
506     RetVals.push_back(MVT::i32);
507     RetVals.push_back(MVT::i32);
508     break;
509   }
510
511   std::vector<MVT::ValueType> NodeTys;
512   NodeTys.push_back(MVT::Other);   // Returns a chain
513   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
514   std::vector<SDOperand> Ops;
515   Ops.push_back(Chain);
516   Ops.push_back(Callee);
517
518   // FIXME: Do not generate X86ISD::TAILCALL for now.
519   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
520   SDOperand InFlag = Chain.getValue(1);
521
522   NodeTys.clear();
523   NodeTys.push_back(MVT::Other);   // Returns a chain
524   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
525   Ops.clear();
526   Ops.push_back(Chain);
527   Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
528   Ops.push_back(DAG.getConstant(0, getPointerTy()));
529   Ops.push_back(InFlag);
530   Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
531   InFlag = Chain.getValue(1);
532   
533   SDOperand RetVal;
534   if (RetTyVT != MVT::isVoid) {
535     switch (RetTyVT) {
536     default: assert(0 && "Unknown value type to return!");
537     case MVT::i1:
538     case MVT::i8:
539       RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
540       Chain = RetVal.getValue(1);
541       if (RetTyVT == MVT::i1) 
542         RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
543       break;
544     case MVT::i16:
545       RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
546       Chain = RetVal.getValue(1);
547       break;
548     case MVT::i32:
549       RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
550       Chain = RetVal.getValue(1);
551       break;
552     case MVT::i64: {
553       SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
554       SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, 
555                                         Lo.getValue(2));
556       RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
557       Chain = Hi.getValue(1);
558       break;
559     }
560     case MVT::f32:
561     case MVT::f64: {
562       std::vector<MVT::ValueType> Tys;
563       Tys.push_back(MVT::f64);
564       Tys.push_back(MVT::Other);
565       Tys.push_back(MVT::Flag);
566       std::vector<SDOperand> Ops;
567       Ops.push_back(Chain);
568       Ops.push_back(InFlag);
569       RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
570       Chain  = RetVal.getValue(1);
571       InFlag = RetVal.getValue(2);
572       if (X86ScalarSSE) {
573         // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
574         // shouldn't be necessary except that RFP cannot be live across
575         // multiple blocks. When stackifier is fixed, they can be uncoupled.
576         MachineFunction &MF = DAG.getMachineFunction();
577         int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
578         SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
579         Tys.clear();
580         Tys.push_back(MVT::Other);
581         Ops.clear();
582         Ops.push_back(Chain);
583         Ops.push_back(RetVal);
584         Ops.push_back(StackSlot);
585         Ops.push_back(DAG.getValueType(RetTyVT));
586         Ops.push_back(InFlag);
587         Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
588         RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
589                              DAG.getSrcValue(NULL));
590         Chain = RetVal.getValue(1);
591       }
592
593       if (RetTyVT == MVT::f32 && !X86ScalarSSE)
594         // FIXME: we would really like to remember that this FP_ROUND
595         // operation is okay to eliminate if we allow excess FP precision.
596         RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
597       break;
598     }
599     }
600   }
601
602   return std::make_pair(RetVal, Chain);
603 }
604
605 //===----------------------------------------------------------------------===//
606 //                    Fast Calling Convention implementation
607 //===----------------------------------------------------------------------===//
608 //
609 // The X86 'fast' calling convention passes up to two integer arguments in
610 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
611 // and requires that the callee pop its arguments off the stack (allowing proper
612 // tail calls), and has the same return value conventions as C calling convs.
613 //
614 // This calling convention always arranges for the callee pop value to be 8n+4
615 // bytes, which is needed for tail recursion elimination and stack alignment
616 // reasons.
617 //
618 // Note that this can be enhanced in the future to pass fp vals in registers
619 // (when we have a global fp allocator) and do other tricks.
620 //
621
622 /// AddLiveIn - This helper function adds the specified physical register to the
623 /// MachineFunction as a live in value.  It also creates a corresponding virtual
624 /// register for it.
625 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
626                           TargetRegisterClass *RC) {
627   assert(RC->contains(PReg) && "Not the correct regclass!");
628   unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
629   MF.addLiveIn(PReg, VReg);
630   return VReg;
631 }
632
633
634 std::vector<SDOperand>
635 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
636   std::vector<SDOperand> ArgValues;
637
638   MachineFunction &MF = DAG.getMachineFunction();
639   MachineFrameInfo *MFI = MF.getFrameInfo();
640
641   // Add DAG nodes to load the arguments...  On entry to a function the stack
642   // frame looks like this:
643   //
644   // [ESP] -- return address
645   // [ESP + 4] -- first nonreg argument (leftmost lexically)
646   // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
647   //    ...
648   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
649
650   // Keep track of the number of integer regs passed so far.  This can be either
651   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
652   // used).
653   unsigned NumIntRegs = 0;
654
655   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
656     MVT::ValueType ObjectVT = getValueType(I->getType());
657     unsigned ArgIncrement = 4;
658     unsigned ObjSize = 0;
659     SDOperand ArgValue;
660
661     switch (ObjectVT) {
662     default: assert(0 && "Unhandled argument type!");
663     case MVT::i1:
664     case MVT::i8:
665       if (NumIntRegs < 2) {
666         if (!I->use_empty()) {
667           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
668                                     X86::R8RegisterClass);
669           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
670           DAG.setRoot(ArgValue.getValue(1));
671           if (ObjectVT == MVT::i1)
672             // FIXME: Should insert a assertzext here.
673             ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
674         }
675         ++NumIntRegs;
676         break;
677       }
678
679       ObjSize = 1;
680       break;
681     case MVT::i16:
682       if (NumIntRegs < 2) {
683         if (!I->use_empty()) {
684           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
685                                     X86::R16RegisterClass);
686           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
687           DAG.setRoot(ArgValue.getValue(1));
688         }
689         ++NumIntRegs;
690         break;
691       }
692       ObjSize = 2;
693       break;
694     case MVT::i32:
695       if (NumIntRegs < 2) {
696         if (!I->use_empty()) {
697           unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
698                                     X86::R32RegisterClass);
699           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
700           DAG.setRoot(ArgValue.getValue(1));
701         }
702         ++NumIntRegs;
703         break;
704       }
705       ObjSize = 4;
706       break;
707     case MVT::i64:
708       if (NumIntRegs == 0) {
709         if (!I->use_empty()) {
710           unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
711           unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
712
713           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
714           SDOperand Hi  = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
715           DAG.setRoot(Hi.getValue(1));
716
717           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
718         }
719         NumIntRegs = 2;
720         break;
721       } else if (NumIntRegs == 1) {
722         if (!I->use_empty()) {
723           unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
724           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
725           DAG.setRoot(Low.getValue(1));
726
727           // Load the high part from memory.
728           // Create the frame index object for this incoming parameter...
729           int FI = MFI->CreateFixedObject(4, ArgOffset);
730           SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
731           SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
732                                      DAG.getSrcValue(NULL));
733           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
734         }
735         ArgOffset += 4;
736         NumIntRegs = 2;
737         break;
738       }
739       ObjSize = ArgIncrement = 8;
740       break;
741     case MVT::f32: ObjSize = 4;                break;
742     case MVT::f64: ObjSize = ArgIncrement = 8; break;
743     }
744
745     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
746     // dead loads.
747     if (ObjSize && !I->use_empty()) {
748       // Create the frame index object for this incoming parameter...
749       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
750
751       // Create the SelectionDAG nodes corresponding to a load from this
752       // parameter.
753       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
754
755       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
756                              DAG.getSrcValue(NULL));
757     } else if (ArgValue.Val == 0) {
758       if (MVT::isInteger(ObjectVT))
759         ArgValue = DAG.getConstant(0, ObjectVT);
760       else
761         ArgValue = DAG.getConstantFP(0, ObjectVT);
762     }
763     ArgValues.push_back(ArgValue);
764
765     if (ObjSize)
766       ArgOffset += ArgIncrement;   // Move on to the next argument.
767   }
768
769   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
770   // arguments and the arguments after the retaddr has been pushed are aligned.
771   if ((ArgOffset & 7) == 0)
772     ArgOffset += 4;
773
774   VarArgsFrameIndex = 0xAAAAAAA;   // fastcc functions can't have varargs.
775   ReturnAddrIndex = 0;             // No return address slot generated yet.
776   BytesToPopOnReturn = ArgOffset;  // Callee pops all stack arguments.
777   BytesCallerReserves = 0;
778
779   // Finally, inform the code generator which regs we return values in.
780   switch (getValueType(F.getReturnType())) {
781   default: assert(0 && "Unknown type!");
782   case MVT::isVoid: break;
783   case MVT::i1:
784   case MVT::i8:
785   case MVT::i16:
786   case MVT::i32:
787     MF.addLiveOut(X86::EAX);
788     break;
789   case MVT::i64:
790     MF.addLiveOut(X86::EAX);
791     MF.addLiveOut(X86::EDX);
792     break;
793   case MVT::f32:
794   case MVT::f64:
795     MF.addLiveOut(X86::ST0);
796     break;
797   }
798   return ArgValues;
799 }
800
801 std::pair<SDOperand, SDOperand>
802 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
803                                      bool isTailCall, SDOperand Callee,
804                                      ArgListTy &Args, SelectionDAG &DAG) {
805   // Count how many bytes are to be pushed on the stack.
806   unsigned NumBytes = 0;
807
808   // Keep track of the number of integer regs passed so far.  This can be either
809   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
810   // used).
811   unsigned NumIntRegs = 0;
812
813   for (unsigned i = 0, e = Args.size(); i != e; ++i)
814     switch (getValueType(Args[i].second)) {
815     default: assert(0 && "Unknown value type!");
816     case MVT::i1:
817     case MVT::i8:
818     case MVT::i16:
819     case MVT::i32:
820       if (NumIntRegs < 2) {
821         ++NumIntRegs;
822         break;
823       }
824       // fall through
825     case MVT::f32:
826       NumBytes += 4;
827       break;
828     case MVT::i64:
829       if (NumIntRegs == 0) {
830         NumIntRegs = 2;
831         break;
832       } else if (NumIntRegs == 1) {
833         NumIntRegs = 2;
834         NumBytes += 4;
835         break;
836       }
837
838       // fall through
839     case MVT::f64:
840       NumBytes += 8;
841       break;
842     }
843
844   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
845   // arguments and the arguments after the retaddr has been pushed are aligned.
846   if ((NumBytes & 7) == 0)
847     NumBytes += 4;
848
849   Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
850
851   // Arguments go on the stack in reverse order, as specified by the ABI.
852   unsigned ArgOffset = 0;
853   SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
854   NumIntRegs = 0;
855   std::vector<SDOperand> Stores;
856   std::vector<SDOperand> RegValuesToPass;
857   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
858     switch (getValueType(Args[i].second)) {
859     default: assert(0 && "Unexpected ValueType for argument!");
860     case MVT::i1:
861       Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
862       // Fall through.
863     case MVT::i8:
864     case MVT::i16:
865     case MVT::i32:
866       if (NumIntRegs < 2) {
867         RegValuesToPass.push_back(Args[i].first);
868         ++NumIntRegs;
869         break;
870       }
871       // Fall through
872     case MVT::f32: {
873       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
874       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
875       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
876                                    Args[i].first, PtrOff,
877                                    DAG.getSrcValue(NULL)));
878       ArgOffset += 4;
879       break;
880     }
881     case MVT::i64:
882       if (NumIntRegs < 2) {    // Can pass part of it in regs?
883         SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
884                                    Args[i].first, DAG.getConstant(1, MVT::i32));
885         SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
886                                    Args[i].first, DAG.getConstant(0, MVT::i32));
887         RegValuesToPass.push_back(Lo);
888         ++NumIntRegs;
889         if (NumIntRegs < 2) {   // Pass both parts in regs?
890           RegValuesToPass.push_back(Hi);
891           ++NumIntRegs;
892         } else {
893           // Pass the high part in memory.
894           SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
895           PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
896           Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
897                                        Hi, PtrOff, DAG.getSrcValue(NULL)));
898           ArgOffset += 4;
899         }
900         break;
901       }
902       // Fall through
903     case MVT::f64:
904       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
905       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
906       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
907                                    Args[i].first, PtrOff,
908                                    DAG.getSrcValue(NULL)));
909       ArgOffset += 8;
910       break;
911     }
912   }
913   if (!Stores.empty())
914     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
915
916   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
917   // arguments and the arguments after the retaddr has been pushed are aligned.
918   if ((ArgOffset & 7) == 0)
919     ArgOffset += 4;
920
921   std::vector<MVT::ValueType> RetVals;
922   MVT::ValueType RetTyVT = getValueType(RetTy);
923
924   RetVals.push_back(MVT::Other);
925
926   // The result values produced have to be legal.  Promote the result.
927   switch (RetTyVT) {
928   case MVT::isVoid: break;
929   default:
930     RetVals.push_back(RetTyVT);
931     break;
932   case MVT::i1:
933   case MVT::i8:
934   case MVT::i16:
935     RetVals.push_back(MVT::i32);
936     break;
937   case MVT::f32:
938     if (X86ScalarSSE)
939       RetVals.push_back(MVT::f32);
940     else
941       RetVals.push_back(MVT::f64);
942     break;
943   case MVT::i64:
944     RetVals.push_back(MVT::i32);
945     RetVals.push_back(MVT::i32);
946     break;
947   }
948
949   // Build a sequence of copy-to-reg nodes chained together with token chain
950   // and flag operands which copy the outgoing args into registers.
951   SDOperand InFlag;
952   for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
953     unsigned CCReg;
954     SDOperand RegToPass = RegValuesToPass[i];
955     switch (RegToPass.getValueType()) {
956     default: assert(0 && "Bad thing to pass in regs");
957     case MVT::i8:
958       CCReg = (i == 0) ? X86::AL  : X86::DL;
959       break;
960     case MVT::i16:
961       CCReg = (i == 0) ? X86::AX  : X86::DX;
962       break;
963     case MVT::i32:
964       CCReg = (i == 0) ? X86::EAX : X86::EDX;
965       break;
966     }
967
968     Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
969     InFlag = Chain.getValue(1);
970   }
971
972   std::vector<MVT::ValueType> NodeTys;
973   NodeTys.push_back(MVT::Other);   // Returns a chain
974   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
975   std::vector<SDOperand> Ops;
976   Ops.push_back(Chain);
977   Ops.push_back(Callee);
978   if (InFlag.Val)
979     Ops.push_back(InFlag);
980
981   // FIXME: Do not generate X86ISD::TAILCALL for now.
982   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
983   InFlag = Chain.getValue(1);
984
985   NodeTys.clear();
986   NodeTys.push_back(MVT::Other);   // Returns a chain
987   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
988   Ops.clear();
989   Ops.push_back(Chain);
990   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
991   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
992   Ops.push_back(InFlag);
993   Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
994   InFlag = Chain.getValue(1);
995   
996   SDOperand RetVal;
997   if (RetTyVT != MVT::isVoid) {
998     switch (RetTyVT) {
999     default: assert(0 && "Unknown value type to return!");
1000     case MVT::i1:
1001     case MVT::i8:
1002       RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1003       Chain = RetVal.getValue(1);
1004       if (RetTyVT == MVT::i1) 
1005         RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1006       break;
1007     case MVT::i16:
1008       RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1009       Chain = RetVal.getValue(1);
1010       break;
1011     case MVT::i32:
1012       RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1013       Chain = RetVal.getValue(1);
1014       break;
1015     case MVT::i64: {
1016       SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1017       SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, 
1018                                         Lo.getValue(2));
1019       RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1020       Chain = Hi.getValue(1);
1021       break;
1022     }
1023     case MVT::f32:
1024     case MVT::f64: {
1025       std::vector<MVT::ValueType> Tys;
1026       Tys.push_back(MVT::f64);
1027       Tys.push_back(MVT::Other);
1028       Tys.push_back(MVT::Flag);
1029       std::vector<SDOperand> Ops;
1030       Ops.push_back(Chain);
1031       Ops.push_back(InFlag);
1032       RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1033       Chain  = RetVal.getValue(1);
1034       InFlag = RetVal.getValue(2);
1035       if (X86ScalarSSE) {
1036         // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1037         // shouldn't be necessary except that RFP cannot be live across
1038         // multiple blocks. When stackifier is fixed, they can be uncoupled.
1039         MachineFunction &MF = DAG.getMachineFunction();
1040         int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1041         SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1042         Tys.clear();
1043         Tys.push_back(MVT::Other);
1044         Ops.clear();
1045         Ops.push_back(Chain);
1046         Ops.push_back(RetVal);
1047         Ops.push_back(StackSlot);
1048         Ops.push_back(DAG.getValueType(RetTyVT));
1049         Ops.push_back(InFlag);
1050         Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1051         RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1052                              DAG.getSrcValue(NULL));
1053         Chain = RetVal.getValue(1);
1054       }
1055
1056       if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1057         // FIXME: we would really like to remember that this FP_ROUND
1058         // operation is okay to eliminate if we allow excess FP precision.
1059         RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1060       break;
1061     }
1062     }
1063   }
1064
1065   return std::make_pair(RetVal, Chain);
1066 }
1067
1068 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1069   if (ReturnAddrIndex == 0) {
1070     // Set up a frame object for the return address.
1071     MachineFunction &MF = DAG.getMachineFunction();
1072     ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1073   }
1074
1075   return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1076 }
1077
1078
1079
1080 std::pair<SDOperand, SDOperand> X86TargetLowering::
1081 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1082                         SelectionDAG &DAG) {
1083   SDOperand Result;
1084   if (Depth)        // Depths > 0 not supported yet!
1085     Result = DAG.getConstant(0, getPointerTy());
1086   else {
1087     SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1088     if (!isFrameAddress)
1089       // Just load the return address
1090       Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1091                            DAG.getSrcValue(NULL));
1092     else
1093       Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1094                            DAG.getConstant(4, MVT::i32));
1095   }
1096   return std::make_pair(Result, Chain);
1097 }
1098
1099 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1100 /// which corresponds to the condition code.
1101 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1102   switch (X86CC) {
1103   default: assert(0 && "Unknown X86 conditional code!");
1104   case X86ISD::COND_A:  return X86::JA;
1105   case X86ISD::COND_AE: return X86::JAE;
1106   case X86ISD::COND_B:  return X86::JB;
1107   case X86ISD::COND_BE: return X86::JBE;
1108   case X86ISD::COND_E:  return X86::JE;
1109   case X86ISD::COND_G:  return X86::JG;
1110   case X86ISD::COND_GE: return X86::JGE;
1111   case X86ISD::COND_L:  return X86::JL;
1112   case X86ISD::COND_LE: return X86::JLE;
1113   case X86ISD::COND_NE: return X86::JNE;
1114   case X86ISD::COND_NO: return X86::JNO;
1115   case X86ISD::COND_NP: return X86::JNP;
1116   case X86ISD::COND_NS: return X86::JNS;
1117   case X86ISD::COND_O:  return X86::JO;
1118   case X86ISD::COND_P:  return X86::JP;
1119   case X86ISD::COND_S:  return X86::JS;
1120   }
1121 }
1122
1123 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1124 /// specific condition code. It returns a false if it cannot do a direct
1125 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1126 /// the order of comparison operands should be flipped.
1127 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1128                            bool &Flip) {
1129   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1130   Flip = false;
1131   X86CC = X86ISD::COND_INVALID;
1132   if (!isFP) {
1133     switch (SetCCOpcode) {
1134     default: break;
1135     case ISD::SETEQ:  X86CC = X86ISD::COND_E;  break;
1136     case ISD::SETGT:  X86CC = X86ISD::COND_G;  break;
1137     case ISD::SETGE:  X86CC = X86ISD::COND_GE; break;
1138     case ISD::SETLT:  X86CC = X86ISD::COND_L;  break;
1139     case ISD::SETLE:  X86CC = X86ISD::COND_LE; break;
1140     case ISD::SETNE:  X86CC = X86ISD::COND_NE; break;
1141     case ISD::SETULT: X86CC = X86ISD::COND_B;  break;
1142     case ISD::SETUGT: X86CC = X86ISD::COND_A;  break;
1143     case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1144     case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1145     }
1146   } else {
1147     // On a floating point condition, the flags are set as follows:
1148     // ZF  PF  CF   op
1149     //  0 | 0 | 0 | X > Y
1150     //  0 | 0 | 1 | X < Y
1151     //  1 | 0 | 0 | X == Y
1152     //  1 | 1 | 1 | unordered
1153     switch (SetCCOpcode) {
1154     default: break;
1155     case ISD::SETUEQ:
1156     case ISD::SETEQ: X86CC = X86ISD::COND_E;  break;
1157     case ISD::SETOLE: Flip = true; // Fallthrough
1158     case ISD::SETOGT:
1159     case ISD::SETGT: X86CC = X86ISD::COND_A;  break;
1160     case ISD::SETOLT: Flip = true; // Fallthrough
1161     case ISD::SETOGE:
1162     case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1163     case ISD::SETUGE: Flip = true; // Fallthrough
1164     case ISD::SETULT:
1165     case ISD::SETLT: X86CC = X86ISD::COND_B;  break;
1166     case ISD::SETUGT: Flip = true; // Fallthrough
1167     case ISD::SETULE:
1168     case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1169     case ISD::SETONE:
1170     case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1171     case ISD::SETUO: X86CC = X86ISD::COND_P;  break;
1172     case ISD::SETO:  X86CC = X86ISD::COND_NP; break;
1173     }
1174   }
1175
1176   return X86CC != X86ISD::COND_INVALID;
1177 }
1178
1179 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1180 /// code. Current x86 isa includes the following FP cmov instructions:
1181 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1182 static bool hasFPCMov(unsigned X86CC) {
1183   switch (X86CC) {
1184   default:
1185     return false;
1186   case X86ISD::COND_B:
1187   case X86ISD::COND_BE:
1188   case X86ISD::COND_E:
1189   case X86ISD::COND_P:
1190   case X86ISD::COND_A:
1191   case X86ISD::COND_AE:
1192   case X86ISD::COND_NE:
1193   case X86ISD::COND_NP:
1194     return true;
1195   }
1196 }
1197
1198 MachineBasicBlock *
1199 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1200                                            MachineBasicBlock *BB) {
1201   switch (MI->getOpcode()) {
1202   default: assert(false && "Unexpected instr type to insert");
1203   case X86::CMOV_FR32:
1204   case X86::CMOV_FR64: {
1205     // To "insert" a SELECT_CC instruction, we actually have to insert the
1206     // diamond control-flow pattern.  The incoming instruction knows the
1207     // destination vreg to set, the condition code register to branch on, the
1208     // true/false values to select between, and a branch opcode to use.
1209     const BasicBlock *LLVM_BB = BB->getBasicBlock();
1210     ilist<MachineBasicBlock>::iterator It = BB;
1211     ++It;
1212   
1213     //  thisMBB:
1214     //  ...
1215     //   TrueVal = ...
1216     //   cmpTY ccX, r1, r2
1217     //   bCC copy1MBB
1218     //   fallthrough --> copy0MBB
1219     MachineBasicBlock *thisMBB = BB;
1220     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1221     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1222     unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1223     BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1224     MachineFunction *F = BB->getParent();
1225     F->getBasicBlockList().insert(It, copy0MBB);
1226     F->getBasicBlockList().insert(It, sinkMBB);
1227     // Update machine-CFG edges
1228     BB->addSuccessor(copy0MBB);
1229     BB->addSuccessor(sinkMBB);
1230   
1231     //  copy0MBB:
1232     //   %FalseValue = ...
1233     //   # fallthrough to sinkMBB
1234     BB = copy0MBB;
1235   
1236     // Update machine-CFG edges
1237     BB->addSuccessor(sinkMBB);
1238   
1239     //  sinkMBB:
1240     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1241     //  ...
1242     BB = sinkMBB;
1243     BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1244       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1245       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1246
1247     delete MI;   // The pseudo instruction is gone now.
1248     return BB;
1249   }
1250
1251   case X86::FP_TO_INT16_IN_MEM:
1252   case X86::FP_TO_INT32_IN_MEM:
1253   case X86::FP_TO_INT64_IN_MEM: {
1254     // Change the floating point control register to use "round towards zero"
1255     // mode when truncating to an integer value.
1256     MachineFunction *F = BB->getParent();
1257     int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1258     addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1259
1260     // Load the old value of the high byte of the control word...
1261     unsigned OldCW =
1262       F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1263     addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1264
1265     // Set the high part to be round to zero...
1266     addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1267
1268     // Reload the modified control word now...
1269     addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1270
1271     // Restore the memory image of control word to original value
1272     addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1273
1274     // Get the X86 opcode to use.
1275     unsigned Opc;
1276     switch (MI->getOpcode()) {
1277     default: assert(0 && "illegal opcode!");
1278     case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1279     case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1280     case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1281     }
1282
1283     X86AddressMode AM;
1284     MachineOperand &Op = MI->getOperand(0);
1285     if (Op.isRegister()) {
1286       AM.BaseType = X86AddressMode::RegBase;
1287       AM.Base.Reg = Op.getReg();
1288     } else {
1289       AM.BaseType = X86AddressMode::FrameIndexBase;
1290       AM.Base.FrameIndex = Op.getFrameIndex();
1291     }
1292     Op = MI->getOperand(1);
1293     if (Op.isImmediate())
1294       AM.Scale = Op.getImmedValue();
1295     Op = MI->getOperand(2);
1296     if (Op.isImmediate())
1297       AM.IndexReg = Op.getImmedValue();
1298     Op = MI->getOperand(3);
1299     if (Op.isGlobalAddress()) {
1300       AM.GV = Op.getGlobal();
1301     } else {
1302       AM.Disp = Op.getImmedValue();
1303     }
1304     addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1305
1306     // Reload the original control word now.
1307     addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1308
1309     delete MI;   // The pseudo instruction is gone now.
1310     return BB;
1311   }
1312   }
1313 }
1314
1315
1316 //===----------------------------------------------------------------------===//
1317 //                           X86 Custom Lowering Hooks
1318 //===----------------------------------------------------------------------===//
1319
1320 /// LowerOperation - Provide custom lowering hooks for some operations.
1321 ///
1322 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1323   switch (Op.getOpcode()) {
1324   default: assert(0 && "Should not custom lower this!");
1325   case ISD::SHL_PARTS:
1326   case ISD::SRA_PARTS:
1327   case ISD::SRL_PARTS: {
1328     assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1329            "Not an i64 shift!");
1330     bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1331     SDOperand ShOpLo = Op.getOperand(0);
1332     SDOperand ShOpHi = Op.getOperand(1);
1333     SDOperand ShAmt  = Op.getOperand(2);
1334     SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1335                                          DAG.getConstant(31, MVT::i8))
1336                            : DAG.getConstant(0, MVT::i32);
1337
1338     SDOperand Tmp2, Tmp3;
1339     if (Op.getOpcode() == ISD::SHL_PARTS) {
1340       Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1341       Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1342     } else {
1343       Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1344       Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1345     }
1346
1347     SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1348                                    ShAmt, DAG.getConstant(32, MVT::i8));
1349
1350     SDOperand Hi, Lo;
1351     SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1352
1353     std::vector<MVT::ValueType> Tys;
1354     Tys.push_back(MVT::i32);
1355     Tys.push_back(MVT::Flag);
1356     std::vector<SDOperand> Ops;
1357     if (Op.getOpcode() == ISD::SHL_PARTS) {
1358       Ops.push_back(Tmp2);
1359       Ops.push_back(Tmp3);
1360       Ops.push_back(CC);
1361       Ops.push_back(InFlag);
1362       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1363       InFlag = Hi.getValue(1);
1364
1365       Ops.clear();
1366       Ops.push_back(Tmp3);
1367       Ops.push_back(Tmp1);
1368       Ops.push_back(CC);
1369       Ops.push_back(InFlag);
1370       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1371     } else {
1372       Ops.push_back(Tmp2);
1373       Ops.push_back(Tmp3);
1374       Ops.push_back(CC);
1375       Ops.push_back(InFlag);
1376       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1377       InFlag = Lo.getValue(1);
1378
1379       Ops.clear();
1380       Ops.push_back(Tmp3);
1381       Ops.push_back(Tmp1);
1382       Ops.push_back(CC);
1383       Ops.push_back(InFlag);
1384       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1385     }
1386
1387     Tys.clear();
1388     Tys.push_back(MVT::i32);
1389     Tys.push_back(MVT::i32);
1390     Ops.clear();
1391     Ops.push_back(Lo);
1392     Ops.push_back(Hi);
1393     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1394   }
1395   case ISD::SINT_TO_FP: {
1396     assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1397            Op.getOperand(0).getValueType() >= MVT::i16 &&
1398            "Unknown SINT_TO_FP to lower!");
1399
1400     SDOperand Result;
1401     MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1402     unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1403     MachineFunction &MF = DAG.getMachineFunction();
1404     int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1405     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1406     SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1407                                   DAG.getEntryNode(), Op.getOperand(0),
1408                                   StackSlot, DAG.getSrcValue(NULL));
1409
1410     // Build the FILD
1411     std::vector<MVT::ValueType> Tys;
1412     Tys.push_back(MVT::f64);
1413     Tys.push_back(MVT::Other);
1414     if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1415     std::vector<SDOperand> Ops;
1416     Ops.push_back(Chain);
1417     Ops.push_back(StackSlot);
1418     Ops.push_back(DAG.getValueType(SrcVT));
1419     Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1420                          Tys, Ops);
1421
1422     if (X86ScalarSSE) {
1423       Chain = Result.getValue(1);
1424       SDOperand InFlag = Result.getValue(2);
1425
1426       // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1427       // shouldn't be necessary except that RFP cannot be live across
1428       // multiple blocks. When stackifier is fixed, they can be uncoupled.
1429       MachineFunction &MF = DAG.getMachineFunction();
1430       int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1431       SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1432       std::vector<MVT::ValueType> Tys;
1433       Tys.push_back(MVT::Other);
1434       std::vector<SDOperand> Ops;
1435       Ops.push_back(Chain);
1436       Ops.push_back(Result);
1437       Ops.push_back(StackSlot);
1438       Ops.push_back(DAG.getValueType(Op.getValueType()));
1439       Ops.push_back(InFlag);
1440       Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1441       Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1442                            DAG.getSrcValue(NULL));
1443     }
1444
1445     return Result;
1446   }
1447   case ISD::FP_TO_SINT: {
1448     assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1449            "Unknown FP_TO_SINT to lower!");
1450     // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1451     // stack slot.
1452     MachineFunction &MF = DAG.getMachineFunction();
1453     unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1454     int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1455     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1456
1457     unsigned Opc;
1458     switch (Op.getValueType()) {
1459     default: assert(0 && "Invalid FP_TO_SINT to lower!");
1460     case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1461     case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1462     case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1463     }
1464
1465     SDOperand Chain = DAG.getEntryNode();
1466     SDOperand Value = Op.getOperand(0);
1467     if (X86ScalarSSE) {
1468       assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1469       Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, 
1470                           DAG.getSrcValue(0));
1471       std::vector<MVT::ValueType> Tys;
1472       Tys.push_back(MVT::f64);
1473       Tys.push_back(MVT::Other);
1474       std::vector<SDOperand> Ops;
1475       Ops.push_back(Chain);
1476       Ops.push_back(StackSlot);
1477       Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
1478       Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1479       Chain = Value.getValue(1);
1480       SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1481       StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1482     }
1483
1484     // Build the FP_TO_INT*_IN_MEM
1485     std::vector<SDOperand> Ops;
1486     Ops.push_back(Chain);
1487     Ops.push_back(Value);
1488     Ops.push_back(StackSlot);
1489     SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1490
1491     // Load the result.
1492     return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1493                        DAG.getSrcValue(NULL));
1494   }
1495   case ISD::READCYCLECOUNTER: {
1496     std::vector<MVT::ValueType> Tys;
1497     Tys.push_back(MVT::Other);
1498     Tys.push_back(MVT::Flag);
1499     std::vector<SDOperand> Ops;
1500     Ops.push_back(Op.getOperand(0));
1501     SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
1502     Ops.clear();
1503     Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1504     Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, 
1505                                      MVT::i32, Ops[0].getValue(2)));
1506     Ops.push_back(Ops[1].getValue(1));
1507     Tys[0] = Tys[1] = MVT::i32;
1508     Tys.push_back(MVT::Other);
1509     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1510   }
1511   case ISD::FABS: {
1512     MVT::ValueType VT = Op.getValueType();
1513     const Type *OpNTy =  MVT::getTypeForValueType(VT);
1514     std::vector<Constant*> CV;
1515     if (VT == MVT::f64) {
1516       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1517       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1518     } else {
1519       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1520       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1521       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1522       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1523     }
1524     Constant *CS = ConstantStruct::get(CV);
1525     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1526     SDOperand Mask 
1527       = DAG.getNode(X86ISD::LOAD_PACK,
1528                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1529     return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1530   }
1531   case ISD::FNEG: {
1532     MVT::ValueType VT = Op.getValueType();
1533     const Type *OpNTy =  MVT::getTypeForValueType(VT);
1534     std::vector<Constant*> CV;
1535     if (VT == MVT::f64) {
1536       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1537       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1538     } else {
1539       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1540       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1541       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1542       CV.push_back(ConstantFP::get(OpNTy, 0.0));
1543     }
1544     Constant *CS = ConstantStruct::get(CV);
1545     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1546     SDOperand Mask 
1547       = DAG.getNode(X86ISD::LOAD_PACK,
1548                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1549     return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1550   }
1551   case ISD::SETCC: {
1552     assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
1553     SDOperand Cond;
1554     SDOperand CC = Op.getOperand(2);
1555     ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1556     bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
1557     bool Flip;
1558     unsigned X86CC;
1559     if (translateX86CC(CC, isFP, X86CC, Flip)) {
1560       if (Flip)
1561         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1562                            Op.getOperand(1), Op.getOperand(0));
1563       else
1564         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1565                            Op.getOperand(0), Op.getOperand(1));
1566       return DAG.getNode(X86ISD::SETCC, MVT::i8, 
1567                          DAG.getConstant(X86CC, MVT::i8), Cond);
1568     } else {
1569       assert(isFP && "Illegal integer SetCC!");
1570
1571       Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1572                          Op.getOperand(0), Op.getOperand(1));
1573       std::vector<MVT::ValueType> Tys;
1574       std::vector<SDOperand> Ops;
1575       switch (SetCCOpcode) {
1576       default: assert(false && "Illegal floating point SetCC!");
1577       case ISD::SETOEQ: {  // !PF & ZF
1578         Tys.push_back(MVT::i8);
1579         Tys.push_back(MVT::Flag);
1580         Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1581         Ops.push_back(Cond);
1582         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1583         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1584                                      DAG.getConstant(X86ISD::COND_E, MVT::i8),
1585                                      Tmp1.getValue(1));
1586         return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1587       }
1588       case ISD::SETUNE: {  // PF | !ZF
1589         Tys.push_back(MVT::i8);
1590         Tys.push_back(MVT::Flag);
1591         Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1592         Ops.push_back(Cond);
1593         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1594         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1595                                      DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1596                                      Tmp1.getValue(1));
1597         return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1598       }
1599       }
1600     }
1601   }
1602   case ISD::SELECT: {
1603     MVT::ValueType VT = Op.getValueType();
1604     bool isFP      = MVT::isFloatingPoint(VT);
1605     bool isFPStack = isFP && !X86ScalarSSE;
1606     bool isFPSSE   = isFP && X86ScalarSSE;
1607     bool addTest   = false;
1608     SDOperand Op0 = Op.getOperand(0);
1609     SDOperand Cond, CC;
1610     if (Op0.getOpcode() == ISD::SETCC)
1611       Op0 = LowerOperation(Op0, DAG);
1612
1613     if (Op0.getOpcode() == X86ISD::SETCC) {
1614       // If condition flag is set by a X86ISD::CMP, then make a copy of it
1615       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1616       // have another use it will be eliminated.
1617       // If the X86ISD::SETCC has more than one use, then it's probably better
1618       // to use a test instead of duplicating the X86ISD::CMP (for register
1619       // pressure reason).
1620       if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1621         if (!Op0.hasOneUse()) {
1622           std::vector<MVT::ValueType> Tys;
1623           for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1624             Tys.push_back(Op0.Val->getValueType(i));
1625           std::vector<SDOperand> Ops;
1626           for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1627             Ops.push_back(Op0.getOperand(i));
1628           Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1629         }
1630
1631         CC   = Op0.getOperand(0);
1632         Cond = Op0.getOperand(1);
1633         // Make a copy as flag result cannot be used by more than one.
1634         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1635                            Cond.getOperand(0), Cond.getOperand(1));
1636         addTest =
1637           isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
1638       } else
1639         addTest = true;
1640     } else
1641       addTest = true;
1642
1643     if (addTest) {
1644       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1645       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
1646     }
1647
1648     std::vector<MVT::ValueType> Tys;
1649     Tys.push_back(Op.getValueType());
1650     Tys.push_back(MVT::Flag);
1651     std::vector<SDOperand> Ops;
1652     // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1653     // condition is true.
1654     Ops.push_back(Op.getOperand(2));
1655     Ops.push_back(Op.getOperand(1));
1656     Ops.push_back(CC);
1657     Ops.push_back(Cond);
1658     return DAG.getNode(X86ISD::CMOV, Tys, Ops);
1659   }
1660   case ISD::BRCOND: {
1661     bool addTest = false;
1662     SDOperand Cond  = Op.getOperand(1);
1663     SDOperand Dest  = Op.getOperand(2);
1664     SDOperand CC;
1665     if (Cond.getOpcode() == ISD::SETCC)
1666       Cond = LowerOperation(Cond, DAG);
1667
1668     if (Cond.getOpcode() == X86ISD::SETCC) {
1669       // If condition flag is set by a X86ISD::CMP, then make a copy of it
1670       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1671       // have another use it will be eliminated.
1672       // If the X86ISD::SETCC has more than one use, then it's probably better
1673       // to use a test instead of duplicating the X86ISD::CMP (for register
1674       // pressure reason).
1675       if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1676         if (!Cond.hasOneUse()) {
1677           std::vector<MVT::ValueType> Tys;
1678           for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1679             Tys.push_back(Cond.Val->getValueType(i));
1680           std::vector<SDOperand> Ops;
1681           for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1682             Ops.push_back(Cond.getOperand(i));
1683           Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1684         }
1685
1686         CC   = Cond.getOperand(0);
1687         Cond = Cond.getOperand(1);
1688         // Make a copy as flag result cannot be used by more than one.
1689         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1690                            Cond.getOperand(0), Cond.getOperand(1));
1691       } else
1692         addTest = true;
1693     } else
1694       addTest = true;
1695
1696     if (addTest) {
1697       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1698       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1699     }
1700     return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1701                        Op.getOperand(0), Op.getOperand(2), CC, Cond);
1702   }
1703   case ISD::MEMSET: {
1704     SDOperand InFlag(0, 0);
1705     SDOperand Chain = Op.getOperand(0);
1706     unsigned Align =
1707       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1708     if (Align == 0) Align = 1;
1709
1710     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1711     // If not DWORD aligned, call memset if size is less than the threshold.
1712     // It knows how to align to the right boundary first.
1713     if ((Align & 3) != 0 ||
1714         !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1715       MVT::ValueType IntPtr = getPointerTy();
1716       const Type *IntPtrTy = getTargetData().getIntPtrType();
1717       std::vector<std::pair<SDOperand, const Type*> > Args;
1718       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1719       // Extend the ubyte argument to be an int value for the call.
1720       SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1721       Args.push_back(std::make_pair(Val, IntPtrTy));
1722       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1723       std::pair<SDOperand,SDOperand> CallResult =
1724         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1725                     DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1726       return CallResult.second;
1727     }
1728
1729     MVT::ValueType AVT;
1730     SDOperand Count;
1731     ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1732     unsigned BytesLeft = 0;
1733     if (ValC) {
1734       unsigned ValReg;
1735       unsigned Val = ValC->getValue() & 255;
1736
1737       // If the value is a constant, then we can potentially use larger sets.
1738       switch (Align & 3) {
1739       case 2:   // WORD aligned
1740         AVT = MVT::i16;
1741         Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1742         BytesLeft = I->getValue() % 2;
1743         Val    = (Val << 8) | Val;
1744         ValReg = X86::AX;
1745         break;
1746       case 0:   // DWORD aligned
1747         AVT = MVT::i32;
1748         Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1749         BytesLeft = I->getValue() % 4;
1750         Val = (Val << 8)  | Val;
1751         Val = (Val << 16) | Val;
1752         ValReg = X86::EAX;
1753         break;
1754       default:  // Byte aligned
1755         AVT = MVT::i8;
1756         Count = Op.getOperand(3);
1757         ValReg = X86::AL;
1758         break;
1759       }
1760
1761       Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1762                                 InFlag);
1763       InFlag = Chain.getValue(1);
1764     } else {
1765       AVT = MVT::i8;
1766       Count  = Op.getOperand(3);
1767       Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1768       InFlag = Chain.getValue(1);
1769     }
1770
1771     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1772     InFlag = Chain.getValue(1);
1773     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1774     InFlag = Chain.getValue(1);
1775     Chain  = DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1776                          DAG.getValueType(AVT), InFlag);
1777
1778     if (BytesLeft) {
1779       // Issue stores for the last 1 - 3 bytes.
1780       SDOperand Value;
1781       unsigned Val = ValC->getValue() & 255;
1782       unsigned Offset = I->getValue() - BytesLeft;
1783       SDOperand DstAddr = Op.getOperand(1);
1784       MVT::ValueType AddrVT = DstAddr.getValueType();
1785       if (BytesLeft >= 2) {
1786         Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1787         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1788                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1789                                         DAG.getConstant(Offset, AddrVT)),
1790                             DAG.getSrcValue(NULL));
1791         BytesLeft -= 2;
1792         Offset += 2;
1793       }
1794
1795       if (BytesLeft == 1) {
1796         Value = DAG.getConstant(Val, MVT::i8);
1797         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1798                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1799                                         DAG.getConstant(Offset, AddrVT)),
1800                             DAG.getSrcValue(NULL));
1801       }
1802     }
1803
1804     return Chain;
1805   }
1806   case ISD::MEMCPY: {
1807     SDOperand Chain = Op.getOperand(0);
1808     unsigned Align =
1809       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1810     if (Align == 0) Align = 1;
1811
1812     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1813     // If not DWORD aligned, call memcpy if size is less than the threshold.
1814     // It knows how to align to the right boundary first.
1815     if ((Align & 3) != 0 ||
1816         !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1817       MVT::ValueType IntPtr = getPointerTy();
1818       const Type *IntPtrTy = getTargetData().getIntPtrType();
1819       std::vector<std::pair<SDOperand, const Type*> > Args;
1820       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1821       Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1822       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1823       std::pair<SDOperand,SDOperand> CallResult =
1824         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1825                     DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1826       return CallResult.second;
1827     }
1828
1829     MVT::ValueType AVT;
1830     SDOperand Count;
1831     unsigned BytesLeft = 0;
1832     switch (Align & 3) {
1833     case 2:   // WORD aligned
1834       AVT = MVT::i16;
1835       Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1836       BytesLeft = I->getValue() % 2;
1837       break;
1838     case 0:   // DWORD aligned
1839       AVT = MVT::i32;
1840       Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1841       BytesLeft = I->getValue() % 4;
1842       break;
1843     default:  // Byte aligned
1844       AVT = MVT::i8;
1845       Count = Op.getOperand(3);
1846       break;
1847     }
1848
1849     SDOperand InFlag(0, 0);
1850     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1851     InFlag = Chain.getValue(1);
1852     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1853     InFlag = Chain.getValue(1);
1854     Chain  = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1855     InFlag = Chain.getValue(1);
1856     Chain = DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1857                         DAG.getValueType(AVT), InFlag);
1858
1859     if (BytesLeft) {
1860       // Issue loads and stores for the last 1 - 3 bytes.
1861       unsigned Offset = I->getValue() - BytesLeft;
1862       SDOperand DstAddr = Op.getOperand(1);
1863       MVT::ValueType DstVT = DstAddr.getValueType();
1864       SDOperand SrcAddr = Op.getOperand(2);
1865       MVT::ValueType SrcVT = SrcAddr.getValueType();
1866       SDOperand Value;
1867       if (BytesLeft >= 2) {
1868         Value = DAG.getLoad(MVT::i16, Chain,
1869                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1870                                         DAG.getConstant(Offset, SrcVT)),
1871                             DAG.getSrcValue(NULL));
1872         Chain = Value.getValue(1);
1873         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1874                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
1875                                         DAG.getConstant(Offset, DstVT)),
1876                             DAG.getSrcValue(NULL));
1877         BytesLeft -= 2;
1878         Offset += 2;
1879       }
1880
1881       if (BytesLeft == 1) {
1882         Value = DAG.getLoad(MVT::i8, Chain,
1883                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1884                                         DAG.getConstant(Offset, SrcVT)),
1885                             DAG.getSrcValue(NULL));
1886         Chain = Value.getValue(1);
1887         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1888                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
1889                                         DAG.getConstant(Offset, DstVT)),
1890                             DAG.getSrcValue(NULL));
1891       }
1892     }
1893
1894     return Chain;
1895   }
1896
1897   // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1898   // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1899   // one of the above mentioned nodes. It has to be wrapped because otherwise
1900   // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1901   // be used to form addressing mode. These wrapped nodes will be selected
1902   // into MOV32ri.
1903   case ISD::ConstantPool: {
1904     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
1905     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1906                          DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1907                                                    CP->getAlignment()));
1908     if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
1909       // With PIC, the address is actually $g + Offset.
1910       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1911         Result = DAG.getNode(ISD::ADD, getPointerTy(),
1912                 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);    
1913     }
1914
1915     return Result;
1916   }
1917   case ISD::GlobalAddress: {
1918     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1919     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1920                          DAG.getTargetGlobalAddress(GV, getPointerTy()));
1921     if (getTargetMachine().
1922         getSubtarget<X86Subtarget>().isTargetDarwin()) {
1923       // With PIC, the address is actually $g + Offset.
1924       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1925         Result = DAG.getNode(ISD::ADD, getPointerTy(),
1926                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1927
1928       // For Darwin, external and weak symbols are indirect, so we want to load
1929       // the value at address GV, not the value of GV itself.  This means that
1930       // the GlobalAddress must be in the base or index register of the address,
1931       // not the GV offset field.
1932       if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1933           (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1934            (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
1935         Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
1936                              Result, DAG.getSrcValue(NULL));
1937     }
1938
1939     return Result;
1940   }
1941   case ISD::ExternalSymbol: {
1942     const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
1943     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1944                          DAG.getTargetExternalSymbol(Sym, getPointerTy()));
1945     if (getTargetMachine().
1946         getSubtarget<X86Subtarget>().isTargetDarwin()) {
1947       // With PIC, the address is actually $g + Offset.
1948       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1949         Result = DAG.getNode(ISD::ADD, getPointerTy(),
1950                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1951     }
1952
1953     return Result;
1954   }
1955   case ISD::VASTART: {
1956     // vastart just stores the address of the VarArgsFrameIndex slot into the
1957     // memory location argument.
1958     // FIXME: Replace MVT::i32 with PointerTy
1959     SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1960     return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, 
1961                        Op.getOperand(1), Op.getOperand(2));
1962   }
1963   case ISD::RET: {
1964     SDOperand Copy;
1965     
1966     switch(Op.getNumOperands()) {
1967     default:
1968       assert(0 && "Do not know how to return this many arguments!");
1969       abort();
1970     case 1: 
1971       return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1972                          DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1973     case 2: {
1974       MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1975       if (MVT::isInteger(ArgVT))
1976         Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1977                                 SDOperand());
1978       else if (!X86ScalarSSE) {
1979         std::vector<MVT::ValueType> Tys;
1980         Tys.push_back(MVT::Other);
1981         Tys.push_back(MVT::Flag);
1982         std::vector<SDOperand> Ops;
1983         Ops.push_back(Op.getOperand(0));
1984         Ops.push_back(Op.getOperand(1));
1985         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1986       } else {
1987         SDOperand MemLoc;
1988         SDOperand Chain = Op.getOperand(0);
1989         SDOperand Value = Op.getOperand(1);
1990
1991         if (Value.getOpcode() == ISD::LOAD &&
1992             (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
1993           Chain  = Value.getOperand(0);
1994           MemLoc = Value.getOperand(1);
1995         } else {
1996           // Spill the value to memory and reload it into top of stack.
1997           unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1998           MachineFunction &MF = DAG.getMachineFunction();
1999           int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2000           MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2001           Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), 
2002                               Value, MemLoc, DAG.getSrcValue(0));
2003         }
2004         std::vector<MVT::ValueType> Tys;
2005         Tys.push_back(MVT::f64);
2006         Tys.push_back(MVT::Other);
2007         std::vector<SDOperand> Ops;
2008         Ops.push_back(Chain);
2009         Ops.push_back(MemLoc);
2010         Ops.push_back(DAG.getValueType(ArgVT));
2011         Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2012         Tys.clear();
2013         Tys.push_back(MVT::Other);
2014         Tys.push_back(MVT::Flag);
2015         Ops.clear();
2016         Ops.push_back(Copy.getValue(1));
2017         Ops.push_back(Copy);
2018         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2019       }
2020       break;
2021     }
2022     case 3:
2023       Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), 
2024                               SDOperand());
2025       Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2026       break;
2027     }
2028     return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2029                        Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2030                        Copy.getValue(1));
2031   }
2032   }
2033 }
2034
2035 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2036   switch (Opcode) {
2037   default: return NULL;
2038   case X86ISD::SHLD:               return "X86ISD::SHLD";
2039   case X86ISD::SHRD:               return "X86ISD::SHRD";
2040   case X86ISD::FAND:               return "X86ISD::FAND";
2041   case X86ISD::FXOR:               return "X86ISD::FXOR";
2042   case X86ISD::FILD:               return "X86ISD::FILD";
2043   case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
2044   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2045   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2046   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
2047   case X86ISD::FLD:                return "X86ISD::FLD";
2048   case X86ISD::FST:                return "X86ISD::FST";
2049   case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
2050   case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
2051   case X86ISD::CALL:               return "X86ISD::CALL";
2052   case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
2053   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
2054   case X86ISD::CMP:                return "X86ISD::CMP";
2055   case X86ISD::TEST:               return "X86ISD::TEST";
2056   case X86ISD::SETCC:              return "X86ISD::SETCC";
2057   case X86ISD::CMOV:               return "X86ISD::CMOV";
2058   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
2059   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
2060   case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
2061   case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
2062   case X86ISD::LOAD_PACK:          return "X86ISD::LOAD_PACK";
2063   case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
2064   case X86ISD::Wrapper:            return "X86ISD::Wrapper";
2065   }
2066 }
2067
2068 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2069                                                        uint64_t Mask,
2070                                                        uint64_t &KnownZero, 
2071                                                        uint64_t &KnownOne,
2072                                                        unsigned Depth) const {
2073
2074   unsigned Opc = Op.getOpcode();
2075   KnownZero = KnownOne = 0;   // Don't know anything.
2076
2077   switch (Opc) {
2078   default:
2079     assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2080     break;
2081   case X86ISD::SETCC: 
2082     KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2083     break;
2084   }
2085 }
2086
2087 std::vector<unsigned> X86TargetLowering::
2088 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2089                                   MVT::ValueType VT) const {
2090   if (Constraint.size() == 1) {
2091     // FIXME: not handling fp-stack yet!
2092     // FIXME: not handling MMX registers yet ('y' constraint).
2093     switch (Constraint[0]) {      // GCC X86 Constraint Letters
2094     default: break;  // Unknown constriant letter
2095     case 'r':   // GENERAL_REGS
2096     case 'R':   // LEGACY_REGS
2097       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2098                                    X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2099     case 'l':   // INDEX_REGS
2100       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2101                                    X86::ESI, X86::EDI, X86::EBP, 0);
2102     case 'q':   // Q_REGS (GENERAL_REGS in 64-bit mode)
2103     case 'Q':   // Q_REGS
2104       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2105     case 'x':   // SSE_REGS if SSE1 allowed
2106       if (Subtarget->hasSSE1())
2107         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2108                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2109                                      0);
2110       return std::vector<unsigned>();
2111     case 'Y':   // SSE_REGS if SSE2 allowed
2112       if (Subtarget->hasSSE2())
2113         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2114                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2115                                      0);
2116       return std::vector<unsigned>();
2117     }
2118   }
2119   
2120   return std::vector<unsigned>();
2121 }