Handle canonical form of e.g.
[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/ADT/VectorExtras.h"
23 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/SelectionDAG.h"
28 #include "llvm/CodeGen/SSARegMap.h"
29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Target/TargetOptions.h"
31 using namespace llvm;
32
33 // FIXME: temporary.
34 #include "llvm/Support/CommandLine.h"
35 static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36                                   cl::desc("Enable fastcc on X86"));
37
38 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39   : TargetLowering(TM) {
40   Subtarget = &TM.getSubtarget<X86Subtarget>();
41   X86ScalarSSE = Subtarget->hasSSE2();
42
43   // Set up the TargetLowering object.
44
45   // X86 is weird, it always uses i8 for shift amounts and setcc results.
46   setShiftAmountType(MVT::i8);
47   setSetCCResultType(MVT::i8);
48   setSetCCResultContents(ZeroOrOneSetCCResult);
49   setSchedulingPreference(SchedulingForRegPressure);
50   setShiftAmountFlavor(Mask);   // shl X, 32 == shl X, 0
51   setStackPointerRegisterToSaveRestore(X86::ESP);
52
53   if (!Subtarget->isTargetDarwin())
54     // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55     setUseUnderscoreSetJmpLongJmp(true);
56     
57   // Add legal addressing mode scale values.
58   addLegalAddressScale(8);
59   addLegalAddressScale(4);
60   addLegalAddressScale(2);
61   // Enter the ones which require both scale + index last. These are more
62   // expensive.
63   addLegalAddressScale(9);
64   addLegalAddressScale(5);
65   addLegalAddressScale(3);
66   
67   // Set up the register classes.
68   addRegisterClass(MVT::i8, X86::R8RegisterClass);
69   addRegisterClass(MVT::i16, X86::R16RegisterClass);
70   addRegisterClass(MVT::i32, X86::R32RegisterClass);
71
72   // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
73   // operation.
74   setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
75   setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
76   setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
77
78   if (X86ScalarSSE)
79     // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
80     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Expand);
81   else
82     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Promote);
83
84   // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
85   // this operation.
86   setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
87   setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
88   // SSE has no i16 to fp conversion, only i32
89   if (X86ScalarSSE)
90     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
91   else {
92     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Custom);
93     setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
94   }
95
96   // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
97   // isn't legal.
98   setOperationAction(ISD::SINT_TO_FP       , MVT::i64  , Custom);
99   setOperationAction(ISD::FP_TO_SINT       , MVT::i64  , Custom);
100
101   // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
102   // this operation.
103   setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
104   setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
105
106   if (X86ScalarSSE) {
107     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
108   } else {
109     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
110     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
111   }
112
113   // Handle FP_TO_UINT by promoting the destination to a larger signed
114   // conversion.
115   setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
116   setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
117   setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
118
119   if (X86ScalarSSE && !Subtarget->hasSSE3())
120     // Expand FP_TO_UINT into a select.
121     // FIXME: We would like to use a Custom expander here eventually to do
122     // the optimal thing for SSE vs. the default expansion in the legalizer.
123     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Expand);
124   else
125     // With SSE3 we can use fisttpll to convert to a signed i64.
126     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
127
128   setOperationAction(ISD::BIT_CONVERT      , MVT::f32  , Expand);
129   setOperationAction(ISD::BIT_CONVERT      , MVT::i32  , Expand);
130
131   setOperationAction(ISD::BRCOND           , MVT::Other, Custom);
132   setOperationAction(ISD::BR_CC            , MVT::Other, Expand);
133   setOperationAction(ISD::SELECT_CC        , MVT::Other, Expand);
134   setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
135   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Expand);
136   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Expand);
137   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
138   setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
139   setOperationAction(ISD::SEXTLOAD         , MVT::i1   , Expand);
140   setOperationAction(ISD::FREM             , MVT::f64  , Expand);
141   setOperationAction(ISD::CTPOP            , MVT::i8   , Expand);
142   setOperationAction(ISD::CTTZ             , MVT::i8   , Expand);
143   setOperationAction(ISD::CTLZ             , MVT::i8   , Expand);
144   setOperationAction(ISD::CTPOP            , MVT::i16  , Expand);
145   setOperationAction(ISD::CTTZ             , MVT::i16  , Expand);
146   setOperationAction(ISD::CTLZ             , MVT::i16  , Expand);
147   setOperationAction(ISD::CTPOP            , MVT::i32  , Expand);
148   setOperationAction(ISD::CTTZ             , MVT::i32  , Expand);
149   setOperationAction(ISD::CTLZ             , MVT::i32  , Expand);
150   setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
151   setOperationAction(ISD::BSWAP            , MVT::i16  , Expand);
152
153   // These should be promoted to a larger select which is supported.
154   setOperationAction(ISD::SELECT           , MVT::i1   , Promote);
155   setOperationAction(ISD::SELECT           , MVT::i8   , Promote);
156
157   // X86 wants to expand cmov itself.
158   setOperationAction(ISD::SELECT          , MVT::i16  , Custom);
159   setOperationAction(ISD::SELECT          , MVT::i32  , Custom);
160   setOperationAction(ISD::SELECT          , MVT::f32  , Custom);
161   setOperationAction(ISD::SELECT          , MVT::f64  , Custom);
162   setOperationAction(ISD::SETCC           , MVT::i8   , Custom);
163   setOperationAction(ISD::SETCC           , MVT::i16  , Custom);
164   setOperationAction(ISD::SETCC           , MVT::i32  , Custom);
165   setOperationAction(ISD::SETCC           , MVT::f32  , Custom);
166   setOperationAction(ISD::SETCC           , MVT::f64  , Custom);
167   // X86 ret instruction may pop stack.
168   setOperationAction(ISD::RET             , MVT::Other, Custom);
169   // Darwin ABI issue.
170   setOperationAction(ISD::ConstantPool    , MVT::i32  , Custom);
171   setOperationAction(ISD::GlobalAddress   , MVT::i32  , Custom);
172   setOperationAction(ISD::ExternalSymbol  , MVT::i32  , Custom);
173   // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
174   setOperationAction(ISD::SHL_PARTS       , MVT::i32  , Custom);
175   setOperationAction(ISD::SRA_PARTS       , MVT::i32  , Custom);
176   setOperationAction(ISD::SRL_PARTS       , MVT::i32  , Custom);
177   // X86 wants to expand memset / memcpy itself.
178   setOperationAction(ISD::MEMSET          , MVT::Other, Custom);
179   setOperationAction(ISD::MEMCPY          , MVT::Other, Custom);
180
181   // We don't have line number support yet.
182   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
183   setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
184   // FIXME - use subtarget debug flags
185   if (!Subtarget->isTargetDarwin())
186     setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
187
188   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
190   
191   // Use the default implementation.
192   setOperationAction(ISD::VAARG             , MVT::Other, Expand);
193   setOperationAction(ISD::VACOPY            , MVT::Other, Expand);
194   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
195   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand); 
196   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
197   setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32  , Expand);
198
199   setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200   setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
202   if (X86ScalarSSE) {
203     // Set up the FP register classes.
204     addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205     addRegisterClass(MVT::f64, X86::FR64RegisterClass);
206
207     // SSE has no load+extend ops
208     setOperationAction(ISD::EXTLOAD,  MVT::f32, Expand);
209     setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
211     // Use ANDPD to simulate FABS.
212     setOperationAction(ISD::FABS , MVT::f64, Custom);
213     setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215     // Use XORP to simulate FNEG.
216     setOperationAction(ISD::FNEG , MVT::f64, Custom);
217     setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
219     // We don't support sin/cos/fmod
220     setOperationAction(ISD::FSIN , MVT::f64, Expand);
221     setOperationAction(ISD::FCOS , MVT::f64, Expand);
222     setOperationAction(ISD::FREM , MVT::f64, Expand);
223     setOperationAction(ISD::FSIN , MVT::f32, Expand);
224     setOperationAction(ISD::FCOS , MVT::f32, Expand);
225     setOperationAction(ISD::FREM , MVT::f32, Expand);
226
227     // Expand FP immediates into loads from the stack, except for the special
228     // cases we handle.
229     setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230     setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
231     addLegalFPImmediate(+0.0); // xorps / xorpd
232   } else {
233     // Set up the FP register classes.
234     addRegisterClass(MVT::f64, X86::RFPRegisterClass);
235     
236     setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237     
238     if (!UnsafeFPMath) {
239       setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
240       setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
241     }
242
243     setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
244     addLegalFPImmediate(+0.0); // FLD0
245     addLegalFPImmediate(+1.0); // FLD1
246     addLegalFPImmediate(-0.0); // FLD0/FCHS
247     addLegalFPImmediate(-1.0); // FLD1/FCHS
248   }
249
250   // First set operation action for all vector types to expand. Then we
251   // will selectively turn on ones that can be effectively codegen'd.
252   for (unsigned VT = (unsigned)MVT::Vector + 1;
253        VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254     setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255     setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256     setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257     setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
258     setOperationAction(ISD::VECTOR_SHUFFLE,     (MVT::ValueType)VT, Expand);
259     setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
260     setOperationAction(ISD::INSERT_VECTOR_ELT,  (MVT::ValueType)VT, Expand);
261   }
262
263   if (Subtarget->hasMMX()) {
264     addRegisterClass(MVT::v8i8,  X86::VR64RegisterClass);
265     addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
266     addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
267
268     // FIXME: add MMX packed arithmetics
269     setOperationAction(ISD::BUILD_VECTOR,     MVT::v8i8,  Expand);
270     setOperationAction(ISD::BUILD_VECTOR,     MVT::v4i16, Expand);
271     setOperationAction(ISD::BUILD_VECTOR,     MVT::v2i32, Expand);
272   }
273
274   if (Subtarget->hasSSE1()) {
275     addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
276
277     setOperationAction(ISD::ADD,              MVT::v4f32, Legal);
278     setOperationAction(ISD::SUB,              MVT::v4f32, Legal);
279     setOperationAction(ISD::MUL,              MVT::v4f32, Legal);
280     setOperationAction(ISD::LOAD,             MVT::v4f32, Legal);
281     setOperationAction(ISD::BUILD_VECTOR,     MVT::v4f32, Custom);
282     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v4f32, Custom);
283     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
284   }
285
286   if (Subtarget->hasSSE2()) {
287     addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
288     addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
289     addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
290     addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
291     addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
292
293
294     setOperationAction(ISD::ADD,              MVT::v2f64, Legal);
295     setOperationAction(ISD::ADD,              MVT::v16i8, Legal);
296     setOperationAction(ISD::ADD,              MVT::v8i16, Legal);
297     setOperationAction(ISD::ADD,              MVT::v4i32, Legal);
298     setOperationAction(ISD::SUB,              MVT::v2f64, Legal);
299     setOperationAction(ISD::SUB,              MVT::v16i8, Legal);
300     setOperationAction(ISD::SUB,              MVT::v8i16, Legal);
301     setOperationAction(ISD::SUB,              MVT::v4i32, Legal);
302     setOperationAction(ISD::MUL,              MVT::v2f64, Legal);
303     setOperationAction(ISD::LOAD,             MVT::v2f64, Legal);
304     setOperationAction(ISD::LOAD,             MVT::v16i8, Legal);
305     setOperationAction(ISD::LOAD,             MVT::v8i16, Legal);
306     setOperationAction(ISD::LOAD,             MVT::v4i32, Legal);
307     setOperationAction(ISD::LOAD,             MVT::v2i64, Legal);
308     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
310     setOperationAction(ISD::BUILD_VECTOR,     MVT::v2f64, Custom);
311     setOperationAction(ISD::BUILD_VECTOR,     MVT::v16i8, Custom);
312     setOperationAction(ISD::BUILD_VECTOR,     MVT::v8i16, Custom);
313     setOperationAction(ISD::BUILD_VECTOR,     MVT::v4i32, Custom);
314     setOperationAction(ISD::BUILD_VECTOR,     MVT::v2i64, Custom);
315     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v2f64, Custom);
316     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v16i8, Custom);
317     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v8i16, Custom);
318     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v4i32, Custom);
319     setOperationAction(ISD::VECTOR_SHUFFLE,   MVT::v2i64, Custom);
320     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
321     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
322     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
323     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
324   }
325
326   computeRegisterProperties();
327
328   // FIXME: These should be based on subtarget info. Plus, the values should
329   // be smaller when we are in optimizing for size mode.
330   maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
331   maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
332   maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
333   allowUnalignedMemoryAccesses = true; // x86 supports it!
334 }
335
336 std::vector<SDOperand>
337 X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
338   if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
339     return LowerFastCCArguments(F, DAG);
340   return LowerCCCArguments(F, DAG);
341 }
342
343 std::pair<SDOperand, SDOperand>
344 X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
345                                bool isVarArg, unsigned CallingConv,
346                                bool isTailCall,
347                                SDOperand Callee, ArgListTy &Args,
348                                SelectionDAG &DAG) {
349   assert((!isVarArg || CallingConv == CallingConv::C) &&
350          "Only C takes varargs!");
351
352   // If the callee is a GlobalAddress node (quite common, every direct call is)
353   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
354   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
355     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
356   else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
357     Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
358
359   if (CallingConv == CallingConv::Fast && EnableFastCC)
360     return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
361   return  LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
362 }
363
364 //===----------------------------------------------------------------------===//
365 //                    C Calling Convention implementation
366 //===----------------------------------------------------------------------===//
367
368 std::vector<SDOperand>
369 X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
370   std::vector<SDOperand> ArgValues;
371
372   MachineFunction &MF = DAG.getMachineFunction();
373   MachineFrameInfo *MFI = MF.getFrameInfo();
374
375   // Add DAG nodes to load the arguments...  On entry to a function on the X86,
376   // the stack frame looks like this:
377   //
378   // [ESP] -- return address
379   // [ESP + 4] -- first argument (leftmost lexically)
380   // [ESP + 8] -- second argument, if first argument is four bytes in size
381   //    ...
382   //
383   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
384   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
385     MVT::ValueType ObjectVT = getValueType(I->getType());
386     unsigned ArgIncrement = 4;
387     unsigned ObjSize;
388     switch (ObjectVT) {
389     default: assert(0 && "Unhandled argument type!");
390     case MVT::i1:
391     case MVT::i8:  ObjSize = 1;                break;
392     case MVT::i16: ObjSize = 2;                break;
393     case MVT::i32: ObjSize = 4;                break;
394     case MVT::i64: ObjSize = ArgIncrement = 8; break;
395     case MVT::f32: ObjSize = 4;                break;
396     case MVT::f64: ObjSize = ArgIncrement = 8; break;
397     }
398     // Create the frame index object for this incoming parameter...
399     int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
400
401     // Create the SelectionDAG nodes corresponding to a load from this parameter
402     SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
403
404     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
405     // dead loads.
406     SDOperand ArgValue;
407     if (!I->use_empty())
408       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
409                              DAG.getSrcValue(NULL));
410     else {
411       if (MVT::isInteger(ObjectVT))
412         ArgValue = DAG.getConstant(0, ObjectVT);
413       else
414         ArgValue = DAG.getConstantFP(0, ObjectVT);
415     }
416     ArgValues.push_back(ArgValue);
417
418     ArgOffset += ArgIncrement;   // Move on to the next argument...
419   }
420
421   // If the function takes variable number of arguments, make a frame index for
422   // the start of the first vararg value... for expansion of llvm.va_start.
423   if (F.isVarArg())
424     VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
425   ReturnAddrIndex = 0;     // No return address slot generated yet.
426   BytesToPopOnReturn = 0;  // Callee pops nothing.
427   BytesCallerReserves = ArgOffset;
428
429   // Finally, inform the code generator which regs we return values in.
430   switch (getValueType(F.getReturnType())) {
431   default: assert(0 && "Unknown type!");
432   case MVT::isVoid: break;
433   case MVT::i1:
434   case MVT::i8:
435   case MVT::i16:
436   case MVT::i32:
437     MF.addLiveOut(X86::EAX);
438     break;
439   case MVT::i64:
440     MF.addLiveOut(X86::EAX);
441     MF.addLiveOut(X86::EDX);
442     break;
443   case MVT::f32:
444   case MVT::f64:
445     MF.addLiveOut(X86::ST0);
446     break;
447   }
448   return ArgValues;
449 }
450
451 std::pair<SDOperand, SDOperand>
452 X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
453                                   bool isVarArg, bool isTailCall,
454                                   SDOperand Callee, ArgListTy &Args,
455                                   SelectionDAG &DAG) {
456   // Count how many bytes are to be pushed on the stack.
457   unsigned NumBytes = 0;
458
459   if (Args.empty()) {
460     // Save zero bytes.
461     Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
462   } else {
463     for (unsigned i = 0, e = Args.size(); i != e; ++i)
464       switch (getValueType(Args[i].second)) {
465       default: assert(0 && "Unknown value type!");
466       case MVT::i1:
467       case MVT::i8:
468       case MVT::i16:
469       case MVT::i32:
470       case MVT::f32:
471         NumBytes += 4;
472         break;
473       case MVT::i64:
474       case MVT::f64:
475         NumBytes += 8;
476         break;
477       }
478
479     Chain = DAG.getCALLSEQ_START(Chain,
480                                  DAG.getConstant(NumBytes, getPointerTy()));
481
482     // Arguments go on the stack in reverse order, as specified by the ABI.
483     unsigned ArgOffset = 0;
484     SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
485     std::vector<SDOperand> Stores;
486
487     for (unsigned i = 0, e = Args.size(); i != e; ++i) {
488       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
489       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
490
491       switch (getValueType(Args[i].second)) {
492       default: assert(0 && "Unexpected ValueType for argument!");
493       case MVT::i1:
494       case MVT::i8:
495       case MVT::i16:
496         // Promote the integer to 32 bits.  If the input type is signed use a
497         // sign extend, otherwise use a zero extend.
498         if (Args[i].second->isSigned())
499           Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
500         else
501           Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
502
503         // FALL THROUGH
504       case MVT::i32:
505       case MVT::f32:
506         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
507                                      Args[i].first, PtrOff,
508                                      DAG.getSrcValue(NULL)));
509         ArgOffset += 4;
510         break;
511       case MVT::i64:
512       case MVT::f64:
513         Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
514                                      Args[i].first, PtrOff,
515                                      DAG.getSrcValue(NULL)));
516         ArgOffset += 8;
517         break;
518       }
519     }
520     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
521   }
522
523   std::vector<MVT::ValueType> RetVals;
524   MVT::ValueType RetTyVT = getValueType(RetTy);
525   RetVals.push_back(MVT::Other);
526
527   // The result values produced have to be legal.  Promote the result.
528   switch (RetTyVT) {
529   case MVT::isVoid: break;
530   default:
531     RetVals.push_back(RetTyVT);
532     break;
533   case MVT::i1:
534   case MVT::i8:
535   case MVT::i16:
536     RetVals.push_back(MVT::i32);
537     break;
538   case MVT::f32:
539     if (X86ScalarSSE)
540       RetVals.push_back(MVT::f32);
541     else
542       RetVals.push_back(MVT::f64);
543     break;
544   case MVT::i64:
545     RetVals.push_back(MVT::i32);
546     RetVals.push_back(MVT::i32);
547     break;
548   }
549
550   std::vector<MVT::ValueType> NodeTys;
551   NodeTys.push_back(MVT::Other);   // Returns a chain
552   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
553   std::vector<SDOperand> Ops;
554   Ops.push_back(Chain);
555   Ops.push_back(Callee);
556
557   // FIXME: Do not generate X86ISD::TAILCALL for now.
558   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
559   SDOperand InFlag = Chain.getValue(1);
560
561   NodeTys.clear();
562   NodeTys.push_back(MVT::Other);   // Returns a chain
563   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
564   Ops.clear();
565   Ops.push_back(Chain);
566   Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
567   Ops.push_back(DAG.getConstant(0, getPointerTy()));
568   Ops.push_back(InFlag);
569   Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
570   InFlag = Chain.getValue(1);
571   
572   SDOperand RetVal;
573   if (RetTyVT != MVT::isVoid) {
574     switch (RetTyVT) {
575     default: assert(0 && "Unknown value type to return!");
576     case MVT::i1:
577     case MVT::i8:
578       RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
579       Chain = RetVal.getValue(1);
580       if (RetTyVT == MVT::i1) 
581         RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
582       break;
583     case MVT::i16:
584       RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
585       Chain = RetVal.getValue(1);
586       break;
587     case MVT::i32:
588       RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
589       Chain = RetVal.getValue(1);
590       break;
591     case MVT::i64: {
592       SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
593       SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, 
594                                         Lo.getValue(2));
595       RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
596       Chain = Hi.getValue(1);
597       break;
598     }
599     case MVT::f32:
600     case MVT::f64: {
601       std::vector<MVT::ValueType> Tys;
602       Tys.push_back(MVT::f64);
603       Tys.push_back(MVT::Other);
604       Tys.push_back(MVT::Flag);
605       std::vector<SDOperand> Ops;
606       Ops.push_back(Chain);
607       Ops.push_back(InFlag);
608       RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
609       Chain  = RetVal.getValue(1);
610       InFlag = RetVal.getValue(2);
611       if (X86ScalarSSE) {
612         // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
613         // shouldn't be necessary except that RFP cannot be live across
614         // multiple blocks. When stackifier is fixed, they can be uncoupled.
615         MachineFunction &MF = DAG.getMachineFunction();
616         int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
617         SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
618         Tys.clear();
619         Tys.push_back(MVT::Other);
620         Ops.clear();
621         Ops.push_back(Chain);
622         Ops.push_back(RetVal);
623         Ops.push_back(StackSlot);
624         Ops.push_back(DAG.getValueType(RetTyVT));
625         Ops.push_back(InFlag);
626         Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
627         RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
628                              DAG.getSrcValue(NULL));
629         Chain = RetVal.getValue(1);
630       }
631
632       if (RetTyVT == MVT::f32 && !X86ScalarSSE)
633         // FIXME: we would really like to remember that this FP_ROUND
634         // operation is okay to eliminate if we allow excess FP precision.
635         RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
636       break;
637     }
638     }
639   }
640
641   return std::make_pair(RetVal, Chain);
642 }
643
644 //===----------------------------------------------------------------------===//
645 //                    Fast Calling Convention implementation
646 //===----------------------------------------------------------------------===//
647 //
648 // The X86 'fast' calling convention passes up to two integer arguments in
649 // registers (an appropriate portion of EAX/EDX), passes arguments in C order,
650 // and requires that the callee pop its arguments off the stack (allowing proper
651 // tail calls), and has the same return value conventions as C calling convs.
652 //
653 // This calling convention always arranges for the callee pop value to be 8n+4
654 // bytes, which is needed for tail recursion elimination and stack alignment
655 // reasons.
656 //
657 // Note that this can be enhanced in the future to pass fp vals in registers
658 // (when we have a global fp allocator) and do other tricks.
659 //
660
661 /// AddLiveIn - This helper function adds the specified physical register to the
662 /// MachineFunction as a live in value.  It also creates a corresponding virtual
663 /// register for it.
664 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
665                           TargetRegisterClass *RC) {
666   assert(RC->contains(PReg) && "Not the correct regclass!");
667   unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
668   MF.addLiveIn(PReg, VReg);
669   return VReg;
670 }
671
672 // FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
673 // to pass in registers.  0 is none, 1 is is "use EAX", 2 is "use EAX and
674 // EDX".  Anything more is illegal.
675 //
676 // FIXME: The linscan register allocator currently has problem with
677 // coalescing.  At the time of this writing, whenever it decides to coalesce
678 // a physreg with a virtreg, this increases the size of the physreg's live
679 // range, and the live range cannot ever be reduced.  This causes problems if
680 // too many physregs are coaleced with virtregs, which can cause the register
681 // allocator to wedge itself.
682 //
683 // This code triggers this problem more often if we pass args in registers,
684 // so disable it until this is fixed.
685 //
686 // NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
687 // about code being dead.
688 //
689 static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
690
691
692 std::vector<SDOperand>
693 X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
694   std::vector<SDOperand> ArgValues;
695
696   MachineFunction &MF = DAG.getMachineFunction();
697   MachineFrameInfo *MFI = MF.getFrameInfo();
698
699   // Add DAG nodes to load the arguments...  On entry to a function the stack
700   // frame looks like this:
701   //
702   // [ESP] -- return address
703   // [ESP + 4] -- first nonreg argument (leftmost lexically)
704   // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
705   //    ...
706   unsigned ArgOffset = 0;   // Frame mechanisms handle retaddr slot
707
708   // Keep track of the number of integer regs passed so far.  This can be either
709   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
710   // used).
711   unsigned NumIntRegs = 0;
712   
713   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
714     MVT::ValueType ObjectVT = getValueType(I->getType());
715     unsigned ArgIncrement = 4;
716     unsigned ObjSize = 0;
717     SDOperand ArgValue;
718
719     switch (ObjectVT) {
720     default: assert(0 && "Unhandled argument type!");
721     case MVT::i1:
722     case MVT::i8:
723       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
724         if (!I->use_empty()) {
725           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
726                                     X86::R8RegisterClass);
727           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
728           DAG.setRoot(ArgValue.getValue(1));
729           if (ObjectVT == MVT::i1)
730             // FIXME: Should insert a assertzext here.
731             ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
732         }
733         ++NumIntRegs;
734         break;
735       }
736
737       ObjSize = 1;
738       break;
739     case MVT::i16:
740       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
741         if (!I->use_empty()) {
742           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
743                                     X86::R16RegisterClass);
744           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
745           DAG.setRoot(ArgValue.getValue(1));
746         }
747         ++NumIntRegs;
748         break;
749       }
750       ObjSize = 2;
751       break;
752     case MVT::i32:
753       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
754         if (!I->use_empty()) {
755           unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
756                                     X86::R32RegisterClass);
757           ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
758           DAG.setRoot(ArgValue.getValue(1));
759         }
760         ++NumIntRegs;
761         break;
762       }
763       ObjSize = 4;
764       break;
765     case MVT::i64:
766       if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
767         if (!I->use_empty()) {
768           unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
769           unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
770
771           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
772           SDOperand Hi  = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
773           DAG.setRoot(Hi.getValue(1));
774
775           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
776         }
777         NumIntRegs += 2;
778         break;
779       } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
780         if (!I->use_empty()) {
781           unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
782           SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
783           DAG.setRoot(Low.getValue(1));
784
785           // Load the high part from memory.
786           // Create the frame index object for this incoming parameter...
787           int FI = MFI->CreateFixedObject(4, ArgOffset);
788           SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
789           SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
790                                      DAG.getSrcValue(NULL));
791           ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
792         }
793         ArgOffset += 4;
794         NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
795         break;
796       }
797       ObjSize = ArgIncrement = 8;
798       break;
799     case MVT::f32: ObjSize = 4;                break;
800     case MVT::f64: ObjSize = ArgIncrement = 8; break;
801     }
802
803     // Don't codegen dead arguments.  FIXME: remove this check when we can nuke
804     // dead loads.
805     if (ObjSize && !I->use_empty()) {
806       // Create the frame index object for this incoming parameter...
807       int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
808
809       // Create the SelectionDAG nodes corresponding to a load from this
810       // parameter.
811       SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
812
813       ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
814                              DAG.getSrcValue(NULL));
815     } else if (ArgValue.Val == 0) {
816       if (MVT::isInteger(ObjectVT))
817         ArgValue = DAG.getConstant(0, ObjectVT);
818       else
819         ArgValue = DAG.getConstantFP(0, ObjectVT);
820     }
821     ArgValues.push_back(ArgValue);
822
823     if (ObjSize)
824       ArgOffset += ArgIncrement;   // Move on to the next argument.
825   }
826
827   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
828   // arguments and the arguments after the retaddr has been pushed are aligned.
829   if ((ArgOffset & 7) == 0)
830     ArgOffset += 4;
831
832   VarArgsFrameIndex = 0xAAAAAAA;   // fastcc functions can't have varargs.
833   ReturnAddrIndex = 0;             // No return address slot generated yet.
834   BytesToPopOnReturn = ArgOffset;  // Callee pops all stack arguments.
835   BytesCallerReserves = 0;
836
837   // Finally, inform the code generator which regs we return values in.
838   switch (getValueType(F.getReturnType())) {
839   default: assert(0 && "Unknown type!");
840   case MVT::isVoid: break;
841   case MVT::i1:
842   case MVT::i8:
843   case MVT::i16:
844   case MVT::i32:
845     MF.addLiveOut(X86::EAX);
846     break;
847   case MVT::i64:
848     MF.addLiveOut(X86::EAX);
849     MF.addLiveOut(X86::EDX);
850     break;
851   case MVT::f32:
852   case MVT::f64:
853     MF.addLiveOut(X86::ST0);
854     break;
855   }
856   return ArgValues;
857 }
858
859 std::pair<SDOperand, SDOperand>
860 X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
861                                      bool isTailCall, SDOperand Callee,
862                                      ArgListTy &Args, SelectionDAG &DAG) {
863   // Count how many bytes are to be pushed on the stack.
864   unsigned NumBytes = 0;
865
866   // Keep track of the number of integer regs passed so far.  This can be either
867   // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
868   // used).
869   unsigned NumIntRegs = 0;
870
871   for (unsigned i = 0, e = Args.size(); i != e; ++i)
872     switch (getValueType(Args[i].second)) {
873     default: assert(0 && "Unknown value type!");
874     case MVT::i1:
875     case MVT::i8:
876     case MVT::i16:
877     case MVT::i32:
878       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
879         ++NumIntRegs;
880         break;
881       }
882       // fall through
883     case MVT::f32:
884       NumBytes += 4;
885       break;
886     case MVT::i64:
887       if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
888         NumIntRegs += 2;
889         break;
890       } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
891         NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
892         NumBytes += 4;
893         break;
894       }
895
896       // fall through
897     case MVT::f64:
898       NumBytes += 8;
899       break;
900     }
901
902   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
903   // arguments and the arguments after the retaddr has been pushed are aligned.
904   if ((NumBytes & 7) == 0)
905     NumBytes += 4;
906
907   Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
908
909   // Arguments go on the stack in reverse order, as specified by the ABI.
910   unsigned ArgOffset = 0;
911   SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
912   NumIntRegs = 0;
913   std::vector<SDOperand> Stores;
914   std::vector<SDOperand> RegValuesToPass;
915   for (unsigned i = 0, e = Args.size(); i != e; ++i) {
916     switch (getValueType(Args[i].second)) {
917     default: assert(0 && "Unexpected ValueType for argument!");
918     case MVT::i1:
919       Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
920       // Fall through.
921     case MVT::i8:
922     case MVT::i16:
923     case MVT::i32:
924       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
925         RegValuesToPass.push_back(Args[i].first);
926         ++NumIntRegs;
927         break;
928       }
929       // Fall through
930     case MVT::f32: {
931       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
932       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
933       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
934                                    Args[i].first, PtrOff,
935                                    DAG.getSrcValue(NULL)));
936       ArgOffset += 4;
937       break;
938     }
939     case MVT::i64:
940        // Can pass (at least) part of it in regs?
941       if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
942         SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
943                                    Args[i].first, DAG.getConstant(1, MVT::i32));
944         SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
945                                    Args[i].first, DAG.getConstant(0, MVT::i32));
946         RegValuesToPass.push_back(Lo);
947         ++NumIntRegs;
948         
949         // Pass both parts in regs?
950         if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
951           RegValuesToPass.push_back(Hi);
952           ++NumIntRegs;
953         } else {
954           // Pass the high part in memory.
955           SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
956           PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
957           Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
958                                        Hi, PtrOff, DAG.getSrcValue(NULL)));
959           ArgOffset += 4;
960         }
961         break;
962       }
963       // Fall through
964     case MVT::f64:
965       SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
966       PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
967       Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
968                                    Args[i].first, PtrOff,
969                                    DAG.getSrcValue(NULL)));
970       ArgOffset += 8;
971       break;
972     }
973   }
974   if (!Stores.empty())
975     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
976
977   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
978   // arguments and the arguments after the retaddr has been pushed are aligned.
979   if ((ArgOffset & 7) == 0)
980     ArgOffset += 4;
981
982   std::vector<MVT::ValueType> RetVals;
983   MVT::ValueType RetTyVT = getValueType(RetTy);
984
985   RetVals.push_back(MVT::Other);
986
987   // The result values produced have to be legal.  Promote the result.
988   switch (RetTyVT) {
989   case MVT::isVoid: break;
990   default:
991     RetVals.push_back(RetTyVT);
992     break;
993   case MVT::i1:
994   case MVT::i8:
995   case MVT::i16:
996     RetVals.push_back(MVT::i32);
997     break;
998   case MVT::f32:
999     if (X86ScalarSSE)
1000       RetVals.push_back(MVT::f32);
1001     else
1002       RetVals.push_back(MVT::f64);
1003     break;
1004   case MVT::i64:
1005     RetVals.push_back(MVT::i32);
1006     RetVals.push_back(MVT::i32);
1007     break;
1008   }
1009
1010   // Build a sequence of copy-to-reg nodes chained together with token chain
1011   // and flag operands which copy the outgoing args into registers.
1012   SDOperand InFlag;
1013   for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1014     unsigned CCReg;
1015     SDOperand RegToPass = RegValuesToPass[i];
1016     switch (RegToPass.getValueType()) {
1017     default: assert(0 && "Bad thing to pass in regs");
1018     case MVT::i8:
1019       CCReg = (i == 0) ? X86::AL  : X86::DL;
1020       break;
1021     case MVT::i16:
1022       CCReg = (i == 0) ? X86::AX  : X86::DX;
1023       break;
1024     case MVT::i32:
1025       CCReg = (i == 0) ? X86::EAX : X86::EDX;
1026       break;
1027     }
1028
1029     Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1030     InFlag = Chain.getValue(1);
1031   }
1032
1033   std::vector<MVT::ValueType> NodeTys;
1034   NodeTys.push_back(MVT::Other);   // Returns a chain
1035   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
1036   std::vector<SDOperand> Ops;
1037   Ops.push_back(Chain);
1038   Ops.push_back(Callee);
1039   if (InFlag.Val)
1040     Ops.push_back(InFlag);
1041
1042   // FIXME: Do not generate X86ISD::TAILCALL for now.
1043   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1044   InFlag = Chain.getValue(1);
1045
1046   NodeTys.clear();
1047   NodeTys.push_back(MVT::Other);   // Returns a chain
1048   NodeTys.push_back(MVT::Flag);    // Returns a flag for retval copy to use.
1049   Ops.clear();
1050   Ops.push_back(Chain);
1051   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1052   Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1053   Ops.push_back(InFlag);
1054   Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1055   InFlag = Chain.getValue(1);
1056   
1057   SDOperand RetVal;
1058   if (RetTyVT != MVT::isVoid) {
1059     switch (RetTyVT) {
1060     default: assert(0 && "Unknown value type to return!");
1061     case MVT::i1:
1062     case MVT::i8:
1063       RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1064       Chain = RetVal.getValue(1);
1065       if (RetTyVT == MVT::i1) 
1066         RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1067       break;
1068     case MVT::i16:
1069       RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1070       Chain = RetVal.getValue(1);
1071       break;
1072     case MVT::i32:
1073       RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1074       Chain = RetVal.getValue(1);
1075       break;
1076     case MVT::i64: {
1077       SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1078       SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32, 
1079                                         Lo.getValue(2));
1080       RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1081       Chain = Hi.getValue(1);
1082       break;
1083     }
1084     case MVT::f32:
1085     case MVT::f64: {
1086       std::vector<MVT::ValueType> Tys;
1087       Tys.push_back(MVT::f64);
1088       Tys.push_back(MVT::Other);
1089       Tys.push_back(MVT::Flag);
1090       std::vector<SDOperand> Ops;
1091       Ops.push_back(Chain);
1092       Ops.push_back(InFlag);
1093       RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1094       Chain  = RetVal.getValue(1);
1095       InFlag = RetVal.getValue(2);
1096       if (X86ScalarSSE) {
1097         // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1098         // shouldn't be necessary except that RFP cannot be live across
1099         // multiple blocks. When stackifier is fixed, they can be uncoupled.
1100         MachineFunction &MF = DAG.getMachineFunction();
1101         int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1102         SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1103         Tys.clear();
1104         Tys.push_back(MVT::Other);
1105         Ops.clear();
1106         Ops.push_back(Chain);
1107         Ops.push_back(RetVal);
1108         Ops.push_back(StackSlot);
1109         Ops.push_back(DAG.getValueType(RetTyVT));
1110         Ops.push_back(InFlag);
1111         Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1112         RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1113                              DAG.getSrcValue(NULL));
1114         Chain = RetVal.getValue(1);
1115       }
1116
1117       if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1118         // FIXME: we would really like to remember that this FP_ROUND
1119         // operation is okay to eliminate if we allow excess FP precision.
1120         RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1121       break;
1122     }
1123     }
1124   }
1125
1126   return std::make_pair(RetVal, Chain);
1127 }
1128
1129 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1130   if (ReturnAddrIndex == 0) {
1131     // Set up a frame object for the return address.
1132     MachineFunction &MF = DAG.getMachineFunction();
1133     ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1134   }
1135
1136   return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1137 }
1138
1139
1140
1141 std::pair<SDOperand, SDOperand> X86TargetLowering::
1142 LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1143                         SelectionDAG &DAG) {
1144   SDOperand Result;
1145   if (Depth)        // Depths > 0 not supported yet!
1146     Result = DAG.getConstant(0, getPointerTy());
1147   else {
1148     SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1149     if (!isFrameAddress)
1150       // Just load the return address
1151       Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1152                            DAG.getSrcValue(NULL));
1153     else
1154       Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1155                            DAG.getConstant(4, MVT::i32));
1156   }
1157   return std::make_pair(Result, Chain);
1158 }
1159
1160 /// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1161 /// which corresponds to the condition code.
1162 static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1163   switch (X86CC) {
1164   default: assert(0 && "Unknown X86 conditional code!");
1165   case X86ISD::COND_A:  return X86::JA;
1166   case X86ISD::COND_AE: return X86::JAE;
1167   case X86ISD::COND_B:  return X86::JB;
1168   case X86ISD::COND_BE: return X86::JBE;
1169   case X86ISD::COND_E:  return X86::JE;
1170   case X86ISD::COND_G:  return X86::JG;
1171   case X86ISD::COND_GE: return X86::JGE;
1172   case X86ISD::COND_L:  return X86::JL;
1173   case X86ISD::COND_LE: return X86::JLE;
1174   case X86ISD::COND_NE: return X86::JNE;
1175   case X86ISD::COND_NO: return X86::JNO;
1176   case X86ISD::COND_NP: return X86::JNP;
1177   case X86ISD::COND_NS: return X86::JNS;
1178   case X86ISD::COND_O:  return X86::JO;
1179   case X86ISD::COND_P:  return X86::JP;
1180   case X86ISD::COND_S:  return X86::JS;
1181   }
1182 }
1183
1184 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1185 /// specific condition code. It returns a false if it cannot do a direct
1186 /// translation. X86CC is the translated CondCode. Flip is set to true if the
1187 /// the order of comparison operands should be flipped.
1188 static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1189                            bool &Flip) {
1190   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1191   Flip = false;
1192   X86CC = X86ISD::COND_INVALID;
1193   if (!isFP) {
1194     switch (SetCCOpcode) {
1195     default: break;
1196     case ISD::SETEQ:  X86CC = X86ISD::COND_E;  break;
1197     case ISD::SETGT:  X86CC = X86ISD::COND_G;  break;
1198     case ISD::SETGE:  X86CC = X86ISD::COND_GE; break;
1199     case ISD::SETLT:  X86CC = X86ISD::COND_L;  break;
1200     case ISD::SETLE:  X86CC = X86ISD::COND_LE; break;
1201     case ISD::SETNE:  X86CC = X86ISD::COND_NE; break;
1202     case ISD::SETULT: X86CC = X86ISD::COND_B;  break;
1203     case ISD::SETUGT: X86CC = X86ISD::COND_A;  break;
1204     case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1205     case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1206     }
1207   } else {
1208     // On a floating point condition, the flags are set as follows:
1209     // ZF  PF  CF   op
1210     //  0 | 0 | 0 | X > Y
1211     //  0 | 0 | 1 | X < Y
1212     //  1 | 0 | 0 | X == Y
1213     //  1 | 1 | 1 | unordered
1214     switch (SetCCOpcode) {
1215     default: break;
1216     case ISD::SETUEQ:
1217     case ISD::SETEQ: X86CC = X86ISD::COND_E;  break;
1218     case ISD::SETOLE: Flip = true; // Fallthrough
1219     case ISD::SETOGT:
1220     case ISD::SETGT: X86CC = X86ISD::COND_A;  break;
1221     case ISD::SETOLT: Flip = true; // Fallthrough
1222     case ISD::SETOGE:
1223     case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
1224     case ISD::SETUGE: Flip = true; // Fallthrough
1225     case ISD::SETULT:
1226     case ISD::SETLT: X86CC = X86ISD::COND_B;  break;
1227     case ISD::SETUGT: Flip = true; // Fallthrough
1228     case ISD::SETULE:
1229     case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1230     case ISD::SETONE:
1231     case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1232     case ISD::SETUO: X86CC = X86ISD::COND_P;  break;
1233     case ISD::SETO:  X86CC = X86ISD::COND_NP; break;
1234     }
1235   }
1236
1237   return X86CC != X86ISD::COND_INVALID;
1238 }
1239
1240 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
1241 /// code. Current x86 isa includes the following FP cmov instructions:
1242 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
1243 static bool hasFPCMov(unsigned X86CC) {
1244   switch (X86CC) {
1245   default:
1246     return false;
1247   case X86ISD::COND_B:
1248   case X86ISD::COND_BE:
1249   case X86ISD::COND_E:
1250   case X86ISD::COND_P:
1251   case X86ISD::COND_A:
1252   case X86ISD::COND_AE:
1253   case X86ISD::COND_NE:
1254   case X86ISD::COND_NP:
1255     return true;
1256   }
1257 }
1258
1259 MachineBasicBlock *
1260 X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1261                                            MachineBasicBlock *BB) {
1262   switch (MI->getOpcode()) {
1263   default: assert(false && "Unexpected instr type to insert");
1264   case X86::CMOV_FR32:
1265   case X86::CMOV_FR64: {
1266     // To "insert" a SELECT_CC instruction, we actually have to insert the
1267     // diamond control-flow pattern.  The incoming instruction knows the
1268     // destination vreg to set, the condition code register to branch on, the
1269     // true/false values to select between, and a branch opcode to use.
1270     const BasicBlock *LLVM_BB = BB->getBasicBlock();
1271     ilist<MachineBasicBlock>::iterator It = BB;
1272     ++It;
1273   
1274     //  thisMBB:
1275     //  ...
1276     //   TrueVal = ...
1277     //   cmpTY ccX, r1, r2
1278     //   bCC copy1MBB
1279     //   fallthrough --> copy0MBB
1280     MachineBasicBlock *thisMBB = BB;
1281     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1282     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1283     unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1284     BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1285     MachineFunction *F = BB->getParent();
1286     F->getBasicBlockList().insert(It, copy0MBB);
1287     F->getBasicBlockList().insert(It, sinkMBB);
1288     // Update machine-CFG edges by first adding all successors of the current
1289     // block to the new block which will contain the Phi node for the select.
1290     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(), 
1291         e = BB->succ_end(); i != e; ++i)
1292       sinkMBB->addSuccessor(*i);
1293     // Next, remove all successors of the current block, and add the true
1294     // and fallthrough blocks as its successors.
1295     while(!BB->succ_empty())
1296       BB->removeSuccessor(BB->succ_begin());
1297     BB->addSuccessor(copy0MBB);
1298     BB->addSuccessor(sinkMBB);
1299   
1300     //  copy0MBB:
1301     //   %FalseValue = ...
1302     //   # fallthrough to sinkMBB
1303     BB = copy0MBB;
1304   
1305     // Update machine-CFG edges
1306     BB->addSuccessor(sinkMBB);
1307   
1308     //  sinkMBB:
1309     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1310     //  ...
1311     BB = sinkMBB;
1312     BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1313       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1314       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
1315
1316     delete MI;   // The pseudo instruction is gone now.
1317     return BB;
1318   }
1319
1320   case X86::FP_TO_INT16_IN_MEM:
1321   case X86::FP_TO_INT32_IN_MEM:
1322   case X86::FP_TO_INT64_IN_MEM: {
1323     // Change the floating point control register to use "round towards zero"
1324     // mode when truncating to an integer value.
1325     MachineFunction *F = BB->getParent();
1326     int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1327     addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1328
1329     // Load the old value of the high byte of the control word...
1330     unsigned OldCW =
1331       F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1332     addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1333
1334     // Set the high part to be round to zero...
1335     addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1336
1337     // Reload the modified control word now...
1338     addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1339
1340     // Restore the memory image of control word to original value
1341     addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1342
1343     // Get the X86 opcode to use.
1344     unsigned Opc;
1345     switch (MI->getOpcode()) {
1346     default: assert(0 && "illegal opcode!");
1347     case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1348     case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1349     case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1350     }
1351
1352     X86AddressMode AM;
1353     MachineOperand &Op = MI->getOperand(0);
1354     if (Op.isRegister()) {
1355       AM.BaseType = X86AddressMode::RegBase;
1356       AM.Base.Reg = Op.getReg();
1357     } else {
1358       AM.BaseType = X86AddressMode::FrameIndexBase;
1359       AM.Base.FrameIndex = Op.getFrameIndex();
1360     }
1361     Op = MI->getOperand(1);
1362     if (Op.isImmediate())
1363       AM.Scale = Op.getImmedValue();
1364     Op = MI->getOperand(2);
1365     if (Op.isImmediate())
1366       AM.IndexReg = Op.getImmedValue();
1367     Op = MI->getOperand(3);
1368     if (Op.isGlobalAddress()) {
1369       AM.GV = Op.getGlobal();
1370     } else {
1371       AM.Disp = Op.getImmedValue();
1372     }
1373     addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1374
1375     // Reload the original control word now.
1376     addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1377
1378     delete MI;   // The pseudo instruction is gone now.
1379     return BB;
1380   }
1381   }
1382 }
1383
1384
1385 //===----------------------------------------------------------------------===//
1386 //                           X86 Custom Lowering Hooks
1387 //===----------------------------------------------------------------------===//
1388
1389 /// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1390 /// load. For Darwin, external and weak symbols are indirect, loading the value
1391 /// at address GV rather then the value of GV itself. This means that the
1392 /// GlobalAddress must be in the base or index register of the address, not the
1393 /// GV offset field.
1394 static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1395   return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1396           (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1397 }
1398
1399 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1400 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
1401 bool X86::isPSHUFDMask(SDNode *N) {
1402   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1403
1404   if (N->getNumOperands() != 4)
1405     return false;
1406
1407   // Check if the value doesn't reference the second vector.
1408   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
1409     SDOperand Arg = N->getOperand(i);
1410     if (Arg.getOpcode() == ISD::UNDEF) continue;
1411     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1412     if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
1413       return false;
1414   }
1415
1416   return true;
1417 }
1418
1419 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1420 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
1421 bool X86::isPSHUFHWMask(SDNode *N) {
1422   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1423
1424   if (N->getNumOperands() != 8)
1425     return false;
1426
1427   // Lower quadword copied in order.
1428   for (unsigned i = 0; i != 4; ++i) {
1429     SDOperand Arg = N->getOperand(i);
1430     if (Arg.getOpcode() == ISD::UNDEF) continue;
1431     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1432     if (cast<ConstantSDNode>(Arg)->getValue() != i)
1433       return false;
1434   }
1435
1436   // Upper quadword shuffled.
1437   for (unsigned i = 4; i != 8; ++i) {
1438     SDOperand Arg = N->getOperand(i);
1439     if (Arg.getOpcode() == ISD::UNDEF) continue;
1440     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1441     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1442     if (Val < 4 || Val > 7)
1443       return false;
1444   }
1445
1446   return true;
1447 }
1448
1449 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1450 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
1451 bool X86::isPSHUFLWMask(SDNode *N) {
1452   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1453
1454   if (N->getNumOperands() != 8)
1455     return false;
1456
1457   // Upper quadword copied in order.
1458   for (unsigned i = 4; i != 8; ++i) {
1459     SDOperand Arg = N->getOperand(i);
1460     if (Arg.getOpcode() == ISD::UNDEF) continue;
1461     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1462     if (cast<ConstantSDNode>(Arg)->getValue() != i)
1463       return false;
1464   }
1465
1466   // Lower quadword shuffled.
1467   for (unsigned i = 0; i != 4; ++i) {
1468     SDOperand Arg = N->getOperand(i);
1469     if (Arg.getOpcode() == ISD::UNDEF) continue;
1470     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1471     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1472     if (Val > 4)
1473       return false;
1474   }
1475
1476   return true;
1477 }
1478
1479 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1480 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
1481 bool X86::isSHUFPMask(SDNode *N) {
1482   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1483
1484   unsigned NumElems = N->getNumOperands();
1485   if (NumElems == 2) {
1486     // The only case that ought be handled by SHUFPD is
1487     // Dest { 2, 1 } <=  shuffle( Dest { 1, 0 },  Src { 3, 2 }
1488     // Expect bit 0 == 1, bit1 == 2
1489     SDOperand Bit0 = N->getOperand(0);
1490     if (Bit0.getOpcode() != ISD::UNDEF) {
1491       assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1492       if (cast<ConstantSDNode>(Bit0)->getValue() != 1)
1493         return false;
1494     }
1495
1496     SDOperand Bit1 = N->getOperand(1);
1497     if (Bit1.getOpcode() != ISD::UNDEF) {
1498       assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1499       if (cast<ConstantSDNode>(Bit1)->getValue() != 2)
1500         return false;
1501     }
1502
1503     return true;
1504   }
1505
1506   if (NumElems != 4) return false;
1507
1508   // Each half must refer to only one of the vector.
1509   for (unsigned i = 0; i < 2; ++i) {
1510     SDOperand Arg = N->getOperand(i);
1511     if (Arg.getOpcode() == ISD::UNDEF) continue;
1512     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1513     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1514     if (Val >= 4) return false;
1515   }
1516   for (unsigned i = 2; i < 4; ++i) {
1517     SDOperand Arg = N->getOperand(i);
1518     if (Arg.getOpcode() == ISD::UNDEF) continue;
1519     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1520     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1521     if (Val < 4) return false;
1522   }
1523
1524   return true;
1525 }
1526
1527 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1528 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1529 bool X86::isMOVHLPSMask(SDNode *N) {
1530   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1531
1532   if (N->getNumOperands() != 4)
1533     return false;
1534
1535   // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
1536   SDOperand Bit0 = N->getOperand(0);
1537   SDOperand Bit1 = N->getOperand(1);
1538   SDOperand Bit2 = N->getOperand(2);
1539   SDOperand Bit3 = N->getOperand(3);
1540
1541   if (Bit0.getOpcode() != ISD::UNDEF) {
1542     assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1543     if (cast<ConstantSDNode>(Bit0)->getValue() != 6)
1544       return false;
1545   }
1546
1547   if (Bit1.getOpcode() != ISD::UNDEF) {
1548     assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1549     if (cast<ConstantSDNode>(Bit1)->getValue() != 7)
1550       return false;
1551   }
1552
1553   if (Bit2.getOpcode() != ISD::UNDEF) {
1554     assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1555     if (cast<ConstantSDNode>(Bit2)->getValue() != 2)
1556       return false;
1557   }
1558
1559   if (Bit3.getOpcode() != ISD::UNDEF) {
1560     assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1561     if (cast<ConstantSDNode>(Bit3)->getValue() != 3)
1562       return false;
1563   }
1564
1565   return true;
1566 }
1567
1568 /// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1569 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1570 bool X86::isMOVLHPSMask(SDNode *N) {
1571   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1572
1573   if (N->getNumOperands() != 4)
1574     return false;
1575
1576   // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
1577   SDOperand Bit0 = N->getOperand(0);
1578   SDOperand Bit1 = N->getOperand(1);
1579   SDOperand Bit2 = N->getOperand(2);
1580   SDOperand Bit3 = N->getOperand(3);
1581
1582   if (Bit0.getOpcode() != ISD::UNDEF) {
1583     assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1584     if (cast<ConstantSDNode>(Bit0)->getValue() != 0)
1585       return false;
1586   }
1587
1588   if (Bit1.getOpcode() != ISD::UNDEF) {
1589     assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1590     if (cast<ConstantSDNode>(Bit1)->getValue() != 1)
1591       return false;
1592   }
1593
1594   if (Bit2.getOpcode() != ISD::UNDEF) {
1595     assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1596     if (cast<ConstantSDNode>(Bit2)->getValue() != 4)
1597       return false;
1598   }
1599
1600   if (Bit3.getOpcode() != ISD::UNDEF) {
1601     assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1602     if (cast<ConstantSDNode>(Bit3)->getValue() != 5)
1603       return false;
1604   }
1605
1606   return true;
1607 }
1608
1609 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1610 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
1611 bool X86::isUNPCKLMask(SDNode *N) {
1612   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1613
1614   unsigned NumElems = N->getNumOperands();
1615   if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1616     return false;
1617
1618   for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1619     SDOperand BitI  = N->getOperand(i);
1620     SDOperand BitI1 = N->getOperand(i+1);
1621
1622     if (BitI.getOpcode() != ISD::UNDEF) {
1623       assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1624       if (cast<ConstantSDNode>(BitI)->getValue() != j)
1625         return false;
1626     }
1627
1628     if (BitI1.getOpcode() != ISD::UNDEF) {
1629       assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1630       if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
1631         return false;
1632     }
1633   }
1634
1635   return true;
1636 }
1637
1638 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1639 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
1640 bool X86::isUNPCKHMask(SDNode *N) {
1641   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1642
1643   unsigned NumElems = N->getNumOperands();
1644   if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1645     return false;
1646
1647   for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1648     SDOperand BitI  = N->getOperand(i);
1649     SDOperand BitI1 = N->getOperand(i+1);
1650
1651     if (BitI.getOpcode() != ISD::UNDEF) {
1652       assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1653       if (cast<ConstantSDNode>(BitI)->getValue() != j + NumElems/2)
1654         return false;
1655     }
1656
1657     if (BitI1.getOpcode() != ISD::UNDEF) {
1658       assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1659       if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
1660         return false;
1661     }
1662   }
1663
1664   return true;
1665 }
1666
1667 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1668 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1669 /// <0, 0, 1, 1>
1670 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1671   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1672
1673   unsigned NumElems = N->getNumOperands();
1674   if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1675     return false;
1676
1677   for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1678     SDOperand BitI  = N->getOperand(i);
1679     SDOperand BitI1 = N->getOperand(i+1);
1680
1681     if (BitI.getOpcode() != ISD::UNDEF) {
1682       assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1683       if (cast<ConstantSDNode>(BitI)->getValue() != j)
1684         return false;
1685     }
1686
1687     if (BitI1.getOpcode() != ISD::UNDEF) {
1688       assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1689       if (cast<ConstantSDNode>(BitI1)->getValue() != j)
1690         return false;
1691     }
1692   }
1693
1694   return true;
1695 }
1696
1697
1698 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1699 /// a splat of a single element.
1700 bool X86::isSplatMask(SDNode *N) {
1701   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1702
1703   // We can only splat 64-bit, and 32-bit quantities.
1704   if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1705     return false;
1706
1707   // This is a splat operation if each element of the permute is the same, and
1708   // if the value doesn't reference the second vector.
1709   SDOperand Elt = N->getOperand(0);
1710   assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1711   for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1712     SDOperand Arg = N->getOperand(i);
1713     if (Arg.getOpcode() == ISD::UNDEF) continue;
1714     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1715     if (Arg != Elt) return false;
1716   }
1717
1718   // Make sure it is a splat of the first vector operand.
1719   return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1720 }
1721
1722 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1723 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1724 /// instructions.
1725 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1726   unsigned NumOperands = N->getNumOperands();
1727   unsigned Shift = (NumOperands == 4) ? 2 : 1;
1728   unsigned Mask = 0;
1729   for (unsigned i = 0; i < NumOperands; ++i) {
1730     unsigned Val = 0;
1731     SDOperand Arg = N->getOperand(NumOperands-i-1);
1732     if (Arg.getOpcode() != ISD::UNDEF)
1733       Val = cast<ConstantSDNode>(Arg)->getValue();
1734     if (Val >= NumOperands) Val -= NumOperands;
1735     Mask |= Val;
1736     if (i != NumOperands - 1)
1737       Mask <<= Shift;
1738   }
1739
1740   return Mask;
1741 }
1742
1743 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1744 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1745 /// instructions.
1746 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1747   unsigned Mask = 0;
1748   // 8 nodes, but we only care about the last 4.
1749   for (unsigned i = 7; i >= 4; --i) {
1750     unsigned Val = 0;
1751     SDOperand Arg = N->getOperand(i);
1752     if (Arg.getOpcode() != ISD::UNDEF)
1753       Val = cast<ConstantSDNode>(Arg)->getValue();
1754     Mask |= (Val - 4);
1755     if (i != 4)
1756       Mask <<= 2;
1757   }
1758
1759   return Mask;
1760 }
1761
1762 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1763 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1764 /// instructions.
1765 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1766   unsigned Mask = 0;
1767   // 8 nodes, but we only care about the first 4.
1768   for (int i = 3; i >= 0; --i) {
1769     unsigned Val = 0;
1770     SDOperand Arg = N->getOperand(i);
1771     if (Arg.getOpcode() != ISD::UNDEF)
1772       Val = cast<ConstantSDNode>(Arg)->getValue();
1773     Mask |= Val;
1774     if (i != 0)
1775       Mask <<= 2;
1776   }
1777
1778   return Mask;
1779 }
1780
1781 /// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1782 /// values in ther permute mask if needed. Use V1 as second vector if it is
1783 /// undef. Return an empty SDOperand is it is already well formed.
1784 static SDOperand NormalizeVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1785   SDOperand V1 = Op.getOperand(0);
1786   SDOperand V2 = Op.getOperand(1);
1787   SDOperand Mask = Op.getOperand(2);
1788   MVT::ValueType VT = Op.getValueType();
1789   unsigned NumElems = Mask.getNumOperands();
1790   SDOperand Half1 = Mask.getOperand(0);
1791   SDOperand Half2 = Mask.getOperand(NumElems/2);
1792   bool V2Undef = false;
1793   if (V2.getOpcode() == ISD::UNDEF) {
1794     V2Undef = true;
1795     V2 = V1;
1796   }
1797
1798   if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1799       cast<ConstantSDNode>(Half2)->getValue() <  NumElems) {
1800     // Swap the operands and change mask.
1801     std::vector<SDOperand> MaskVec;
1802     for (unsigned i = NumElems / 2; i != NumElems; ++i)
1803       MaskVec.push_back(Mask.getOperand(i));
1804     for (unsigned i = 0; i != NumElems / 2; ++i)
1805       MaskVec.push_back(Mask.getOperand(i));
1806     Mask =
1807       DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1808     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1809   }
1810
1811   if (V2Undef)
1812     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1813
1814   return Op;
1815 }
1816
1817 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1818 /// specifies a 8 element shuffle that can be broken into a pair of
1819 /// PSHUFHW and PSHUFLW.
1820 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1821   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1822
1823   if (N->getNumOperands() != 8)
1824     return false;
1825
1826   // Lower quadword shuffled.
1827   for (unsigned i = 0; i != 4; ++i) {
1828     SDOperand Arg = N->getOperand(i);
1829     if (Arg.getOpcode() == ISD::UNDEF) continue;
1830     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1831     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1832     if (Val > 4)
1833       return false;
1834   }
1835
1836   // Upper quadword shuffled.
1837   for (unsigned i = 4; i != 8; ++i) {
1838     SDOperand Arg = N->getOperand(i);
1839     if (Arg.getOpcode() == ISD::UNDEF) continue;
1840     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1841     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1842     if (Val < 4 || Val > 7)
1843       return false;
1844   }
1845
1846   return true;
1847 }
1848
1849 /// LowerOperation - Provide custom lowering hooks for some operations.
1850 ///
1851 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1852   switch (Op.getOpcode()) {
1853   default: assert(0 && "Should not custom lower this!");
1854   case ISD::SHL_PARTS:
1855   case ISD::SRA_PARTS:
1856   case ISD::SRL_PARTS: {
1857     assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1858            "Not an i64 shift!");
1859     bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1860     SDOperand ShOpLo = Op.getOperand(0);
1861     SDOperand ShOpHi = Op.getOperand(1);
1862     SDOperand ShAmt  = Op.getOperand(2);
1863     SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1864                                          DAG.getConstant(31, MVT::i8))
1865                            : DAG.getConstant(0, MVT::i32);
1866
1867     SDOperand Tmp2, Tmp3;
1868     if (Op.getOpcode() == ISD::SHL_PARTS) {
1869       Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1870       Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1871     } else {
1872       Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1873       Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1874     }
1875
1876     SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1877                                    ShAmt, DAG.getConstant(32, MVT::i8));
1878
1879     SDOperand Hi, Lo;
1880     SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1881
1882     std::vector<MVT::ValueType> Tys;
1883     Tys.push_back(MVT::i32);
1884     Tys.push_back(MVT::Flag);
1885     std::vector<SDOperand> Ops;
1886     if (Op.getOpcode() == ISD::SHL_PARTS) {
1887       Ops.push_back(Tmp2);
1888       Ops.push_back(Tmp3);
1889       Ops.push_back(CC);
1890       Ops.push_back(InFlag);
1891       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1892       InFlag = Hi.getValue(1);
1893
1894       Ops.clear();
1895       Ops.push_back(Tmp3);
1896       Ops.push_back(Tmp1);
1897       Ops.push_back(CC);
1898       Ops.push_back(InFlag);
1899       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1900     } else {
1901       Ops.push_back(Tmp2);
1902       Ops.push_back(Tmp3);
1903       Ops.push_back(CC);
1904       Ops.push_back(InFlag);
1905       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1906       InFlag = Lo.getValue(1);
1907
1908       Ops.clear();
1909       Ops.push_back(Tmp3);
1910       Ops.push_back(Tmp1);
1911       Ops.push_back(CC);
1912       Ops.push_back(InFlag);
1913       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1914     }
1915
1916     Tys.clear();
1917     Tys.push_back(MVT::i32);
1918     Tys.push_back(MVT::i32);
1919     Ops.clear();
1920     Ops.push_back(Lo);
1921     Ops.push_back(Hi);
1922     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1923   }
1924   case ISD::SINT_TO_FP: {
1925     assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1926            Op.getOperand(0).getValueType() >= MVT::i16 &&
1927            "Unknown SINT_TO_FP to lower!");
1928
1929     SDOperand Result;
1930     MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1931     unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1932     MachineFunction &MF = DAG.getMachineFunction();
1933     int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1934     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1935     SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1936                                   DAG.getEntryNode(), Op.getOperand(0),
1937                                   StackSlot, DAG.getSrcValue(NULL));
1938
1939     // Build the FILD
1940     std::vector<MVT::ValueType> Tys;
1941     Tys.push_back(MVT::f64);
1942     Tys.push_back(MVT::Other);
1943     if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1944     std::vector<SDOperand> Ops;
1945     Ops.push_back(Chain);
1946     Ops.push_back(StackSlot);
1947     Ops.push_back(DAG.getValueType(SrcVT));
1948     Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1949                          Tys, Ops);
1950
1951     if (X86ScalarSSE) {
1952       Chain = Result.getValue(1);
1953       SDOperand InFlag = Result.getValue(2);
1954
1955       // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1956       // shouldn't be necessary except that RFP cannot be live across
1957       // multiple blocks. When stackifier is fixed, they can be uncoupled.
1958       MachineFunction &MF = DAG.getMachineFunction();
1959       int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1960       SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1961       std::vector<MVT::ValueType> Tys;
1962       Tys.push_back(MVT::Other);
1963       std::vector<SDOperand> Ops;
1964       Ops.push_back(Chain);
1965       Ops.push_back(Result);
1966       Ops.push_back(StackSlot);
1967       Ops.push_back(DAG.getValueType(Op.getValueType()));
1968       Ops.push_back(InFlag);
1969       Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1970       Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1971                            DAG.getSrcValue(NULL));
1972     }
1973
1974     return Result;
1975   }
1976   case ISD::FP_TO_SINT: {
1977     assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1978            "Unknown FP_TO_SINT to lower!");
1979     // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1980     // stack slot.
1981     MachineFunction &MF = DAG.getMachineFunction();
1982     unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1983     int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1984     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1985
1986     unsigned Opc;
1987     switch (Op.getValueType()) {
1988     default: assert(0 && "Invalid FP_TO_SINT to lower!");
1989     case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1990     case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1991     case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1992     }
1993
1994     SDOperand Chain = DAG.getEntryNode();
1995     SDOperand Value = Op.getOperand(0);
1996     if (X86ScalarSSE) {
1997       assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1998       Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, 
1999                           DAG.getSrcValue(0));
2000       std::vector<MVT::ValueType> Tys;
2001       Tys.push_back(MVT::f64);
2002       Tys.push_back(MVT::Other);
2003       std::vector<SDOperand> Ops;
2004       Ops.push_back(Chain);
2005       Ops.push_back(StackSlot);
2006       Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
2007       Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2008       Chain = Value.getValue(1);
2009       SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2010       StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2011     }
2012
2013     // Build the FP_TO_INT*_IN_MEM
2014     std::vector<SDOperand> Ops;
2015     Ops.push_back(Chain);
2016     Ops.push_back(Value);
2017     Ops.push_back(StackSlot);
2018     SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2019
2020     // Load the result.
2021     return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2022                        DAG.getSrcValue(NULL));
2023   }
2024   case ISD::READCYCLECOUNTER: {
2025     std::vector<MVT::ValueType> Tys;
2026     Tys.push_back(MVT::Other);
2027     Tys.push_back(MVT::Flag);
2028     std::vector<SDOperand> Ops;
2029     Ops.push_back(Op.getOperand(0));
2030     SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
2031     Ops.clear();
2032     Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2033     Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, 
2034                                      MVT::i32, Ops[0].getValue(2)));
2035     Ops.push_back(Ops[1].getValue(1));
2036     Tys[0] = Tys[1] = MVT::i32;
2037     Tys.push_back(MVT::Other);
2038     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2039   }
2040   case ISD::FABS: {
2041     MVT::ValueType VT = Op.getValueType();
2042     const Type *OpNTy =  MVT::getTypeForValueType(VT);
2043     std::vector<Constant*> CV;
2044     if (VT == MVT::f64) {
2045       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2046       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2047     } else {
2048       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2049       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2050       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2051       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2052     }
2053     Constant *CS = ConstantStruct::get(CV);
2054     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2055     SDOperand Mask 
2056       = DAG.getNode(X86ISD::LOAD_PACK,
2057                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2058     return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2059   }
2060   case ISD::FNEG: {
2061     MVT::ValueType VT = Op.getValueType();
2062     const Type *OpNTy =  MVT::getTypeForValueType(VT);
2063     std::vector<Constant*> CV;
2064     if (VT == MVT::f64) {
2065       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2066       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2067     } else {
2068       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2069       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2070       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2071       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2072     }
2073     Constant *CS = ConstantStruct::get(CV);
2074     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2075     SDOperand Mask 
2076       = DAG.getNode(X86ISD::LOAD_PACK,
2077                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2078     return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2079   }
2080   case ISD::SETCC: {
2081     assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
2082     SDOperand Cond;
2083     SDOperand CC = Op.getOperand(2);
2084     ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2085     bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
2086     bool Flip;
2087     unsigned X86CC;
2088     if (translateX86CC(CC, isFP, X86CC, Flip)) {
2089       if (Flip)
2090         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2091                            Op.getOperand(1), Op.getOperand(0));
2092       else
2093         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2094                            Op.getOperand(0), Op.getOperand(1));
2095       return DAG.getNode(X86ISD::SETCC, MVT::i8, 
2096                          DAG.getConstant(X86CC, MVT::i8), Cond);
2097     } else {
2098       assert(isFP && "Illegal integer SetCC!");
2099
2100       Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2101                          Op.getOperand(0), Op.getOperand(1));
2102       std::vector<MVT::ValueType> Tys;
2103       std::vector<SDOperand> Ops;
2104       switch (SetCCOpcode) {
2105       default: assert(false && "Illegal floating point SetCC!");
2106       case ISD::SETOEQ: {  // !PF & ZF
2107         Tys.push_back(MVT::i8);
2108         Tys.push_back(MVT::Flag);
2109         Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2110         Ops.push_back(Cond);
2111         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2112         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2113                                      DAG.getConstant(X86ISD::COND_E, MVT::i8),
2114                                      Tmp1.getValue(1));
2115         return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2116       }
2117       case ISD::SETUNE: {  // PF | !ZF
2118         Tys.push_back(MVT::i8);
2119         Tys.push_back(MVT::Flag);
2120         Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2121         Ops.push_back(Cond);
2122         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2123         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2124                                      DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2125                                      Tmp1.getValue(1));
2126         return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2127       }
2128       }
2129     }
2130   }
2131   case ISD::SELECT: {
2132     MVT::ValueType VT = Op.getValueType();
2133     bool isFP      = MVT::isFloatingPoint(VT);
2134     bool isFPStack = isFP && !X86ScalarSSE;
2135     bool isFPSSE   = isFP && X86ScalarSSE;
2136     bool addTest   = false;
2137     SDOperand Op0 = Op.getOperand(0);
2138     SDOperand Cond, CC;
2139     if (Op0.getOpcode() == ISD::SETCC)
2140       Op0 = LowerOperation(Op0, DAG);
2141
2142     if (Op0.getOpcode() == X86ISD::SETCC) {
2143       // If condition flag is set by a X86ISD::CMP, then make a copy of it
2144       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2145       // have another use it will be eliminated.
2146       // If the X86ISD::SETCC has more than one use, then it's probably better
2147       // to use a test instead of duplicating the X86ISD::CMP (for register
2148       // pressure reason).
2149       if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
2150         if (!Op0.hasOneUse()) {
2151           std::vector<MVT::ValueType> Tys;
2152           for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2153             Tys.push_back(Op0.Val->getValueType(i));
2154           std::vector<SDOperand> Ops;
2155           for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2156             Ops.push_back(Op0.getOperand(i));
2157           Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2158         }
2159
2160         CC   = Op0.getOperand(0);
2161         Cond = Op0.getOperand(1);
2162         // Make a copy as flag result cannot be used by more than one.
2163         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2164                            Cond.getOperand(0), Cond.getOperand(1));
2165         addTest =
2166           isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
2167       } else
2168         addTest = true;
2169     } else
2170       addTest = true;
2171
2172     if (addTest) {
2173       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2174       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
2175     }
2176
2177     std::vector<MVT::ValueType> Tys;
2178     Tys.push_back(Op.getValueType());
2179     Tys.push_back(MVT::Flag);
2180     std::vector<SDOperand> Ops;
2181     // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2182     // condition is true.
2183     Ops.push_back(Op.getOperand(2));
2184     Ops.push_back(Op.getOperand(1));
2185     Ops.push_back(CC);
2186     Ops.push_back(Cond);
2187     return DAG.getNode(X86ISD::CMOV, Tys, Ops);
2188   }
2189   case ISD::BRCOND: {
2190     bool addTest = false;
2191     SDOperand Cond  = Op.getOperand(1);
2192     SDOperand Dest  = Op.getOperand(2);
2193     SDOperand CC;
2194     if (Cond.getOpcode() == ISD::SETCC)
2195       Cond = LowerOperation(Cond, DAG);
2196
2197     if (Cond.getOpcode() == X86ISD::SETCC) {
2198       // If condition flag is set by a X86ISD::CMP, then make a copy of it
2199       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2200       // have another use it will be eliminated.
2201       // If the X86ISD::SETCC has more than one use, then it's probably better
2202       // to use a test instead of duplicating the X86ISD::CMP (for register
2203       // pressure reason).
2204       if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
2205         if (!Cond.hasOneUse()) {
2206           std::vector<MVT::ValueType> Tys;
2207           for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2208             Tys.push_back(Cond.Val->getValueType(i));
2209           std::vector<SDOperand> Ops;
2210           for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2211             Ops.push_back(Cond.getOperand(i));
2212           Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2213         }
2214
2215         CC   = Cond.getOperand(0);
2216         Cond = Cond.getOperand(1);
2217         // Make a copy as flag result cannot be used by more than one.
2218         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2219                            Cond.getOperand(0), Cond.getOperand(1));
2220       } else
2221         addTest = true;
2222     } else
2223       addTest = true;
2224
2225     if (addTest) {
2226       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2227       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2228     }
2229     return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2230                        Op.getOperand(0), Op.getOperand(2), CC, Cond);
2231   }
2232   case ISD::MEMSET: {
2233     SDOperand InFlag(0, 0);
2234     SDOperand Chain = Op.getOperand(0);
2235     unsigned Align =
2236       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2237     if (Align == 0) Align = 1;
2238
2239     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2240     // If not DWORD aligned, call memset if size is less than the threshold.
2241     // It knows how to align to the right boundary first.
2242     if ((Align & 3) != 0 ||
2243         (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
2244       MVT::ValueType IntPtr = getPointerTy();
2245       const Type *IntPtrTy = getTargetData().getIntPtrType();
2246       std::vector<std::pair<SDOperand, const Type*> > Args;
2247       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2248       // Extend the ubyte argument to be an int value for the call.
2249       SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2250       Args.push_back(std::make_pair(Val, IntPtrTy));
2251       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2252       std::pair<SDOperand,SDOperand> CallResult =
2253         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2254                     DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2255       return CallResult.second;
2256     }
2257
2258     MVT::ValueType AVT;
2259     SDOperand Count;
2260     ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2261     unsigned BytesLeft = 0;
2262     bool TwoRepStos = false;
2263     if (ValC) {
2264       unsigned ValReg;
2265       unsigned Val = ValC->getValue() & 255;
2266
2267       // If the value is a constant, then we can potentially use larger sets.
2268       switch (Align & 3) {
2269       case 2:   // WORD aligned
2270         AVT = MVT::i16;
2271         Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2272         BytesLeft = I->getValue() % 2;
2273         Val    = (Val << 8) | Val;
2274         ValReg = X86::AX;
2275         break;
2276       case 0:   // DWORD aligned
2277         AVT = MVT::i32;
2278         if (I) {
2279           Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2280           BytesLeft = I->getValue() % 4;
2281         } else {
2282           Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2283                               DAG.getConstant(2, MVT::i8));
2284           TwoRepStos = true;
2285         }
2286         Val = (Val << 8)  | Val;
2287         Val = (Val << 16) | Val;
2288         ValReg = X86::EAX;
2289         break;
2290       default:  // Byte aligned
2291         AVT = MVT::i8;
2292         Count = Op.getOperand(3);
2293         ValReg = X86::AL;
2294         break;
2295       }
2296
2297       Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2298                                 InFlag);
2299       InFlag = Chain.getValue(1);
2300     } else {
2301       AVT = MVT::i8;
2302       Count  = Op.getOperand(3);
2303       Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2304       InFlag = Chain.getValue(1);
2305     }
2306
2307     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2308     InFlag = Chain.getValue(1);
2309     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2310     InFlag = Chain.getValue(1);
2311
2312     std::vector<MVT::ValueType> Tys;
2313     Tys.push_back(MVT::Other);
2314     Tys.push_back(MVT::Flag);
2315     std::vector<SDOperand> Ops;
2316     Ops.push_back(Chain);
2317     Ops.push_back(DAG.getValueType(AVT));
2318     Ops.push_back(InFlag);
2319     Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2320
2321     if (TwoRepStos) {
2322       InFlag = Chain.getValue(1);
2323       Count = Op.getOperand(3);
2324       MVT::ValueType CVT = Count.getValueType();
2325       SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2326                                    DAG.getConstant(3, CVT));
2327       Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2328       InFlag = Chain.getValue(1);
2329       Tys.clear();
2330       Tys.push_back(MVT::Other);
2331       Tys.push_back(MVT::Flag);
2332       Ops.clear();
2333       Ops.push_back(Chain);
2334       Ops.push_back(DAG.getValueType(MVT::i8));
2335       Ops.push_back(InFlag);
2336       Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2337     } else if (BytesLeft) {
2338       // Issue stores for the last 1 - 3 bytes.
2339       SDOperand Value;
2340       unsigned Val = ValC->getValue() & 255;
2341       unsigned Offset = I->getValue() - BytesLeft;
2342       SDOperand DstAddr = Op.getOperand(1);
2343       MVT::ValueType AddrVT = DstAddr.getValueType();
2344       if (BytesLeft >= 2) {
2345         Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2346         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2347                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2348                                         DAG.getConstant(Offset, AddrVT)),
2349                             DAG.getSrcValue(NULL));
2350         BytesLeft -= 2;
2351         Offset += 2;
2352       }
2353
2354       if (BytesLeft == 1) {
2355         Value = DAG.getConstant(Val, MVT::i8);
2356         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2357                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2358                                         DAG.getConstant(Offset, AddrVT)),
2359                             DAG.getSrcValue(NULL));
2360       }
2361     }
2362
2363     return Chain;
2364   }
2365   case ISD::MEMCPY: {
2366     SDOperand Chain = Op.getOperand(0);
2367     unsigned Align =
2368       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2369     if (Align == 0) Align = 1;
2370
2371     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2372     // If not DWORD aligned, call memcpy if size is less than the threshold.
2373     // It knows how to align to the right boundary first.
2374     if ((Align & 3) != 0 ||
2375         (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
2376       MVT::ValueType IntPtr = getPointerTy();
2377       const Type *IntPtrTy = getTargetData().getIntPtrType();
2378       std::vector<std::pair<SDOperand, const Type*> > Args;
2379       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2380       Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2381       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2382       std::pair<SDOperand,SDOperand> CallResult =
2383         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2384                     DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2385       return CallResult.second;
2386     }
2387
2388     MVT::ValueType AVT;
2389     SDOperand Count;
2390     unsigned BytesLeft = 0;
2391     bool TwoRepMovs = false;
2392     switch (Align & 3) {
2393     case 2:   // WORD aligned
2394       AVT = MVT::i16;
2395       Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2396       BytesLeft = I->getValue() % 2;
2397       break;
2398     case 0:   // DWORD aligned
2399       AVT = MVT::i32;
2400       if (I) {
2401         Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2402         BytesLeft = I->getValue() % 4;
2403       } else {
2404         Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2405                             DAG.getConstant(2, MVT::i8));
2406         TwoRepMovs = true;
2407       }
2408       break;
2409     default:  // Byte aligned
2410       AVT = MVT::i8;
2411       Count = Op.getOperand(3);
2412       break;
2413     }
2414
2415     SDOperand InFlag(0, 0);
2416     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2417     InFlag = Chain.getValue(1);
2418     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2419     InFlag = Chain.getValue(1);
2420     Chain  = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2421     InFlag = Chain.getValue(1);
2422
2423     std::vector<MVT::ValueType> Tys;
2424     Tys.push_back(MVT::Other);
2425     Tys.push_back(MVT::Flag);
2426     std::vector<SDOperand> Ops;
2427     Ops.push_back(Chain);
2428     Ops.push_back(DAG.getValueType(AVT));
2429     Ops.push_back(InFlag);
2430     Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2431
2432     if (TwoRepMovs) {
2433       InFlag = Chain.getValue(1);
2434       Count = Op.getOperand(3);
2435       MVT::ValueType CVT = Count.getValueType();
2436       SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2437                                    DAG.getConstant(3, CVT));
2438       Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2439       InFlag = Chain.getValue(1);
2440       Tys.clear();
2441       Tys.push_back(MVT::Other);
2442       Tys.push_back(MVT::Flag);
2443       Ops.clear();
2444       Ops.push_back(Chain);
2445       Ops.push_back(DAG.getValueType(MVT::i8));
2446       Ops.push_back(InFlag);
2447       Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2448     } else if (BytesLeft) {
2449       // Issue loads and stores for the last 1 - 3 bytes.
2450       unsigned Offset = I->getValue() - BytesLeft;
2451       SDOperand DstAddr = Op.getOperand(1);
2452       MVT::ValueType DstVT = DstAddr.getValueType();
2453       SDOperand SrcAddr = Op.getOperand(2);
2454       MVT::ValueType SrcVT = SrcAddr.getValueType();
2455       SDOperand Value;
2456       if (BytesLeft >= 2) {
2457         Value = DAG.getLoad(MVT::i16, Chain,
2458                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2459                                         DAG.getConstant(Offset, SrcVT)),
2460                             DAG.getSrcValue(NULL));
2461         Chain = Value.getValue(1);
2462         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2463                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
2464                                         DAG.getConstant(Offset, DstVT)),
2465                             DAG.getSrcValue(NULL));
2466         BytesLeft -= 2;
2467         Offset += 2;
2468       }
2469
2470       if (BytesLeft == 1) {
2471         Value = DAG.getLoad(MVT::i8, Chain,
2472                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2473                                         DAG.getConstant(Offset, SrcVT)),
2474                             DAG.getSrcValue(NULL));
2475         Chain = Value.getValue(1);
2476         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2477                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
2478                                         DAG.getConstant(Offset, DstVT)),
2479                             DAG.getSrcValue(NULL));
2480       }
2481     }
2482
2483     return Chain;
2484   }
2485
2486   // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2487   // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2488   // one of the above mentioned nodes. It has to be wrapped because otherwise
2489   // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2490   // be used to form addressing mode. These wrapped nodes will be selected
2491   // into MOV32ri.
2492   case ISD::ConstantPool: {
2493     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2494     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2495                          DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2496                                                    CP->getAlignment()));
2497     if (Subtarget->isTargetDarwin()) {
2498       // With PIC, the address is actually $g + Offset.
2499       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2500         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2501                 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);    
2502     }
2503
2504     return Result;
2505   }
2506   case ISD::GlobalAddress: {
2507     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2508     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2509                          DAG.getTargetGlobalAddress(GV, getPointerTy()));
2510     if (Subtarget->isTargetDarwin()) {
2511       // With PIC, the address is actually $g + Offset.
2512       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2513         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2514                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2515
2516       // For Darwin, external and weak symbols are indirect, so we want to load
2517       // the value at address GV, not the value of GV itself. This means that
2518       // the GlobalAddress must be in the base or index register of the address,
2519       // not the GV offset field.
2520       if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2521           DarwinGVRequiresExtraLoad(GV))
2522         Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2523                              Result, DAG.getSrcValue(NULL));
2524     }
2525
2526     return Result;
2527   }
2528   case ISD::ExternalSymbol: {
2529     const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2530     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2531                          DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2532     if (Subtarget->isTargetDarwin()) {
2533       // With PIC, the address is actually $g + Offset.
2534       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2535         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2536                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2537     }
2538
2539     return Result;
2540   }
2541   case ISD::VASTART: {
2542     // vastart just stores the address of the VarArgsFrameIndex slot into the
2543     // memory location argument.
2544     // FIXME: Replace MVT::i32 with PointerTy
2545     SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2546     return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, 
2547                        Op.getOperand(1), Op.getOperand(2));
2548   }
2549   case ISD::RET: {
2550     SDOperand Copy;
2551     
2552     switch(Op.getNumOperands()) {
2553     default:
2554       assert(0 && "Do not know how to return this many arguments!");
2555       abort();
2556     case 1: 
2557       return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2558                          DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2559     case 2: {
2560       MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2561       if (MVT::isInteger(ArgVT))
2562         Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2563                                 SDOperand());
2564       else if (!X86ScalarSSE) {
2565         std::vector<MVT::ValueType> Tys;
2566         Tys.push_back(MVT::Other);
2567         Tys.push_back(MVT::Flag);
2568         std::vector<SDOperand> Ops;
2569         Ops.push_back(Op.getOperand(0));
2570         Ops.push_back(Op.getOperand(1));
2571         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2572       } else {
2573         SDOperand MemLoc;
2574         SDOperand Chain = Op.getOperand(0);
2575         SDOperand Value = Op.getOperand(1);
2576
2577         if (Value.getOpcode() == ISD::LOAD &&
2578             (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
2579           Chain  = Value.getOperand(0);
2580           MemLoc = Value.getOperand(1);
2581         } else {
2582           // Spill the value to memory and reload it into top of stack.
2583           unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2584           MachineFunction &MF = DAG.getMachineFunction();
2585           int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2586           MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2587           Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), 
2588                               Value, MemLoc, DAG.getSrcValue(0));
2589         }
2590         std::vector<MVT::ValueType> Tys;
2591         Tys.push_back(MVT::f64);
2592         Tys.push_back(MVT::Other);
2593         std::vector<SDOperand> Ops;
2594         Ops.push_back(Chain);
2595         Ops.push_back(MemLoc);
2596         Ops.push_back(DAG.getValueType(ArgVT));
2597         Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2598         Tys.clear();
2599         Tys.push_back(MVT::Other);
2600         Tys.push_back(MVT::Flag);
2601         Ops.clear();
2602         Ops.push_back(Copy.getValue(1));
2603         Ops.push_back(Copy);
2604         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2605       }
2606       break;
2607     }
2608     case 3:
2609       Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), 
2610                               SDOperand());
2611       Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2612       break;
2613     }
2614     return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2615                        Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2616                        Copy.getValue(1));
2617   }
2618   case ISD::SCALAR_TO_VECTOR: {
2619     SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2620     return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2621   }
2622   case ISD::VECTOR_SHUFFLE: {
2623     SDOperand V1 = Op.getOperand(0);
2624     SDOperand V2 = Op.getOperand(1);
2625     SDOperand PermMask = Op.getOperand(2);
2626     MVT::ValueType VT = Op.getValueType();
2627     unsigned NumElems = PermMask.getNumOperands();
2628
2629     // Splat && PSHUFD's 2nd vector must be undef.
2630     if (X86::isSplatMask(PermMask.Val)) {
2631       if (V2.getOpcode() != ISD::UNDEF)
2632         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2633                            DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2634       return Op;
2635     }
2636
2637     if (X86::isUNPCKLMask(PermMask.Val) ||
2638         X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2639         X86::isUNPCKHMask(PermMask.Val))
2640       // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
2641       return Op;
2642
2643     if (NumElems == 2)
2644       return NormalizeVectorShuffle(Op, DAG);
2645
2646     // If VT is integer, try PSHUF* first, then SHUFP*.
2647     if (MVT::isInteger(VT)) {
2648       if (X86::isPSHUFDMask(PermMask.Val) ||
2649           X86::isPSHUFHWMask(PermMask.Val) ||
2650           X86::isPSHUFLWMask(PermMask.Val)) {
2651         if (V2.getOpcode() != ISD::UNDEF)
2652           return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2653                              DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2654         return Op;
2655       }
2656
2657       if (X86::isSHUFPMask(PermMask.Val))
2658         return NormalizeVectorShuffle(Op, DAG);
2659
2660       // Handle v8i16 shuffle high / low shuffle node pair.
2661       if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2662         MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2663         MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2664         std::vector<SDOperand> MaskVec;
2665         for (unsigned i = 0; i != 4; ++i)
2666           MaskVec.push_back(PermMask.getOperand(i));
2667         for (unsigned i = 4; i != 8; ++i)
2668           MaskVec.push_back(DAG.getConstant(i, BaseVT));
2669         SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2670         V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2671         MaskVec.clear();
2672         for (unsigned i = 0; i != 4; ++i)
2673           MaskVec.push_back(DAG.getConstant(i, BaseVT));
2674         for (unsigned i = 4; i != 8; ++i)
2675           MaskVec.push_back(PermMask.getOperand(i));
2676         Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2677         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2678       }
2679     } else {
2680       // Floating point cases in the other order.
2681       if (X86::isSHUFPMask(PermMask.Val))
2682         return NormalizeVectorShuffle(Op, DAG);
2683       if (X86::isPSHUFDMask(PermMask.Val) ||
2684           X86::isPSHUFHWMask(PermMask.Val) ||
2685           X86::isPSHUFLWMask(PermMask.Val)) {
2686         if (V2.getOpcode() != ISD::UNDEF)
2687           return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2688                              DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2689         return Op;
2690       }
2691     }
2692
2693     return SDOperand();
2694   }
2695   case ISD::BUILD_VECTOR: {
2696     // All one's are handled with pcmpeqd.
2697     if (ISD::isBuildVectorAllOnes(Op.Val))
2698       return Op;
2699
2700     std::set<SDOperand> Values;
2701     SDOperand Elt0 = Op.getOperand(0);
2702     Values.insert(Elt0);
2703     bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2704                        cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2705       (isa<ConstantFPSDNode>(Elt0) &&
2706        cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2707     bool RestAreZero = true;
2708     unsigned NumElems = Op.getNumOperands();
2709     for (unsigned i = 1; i < NumElems; ++i) {
2710       SDOperand Elt = Op.getOperand(i);
2711       if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
2712         if (!FPC->isExactlyValue(+0.0))
2713           RestAreZero = false;
2714       } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2715         if (!C->isNullValue())
2716           RestAreZero = false;
2717       } else
2718         RestAreZero = false;
2719       Values.insert(Elt);
2720     }
2721
2722     if (RestAreZero) {
2723       if (Elt0IsZero) return Op;
2724
2725       // Zero extend a scalar to a vector.
2726       return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2727     }
2728
2729     if (Values.size() > 2) {
2730       // Expand into a number of unpckl*.
2731       // e.g. for v4f32
2732       //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2733       //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2734       //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
2735       MVT::ValueType VT = Op.getValueType();
2736       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2737       MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2738       std::vector<SDOperand> MaskVec;
2739       for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2740         MaskVec.push_back(DAG.getConstant(i,            BaseVT));
2741         MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2742       }
2743       SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2744       std::vector<SDOperand> V(NumElems);
2745       for (unsigned i = 0; i < NumElems; ++i)
2746         V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2747       NumElems >>= 1;
2748       while (NumElems != 0) {
2749         for (unsigned i = 0; i < NumElems; ++i)
2750           V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2751                              PermMask);
2752         NumElems >>= 1;
2753       }
2754       return V[0];
2755     }
2756
2757     return SDOperand();
2758   }
2759   case ISD::EXTRACT_VECTOR_ELT: {
2760     if (!isa<ConstantSDNode>(Op.getOperand(1)))
2761         return SDOperand();
2762
2763     MVT::ValueType VT = Op.getValueType();
2764     if (MVT::getSizeInBits(VT) == 16) {
2765       // Transform it so it match pextrw which produces a 32-bit result.
2766       MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2767       SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2768                                       Op.getOperand(0), Op.getOperand(1));
2769       SDOperand Assert  = DAG.getNode(ISD::AssertZext, EVT, Extract,
2770                                       DAG.getValueType(VT));
2771       return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2772     } else if (MVT::getSizeInBits(VT) == 32) {
2773       SDOperand Vec = Op.getOperand(0);
2774       unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2775       if (Idx == 0)
2776         return Op;
2777
2778       // TODO: if Idex == 2, we can use unpckhps
2779       // SHUFPS the element to the lowest double word, then movss.
2780       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2781       SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2782                                           MVT::getVectorBaseType(MaskVT));
2783       std::vector<SDOperand> IdxVec;
2784       IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2785       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2786       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2787       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2788       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2789       Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2790                         Vec, Vec, Mask);
2791       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2792                          DAG.getConstant(0, MVT::i32));
2793     } else if (MVT::getSizeInBits(VT) == 64) {
2794       SDOperand Vec = Op.getOperand(0);
2795       unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2796       if (Idx == 0)
2797         return Op;
2798
2799       // UNPCKHPD the element to the lowest double word, then movsd.
2800       // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2801       // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2802       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2803       std::vector<SDOperand> IdxVec;
2804       IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2805       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2806       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2807       Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2808                         Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2809       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2810                          DAG.getConstant(0, MVT::i32));
2811     }
2812
2813     return SDOperand();
2814   }
2815   case ISD::INSERT_VECTOR_ELT: {
2816     // Transform it so it match pinsrw which expects a 16-bit value in a R32
2817     // as its second argument.
2818     MVT::ValueType VT = Op.getValueType();
2819     MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2820     if (MVT::getSizeInBits(BaseVT) == 16) {
2821       SDOperand N1 = Op.getOperand(1);
2822       SDOperand N2 = Op.getOperand(2);
2823       if (N1.getValueType() != MVT::i32)
2824         N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2825       if (N2.getValueType() != MVT::i32)
2826         N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2827       return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
2828     }
2829
2830     return SDOperand();
2831   }
2832   }
2833 }
2834
2835 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2836   switch (Opcode) {
2837   default: return NULL;
2838   case X86ISD::SHLD:               return "X86ISD::SHLD";
2839   case X86ISD::SHRD:               return "X86ISD::SHRD";
2840   case X86ISD::FAND:               return "X86ISD::FAND";
2841   case X86ISD::FXOR:               return "X86ISD::FXOR";
2842   case X86ISD::FILD:               return "X86ISD::FILD";
2843   case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
2844   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2845   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2846   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
2847   case X86ISD::FLD:                return "X86ISD::FLD";
2848   case X86ISD::FST:                return "X86ISD::FST";
2849   case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
2850   case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
2851   case X86ISD::CALL:               return "X86ISD::CALL";
2852   case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
2853   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
2854   case X86ISD::CMP:                return "X86ISD::CMP";
2855   case X86ISD::TEST:               return "X86ISD::TEST";
2856   case X86ISD::SETCC:              return "X86ISD::SETCC";
2857   case X86ISD::CMOV:               return "X86ISD::CMOV";
2858   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
2859   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
2860   case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
2861   case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
2862   case X86ISD::LOAD_PACK:          return "X86ISD::LOAD_PACK";
2863   case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
2864   case X86ISD::Wrapper:            return "X86ISD::Wrapper";
2865   case X86ISD::S2VEC:              return "X86ISD::S2VEC";
2866   case X86ISD::ZEXT_S2VEC:         return "X86ISD::ZEXT_S2VEC";
2867   case X86ISD::PEXTRW:             return "X86ISD::PEXTRW";
2868   case X86ISD::PINSRW:             return "X86ISD::PINSRW";
2869   }
2870 }
2871
2872 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2873                                                        uint64_t Mask,
2874                                                        uint64_t &KnownZero, 
2875                                                        uint64_t &KnownOne,
2876                                                        unsigned Depth) const {
2877   unsigned Opc = Op.getOpcode();
2878   assert((Opc >= ISD::BUILTIN_OP_END ||
2879           Opc == ISD::INTRINSIC_WO_CHAIN ||
2880           Opc == ISD::INTRINSIC_W_CHAIN ||
2881           Opc == ISD::INTRINSIC_VOID) &&
2882          "Should use MaskedValueIsZero if you don't know whether Op"
2883          " is a target node!");
2884
2885   KnownZero = KnownOne = 0;   // Don't know anything.
2886   switch (Opc) {
2887   default: break;
2888   case X86ISD::SETCC: 
2889     KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2890     break;
2891   }
2892 }
2893
2894 std::vector<unsigned> X86TargetLowering::
2895 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2896                                   MVT::ValueType VT) const {
2897   if (Constraint.size() == 1) {
2898     // FIXME: not handling fp-stack yet!
2899     // FIXME: not handling MMX registers yet ('y' constraint).
2900     switch (Constraint[0]) {      // GCC X86 Constraint Letters
2901     default: break;  // Unknown constriant letter
2902     case 'r':   // GENERAL_REGS
2903     case 'R':   // LEGACY_REGS
2904       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2905                                    X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2906     case 'l':   // INDEX_REGS
2907       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2908                                    X86::ESI, X86::EDI, X86::EBP, 0);
2909     case 'q':   // Q_REGS (GENERAL_REGS in 64-bit mode)
2910     case 'Q':   // Q_REGS
2911       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2912     case 'x':   // SSE_REGS if SSE1 allowed
2913       if (Subtarget->hasSSE1())
2914         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2915                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2916                                      0);
2917       return std::vector<unsigned>();
2918     case 'Y':   // SSE_REGS if SSE2 allowed
2919       if (Subtarget->hasSSE2())
2920         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2921                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2922                                      0);
2923       return std::vector<unsigned>();
2924     }
2925   }
2926   
2927   return std::vector<unsigned>();
2928 }
2929
2930 /// isLegalAddressImmediate - Return true if the integer value or
2931 /// GlobalValue can be used as the offset of the target addressing mode.
2932 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2933   // X86 allows a sign-extended 32-bit immediate field.
2934   return (V > -(1LL << 32) && V < (1LL << 32)-1);
2935 }
2936
2937 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2938   if (Subtarget->isTargetDarwin()) {
2939     Reloc::Model RModel = getTargetMachine().getRelocationModel();
2940     if (RModel == Reloc::Static)
2941       return true;
2942     else if (RModel == Reloc::DynamicNoPIC)
2943       return !DarwinGVRequiresExtraLoad(GV);
2944     else
2945       return false;
2946   } else
2947     return true;
2948 }
2949
2950 /// isShuffleMaskLegal - Targets can use this to indicate that they only
2951 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2952 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2953 /// are assumed to be legal.
2954 bool
2955 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2956   // Only do shuffles on 128-bit vector types for now.
2957   if (MVT::getSizeInBits(VT) == 64) return false;
2958   return (Mask.Val->getNumOperands() == 2 ||
2959           X86::isSplatMask(Mask.Val) ||
2960           X86::isPSHUFDMask(Mask.Val) ||
2961           isPSHUFHW_PSHUFLWMask(Mask.Val) ||
2962           X86::isSHUFPMask(Mask.Val) ||
2963           X86::isUNPCKLMask(Mask.Val) ||
2964           X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
2965           X86::isUNPCKHMask(Mask.Val));
2966 }