Bogus assert
[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 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1668 /// a splat of a single element.
1669 bool X86::isSplatMask(SDNode *N) {
1670   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1671
1672   // We can only splat 64-bit, and 32-bit quantities.
1673   if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1674     return false;
1675
1676   // This is a splat operation if each element of the permute is the same, and
1677   // if the value doesn't reference the second vector.
1678   SDOperand Elt = N->getOperand(0);
1679   assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1680   for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1681     SDOperand Arg = N->getOperand(i);
1682     if (Arg.getOpcode() == ISD::UNDEF) continue;
1683     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1684     if (Arg != Elt) return false;
1685   }
1686
1687   // Make sure it is a splat of the first vector operand.
1688   return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1689 }
1690
1691 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1692 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1693 /// instructions.
1694 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
1695   unsigned NumOperands = N->getNumOperands();
1696   unsigned Shift = (NumOperands == 4) ? 2 : 1;
1697   unsigned Mask = 0;
1698   for (unsigned i = 0; i < NumOperands; ++i) {
1699     unsigned Val = 0;
1700     SDOperand Arg = N->getOperand(NumOperands-i-1);
1701     if (Arg.getOpcode() != ISD::UNDEF)
1702       Val = cast<ConstantSDNode>(Arg)->getValue();
1703     if (Val >= NumOperands) Val -= NumOperands;
1704     Mask |= Val;
1705     if (i != NumOperands - 1)
1706       Mask <<= Shift;
1707   }
1708
1709   return Mask;
1710 }
1711
1712 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1713 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1714 /// instructions.
1715 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1716   unsigned Mask = 0;
1717   // 8 nodes, but we only care about the last 4.
1718   for (unsigned i = 7; i >= 4; --i) {
1719     unsigned Val = 0;
1720     SDOperand Arg = N->getOperand(i);
1721     if (Arg.getOpcode() != ISD::UNDEF)
1722       Val = cast<ConstantSDNode>(Arg)->getValue();
1723     Mask |= (Val - 4);
1724     if (i != 4)
1725       Mask <<= 2;
1726   }
1727
1728   return Mask;
1729 }
1730
1731 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1732 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1733 /// instructions.
1734 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1735   unsigned Mask = 0;
1736   // 8 nodes, but we only care about the first 4.
1737   for (int i = 3; i >= 0; --i) {
1738     unsigned Val = 0;
1739     SDOperand Arg = N->getOperand(i);
1740     if (Arg.getOpcode() != ISD::UNDEF)
1741       Val = cast<ConstantSDNode>(Arg)->getValue();
1742     Mask |= Val;
1743     if (i != 0)
1744       Mask <<= 2;
1745   }
1746
1747   return Mask;
1748 }
1749
1750 /// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1751 /// values in ther permute mask if needed. Use V1 as second vector if it is
1752 /// undef. Return an empty SDOperand is it is already well formed.
1753 static SDOperand NormalizeVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1754   SDOperand V1 = Op.getOperand(0);
1755   SDOperand V2 = Op.getOperand(1);
1756   SDOperand Mask = Op.getOperand(2);
1757   MVT::ValueType VT = Op.getValueType();
1758   unsigned NumElems = Mask.getNumOperands();
1759   SDOperand Half1 = Mask.getOperand(0);
1760   SDOperand Half2 = Mask.getOperand(NumElems/2);
1761   bool V2Undef = false;
1762   if (V2.getOpcode() == ISD::UNDEF) {
1763     V2Undef = true;
1764     V2 = V1;
1765   }
1766
1767   if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1768       cast<ConstantSDNode>(Half2)->getValue() <  NumElems) {
1769     // Swap the operands and change mask.
1770     std::vector<SDOperand> MaskVec;
1771     for (unsigned i = NumElems / 2; i != NumElems; ++i)
1772       MaskVec.push_back(Mask.getOperand(i));
1773     for (unsigned i = 0; i != NumElems / 2; ++i)
1774       MaskVec.push_back(Mask.getOperand(i));
1775     Mask =
1776       DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1777     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1778   }
1779
1780   if (V2Undef)
1781     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1782
1783   return Op;
1784 }
1785
1786 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1787 /// specifies a 8 element shuffle that can be broken into a pair of
1788 /// PSHUFHW and PSHUFLW.
1789 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1790   assert(N->getOpcode() == ISD::BUILD_VECTOR);
1791
1792   if (N->getNumOperands() != 8)
1793     return false;
1794
1795   // Lower quadword shuffled.
1796   for (unsigned i = 0; i != 4; ++i) {
1797     SDOperand Arg = N->getOperand(i);
1798     if (Arg.getOpcode() == ISD::UNDEF) continue;
1799     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1800     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1801     if (Val > 4)
1802       return false;
1803   }
1804
1805   // Upper quadword shuffled.
1806   for (unsigned i = 4; i != 8; ++i) {
1807     SDOperand Arg = N->getOperand(i);
1808     if (Arg.getOpcode() == ISD::UNDEF) continue;
1809     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1810     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1811     if (Val < 4 || Val > 7)
1812       return false;
1813   }
1814
1815   return true;
1816 }
1817
1818 /// LowerOperation - Provide custom lowering hooks for some operations.
1819 ///
1820 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1821   switch (Op.getOpcode()) {
1822   default: assert(0 && "Should not custom lower this!");
1823   case ISD::SHL_PARTS:
1824   case ISD::SRA_PARTS:
1825   case ISD::SRL_PARTS: {
1826     assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1827            "Not an i64 shift!");
1828     bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1829     SDOperand ShOpLo = Op.getOperand(0);
1830     SDOperand ShOpHi = Op.getOperand(1);
1831     SDOperand ShAmt  = Op.getOperand(2);
1832     SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
1833                                          DAG.getConstant(31, MVT::i8))
1834                            : DAG.getConstant(0, MVT::i32);
1835
1836     SDOperand Tmp2, Tmp3;
1837     if (Op.getOpcode() == ISD::SHL_PARTS) {
1838       Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1839       Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1840     } else {
1841       Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
1842       Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
1843     }
1844
1845     SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1846                                    ShAmt, DAG.getConstant(32, MVT::i8));
1847
1848     SDOperand Hi, Lo;
1849     SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
1850
1851     std::vector<MVT::ValueType> Tys;
1852     Tys.push_back(MVT::i32);
1853     Tys.push_back(MVT::Flag);
1854     std::vector<SDOperand> Ops;
1855     if (Op.getOpcode() == ISD::SHL_PARTS) {
1856       Ops.push_back(Tmp2);
1857       Ops.push_back(Tmp3);
1858       Ops.push_back(CC);
1859       Ops.push_back(InFlag);
1860       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1861       InFlag = Hi.getValue(1);
1862
1863       Ops.clear();
1864       Ops.push_back(Tmp3);
1865       Ops.push_back(Tmp1);
1866       Ops.push_back(CC);
1867       Ops.push_back(InFlag);
1868       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1869     } else {
1870       Ops.push_back(Tmp2);
1871       Ops.push_back(Tmp3);
1872       Ops.push_back(CC);
1873       Ops.push_back(InFlag);
1874       Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1875       InFlag = Lo.getValue(1);
1876
1877       Ops.clear();
1878       Ops.push_back(Tmp3);
1879       Ops.push_back(Tmp1);
1880       Ops.push_back(CC);
1881       Ops.push_back(InFlag);
1882       Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1883     }
1884
1885     Tys.clear();
1886     Tys.push_back(MVT::i32);
1887     Tys.push_back(MVT::i32);
1888     Ops.clear();
1889     Ops.push_back(Lo);
1890     Ops.push_back(Hi);
1891     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1892   }
1893   case ISD::SINT_TO_FP: {
1894     assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
1895            Op.getOperand(0).getValueType() >= MVT::i16 &&
1896            "Unknown SINT_TO_FP to lower!");
1897
1898     SDOperand Result;
1899     MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1900     unsigned Size = MVT::getSizeInBits(SrcVT)/8;
1901     MachineFunction &MF = DAG.getMachineFunction();
1902     int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1903     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1904     SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1905                                   DAG.getEntryNode(), Op.getOperand(0),
1906                                   StackSlot, DAG.getSrcValue(NULL));
1907
1908     // Build the FILD
1909     std::vector<MVT::ValueType> Tys;
1910     Tys.push_back(MVT::f64);
1911     Tys.push_back(MVT::Other);
1912     if (X86ScalarSSE) Tys.push_back(MVT::Flag);
1913     std::vector<SDOperand> Ops;
1914     Ops.push_back(Chain);
1915     Ops.push_back(StackSlot);
1916     Ops.push_back(DAG.getValueType(SrcVT));
1917     Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1918                          Tys, Ops);
1919
1920     if (X86ScalarSSE) {
1921       Chain = Result.getValue(1);
1922       SDOperand InFlag = Result.getValue(2);
1923
1924       // FIXME: Currently the FST is flagged to the FILD_FLAG. This
1925       // shouldn't be necessary except that RFP cannot be live across
1926       // multiple blocks. When stackifier is fixed, they can be uncoupled.
1927       MachineFunction &MF = DAG.getMachineFunction();
1928       int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1929       SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1930       std::vector<MVT::ValueType> Tys;
1931       Tys.push_back(MVT::Other);
1932       std::vector<SDOperand> Ops;
1933       Ops.push_back(Chain);
1934       Ops.push_back(Result);
1935       Ops.push_back(StackSlot);
1936       Ops.push_back(DAG.getValueType(Op.getValueType()));
1937       Ops.push_back(InFlag);
1938       Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1939       Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1940                            DAG.getSrcValue(NULL));
1941     }
1942
1943     return Result;
1944   }
1945   case ISD::FP_TO_SINT: {
1946     assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
1947            "Unknown FP_TO_SINT to lower!");
1948     // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1949     // stack slot.
1950     MachineFunction &MF = DAG.getMachineFunction();
1951     unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1952     int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1953     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1954
1955     unsigned Opc;
1956     switch (Op.getValueType()) {
1957     default: assert(0 && "Invalid FP_TO_SINT to lower!");
1958     case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1959     case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1960     case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1961     }
1962
1963     SDOperand Chain = DAG.getEntryNode();
1964     SDOperand Value = Op.getOperand(0);
1965     if (X86ScalarSSE) {
1966       assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1967       Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot, 
1968                           DAG.getSrcValue(0));
1969       std::vector<MVT::ValueType> Tys;
1970       Tys.push_back(MVT::f64);
1971       Tys.push_back(MVT::Other);
1972       std::vector<SDOperand> Ops;
1973       Ops.push_back(Chain);
1974       Ops.push_back(StackSlot);
1975       Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
1976       Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1977       Chain = Value.getValue(1);
1978       SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1979       StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1980     }
1981
1982     // Build the FP_TO_INT*_IN_MEM
1983     std::vector<SDOperand> Ops;
1984     Ops.push_back(Chain);
1985     Ops.push_back(Value);
1986     Ops.push_back(StackSlot);
1987     SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1988
1989     // Load the result.
1990     return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1991                        DAG.getSrcValue(NULL));
1992   }
1993   case ISD::READCYCLECOUNTER: {
1994     std::vector<MVT::ValueType> Tys;
1995     Tys.push_back(MVT::Other);
1996     Tys.push_back(MVT::Flag);
1997     std::vector<SDOperand> Ops;
1998     Ops.push_back(Op.getOperand(0));
1999     SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
2000     Ops.clear();
2001     Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2002     Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, 
2003                                      MVT::i32, Ops[0].getValue(2)));
2004     Ops.push_back(Ops[1].getValue(1));
2005     Tys[0] = Tys[1] = MVT::i32;
2006     Tys.push_back(MVT::Other);
2007     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2008   }
2009   case ISD::FABS: {
2010     MVT::ValueType VT = Op.getValueType();
2011     const Type *OpNTy =  MVT::getTypeForValueType(VT);
2012     std::vector<Constant*> CV;
2013     if (VT == MVT::f64) {
2014       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2015       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2016     } else {
2017       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2018       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2019       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2020       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2021     }
2022     Constant *CS = ConstantStruct::get(CV);
2023     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2024     SDOperand Mask 
2025       = DAG.getNode(X86ISD::LOAD_PACK,
2026                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2027     return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2028   }
2029   case ISD::FNEG: {
2030     MVT::ValueType VT = Op.getValueType();
2031     const Type *OpNTy =  MVT::getTypeForValueType(VT);
2032     std::vector<Constant*> CV;
2033     if (VT == MVT::f64) {
2034       CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2035       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2036     } else {
2037       CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2038       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2039       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2040       CV.push_back(ConstantFP::get(OpNTy, 0.0));
2041     }
2042     Constant *CS = ConstantStruct::get(CV);
2043     SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2044     SDOperand Mask 
2045       = DAG.getNode(X86ISD::LOAD_PACK,
2046                     VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2047     return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2048   }
2049   case ISD::SETCC: {
2050     assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
2051     SDOperand Cond;
2052     SDOperand CC = Op.getOperand(2);
2053     ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2054     bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
2055     bool Flip;
2056     unsigned X86CC;
2057     if (translateX86CC(CC, isFP, X86CC, Flip)) {
2058       if (Flip)
2059         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2060                            Op.getOperand(1), Op.getOperand(0));
2061       else
2062         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2063                            Op.getOperand(0), Op.getOperand(1));
2064       return DAG.getNode(X86ISD::SETCC, MVT::i8, 
2065                          DAG.getConstant(X86CC, MVT::i8), Cond);
2066     } else {
2067       assert(isFP && "Illegal integer SetCC!");
2068
2069       Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2070                          Op.getOperand(0), Op.getOperand(1));
2071       std::vector<MVT::ValueType> Tys;
2072       std::vector<SDOperand> Ops;
2073       switch (SetCCOpcode) {
2074       default: assert(false && "Illegal floating point SetCC!");
2075       case ISD::SETOEQ: {  // !PF & ZF
2076         Tys.push_back(MVT::i8);
2077         Tys.push_back(MVT::Flag);
2078         Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2079         Ops.push_back(Cond);
2080         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2081         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2082                                      DAG.getConstant(X86ISD::COND_E, MVT::i8),
2083                                      Tmp1.getValue(1));
2084         return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2085       }
2086       case ISD::SETUNE: {  // PF | !ZF
2087         Tys.push_back(MVT::i8);
2088         Tys.push_back(MVT::Flag);
2089         Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2090         Ops.push_back(Cond);
2091         SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2092         SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2093                                      DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2094                                      Tmp1.getValue(1));
2095         return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2096       }
2097       }
2098     }
2099   }
2100   case ISD::SELECT: {
2101     MVT::ValueType VT = Op.getValueType();
2102     bool isFP      = MVT::isFloatingPoint(VT);
2103     bool isFPStack = isFP && !X86ScalarSSE;
2104     bool isFPSSE   = isFP && X86ScalarSSE;
2105     bool addTest   = false;
2106     SDOperand Op0 = Op.getOperand(0);
2107     SDOperand Cond, CC;
2108     if (Op0.getOpcode() == ISD::SETCC)
2109       Op0 = LowerOperation(Op0, DAG);
2110
2111     if (Op0.getOpcode() == X86ISD::SETCC) {
2112       // If condition flag is set by a X86ISD::CMP, then make a copy of it
2113       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2114       // have another use it will be eliminated.
2115       // If the X86ISD::SETCC has more than one use, then it's probably better
2116       // to use a test instead of duplicating the X86ISD::CMP (for register
2117       // pressure reason).
2118       if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
2119         if (!Op0.hasOneUse()) {
2120           std::vector<MVT::ValueType> Tys;
2121           for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2122             Tys.push_back(Op0.Val->getValueType(i));
2123           std::vector<SDOperand> Ops;
2124           for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2125             Ops.push_back(Op0.getOperand(i));
2126           Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2127         }
2128
2129         CC   = Op0.getOperand(0);
2130         Cond = Op0.getOperand(1);
2131         // Make a copy as flag result cannot be used by more than one.
2132         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2133                            Cond.getOperand(0), Cond.getOperand(1));
2134         addTest =
2135           isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
2136       } else
2137         addTest = true;
2138     } else
2139       addTest = true;
2140
2141     if (addTest) {
2142       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2143       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
2144     }
2145
2146     std::vector<MVT::ValueType> Tys;
2147     Tys.push_back(Op.getValueType());
2148     Tys.push_back(MVT::Flag);
2149     std::vector<SDOperand> Ops;
2150     // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2151     // condition is true.
2152     Ops.push_back(Op.getOperand(2));
2153     Ops.push_back(Op.getOperand(1));
2154     Ops.push_back(CC);
2155     Ops.push_back(Cond);
2156     return DAG.getNode(X86ISD::CMOV, Tys, Ops);
2157   }
2158   case ISD::BRCOND: {
2159     bool addTest = false;
2160     SDOperand Cond  = Op.getOperand(1);
2161     SDOperand Dest  = Op.getOperand(2);
2162     SDOperand CC;
2163     if (Cond.getOpcode() == ISD::SETCC)
2164       Cond = LowerOperation(Cond, DAG);
2165
2166     if (Cond.getOpcode() == X86ISD::SETCC) {
2167       // If condition flag is set by a X86ISD::CMP, then make a copy of it
2168       // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2169       // have another use it will be eliminated.
2170       // If the X86ISD::SETCC has more than one use, then it's probably better
2171       // to use a test instead of duplicating the X86ISD::CMP (for register
2172       // pressure reason).
2173       if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
2174         if (!Cond.hasOneUse()) {
2175           std::vector<MVT::ValueType> Tys;
2176           for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2177             Tys.push_back(Cond.Val->getValueType(i));
2178           std::vector<SDOperand> Ops;
2179           for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2180             Ops.push_back(Cond.getOperand(i));
2181           Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2182         }
2183
2184         CC   = Cond.getOperand(0);
2185         Cond = Cond.getOperand(1);
2186         // Make a copy as flag result cannot be used by more than one.
2187         Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2188                            Cond.getOperand(0), Cond.getOperand(1));
2189       } else
2190         addTest = true;
2191     } else
2192       addTest = true;
2193
2194     if (addTest) {
2195       CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
2196       Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2197     }
2198     return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2199                        Op.getOperand(0), Op.getOperand(2), CC, Cond);
2200   }
2201   case ISD::MEMSET: {
2202     SDOperand InFlag(0, 0);
2203     SDOperand Chain = Op.getOperand(0);
2204     unsigned Align =
2205       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2206     if (Align == 0) Align = 1;
2207
2208     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2209     // If not DWORD aligned, call memset if size is less than the threshold.
2210     // It knows how to align to the right boundary first.
2211     if ((Align & 3) != 0 ||
2212         (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
2213       MVT::ValueType IntPtr = getPointerTy();
2214       const Type *IntPtrTy = getTargetData().getIntPtrType();
2215       std::vector<std::pair<SDOperand, const Type*> > Args;
2216       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2217       // Extend the ubyte argument to be an int value for the call.
2218       SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2219       Args.push_back(std::make_pair(Val, IntPtrTy));
2220       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2221       std::pair<SDOperand,SDOperand> CallResult =
2222         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2223                     DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2224       return CallResult.second;
2225     }
2226
2227     MVT::ValueType AVT;
2228     SDOperand Count;
2229     ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2230     unsigned BytesLeft = 0;
2231     bool TwoRepStos = false;
2232     if (ValC) {
2233       unsigned ValReg;
2234       unsigned Val = ValC->getValue() & 255;
2235
2236       // If the value is a constant, then we can potentially use larger sets.
2237       switch (Align & 3) {
2238       case 2:   // WORD aligned
2239         AVT = MVT::i16;
2240         Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2241         BytesLeft = I->getValue() % 2;
2242         Val    = (Val << 8) | Val;
2243         ValReg = X86::AX;
2244         break;
2245       case 0:   // DWORD aligned
2246         AVT = MVT::i32;
2247         if (I) {
2248           Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2249           BytesLeft = I->getValue() % 4;
2250         } else {
2251           Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2252                               DAG.getConstant(2, MVT::i8));
2253           TwoRepStos = true;
2254         }
2255         Val = (Val << 8)  | Val;
2256         Val = (Val << 16) | Val;
2257         ValReg = X86::EAX;
2258         break;
2259       default:  // Byte aligned
2260         AVT = MVT::i8;
2261         Count = Op.getOperand(3);
2262         ValReg = X86::AL;
2263         break;
2264       }
2265
2266       Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2267                                 InFlag);
2268       InFlag = Chain.getValue(1);
2269     } else {
2270       AVT = MVT::i8;
2271       Count  = Op.getOperand(3);
2272       Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2273       InFlag = Chain.getValue(1);
2274     }
2275
2276     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2277     InFlag = Chain.getValue(1);
2278     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2279     InFlag = Chain.getValue(1);
2280
2281     std::vector<MVT::ValueType> Tys;
2282     Tys.push_back(MVT::Other);
2283     Tys.push_back(MVT::Flag);
2284     std::vector<SDOperand> Ops;
2285     Ops.push_back(Chain);
2286     Ops.push_back(DAG.getValueType(AVT));
2287     Ops.push_back(InFlag);
2288     Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2289
2290     if (TwoRepStos) {
2291       InFlag = Chain.getValue(1);
2292       Count = Op.getOperand(3);
2293       MVT::ValueType CVT = Count.getValueType();
2294       SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2295                                    DAG.getConstant(3, CVT));
2296       Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2297       InFlag = Chain.getValue(1);
2298       Tys.clear();
2299       Tys.push_back(MVT::Other);
2300       Tys.push_back(MVT::Flag);
2301       Ops.clear();
2302       Ops.push_back(Chain);
2303       Ops.push_back(DAG.getValueType(MVT::i8));
2304       Ops.push_back(InFlag);
2305       Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2306     } else if (BytesLeft) {
2307       // Issue stores for the last 1 - 3 bytes.
2308       SDOperand Value;
2309       unsigned Val = ValC->getValue() & 255;
2310       unsigned Offset = I->getValue() - BytesLeft;
2311       SDOperand DstAddr = Op.getOperand(1);
2312       MVT::ValueType AddrVT = DstAddr.getValueType();
2313       if (BytesLeft >= 2) {
2314         Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2315         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2316                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2317                                         DAG.getConstant(Offset, AddrVT)),
2318                             DAG.getSrcValue(NULL));
2319         BytesLeft -= 2;
2320         Offset += 2;
2321       }
2322
2323       if (BytesLeft == 1) {
2324         Value = DAG.getConstant(Val, MVT::i8);
2325         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2326                             DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2327                                         DAG.getConstant(Offset, AddrVT)),
2328                             DAG.getSrcValue(NULL));
2329       }
2330     }
2331
2332     return Chain;
2333   }
2334   case ISD::MEMCPY: {
2335     SDOperand Chain = Op.getOperand(0);
2336     unsigned Align =
2337       (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2338     if (Align == 0) Align = 1;
2339
2340     ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2341     // If not DWORD aligned, call memcpy if size is less than the threshold.
2342     // It knows how to align to the right boundary first.
2343     if ((Align & 3) != 0 ||
2344         (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
2345       MVT::ValueType IntPtr = getPointerTy();
2346       const Type *IntPtrTy = getTargetData().getIntPtrType();
2347       std::vector<std::pair<SDOperand, const Type*> > Args;
2348       Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2349       Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2350       Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2351       std::pair<SDOperand,SDOperand> CallResult =
2352         LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2353                     DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2354       return CallResult.second;
2355     }
2356
2357     MVT::ValueType AVT;
2358     SDOperand Count;
2359     unsigned BytesLeft = 0;
2360     bool TwoRepMovs = false;
2361     switch (Align & 3) {
2362     case 2:   // WORD aligned
2363       AVT = MVT::i16;
2364       Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2365       BytesLeft = I->getValue() % 2;
2366       break;
2367     case 0:   // DWORD aligned
2368       AVT = MVT::i32;
2369       if (I) {
2370         Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2371         BytesLeft = I->getValue() % 4;
2372       } else {
2373         Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2374                             DAG.getConstant(2, MVT::i8));
2375         TwoRepMovs = true;
2376       }
2377       break;
2378     default:  // Byte aligned
2379       AVT = MVT::i8;
2380       Count = Op.getOperand(3);
2381       break;
2382     }
2383
2384     SDOperand InFlag(0, 0);
2385     Chain  = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2386     InFlag = Chain.getValue(1);
2387     Chain  = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2388     InFlag = Chain.getValue(1);
2389     Chain  = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2390     InFlag = Chain.getValue(1);
2391
2392     std::vector<MVT::ValueType> Tys;
2393     Tys.push_back(MVT::Other);
2394     Tys.push_back(MVT::Flag);
2395     std::vector<SDOperand> Ops;
2396     Ops.push_back(Chain);
2397     Ops.push_back(DAG.getValueType(AVT));
2398     Ops.push_back(InFlag);
2399     Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2400
2401     if (TwoRepMovs) {
2402       InFlag = Chain.getValue(1);
2403       Count = Op.getOperand(3);
2404       MVT::ValueType CVT = Count.getValueType();
2405       SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2406                                    DAG.getConstant(3, CVT));
2407       Chain  = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2408       InFlag = Chain.getValue(1);
2409       Tys.clear();
2410       Tys.push_back(MVT::Other);
2411       Tys.push_back(MVT::Flag);
2412       Ops.clear();
2413       Ops.push_back(Chain);
2414       Ops.push_back(DAG.getValueType(MVT::i8));
2415       Ops.push_back(InFlag);
2416       Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2417     } else if (BytesLeft) {
2418       // Issue loads and stores for the last 1 - 3 bytes.
2419       unsigned Offset = I->getValue() - BytesLeft;
2420       SDOperand DstAddr = Op.getOperand(1);
2421       MVT::ValueType DstVT = DstAddr.getValueType();
2422       SDOperand SrcAddr = Op.getOperand(2);
2423       MVT::ValueType SrcVT = SrcAddr.getValueType();
2424       SDOperand Value;
2425       if (BytesLeft >= 2) {
2426         Value = DAG.getLoad(MVT::i16, Chain,
2427                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2428                                         DAG.getConstant(Offset, SrcVT)),
2429                             DAG.getSrcValue(NULL));
2430         Chain = Value.getValue(1);
2431         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2432                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
2433                                         DAG.getConstant(Offset, DstVT)),
2434                             DAG.getSrcValue(NULL));
2435         BytesLeft -= 2;
2436         Offset += 2;
2437       }
2438
2439       if (BytesLeft == 1) {
2440         Value = DAG.getLoad(MVT::i8, Chain,
2441                             DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2442                                         DAG.getConstant(Offset, SrcVT)),
2443                             DAG.getSrcValue(NULL));
2444         Chain = Value.getValue(1);
2445         Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2446                             DAG.getNode(ISD::ADD, DstVT, DstAddr,
2447                                         DAG.getConstant(Offset, DstVT)),
2448                             DAG.getSrcValue(NULL));
2449       }
2450     }
2451
2452     return Chain;
2453   }
2454
2455   // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2456   // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2457   // one of the above mentioned nodes. It has to be wrapped because otherwise
2458   // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2459   // be used to form addressing mode. These wrapped nodes will be selected
2460   // into MOV32ri.
2461   case ISD::ConstantPool: {
2462     ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2463     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2464                          DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2465                                                    CP->getAlignment()));
2466     if (Subtarget->isTargetDarwin()) {
2467       // With PIC, the address is actually $g + Offset.
2468       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2469         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2470                 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);    
2471     }
2472
2473     return Result;
2474   }
2475   case ISD::GlobalAddress: {
2476     GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2477     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2478                          DAG.getTargetGlobalAddress(GV, getPointerTy()));
2479     if (Subtarget->isTargetDarwin()) {
2480       // With PIC, the address is actually $g + Offset.
2481       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2482         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2483                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2484
2485       // For Darwin, external and weak symbols are indirect, so we want to load
2486       // the value at address GV, not the value of GV itself. This means that
2487       // the GlobalAddress must be in the base or index register of the address,
2488       // not the GV offset field.
2489       if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2490           DarwinGVRequiresExtraLoad(GV))
2491         Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2492                              Result, DAG.getSrcValue(NULL));
2493     }
2494
2495     return Result;
2496   }
2497   case ISD::ExternalSymbol: {
2498     const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2499     SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2500                          DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2501     if (Subtarget->isTargetDarwin()) {
2502       // With PIC, the address is actually $g + Offset.
2503       if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2504         Result = DAG.getNode(ISD::ADD, getPointerTy(),
2505                     DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2506     }
2507
2508     return Result;
2509   }
2510   case ISD::VASTART: {
2511     // vastart just stores the address of the VarArgsFrameIndex slot into the
2512     // memory location argument.
2513     // FIXME: Replace MVT::i32 with PointerTy
2514     SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2515     return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR, 
2516                        Op.getOperand(1), Op.getOperand(2));
2517   }
2518   case ISD::RET: {
2519     SDOperand Copy;
2520     
2521     switch(Op.getNumOperands()) {
2522     default:
2523       assert(0 && "Do not know how to return this many arguments!");
2524       abort();
2525     case 1: 
2526       return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2527                          DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2528     case 2: {
2529       MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2530       if (MVT::isInteger(ArgVT))
2531         Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2532                                 SDOperand());
2533       else if (!X86ScalarSSE) {
2534         std::vector<MVT::ValueType> Tys;
2535         Tys.push_back(MVT::Other);
2536         Tys.push_back(MVT::Flag);
2537         std::vector<SDOperand> Ops;
2538         Ops.push_back(Op.getOperand(0));
2539         Ops.push_back(Op.getOperand(1));
2540         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2541       } else {
2542         SDOperand MemLoc;
2543         SDOperand Chain = Op.getOperand(0);
2544         SDOperand Value = Op.getOperand(1);
2545
2546         if (Value.getOpcode() == ISD::LOAD &&
2547             (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
2548           Chain  = Value.getOperand(0);
2549           MemLoc = Value.getOperand(1);
2550         } else {
2551           // Spill the value to memory and reload it into top of stack.
2552           unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2553           MachineFunction &MF = DAG.getMachineFunction();
2554           int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2555           MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2556           Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), 
2557                               Value, MemLoc, DAG.getSrcValue(0));
2558         }
2559         std::vector<MVT::ValueType> Tys;
2560         Tys.push_back(MVT::f64);
2561         Tys.push_back(MVT::Other);
2562         std::vector<SDOperand> Ops;
2563         Ops.push_back(Chain);
2564         Ops.push_back(MemLoc);
2565         Ops.push_back(DAG.getValueType(ArgVT));
2566         Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2567         Tys.clear();
2568         Tys.push_back(MVT::Other);
2569         Tys.push_back(MVT::Flag);
2570         Ops.clear();
2571         Ops.push_back(Copy.getValue(1));
2572         Ops.push_back(Copy);
2573         Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2574       }
2575       break;
2576     }
2577     case 3:
2578       Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2), 
2579                               SDOperand());
2580       Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2581       break;
2582     }
2583     return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2584                        Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2585                        Copy.getValue(1));
2586   }
2587   case ISD::SCALAR_TO_VECTOR: {
2588     SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2589     return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2590   }
2591   case ISD::VECTOR_SHUFFLE: {
2592     SDOperand V1 = Op.getOperand(0);
2593     SDOperand V2 = Op.getOperand(1);
2594     SDOperand PermMask = Op.getOperand(2);
2595     MVT::ValueType VT = Op.getValueType();
2596     unsigned NumElems = PermMask.getNumOperands();
2597
2598     // Splat && PSHUFD's 2nd vector must be undef.
2599     if (X86::isSplatMask(PermMask.Val)) {
2600       if (V2.getOpcode() != ISD::UNDEF)
2601         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2602                            DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2603       return Op;
2604     }
2605
2606     if (X86::isUNPCKLMask(PermMask.Val) ||
2607         X86::isUNPCKHMask(PermMask.Val))
2608       // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
2609       return Op;
2610
2611     if (NumElems == 2)
2612       return NormalizeVectorShuffle(Op, DAG);
2613
2614     // If VT is integer, try PSHUF* first, then SHUFP*.
2615     if (MVT::isInteger(VT)) {
2616       if (X86::isPSHUFDMask(PermMask.Val) ||
2617           X86::isPSHUFHWMask(PermMask.Val) ||
2618           X86::isPSHUFLWMask(PermMask.Val)) {
2619         if (V2.getOpcode() != ISD::UNDEF)
2620           return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2621                              DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2622         return Op;
2623       }
2624
2625       if (X86::isSHUFPMask(PermMask.Val))
2626         return NormalizeVectorShuffle(Op, DAG);
2627
2628       // Handle v8i16 shuffle high / low shuffle node pair.
2629       if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2630         MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2631         MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2632         std::vector<SDOperand> MaskVec;
2633         for (unsigned i = 0; i != 4; ++i)
2634           MaskVec.push_back(PermMask.getOperand(i));
2635         for (unsigned i = 4; i != 8; ++i)
2636           MaskVec.push_back(DAG.getConstant(i, BaseVT));
2637         SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2638         V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2639         MaskVec.clear();
2640         for (unsigned i = 0; i != 4; ++i)
2641           MaskVec.push_back(DAG.getConstant(i, BaseVT));
2642         for (unsigned i = 4; i != 8; ++i)
2643           MaskVec.push_back(PermMask.getOperand(i));
2644         Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2645         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2646       }
2647     } else {
2648       // Floating point cases in the other order.
2649       if (X86::isSHUFPMask(PermMask.Val))
2650         return NormalizeVectorShuffle(Op, DAG);
2651       if (X86::isPSHUFDMask(PermMask.Val) ||
2652           X86::isPSHUFHWMask(PermMask.Val) ||
2653           X86::isPSHUFLWMask(PermMask.Val)) {
2654         if (V2.getOpcode() != ISD::UNDEF)
2655           return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2656                              DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2657         return Op;
2658       }
2659     }
2660
2661     return SDOperand();
2662   }
2663   case ISD::BUILD_VECTOR: {
2664     // All one's are handled with pcmpeqd.
2665     if (ISD::isBuildVectorAllOnes(Op.Val))
2666       return Op;
2667
2668     std::set<SDOperand> Values;
2669     SDOperand Elt0 = Op.getOperand(0);
2670     Values.insert(Elt0);
2671     bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2672                        cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2673       (isa<ConstantFPSDNode>(Elt0) &&
2674        cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2675     bool RestAreZero = true;
2676     unsigned NumElems = Op.getNumOperands();
2677     for (unsigned i = 1; i < NumElems; ++i) {
2678       SDOperand Elt = Op.getOperand(i);
2679       if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
2680         if (!FPC->isExactlyValue(+0.0))
2681           RestAreZero = false;
2682       } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
2683         if (!C->isNullValue())
2684           RestAreZero = false;
2685       } else
2686         RestAreZero = false;
2687       Values.insert(Elt);
2688     }
2689
2690     if (RestAreZero) {
2691       if (Elt0IsZero) return Op;
2692
2693       // Zero extend a scalar to a vector.
2694       return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2695     }
2696
2697     if (Values.size() > 2) {
2698       // Expand into a number of unpckl*.
2699       // e.g. for v4f32
2700       //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2701       //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2702       //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
2703       MVT::ValueType VT = Op.getValueType();
2704       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2705       MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2706       std::vector<SDOperand> MaskVec;
2707       for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2708         MaskVec.push_back(DAG.getConstant(i,            BaseVT));
2709         MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2710       }
2711       SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2712       std::vector<SDOperand> V(NumElems);
2713       for (unsigned i = 0; i < NumElems; ++i)
2714         V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2715       NumElems >>= 1;
2716       while (NumElems != 0) {
2717         for (unsigned i = 0; i < NumElems; ++i)
2718           V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2719                              PermMask);
2720         NumElems >>= 1;
2721       }
2722       return V[0];
2723     }
2724
2725     return SDOperand();
2726   }
2727   case ISD::EXTRACT_VECTOR_ELT: {
2728     if (!isa<ConstantSDNode>(Op.getOperand(1)))
2729         return SDOperand();
2730
2731     MVT::ValueType VT = Op.getValueType();
2732     if (MVT::getSizeInBits(VT) == 16) {
2733       // Transform it so it match pextrw which produces a 32-bit result.
2734       MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2735       SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2736                                       Op.getOperand(0), Op.getOperand(1));
2737       SDOperand Assert  = DAG.getNode(ISD::AssertZext, EVT, Extract,
2738                                       DAG.getValueType(VT));
2739       return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2740     } else if (MVT::getSizeInBits(VT) == 32) {
2741       SDOperand Vec = Op.getOperand(0);
2742       unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2743       if (Idx == 0)
2744         return Op;
2745
2746       // TODO: if Idex == 2, we can use unpckhps
2747       // SHUFPS the element to the lowest double word, then movss.
2748       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2749       SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2750                                           MVT::getVectorBaseType(MaskVT));
2751       std::vector<SDOperand> IdxVec;
2752       IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2753       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2754       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2755       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2756       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2757       Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2758                         Vec, Vec, Mask);
2759       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2760                          DAG.getConstant(0, MVT::i32));
2761     } else if (MVT::getSizeInBits(VT) == 64) {
2762       SDOperand Vec = Op.getOperand(0);
2763       unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2764       if (Idx == 0)
2765         return Op;
2766
2767       // UNPCKHPD the element to the lowest double word, then movsd.
2768       // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2769       // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2770       MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2771       std::vector<SDOperand> IdxVec;
2772       IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2773       IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2774       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2775       Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2776                         Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2777       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2778                          DAG.getConstant(0, MVT::i32));
2779     }
2780
2781     return SDOperand();
2782   }
2783   case ISD::INSERT_VECTOR_ELT: {
2784     // Transform it so it match pinsrw which expects a 16-bit value in a R32
2785     // as its second argument.
2786     MVT::ValueType VT = Op.getValueType();
2787     MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2788     if (MVT::getSizeInBits(BaseVT) == 16) {
2789       SDOperand N1 = Op.getOperand(1);
2790       SDOperand N2 = Op.getOperand(2);
2791       if (N1.getValueType() != MVT::i32)
2792         N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2793       if (N2.getValueType() != MVT::i32)
2794         N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2795       return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
2796     }
2797
2798     return SDOperand();
2799   }
2800   }
2801 }
2802
2803 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2804   switch (Opcode) {
2805   default: return NULL;
2806   case X86ISD::SHLD:               return "X86ISD::SHLD";
2807   case X86ISD::SHRD:               return "X86ISD::SHRD";
2808   case X86ISD::FAND:               return "X86ISD::FAND";
2809   case X86ISD::FXOR:               return "X86ISD::FXOR";
2810   case X86ISD::FILD:               return "X86ISD::FILD";
2811   case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
2812   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2813   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2814   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
2815   case X86ISD::FLD:                return "X86ISD::FLD";
2816   case X86ISD::FST:                return "X86ISD::FST";
2817   case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
2818   case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
2819   case X86ISD::CALL:               return "X86ISD::CALL";
2820   case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
2821   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
2822   case X86ISD::CMP:                return "X86ISD::CMP";
2823   case X86ISD::TEST:               return "X86ISD::TEST";
2824   case X86ISD::SETCC:              return "X86ISD::SETCC";
2825   case X86ISD::CMOV:               return "X86ISD::CMOV";
2826   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
2827   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
2828   case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
2829   case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
2830   case X86ISD::LOAD_PACK:          return "X86ISD::LOAD_PACK";
2831   case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
2832   case X86ISD::Wrapper:            return "X86ISD::Wrapper";
2833   case X86ISD::S2VEC:              return "X86ISD::S2VEC";
2834   case X86ISD::ZEXT_S2VEC:         return "X86ISD::ZEXT_S2VEC";
2835   case X86ISD::PEXTRW:             return "X86ISD::PEXTRW";
2836   case X86ISD::PINSRW:             return "X86ISD::PINSRW";
2837   }
2838 }
2839
2840 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2841                                                        uint64_t Mask,
2842                                                        uint64_t &KnownZero, 
2843                                                        uint64_t &KnownOne,
2844                                                        unsigned Depth) const {
2845   unsigned Opc = Op.getOpcode();
2846   assert((Opc >= ISD::BUILTIN_OP_END ||
2847           Opc == ISD::INTRINSIC_WO_CHAIN ||
2848           Opc == ISD::INTRINSIC_W_CHAIN ||
2849           Opc == ISD::INTRINSIC_VOID) &&
2850          "Should use MaskedValueIsZero if you don't know whether Op"
2851          " is a target node!");
2852
2853   KnownZero = KnownOne = 0;   // Don't know anything.
2854   switch (Opc) {
2855   default: break;
2856   case X86ISD::SETCC: 
2857     KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2858     break;
2859   }
2860 }
2861
2862 std::vector<unsigned> X86TargetLowering::
2863 getRegClassForInlineAsmConstraint(const std::string &Constraint,
2864                                   MVT::ValueType VT) const {
2865   if (Constraint.size() == 1) {
2866     // FIXME: not handling fp-stack yet!
2867     // FIXME: not handling MMX registers yet ('y' constraint).
2868     switch (Constraint[0]) {      // GCC X86 Constraint Letters
2869     default: break;  // Unknown constriant letter
2870     case 'r':   // GENERAL_REGS
2871     case 'R':   // LEGACY_REGS
2872       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2873                                    X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2874     case 'l':   // INDEX_REGS
2875       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2876                                    X86::ESI, X86::EDI, X86::EBP, 0);
2877     case 'q':   // Q_REGS (GENERAL_REGS in 64-bit mode)
2878     case 'Q':   // Q_REGS
2879       return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2880     case 'x':   // SSE_REGS if SSE1 allowed
2881       if (Subtarget->hasSSE1())
2882         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2883                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2884                                      0);
2885       return std::vector<unsigned>();
2886     case 'Y':   // SSE_REGS if SSE2 allowed
2887       if (Subtarget->hasSSE2())
2888         return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2889                                      X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2890                                      0);
2891       return std::vector<unsigned>();
2892     }
2893   }
2894   
2895   return std::vector<unsigned>();
2896 }
2897
2898 /// isLegalAddressImmediate - Return true if the integer value or
2899 /// GlobalValue can be used as the offset of the target addressing mode.
2900 bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2901   // X86 allows a sign-extended 32-bit immediate field.
2902   return (V > -(1LL << 32) && V < (1LL << 32)-1);
2903 }
2904
2905 bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2906   if (Subtarget->isTargetDarwin()) {
2907     Reloc::Model RModel = getTargetMachine().getRelocationModel();
2908     if (RModel == Reloc::Static)
2909       return true;
2910     else if (RModel == Reloc::DynamicNoPIC)
2911       return !DarwinGVRequiresExtraLoad(GV);
2912     else
2913       return false;
2914   } else
2915     return true;
2916 }
2917
2918 /// isShuffleMaskLegal - Targets can use this to indicate that they only
2919 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2920 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2921 /// are assumed to be legal.
2922 bool
2923 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2924   // Only do shuffles on 128-bit vector types for now.
2925   if (MVT::getSizeInBits(VT) == 64) return false;
2926   return (Mask.Val->getNumOperands() == 2 ||
2927           X86::isSplatMask(Mask.Val) ||
2928           X86::isPSHUFDMask(Mask.Val) ||
2929           isPSHUFHW_PSHUFLWMask(Mask.Val) ||
2930           X86::isSHUFPMask(Mask.Val) ||
2931           X86::isUNPCKLMask(Mask.Val) ||
2932           X86::isUNPCKHMask(Mask.Val));
2933 }