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