minor simplifications to this code, don't create a dead
[oota-llvm.git] / lib / Target / X86 / X86ISelLowering.cpp
1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 "X86MachineFunctionInfo.h"
19 #include "X86TargetMachine.h"
20 #include "llvm/CallingConv.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/Function.h"
25 #include "llvm/Intrinsics.h"
26 #include "llvm/ADT/BitVector.h"
27 #include "llvm/ADT/VectorExtras.h"
28 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
29 #include "llvm/CodeGen/CallingConvLower.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineModuleInfo.h"
34 #include "llvm/CodeGen/MachineRegisterInfo.h"
35 #include "llvm/CodeGen/PseudoSourceValue.h"
36 #include "llvm/CodeGen/SelectionDAG.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/Debug.h"
39 #include "llvm/Target/TargetOptions.h"
40 #include "llvm/ADT/SmallSet.h"
41 #include "llvm/ADT/StringExtras.h"
42 #include "llvm/ParamAttrsList.h"
43 using namespace llvm;
44
45 X86TargetLowering::X86TargetLowering(TargetMachine &TM)
46   : TargetLowering(TM) {
47   Subtarget = &TM.getSubtarget<X86Subtarget>();
48   X86ScalarSSEf64 = Subtarget->hasSSE2();
49   X86ScalarSSEf32 = Subtarget->hasSSE1();
50   X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
51   
52   bool Fast = false;
53
54   RegInfo = TM.getRegisterInfo();
55
56   // Set up the TargetLowering object.
57
58   // X86 is weird, it always uses i8 for shift amounts and setcc results.
59   setShiftAmountType(MVT::i8);
60   setSetCCResultType(MVT::i8);
61   setSetCCResultContents(ZeroOrOneSetCCResult);
62   setSchedulingPreference(SchedulingForRegPressure);
63   setShiftAmountFlavor(Mask);   // shl X, 32 == shl X, 0
64   setStackPointerRegisterToSaveRestore(X86StackPtr);
65
66   if (Subtarget->isTargetDarwin()) {
67     // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
68     setUseUnderscoreSetJmp(false);
69     setUseUnderscoreLongJmp(false);
70   } else if (Subtarget->isTargetMingw()) {
71     // MS runtime is weird: it exports _setjmp, but longjmp!
72     setUseUnderscoreSetJmp(true);
73     setUseUnderscoreLongJmp(false);
74   } else {
75     setUseUnderscoreSetJmp(true);
76     setUseUnderscoreLongJmp(true);
77   }
78   
79   // Set up the register classes.
80   addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81   addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82   addRegisterClass(MVT::i32, X86::GR32RegisterClass);
83   if (Subtarget->is64Bit())
84     addRegisterClass(MVT::i64, X86::GR64RegisterClass);
85
86   setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
87
88   // We don't accept any truncstore of integer registers.  
89   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90   setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91   setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92   setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93   setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94   setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
96   // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97   // operation.
98   setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
99   setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
100   setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
101
102   if (Subtarget->is64Bit()) {
103     setOperationAction(ISD::UINT_TO_FP     , MVT::i64  , Expand);
104     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Promote);
105   } else {
106     if (X86ScalarSSEf64)
107       // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108       setOperationAction(ISD::UINT_TO_FP   , MVT::i32  , Expand);
109     else
110       setOperationAction(ISD::UINT_TO_FP   , MVT::i32  , Promote);
111   }
112
113   // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114   // this operation.
115   setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
116   setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
117   // SSE has no i16 to fp conversion, only i32
118   if (X86ScalarSSEf32) {
119     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
120     // f32 and f64 cases are Legal, f80 case is not
121     setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
122   } else {
123     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Custom);
124     setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
125   }
126
127   // In 32-bit mode these are custom lowered.  In 64-bit mode F32 and F64
128   // are Legal, f80 is custom lowered.
129   setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Custom);
130   setOperationAction(ISD::SINT_TO_FP     , MVT::i64  , Custom);
131
132   // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133   // this operation.
134   setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
135   setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
136
137   if (X86ScalarSSEf32) {
138     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
139     // f32 and f64 cases are Legal, f80 case is not
140     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
141   } else {
142     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
143     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
144   }
145
146   // Handle FP_TO_UINT by promoting the destination to a larger signed
147   // conversion.
148   setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
149   setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
150   setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
151
152   if (Subtarget->is64Bit()) {
153     setOperationAction(ISD::FP_TO_UINT     , MVT::i64  , Expand);
154     setOperationAction(ISD::FP_TO_UINT     , MVT::i32  , Promote);
155   } else {
156     if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
157       // Expand FP_TO_UINT into a select.
158       // FIXME: We would like to use a Custom expander here eventually to do
159       // the optimal thing for SSE vs. the default expansion in the legalizer.
160       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Expand);
161     else
162       // With SSE3 we can use fisttpll to convert to a signed i64.
163       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Promote);
164   }
165
166   // TODO: when we have SSE, these could be more efficient, by using movd/movq.
167   if (!X86ScalarSSEf64) {
168     setOperationAction(ISD::BIT_CONVERT      , MVT::f32  , Expand);
169     setOperationAction(ISD::BIT_CONVERT      , MVT::i32  , Expand);
170   }
171
172   // Scalar integer divide and remainder are lowered to use operations that
173   // produce two results, to match the available instructions. This exposes
174   // the two-result form to trivial CSE, which is able to combine x/y and x%y
175   // into a single instruction.
176   //
177   // Scalar integer multiply-high is also lowered to use two-result
178   // operations, to match the available instructions. However, plain multiply
179   // (low) operations are left as Legal, as there are single-result
180   // instructions for this in x86. Using the two-result multiply instructions
181   // when both high and low results are needed must be arranged by dagcombine.
182   setOperationAction(ISD::MULHS           , MVT::i8    , Expand);
183   setOperationAction(ISD::MULHU           , MVT::i8    , Expand);
184   setOperationAction(ISD::SDIV            , MVT::i8    , Expand);
185   setOperationAction(ISD::UDIV            , MVT::i8    , Expand);
186   setOperationAction(ISD::SREM            , MVT::i8    , Expand);
187   setOperationAction(ISD::UREM            , MVT::i8    , Expand);
188   setOperationAction(ISD::MULHS           , MVT::i16   , Expand);
189   setOperationAction(ISD::MULHU           , MVT::i16   , Expand);
190   setOperationAction(ISD::SDIV            , MVT::i16   , Expand);
191   setOperationAction(ISD::UDIV            , MVT::i16   , Expand);
192   setOperationAction(ISD::SREM            , MVT::i16   , Expand);
193   setOperationAction(ISD::UREM            , MVT::i16   , Expand);
194   setOperationAction(ISD::MULHS           , MVT::i32   , Expand);
195   setOperationAction(ISD::MULHU           , MVT::i32   , Expand);
196   setOperationAction(ISD::SDIV            , MVT::i32   , Expand);
197   setOperationAction(ISD::UDIV            , MVT::i32   , Expand);
198   setOperationAction(ISD::SREM            , MVT::i32   , Expand);
199   setOperationAction(ISD::UREM            , MVT::i32   , Expand);
200   setOperationAction(ISD::MULHS           , MVT::i64   , Expand);
201   setOperationAction(ISD::MULHU           , MVT::i64   , Expand);
202   setOperationAction(ISD::SDIV            , MVT::i64   , Expand);
203   setOperationAction(ISD::UDIV            , MVT::i64   , Expand);
204   setOperationAction(ISD::SREM            , MVT::i64   , Expand);
205   setOperationAction(ISD::UREM            , MVT::i64   , Expand);
206
207   setOperationAction(ISD::BR_JT            , MVT::Other, Expand);
208   setOperationAction(ISD::BRCOND           , MVT::Other, Custom);
209   setOperationAction(ISD::BR_CC            , MVT::Other, Expand);
210   setOperationAction(ISD::SELECT_CC        , MVT::Other, Expand);
211   setOperationAction(ISD::MEMMOVE          , MVT::Other, Expand);
212   if (Subtarget->is64Bit())
213     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Legal);
215   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Legal);
216   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
217   setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
218   setOperationAction(ISD::FREM             , MVT::f32  , Expand);
219   setOperationAction(ISD::FREM             , MVT::f64  , Expand);
220   setOperationAction(ISD::FREM             , MVT::f80  , Expand);
221   setOperationAction(ISD::FLT_ROUNDS_      , MVT::i32  , Custom);
222   
223   setOperationAction(ISD::CTPOP            , MVT::i8   , Expand);
224   setOperationAction(ISD::CTTZ             , MVT::i8   , Custom);
225   setOperationAction(ISD::CTLZ             , MVT::i8   , Custom);
226   setOperationAction(ISD::CTPOP            , MVT::i16  , Expand);
227   setOperationAction(ISD::CTTZ             , MVT::i16  , Custom);
228   setOperationAction(ISD::CTLZ             , MVT::i16  , Custom);
229   setOperationAction(ISD::CTPOP            , MVT::i32  , Expand);
230   setOperationAction(ISD::CTTZ             , MVT::i32  , Custom);
231   setOperationAction(ISD::CTLZ             , MVT::i32  , Custom);
232   if (Subtarget->is64Bit()) {
233     setOperationAction(ISD::CTPOP          , MVT::i64  , Expand);
234     setOperationAction(ISD::CTTZ           , MVT::i64  , Custom);
235     setOperationAction(ISD::CTLZ           , MVT::i64  , Custom);
236   }
237
238   setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
239   setOperationAction(ISD::BSWAP            , MVT::i16  , Expand);
240
241   // These should be promoted to a larger select which is supported.
242   setOperationAction(ISD::SELECT           , MVT::i1   , Promote);
243   setOperationAction(ISD::SELECT           , MVT::i8   , Promote);
244   // X86 wants to expand cmov itself.
245   setOperationAction(ISD::SELECT          , MVT::i16  , Custom);
246   setOperationAction(ISD::SELECT          , MVT::i32  , Custom);
247   setOperationAction(ISD::SELECT          , MVT::f32  , Custom);
248   setOperationAction(ISD::SELECT          , MVT::f64  , Custom);
249   setOperationAction(ISD::SELECT          , MVT::f80  , Custom);
250   setOperationAction(ISD::SETCC           , MVT::i8   , Custom);
251   setOperationAction(ISD::SETCC           , MVT::i16  , Custom);
252   setOperationAction(ISD::SETCC           , MVT::i32  , Custom);
253   setOperationAction(ISD::SETCC           , MVT::f32  , Custom);
254   setOperationAction(ISD::SETCC           , MVT::f64  , Custom);
255   setOperationAction(ISD::SETCC           , MVT::f80  , Custom);
256   if (Subtarget->is64Bit()) {
257     setOperationAction(ISD::SELECT        , MVT::i64  , Custom);
258     setOperationAction(ISD::SETCC         , MVT::i64  , Custom);
259   }
260   // X86 ret instruction may pop stack.
261   setOperationAction(ISD::RET             , MVT::Other, Custom);
262   if (!Subtarget->is64Bit())
263     setOperationAction(ISD::EH_RETURN       , MVT::Other, Custom);
264
265   // Darwin ABI issue.
266   setOperationAction(ISD::ConstantPool    , MVT::i32  , Custom);
267   setOperationAction(ISD::JumpTable       , MVT::i32  , Custom);
268   setOperationAction(ISD::GlobalAddress   , MVT::i32  , Custom);
269   setOperationAction(ISD::GlobalTLSAddress, MVT::i32  , Custom);
270   setOperationAction(ISD::ExternalSymbol  , MVT::i32  , Custom);
271   if (Subtarget->is64Bit()) {
272     setOperationAction(ISD::ConstantPool  , MVT::i64  , Custom);
273     setOperationAction(ISD::JumpTable     , MVT::i64  , Custom);
274     setOperationAction(ISD::GlobalAddress , MVT::i64  , Custom);
275     setOperationAction(ISD::ExternalSymbol, MVT::i64  , Custom);
276   }
277   // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
278   setOperationAction(ISD::SHL_PARTS       , MVT::i32  , Custom);
279   setOperationAction(ISD::SRA_PARTS       , MVT::i32  , Custom);
280   setOperationAction(ISD::SRL_PARTS       , MVT::i32  , Custom);
281   if (Subtarget->is64Bit()) {
282     setOperationAction(ISD::SHL_PARTS     , MVT::i64  , Custom);
283     setOperationAction(ISD::SRA_PARTS     , MVT::i64  , Custom);
284     setOperationAction(ISD::SRL_PARTS     , MVT::i64  , Custom);
285   }
286   // X86 wants to expand memset / memcpy itself.
287   setOperationAction(ISD::MEMSET          , MVT::Other, Custom);
288   setOperationAction(ISD::MEMCPY          , MVT::Other, Custom);
289
290   if (!Subtarget->hasSSE1())
291     setOperationAction(ISD::PREFETCH      , MVT::Other, Expand);
292
293   if (!Subtarget->hasSSE2())
294     setOperationAction(ISD::MEMBARRIER    , MVT::Other, Expand);
295
296   setOperationAction(ISD::ATOMIC_LCS     , MVT::i8, Custom);
297   setOperationAction(ISD::ATOMIC_LCS     , MVT::i16, Custom);
298   setOperationAction(ISD::ATOMIC_LCS     , MVT::i32, Custom);
299   setOperationAction(ISD::ATOMIC_LCS     , MVT::i64, Custom);
300
301   // Use the default ISD::LOCATION, ISD::DECLARE expansion.
302   setOperationAction(ISD::LOCATION, MVT::Other, Expand);
303   // FIXME - use subtarget debug flags
304   if (!Subtarget->isTargetDarwin() &&
305       !Subtarget->isTargetELF() &&
306       !Subtarget->isTargetCygMing())
307     setOperationAction(ISD::LABEL, MVT::Other, Expand);
308
309   setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
310   setOperationAction(ISD::EHSELECTION,   MVT::i64, Expand);
311   setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
312   setOperationAction(ISD::EHSELECTION,   MVT::i32, Expand);
313   if (Subtarget->is64Bit()) {
314     // FIXME: Verify
315     setExceptionPointerRegister(X86::RAX);
316     setExceptionSelectorRegister(X86::RDX);
317   } else {
318     setExceptionPointerRegister(X86::EAX);
319     setExceptionSelectorRegister(X86::EDX);
320   }
321   setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
322   
323   setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
324
325   setOperationAction(ISD::TRAP, MVT::Other, Legal);
326
327   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
328   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
329   setOperationAction(ISD::VAARG             , MVT::Other, Expand);
330   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
331   if (Subtarget->is64Bit())
332     setOperationAction(ISD::VACOPY          , MVT::Other, Custom);
333   else
334     setOperationAction(ISD::VACOPY          , MVT::Other, Expand);
335
336   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
337   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
338   if (Subtarget->is64Bit())
339     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
340   if (Subtarget->isTargetCygMing())
341     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
342   else
343     setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
344
345   if (X86ScalarSSEf64) {
346     // f32 and f64 use SSE.
347     // Set up the FP register classes.
348     addRegisterClass(MVT::f32, X86::FR32RegisterClass);
349     addRegisterClass(MVT::f64, X86::FR64RegisterClass);
350
351     // Use ANDPD to simulate FABS.
352     setOperationAction(ISD::FABS , MVT::f64, Custom);
353     setOperationAction(ISD::FABS , MVT::f32, Custom);
354
355     // Use XORP to simulate FNEG.
356     setOperationAction(ISD::FNEG , MVT::f64, Custom);
357     setOperationAction(ISD::FNEG , MVT::f32, Custom);
358
359     // Use ANDPD and ORPD to simulate FCOPYSIGN.
360     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
361     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
362
363     // We don't support sin/cos/fmod
364     setOperationAction(ISD::FSIN , MVT::f64, Expand);
365     setOperationAction(ISD::FCOS , MVT::f64, Expand);
366     setOperationAction(ISD::FSIN , MVT::f32, Expand);
367     setOperationAction(ISD::FCOS , MVT::f32, Expand);
368
369     // Expand FP immediates into loads from the stack, except for the special
370     // cases we handle.
371     addLegalFPImmediate(APFloat(+0.0)); // xorpd
372     addLegalFPImmediate(APFloat(+0.0f)); // xorps
373
374     // Floating truncations from f80 and extensions to f80 go through memory.
375     // If optimizing, we lie about this though and handle it in
376     // InstructionSelectPreprocess so that dagcombine2 can hack on these.
377     if (Fast) {
378       setConvertAction(MVT::f32, MVT::f80, Expand);
379       setConvertAction(MVT::f64, MVT::f80, Expand);
380       setConvertAction(MVT::f80, MVT::f32, Expand);
381       setConvertAction(MVT::f80, MVT::f64, Expand);
382     }
383   } else if (X86ScalarSSEf32) {
384     // Use SSE for f32, x87 for f64.
385     // Set up the FP register classes.
386     addRegisterClass(MVT::f32, X86::FR32RegisterClass);
387     addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
388
389     // Use ANDPS to simulate FABS.
390     setOperationAction(ISD::FABS , MVT::f32, Custom);
391
392     // Use XORP to simulate FNEG.
393     setOperationAction(ISD::FNEG , MVT::f32, Custom);
394
395     setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
396
397     // Use ANDPS and ORPS to simulate FCOPYSIGN.
398     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
399     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
400
401     // We don't support sin/cos/fmod
402     setOperationAction(ISD::FSIN , MVT::f32, Expand);
403     setOperationAction(ISD::FCOS , MVT::f32, Expand);
404
405     // Special cases we handle for FP constants.
406     addLegalFPImmediate(APFloat(+0.0f)); // xorps
407     addLegalFPImmediate(APFloat(+0.0)); // FLD0
408     addLegalFPImmediate(APFloat(+1.0)); // FLD1
409     addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
410     addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
411
412     // SSE <-> X87 conversions go through memory.  If optimizing, we lie about
413     // this though and handle it in InstructionSelectPreprocess so that
414     // dagcombine2 can hack on these.
415     if (Fast) {
416       setConvertAction(MVT::f32, MVT::f64, Expand);
417       setConvertAction(MVT::f32, MVT::f80, Expand);
418       setConvertAction(MVT::f80, MVT::f32, Expand);    
419       setConvertAction(MVT::f64, MVT::f32, Expand);
420       // And x87->x87 truncations also.
421       setConvertAction(MVT::f80, MVT::f64, Expand);
422     }
423
424     if (!UnsafeFPMath) {
425       setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
426       setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
427     }
428   } else {
429     // f32 and f64 in x87.
430     // Set up the FP register classes.
431     addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
432     addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
433
434     setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
435     setOperationAction(ISD::UNDEF,     MVT::f32, Expand);
436     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
437     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
438
439     // Floating truncations go through memory.  If optimizing, we lie about
440     // this though and handle it in InstructionSelectPreprocess so that
441     // dagcombine2 can hack on these.
442     if (Fast) {
443       setConvertAction(MVT::f80, MVT::f32, Expand);    
444       setConvertAction(MVT::f64, MVT::f32, Expand);
445       setConvertAction(MVT::f80, MVT::f64, Expand);
446     }
447
448     if (!UnsafeFPMath) {
449       setOperationAction(ISD::FSIN           , MVT::f64  , Expand);
450       setOperationAction(ISD::FCOS           , MVT::f64  , Expand);
451     }
452     addLegalFPImmediate(APFloat(+0.0)); // FLD0
453     addLegalFPImmediate(APFloat(+1.0)); // FLD1
454     addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
455     addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
456     addLegalFPImmediate(APFloat(+0.0f)); // FLD0
457     addLegalFPImmediate(APFloat(+1.0f)); // FLD1
458     addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
459     addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
460   }
461
462   // Long double always uses X87.
463   addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
464   setOperationAction(ISD::UNDEF,     MVT::f80, Expand);
465   setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
466   {
467     APFloat TmpFlt(+0.0);
468     TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
469     addLegalFPImmediate(TmpFlt);  // FLD0
470     TmpFlt.changeSign();
471     addLegalFPImmediate(TmpFlt);  // FLD0/FCHS
472     APFloat TmpFlt2(+1.0);
473     TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
474     addLegalFPImmediate(TmpFlt2);  // FLD1
475     TmpFlt2.changeSign();
476     addLegalFPImmediate(TmpFlt2);  // FLD1/FCHS
477   }
478     
479   if (!UnsafeFPMath) {
480     setOperationAction(ISD::FSIN           , MVT::f80  , Expand);
481     setOperationAction(ISD::FCOS           , MVT::f80  , Expand);
482   }
483
484   // Always use a library call for pow.
485   setOperationAction(ISD::FPOW             , MVT::f32  , Expand);
486   setOperationAction(ISD::FPOW             , MVT::f64  , Expand);
487   setOperationAction(ISD::FPOW             , MVT::f80  , Expand);
488
489   // First set operation action for all vector types to expand. Then we
490   // will selectively turn on ones that can be effectively codegen'd.
491   for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
492        VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
493     setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
494     setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
495     setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
496     setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
497     setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
498     setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
499     setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
500     setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
501     setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
502     setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
503     setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
504     setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
505     setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
506     setOperationAction(ISD::VECTOR_SHUFFLE,     (MVT::ValueType)VT, Expand);
507     setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
508     setOperationAction(ISD::INSERT_VECTOR_ELT,  (MVT::ValueType)VT, Expand);
509     setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
510     setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
511     setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
512     setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
513     setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
514     setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
515     setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
516     setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
517     setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
518     setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
519     setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
520     setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
521     setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
522     setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
523     setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
524     setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
525     setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
526     setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
527     setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
528     setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
529     setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
530   }
531
532   if (Subtarget->hasMMX()) {
533     addRegisterClass(MVT::v8i8,  X86::VR64RegisterClass);
534     addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
535     addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
536     addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
537
538     // FIXME: add MMX packed arithmetics
539
540     setOperationAction(ISD::ADD,                MVT::v8i8,  Legal);
541     setOperationAction(ISD::ADD,                MVT::v4i16, Legal);
542     setOperationAction(ISD::ADD,                MVT::v2i32, Legal);
543     setOperationAction(ISD::ADD,                MVT::v1i64, Legal);
544
545     setOperationAction(ISD::SUB,                MVT::v8i8,  Legal);
546     setOperationAction(ISD::SUB,                MVT::v4i16, Legal);
547     setOperationAction(ISD::SUB,                MVT::v2i32, Legal);
548     setOperationAction(ISD::SUB,                MVT::v1i64, Legal);
549
550     setOperationAction(ISD::MULHS,              MVT::v4i16, Legal);
551     setOperationAction(ISD::MUL,                MVT::v4i16, Legal);
552
553     setOperationAction(ISD::AND,                MVT::v8i8,  Promote);
554     AddPromotedToType (ISD::AND,                MVT::v8i8,  MVT::v1i64);
555     setOperationAction(ISD::AND,                MVT::v4i16, Promote);
556     AddPromotedToType (ISD::AND,                MVT::v4i16, MVT::v1i64);
557     setOperationAction(ISD::AND,                MVT::v2i32, Promote);
558     AddPromotedToType (ISD::AND,                MVT::v2i32, MVT::v1i64);
559     setOperationAction(ISD::AND,                MVT::v1i64, Legal);
560
561     setOperationAction(ISD::OR,                 MVT::v8i8,  Promote);
562     AddPromotedToType (ISD::OR,                 MVT::v8i8,  MVT::v1i64);
563     setOperationAction(ISD::OR,                 MVT::v4i16, Promote);
564     AddPromotedToType (ISD::OR,                 MVT::v4i16, MVT::v1i64);
565     setOperationAction(ISD::OR,                 MVT::v2i32, Promote);
566     AddPromotedToType (ISD::OR,                 MVT::v2i32, MVT::v1i64);
567     setOperationAction(ISD::OR,                 MVT::v1i64, Legal);
568
569     setOperationAction(ISD::XOR,                MVT::v8i8,  Promote);
570     AddPromotedToType (ISD::XOR,                MVT::v8i8,  MVT::v1i64);
571     setOperationAction(ISD::XOR,                MVT::v4i16, Promote);
572     AddPromotedToType (ISD::XOR,                MVT::v4i16, MVT::v1i64);
573     setOperationAction(ISD::XOR,                MVT::v2i32, Promote);
574     AddPromotedToType (ISD::XOR,                MVT::v2i32, MVT::v1i64);
575     setOperationAction(ISD::XOR,                MVT::v1i64, Legal);
576
577     setOperationAction(ISD::LOAD,               MVT::v8i8,  Promote);
578     AddPromotedToType (ISD::LOAD,               MVT::v8i8,  MVT::v1i64);
579     setOperationAction(ISD::LOAD,               MVT::v4i16, Promote);
580     AddPromotedToType (ISD::LOAD,               MVT::v4i16, MVT::v1i64);
581     setOperationAction(ISD::LOAD,               MVT::v2i32, Promote);
582     AddPromotedToType (ISD::LOAD,               MVT::v2i32, MVT::v1i64);
583     setOperationAction(ISD::LOAD,               MVT::v1i64, Legal);
584
585     setOperationAction(ISD::BUILD_VECTOR,       MVT::v8i8,  Custom);
586     setOperationAction(ISD::BUILD_VECTOR,       MVT::v4i16, Custom);
587     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2i32, Custom);
588     setOperationAction(ISD::BUILD_VECTOR,       MVT::v1i64, Custom);
589
590     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v8i8,  Custom);
591     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v4i16, Custom);
592     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2i32, Custom);
593     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v1i64, Custom);
594
595     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v8i8,  Custom);
596     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v4i16, Custom);
597     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v1i64, Custom);
598   }
599
600   if (Subtarget->hasSSE1()) {
601     addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
602
603     setOperationAction(ISD::FADD,               MVT::v4f32, Legal);
604     setOperationAction(ISD::FSUB,               MVT::v4f32, Legal);
605     setOperationAction(ISD::FMUL,               MVT::v4f32, Legal);
606     setOperationAction(ISD::FDIV,               MVT::v4f32, Legal);
607     setOperationAction(ISD::FSQRT,              MVT::v4f32, Legal);
608     setOperationAction(ISD::FNEG,               MVT::v4f32, Custom);
609     setOperationAction(ISD::LOAD,               MVT::v4f32, Legal);
610     setOperationAction(ISD::BUILD_VECTOR,       MVT::v4f32, Custom);
611     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v4f32, Custom);
612     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
613     setOperationAction(ISD::SELECT,             MVT::v4f32, Custom);
614   }
615
616   if (Subtarget->hasSSE2()) {
617     addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
618     addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
619     addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
620     addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
621     addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
622
623     setOperationAction(ISD::ADD,                MVT::v16i8, Legal);
624     setOperationAction(ISD::ADD,                MVT::v8i16, Legal);
625     setOperationAction(ISD::ADD,                MVT::v4i32, Legal);
626     setOperationAction(ISD::ADD,                MVT::v2i64, Legal);
627     setOperationAction(ISD::SUB,                MVT::v16i8, Legal);
628     setOperationAction(ISD::SUB,                MVT::v8i16, Legal);
629     setOperationAction(ISD::SUB,                MVT::v4i32, Legal);
630     setOperationAction(ISD::SUB,                MVT::v2i64, Legal);
631     setOperationAction(ISD::MUL,                MVT::v8i16, Legal);
632     setOperationAction(ISD::FADD,               MVT::v2f64, Legal);
633     setOperationAction(ISD::FSUB,               MVT::v2f64, Legal);
634     setOperationAction(ISD::FMUL,               MVT::v2f64, Legal);
635     setOperationAction(ISD::FDIV,               MVT::v2f64, Legal);
636     setOperationAction(ISD::FSQRT,              MVT::v2f64, Legal);
637     setOperationAction(ISD::FNEG,               MVT::v2f64, Custom);
638
639     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v16i8, Custom);
640     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v8i16, Custom);
641     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
642     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Custom);
643     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
644
645     // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
646     for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
647       // Do not attempt to custom lower non-power-of-2 vectors
648       if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
649         continue;
650       setOperationAction(ISD::BUILD_VECTOR,        (MVT::ValueType)VT, Custom);
651       setOperationAction(ISD::VECTOR_SHUFFLE,      (MVT::ValueType)VT, Custom);
652       setOperationAction(ISD::EXTRACT_VECTOR_ELT,  (MVT::ValueType)VT, Custom);
653     }
654     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2f64, Custom);
655     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2i64, Custom);
656     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2f64, Custom);
657     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2i64, Custom);
658     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2f64, Custom);
659     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
660     if (Subtarget->is64Bit()) {
661       setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Custom);
662       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
663     }
664
665     // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
666     for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
667       setOperationAction(ISD::AND,    (MVT::ValueType)VT, Promote);
668       AddPromotedToType (ISD::AND,    (MVT::ValueType)VT, MVT::v2i64);
669       setOperationAction(ISD::OR,     (MVT::ValueType)VT, Promote);
670       AddPromotedToType (ISD::OR,     (MVT::ValueType)VT, MVT::v2i64);
671       setOperationAction(ISD::XOR,    (MVT::ValueType)VT, Promote);
672       AddPromotedToType (ISD::XOR,    (MVT::ValueType)VT, MVT::v2i64);
673       setOperationAction(ISD::LOAD,   (MVT::ValueType)VT, Promote);
674       AddPromotedToType (ISD::LOAD,   (MVT::ValueType)VT, MVT::v2i64);
675       setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
676       AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
677     }
678
679     setTruncStoreAction(MVT::f64, MVT::f32, Expand);
680
681     // Custom lower v2i64 and v2f64 selects.
682     setOperationAction(ISD::LOAD,               MVT::v2f64, Legal);
683     setOperationAction(ISD::LOAD,               MVT::v2i64, Legal);
684     setOperationAction(ISD::SELECT,             MVT::v2f64, Custom);
685     setOperationAction(ISD::SELECT,             MVT::v2i64, Custom);
686   }
687   
688   if (Subtarget->hasSSE41()) {
689     // FIXME: Do we need to handle scalar-to-vector here?
690     setOperationAction(ISD::MUL,                MVT::v4i32, Legal);
691
692     // i8 and i16 vectors are custom , because the source register and source
693     // source memory operand types are not the same width.  f32 vectors are
694     // custom since the immediate controlling the insert encodes additional
695     // information.
696     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v16i8, Custom);
697     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
698     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Legal);
699     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
700
701     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
702     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
703     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
704     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Legal);
705
706     if (Subtarget->is64Bit()) {
707       setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Legal);
708       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
709     }
710   }
711
712   // We want to custom lower some of our intrinsics.
713   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
714
715   // We have target-specific dag combine patterns for the following nodes:
716   setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
717   setTargetDAGCombine(ISD::SELECT);
718   setTargetDAGCombine(ISD::STORE);
719
720   computeRegisterProperties();
721
722   // FIXME: These should be based on subtarget info. Plus, the values should
723   // be smaller when we are in optimizing for size mode.
724   maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
725   maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
726   maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
727   allowUnalignedMemoryAccesses = true; // x86 supports it!
728   setPrefLoopAlignment(16);
729 }
730
731 /// getMaxByValAlign - Helper for getByValTypeAlignment to determine
732 /// the desired ByVal argument alignment.
733 static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
734   if (MaxAlign == 16)
735     return;
736   if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
737     if (VTy->getBitWidth() == 128)
738       MaxAlign = 16;
739   } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
740     unsigned EltAlign = 0;
741     getMaxByValAlign(ATy->getElementType(), EltAlign);
742     if (EltAlign > MaxAlign)
743       MaxAlign = EltAlign;
744   } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
745     for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
746       unsigned EltAlign = 0;
747       getMaxByValAlign(STy->getElementType(i), EltAlign);
748       if (EltAlign > MaxAlign)
749         MaxAlign = EltAlign;
750       if (MaxAlign == 16)
751         break;
752     }
753   }
754   return;
755 }
756
757 /// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
758 /// function arguments in the caller parameter area. For X86, aggregates
759 /// that contain SSE vectors are placed at 16-byte boundaries while the rest
760 /// are at 4-byte boundaries.
761 unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
762   if (Subtarget->is64Bit())
763     return getTargetData()->getABITypeAlignment(Ty);
764   unsigned Align = 4;
765   if (Subtarget->hasSSE1())
766     getMaxByValAlign(Ty, Align);
767   return Align;
768 }
769
770 /// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
771 /// jumptable.
772 SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
773                                                       SelectionDAG &DAG) const {
774   if (usesGlobalOffsetTable())
775     return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
776   if (!Subtarget->isPICStyleRIPRel())
777     return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
778   return Table;
779 }
780
781 //===----------------------------------------------------------------------===//
782 //               Return Value Calling Convention Implementation
783 //===----------------------------------------------------------------------===//
784
785 #include "X86GenCallingConv.inc"
786
787 /// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
788 /// exists skip possible ISD:TokenFactor.
789 static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
790   if (Chain.getOpcode() == X86ISD::TAILCALL) {
791     return Chain;
792   } else if (Chain.getOpcode() == ISD::TokenFactor) {
793     if (Chain.getNumOperands() &&
794         Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
795       return Chain.getOperand(0);
796   }
797   return Chain;
798 }
799
800 /// LowerRET - Lower an ISD::RET node.
801 SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
802   assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
803   
804   SmallVector<CCValAssign, 16> RVLocs;
805   unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
806   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
807   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
808   CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
809     
810   // If this is the first return lowered for this function, add the regs to the
811   // liveout set for the function.
812   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
813     for (unsigned i = 0; i != RVLocs.size(); ++i)
814       if (RVLocs[i].isRegLoc())
815         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
816   }
817   SDOperand Chain = Op.getOperand(0);
818   
819   // Handle tail call return.
820   Chain = GetPossiblePreceedingTailCall(Chain);
821   if (Chain.getOpcode() == X86ISD::TAILCALL) {
822     SDOperand TailCall = Chain;
823     SDOperand TargetAddress = TailCall.getOperand(1);
824     SDOperand StackAdjustment = TailCall.getOperand(2);
825     assert(((TargetAddress.getOpcode() == ISD::Register &&
826                (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
827                 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
828               TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
829               TargetAddress.getOpcode() == ISD::TargetGlobalAddress) && 
830              "Expecting an global address, external symbol, or register");
831     assert(StackAdjustment.getOpcode() == ISD::Constant &&
832            "Expecting a const value");
833
834     SmallVector<SDOperand,8> Operands;
835     Operands.push_back(Chain.getOperand(0));
836     Operands.push_back(TargetAddress);
837     Operands.push_back(StackAdjustment);
838     // Copy registers used by the call. Last operand is a flag so it is not
839     // copied.
840     for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
841       Operands.push_back(Chain.getOperand(i));
842     }
843     return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0], 
844                        Operands.size());
845   }
846   
847   // Regular return.
848   SDOperand Flag;
849
850   // Copy the result values into the output registers.
851   if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
852       RVLocs[0].getLocReg() != X86::ST0) {
853     for (unsigned i = 0; i != RVLocs.size(); ++i) {
854       CCValAssign &VA = RVLocs[i];
855       assert(VA.isRegLoc() && "Can only return in registers!");
856       Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
857                                Flag);
858       Flag = Chain.getValue(1);
859     }
860   } else {
861     // We need to handle a destination of ST0 specially, because it isn't really
862     // a register.
863     SDOperand Value = Op.getOperand(1);
864     
865     // an XMM register onto the fp-stack.  Do this with an FP_EXTEND to f80.
866     // This will get legalized into a load/store if it can't get optimized away.
867     if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
868       Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
869     
870     SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
871     SDOperand Ops[] = { Chain, Value };
872     Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
873     Flag = Chain.getValue(1);
874   }
875   
876   SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
877   if (Flag.Val)
878     return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
879   else
880     return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
881 }
882
883
884 /// LowerCallResult - Lower the result values of an ISD::CALL into the
885 /// appropriate copies out of appropriate physical registers.  This assumes that
886 /// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
887 /// being lowered.  The returns a SDNode with the same number of values as the
888 /// ISD::CALL.
889 SDNode *X86TargetLowering::
890 LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall, 
891                 unsigned CallingConv, SelectionDAG &DAG) {
892   
893   // Assign locations to each value returned by this call.
894   SmallVector<CCValAssign, 16> RVLocs;
895   bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
896   CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
897   CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
898
899   SmallVector<SDOperand, 8> ResultVals;
900   
901   // Copy all of the result registers out of their specified physreg.
902   if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
903     for (unsigned i = 0; i != RVLocs.size(); ++i) {
904       Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
905                                  RVLocs[i].getValVT(), InFlag).getValue(1);
906       InFlag = Chain.getValue(2);
907       ResultVals.push_back(Chain.getValue(0));
908     }
909   } else {
910     // Copies from the FP stack are special, as ST0 isn't a valid register
911     // before the fp stackifier runs.
912     
913     // Copy ST0 into an RFP register with FP_GET_RESULT.  If this will end up
914     // in an SSE register, copy it out as F80 and do a truncate, otherwise use
915     // the specified value type.
916     MVT::ValueType GetResultTy = RVLocs[0].getValVT();
917     if (isScalarFPTypeInSSEReg(GetResultTy))
918       GetResultTy = MVT::f80;
919     SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
920     
921     SDOperand GROps[] = { Chain, InFlag };
922     SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
923     Chain  = RetVal.getValue(1);
924     InFlag = RetVal.getValue(2);
925
926     // If we want the result in an SSE register, use an FP_TRUNCATE to get it
927     // there.
928     if (GetResultTy != RVLocs[0].getValVT())
929       RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
930                            // This truncation won't change the value.
931                            DAG.getIntPtrConstant(1));
932     
933     ResultVals.push_back(RetVal);
934   }
935   
936   // Merge everything together with a MERGE_VALUES node.
937   ResultVals.push_back(Chain);
938   return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
939                      &ResultVals[0], ResultVals.size()).Val;
940 }
941
942 /// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
943 /// ISD::CALL where the results are known to be in two 64-bit registers,
944 /// e.g. XMM0 and XMM1. This simplify store the two values back to the
945 /// fixed stack slot allocated for StructRet.
946 SDNode *X86TargetLowering::
947 LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
948                               SDNode *TheCall, unsigned Reg1, unsigned Reg2,
949                               MVT::ValueType VT, SelectionDAG &DAG) {
950   SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
951   Chain = RetVal1.getValue(1);
952   InFlag = RetVal1.getValue(2);
953   SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
954   Chain = RetVal2.getValue(1);
955   InFlag = RetVal2.getValue(2);
956   SDOperand FIN = TheCall->getOperand(5);
957   Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
958   FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
959   Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
960   return Chain.Val;
961 }
962
963 /// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
964 /// where the results are known to be in ST0 and ST1.
965 SDNode *X86TargetLowering::
966 LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
967                             SDNode *TheCall, SelectionDAG &DAG) {
968   SmallVector<SDOperand, 8> ResultVals;
969   const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
970   SDVTList Tys = DAG.getVTList(VTs, 4);
971   SDOperand Ops[] = { Chain, InFlag };
972   SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
973   Chain = RetVal.getValue(2);
974   SDOperand FIN = TheCall->getOperand(5);
975   Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
976   FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
977   Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
978   return Chain.Val;
979 }
980
981 //===----------------------------------------------------------------------===//
982 //                C & StdCall & Fast Calling Convention implementation
983 //===----------------------------------------------------------------------===//
984 //  StdCall calling convention seems to be standard for many Windows' API
985 //  routines and around. It differs from C calling convention just a little:
986 //  callee should clean up the stack, not caller. Symbols should be also
987 //  decorated in some fancy way :) It doesn't support any vector arguments.
988 //  For info on fast calling convention see Fast Calling Convention (tail call)
989 //  implementation LowerX86_32FastCCCallTo.
990
991 /// AddLiveIn - This helper function adds the specified physical register to the
992 /// MachineFunction as a live in value.  It also creates a corresponding virtual
993 /// register for it.
994 static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
995                           const TargetRegisterClass *RC) {
996   assert(RC->contains(PReg) && "Not the correct regclass!");
997   unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
998   MF.getRegInfo().addLiveIn(PReg, VReg);
999   return VReg;
1000 }
1001
1002 /// CallIsStructReturn - Determines whether a CALL node uses struct return
1003 /// semantics.
1004 static bool CallIsStructReturn(SDOperand Op) {
1005   unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1006   if (!NumOps)
1007     return false;
1008   
1009   ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
1010   return Flags->getValue() & ISD::ParamFlags::StructReturn;
1011 }
1012
1013 /// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1014 /// return semantics.
1015 static bool ArgsAreStructReturn(SDOperand Op) {
1016   unsigned NumArgs = Op.Val->getNumValues() - 1;
1017   if (!NumArgs)
1018     return false;
1019   
1020   ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
1021   return Flags->getValue() & ISD::ParamFlags::StructReturn;
1022 }
1023
1024 /// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires the
1025 /// callee to pop its own arguments. Callee pop is necessary to support tail
1026 /// calls.
1027 bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1028   bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1029   if (IsVarArg)
1030     return false;
1031
1032   switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1033   default:
1034     return false;
1035   case CallingConv::X86_StdCall:
1036     return !Subtarget->is64Bit();
1037   case CallingConv::X86_FastCall:
1038     return !Subtarget->is64Bit();
1039   case CallingConv::Fast:
1040     return PerformTailCallOpt;
1041   }
1042 }
1043
1044 /// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1045 /// FORMAL_ARGUMENTS node.
1046 CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1047   unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1048   
1049   if (Subtarget->is64Bit()) {
1050     if (CC == CallingConv::Fast && PerformTailCallOpt)
1051       return CC_X86_64_TailCall;
1052     else
1053       return CC_X86_64_C;
1054   }
1055
1056   if (CC == CallingConv::X86_FastCall)
1057     return CC_X86_32_FastCall;
1058   else if (CC == CallingConv::Fast && PerformTailCallOpt)
1059     return CC_X86_32_TailCall;
1060   else
1061     return CC_X86_32_C;
1062 }
1063
1064 /// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1065 /// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
1066 NameDecorationStyle
1067 X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1068   unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1069   if (CC == CallingConv::X86_FastCall)
1070     return FastCall;
1071   else if (CC == CallingConv::X86_StdCall)
1072     return StdCall;
1073   return None;
1074 }
1075
1076 /// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could
1077 /// possibly be overwritten when lowering the outgoing arguments in a tail
1078 /// call. Currently the implementation of this call is very conservative and
1079 /// assumes all arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with
1080 /// virtual registers would be overwritten by direct lowering.
1081 static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op, 
1082                                                     MachineFrameInfo * MFI) {
1083   RegisterSDNode * OpReg = NULL;
1084   FrameIndexSDNode * FrameIdxNode = NULL;
1085   int FrameIdx = 0;
1086   if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1087       (Op.getOpcode()== ISD::CopyFromReg &&
1088        (OpReg = dyn_cast<RegisterSDNode>(Op.getOperand(1))) &&
1089        (OpReg->getReg() >= TargetRegisterInfo::FirstVirtualRegister)) ||
1090       (Op.getOpcode() == ISD::LOAD &&
1091        (FrameIdxNode = dyn_cast<FrameIndexSDNode>(Op.getOperand(1))) &&
1092        (MFI->isFixedObjectIndex((FrameIdx = FrameIdxNode->getIndex()))) &&
1093        (MFI->getObjectOffset(FrameIdx) >= 0)))
1094     return true;
1095   return false;
1096 }
1097
1098 /// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1099 /// in a register before calling.
1100 bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1101   return !IsTailCall && !Is64Bit &&
1102     getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1103     Subtarget->isPICStyleGOT();
1104 }
1105
1106
1107 /// CallRequiresFnAddressInReg - Check whether the call requires the function
1108 /// address to be loaded in a register.
1109 bool 
1110 X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1111   return !Is64Bit && IsTailCall &&  
1112     getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1113     Subtarget->isPICStyleGOT();
1114 }
1115
1116 /// CopyTailCallClobberedArgumentsToVRegs - Create virtual registers for all
1117 /// arguments to force loading and guarantee that arguments sourcing from
1118 /// incomming parameters are not overwriting each other.
1119 static SDOperand 
1120 CopyTailCallClobberedArgumentsToVRegs(SDOperand Chain,
1121      SmallVector<std::pair<unsigned, SDOperand>, 8> &TailCallClobberedVRegs,
1122                                       SelectionDAG &DAG,
1123                                       MachineFunction &MF,
1124                                       const TargetLowering * TL) {
1125       
1126   SDOperand InFlag;
1127   for (unsigned i = 0, e = TailCallClobberedVRegs.size(); i != e; i++) {
1128     SDOperand Arg = TailCallClobberedVRegs[i].second;
1129     unsigned Idx = TailCallClobberedVRegs[i].first;
1130     unsigned VReg = 
1131       MF.getRegInfo().
1132       createVirtualRegister(TL->getRegClassFor(Arg.getValueType()));
1133     Chain = DAG.getCopyToReg(Chain, VReg, Arg, InFlag);
1134     InFlag = Chain.getValue(1);
1135     Arg = DAG.getCopyFromReg(Chain, VReg, Arg.getValueType(), InFlag);
1136     TailCallClobberedVRegs[i] = std::make_pair(Idx, Arg);
1137     Chain = Arg.getValue(1);
1138     InFlag = Arg.getValue(2);
1139   }
1140   return Chain;
1141
1142
1143 /// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1144 /// by "Src" to address "Dst" with size and alignment information specified by
1145 /// the specific parameter attribute. The copy will be passed as a byval function
1146 /// parameter.
1147 static SDOperand 
1148 CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1149                           unsigned Flags, SelectionDAG &DAG) {
1150   unsigned Align = 1 <<
1151     ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1152   unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1153     ISD::ParamFlags::ByValSizeOffs;
1154   SDOperand AlignNode    = DAG.getConstant(Align, MVT::i32);
1155   SDOperand SizeNode     = DAG.getConstant(Size, MVT::i32);
1156   SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
1157   return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
1158 }
1159
1160 SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1161                                               const CCValAssign &VA,
1162                                               MachineFrameInfo *MFI,
1163                                               unsigned CC,
1164                                               SDOperand Root, unsigned i) {
1165   // Create the nodes corresponding to a load from this parameter slot.
1166   unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1167   bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
1168   bool isByVal = Flags & ISD::ParamFlags::ByVal;
1169   bool isImmutable = !AlwaysUseMutable && !isByVal;
1170
1171   // FIXME: For now, all byval parameter objects are marked mutable. This can be
1172   // changed with more analysis.  
1173   // In case of tail call optimization mark all arguments mutable. Since they
1174   // could be overwritten by lowering of arguments in case of a tail call.
1175   int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1176                                   VA.getLocMemOffset(), isImmutable);
1177   SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1178   if (isByVal)
1179     return FIN;
1180   return DAG.getLoad(VA.getValVT(), Root, FIN,
1181                      PseudoSourceValue::getFixedStack(), FI);
1182 }
1183
1184 SDOperand
1185 X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
1186   MachineFunction &MF = DAG.getMachineFunction();
1187   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1188   
1189   const Function* Fn = MF.getFunction();
1190   if (Fn->hasExternalLinkage() &&
1191       Subtarget->isTargetCygMing() &&
1192       Fn->getName() == "main")
1193     FuncInfo->setForceFramePointer(true);
1194
1195   // Decorate the function name.
1196   FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1197   
1198   MachineFrameInfo *MFI = MF.getFrameInfo();
1199   SDOperand Root = Op.getOperand(0);
1200   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1201   unsigned CC = MF.getFunction()->getCallingConv();
1202   bool Is64Bit = Subtarget->is64Bit();
1203
1204   assert(!(isVarArg && CC == CallingConv::Fast) &&
1205          "Var args not supported with calling convention fastcc");
1206
1207   // Assign locations to all of the incoming arguments.
1208   SmallVector<CCValAssign, 16> ArgLocs;
1209   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1210   CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
1211   
1212   SmallVector<SDOperand, 8> ArgValues;
1213   unsigned LastVal = ~0U;
1214   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1215     CCValAssign &VA = ArgLocs[i];
1216     // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1217     // places.
1218     assert(VA.getValNo() != LastVal &&
1219            "Don't support value assigned to multiple locs yet");
1220     LastVal = VA.getValNo();
1221     
1222     if (VA.isRegLoc()) {
1223       MVT::ValueType RegVT = VA.getLocVT();
1224       TargetRegisterClass *RC;
1225       if (RegVT == MVT::i32)
1226         RC = X86::GR32RegisterClass;
1227       else if (Is64Bit && RegVT == MVT::i64)
1228         RC = X86::GR64RegisterClass;
1229       else if (RegVT == MVT::f32)
1230         RC = X86::FR32RegisterClass;
1231       else if (RegVT == MVT::f64)
1232         RC = X86::FR64RegisterClass;
1233       else {
1234         assert(MVT::isVector(RegVT));
1235         if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1236           RC = X86::GR64RegisterClass;       // MMX values are passed in GPRs.
1237           RegVT = MVT::i64;
1238         } else
1239           RC = X86::VR128RegisterClass;
1240       }
1241
1242       unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1243       SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
1244       
1245       // If this is an 8 or 16-bit value, it is really passed promoted to 32
1246       // bits.  Insert an assert[sz]ext to capture this, then truncate to the
1247       // right size.
1248       if (VA.getLocInfo() == CCValAssign::SExt)
1249         ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1250                                DAG.getValueType(VA.getValVT()));
1251       else if (VA.getLocInfo() == CCValAssign::ZExt)
1252         ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1253                                DAG.getValueType(VA.getValVT()));
1254       
1255       if (VA.getLocInfo() != CCValAssign::Full)
1256         ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1257       
1258       // Handle MMX values passed in GPRs.
1259       if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1260           MVT::getSizeInBits(RegVT) == 64)
1261         ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1262       
1263       ArgValues.push_back(ArgValue);
1264     } else {
1265       assert(VA.isMemLoc());
1266       ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
1267     }
1268   }
1269
1270   unsigned StackSize = CCInfo.getNextStackOffset();
1271   // align stack specially for tail calls
1272   if (CC == CallingConv::Fast)
1273     StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
1274
1275   // If the function takes variable number of arguments, make a frame index for
1276   // the start of the first vararg value... for expansion of llvm.va_start.
1277   if (isVarArg) {
1278     if (Is64Bit || CC != CallingConv::X86_FastCall) {
1279       VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1280     }
1281     if (Is64Bit) {
1282       static const unsigned GPR64ArgRegs[] = {
1283         X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8,  X86::R9
1284       };
1285       static const unsigned XMMArgRegs[] = {
1286         X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1287         X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1288       };
1289       
1290       unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1291       unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1292     
1293       // For X86-64, if there are vararg parameters that are passed via
1294       // registers, then we must store them to their spots on the stack so they
1295       // may be loaded by deferencing the result of va_next.
1296       VarArgsGPOffset = NumIntRegs * 8;
1297       VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1298       RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1299       
1300       // Store the integer parameter registers.
1301       SmallVector<SDOperand, 8> MemOps;
1302       SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1303       SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1304                                   DAG.getIntPtrConstant(VarArgsGPOffset));
1305       for (; NumIntRegs != 6; ++NumIntRegs) {
1306         unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1307                                   X86::GR64RegisterClass);
1308         SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1309         SDOperand Store =
1310           DAG.getStore(Val.getValue(1), Val, FIN,
1311                        PseudoSourceValue::getFixedStack(),
1312                        RegSaveFrameIndex);
1313         MemOps.push_back(Store);
1314         FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1315                           DAG.getIntPtrConstant(8));
1316       }
1317       
1318       // Now store the XMM (fp + vector) parameter registers.
1319       FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1320                         DAG.getIntPtrConstant(VarArgsFPOffset));
1321       for (; NumXMMRegs != 8; ++NumXMMRegs) {
1322         unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1323                                   X86::VR128RegisterClass);
1324         SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1325         SDOperand Store =
1326           DAG.getStore(Val.getValue(1), Val, FIN,
1327                        PseudoSourceValue::getFixedStack(),
1328                        RegSaveFrameIndex);
1329         MemOps.push_back(Store);
1330         FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1331                           DAG.getIntPtrConstant(16));
1332       }
1333       if (!MemOps.empty())
1334           Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1335                              &MemOps[0], MemOps.size());
1336     }
1337   }
1338   
1339   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1340   // arguments and the arguments after the retaddr has been pushed are
1341   // aligned.
1342   if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1343       !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1344       (StackSize & 7) == 0)
1345     StackSize += 4;
1346
1347   ArgValues.push_back(Root);
1348
1349   // Some CCs need callee pop.
1350   if (IsCalleePop(Op)) {
1351     BytesToPopOnReturn  = StackSize; // Callee pops everything.
1352     BytesCallerReserves = 0;
1353   } else {
1354     BytesToPopOnReturn  = 0; // Callee pops nothing.
1355     // If this is an sret function, the return should pop the hidden pointer.
1356     if (!Is64Bit && ArgsAreStructReturn(Op))
1357       BytesToPopOnReturn = 4;  
1358     BytesCallerReserves = StackSize;
1359   }
1360
1361   if (!Is64Bit) {
1362     RegSaveFrameIndex = 0xAAAAAAA;   // RegSaveFrameIndex is X86-64 only.
1363     if (CC == CallingConv::X86_FastCall)
1364       VarArgsFrameIndex = 0xAAAAAAA;   // fastcc functions can't have varargs.
1365   }
1366
1367   FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1368
1369   // Return the new list of results.
1370   return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1371                      &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1372 }
1373
1374 SDOperand
1375 X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1376                                     const SDOperand &StackPtr,
1377                                     const CCValAssign &VA,
1378                                     SDOperand Chain,
1379                                     SDOperand Arg) {
1380   unsigned LocMemOffset = VA.getLocMemOffset();
1381   SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
1382   PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1383   SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1384   unsigned Flags    = cast<ConstantSDNode>(FlagsOp)->getValue();
1385   if (Flags & ISD::ParamFlags::ByVal) {
1386     return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
1387   }
1388   return DAG.getStore(Chain, Arg, PtrOff,
1389                       PseudoSourceValue::getStack(), LocMemOffset);
1390 }
1391
1392 /// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1393 /// struct return call to the specified function. X86-64 ABI specifies
1394 /// some SRet calls are actually returned in registers. Since current
1395 /// LLVM cannot represent multi-value calls, they are represent as 
1396 /// calls where the results are passed in a hidden struct provided by
1397 /// the caller. This function examines the type of the struct to
1398 /// determine the correct way to implement the call.
1399 X86::X86_64SRet
1400 X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1401   // FIXME: Disabled for now.
1402   return X86::InMemory;
1403
1404   const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1405   const Type *RTy = PTy->getElementType();
1406   unsigned Size = getTargetData()->getABITypeSize(RTy);
1407   if (Size != 16 && Size != 32)
1408     return X86::InMemory;
1409
1410   if (Size == 32) {
1411     const StructType *STy = dyn_cast<StructType>(RTy);
1412     if (!STy) return X86::InMemory;
1413     if (STy->getNumElements() == 2 &&
1414         STy->getElementType(0) == Type::X86_FP80Ty &&
1415         STy->getElementType(1) == Type::X86_FP80Ty)
1416       return X86::InX87;
1417   }
1418
1419   bool AllFP = true;
1420   for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1421        I != E; ++I) {
1422     const Type *STy = I->get();
1423     if (!STy->isFPOrFPVector()) {
1424       AllFP = false;
1425       break;
1426     }
1427   }
1428
1429   if (AllFP)
1430     return X86::InSSE;
1431   return X86::InGPR64;
1432 }
1433
1434 void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1435                                                       CCAssignFn *Fn,
1436                                                       CCState &CCInfo) {
1437   unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1438   for (unsigned i = 1; i != NumOps; ++i) {
1439     MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1440     SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1441     unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1442     if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1443       cerr << "Call operand #" << i << " has unhandled type "
1444            << MVT::getValueTypeString(ArgVT) << "\n";
1445       abort();
1446     }
1447   }
1448 }
1449
1450 SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1451   MachineFunction &MF = DAG.getMachineFunction();
1452   MachineFrameInfo * MFI = MF.getFrameInfo();
1453   SDOperand Chain     = Op.getOperand(0);
1454   unsigned CC         = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1455   bool isVarArg       = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1456   bool IsTailCall     = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1457                         && CC == CallingConv::Fast && PerformTailCallOpt;
1458   SDOperand Callee    = Op.getOperand(4);
1459   bool Is64Bit        = Subtarget->is64Bit();
1460   bool IsStructRet    = CallIsStructReturn(Op);
1461
1462   assert(!(isVarArg && CC == CallingConv::Fast) &&
1463          "Var args not supported with calling convention fastcc");
1464
1465   // Analyze operands of the call, assigning locations to each operand.
1466   SmallVector<CCValAssign, 16> ArgLocs;
1467   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
1468   CCAssignFn *CCFn = CCAssignFnForNode(Op);
1469
1470   X86::X86_64SRet SRetMethod = X86::InMemory;
1471   if (Is64Bit && IsStructRet)
1472     // FIXME: We can't figure out type of the sret structure for indirect
1473     // calls. We need to copy more information from CallSite to the ISD::CALL
1474     // node.
1475     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1476       SRetMethod =
1477         ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1478
1479   // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1480   // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1481   // a sret call.
1482   if (SRetMethod != X86::InMemory)
1483     X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1484   else 
1485     CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
1486   
1487   // Get a count of how many bytes are to be pushed on the stack.
1488   unsigned NumBytes = CCInfo.getNextStackOffset();
1489   if (CC == CallingConv::Fast)
1490     NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
1491
1492   // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1493   // arguments and the arguments after the retaddr has been pushed are aligned.
1494   if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1495       !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1496       (NumBytes & 7) == 0)
1497     NumBytes += 4;
1498
1499   int FPDiff = 0;
1500   if (IsTailCall) {
1501     // Lower arguments at fp - stackoffset + fpdiff.
1502     unsigned NumBytesCallerPushed = 
1503       MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1504     FPDiff = NumBytesCallerPushed - NumBytes;
1505
1506     // Set the delta of movement of the returnaddr stackslot.
1507     // But only set if delta is greater than previous delta.
1508     if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1509       MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1510   }
1511
1512   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
1513
1514   SDOperand RetAddrFrIdx;
1515   if (IsTailCall) {
1516     // Adjust the Return address stack slot.
1517     if (FPDiff) {
1518       MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1519       RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1520       // Load the "old" Return address.
1521       RetAddrFrIdx = 
1522         DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1523       Chain = SDOperand(RetAddrFrIdx.Val, 1);
1524     }
1525   }
1526
1527   SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1528   SmallVector<std::pair<unsigned, SDOperand>, 8> TailCallClobberedVRegs;
1529   SmallVector<SDOperand, 8> MemOpChains;
1530
1531   SDOperand StackPtr;
1532
1533   // Walk the register/memloc assignments, inserting copies/loads.  For tail
1534   // calls, remember all arguments for later special lowering.
1535   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1536     CCValAssign &VA = ArgLocs[i];
1537     SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1538     
1539     // Promote the value if needed.
1540     switch (VA.getLocInfo()) {
1541     default: assert(0 && "Unknown loc info!");
1542     case CCValAssign::Full: break;
1543     case CCValAssign::SExt:
1544       Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1545       break;
1546     case CCValAssign::ZExt:
1547       Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1548       break;
1549     case CCValAssign::AExt:
1550       Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1551       break;
1552     }
1553     
1554     if (VA.isRegLoc()) {
1555       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1556     } else {
1557       if (!IsTailCall) {
1558         assert(VA.isMemLoc());
1559         if (StackPtr.Val == 0)
1560           StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1561         
1562         MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1563                                                Arg));
1564       } else if (IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)) {
1565         TailCallClobberedVRegs.push_back(std::make_pair(i,Arg));
1566       }
1567     }
1568   }
1569   
1570   if (!MemOpChains.empty())
1571     Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1572                         &MemOpChains[0], MemOpChains.size());
1573
1574   // Build a sequence of copy-to-reg nodes chained together with token chain
1575   // and flag operands which copy the outgoing args into registers.
1576   SDOperand InFlag;
1577   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1578     Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1579                              InFlag);
1580     InFlag = Chain.getValue(1);
1581   }
1582
1583   // ELF / PIC requires GOT in the EBX register before function calls via PLT
1584   // GOT pointer.  
1585   if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1586     Chain = DAG.getCopyToReg(Chain, X86::EBX,
1587                              DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1588                              InFlag);
1589     InFlag = Chain.getValue(1);
1590   }
1591   // If we are tail calling and generating PIC/GOT style code load the address
1592   // of the callee into ecx. The value in ecx is used as target of the tail
1593   // jump. This is done to circumvent the ebx/callee-saved problem for tail
1594   // calls on PIC/GOT architectures. Normally we would just put the address of
1595   // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1596   // restored (since ebx is callee saved) before jumping to the target@PLT.
1597   if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
1598     // Note: The actual moving to ecx is done further down.
1599     GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1600     if (G &&  !G->getGlobal()->hasHiddenVisibility() &&
1601         !G->getGlobal()->hasProtectedVisibility())
1602       Callee =  LowerGlobalAddress(Callee, DAG);
1603     else if (isa<ExternalSymbolSDNode>(Callee))
1604       Callee = LowerExternalSymbol(Callee,DAG);
1605   }
1606
1607   if (Is64Bit && isVarArg) {
1608     // From AMD64 ABI document:
1609     // For calls that may call functions that use varargs or stdargs
1610     // (prototype-less calls or calls to functions containing ellipsis (...) in
1611     // the declaration) %al is used as hidden argument to specify the number
1612     // of SSE registers used. The contents of %al do not need to match exactly
1613     // the number of registers, but must be an ubound on the number of SSE
1614     // registers used and is in the range 0 - 8 inclusive.
1615     
1616     // Count the number of XMM registers allocated.
1617     static const unsigned XMMArgRegs[] = {
1618       X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1619       X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1620     };
1621     unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1622     
1623     Chain = DAG.getCopyToReg(Chain, X86::AL,
1624                              DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1625     InFlag = Chain.getValue(1);
1626   }
1627
1628
1629   // For tail calls lower the arguments to the 'real' stack slot.
1630   if (IsTailCall) {
1631     SmallVector<SDOperand, 8> MemOpChains2;
1632     SDOperand FIN;
1633     int FI = 0;
1634     // Do not flag preceeding copytoreg stuff together with the following stuff.
1635     InFlag = SDOperand();
1636     
1637     Chain = CopyTailCallClobberedArgumentsToVRegs(Chain, TailCallClobberedVRegs,
1638                                                   DAG, MF, this);
1639  
1640     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1641       CCValAssign &VA = ArgLocs[i];
1642       if (!VA.isRegLoc()) {
1643         assert(VA.isMemLoc());
1644         SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1645         SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1646         unsigned Flags    = cast<ConstantSDNode>(FlagsOp)->getValue();
1647         // Create frame index.
1648         int32_t Offset = VA.getLocMemOffset()+FPDiff;
1649         uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1650         FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1651         FIN = DAG.getFrameIndex(FI, MVT::i32);
1652
1653         // Find virtual register for this argument.
1654         bool Found=false;
1655         for (unsigned idx=0, e= TailCallClobberedVRegs.size(); idx < e; idx++)
1656           if (TailCallClobberedVRegs[idx].first==i) {
1657             Arg = TailCallClobberedVRegs[idx].second;
1658             Found=true;
1659             break;
1660           }
1661         assert(IsPossiblyOverwrittenArgumentOfTailCall(Arg, MFI)==false || 
1662           (Found==true && "No corresponding Argument was found"));
1663         
1664         if (Flags & ISD::ParamFlags::ByVal) {
1665           // Copy relative to framepointer.
1666           MemOpChains2.push_back(CreateCopyOfByValArgument(Arg, FIN, Chain,
1667                                                            Flags, DAG));
1668         } else {
1669           // Store relative to framepointer.
1670           MemOpChains2.push_back(
1671             DAG.getStore(Chain, Arg, FIN,
1672                          PseudoSourceValue::getFixedStack(), FI));
1673         }            
1674       }
1675     }
1676
1677     if (!MemOpChains2.empty())
1678       Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1679                           &MemOpChains2[0], MemOpChains2.size());
1680
1681     // Store the return address to the appropriate stack slot.
1682     if (FPDiff) {
1683       // Calculate the new stack slot for the return address.
1684       int SlotSize = Is64Bit ? 8 : 4;
1685       int NewReturnAddrFI = 
1686         MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1687       MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1688       SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1689       Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx, 
1690                            PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1691     }
1692   }
1693
1694   // If the callee is a GlobalAddress node (quite common, every direct call is)
1695   // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1696   if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1697     // We should use extra load for direct calls to dllimported functions in
1698     // non-JIT mode.
1699     if ((IsTailCall || !Is64Bit ||
1700          getTargetMachine().getCodeModel() != CodeModel::Large)
1701         && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1702                                            getTargetMachine(), true))
1703       Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1704   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1705     if (IsTailCall || !Is64Bit ||
1706         getTargetMachine().getCodeModel() != CodeModel::Large)
1707       Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1708   } else if (IsTailCall) {
1709     unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1710
1711     Chain = DAG.getCopyToReg(Chain, 
1712                              DAG.getRegister(Opc, getPointerTy()), 
1713                              Callee,InFlag);
1714     Callee = DAG.getRegister(Opc, getPointerTy());
1715     // Add register as live out.
1716     DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
1717   }
1718  
1719   // Returns a chain & a flag for retval copy to use.
1720   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1721   SmallVector<SDOperand, 8> Ops;
1722
1723   if (IsTailCall) {
1724     Ops.push_back(Chain);
1725     Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1726     Ops.push_back(DAG.getIntPtrConstant(0));
1727     if (InFlag.Val)
1728       Ops.push_back(InFlag);
1729     Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1730     InFlag = Chain.getValue(1);
1731  
1732     // Returns a chain & a flag for retval copy to use.
1733     NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1734     Ops.clear();
1735   }
1736   
1737   Ops.push_back(Chain);
1738   Ops.push_back(Callee);
1739
1740   if (IsTailCall)
1741     Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
1742
1743   // Add an implicit use GOT pointer in EBX.
1744   if (!IsTailCall && !Is64Bit &&
1745       getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1746       Subtarget->isPICStyleGOT())
1747     Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1748
1749   // Add argument registers to the end of the list so that they are known live
1750   // into the call.
1751   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1752     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1753                                   RegsToPass[i].second.getValueType()));
1754   
1755   if (InFlag.Val)
1756     Ops.push_back(InFlag);
1757
1758   if (IsTailCall) {
1759     assert(InFlag.Val && 
1760            "Flag must be set. Depend on flag being set in LowerRET");
1761     Chain = DAG.getNode(X86ISD::TAILCALL,
1762                         Op.Val->getVTList(), &Ops[0], Ops.size());
1763       
1764     return SDOperand(Chain.Val, Op.ResNo);
1765   }
1766
1767   Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
1768   InFlag = Chain.getValue(1);
1769
1770   // Create the CALLSEQ_END node.
1771   unsigned NumBytesForCalleeToPush;
1772   if (IsCalleePop(Op))
1773     NumBytesForCalleeToPush = NumBytes;    // Callee pops everything
1774   else if (!Is64Bit && IsStructRet)
1775     // If this is is a call to a struct-return function, the callee
1776     // pops the hidden struct pointer, so we have to push it back.
1777     // This is common for Darwin/X86, Linux & Mingw32 targets.
1778     NumBytesForCalleeToPush = 4;
1779   else
1780     NumBytesForCalleeToPush = 0;  // Callee pops nothing.
1781   
1782   // Returns a flag for retval copy to use.
1783   Chain = DAG.getCALLSEQ_END(Chain,
1784                              DAG.getIntPtrConstant(NumBytes),
1785                              DAG.getIntPtrConstant(NumBytesForCalleeToPush),
1786                              InFlag);
1787   InFlag = Chain.getValue(1);
1788
1789   // Handle result values, copying them out of physregs into vregs that we
1790   // return.
1791   switch (SRetMethod) {
1792   default:
1793     return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1794   case X86::InGPR64:
1795     return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1796                                                    X86::RAX, X86::RDX,
1797                                                    MVT::i64, DAG), Op.ResNo);
1798   case X86::InSSE:
1799     return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1800                                                    X86::XMM0, X86::XMM1,
1801                                                    MVT::f64, DAG), Op.ResNo);
1802   case X86::InX87:
1803     return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1804                      Op.ResNo);
1805   }
1806 }
1807
1808
1809 //===----------------------------------------------------------------------===//
1810 //                Fast Calling Convention (tail call) implementation
1811 //===----------------------------------------------------------------------===//
1812
1813 //  Like std call, callee cleans arguments, convention except that ECX is
1814 //  reserved for storing the tail called function address. Only 2 registers are
1815 //  free for argument passing (inreg). Tail call optimization is performed
1816 //  provided:
1817 //                * tailcallopt is enabled
1818 //                * caller/callee are fastcc
1819 //  On X86_64 architecture with GOT-style position independent code only local
1820 //  (within module) calls are supported at the moment.
1821 //  To keep the stack aligned according to platform abi the function
1822 //  GetAlignedArgumentStackSize ensures that argument delta is always multiples
1823 //  of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
1824 //  If a tail called function callee has more arguments than the caller the
1825 //  caller needs to make sure that there is room to move the RETADDR to. This is
1826 //  achieved by reserving an area the size of the argument delta right after the
1827 //  original REtADDR, but before the saved framepointer or the spilled registers
1828 //  e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1829 //  stack layout:
1830 //    arg1
1831 //    arg2
1832 //    RETADDR
1833 //    [ new RETADDR 
1834 //      move area ]
1835 //    (possible EBP)
1836 //    ESI
1837 //    EDI
1838 //    local1 ..
1839
1840 /// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1841 /// for a 16 byte align requirement.
1842 unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize, 
1843                                                         SelectionDAG& DAG) {
1844   if (PerformTailCallOpt) {
1845     MachineFunction &MF = DAG.getMachineFunction();
1846     const TargetMachine &TM = MF.getTarget();
1847     const TargetFrameInfo &TFI = *TM.getFrameInfo();
1848     unsigned StackAlignment = TFI.getStackAlignment();
1849     uint64_t AlignMask = StackAlignment - 1; 
1850     int64_t Offset = StackSize;
1851     unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1852     if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1853       // Number smaller than 12 so just add the difference.
1854       Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1855     } else {
1856       // Mask out lower bits, add stackalignment once plus the 12 bytes.
1857       Offset = ((~AlignMask) & Offset) + StackAlignment + 
1858         (StackAlignment-SlotSize);
1859     }
1860     StackSize = Offset;
1861   }
1862   return StackSize;
1863 }
1864
1865 /// IsEligibleForTailCallElimination - Check to see whether the next instruction
1866 /// following the call is a return. A function is eligible if caller/callee
1867 /// calling conventions match, currently only fastcc supports tail calls, and
1868 /// the function CALL is immediatly followed by a RET.
1869 bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1870                                                       SDOperand Ret,
1871                                                       SelectionDAG& DAG) const {
1872   if (!PerformTailCallOpt)
1873     return false;
1874
1875   // Check whether CALL node immediatly preceeds the RET node and whether the
1876   // return uses the result of the node or is a void return.
1877   unsigned NumOps = Ret.getNumOperands();
1878   if ((NumOps == 1 && 
1879        (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1880         Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
1881       (NumOps > 1 &&
1882        Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1883        Ret.getOperand(1) == SDOperand(Call.Val,0))) {
1884     MachineFunction &MF = DAG.getMachineFunction();
1885     unsigned CallerCC = MF.getFunction()->getCallingConv();
1886     unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1887     if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1888       SDOperand Callee = Call.getOperand(4);
1889       // On x86/32Bit PIC/GOT  tail calls are supported.
1890       if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1891           !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
1892         return true;
1893
1894       // Can only do local tail calls (in same module, hidden or protected) on
1895       // x86_64 PIC/GOT at the moment.
1896       if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1897         return G->getGlobal()->hasHiddenVisibility()
1898             || G->getGlobal()->hasProtectedVisibility();
1899     }
1900   }
1901
1902   return false;
1903 }
1904
1905 //===----------------------------------------------------------------------===//
1906 //                           Other Lowering Hooks
1907 //===----------------------------------------------------------------------===//
1908
1909
1910 SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1911   MachineFunction &MF = DAG.getMachineFunction();
1912   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1913   int ReturnAddrIndex = FuncInfo->getRAIndex();
1914
1915   if (ReturnAddrIndex == 0) {
1916     // Set up a frame object for the return address.
1917     if (Subtarget->is64Bit())
1918       ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1919     else
1920       ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1921
1922     FuncInfo->setRAIndex(ReturnAddrIndex);
1923   }
1924
1925   return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
1926 }
1927
1928
1929
1930 /// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1931 /// specific condition code. It returns a false if it cannot do a direct
1932 /// translation. X86CC is the translated CondCode.  LHS/RHS are modified as
1933 /// needed.
1934 static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1935                            unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1936                            SelectionDAG &DAG) {
1937   X86CC = X86::COND_INVALID;
1938   if (!isFP) {
1939     if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1940       if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1941         // X > -1   -> X == 0, jump !sign.
1942         RHS = DAG.getConstant(0, RHS.getValueType());
1943         X86CC = X86::COND_NS;
1944         return true;
1945       } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1946         // X < 0   -> X == 0, jump on sign.
1947         X86CC = X86::COND_S;
1948         return true;
1949       } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1950         // X < 1   -> X <= 0
1951         RHS = DAG.getConstant(0, RHS.getValueType());
1952         X86CC = X86::COND_LE;
1953         return true;
1954       }
1955     }
1956
1957     switch (SetCCOpcode) {
1958     default: break;
1959     case ISD::SETEQ:  X86CC = X86::COND_E;  break;
1960     case ISD::SETGT:  X86CC = X86::COND_G;  break;
1961     case ISD::SETGE:  X86CC = X86::COND_GE; break;
1962     case ISD::SETLT:  X86CC = X86::COND_L;  break;
1963     case ISD::SETLE:  X86CC = X86::COND_LE; break;
1964     case ISD::SETNE:  X86CC = X86::COND_NE; break;
1965     case ISD::SETULT: X86CC = X86::COND_B;  break;
1966     case ISD::SETUGT: X86CC = X86::COND_A;  break;
1967     case ISD::SETULE: X86CC = X86::COND_BE; break;
1968     case ISD::SETUGE: X86CC = X86::COND_AE; break;
1969     }
1970   } else {
1971     // On a floating point condition, the flags are set as follows:
1972     // ZF  PF  CF   op
1973     //  0 | 0 | 0 | X > Y
1974     //  0 | 0 | 1 | X < Y
1975     //  1 | 0 | 0 | X == Y
1976     //  1 | 1 | 1 | unordered
1977     bool Flip = false;
1978     switch (SetCCOpcode) {
1979     default: break;
1980     case ISD::SETUEQ:
1981     case ISD::SETEQ: X86CC = X86::COND_E;  break;
1982     case ISD::SETOLT: Flip = true; // Fallthrough
1983     case ISD::SETOGT:
1984     case ISD::SETGT: X86CC = X86::COND_A;  break;
1985     case ISD::SETOLE: Flip = true; // Fallthrough
1986     case ISD::SETOGE:
1987     case ISD::SETGE: X86CC = X86::COND_AE; break;
1988     case ISD::SETUGT: Flip = true; // Fallthrough
1989     case ISD::SETULT:
1990     case ISD::SETLT: X86CC = X86::COND_B;  break;
1991     case ISD::SETUGE: Flip = true; // Fallthrough
1992     case ISD::SETULE:
1993     case ISD::SETLE: X86CC = X86::COND_BE; break;
1994     case ISD::SETONE:
1995     case ISD::SETNE: X86CC = X86::COND_NE; break;
1996     case ISD::SETUO: X86CC = X86::COND_P;  break;
1997     case ISD::SETO:  X86CC = X86::COND_NP; break;
1998     }
1999     if (Flip)
2000       std::swap(LHS, RHS);
2001   }
2002
2003   return X86CC != X86::COND_INVALID;
2004 }
2005
2006 /// hasFPCMov - is there a floating point cmov for the specific X86 condition
2007 /// code. Current x86 isa includes the following FP cmov instructions:
2008 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
2009 static bool hasFPCMov(unsigned X86CC) {
2010   switch (X86CC) {
2011   default:
2012     return false;
2013   case X86::COND_B:
2014   case X86::COND_BE:
2015   case X86::COND_E:
2016   case X86::COND_P:
2017   case X86::COND_A:
2018   case X86::COND_AE:
2019   case X86::COND_NE:
2020   case X86::COND_NP:
2021     return true;
2022   }
2023 }
2024
2025 /// isUndefOrInRange - Op is either an undef node or a ConstantSDNode.  Return
2026 /// true if Op is undef or if its value falls within the specified range (L, H].
2027 static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2028   if (Op.getOpcode() == ISD::UNDEF)
2029     return true;
2030
2031   unsigned Val = cast<ConstantSDNode>(Op)->getValue();
2032   return (Val >= Low && Val < Hi);
2033 }
2034
2035 /// isUndefOrEqual - Op is either an undef node or a ConstantSDNode.  Return
2036 /// true if Op is undef or if its value equal to the specified value.
2037 static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2038   if (Op.getOpcode() == ISD::UNDEF)
2039     return true;
2040   return cast<ConstantSDNode>(Op)->getValue() == Val;
2041 }
2042
2043 /// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2044 /// specifies a shuffle of elements that is suitable for input to PSHUFD.
2045 bool X86::isPSHUFDMask(SDNode *N) {
2046   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2047
2048   if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
2049     return false;
2050
2051   // Check if the value doesn't reference the second vector.
2052   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
2053     SDOperand Arg = N->getOperand(i);
2054     if (Arg.getOpcode() == ISD::UNDEF) continue;
2055     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2056     if (cast<ConstantSDNode>(Arg)->getValue() >= e)
2057       return false;
2058   }
2059
2060   return true;
2061 }
2062
2063 /// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
2064 /// specifies a shuffle of elements that is suitable for input to PSHUFHW.
2065 bool X86::isPSHUFHWMask(SDNode *N) {
2066   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2067
2068   if (N->getNumOperands() != 8)
2069     return false;
2070
2071   // Lower quadword copied in order.
2072   for (unsigned i = 0; i != 4; ++i) {
2073     SDOperand Arg = N->getOperand(i);
2074     if (Arg.getOpcode() == ISD::UNDEF) continue;
2075     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2076     if (cast<ConstantSDNode>(Arg)->getValue() != i)
2077       return false;
2078   }
2079
2080   // Upper quadword shuffled.
2081   for (unsigned i = 4; i != 8; ++i) {
2082     SDOperand Arg = N->getOperand(i);
2083     if (Arg.getOpcode() == ISD::UNDEF) continue;
2084     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2085     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2086     if (Val < 4 || Val > 7)
2087       return false;
2088   }
2089
2090   return true;
2091 }
2092
2093 /// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
2094 /// specifies a shuffle of elements that is suitable for input to PSHUFLW.
2095 bool X86::isPSHUFLWMask(SDNode *N) {
2096   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2097
2098   if (N->getNumOperands() != 8)
2099     return false;
2100
2101   // Upper quadword copied in order.
2102   for (unsigned i = 4; i != 8; ++i)
2103     if (!isUndefOrEqual(N->getOperand(i), i))
2104       return false;
2105
2106   // Lower quadword shuffled.
2107   for (unsigned i = 0; i != 4; ++i)
2108     if (!isUndefOrInRange(N->getOperand(i), 0, 4))
2109       return false;
2110
2111   return true;
2112 }
2113
2114 /// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2115 /// specifies a shuffle of elements that is suitable for input to SHUFP*.
2116 static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
2117   if (NumElems != 2 && NumElems != 4) return false;
2118
2119   unsigned Half = NumElems / 2;
2120   for (unsigned i = 0; i < Half; ++i)
2121     if (!isUndefOrInRange(Elems[i], 0, NumElems))
2122       return false;
2123   for (unsigned i = Half; i < NumElems; ++i)
2124     if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
2125       return false;
2126
2127   return true;
2128 }
2129
2130 bool X86::isSHUFPMask(SDNode *N) {
2131   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2132   return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
2133 }
2134
2135 /// isCommutedSHUFP - Returns true if the shuffle mask is exactly
2136 /// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2137 /// half elements to come from vector 1 (which would equal the dest.) and
2138 /// the upper half to come from vector 2.
2139 static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2140   if (NumOps != 2 && NumOps != 4) return false;
2141
2142   unsigned Half = NumOps / 2;
2143   for (unsigned i = 0; i < Half; ++i)
2144     if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
2145       return false;
2146   for (unsigned i = Half; i < NumOps; ++i)
2147     if (!isUndefOrInRange(Ops[i], 0, NumOps))
2148       return false;
2149   return true;
2150 }
2151
2152 static bool isCommutedSHUFP(SDNode *N) {
2153   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2154   return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
2155 }
2156
2157 /// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2158 /// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2159 bool X86::isMOVHLPSMask(SDNode *N) {
2160   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2161
2162   if (N->getNumOperands() != 4)
2163     return false;
2164
2165   // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
2166   return isUndefOrEqual(N->getOperand(0), 6) &&
2167          isUndefOrEqual(N->getOperand(1), 7) &&
2168          isUndefOrEqual(N->getOperand(2), 2) &&
2169          isUndefOrEqual(N->getOperand(3), 3);
2170 }
2171
2172 /// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2173 /// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2174 /// <2, 3, 2, 3>
2175 bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2176   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178   if (N->getNumOperands() != 4)
2179     return false;
2180
2181   // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2182   return isUndefOrEqual(N->getOperand(0), 2) &&
2183          isUndefOrEqual(N->getOperand(1), 3) &&
2184          isUndefOrEqual(N->getOperand(2), 2) &&
2185          isUndefOrEqual(N->getOperand(3), 3);
2186 }
2187
2188 /// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2189 /// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2190 bool X86::isMOVLPMask(SDNode *N) {
2191   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2192
2193   unsigned NumElems = N->getNumOperands();
2194   if (NumElems != 2 && NumElems != 4)
2195     return false;
2196
2197   for (unsigned i = 0; i < NumElems/2; ++i)
2198     if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2199       return false;
2200
2201   for (unsigned i = NumElems/2; i < NumElems; ++i)
2202     if (!isUndefOrEqual(N->getOperand(i), i))
2203       return false;
2204
2205   return true;
2206 }
2207
2208 /// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
2209 /// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2210 /// and MOVLHPS.
2211 bool X86::isMOVHPMask(SDNode *N) {
2212   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2213
2214   unsigned NumElems = N->getNumOperands();
2215   if (NumElems != 2 && NumElems != 4)
2216     return false;
2217
2218   for (unsigned i = 0; i < NumElems/2; ++i)
2219     if (!isUndefOrEqual(N->getOperand(i), i))
2220       return false;
2221
2222   for (unsigned i = 0; i < NumElems/2; ++i) {
2223     SDOperand Arg = N->getOperand(i + NumElems/2);
2224     if (!isUndefOrEqual(Arg, i + NumElems))
2225       return false;
2226   }
2227
2228   return true;
2229 }
2230
2231 /// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2232 /// specifies a shuffle of elements that is suitable for input to UNPCKL.
2233 bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2234                          bool V2IsSplat = false) {
2235   if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2236     return false;
2237
2238   for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2239     SDOperand BitI  = Elts[i];
2240     SDOperand BitI1 = Elts[i+1];
2241     if (!isUndefOrEqual(BitI, j))
2242       return false;
2243     if (V2IsSplat) {
2244       if (isUndefOrEqual(BitI1, NumElts))
2245         return false;
2246     } else {
2247       if (!isUndefOrEqual(BitI1, j + NumElts))
2248         return false;
2249     }
2250   }
2251
2252   return true;
2253 }
2254
2255 bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2256   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2257   return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2258 }
2259
2260 /// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2261 /// specifies a shuffle of elements that is suitable for input to UNPCKH.
2262 bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2263                          bool V2IsSplat = false) {
2264   if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
2265     return false;
2266
2267   for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2268     SDOperand BitI  = Elts[i];
2269     SDOperand BitI1 = Elts[i+1];
2270     if (!isUndefOrEqual(BitI, j + NumElts/2))
2271       return false;
2272     if (V2IsSplat) {
2273       if (isUndefOrEqual(BitI1, NumElts))
2274         return false;
2275     } else {
2276       if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
2277         return false;
2278     }
2279   }
2280
2281   return true;
2282 }
2283
2284 bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2285   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2286   return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
2287 }
2288
2289 /// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2290 /// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2291 /// <0, 0, 1, 1>
2292 bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2293   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2294
2295   unsigned NumElems = N->getNumOperands();
2296   if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2297     return false;
2298
2299   for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2300     SDOperand BitI  = N->getOperand(i);
2301     SDOperand BitI1 = N->getOperand(i+1);
2302
2303     if (!isUndefOrEqual(BitI, j))
2304       return false;
2305     if (!isUndefOrEqual(BitI1, j))
2306       return false;
2307   }
2308
2309   return true;
2310 }
2311
2312 /// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2313 /// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2314 /// <2, 2, 3, 3>
2315 bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2316   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2317
2318   unsigned NumElems = N->getNumOperands();
2319   if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2320     return false;
2321
2322   for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2323     SDOperand BitI  = N->getOperand(i);
2324     SDOperand BitI1 = N->getOperand(i + 1);
2325
2326     if (!isUndefOrEqual(BitI, j))
2327       return false;
2328     if (!isUndefOrEqual(BitI1, j))
2329       return false;
2330   }
2331
2332   return true;
2333 }
2334
2335 /// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2336 /// specifies a shuffle of elements that is suitable for input to MOVSS,
2337 /// MOVSD, and MOVD, i.e. setting the lowest element.
2338 static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
2339   if (NumElts != 2 && NumElts != 4)
2340     return false;
2341
2342   if (!isUndefOrEqual(Elts[0], NumElts))
2343     return false;
2344
2345   for (unsigned i = 1; i < NumElts; ++i) {
2346     if (!isUndefOrEqual(Elts[i], i))
2347       return false;
2348   }
2349
2350   return true;
2351 }
2352
2353 bool X86::isMOVLMask(SDNode *N) {
2354   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2355   return ::isMOVLMask(N->op_begin(), N->getNumOperands());
2356 }
2357
2358 /// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2359 /// of what x86 movss want. X86 movs requires the lowest  element to be lowest
2360 /// element of vector 2 and the other elements to come from vector 1 in order.
2361 static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2362                            bool V2IsSplat = false,
2363                            bool V2IsUndef = false) {
2364   if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
2365     return false;
2366
2367   if (!isUndefOrEqual(Ops[0], 0))
2368     return false;
2369
2370   for (unsigned i = 1; i < NumOps; ++i) {
2371     SDOperand Arg = Ops[i];
2372     if (!(isUndefOrEqual(Arg, i+NumOps) ||
2373           (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2374           (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
2375       return false;
2376   }
2377
2378   return true;
2379 }
2380
2381 static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2382                            bool V2IsUndef = false) {
2383   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2384   return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2385                         V2IsSplat, V2IsUndef);
2386 }
2387
2388 /// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2389 /// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2390 bool X86::isMOVSHDUPMask(SDNode *N) {
2391   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2392
2393   if (N->getNumOperands() != 4)
2394     return false;
2395
2396   // Expect 1, 1, 3, 3
2397   for (unsigned i = 0; i < 2; ++i) {
2398     SDOperand Arg = N->getOperand(i);
2399     if (Arg.getOpcode() == ISD::UNDEF) continue;
2400     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2401     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2402     if (Val != 1) return false;
2403   }
2404
2405   bool HasHi = false;
2406   for (unsigned i = 2; i < 4; ++i) {
2407     SDOperand Arg = N->getOperand(i);
2408     if (Arg.getOpcode() == ISD::UNDEF) continue;
2409     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2410     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2411     if (Val != 3) return false;
2412     HasHi = true;
2413   }
2414
2415   // Don't use movshdup if it can be done with a shufps.
2416   return HasHi;
2417 }
2418
2419 /// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2420 /// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2421 bool X86::isMOVSLDUPMask(SDNode *N) {
2422   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2423
2424   if (N->getNumOperands() != 4)
2425     return false;
2426
2427   // Expect 0, 0, 2, 2
2428   for (unsigned i = 0; i < 2; ++i) {
2429     SDOperand Arg = N->getOperand(i);
2430     if (Arg.getOpcode() == ISD::UNDEF) continue;
2431     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2432     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2433     if (Val != 0) return false;
2434   }
2435
2436   bool HasHi = false;
2437   for (unsigned i = 2; i < 4; ++i) {
2438     SDOperand Arg = N->getOperand(i);
2439     if (Arg.getOpcode() == ISD::UNDEF) continue;
2440     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2441     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2442     if (Val != 2) return false;
2443     HasHi = true;
2444   }
2445
2446   // Don't use movshdup if it can be done with a shufps.
2447   return HasHi;
2448 }
2449
2450 /// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2451 /// specifies a identity operation on the LHS or RHS.
2452 static bool isIdentityMask(SDNode *N, bool RHS = false) {
2453   unsigned NumElems = N->getNumOperands();
2454   for (unsigned i = 0; i < NumElems; ++i)
2455     if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2456       return false;
2457   return true;
2458 }
2459
2460 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2461 /// a splat of a single element.
2462 static bool isSplatMask(SDNode *N) {
2463   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2464
2465   // This is a splat operation if each element of the permute is the same, and
2466   // if the value doesn't reference the second vector.
2467   unsigned NumElems = N->getNumOperands();
2468   SDOperand ElementBase;
2469   unsigned i = 0;
2470   for (; i != NumElems; ++i) {
2471     SDOperand Elt = N->getOperand(i);
2472     if (isa<ConstantSDNode>(Elt)) {
2473       ElementBase = Elt;
2474       break;
2475     }
2476   }
2477
2478   if (!ElementBase.Val)
2479     return false;
2480
2481   for (; i != NumElems; ++i) {
2482     SDOperand Arg = N->getOperand(i);
2483     if (Arg.getOpcode() == ISD::UNDEF) continue;
2484     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2485     if (Arg != ElementBase) return false;
2486   }
2487
2488   // Make sure it is a splat of the first vector operand.
2489   return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
2490 }
2491
2492 /// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2493 /// a splat of a single element and it's a 2 or 4 element mask.
2494 bool X86::isSplatMask(SDNode *N) {
2495   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2496
2497   // We can only splat 64-bit, and 32-bit quantities with a single instruction.
2498   if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2499     return false;
2500   return ::isSplatMask(N);
2501 }
2502
2503 /// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2504 /// specifies a splat of zero element.
2505 bool X86::isSplatLoMask(SDNode *N) {
2506   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2507
2508   for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
2509     if (!isUndefOrEqual(N->getOperand(i), 0))
2510       return false;
2511   return true;
2512 }
2513
2514 /// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2515 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2516 /// instructions.
2517 unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
2518   unsigned NumOperands = N->getNumOperands();
2519   unsigned Shift = (NumOperands == 4) ? 2 : 1;
2520   unsigned Mask = 0;
2521   for (unsigned i = 0; i < NumOperands; ++i) {
2522     unsigned Val = 0;
2523     SDOperand Arg = N->getOperand(NumOperands-i-1);
2524     if (Arg.getOpcode() != ISD::UNDEF)
2525       Val = cast<ConstantSDNode>(Arg)->getValue();
2526     if (Val >= NumOperands) Val -= NumOperands;
2527     Mask |= Val;
2528     if (i != NumOperands - 1)
2529       Mask <<= Shift;
2530   }
2531
2532   return Mask;
2533 }
2534
2535 /// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2536 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2537 /// instructions.
2538 unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2539   unsigned Mask = 0;
2540   // 8 nodes, but we only care about the last 4.
2541   for (unsigned i = 7; i >= 4; --i) {
2542     unsigned Val = 0;
2543     SDOperand Arg = N->getOperand(i);
2544     if (Arg.getOpcode() != ISD::UNDEF)
2545       Val = cast<ConstantSDNode>(Arg)->getValue();
2546     Mask |= (Val - 4);
2547     if (i != 4)
2548       Mask <<= 2;
2549   }
2550
2551   return Mask;
2552 }
2553
2554 /// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2555 /// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2556 /// instructions.
2557 unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2558   unsigned Mask = 0;
2559   // 8 nodes, but we only care about the first 4.
2560   for (int i = 3; i >= 0; --i) {
2561     unsigned Val = 0;
2562     SDOperand Arg = N->getOperand(i);
2563     if (Arg.getOpcode() != ISD::UNDEF)
2564       Val = cast<ConstantSDNode>(Arg)->getValue();
2565     Mask |= Val;
2566     if (i != 0)
2567       Mask <<= 2;
2568   }
2569
2570   return Mask;
2571 }
2572
2573 /// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2574 /// specifies a 8 element shuffle that can be broken into a pair of
2575 /// PSHUFHW and PSHUFLW.
2576 static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2577   assert(N->getOpcode() == ISD::BUILD_VECTOR);
2578
2579   if (N->getNumOperands() != 8)
2580     return false;
2581
2582   // Lower quadword shuffled.
2583   for (unsigned i = 0; i != 4; ++i) {
2584     SDOperand Arg = N->getOperand(i);
2585     if (Arg.getOpcode() == ISD::UNDEF) continue;
2586     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2587     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2588     if (Val >= 4)
2589       return false;
2590   }
2591
2592   // Upper quadword shuffled.
2593   for (unsigned i = 4; i != 8; ++i) {
2594     SDOperand Arg = N->getOperand(i);
2595     if (Arg.getOpcode() == ISD::UNDEF) continue;
2596     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2597     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2598     if (Val < 4 || Val > 7)
2599       return false;
2600   }
2601
2602   return true;
2603 }
2604
2605 /// CommuteVectorShuffle - Swap vector_shuffle operands as well as
2606 /// values in ther permute mask.
2607 static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2608                                       SDOperand &V2, SDOperand &Mask,
2609                                       SelectionDAG &DAG) {
2610   MVT::ValueType VT = Op.getValueType();
2611   MVT::ValueType MaskVT = Mask.getValueType();
2612   MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2613   unsigned NumElems = Mask.getNumOperands();
2614   SmallVector<SDOperand, 8> MaskVec;
2615
2616   for (unsigned i = 0; i != NumElems; ++i) {
2617     SDOperand Arg = Mask.getOperand(i);
2618     if (Arg.getOpcode() == ISD::UNDEF) {
2619       MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2620       continue;
2621     }
2622     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2623     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2624     if (Val < NumElems)
2625       MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2626     else
2627       MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2628   }
2629
2630   std::swap(V1, V2);
2631   Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2632   return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2633 }
2634
2635 /// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2636 /// the two vector operands have swapped position.
2637 static
2638 SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2639   MVT::ValueType MaskVT = Mask.getValueType();
2640   MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2641   unsigned NumElems = Mask.getNumOperands();
2642   SmallVector<SDOperand, 8> MaskVec;
2643   for (unsigned i = 0; i != NumElems; ++i) {
2644     SDOperand Arg = Mask.getOperand(i);
2645     if (Arg.getOpcode() == ISD::UNDEF) {
2646       MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2647       continue;
2648     }
2649     assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2650     unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2651     if (Val < NumElems)
2652       MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2653     else
2654       MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2655   }
2656   return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2657 }
2658
2659
2660 /// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2661 /// match movhlps. The lower half elements should come from upper half of
2662 /// V1 (and in order), and the upper half elements should come from the upper
2663 /// half of V2 (and in order).
2664 static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2665   unsigned NumElems = Mask->getNumOperands();
2666   if (NumElems != 4)
2667     return false;
2668   for (unsigned i = 0, e = 2; i != e; ++i)
2669     if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2670       return false;
2671   for (unsigned i = 2; i != 4; ++i)
2672     if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2673       return false;
2674   return true;
2675 }
2676
2677 /// isScalarLoadToVector - Returns true if the node is a scalar load that
2678 /// is promoted to a vector.
2679 static inline bool isScalarLoadToVector(SDNode *N) {
2680   if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2681     N = N->getOperand(0).Val;
2682     return ISD::isNON_EXTLoad(N);
2683   }
2684   return false;
2685 }
2686
2687 /// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2688 /// match movlp{s|d}. The lower half elements should come from lower half of
2689 /// V1 (and in order), and the upper half elements should come from the upper
2690 /// half of V2 (and in order). And since V1 will become the source of the
2691 /// MOVLP, it must be either a vector load or a scalar load to vector.
2692 static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
2693   if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
2694     return false;
2695   // Is V2 is a vector load, don't do this transformation. We will try to use
2696   // load folding shufps op.
2697   if (ISD::isNON_EXTLoad(V2))
2698     return false;
2699
2700   unsigned NumElems = Mask->getNumOperands();
2701   if (NumElems != 2 && NumElems != 4)
2702     return false;
2703   for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2704     if (!isUndefOrEqual(Mask->getOperand(i), i))
2705       return false;
2706   for (unsigned i = NumElems/2; i != NumElems; ++i)
2707     if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2708       return false;
2709   return true;
2710 }
2711
2712 /// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2713 /// all the same.
2714 static bool isSplatVector(SDNode *N) {
2715   if (N->getOpcode() != ISD::BUILD_VECTOR)
2716     return false;
2717
2718   SDOperand SplatValue = N->getOperand(0);
2719   for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2720     if (N->getOperand(i) != SplatValue)
2721       return false;
2722   return true;
2723 }
2724
2725 /// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2726 /// to an undef.
2727 static bool isUndefShuffle(SDNode *N) {
2728   if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2729     return false;
2730
2731   SDOperand V1 = N->getOperand(0);
2732   SDOperand V2 = N->getOperand(1);
2733   SDOperand Mask = N->getOperand(2);
2734   unsigned NumElems = Mask.getNumOperands();
2735   for (unsigned i = 0; i != NumElems; ++i) {
2736     SDOperand Arg = Mask.getOperand(i);
2737     if (Arg.getOpcode() != ISD::UNDEF) {
2738       unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2739       if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2740         return false;
2741       else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2742         return false;
2743     }
2744   }
2745   return true;
2746 }
2747
2748 /// isZeroNode - Returns true if Elt is a constant zero or a floating point
2749 /// constant +0.0.
2750 static inline bool isZeroNode(SDOperand Elt) {
2751   return ((isa<ConstantSDNode>(Elt) &&
2752            cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2753           (isa<ConstantFPSDNode>(Elt) &&
2754            cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2755 }
2756
2757 /// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2758 /// to an zero vector.
2759 static bool isZeroShuffle(SDNode *N) {
2760   if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2761     return false;
2762
2763   SDOperand V1 = N->getOperand(0);
2764   SDOperand V2 = N->getOperand(1);
2765   SDOperand Mask = N->getOperand(2);
2766   unsigned NumElems = Mask.getNumOperands();
2767   for (unsigned i = 0; i != NumElems; ++i) {
2768     SDOperand Arg = Mask.getOperand(i);
2769     if (Arg.getOpcode() == ISD::UNDEF)
2770       continue;
2771     
2772     unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2773     if (Idx < NumElems) {
2774       unsigned Opc = V1.Val->getOpcode();
2775       if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2776         continue;
2777       if (Opc != ISD::BUILD_VECTOR ||
2778           !isZeroNode(V1.Val->getOperand(Idx)))
2779         return false;
2780     } else if (Idx >= NumElems) {
2781       unsigned Opc = V2.Val->getOpcode();
2782       if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2783         continue;
2784       if (Opc != ISD::BUILD_VECTOR ||
2785           !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2786         return false;
2787     }
2788   }
2789   return true;
2790 }
2791
2792 /// getZeroVector - Returns a vector of specified type with all zero elements.
2793 ///
2794 static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2795   assert(MVT::isVector(VT) && "Expected a vector type");
2796   
2797   // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2798   // type.  This ensures they get CSE'd.
2799   SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2800   SDOperand Vec;
2801   if (MVT::getSizeInBits(VT) == 64)  // MMX
2802     Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2803   else                                              // SSE
2804     Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2805   return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2806 }
2807
2808 /// getOnesVector - Returns a vector of specified type with all bits set.
2809 ///
2810 static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2811   assert(MVT::isVector(VT) && "Expected a vector type");
2812   
2813   // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2814   // type.  This ensures they get CSE'd.
2815   SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2816   SDOperand Vec;
2817   if (MVT::getSizeInBits(VT) == 64)  // MMX
2818     Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2819   else                                              // SSE
2820     Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2821   return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2822 }
2823
2824
2825 /// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2826 /// that point to V2 points to its first element.
2827 static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2828   assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2829
2830   bool Changed = false;
2831   SmallVector<SDOperand, 8> MaskVec;
2832   unsigned NumElems = Mask.getNumOperands();
2833   for (unsigned i = 0; i != NumElems; ++i) {
2834     SDOperand Arg = Mask.getOperand(i);
2835     if (Arg.getOpcode() != ISD::UNDEF) {
2836       unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2837       if (Val > NumElems) {
2838         Arg = DAG.getConstant(NumElems, Arg.getValueType());
2839         Changed = true;
2840       }
2841     }
2842     MaskVec.push_back(Arg);
2843   }
2844
2845   if (Changed)
2846     Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2847                        &MaskVec[0], MaskVec.size());
2848   return Mask;
2849 }
2850
2851 /// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2852 /// operation of specified width.
2853 static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
2854   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2855   MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2856
2857   SmallVector<SDOperand, 8> MaskVec;
2858   MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2859   for (unsigned i = 1; i != NumElems; ++i)
2860     MaskVec.push_back(DAG.getConstant(i, BaseVT));
2861   return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2862 }
2863
2864 /// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2865 /// of specified width.
2866 static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2867   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2868   MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2869   SmallVector<SDOperand, 8> MaskVec;
2870   for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2871     MaskVec.push_back(DAG.getConstant(i,            BaseVT));
2872     MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2873   }
2874   return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2875 }
2876
2877 /// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2878 /// of specified width.
2879 static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2880   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2881   MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
2882   unsigned Half = NumElems/2;
2883   SmallVector<SDOperand, 8> MaskVec;
2884   for (unsigned i = 0; i != Half; ++i) {
2885     MaskVec.push_back(DAG.getConstant(i + Half,            BaseVT));
2886     MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2887   }
2888   return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2889 }
2890
2891 /// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2892 ///
2893 static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2894   SDOperand V1 = Op.getOperand(0);
2895   SDOperand Mask = Op.getOperand(2);
2896   MVT::ValueType VT = Op.getValueType();
2897   unsigned NumElems = Mask.getNumOperands();
2898   Mask = getUnpacklMask(NumElems, DAG);
2899   while (NumElems != 4) {
2900     V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2901     NumElems >>= 1;
2902   }
2903   V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2904
2905   Mask = getZeroVector(MVT::v4i32, DAG);
2906   SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
2907                                   DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
2908   return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2909 }
2910
2911 /// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2912 /// vector of zero or undef vector.  This produces a shuffle where the low
2913 /// element of V2 is swizzled into the zero/undef vector, landing at element
2914 /// Idx.  This produces a shuffle mask like 4,1,2,3 (idx=0) or  0,1,2,4 (idx=3).
2915 static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
2916                                              unsigned NumElems, unsigned Idx,
2917                                              bool isZero, SelectionDAG &DAG) {
2918   SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
2919   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920   MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
2921   SmallVector<SDOperand, 16> MaskVec;
2922   for (unsigned i = 0; i != NumElems; ++i)
2923     if (i == Idx)  // If this is the insertion idx, put the low elt of V2 here.
2924       MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2925     else
2926       MaskVec.push_back(DAG.getConstant(i, EVT));
2927   SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2928                                &MaskVec[0], MaskVec.size());
2929   return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2930 }
2931
2932 /// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2933 ///
2934 static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2935                                        unsigned NumNonZero, unsigned NumZero,
2936                                        SelectionDAG &DAG, TargetLowering &TLI) {
2937   if (NumNonZero > 8)
2938     return SDOperand();
2939
2940   SDOperand V(0, 0);
2941   bool First = true;
2942   for (unsigned i = 0; i < 16; ++i) {
2943     bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2944     if (ThisIsNonZero && First) {
2945       if (NumZero)
2946         V = getZeroVector(MVT::v8i16, DAG);
2947       else
2948         V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2949       First = false;
2950     }
2951
2952     if ((i & 1) != 0) {
2953       SDOperand ThisElt(0, 0), LastElt(0, 0);
2954       bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2955       if (LastIsNonZero) {
2956         LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2957       }
2958       if (ThisIsNonZero) {
2959         ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2960         ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2961                               ThisElt, DAG.getConstant(8, MVT::i8));
2962         if (LastIsNonZero)
2963           ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2964       } else
2965         ThisElt = LastElt;
2966
2967       if (ThisElt.Val)
2968         V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2969                         DAG.getIntPtrConstant(i/2));
2970     }
2971   }
2972
2973   return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2974 }
2975
2976 /// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
2977 ///
2978 static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2979                                        unsigned NumNonZero, unsigned NumZero,
2980                                        SelectionDAG &DAG, TargetLowering &TLI) {
2981   if (NumNonZero > 4)
2982     return SDOperand();
2983
2984   SDOperand V(0, 0);
2985   bool First = true;
2986   for (unsigned i = 0; i < 8; ++i) {
2987     bool isNonZero = (NonZeros & (1 << i)) != 0;
2988     if (isNonZero) {
2989       if (First) {
2990         if (NumZero)
2991           V = getZeroVector(MVT::v8i16, DAG);
2992         else
2993           V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2994         First = false;
2995       }
2996       V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2997                       DAG.getIntPtrConstant(i));
2998     }
2999   }
3000
3001   return V;
3002 }
3003
3004 SDOperand
3005 X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3006   // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3007   if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3008     // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3009     // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3010     // eliminated on x86-32 hosts.
3011     if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3012       return Op;
3013
3014     if (ISD::isBuildVectorAllOnes(Op.Val))
3015       return getOnesVector(Op.getValueType(), DAG);
3016     return getZeroVector(Op.getValueType(), DAG);
3017   }
3018
3019   MVT::ValueType VT = Op.getValueType();
3020   MVT::ValueType EVT = MVT::getVectorElementType(VT);
3021   unsigned EVTBits = MVT::getSizeInBits(EVT);
3022
3023   unsigned NumElems = Op.getNumOperands();
3024   unsigned NumZero  = 0;
3025   unsigned NumNonZero = 0;
3026   unsigned NonZeros = 0;
3027   bool IsAllConstants = true;
3028   SmallSet<SDOperand, 8> Values;
3029   for (unsigned i = 0; i < NumElems; ++i) {
3030     SDOperand Elt = Op.getOperand(i);
3031     if (Elt.getOpcode() == ISD::UNDEF)
3032       continue;
3033     Values.insert(Elt);
3034     if (Elt.getOpcode() != ISD::Constant &&
3035         Elt.getOpcode() != ISD::ConstantFP)
3036       IsAllConstants = false;
3037     if (isZeroNode(Elt))
3038       NumZero++;
3039     else {
3040       NonZeros |= (1 << i);
3041       NumNonZero++;
3042     }
3043   }
3044
3045   if (NumNonZero == 0) {
3046     // All undef vector. Return an UNDEF.  All zero vectors were handled above.
3047     return DAG.getNode(ISD::UNDEF, VT);
3048   }
3049
3050   // Splat is obviously ok. Let legalizer expand it to a shuffle.
3051   if (Values.size() == 1)
3052     return SDOperand();
3053
3054   // Special case for single non-zero element.
3055   if (NumNonZero == 1 && NumElems <= 4) {
3056     unsigned Idx = CountTrailingZeros_32(NonZeros);
3057     SDOperand Item = Op.getOperand(Idx);
3058     if (Idx == 0) {
3059       Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3060       // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3061       return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
3062                                          NumZero > 0, DAG);
3063     }
3064     
3065     if (IsAllConstants) // Otherwise, it's better to do a constpool load.
3066       return SDOperand();
3067
3068     if (EVTBits == 32) {
3069       Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3070       
3071       // Turn it into a shuffle of zero and zero-extended scalar to vector.
3072       Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
3073                                          DAG);
3074       MVT::ValueType MaskVT  = MVT::getIntVectorWithNumElements(NumElems);
3075       MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3076       SmallVector<SDOperand, 8> MaskVec;
3077       for (unsigned i = 0; i < NumElems; i++)
3078         MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
3079       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3080                                    &MaskVec[0], MaskVec.size());
3081       return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3082                          DAG.getNode(ISD::UNDEF, VT), Mask);
3083     }
3084   }
3085
3086   // A vector full of immediates; various special cases are already
3087   // handled, so this is best done with a single constant-pool load.
3088   if (IsAllConstants)
3089     return SDOperand();
3090
3091   // Let legalizer expand 2-wide build_vectors.
3092   if (EVTBits == 64)
3093     return SDOperand();
3094
3095   // If element VT is < 32 bits, convert it to inserts into a zero vector.
3096   if (EVTBits == 8 && NumElems == 16) {
3097     SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3098                                         *this);
3099     if (V.Val) return V;
3100   }
3101
3102   if (EVTBits == 16 && NumElems == 8) {
3103     SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3104                                         *this);
3105     if (V.Val) return V;
3106   }
3107
3108   // If element VT is == 32 bits, turn it into a number of shuffles.
3109   SmallVector<SDOperand, 8> V;
3110   V.resize(NumElems);
3111   if (NumElems == 4 && NumZero > 0) {
3112     for (unsigned i = 0; i < 4; ++i) {
3113       bool isZero = !(NonZeros & (1 << i));
3114       if (isZero)
3115         V[i] = getZeroVector(VT, DAG);
3116       else
3117         V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3118     }
3119
3120     for (unsigned i = 0; i < 2; ++i) {
3121       switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3122         default: break;
3123         case 0:
3124           V[i] = V[i*2];  // Must be a zero vector.
3125           break;
3126         case 1:
3127           V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3128                              getMOVLMask(NumElems, DAG));
3129           break;
3130         case 2:
3131           V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3132                              getMOVLMask(NumElems, DAG));
3133           break;
3134         case 3:
3135           V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3136                              getUnpacklMask(NumElems, DAG));
3137           break;
3138       }
3139     }
3140
3141     // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
3142     // clears the upper bits.
3143     // FIXME: we can do the same for v4f32 case when we know both parts of
3144     // the lower half come from scalar_to_vector (loadf32). We should do
3145     // that in post legalizer dag combiner with target specific hooks.
3146     if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
3147       return V[0];
3148     MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3149     MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
3150     SmallVector<SDOperand, 8> MaskVec;
3151     bool Reverse = (NonZeros & 0x3) == 2;
3152     for (unsigned i = 0; i < 2; ++i)
3153       if (Reverse)
3154         MaskVec.push_back(DAG.getConstant(1-i, EVT));
3155       else
3156         MaskVec.push_back(DAG.getConstant(i, EVT));
3157     Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3158     for (unsigned i = 0; i < 2; ++i)
3159       if (Reverse)
3160         MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3161       else
3162         MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
3163     SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3164                                      &MaskVec[0], MaskVec.size());
3165     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3166   }
3167
3168   if (Values.size() > 2) {
3169     // Expand into a number of unpckl*.
3170     // e.g. for v4f32
3171     //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3172     //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3173     //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
3174     SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3175     for (unsigned i = 0; i < NumElems; ++i)
3176       V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3177     NumElems >>= 1;
3178     while (NumElems != 0) {
3179       for (unsigned i = 0; i < NumElems; ++i)
3180         V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3181                            UnpckMask);
3182       NumElems >>= 1;
3183     }
3184     return V[0];
3185   }
3186
3187   return SDOperand();
3188 }
3189
3190 static
3191 SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3192                                    SDOperand PermMask, SelectionDAG &DAG,
3193                                    TargetLowering &TLI) {
3194   SDOperand NewV;
3195   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3196   MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3197   MVT::ValueType PtrVT = TLI.getPointerTy();
3198   SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3199                                      PermMask.Val->op_end());
3200
3201   // First record which half of which vector the low elements come from.
3202   SmallVector<unsigned, 4> LowQuad(4);
3203   for (unsigned i = 0; i < 4; ++i) {
3204     SDOperand Elt = MaskElts[i];
3205     if (Elt.getOpcode() == ISD::UNDEF)
3206       continue;
3207     unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3208     int QuadIdx = EltIdx / 4;
3209     ++LowQuad[QuadIdx];
3210   }
3211   int BestLowQuad = -1;
3212   unsigned MaxQuad = 1;
3213   for (unsigned i = 0; i < 4; ++i) {
3214     if (LowQuad[i] > MaxQuad) {
3215       BestLowQuad = i;
3216       MaxQuad = LowQuad[i];
3217     }
3218   }
3219
3220   // Record which half of which vector the high elements come from.
3221   SmallVector<unsigned, 4> HighQuad(4);
3222   for (unsigned i = 4; i < 8; ++i) {
3223     SDOperand Elt = MaskElts[i];
3224     if (Elt.getOpcode() == ISD::UNDEF)
3225       continue;
3226     unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3227     int QuadIdx = EltIdx / 4;
3228     ++HighQuad[QuadIdx];
3229   }
3230   int BestHighQuad = -1;
3231   MaxQuad = 1;
3232   for (unsigned i = 0; i < 4; ++i) {
3233     if (HighQuad[i] > MaxQuad) {
3234       BestHighQuad = i;
3235       MaxQuad = HighQuad[i];
3236     }
3237   }
3238
3239   // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3240   if (BestLowQuad != -1 || BestHighQuad != -1) {
3241     // First sort the 4 chunks in order using shufpd.
3242     SmallVector<SDOperand, 8> MaskVec;
3243     if (BestLowQuad != -1)
3244       MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3245     else
3246       MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3247     if (BestHighQuad != -1)
3248       MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3249     else
3250       MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3251     SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3252     NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3253                        DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3254                        DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3255     NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3256
3257     // Now sort high and low parts separately.
3258     BitVector InOrder(8);
3259     if (BestLowQuad != -1) {
3260       // Sort lower half in order using PSHUFLW.
3261       MaskVec.clear();
3262       bool AnyOutOrder = false;
3263       for (unsigned i = 0; i != 4; ++i) {
3264         SDOperand Elt = MaskElts[i];
3265         if (Elt.getOpcode() == ISD::UNDEF) {
3266           MaskVec.push_back(Elt);
3267           InOrder.set(i);
3268         } else {
3269           unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3270           if (EltIdx != i)
3271             AnyOutOrder = true;
3272           MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3273           // If this element is in the right place after this shuffle, then
3274           // remember it.
3275           if ((int)(EltIdx / 4) == BestLowQuad)
3276             InOrder.set(i);
3277         }
3278       }
3279       if (AnyOutOrder) {
3280         for (unsigned i = 4; i != 8; ++i)
3281           MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3282         SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3283         NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3284       }
3285     }
3286
3287     if (BestHighQuad != -1) {
3288       // Sort high half in order using PSHUFHW if possible.
3289       MaskVec.clear();
3290       for (unsigned i = 0; i != 4; ++i)
3291         MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3292       bool AnyOutOrder = false;
3293       for (unsigned i = 4; i != 8; ++i) {
3294         SDOperand Elt = MaskElts[i];
3295         if (Elt.getOpcode() == ISD::UNDEF) {
3296           MaskVec.push_back(Elt);
3297           InOrder.set(i);
3298         } else {
3299           unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3300           if (EltIdx != i)
3301             AnyOutOrder = true;
3302           MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3303           // If this element is in the right place after this shuffle, then
3304           // remember it.
3305           if ((int)(EltIdx / 4) == BestHighQuad)
3306             InOrder.set(i);
3307         }
3308       }
3309       if (AnyOutOrder) {
3310         SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3311         NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3312       }
3313     }
3314
3315     // The other elements are put in the right place using pextrw and pinsrw.
3316     for (unsigned i = 0; i != 8; ++i) {
3317       if (InOrder[i])
3318         continue;
3319       SDOperand Elt = MaskElts[i];
3320       unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3321       if (EltIdx == i)
3322         continue;
3323       SDOperand ExtOp = (EltIdx < 8)
3324         ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3325                       DAG.getConstant(EltIdx, PtrVT))
3326         : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3327                       DAG.getConstant(EltIdx - 8, PtrVT));
3328       NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3329                          DAG.getConstant(i, PtrVT));
3330     }
3331     return NewV;
3332   }
3333
3334   // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3335   ///as few as possible.
3336   // First, let's find out how many elements are already in the right order.
3337   unsigned V1InOrder = 0;
3338   unsigned V1FromV1 = 0;
3339   unsigned V2InOrder = 0;
3340   unsigned V2FromV2 = 0;
3341   SmallVector<SDOperand, 8> V1Elts;
3342   SmallVector<SDOperand, 8> V2Elts;
3343   for (unsigned i = 0; i < 8; ++i) {
3344     SDOperand Elt = MaskElts[i];
3345     if (Elt.getOpcode() == ISD::UNDEF) {
3346       V1Elts.push_back(Elt);
3347       V2Elts.push_back(Elt);
3348       ++V1InOrder;
3349       ++V2InOrder;
3350       continue;
3351     }
3352     unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3353     if (EltIdx == i) {
3354       V1Elts.push_back(Elt);
3355       V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3356       ++V1InOrder;
3357     } else if (EltIdx == i+8) {
3358       V1Elts.push_back(Elt);
3359       V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3360       ++V2InOrder;
3361     } else if (EltIdx < 8) {
3362       V1Elts.push_back(Elt);
3363       ++V1FromV1;
3364     } else {
3365       V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3366       ++V2FromV2;
3367     }
3368   }
3369
3370   if (V2InOrder > V1InOrder) {
3371     PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3372     std::swap(V1, V2);
3373     std::swap(V1Elts, V2Elts);
3374     std::swap(V1FromV1, V2FromV2);
3375   }
3376
3377   if ((V1FromV1 + V1InOrder) != 8) {
3378     // Some elements are from V2.
3379     if (V1FromV1) {
3380       // If there are elements that are from V1 but out of place,
3381       // then first sort them in place
3382       SmallVector<SDOperand, 8> MaskVec;
3383       for (unsigned i = 0; i < 8; ++i) {
3384         SDOperand Elt = V1Elts[i];
3385         if (Elt.getOpcode() == ISD::UNDEF) {
3386           MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3387           continue;
3388         }
3389         unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3390         if (EltIdx >= 8)
3391           MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3392         else
3393           MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3394       }
3395       SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3396       V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
3397     }
3398
3399     NewV = V1;
3400     for (unsigned i = 0; i < 8; ++i) {
3401       SDOperand Elt = V1Elts[i];
3402       if (Elt.getOpcode() == ISD::UNDEF)
3403         continue;
3404       unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3405       if (EltIdx < 8)
3406         continue;
3407       SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3408                                     DAG.getConstant(EltIdx - 8, PtrVT));
3409       NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3410                          DAG.getConstant(i, PtrVT));
3411     }
3412     return NewV;
3413   } else {
3414     // All elements are from V1.
3415     NewV = V1;
3416     for (unsigned i = 0; i < 8; ++i) {
3417       SDOperand Elt = V1Elts[i];
3418       if (Elt.getOpcode() == ISD::UNDEF)
3419         continue;
3420       unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3421       SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3422                                     DAG.getConstant(EltIdx, PtrVT));
3423       NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3424                          DAG.getConstant(i, PtrVT));
3425     }
3426     return NewV;
3427   }
3428 }
3429
3430 /// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3431 /// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3432 /// done when every pair / quad of shuffle mask elements point to elements in
3433 /// the right sequence. e.g.
3434 /// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3435 static
3436 SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3437                                 MVT::ValueType VT,
3438                                 SDOperand PermMask, SelectionDAG &DAG,
3439                                 TargetLowering &TLI) {
3440   unsigned NumElems = PermMask.getNumOperands();
3441   unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3442   MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3443   MVT::ValueType NewVT = MaskVT;
3444   switch (VT) {
3445   case MVT::v4f32: NewVT = MVT::v2f64; break;
3446   case MVT::v4i32: NewVT = MVT::v2i64; break;
3447   case MVT::v8i16: NewVT = MVT::v4i32; break;
3448   case MVT::v16i8: NewVT = MVT::v4i32; break;
3449   default: assert(false && "Unexpected!");
3450   }
3451
3452   if (NewWidth == 2) {
3453     if (MVT::isInteger(VT))
3454       NewVT = MVT::v2i64;
3455     else
3456       NewVT = MVT::v2f64;
3457   }
3458   unsigned Scale = NumElems / NewWidth;
3459   SmallVector<SDOperand, 8> MaskVec;
3460   for (unsigned i = 0; i < NumElems; i += Scale) {
3461     unsigned StartIdx = ~0U;
3462     for (unsigned j = 0; j < Scale; ++j) {
3463       SDOperand Elt = PermMask.getOperand(i+j);
3464       if (Elt.getOpcode() == ISD::UNDEF)
3465         continue;
3466       unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3467       if (StartIdx == ~0U)
3468         StartIdx = EltIdx - (EltIdx % Scale);
3469       if (EltIdx != StartIdx + j)
3470         return SDOperand();
3471     }
3472     if (StartIdx == ~0U)
3473       MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3474     else
3475       MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
3476   }
3477
3478   V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3479   V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3480   return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3481                      DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3482                                  &MaskVec[0], MaskVec.size()));
3483 }
3484
3485 SDOperand
3486 X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3487   SDOperand V1 = Op.getOperand(0);
3488   SDOperand V2 = Op.getOperand(1);
3489   SDOperand PermMask = Op.getOperand(2);
3490   MVT::ValueType VT = Op.getValueType();
3491   unsigned NumElems = PermMask.getNumOperands();
3492   bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3493   bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
3494   bool V1IsSplat = false;
3495   bool V2IsSplat = false;
3496
3497   if (isUndefShuffle(Op.Val))
3498     return DAG.getNode(ISD::UNDEF, VT);
3499
3500   if (isZeroShuffle(Op.Val))
3501     return getZeroVector(VT, DAG);
3502
3503   if (isIdentityMask(PermMask.Val))
3504     return V1;
3505   else if (isIdentityMask(PermMask.Val, true))
3506     return V2;
3507
3508   if (isSplatMask(PermMask.Val)) {
3509     if (NumElems <= 4) return Op;
3510     // Promote it to a v4i32 splat.
3511     return PromoteSplat(Op, DAG);
3512   }
3513
3514   // If the shuffle can be profitably rewritten as a narrower shuffle, then
3515   // do it!
3516   if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3517     SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3518     if (NewOp.Val)
3519       return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3520   } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3521     // FIXME: Figure out a cleaner way to do this.
3522     // Try to make use of movq to zero out the top part.
3523     if (ISD::isBuildVectorAllZeros(V2.Val)) {
3524       SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3525       if (NewOp.Val) {
3526         SDOperand NewV1 = NewOp.getOperand(0);
3527         SDOperand NewV2 = NewOp.getOperand(1);
3528         SDOperand NewMask = NewOp.getOperand(2);
3529         if (isCommutedMOVL(NewMask.Val, true, false)) {
3530           NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3531           NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3532                               NewV1, NewV2, getMOVLMask(2, DAG));
3533           return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3534         }
3535       }
3536     } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3537       SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3538       if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3539         return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3540     }
3541   }
3542
3543   if (X86::isMOVLMask(PermMask.Val))
3544     return (V1IsUndef) ? V2 : Op;
3545
3546   if (X86::isMOVSHDUPMask(PermMask.Val) ||
3547       X86::isMOVSLDUPMask(PermMask.Val) ||
3548       X86::isMOVHLPSMask(PermMask.Val) ||
3549       X86::isMOVHPMask(PermMask.Val) ||
3550       X86::isMOVLPMask(PermMask.Val))
3551     return Op;
3552
3553   if (ShouldXformToMOVHLPS(PermMask.Val) ||
3554       ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
3555     return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3556
3557   bool Commuted = false;
3558   // FIXME: This should also accept a bitcast of a splat?  Be careful, not
3559   // 1,1,1,1 -> v8i16 though.
3560   V1IsSplat = isSplatVector(V1.Val);
3561   V2IsSplat = isSplatVector(V2.Val);
3562   
3563   // Canonicalize the splat or undef, if present, to be on the RHS.
3564   if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
3565     Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3566     std::swap(V1IsSplat, V2IsSplat);
3567     std::swap(V1IsUndef, V2IsUndef);
3568     Commuted = true;
3569   }
3570
3571   // FIXME: Figure out a cleaner way to do this.
3572   if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3573     if (V2IsUndef) return V1;
3574     Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3575     if (V2IsSplat) {
3576       // V2 is a splat, so the mask may be malformed. That is, it may point
3577       // to any V2 element. The instruction selectior won't like this. Get
3578       // a corrected mask and commute to form a proper MOVS{S|D}.
3579       SDOperand NewMask = getMOVLMask(NumElems, DAG);
3580       if (NewMask.Val != PermMask.Val)
3581         Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3582     }
3583     return Op;
3584   }
3585
3586   if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3587       X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3588       X86::isUNPCKLMask(PermMask.Val) ||
3589       X86::isUNPCKHMask(PermMask.Val))
3590     return Op;
3591
3592   if (V2IsSplat) {
3593     // Normalize mask so all entries that point to V2 points to its first
3594     // element then try to match unpck{h|l} again. If match, return a
3595     // new vector_shuffle with the corrected mask.
3596     SDOperand NewMask = NormalizeMask(PermMask, DAG);
3597     if (NewMask.Val != PermMask.Val) {
3598       if (X86::isUNPCKLMask(PermMask.Val, true)) {
3599         SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3600         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3601       } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3602         SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3603         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3604       }
3605     }
3606   }
3607
3608   // Normalize the node to match x86 shuffle ops if needed
3609   if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3610       Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3611
3612   if (Commuted) {
3613     // Commute is back and try unpck* again.
3614     Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3615     if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3616         X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
3617         X86::isUNPCKLMask(PermMask.Val) ||
3618         X86::isUNPCKHMask(PermMask.Val))
3619       return Op;
3620   }
3621
3622   // If VT is integer, try PSHUF* first, then SHUFP*.
3623   if (MVT::isInteger(VT)) {
3624     // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3625     // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3626     if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3627          X86::isPSHUFDMask(PermMask.Val)) ||
3628         X86::isPSHUFHWMask(PermMask.Val) ||
3629         X86::isPSHUFLWMask(PermMask.Val)) {
3630       if (V2.getOpcode() != ISD::UNDEF)
3631         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3632                            DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3633       return Op;
3634     }
3635
3636     if (X86::isSHUFPMask(PermMask.Val) &&
3637         MVT::getSizeInBits(VT) != 64)    // Don't do this for MMX.
3638       return Op;
3639   } else {
3640     // Floating point cases in the other order.
3641     if (X86::isSHUFPMask(PermMask.Val))
3642       return Op;
3643     if (X86::isPSHUFDMask(PermMask.Val) ||
3644         X86::isPSHUFHWMask(PermMask.Val) ||
3645         X86::isPSHUFLWMask(PermMask.Val)) {
3646       if (V2.getOpcode() != ISD::UNDEF)
3647         return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3648                            DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3649       return Op;
3650     }
3651   }
3652
3653   // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3654   if (VT == MVT::v8i16) {
3655     SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3656     if (NewOp.Val)
3657       return NewOp;
3658   }
3659
3660   // Handle all 4 wide cases with a number of shuffles.
3661   if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
3662     // Don't do this for MMX.
3663     MVT::ValueType MaskVT = PermMask.getValueType();
3664     MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
3665     SmallVector<std::pair<int, int>, 8> Locs;
3666     Locs.reserve(NumElems);
3667     SmallVector<SDOperand, 8> Mask1(NumElems,
3668                                     DAG.getNode(ISD::UNDEF, MaskEVT));
3669     SmallVector<SDOperand, 8> Mask2(NumElems,
3670                                     DAG.getNode(ISD::UNDEF, MaskEVT));
3671     unsigned NumHi = 0;
3672     unsigned NumLo = 0;
3673     // If no more than two elements come from either vector. This can be
3674     // implemented with two shuffles. First shuffle gather the elements.
3675     // The second shuffle, which takes the first shuffle as both of its
3676     // vector operands, put the elements into the right order.
3677     for (unsigned i = 0; i != NumElems; ++i) {
3678       SDOperand Elt = PermMask.getOperand(i);
3679       if (Elt.getOpcode() == ISD::UNDEF) {
3680         Locs[i] = std::make_pair(-1, -1);
3681       } else {
3682         unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3683         if (Val < NumElems) {
3684           Locs[i] = std::make_pair(0, NumLo);
3685           Mask1[NumLo] = Elt;
3686           NumLo++;
3687         } else {
3688           Locs[i] = std::make_pair(1, NumHi);
3689           if (2+NumHi < NumElems)
3690             Mask1[2+NumHi] = Elt;
3691           NumHi++;
3692         }
3693       }
3694     }
3695     if (NumLo <= 2 && NumHi <= 2) {
3696       V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3697                        DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3698                                    &Mask1[0], Mask1.size()));
3699       for (unsigned i = 0; i != NumElems; ++i) {
3700         if (Locs[i].first == -1)
3701           continue;
3702         else {
3703           unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3704           Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3705           Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3706         }
3707       }
3708
3709       return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3710                          DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3711                                      &Mask2[0], Mask2.size()));
3712     }
3713
3714     // Break it into (shuffle shuffle_hi, shuffle_lo).
3715     Locs.clear();
3716     SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3717     SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3718     SmallVector<SDOperand,8> *MaskPtr = &LoMask;
3719     unsigned MaskIdx = 0;
3720     unsigned LoIdx = 0;
3721     unsigned HiIdx = NumElems/2;
3722     for (unsigned i = 0; i != NumElems; ++i) {
3723       if (i == NumElems/2) {
3724         MaskPtr = &HiMask;
3725         MaskIdx = 1;
3726         LoIdx = 0;
3727         HiIdx = NumElems/2;
3728       }
3729       SDOperand Elt = PermMask.getOperand(i);
3730       if (Elt.getOpcode() == ISD::UNDEF) {
3731         Locs[i] = std::make_pair(-1, -1);
3732       } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3733         Locs[i] = std::make_pair(MaskIdx, LoIdx);
3734         (*MaskPtr)[LoIdx] = Elt;
3735         LoIdx++;
3736       } else {
3737         Locs[i] = std::make_pair(MaskIdx, HiIdx);
3738         (*MaskPtr)[HiIdx] = Elt;
3739         HiIdx++;
3740       }
3741     }
3742
3743     SDOperand LoShuffle =
3744       DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3745                   DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3746                               &LoMask[0], LoMask.size()));
3747     SDOperand HiShuffle =
3748       DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3749                   DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3750                               &HiMask[0], HiMask.size()));
3751     SmallVector<SDOperand, 8> MaskOps;
3752     for (unsigned i = 0; i != NumElems; ++i) {
3753       if (Locs[i].first == -1) {
3754         MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3755       } else {
3756         unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3757         MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3758       }
3759     }
3760     return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3761                        DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3762                                    &MaskOps[0], MaskOps.size()));
3763   }
3764
3765   return SDOperand();
3766 }
3767
3768 SDOperand
3769 X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3770                                                 SelectionDAG &DAG) {
3771   MVT::ValueType VT = Op.getValueType();
3772   if (MVT::getSizeInBits(VT) == 8) {
3773     SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3774                                     Op.getOperand(0), Op.getOperand(1));
3775     SDOperand Assert  = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3776                                     DAG.getValueType(VT));
3777     return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3778   } else if (MVT::getSizeInBits(VT) == 16) {
3779     SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3780                                     Op.getOperand(0), Op.getOperand(1));
3781     SDOperand Assert  = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3782                                     DAG.getValueType(VT));
3783     return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3784   }
3785   return SDOperand();
3786 }
3787
3788
3789 SDOperand
3790 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3791   if (!isa<ConstantSDNode>(Op.getOperand(1)))
3792     return SDOperand();
3793
3794   if (Subtarget->hasSSE41())
3795     return LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
3796
3797   MVT::ValueType VT = Op.getValueType();
3798   // TODO: handle v16i8.
3799   if (MVT::getSizeInBits(VT) == 16) {
3800     SDOperand Vec = Op.getOperand(0);
3801     unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3802     if (Idx == 0)
3803       return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3804                          DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3805                                  DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3806                                      Op.getOperand(1)));
3807     // Transform it so it match pextrw which produces a 32-bit result.
3808     MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3809     SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3810                                     Op.getOperand(0), Op.getOperand(1));
3811     SDOperand Assert  = DAG.getNode(ISD::AssertZext, EVT, Extract,
3812                                     DAG.getValueType(VT));
3813     return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3814   } else if (MVT::getSizeInBits(VT) == 32) {
3815     unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3816     if (Idx == 0)
3817       return Op;
3818     // SHUFPS the element to the lowest double word, then movss.
3819     MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3820     SmallVector<SDOperand, 8> IdxVec;
3821     IdxVec.
3822       push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3823     IdxVec.
3824       push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3825     IdxVec.
3826       push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3827     IdxVec.
3828       push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3829     SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3830                                  &IdxVec[0], IdxVec.size());
3831     SDOperand Vec = Op.getOperand(0);
3832     Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3833                       Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3834     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3835                        DAG.getIntPtrConstant(0));
3836   } else if (MVT::getSizeInBits(VT) == 64) {
3837     // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
3838     // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
3839     //        to match extract_elt for f64.
3840     unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3841     if (Idx == 0)
3842       return Op;
3843
3844     // UNPCKHPD the element to the lowest double word, then movsd.
3845     // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3846     // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3847     MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3848     SmallVector<SDOperand, 8> IdxVec;
3849     IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3850     IdxVec.
3851       push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3852     SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3853                                  &IdxVec[0], IdxVec.size());
3854     SDOperand Vec = Op.getOperand(0);
3855     Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3856                       Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3857     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
3858                        DAG.getIntPtrConstant(0));
3859   }
3860
3861   return SDOperand();
3862 }
3863
3864 SDOperand
3865 X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
3866   MVT::ValueType VT = Op.getValueType();
3867   MVT::ValueType EVT = MVT::getVectorElementType(VT);
3868
3869   SDOperand N0 = Op.getOperand(0);
3870   SDOperand N1 = Op.getOperand(1);
3871   SDOperand N2 = Op.getOperand(2);
3872
3873   if ((MVT::getSizeInBits(EVT) == 8) || (MVT::getSizeInBits(EVT) == 16)) {
3874     unsigned Opc = (MVT::getSizeInBits(EVT) == 8) ? X86ISD::PINSRB
3875                                                   : X86ISD::PINSRW;
3876     // Transform it so it match pinsr{b,w} which expects a GR32 as its second
3877     // argument.
3878     if (N1.getValueType() != MVT::i32)
3879       N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3880     if (N2.getValueType() != MVT::i32)
3881       N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3882     return DAG.getNode(Opc, VT, N0, N1, N2);
3883   } else if (EVT == MVT::f32) {
3884     // Bits [7:6] of the constant are the source select.  This will always be
3885     //  zero here.  The DAG Combiner may combine an extract_elt index into these
3886     //  bits.  For example (insert (extract, 3), 2) could be matched by putting
3887     //  the '3' into bits [7:6] of X86ISD::INSERTPS.
3888     // Bits [5:4] of the constant are the destination select.  This is the 
3889     //  value of the incoming immediate.
3890     // Bits [3:0] of the constant are the zero mask.  The DAG Combiner may 
3891     //   combine either bitwise AND or insert of float 0.0 to set these bits.
3892     N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
3893     return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
3894   }
3895   return SDOperand();
3896 }
3897
3898 SDOperand
3899 X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3900   MVT::ValueType VT = Op.getValueType();
3901   MVT::ValueType EVT = MVT::getVectorElementType(VT);
3902
3903   if (Subtarget->hasSSE41())
3904     return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
3905
3906   if (EVT == MVT::i8)
3907     return SDOperand();
3908
3909   SDOperand N0 = Op.getOperand(0);
3910   SDOperand N1 = Op.getOperand(1);
3911   SDOperand N2 = Op.getOperand(2);
3912
3913   if (MVT::getSizeInBits(EVT) == 16) {
3914     // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3915     // as its second argument.
3916     if (N1.getValueType() != MVT::i32)
3917       N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3918     if (N2.getValueType() != MVT::i32)
3919       N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
3920     return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3921   }
3922   return SDOperand();
3923 }
3924
3925 SDOperand
3926 X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3927   SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3928   MVT::ValueType VT = MVT::v2i32;
3929   switch (Op.getValueType()) {
3930   default: break;
3931   case MVT::v16i8:
3932   case MVT::v8i16:
3933     VT = MVT::v4i32;
3934     break;
3935   }
3936   return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
3937                      DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
3938 }
3939
3940 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
3941 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3942 // one of the above mentioned nodes. It has to be wrapped because otherwise
3943 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3944 // be used to form addressing mode. These wrapped nodes will be selected
3945 // into MOV32ri.
3946 SDOperand
3947 X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3948   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
3949   SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3950                                                getPointerTy(),
3951                                                CP->getAlignment());
3952   Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3953   // With PIC, the address is actually $g + Offset.
3954   if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3955       !Subtarget->isPICStyleRIPRel()) {
3956     Result = DAG.getNode(ISD::ADD, getPointerTy(),
3957                          DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3958                          Result);
3959   }
3960
3961   return Result;
3962 }
3963
3964 SDOperand
3965 X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3966   GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
3967   SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
3968   // If it's a debug information descriptor, don't mess with it.
3969   if (DAG.isVerifiedDebugInfoDesc(Op))
3970     return Result;
3971   Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3972   // With PIC, the address is actually $g + Offset.
3973   if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3974       !Subtarget->isPICStyleRIPRel()) {
3975     Result = DAG.getNode(ISD::ADD, getPointerTy(),
3976                          DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3977                          Result);
3978   }
3979   
3980   // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3981   // load the value at address GV, not the value of GV itself. This means that
3982   // the GlobalAddress must be in the base or index register of the address, not
3983   // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
3984   // The same applies for external symbols during PIC codegen
3985   if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3986     Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
3987                          PseudoSourceValue::getGOT(), 0);
3988
3989   return Result;
3990 }
3991
3992 // Lower ISD::GlobalTLSAddress using the "general dynamic" model
3993 static SDOperand
3994 LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3995                               const MVT::ValueType PtrVT) {
3996   SDOperand InFlag;
3997   SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3998                                      DAG.getNode(X86ISD::GlobalBaseReg,
3999                                                  PtrVT), InFlag);
4000   InFlag = Chain.getValue(1);
4001
4002   // emit leal symbol@TLSGD(,%ebx,1), %eax
4003   SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4004   SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4005                                              GA->getValueType(0),
4006                                              GA->getOffset());
4007   SDOperand Ops[] = { Chain,  TGA, InFlag };
4008   SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4009   InFlag = Result.getValue(2);
4010   Chain = Result.getValue(1);
4011
4012   // call ___tls_get_addr. This function receives its argument in
4013   // the register EAX.
4014   Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4015   InFlag = Chain.getValue(1);
4016
4017   NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4018   SDOperand Ops1[] = { Chain,
4019                       DAG.getTargetExternalSymbol("___tls_get_addr",
4020                                                   PtrVT),
4021                       DAG.getRegister(X86::EAX, PtrVT),
4022                       DAG.getRegister(X86::EBX, PtrVT),
4023                       InFlag };
4024   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4025   InFlag = Chain.getValue(1);
4026
4027   return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4028 }
4029
4030 // Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4031 // "local exec" model.
4032 static SDOperand
4033 LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4034                          const MVT::ValueType PtrVT) {
4035   // Get the Thread Pointer
4036   SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4037   // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4038   // exec)
4039   SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4040                                              GA->getValueType(0),
4041                                              GA->getOffset());
4042   SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
4043
4044   if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
4045     Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
4046                          PseudoSourceValue::getGOT(), 0);
4047
4048   // The address of the thread local variable is the add of the thread
4049   // pointer with the offset of the variable.
4050   return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4051 }
4052
4053 SDOperand
4054 X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4055   // TODO: implement the "local dynamic" model
4056   // TODO: implement the "initial exec"model for pic executables
4057   assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
4058          "TLS not implemented for non-ELF and 64-bit targets");
4059   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4060   // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4061   // otherwise use the "Local Exec"TLS Model
4062   if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4063     return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
4064   else
4065     return LowerToTLSExecModel(GA, DAG, getPointerTy());
4066 }
4067
4068 SDOperand
4069 X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4070   const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4071   SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
4072   Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4073   // With PIC, the address is actually $g + Offset.
4074   if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4075       !Subtarget->isPICStyleRIPRel()) {
4076     Result = DAG.getNode(ISD::ADD, getPointerTy(),
4077                          DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4078                          Result);
4079   }
4080
4081   return Result;
4082 }
4083
4084 SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4085   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4086   SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4087   Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4088   // With PIC, the address is actually $g + Offset.
4089   if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4090       !Subtarget->isPICStyleRIPRel()) {
4091     Result = DAG.getNode(ISD::ADD, getPointerTy(),
4092                          DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4093                          Result);
4094   }
4095
4096   return Result;
4097 }
4098
4099 /// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4100 /// take a 2 x i32 value to shift plus a shift amount. 
4101 SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
4102   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
4103   MVT::ValueType VT = Op.getValueType();
4104   unsigned VTBits = MVT::getSizeInBits(VT);
4105   bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4106   SDOperand ShOpLo = Op.getOperand(0);
4107   SDOperand ShOpHi = Op.getOperand(1);
4108   SDOperand ShAmt  = Op.getOperand(2);
4109   SDOperand Tmp1 = isSRA ?
4110     DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4111     DAG.getConstant(0, VT);
4112
4113   SDOperand Tmp2, Tmp3;
4114   if (Op.getOpcode() == ISD::SHL_PARTS) {
4115     Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4116     Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
4117   } else {
4118     Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4119     Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
4120   }
4121
4122   const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4123   SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
4124                                   DAG.getConstant(VTBits, MVT::i8));
4125   SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
4126                                AndNode, DAG.getConstant(0, MVT::i8));
4127
4128   SDOperand Hi, Lo;
4129   SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4130   VTs = DAG.getNodeValueTypes(VT, MVT::Flag);
4131   SmallVector<SDOperand, 4> Ops;
4132   if (Op.getOpcode() == ISD::SHL_PARTS) {
4133     Ops.push_back(Tmp2);
4134     Ops.push_back(Tmp3);
4135     Ops.push_back(CC);
4136     Ops.push_back(Cond);
4137     Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4138
4139     Ops.clear();
4140     Ops.push_back(Tmp3);
4141     Ops.push_back(Tmp1);
4142     Ops.push_back(CC);
4143     Ops.push_back(Cond);
4144     Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4145   } else {
4146     Ops.push_back(Tmp2);
4147     Ops.push_back(Tmp3);
4148     Ops.push_back(CC);
4149     Ops.push_back(Cond);
4150     Lo = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4151
4152     Ops.clear();
4153     Ops.push_back(Tmp3);
4154     Ops.push_back(Tmp1);
4155     Ops.push_back(CC);
4156     Ops.push_back(Cond);
4157     Hi = DAG.getNode(X86ISD::CMOV, VT, &Ops[0], Ops.size());
4158   }
4159
4160   VTs = DAG.getNodeValueTypes(VT, VT);
4161   Ops.clear();
4162   Ops.push_back(Lo);
4163   Ops.push_back(Hi);
4164   return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
4165 }
4166
4167 SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
4168   MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
4169   assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
4170          "Unknown SINT_TO_FP to lower!");
4171   
4172   // These are really Legal; caller falls through into that case.
4173   if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4174     return SDOperand();
4175   if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 && 
4176       Subtarget->is64Bit())
4177     return SDOperand();
4178   
4179   unsigned Size = MVT::getSizeInBits(SrcVT)/8;
4180   MachineFunction &MF = DAG.getMachineFunction();
4181   int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4182   SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4183   SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
4184                                  StackSlot,
4185                                  PseudoSourceValue::getFixedStack(),
4186                                  SSFI);
4187
4188   // Build the FILD
4189   SDVTList Tys;
4190   bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
4191   if (useSSE)
4192     Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4193   else
4194     Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
4195   SmallVector<SDOperand, 8> Ops;
4196   Ops.push_back(Chain);
4197   Ops.push_back(StackSlot);
4198   Ops.push_back(DAG.getValueType(SrcVT));
4199   SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4200                                  Tys, &Ops[0], Ops.size());
4201
4202   if (useSSE) {
4203     Chain = Result.getValue(1);
4204     SDOperand InFlag = Result.getValue(2);
4205
4206     // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4207     // shouldn't be necessary except that RFP cannot be live across
4208     // multiple blocks. When stackifier is fixed, they can be uncoupled.
4209     MachineFunction &MF = DAG.getMachineFunction();
4210     int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
4211     SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4212     Tys = DAG.getVTList(MVT::Other);
4213     SmallVector<SDOperand, 8> Ops;
4214     Ops.push_back(Chain);
4215     Ops.push_back(Result);
4216     Ops.push_back(StackSlot);
4217     Ops.push_back(DAG.getValueType(Op.getValueType()));
4218     Ops.push_back(InFlag);
4219     Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
4220     Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
4221                          PseudoSourceValue::getFixedStack(), SSFI);
4222   }
4223
4224   return Result;
4225 }
4226
4227 std::pair<SDOperand,SDOperand> X86TargetLowering::
4228 FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
4229   assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4230          "Unknown FP_TO_SINT to lower!");
4231
4232   // These are really Legal.
4233   if (Op.getValueType() == MVT::i32 && 
4234       isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
4235     return std::make_pair(SDOperand(), SDOperand());
4236   if (Subtarget->is64Bit() &&
4237       Op.getValueType() == MVT::i64 &&
4238       Op.getOperand(0).getValueType() != MVT::f80)
4239     return std::make_pair(SDOperand(), SDOperand());
4240
4241   // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4242   // stack slot.
4243   MachineFunction &MF = DAG.getMachineFunction();
4244   unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4245   int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4246   SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4247   unsigned Opc;
4248   switch (Op.getValueType()) {
4249   default: assert(0 && "Invalid FP_TO_SINT to lower!");
4250   case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4251   case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4252   case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
4253   }
4254
4255   SDOperand Chain = DAG.getEntryNode();
4256   SDOperand Value = Op.getOperand(0);
4257   if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
4258     assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
4259     Chain = DAG.getStore(Chain, Value, StackSlot,
4260                          PseudoSourceValue::getFixedStack(), SSFI);
4261     SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
4262     SDOperand Ops[] = {
4263       Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4264     };
4265     Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
4266     Chain = Value.getValue(1);
4267     SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4268     StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4269   }
4270
4271   // Build the FP_TO_INT*_IN_MEM
4272   SDOperand Ops[] = { Chain, Value, StackSlot };
4273   SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
4274
4275   return std::make_pair(FIST, StackSlot);
4276 }
4277
4278 SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
4279   std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4280   SDOperand FIST = Vals.first, StackSlot = Vals.second;
4281   if (FIST.Val == 0) return SDOperand();
4282   
4283   // Load the result.
4284   return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4285 }
4286
4287 SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4288   std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4289   SDOperand FIST = Vals.first, StackSlot = Vals.second;
4290   if (FIST.Val == 0) return 0;
4291   
4292   // Return an i64 load from the stack slot.
4293   SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4294
4295   // Use a MERGE_VALUES node to drop the chain result value.
4296   return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4297 }  
4298
4299 SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4300   MVT::ValueType VT = Op.getValueType();
4301   MVT::ValueType EltVT = VT;
4302   if (MVT::isVector(VT))
4303     EltVT = MVT::getVectorElementType(VT);
4304   const Type *OpNTy =  MVT::getTypeForValueType(EltVT);
4305   std::vector<Constant*> CV;
4306   if (EltVT == MVT::f64) {
4307     Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
4308     CV.push_back(C);
4309     CV.push_back(C);
4310   } else {
4311     Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
4312     CV.push_back(C);
4313     CV.push_back(C);
4314     CV.push_back(C);
4315     CV.push_back(C);
4316   }
4317   Constant *C = ConstantVector::get(CV);
4318   SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4319   SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4320                                PseudoSourceValue::getConstantPool(), 0,
4321                                false, 16);
4322   return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4323 }
4324
4325 SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4326   MVT::ValueType VT = Op.getValueType();
4327   MVT::ValueType EltVT = VT;
4328   unsigned EltNum = 1;
4329   if (MVT::isVector(VT)) {
4330     EltVT = MVT::getVectorElementType(VT);
4331     EltNum = MVT::getVectorNumElements(VT);
4332   }
4333   const Type *OpNTy =  MVT::getTypeForValueType(EltVT);
4334   std::vector<Constant*> CV;
4335   if (EltVT == MVT::f64) {
4336     Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
4337     CV.push_back(C);
4338     CV.push_back(C);
4339   } else {
4340     Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
4341     CV.push_back(C);
4342     CV.push_back(C);
4343     CV.push_back(C);
4344     CV.push_back(C);
4345   }
4346   Constant *C = ConstantVector::get(CV);
4347   SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4348   SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4349                                PseudoSourceValue::getConstantPool(), 0,
4350                                false, 16);
4351   if (MVT::isVector(VT)) {
4352     return DAG.getNode(ISD::BIT_CONVERT, VT,
4353                        DAG.getNode(ISD::XOR, MVT::v2i64,
4354                     DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4355                     DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4356   } else {
4357     return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4358   }
4359 }
4360
4361 SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
4362   SDOperand Op0 = Op.getOperand(0);
4363   SDOperand Op1 = Op.getOperand(1);
4364   MVT::ValueType VT = Op.getValueType();
4365   MVT::ValueType SrcVT = Op1.getValueType();
4366   const Type *SrcTy =  MVT::getTypeForValueType(SrcVT);
4367
4368   // If second operand is smaller, extend it first.
4369   if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4370     Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4371     SrcVT = VT;
4372     SrcTy = MVT::getTypeForValueType(SrcVT);
4373   }
4374   // And if it is bigger, shrink it first.
4375   if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4376     Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
4377     SrcVT = VT;
4378     SrcTy = MVT::getTypeForValueType(SrcVT);
4379   }
4380
4381   // At this point the operands and the result should have the same
4382   // type, and that won't be f80 since that is not custom lowered.
4383
4384   // First get the sign bit of second operand.
4385   std::vector<Constant*> CV;
4386   if (SrcVT == MVT::f64) {
4387     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4388     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4389   } else {
4390     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4391     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4392     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4393     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4394   }
4395   Constant *C = ConstantVector::get(CV);
4396   SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4397   SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
4398                                 PseudoSourceValue::getConstantPool(), 0,
4399                                 false, 16);
4400   SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
4401
4402   // Shift sign bit right or left if the two operands have different types.
4403   if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4404     // Op0 is MVT::f32, Op1 is MVT::f64.
4405     SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4406     SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4407                           DAG.getConstant(32, MVT::i32));
4408     SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4409     SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
4410                           DAG.getIntPtrConstant(0));
4411   }
4412
4413   // Clear first operand sign bit.
4414   CV.clear();
4415   if (VT == MVT::f64) {
4416     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4417     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
4418   } else {
4419     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4420     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4421     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4422     CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4423   }
4424   C = ConstantVector::get(CV);
4425   CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4426   SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
4427                                 PseudoSourceValue::getConstantPool(), 0,
4428                                 false, 16);
4429   SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4430
4431   // Or the value with the sign bit.
4432   return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
4433 }
4434
4435 SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
4436   assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
4437   SDOperand Cond;
4438   SDOperand Op0 = Op.getOperand(0);
4439   SDOperand Op1 = Op.getOperand(1);
4440   SDOperand CC = Op.getOperand(2);
4441   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4442   bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4443   unsigned X86CC;
4444
4445   if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
4446                      Op0, Op1, DAG)) {
4447     Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4448     return DAG.getNode(X86ISD::SETCC, MVT::i8,
4449                        DAG.getConstant(X86CC, MVT::i8), Cond);
4450   }
4451
4452   assert(isFP && "Illegal integer SetCC!");
4453
4454   Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4455   switch (SetCCOpcode) {
4456   default: assert(false && "Illegal floating point SetCC!");
4457   case ISD::SETOEQ: {  // !PF & ZF
4458     SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4459                                  DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
4460     SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4461                                  DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4462     return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4463   }
4464   case ISD::SETUNE: {  // PF | !ZF
4465     SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4466                                  DAG.getConstant(X86::COND_P, MVT::i8), Cond);
4467     SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
4468                                  DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4469     return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4470   }
4471   }
4472 }
4473
4474
4475 SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
4476   bool addTest = true;
4477   SDOperand Cond  = Op.getOperand(0);
4478   SDOperand CC;
4479
4480   if (Cond.getOpcode() == ISD::SETCC)
4481     Cond = LowerSETCC(Cond, DAG);
4482
4483   // If condition flag is set by a X86ISD::CMP, then use it as the condition
4484   // setting operand in place of the X86ISD::SETCC.
4485   if (Cond.getOpcode() == X86ISD::SETCC) {
4486     CC = Cond.getOperand(0);
4487
4488     SDOperand Cmp = Cond.getOperand(1);
4489     unsigned Opc = Cmp.getOpcode();
4490     MVT::ValueType VT = Op.getValueType();
4491     
4492     bool IllegalFPCMov = false;
4493     if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
4494         !isScalarFPTypeInSSEReg(VT))  // FPStack?
4495       IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
4496     
4497     if ((Opc == X86ISD::CMP ||
4498          Opc == X86ISD::COMI ||
4499          Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
4500       Cond = Cmp;
4501       addTest = false;
4502     }
4503   }
4504
4505   if (addTest) {
4506     CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4507     Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4508   }
4509
4510   const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4511                                                     MVT::Flag);
4512   SmallVector<SDOperand, 4> Ops;
4513   // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4514   // condition is true.
4515   Ops.push_back(Op.getOperand(2));
4516   Ops.push_back(Op.getOperand(1));
4517   Ops.push_back(CC);
4518   Ops.push_back(Cond);
4519   return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
4520 }
4521
4522 SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
4523   bool addTest = true;
4524   SDOperand Chain = Op.getOperand(0);
4525   SDOperand Cond  = Op.getOperand(1);
4526   SDOperand Dest  = Op.getOperand(2);
4527   SDOperand CC;
4528
4529   if (Cond.getOpcode() == ISD::SETCC)
4530     Cond = LowerSETCC(Cond, DAG);
4531
4532   // If condition flag is set by a X86ISD::CMP, then use it as the condition
4533   // setting operand in place of the X86ISD::SETCC.
4534   if (Cond.getOpcode() == X86ISD::SETCC) {
4535     CC = Cond.getOperand(0);
4536
4537     SDOperand Cmp = Cond.getOperand(1);
4538     unsigned Opc = Cmp.getOpcode();
4539     if (Opc == X86ISD::CMP ||
4540         Opc == X86ISD::COMI ||
4541         Opc == X86ISD::UCOMI) {
4542       Cond = Cmp;
4543       addTest = false;
4544     }
4545   }
4546
4547   if (addTest) {
4548     CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4549     Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
4550   }
4551   return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
4552                      Chain, Op.getOperand(2), CC, Cond);
4553 }
4554
4555
4556 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4557 // Calls to _alloca is needed to probe the stack when allocating more than 4k
4558 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
4559 // that the guard pages used by the OS virtual memory manager are allocated in
4560 // correct sequence.
4561 SDOperand
4562 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4563                                            SelectionDAG &DAG) {
4564   assert(Subtarget->isTargetCygMing() &&
4565          "This should be used only on Cygwin/Mingw targets");
4566   
4567   // Get the inputs.
4568   SDOperand Chain = Op.getOperand(0);
4569   SDOperand Size  = Op.getOperand(1);
4570   // FIXME: Ensure alignment here
4571
4572   SDOperand Flag;
4573   
4574   MVT::ValueType IntPtr = getPointerTy();
4575   MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
4576
4577   Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4578   Flag = Chain.getValue(1);
4579
4580   SDVTList  NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4581   SDOperand Ops[] = { Chain,
4582                       DAG.getTargetExternalSymbol("_alloca", IntPtr),
4583                       DAG.getRegister(X86::EAX, IntPtr),
4584                       Flag };
4585   Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4586   Flag = Chain.getValue(1);
4587
4588   Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
4589   
4590   std::vector<MVT::ValueType> Tys;
4591   Tys.push_back(SPTy);
4592   Tys.push_back(MVT::Other);
4593   SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4594   return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
4595 }
4596
4597 SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4598   SDOperand InFlag(0, 0);
4599   SDOperand Chain = Op.getOperand(0);
4600   unsigned Align =
4601     (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4602   if (Align == 0) Align = 1;
4603
4604   ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4605   // If not DWORD aligned or size is more than the threshold, call memset.
4606   // The libc version is likely to be faster for these cases. It can use the
4607   // address value and run time information about the CPU.
4608   if ((Align & 3) != 0 ||
4609       (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
4610     MVT::ValueType IntPtr = getPointerTy();
4611     const Type *IntPtrTy = getTargetData()->getIntPtrType();
4612     TargetLowering::ArgListTy Args; 
4613     TargetLowering::ArgListEntry Entry;
4614     Entry.Node = Op.getOperand(1);
4615     Entry.Ty = IntPtrTy;
4616     Args.push_back(Entry);
4617     // Extend the unsigned i8 argument to be an int value for the call.
4618     Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4619     Entry.Ty = IntPtrTy;
4620     Args.push_back(Entry);
4621     Entry.Node = Op.getOperand(3);
4622     Args.push_back(Entry);
4623     std::pair<SDOperand,SDOperand> CallResult =
4624       LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4625                   false, DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4626     return CallResult.second;
4627   }
4628
4629   MVT::ValueType AVT;
4630   SDOperand Count;
4631   ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4632   unsigned BytesLeft = 0;
4633   bool TwoRepStos = false;
4634   if (ValC) {
4635     unsigned ValReg;
4636     uint64_t Val = ValC->getValue() & 255;
4637
4638     // If the value is a constant, then we can potentially use larger sets.
4639     switch (Align & 3) {
4640       case 2:   // WORD aligned
4641         AVT = MVT::i16;
4642         ValReg = X86::AX;
4643         Val = (Val << 8) | Val;
4644         break;
4645       case 0:  // DWORD aligned
4646         AVT = MVT::i32;
4647         ValReg = X86::EAX;
4648         Val = (Val << 8)  | Val;
4649         Val = (Val << 16) | Val;
4650         if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) {  // QWORD aligned
4651           AVT = MVT::i64;
4652           ValReg = X86::RAX;
4653           Val = (Val << 32) | Val;
4654         }
4655         break;
4656       default:  // Byte aligned
4657         AVT = MVT::i8;
4658         ValReg = X86::AL;
4659         Count = Op.getOperand(3);
4660         break;
4661     }
4662
4663     if (AVT > MVT::i8) {
4664       if (I) {
4665         unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4666         Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
4667         BytesLeft = I->getValue() % UBytes;
4668       } else {
4669         assert(AVT >= MVT::i32 &&
4670                "Do not use rep;stos if not at least DWORD aligned");
4671         Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4672                             Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4673         TwoRepStos = true;
4674       }
4675     }
4676
4677     Chain  = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4678                               InFlag);
4679     InFlag = Chain.getValue(1);
4680   } else {
4681     AVT = MVT::i8;
4682     Count  = Op.getOperand(3);
4683     Chain  = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4684     InFlag = Chain.getValue(1);
4685   }
4686
4687   Chain  = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4688                             Count, InFlag);
4689   InFlag = Chain.getValue(1);
4690   Chain  = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4691                             Op.getOperand(1), InFlag);
4692   InFlag = Chain.getValue(1);
4693
4694   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4695   SmallVector<SDOperand, 8> Ops;
4696   Ops.push_back(Chain);
4697   Ops.push_back(DAG.getValueType(AVT));
4698   Ops.push_back(InFlag);
4699   Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4700
4701   if (TwoRepStos) {
4702     InFlag = Chain.getValue(1);
4703     Count = Op.getOperand(3);
4704     MVT::ValueType CVT = Count.getValueType();
4705     SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
4706                                DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4707     Chain  = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4708                               Left, InFlag);
4709     InFlag = Chain.getValue(1);
4710     Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4711     Ops.clear();
4712     Ops.push_back(Chain);
4713     Ops.push_back(DAG.getValueType(MVT::i8));
4714     Ops.push_back(InFlag);
4715     Chain  = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
4716   } else if (BytesLeft) {
4717     // Issue stores for the last 1 - 7 bytes.
4718     SDOperand Value;
4719     unsigned Val = ValC->getValue() & 255;
4720     unsigned Offset = I->getValue() - BytesLeft;
4721     SDOperand DstAddr = Op.getOperand(1);
4722     MVT::ValueType AddrVT = DstAddr.getValueType();
4723     if (BytesLeft >= 4) {
4724       Val = (Val << 8)  | Val;
4725       Val = (Val << 16) | Val;
4726       Value = DAG.getConstant(Val, MVT::i32);
4727       Chain = DAG.getStore(Chain, Value,
4728                            DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4729                                        DAG.getConstant(Offset, AddrVT)),
4730                            NULL, 0);
4731       BytesLeft -= 4;
4732       Offset += 4;
4733     }
4734     if (BytesLeft >= 2) {
4735       Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
4736       Chain = DAG.getStore(Chain, Value,
4737                            DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4738                                        DAG.getConstant(Offset, AddrVT)),
4739                            NULL, 0);
4740       BytesLeft -= 2;
4741       Offset += 2;
4742     }
4743     if (BytesLeft == 1) {
4744       Value = DAG.getConstant(Val, MVT::i8);
4745       Chain = DAG.getStore(Chain, Value,
4746                            DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4747                                        DAG.getConstant(Offset, AddrVT)),
4748                            NULL, 0);
4749     }
4750   }
4751
4752   return Chain;
4753 }
4754
4755 SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4756                                                SDOperand Dest,
4757                                                SDOperand Source,
4758                                                unsigned Size,
4759                                                unsigned Align,
4760                                                SelectionDAG &DAG) {
4761   MVT::ValueType AVT;
4762   unsigned BytesLeft = 0;
4763   switch (Align & 3) {
4764     case 2:   // WORD aligned
4765       AVT = MVT::i16;
4766       break;
4767     case 0:  // DWORD aligned
4768       AVT = MVT::i32;
4769       if (Subtarget->is64Bit() && ((Align & 0xF) == 0))  // QWORD aligned
4770         AVT = MVT::i64;
4771       break;
4772     default:  // Byte aligned
4773       AVT = MVT::i8;
4774       break;
4775   }
4776
4777   unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4778   SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
4779   BytesLeft = Size % UBytes;
4780
4781   SDOperand InFlag(0, 0);
4782   Chain  = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4783                             Count, InFlag);
4784   InFlag = Chain.getValue(1);
4785   Chain  = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4786                             Dest, InFlag);
4787   InFlag = Chain.getValue(1);
4788   Chain  = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4789                             Source, InFlag);
4790   InFlag = Chain.getValue(1);
4791
4792   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4793   SmallVector<SDOperand, 8> Ops;
4794   Ops.push_back(Chain);
4795   Ops.push_back(DAG.getValueType(AVT));
4796   Ops.push_back(InFlag);
4797   Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
4798
4799   if (BytesLeft) {
4800     // Issue loads and stores for the last 1 - 7 bytes.
4801     unsigned Offset = Size - BytesLeft;
4802     SDOperand DstAddr = Dest;
4803     MVT::ValueType DstVT = DstAddr.getValueType();
4804     SDOperand SrcAddr = Source;
4805     MVT::ValueType SrcVT = SrcAddr.getValueType();
4806     SDOperand Value;
4807     if (BytesLeft >= 4) {
4808       Value = DAG.getLoad(MVT::i32, Chain,
4809                           DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4810                                       DAG.getConstant(Offset, SrcVT)),
4811                           NULL, 0);
4812       Chain = Value.getValue(1);
4813       Chain = DAG.getStore(Chain, Value,
4814                            DAG.getNode(ISD::ADD, DstVT, DstAddr,
4815                                        DAG.getConstant(Offset, DstVT)),
4816                            NULL, 0);
4817       BytesLeft -= 4;
4818       Offset += 4;
4819     }
4820     if (BytesLeft >= 2) {
4821       Value = DAG.getLoad(MVT::i16, Chain,
4822                           DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4823                                       DAG.getConstant(Offset, SrcVT)),
4824                           NULL, 0);
4825       Chain = Value.getValue(1);
4826       Chain = DAG.getStore(Chain, Value,
4827                            DAG.getNode(ISD::ADD, DstVT, DstAddr,
4828                                        DAG.getConstant(Offset, DstVT)),
4829                            NULL, 0);
4830       BytesLeft -= 2;
4831       Offset += 2;
4832     }
4833
4834     if (BytesLeft == 1) {
4835       Value = DAG.getLoad(MVT::i8, Chain,
4836                           DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4837                                       DAG.getConstant(Offset, SrcVT)),
4838                           NULL, 0);
4839       Chain = Value.getValue(1);
4840       Chain = DAG.getStore(Chain, Value,
4841                            DAG.getNode(ISD::ADD, DstVT, DstAddr,
4842                                        DAG.getConstant(Offset, DstVT)),
4843                            NULL, 0);
4844     }
4845   }
4846
4847   return Chain;
4848 }
4849
4850 /// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4851 SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
4852   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
4853   SDOperand TheChain = N->getOperand(0);
4854   SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
4855   if (Subtarget->is64Bit()) {
4856     SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4857     SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4858                                        MVT::i64, rax.getValue(2));
4859     SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
4860                                 DAG.getConstant(32, MVT::i8));
4861     SDOperand Ops[] = {
4862       DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
4863     };
4864     
4865     Tys = DAG.getVTList(MVT::i64, MVT::Other);
4866     return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4867   }
4868   
4869   SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4870   SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4871                                        MVT::i32, eax.getValue(2));
4872   // Use a buildpair to merge the two 32-bit values into a 64-bit one. 
4873   SDOperand Ops[] = { eax, edx };
4874   Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4875
4876   // Use a MERGE_VALUES to return the value and chain.
4877   Ops[1] = edx.getValue(1);
4878   Tys = DAG.getVTList(MVT::i64, MVT::Other);
4879   return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
4880 }
4881
4882 SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
4883   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
4884
4885   if (!Subtarget->is64Bit()) {
4886     // vastart just stores the address of the VarArgsFrameIndex slot into the
4887     // memory location argument.
4888     SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4889     return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
4890   }
4891
4892   // __va_list_tag:
4893   //   gp_offset         (0 - 6 * 8)
4894   //   fp_offset         (48 - 48 + 8 * 16)
4895   //   overflow_arg_area (point to parameters coming in memory).
4896   //   reg_save_area
4897   SmallVector<SDOperand, 8> MemOps;
4898   SDOperand FIN = Op.getOperand(1);
4899   // Store gp_offset
4900   SDOperand Store = DAG.getStore(Op.getOperand(0),
4901                                  DAG.getConstant(VarArgsGPOffset, MVT::i32),
4902                                  FIN, SV, 0);
4903   MemOps.push_back(Store);
4904
4905   // Store fp_offset
4906   FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4907   Store = DAG.getStore(Op.getOperand(0),
4908                        DAG.getConstant(VarArgsFPOffset, MVT::i32),
4909                        FIN, SV, 0);
4910   MemOps.push_back(Store);
4911
4912   // Store ptr to overflow_arg_area
4913   FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
4914   SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
4915   Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
4916   MemOps.push_back(Store);
4917
4918   // Store ptr to reg_save_area.
4919   FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
4920   SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
4921   Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
4922   MemOps.push_back(Store);
4923   return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
4924 }
4925
4926 SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4927   // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4928   SDOperand Chain = Op.getOperand(0);
4929   SDOperand DstPtr = Op.getOperand(1);
4930   SDOperand SrcPtr = Op.getOperand(2);
4931   const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
4932   const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
4933
4934   SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr, SrcSV, 0);
4935   Chain = SrcPtr.getValue(1);
4936   for (unsigned i = 0; i < 3; ++i) {
4937     SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr, SrcSV, 0);
4938     Chain = Val.getValue(1);
4939     Chain = DAG.getStore(Chain, Val, DstPtr, DstSV, 0);
4940     if (i == 2)
4941       break;
4942     SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr, 
4943                          DAG.getIntPtrConstant(8));
4944     DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr, 
4945                          DAG.getIntPtrConstant(8));
4946   }
4947   return Chain;
4948 }
4949
4950 SDOperand
4951 X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4952   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4953   switch (IntNo) {
4954   default: return SDOperand();    // Don't custom lower most intrinsics.
4955     // Comparison intrinsics.
4956   case Intrinsic::x86_sse_comieq_ss:
4957   case Intrinsic::x86_sse_comilt_ss:
4958   case Intrinsic::x86_sse_comile_ss:
4959   case Intrinsic::x86_sse_comigt_ss:
4960   case Intrinsic::x86_sse_comige_ss:
4961   case Intrinsic::x86_sse_comineq_ss:
4962   case Intrinsic::x86_sse_ucomieq_ss:
4963   case Intrinsic::x86_sse_ucomilt_ss:
4964   case Intrinsic::x86_sse_ucomile_ss:
4965   case Intrinsic::x86_sse_ucomigt_ss:
4966   case Intrinsic::x86_sse_ucomige_ss:
4967   case Intrinsic::x86_sse_ucomineq_ss:
4968   case Intrinsic::x86_sse2_comieq_sd:
4969   case Intrinsic::x86_sse2_comilt_sd:
4970   case Intrinsic::x86_sse2_comile_sd:
4971   case Intrinsic::x86_sse2_comigt_sd:
4972   case Intrinsic::x86_sse2_comige_sd:
4973   case Intrinsic::x86_sse2_comineq_sd:
4974   case Intrinsic::x86_sse2_ucomieq_sd:
4975   case Intrinsic::x86_sse2_ucomilt_sd:
4976   case Intrinsic::x86_sse2_ucomile_sd:
4977   case Intrinsic::x86_sse2_ucomigt_sd:
4978   case Intrinsic::x86_sse2_ucomige_sd:
4979   case Intrinsic::x86_sse2_ucomineq_sd: {
4980     unsigned Opc = 0;
4981     ISD::CondCode CC = ISD::SETCC_INVALID;
4982     switch (IntNo) {
4983     default: break;
4984     case Intrinsic::x86_sse_comieq_ss:
4985     case Intrinsic::x86_sse2_comieq_sd:
4986       Opc = X86ISD::COMI;
4987       CC = ISD::SETEQ;
4988       break;
4989     case Intrinsic::x86_sse_comilt_ss:
4990     case Intrinsic::x86_sse2_comilt_sd:
4991       Opc = X86ISD::COMI;
4992       CC = ISD::SETLT;
4993       break;
4994     case Intrinsic::x86_sse_comile_ss:
4995     case Intrinsic::x86_sse2_comile_sd:
4996       Opc = X86ISD::COMI;
4997       CC = ISD::SETLE;
4998       break;
4999     case Intrinsic::x86_sse_comigt_ss:
5000     case Intrinsic::x86_sse2_comigt_sd:
5001       Opc = X86ISD::COMI;
5002       CC = ISD::SETGT;
5003       break;
5004     case Intrinsic::x86_sse_comige_ss:
5005     case Intrinsic::x86_sse2_comige_sd:
5006       Opc = X86ISD::COMI;
5007       CC = ISD::SETGE;
5008       break;
5009     case Intrinsic::x86_sse_comineq_ss:
5010     case Intrinsic::x86_sse2_comineq_sd:
5011       Opc = X86ISD::COMI;
5012       CC = ISD::SETNE;
5013       break;
5014     case Intrinsic::x86_sse_ucomieq_ss:
5015     case Intrinsic::x86_sse2_ucomieq_sd:
5016       Opc = X86ISD::UCOMI;
5017       CC = ISD::SETEQ;
5018       break;
5019     case Intrinsic::x86_sse_ucomilt_ss:
5020     case Intrinsic::x86_sse2_ucomilt_sd:
5021       Opc = X86ISD::UCOMI;
5022       CC = ISD::SETLT;
5023       break;
5024     case Intrinsic::x86_sse_ucomile_ss:
5025     case Intrinsic::x86_sse2_ucomile_sd:
5026       Opc = X86ISD::UCOMI;
5027       CC = ISD::SETLE;
5028       break;
5029     case Intrinsic::x86_sse_ucomigt_ss:
5030     case Intrinsic::x86_sse2_ucomigt_sd:
5031       Opc = X86ISD::UCOMI;
5032       CC = ISD::SETGT;
5033       break;
5034     case Intrinsic::x86_sse_ucomige_ss:
5035     case Intrinsic::x86_sse2_ucomige_sd:
5036       Opc = X86ISD::UCOMI;
5037       CC = ISD::SETGE;
5038       break;
5039     case Intrinsic::x86_sse_ucomineq_ss:
5040     case Intrinsic::x86_sse2_ucomineq_sd:
5041       Opc = X86ISD::UCOMI;
5042       CC = ISD::SETNE;
5043       break;
5044     }
5045
5046     unsigned X86CC;
5047     SDOperand LHS = Op.getOperand(1);
5048     SDOperand RHS = Op.getOperand(2);
5049     translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
5050
5051     SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5052     SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5053                                   DAG.getConstant(X86CC, MVT::i8), Cond);
5054     return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
5055   }
5056   }
5057 }
5058
5059 SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5060   // Depths > 0 not supported yet!
5061   if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5062     return SDOperand();
5063   
5064   // Just load the return address
5065   SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5066   return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5067 }
5068
5069 SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5070   // Depths > 0 not supported yet!
5071   if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5072     return SDOperand();
5073     
5074   SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5075   return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI, 
5076                      DAG.getIntPtrConstant(4));
5077 }
5078
5079 SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5080                                                        SelectionDAG &DAG) {
5081   // Is not yet supported on x86-64
5082   if (Subtarget->is64Bit())
5083     return SDOperand();
5084   
5085   return DAG.getIntPtrConstant(8);
5086 }
5087
5088 SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5089 {
5090   assert(!Subtarget->is64Bit() &&
5091          "Lowering of eh_return builtin is not supported yet on x86-64");
5092     
5093   MachineFunction &MF = DAG.getMachineFunction();
5094   SDOperand Chain     = Op.getOperand(0);
5095   SDOperand Offset    = Op.getOperand(1);
5096   SDOperand Handler   = Op.getOperand(2);
5097
5098   SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5099                                     getPointerTy());
5100
5101   SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
5102                                     DAG.getIntPtrConstant(-4UL));
5103   StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5104   Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5105   Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
5106   MF.getRegInfo().addLiveOut(X86::ECX);
5107
5108   return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5109                      Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5110 }
5111
5112 SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5113                                              SelectionDAG &DAG) {
5114   SDOperand Root = Op.getOperand(0);
5115   SDOperand Trmp = Op.getOperand(1); // trampoline
5116   SDOperand FPtr = Op.getOperand(2); // nested function
5117   SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5118
5119   const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
5120
5121   const X86InstrInfo *TII =
5122     ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5123
5124   if (Subtarget->is64Bit()) {
5125     SDOperand OutChains[6];
5126
5127     // Large code-model.
5128
5129     const unsigned char JMP64r  = TII->getBaseOpcodeFor(X86::JMP64r);
5130     const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5131
5132     const unsigned char N86R10 =
5133       ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
5134     const unsigned char N86R11 =
5135       ((const X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
5136
5137     const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5138
5139     // Load the pointer to the nested function into R11.
5140     unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5141     SDOperand Addr = Trmp;
5142     OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5143                                 TrmpAddr, 0);
5144
5145     Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
5146     OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
5147
5148     // Load the 'nest' parameter value into R10.
5149     // R10 is specified in X86CallingConv.td
5150     OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5151     Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5152     OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5153                                 TrmpAddr, 10);
5154
5155     Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
5156     OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
5157
5158     // Jump to the nested function.
5159     OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5160     Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5161     OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
5162                                 TrmpAddr, 20);
5163
5164     unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5165     Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5166     OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
5167                                 TrmpAddr, 22);
5168
5169     SDOperand Ops[] =
5170       { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
5171     return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5172   } else {
5173     const Function *Func =
5174       cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5175     unsigned CC = Func->getCallingConv();
5176     unsigned NestReg;
5177
5178     switch (CC) {
5179     default:
5180       assert(0 && "Unsupported calling convention");
5181     case CallingConv::C:
5182     case CallingConv::X86_StdCall: {
5183       // Pass 'nest' parameter in ECX.
5184       // Must be kept in sync with X86CallingConv.td
5185       NestReg = X86::ECX;
5186
5187       // Check that ECX wasn't needed by an 'inreg' parameter.
5188       const FunctionType *FTy = Func->getFunctionType();
5189       const ParamAttrsList *Attrs = Func->getParamAttrs();
5190
5191       if (Attrs && !Func->isVarArg()) {
5192         unsigned InRegCount = 0;
5193         unsigned Idx = 1;
5194
5195         for (FunctionType::param_iterator I = FTy->param_begin(),
5196              E = FTy->param_end(); I != E; ++I, ++Idx)
5197           if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
5198             // FIXME: should only count parameters that are lowered to integers.
5199             InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5200
5201         if (InRegCount > 2) {
5202           cerr << "Nest register in use - reduce number of inreg parameters!\n";
5203           abort();
5204         }
5205       }
5206       break;
5207     }
5208     case CallingConv::X86_FastCall:
5209       // Pass 'nest' parameter in EAX.
5210       // Must be kept in sync with X86CallingConv.td
5211       NestReg = X86::EAX;
5212       break;
5213     }
5214
5215     SDOperand OutChains[4];
5216     SDOperand Addr, Disp;
5217
5218     Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5219     Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5220
5221     const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5222     const unsigned char N86Reg =
5223       ((const X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
5224     OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
5225                                 Trmp, TrmpAddr, 0);
5226
5227     Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5228     OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
5229
5230     const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
5231     Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5232     OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5233                                 TrmpAddr, 5, false, 1);
5234
5235     Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5236     OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
5237
5238     SDOperand Ops[] =
5239       { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5240     return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
5241   }
5242 }
5243
5244 SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
5245   /*
5246    The rounding mode is in bits 11:10 of FPSR, and has the following
5247    settings:
5248      00 Round to nearest
5249      01 Round to -inf
5250      10 Round to +inf
5251      11 Round to 0
5252
5253   FLT_ROUNDS, on the other hand, expects the following:
5254     -1 Undefined
5255      0 Round to 0
5256      1 Round to nearest
5257      2 Round to +inf
5258      3 Round to -inf
5259
5260   To perform the conversion, we do:
5261     (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5262   */
5263
5264   MachineFunction &MF = DAG.getMachineFunction();
5265   const TargetMachine &TM = MF.getTarget();
5266   const TargetFrameInfo &TFI = *TM.getFrameInfo();
5267   unsigned StackAlignment = TFI.getStackAlignment();
5268   MVT::ValueType VT = Op.getValueType();
5269
5270   // Save FP Control Word to stack slot
5271   int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5272   SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5273
5274   SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5275                                 DAG.getEntryNode(), StackSlot);
5276
5277   // Load FP Control Word from stack slot
5278   SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5279
5280   // Transform as necessary
5281   SDOperand CWD1 =
5282     DAG.getNode(ISD::SRL, MVT::i16,
5283                 DAG.getNode(ISD::AND, MVT::i16,
5284                             CWD, DAG.getConstant(0x800, MVT::i16)),
5285                 DAG.getConstant(11, MVT::i8));
5286   SDOperand CWD2 =
5287     DAG.getNode(ISD::SRL, MVT::i16,
5288                 DAG.getNode(ISD::AND, MVT::i16,
5289                             CWD, DAG.getConstant(0x400, MVT::i16)),
5290                 DAG.getConstant(9, MVT::i8));
5291
5292   SDOperand RetVal =
5293     DAG.getNode(ISD::AND, MVT::i16,
5294                 DAG.getNode(ISD::ADD, MVT::i16,
5295                             DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5296                             DAG.getConstant(1, MVT::i16)),
5297                 DAG.getConstant(3, MVT::i16));
5298
5299
5300   return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5301                       ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5302 }
5303
5304 SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5305   MVT::ValueType VT = Op.getValueType();
5306   MVT::ValueType OpVT = VT;
5307   unsigned NumBits = MVT::getSizeInBits(VT);
5308
5309   Op = Op.getOperand(0);
5310   if (VT == MVT::i8) {
5311     // Zero extend to i32 since there is not an i8 bsr.
5312     OpVT = MVT::i32;
5313     Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5314   }
5315
5316   // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5317   SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5318   Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5319
5320   // If src is zero (i.e. bsr sets ZF), returns NumBits.
5321   SmallVector<SDOperand, 4> Ops;
5322   Ops.push_back(Op);
5323   Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5324   Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5325   Ops.push_back(Op.getValue(1));
5326   Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5327
5328   // Finally xor with NumBits-1.
5329   Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5330
5331   if (VT == MVT::i8)
5332     Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5333   return Op;
5334 }
5335
5336 SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5337   MVT::ValueType VT = Op.getValueType();
5338   MVT::ValueType OpVT = VT;
5339   unsigned NumBits = MVT::getSizeInBits(VT);
5340
5341   Op = Op.getOperand(0);
5342   if (VT == MVT::i8) {
5343     OpVT = MVT::i32;
5344     Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5345   }
5346
5347   // Issue a bsf (scan bits forward) which also sets EFLAGS.
5348   SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5349   Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5350
5351   // If src is zero (i.e. bsf sets ZF), returns NumBits.
5352   SmallVector<SDOperand, 4> Ops;
5353   Ops.push_back(Op);
5354   Ops.push_back(DAG.getConstant(NumBits, OpVT));
5355   Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5356   Ops.push_back(Op.getValue(1));
5357   Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5358
5359   if (VT == MVT::i8)
5360     Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5361   return Op;
5362 }
5363
5364 SDOperand X86TargetLowering::LowerLCS(SDOperand Op, SelectionDAG &DAG) {
5365   MVT::ValueType T = cast<AtomicSDNode>(Op.Val)->getVT();
5366   unsigned Reg = 0;
5367   unsigned size = 0;
5368   switch(T) {
5369   case MVT::i8:  Reg = X86::AL;  size = 1; break;
5370   case MVT::i16: Reg = X86::AX;  size = 2; break;
5371   case MVT::i32: Reg = X86::EAX; size = 4; break;
5372   case MVT::i64: 
5373     if (Subtarget->is64Bit()) {
5374       Reg = X86::RAX; size = 8;
5375     } else //Should go away when LowerType stuff lands
5376       return SDOperand(ExpandATOMIC_LCS(Op.Val, DAG), 0);
5377     break;
5378   };
5379   SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5380                                     Op.getOperand(3), SDOperand());
5381   SDOperand Ops[] = { cpIn.getValue(0),
5382                       Op.getOperand(1),
5383                       Op.getOperand(2),
5384                       DAG.getTargetConstant(size, MVT::i8),
5385                       cpIn.getValue(1) };
5386   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5387   SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5388   SDOperand cpOut = 
5389     DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5390   return cpOut;
5391 }
5392
5393 SDNode* X86TargetLowering::ExpandATOMIC_LCS(SDNode* Op, SelectionDAG &DAG) {
5394   MVT::ValueType T = cast<AtomicSDNode>(Op)->getVT();
5395   assert (T == MVT::i64 && "Only know how to expand i64 CAS");
5396   SDOperand cpInL, cpInH;
5397   cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5398                       DAG.getConstant(0, MVT::i32));
5399   cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5400                       DAG.getConstant(1, MVT::i32));
5401   cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5402                            cpInL, SDOperand());
5403   cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5404                            cpInH, cpInL.getValue(1));
5405   SDOperand swapInL, swapInH;
5406   swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5407                         DAG.getConstant(0, MVT::i32));
5408   swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5409                         DAG.getConstant(1, MVT::i32));
5410   swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5411                              swapInL, cpInH.getValue(1));
5412   swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5413                              swapInH, swapInL.getValue(1));
5414   SDOperand Ops[] = { swapInH.getValue(0),
5415                       Op->getOperand(1),
5416                       swapInH.getValue(1)};
5417   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5418   SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5419   SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32, 
5420                                         Result.getValue(1));
5421   SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32, 
5422                                         cpOutL.getValue(2));
5423   SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5424   SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5425   Tys = DAG.getVTList(MVT::i64, MVT::Other);
5426   return DAG.getNode(ISD::MERGE_VALUES, Tys, ResultVal, cpOutH.getValue(1)).Val;
5427 }
5428
5429 /// LowerOperation - Provide custom lowering hooks for some operations.
5430 ///
5431 SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5432   switch (Op.getOpcode()) {
5433   default: assert(0 && "Should not custom lower this!");
5434   case ISD::ATOMIC_LCS:         return LowerLCS(Op,DAG);
5435   case ISD::BUILD_VECTOR:       return LowerBUILD_VECTOR(Op, DAG);
5436   case ISD::VECTOR_SHUFFLE:     return LowerVECTOR_SHUFFLE(Op, DAG);
5437   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5438   case ISD::INSERT_VECTOR_ELT:  return LowerINSERT_VECTOR_ELT(Op, DAG);
5439   case ISD::SCALAR_TO_VECTOR:   return LowerSCALAR_TO_VECTOR(Op, DAG);
5440   case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
5441   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
5442   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
5443   case ISD::ExternalSymbol:     return LowerExternalSymbol(Op, DAG);
5444   case ISD::SHL_PARTS:
5445   case ISD::SRA_PARTS:
5446   case ISD::SRL_PARTS:          return LowerShift(Op, DAG);
5447   case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
5448   case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG);
5449   case ISD::FABS:               return LowerFABS(Op, DAG);
5450   case ISD::FNEG:               return LowerFNEG(Op, DAG);
5451   case ISD::FCOPYSIGN:          return LowerFCOPYSIGN(Op, DAG);
5452   case ISD::SETCC:              return LowerSETCC(Op, DAG);
5453   case ISD::SELECT:             return LowerSELECT(Op, DAG);
5454   case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
5455   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
5456   case ISD::CALL:               return LowerCALL(Op, DAG);
5457   case ISD::RET:                return LowerRET(Op, DAG);
5458   case ISD::FORMAL_ARGUMENTS:   return LowerFORMAL_ARGUMENTS(Op, DAG);
5459   case ISD::MEMSET:             return LowerMEMSET(Op, DAG);
5460   case ISD::MEMCPY:             return LowerMEMCPY(Op, DAG);
5461   case ISD::VASTART:            return LowerVASTART(Op, DAG);
5462   case ISD::VACOPY:             return LowerVACOPY(Op, DAG);
5463   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
5464   case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
5465   case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
5466   case ISD::FRAME_TO_ARGS_OFFSET:
5467                                 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
5468   case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
5469   case ISD::EH_RETURN:          return LowerEH_RETURN(Op, DAG);
5470   case ISD::TRAMPOLINE:         return LowerTRAMPOLINE(Op, DAG);
5471   case ISD::FLT_ROUNDS_:        return LowerFLT_ROUNDS_(Op, DAG);
5472   case ISD::CTLZ:               return LowerCTLZ(Op, DAG);
5473   case ISD::CTTZ:               return LowerCTTZ(Op, DAG);
5474       
5475   // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5476   case ISD::READCYCLECOUNTER:
5477     return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
5478   }
5479 }
5480
5481 /// ExpandOperation - Provide custom lowering hooks for expanding operations.
5482 SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5483   switch (N->getOpcode()) {
5484   default: assert(0 && "Should not custom lower this!");
5485   case ISD::FP_TO_SINT:         return ExpandFP_TO_SINT(N, DAG);
5486   case ISD::READCYCLECOUNTER:   return ExpandREADCYCLECOUNTER(N, DAG);
5487   case ISD::ATOMIC_LCS:         return ExpandATOMIC_LCS(N, DAG);
5488   }
5489 }
5490
5491 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5492   switch (Opcode) {
5493   default: return NULL;
5494   case X86ISD::BSF:                return "X86ISD::BSF";
5495   case X86ISD::BSR:                return "X86ISD::BSR";
5496   case X86ISD::SHLD:               return "X86ISD::SHLD";
5497   case X86ISD::SHRD:               return "X86ISD::SHRD";
5498   case X86ISD::FAND:               return "X86ISD::FAND";
5499   case X86ISD::FOR:                return "X86ISD::FOR";
5500   case X86ISD::FXOR:               return "X86ISD::FXOR";
5501   case X86ISD::FSRL:               return "X86ISD::FSRL";
5502   case X86ISD::FILD:               return "X86ISD::FILD";
5503   case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
5504   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5505   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5506   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
5507   case X86ISD::FLD:                return "X86ISD::FLD";
5508   case X86ISD::FST:                return "X86ISD::FST";
5509   case X86ISD::FP_GET_RESULT:      return "X86ISD::FP_GET_RESULT";
5510   case X86ISD::FP_GET_RESULT2:     return "X86ISD::FP_GET_RESULT2";
5511   case X86ISD::FP_SET_RESULT:      return "X86ISD::FP_SET_RESULT";
5512   case X86ISD::CALL:               return "X86ISD::CALL";
5513   case X86ISD::TAILCALL:           return "X86ISD::TAILCALL";
5514   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
5515   case X86ISD::CMP:                return "X86ISD::CMP";
5516   case X86ISD::COMI:               return "X86ISD::COMI";
5517   case X86ISD::UCOMI:              return "X86ISD::UCOMI";
5518   case X86ISD::SETCC:              return "X86ISD::SETCC";
5519   case X86ISD::CMOV:               return "X86ISD::CMOV";
5520   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
5521   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
5522   case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
5523   case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
5524   case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
5525   case X86ISD::Wrapper:            return "X86ISD::Wrapper";
5526   case X86ISD::PEXTRB:             return "X86ISD::PEXTRB";
5527   case X86ISD::PEXTRW:             return "X86ISD::PEXTRW";
5528   case X86ISD::INSERTPS:           return "X86ISD::INSERTPS";
5529   case X86ISD::PINSRB:             return "X86ISD::PINSRB";
5530   case X86ISD::PINSRW:             return "X86ISD::PINSRW";
5531   case X86ISD::FMAX:               return "X86ISD::FMAX";
5532   case X86ISD::FMIN:               return "X86ISD::FMIN";
5533   case X86ISD::FRSQRT:             return "X86ISD::FRSQRT";
5534   case X86ISD::FRCP:               return "X86ISD::FRCP";
5535   case X86ISD::TLSADDR:            return "X86ISD::TLSADDR";
5536   case X86ISD::THREAD_POINTER:     return "X86ISD::THREAD_POINTER";
5537   case X86ISD::EH_RETURN:          return "X86ISD::EH_RETURN";
5538   case X86ISD::TC_RETURN:          return "X86ISD::TC_RETURN";
5539   case X86ISD::FNSTCW16m:          return "X86ISD::FNSTCW16m";
5540   case X86ISD::LCMPXCHG_DAG:       return "x86ISD::LCMPXCHG_DAG";
5541   case X86ISD::LCMPXCHG8_DAG:      return "x86ISD::LCMPXCHG8_DAG";
5542   }
5543 }
5544
5545 // isLegalAddressingMode - Return true if the addressing mode represented
5546 // by AM is legal for this target, for a load/store of the specified type.
5547 bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM, 
5548                                               const Type *Ty) const {
5549   // X86 supports extremely general addressing modes.
5550   
5551   // X86 allows a sign-extended 32-bit immediate field as a displacement.
5552   if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5553     return false;
5554   
5555   if (AM.BaseGV) {
5556     // We can only fold this if we don't need an extra load.
5557     if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5558       return false;
5559
5560     // X86-64 only supports addr of globals in small code model.
5561     if (Subtarget->is64Bit()) {
5562       if (getTargetMachine().getCodeModel() != CodeModel::Small)
5563         return false;
5564       // If lower 4G is not available, then we must use rip-relative addressing.
5565       if (AM.BaseOffs || AM.Scale > 1)
5566         return false;
5567     }
5568   }
5569   
5570   switch (AM.Scale) {
5571   case 0:
5572   case 1:
5573   case 2:
5574   case 4:
5575   case 8:
5576     // These scales always work.
5577     break;
5578   case 3:
5579   case 5:
5580   case 9:
5581     // These scales are formed with basereg+scalereg.  Only accept if there is
5582     // no basereg yet.
5583     if (AM.HasBaseReg)
5584       return false;
5585     break;
5586   default:  // Other stuff never works.
5587     return false;
5588   }
5589   
5590   return true;
5591 }
5592
5593
5594 bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5595   if (!Ty1->isInteger() || !Ty2->isInteger())
5596     return false;
5597   unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5598   unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5599   if (NumBits1 <= NumBits2)
5600     return false;
5601   return Subtarget->is64Bit() || NumBits1 < 64;
5602 }
5603
5604 bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5605                                        MVT::ValueType VT2) const {
5606   if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5607     return false;
5608   unsigned NumBits1 = MVT::getSizeInBits(VT1);
5609   unsigned NumBits2 = MVT::getSizeInBits(VT2);
5610   if (NumBits1 <= NumBits2)
5611     return false;
5612   return Subtarget->is64Bit() || NumBits1 < 64;
5613 }
5614
5615 /// isShuffleMaskLegal - Targets can use this to indicate that they only
5616 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5617 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5618 /// are assumed to be legal.
5619 bool
5620 X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5621   // Only do shuffles on 128-bit vector types for now.
5622   if (MVT::getSizeInBits(VT) == 64) return false;
5623   return (Mask.Val->getNumOperands() <= 4 ||
5624           isIdentityMask(Mask.Val) ||
5625           isIdentityMask(Mask.Val, true) ||
5626           isSplatMask(Mask.Val)  ||
5627           isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5628           X86::isUNPCKLMask(Mask.Val) ||
5629           X86::isUNPCKHMask(Mask.Val) ||
5630           X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
5631           X86::isUNPCKH_v_undef_Mask(Mask.Val));
5632 }
5633
5634 bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5635                                                MVT::ValueType EVT,
5636                                                SelectionDAG &DAG) const {
5637   unsigned NumElts = BVOps.size();
5638   // Only do shuffles on 128-bit vector types for now.
5639   if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5640   if (NumElts == 2) return true;
5641   if (NumElts == 4) {
5642     return (isMOVLMask(&BVOps[0], 4)  ||
5643             isCommutedMOVL(&BVOps[0], 4, true) ||
5644             isSHUFPMask(&BVOps[0], 4) || 
5645             isCommutedSHUFP(&BVOps[0], 4));
5646   }
5647   return false;
5648 }
5649
5650 //===----------------------------------------------------------------------===//
5651 //                           X86 Scheduler Hooks
5652 //===----------------------------------------------------------------------===//
5653
5654 MachineBasicBlock *
5655 X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5656                                                MachineBasicBlock *BB) {
5657   const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5658   switch (MI->getOpcode()) {
5659   default: assert(false && "Unexpected instr type to insert");
5660   case X86::CMOV_FR32:
5661   case X86::CMOV_FR64:
5662   case X86::CMOV_V4F32:
5663   case X86::CMOV_V2F64:
5664   case X86::CMOV_V2I64: {
5665     // To "insert" a SELECT_CC instruction, we actually have to insert the
5666     // diamond control-flow pattern.  The incoming instruction knows the
5667     // destination vreg to set, the condition code register to branch on, the
5668     // true/false values to select between, and a branch opcode to use.
5669     const BasicBlock *LLVM_BB = BB->getBasicBlock();
5670     ilist<MachineBasicBlock>::iterator It = BB;
5671     ++It;
5672
5673     //  thisMBB:
5674     //  ...
5675     //   TrueVal = ...
5676     //   cmpTY ccX, r1, r2
5677     //   bCC copy1MBB
5678     //   fallthrough --> copy0MBB
5679     MachineBasicBlock *thisMBB = BB;
5680     MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5681     MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
5682     unsigned Opc =
5683       X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
5684     BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
5685     MachineFunction *F = BB->getParent();
5686     F->getBasicBlockList().insert(It, copy0MBB);
5687     F->getBasicBlockList().insert(It, sinkMBB);
5688     // Update machine-CFG edges by first adding all successors of the current
5689     // block to the new block which will contain the Phi node for the select.
5690     for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
5691         e = BB->succ_end(); i != e; ++i)
5692       sinkMBB->addSuccessor(*i);
5693     // Next, remove all successors of the current block, and add the true
5694     // and fallthrough blocks as its successors.
5695     while(!BB->succ_empty())
5696       BB->removeSuccessor(BB->succ_begin());
5697     BB->addSuccessor(copy0MBB);
5698     BB->addSuccessor(sinkMBB);
5699
5700     //  copy0MBB:
5701     //   %FalseValue = ...
5702     //   # fallthrough to sinkMBB
5703     BB = copy0MBB;
5704
5705     // Update machine-CFG edges
5706     BB->addSuccessor(sinkMBB);
5707
5708     //  sinkMBB:
5709     //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5710     //  ...
5711     BB = sinkMBB;
5712     BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
5713       .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5714       .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5715
5716     delete MI;   // The pseudo instruction is gone now.
5717     return BB;
5718   }
5719
5720   case X86::FP32_TO_INT16_IN_MEM:
5721   case X86::FP32_TO_INT32_IN_MEM:
5722   case X86::FP32_TO_INT64_IN_MEM:
5723   case X86::FP64_TO_INT16_IN_MEM:
5724   case X86::FP64_TO_INT32_IN_MEM:
5725   case X86::FP64_TO_INT64_IN_MEM:
5726   case X86::FP80_TO_INT16_IN_MEM:
5727   case X86::FP80_TO_INT32_IN_MEM:
5728   case X86::FP80_TO_INT64_IN_MEM: {
5729     // Change the floating point control register to use "round towards zero"
5730     // mode when truncating to an integer value.
5731     MachineFunction *F = BB->getParent();
5732     int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
5733     addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
5734
5735     // Load the old value of the high byte of the control word...
5736     unsigned OldCW =
5737       F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
5738     addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
5739
5740     // Set the high part to be round to zero...
5741     addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5742       .addImm(0xC7F);
5743
5744     // Reload the modified control word now...
5745     addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5746
5747     // Restore the memory image of control word to original value
5748     addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5749       .addReg(OldCW);
5750
5751     // Get the X86 opcode to use.
5752     unsigned Opc;
5753     switch (MI->getOpcode()) {
5754     default: assert(0 && "illegal opcode!");
5755     case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5756     case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5757     case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5758     case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5759     case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5760     case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
5761     case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5762     case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5763     case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
5764     }
5765
5766     X86AddressMode AM;
5767     MachineOperand &Op = MI->getOperand(0);
5768     if (Op.isRegister()) {
5769       AM.BaseType = X86AddressMode::RegBase;
5770       AM.Base.Reg = Op.getReg();
5771     } else {
5772       AM.BaseType = X86AddressMode::FrameIndexBase;
5773       AM.Base.FrameIndex = Op.getIndex();
5774     }
5775     Op = MI->getOperand(1);
5776     if (Op.isImmediate())
5777       AM.Scale = Op.getImm();
5778     Op = MI->getOperand(2);
5779     if (Op.isImmediate())
5780       AM.IndexReg = Op.getImm();
5781     Op = MI->getOperand(3);
5782     if (Op.isGlobalAddress()) {
5783       AM.GV = Op.getGlobal();
5784     } else {
5785       AM.Disp = Op.getImm();
5786     }
5787     addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5788                       .addReg(MI->getOperand(4).getReg());
5789
5790     // Reload the original control word now.
5791     addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
5792
5793     delete MI;   // The pseudo instruction is gone now.
5794     return BB;
5795   }
5796   }
5797 }
5798
5799 //===----------------------------------------------------------------------===//
5800 //                           X86 Optimization Hooks
5801 //===----------------------------------------------------------------------===//
5802
5803 void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5804                                                        const APInt &Mask,
5805                                                        APInt &KnownZero,
5806                                                        APInt &KnownOne,
5807                                                        const SelectionDAG &DAG,
5808                                                        unsigned Depth) const {
5809   unsigned Opc = Op.getOpcode();
5810   assert((Opc >= ISD::BUILTIN_OP_END ||
5811           Opc == ISD::INTRINSIC_WO_CHAIN ||
5812           Opc == ISD::INTRINSIC_W_CHAIN ||
5813           Opc == ISD::INTRINSIC_VOID) &&
5814          "Should use MaskedValueIsZero if you don't know whether Op"
5815          " is a target node!");
5816
5817   KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0);   // Don't know anything.
5818   switch (Opc) {
5819   default: break;
5820   case X86ISD::SETCC:
5821     KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
5822                                        Mask.getBitWidth() - 1);
5823     break;
5824   }
5825 }
5826
5827 /// getShuffleScalarElt - Returns the scalar element that will make up the ith
5828 /// element of the result of the vector shuffle.
5829 static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5830   MVT::ValueType VT = N->getValueType(0);
5831   SDOperand PermMask = N->getOperand(2);
5832   unsigned NumElems = PermMask.getNumOperands();
5833   SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5834   i %= NumElems;
5835   if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5836     return (i == 0)
5837      ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5838   } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5839     SDOperand Idx = PermMask.getOperand(i);
5840     if (Idx.getOpcode() == ISD::UNDEF)
5841       return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
5842     return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5843   }
5844   return SDOperand();
5845 }
5846
5847 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5848 /// node is a GlobalAddress + an offset.
5849 static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
5850   unsigned Opc = N->getOpcode();
5851   if (Opc == X86ISD::Wrapper) {
5852     if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5853       GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5854       return true;
5855     }
5856   } else if (Opc == ISD::ADD) {
5857     SDOperand N1 = N->getOperand(0);
5858     SDOperand N2 = N->getOperand(1);
5859     if (isGAPlusOffset(N1.Val, GA, Offset)) {
5860       ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5861       if (V) {
5862         Offset += V->getSignExtended();
5863         return true;
5864       }
5865     } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5866       ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5867       if (V) {
5868         Offset += V->getSignExtended();
5869         return true;
5870       }
5871     }
5872   }
5873   return false;
5874 }
5875
5876 /// isConsecutiveLoad - Returns true if N is loading from an address of Base
5877 /// + Dist * Size.
5878 static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5879                               MachineFrameInfo *MFI) {
5880   if (N->getOperand(0).Val != Base->getOperand(0).Val)
5881     return false;
5882
5883   SDOperand Loc = N->getOperand(1);
5884   SDOperand BaseLoc = Base->getOperand(1);
5885   if (Loc.getOpcode() == ISD::FrameIndex) {
5886     if (BaseLoc.getOpcode() != ISD::FrameIndex)
5887       return false;
5888     int FI  = cast<FrameIndexSDNode>(Loc)->getIndex();
5889     int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5890     int FS  = MFI->getObjectSize(FI);
5891     int BFS = MFI->getObjectSize(BFI);
5892     if (FS != BFS || FS != Size) return false;
5893     return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5894   } else {
5895     GlobalValue *GV1 = NULL;
5896     GlobalValue *GV2 = NULL;
5897     int64_t Offset1 = 0;
5898     int64_t Offset2 = 0;
5899     bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5900     bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5901     if (isGA1 && isGA2 && GV1 == GV2)
5902       return Offset1 == (Offset2 + Dist*Size);
5903   }
5904
5905   return false;
5906 }
5907
5908 static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5909                               const X86Subtarget *Subtarget) {
5910   GlobalValue *GV;
5911   int64_t Offset = 0;
5912   if (isGAPlusOffset(Base, GV, Offset))
5913     return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5914   // DAG combine handles the stack object case.
5915   return false;
5916 }
5917
5918
5919 /// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5920 /// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5921 /// if the load addresses are consecutive, non-overlapping, and in the right
5922 /// order.
5923 static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5924                                        const X86Subtarget *Subtarget) {
5925   MachineFunction &MF = DAG.getMachineFunction();
5926   MachineFrameInfo *MFI = MF.getFrameInfo();
5927   MVT::ValueType VT = N->getValueType(0);
5928   MVT::ValueType EVT = MVT::getVectorElementType(VT);
5929   SDOperand PermMask = N->getOperand(2);
5930   int NumElems = (int)PermMask.getNumOperands();
5931   SDNode *Base = NULL;
5932   for (int i = 0; i < NumElems; ++i) {
5933     SDOperand Idx = PermMask.getOperand(i);
5934     if (Idx.getOpcode() == ISD::UNDEF) {
5935       if (!Base) return SDOperand();
5936     } else {
5937       SDOperand Arg =
5938         getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
5939       if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
5940         return SDOperand();
5941       if (!Base)
5942         Base = Arg.Val;
5943       else if (!isConsecutiveLoad(Arg.Val, Base,
5944                                   i, MVT::getSizeInBits(EVT)/8,MFI))
5945         return SDOperand();
5946     }
5947   }
5948
5949   bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
5950   LoadSDNode *LD = cast<LoadSDNode>(Base);
5951   if (isAlign16) {
5952     return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5953                        LD->getSrcValueOffset(), LD->isVolatile());
5954   } else {
5955     return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5956                        LD->getSrcValueOffset(), LD->isVolatile(),
5957                        LD->getAlignment());
5958   }
5959 }
5960
5961 /// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5962 static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5963                                       const X86Subtarget *Subtarget) {
5964   SDOperand Cond = N->getOperand(0);
5965
5966   // If we have SSE[12] support, try to form min/max nodes.
5967   if (Subtarget->hasSSE2() &&
5968       (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5969     if (Cond.getOpcode() == ISD::SETCC) {
5970       // Get the LHS/RHS of the select.
5971       SDOperand LHS = N->getOperand(1);
5972       SDOperand RHS = N->getOperand(2);
5973       ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
5974
5975       unsigned Opcode = 0;
5976       if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
5977         switch (CC) {
5978         default: break;
5979         case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5980         case ISD::SETULE:
5981         case ISD::SETLE:
5982           if (!UnsafeFPMath) break;
5983           // FALL THROUGH.
5984         case ISD::SETOLT:  // (X olt/lt Y) ? X : Y -> min
5985         case ISD::SETLT:
5986           Opcode = X86ISD::FMIN;
5987           break;
5988
5989         case ISD::SETOGT: // (X > Y) ? X : Y -> max
5990         case ISD::SETUGT:
5991         case ISD::SETGT:
5992           if (!UnsafeFPMath) break;
5993           // FALL THROUGH.
5994         case ISD::SETUGE:  // (X uge/ge Y) ? X : Y -> max
5995         case ISD::SETGE:
5996           Opcode = X86ISD::FMAX;
5997           break;
5998         }
5999       } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
6000         switch (CC) {
6001         default: break;
6002         case ISD::SETOGT: // (X > Y) ? Y : X -> min
6003         case ISD::SETUGT:
6004         case ISD::SETGT:
6005           if (!UnsafeFPMath) break;
6006           // FALL THROUGH.
6007         case ISD::SETUGE:  // (X uge/ge Y) ? Y : X -> min
6008         case ISD::SETGE:
6009           Opcode = X86ISD::FMIN;
6010           break;
6011
6012         case ISD::SETOLE:   // (X <= Y) ? Y : X -> max
6013         case ISD::SETULE:
6014         case ISD::SETLE:
6015           if (!UnsafeFPMath) break;
6016           // FALL THROUGH.
6017         case ISD::SETOLT:   // (X olt/lt Y) ? Y : X -> max
6018         case ISD::SETLT:
6019           Opcode = X86ISD::FMAX;
6020           break;
6021         }
6022       }
6023
6024       if (Opcode)
6025         return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
6026     }
6027
6028   }
6029
6030   return SDOperand();
6031 }
6032
6033 /// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
6034 static SDOperand PerformSTORECombine(StoreSDNode *St, SelectionDAG &DAG,
6035                                      const X86Subtarget *Subtarget) {
6036   // Turn load->store of MMX types into GPR load/stores.  This avoids clobbering
6037   // the FP state in cases where an emms may be missing.
6038   // A preferable solution to the general problem is to figure out the right
6039   // places to insert EMMS.  This qualifies as a quick hack.
6040   if (MVT::isVector(St->getValue().getValueType()) && 
6041       MVT::getSizeInBits(St->getValue().getValueType()) == 64 &&
6042       isa<LoadSDNode>(St->getValue()) &&
6043       !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6044       St->getChain().hasOneUse() && !St->isVolatile()) {
6045     SDNode* LdVal = St->getValue().Val;
6046     LoadSDNode *Ld = 0;
6047     int TokenFactorIndex = -1;
6048     SmallVector<SDOperand, 8> Ops;
6049     SDNode* ChainVal = St->getChain().Val;
6050     // Must be a store of a load.  We currently handle two cases:  the load
6051     // is a direct child, and it's under an intervening TokenFactor.  It is
6052     // possible to dig deeper under nested TokenFactors.
6053     if (ChainVal == LdVal)
6054       Ld = cast<LoadSDNode>(St->getChain());
6055     else if (St->getValue().hasOneUse() &&
6056              ChainVal->getOpcode() == ISD::TokenFactor) {
6057       for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
6058         if (ChainVal->getOperand(i).Val == LdVal) {
6059           TokenFactorIndex = i;
6060           Ld = cast<LoadSDNode>(St->getValue());
6061         } else
6062           Ops.push_back(ChainVal->getOperand(i));
6063       }
6064     }
6065     if (Ld) {
6066       // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6067       if (Subtarget->is64Bit()) {
6068         SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(), 
6069                                       Ld->getBasePtr(), Ld->getSrcValue(), 
6070                                       Ld->getSrcValueOffset(), Ld->isVolatile(),
6071                                       Ld->getAlignment());
6072         SDOperand NewChain = NewLd.getValue(1);
6073         if (TokenFactorIndex != -1) {
6074           Ops.push_back(NewLd);
6075           NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], 
6076                                  Ops.size());
6077         }
6078         return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6079                             St->getSrcValue(), St->getSrcValueOffset(),
6080                             St->isVolatile(), St->getAlignment());
6081       }
6082
6083       // Otherwise, lower to two 32-bit copies.
6084       SDOperand LoAddr = Ld->getBasePtr();
6085       SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6086                                      DAG.getConstant(MVT::i32, 4));
6087
6088       SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6089                                    Ld->getSrcValue(), Ld->getSrcValueOffset(),
6090                                    Ld->isVolatile(), Ld->getAlignment());
6091       SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6092                                    Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6093                                    Ld->isVolatile(), 
6094                                    MinAlign(Ld->getAlignment(), 4));
6095
6096       SDOperand NewChain = LoLd.getValue(1);
6097       if (TokenFactorIndex != -1) {
6098         Ops.push_back(LoLd);
6099         Ops.push_back(HiLd);
6100         NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0], 
6101                                Ops.size());
6102       }
6103
6104       LoAddr = St->getBasePtr();
6105       HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
6106                            DAG.getConstant(MVT::i32, 4));
6107
6108       SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
6109                           St->getSrcValue(), St->getSrcValueOffset(),
6110                           St->isVolatile(), St->getAlignment());
6111       SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6112                                     St->getSrcValue(), St->getSrcValueOffset()+4,
6113                                     St->isVolatile(), 
6114                                     MinAlign(St->getAlignment(), 4));
6115       return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
6116     }
6117   }
6118   return SDOperand();
6119 }
6120
6121 /// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6122 /// X86ISD::FXOR nodes.
6123 static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
6124   assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6125   // F[X]OR(0.0, x) -> x
6126   // F[X]OR(x, 0.0) -> x
6127   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6128     if (C->getValueAPF().isPosZero())
6129       return N->getOperand(1);
6130   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6131     if (C->getValueAPF().isPosZero())
6132       return N->getOperand(0);
6133   return SDOperand();
6134 }
6135
6136 /// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6137 static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6138   // FAND(0.0, x) -> 0.0
6139   // FAND(x, 0.0) -> 0.0
6140   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6141     if (C->getValueAPF().isPosZero())
6142       return N->getOperand(0);
6143   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6144     if (C->getValueAPF().isPosZero())
6145       return N->getOperand(1);
6146   return SDOperand();
6147 }
6148
6149
6150 SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
6151                                                DAGCombinerInfo &DCI) const {
6152   SelectionDAG &DAG = DCI.DAG;
6153   switch (N->getOpcode()) {
6154   default: break;
6155   case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
6156   case ISD::SELECT:         return PerformSELECTCombine(N, DAG, Subtarget);
6157   case ISD::STORE:          
6158       return PerformSTORECombine(cast<StoreSDNode>(N), DAG, Subtarget);
6159   case X86ISD::FXOR:
6160   case X86ISD::FOR:         return PerformFORCombine(N, DAG);
6161   case X86ISD::FAND:        return PerformFANDCombine(N, DAG);
6162   }
6163
6164   return SDOperand();
6165 }
6166
6167 //===----------------------------------------------------------------------===//
6168 //                           X86 Inline Assembly Support
6169 //===----------------------------------------------------------------------===//
6170
6171 /// getConstraintType - Given a constraint letter, return the type of
6172 /// constraint it is for this target.
6173 X86TargetLowering::ConstraintType
6174 X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6175   if (Constraint.size() == 1) {
6176     switch (Constraint[0]) {
6177     case 'A':
6178     case 'r':
6179     case 'R':
6180     case 'l':
6181     case 'q':
6182     case 'Q':
6183     case 'x':
6184     case 'Y':
6185       return C_RegisterClass;
6186     default:
6187       break;
6188     }
6189   }
6190   return TargetLowering::getConstraintType(Constraint);
6191 }
6192
6193 /// LowerXConstraint - try to replace an X constraint, which matches anything,
6194 /// with another that has more specific requirements based on the type of the
6195 /// corresponding operand.
6196 void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT, 
6197                                          std::string& s) const {
6198   if (MVT::isFloatingPoint(ConstraintVT)) {
6199     if (Subtarget->hasSSE2())
6200       s = "Y";
6201     else if (Subtarget->hasSSE1())
6202       s = "x";
6203     else
6204       s = "f";
6205   } else
6206     return TargetLowering::lowerXConstraint(ConstraintVT, s);
6207 }
6208
6209 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6210 /// vector.  If it is invalid, don't add anything to Ops.
6211 void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6212                                                      char Constraint,
6213                                                      std::vector<SDOperand>&Ops,
6214                                                      SelectionDAG &DAG) {
6215   SDOperand Result(0, 0);
6216   
6217   switch (Constraint) {
6218   default: break;
6219   case 'I':
6220     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6221       if (C->getValue() <= 31) {
6222         Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6223         break;
6224       }
6225     }
6226     return;
6227   case 'N':
6228     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
6229       if (C->getValue() <= 255) {
6230         Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6231         break;
6232       }
6233     }
6234     return;
6235   case 'i': {
6236     // Literal immediates are always ok.
6237     if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6238       Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6239       break;
6240     }
6241
6242     // If we are in non-pic codegen mode, we allow the address of a global (with
6243     // an optional displacement) to be used with 'i'.
6244     GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6245     int64_t Offset = 0;
6246     
6247     // Match either (GA) or (GA+C)
6248     if (GA) {
6249       Offset = GA->getOffset();
6250     } else if (Op.getOpcode() == ISD::ADD) {
6251       ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6252       GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6253       if (C && GA) {
6254         Offset = GA->getOffset()+C->getValue();
6255       } else {
6256         C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6257         GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6258         if (C && GA)
6259           Offset = GA->getOffset()+C->getValue();
6260         else
6261           C = 0, GA = 0;
6262       }
6263     }
6264     
6265     if (GA) {
6266       // If addressing this global requires a load (e.g. in PIC mode), we can't
6267       // match.
6268       if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6269                                          false))
6270         return;
6271
6272       Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6273                                       Offset);
6274       Result = Op;
6275       break;
6276     }
6277
6278     // Otherwise, not valid for this mode.
6279     return;
6280   }
6281   }
6282   
6283   if (Result.Val) {
6284     Ops.push_back(Result);
6285     return;
6286   }
6287   return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
6288 }
6289
6290 std::vector<unsigned> X86TargetLowering::
6291 getRegClassForInlineAsmConstraint(const std::string &Constraint,
6292                                   MVT::ValueType VT) const {
6293   if (Constraint.size() == 1) {
6294     // FIXME: not handling fp-stack yet!
6295     switch (Constraint[0]) {      // GCC X86 Constraint Letters
6296     default: break;  // Unknown constraint letter
6297     case 'A':   // EAX/EDX
6298       if (VT == MVT::i32 || VT == MVT::i64)
6299         return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6300       break;
6301     case 'q':   // Q_REGS (GENERAL_REGS in 64-bit mode)
6302     case 'Q':   // Q_REGS
6303       if (VT == MVT::i32)
6304         return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6305       else if (VT == MVT::i16)
6306         return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6307       else if (VT == MVT::i8)
6308         return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
6309       else if (VT == MVT::i64)
6310         return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6311       break;
6312     }
6313   }
6314
6315   return std::vector<unsigned>();
6316 }
6317
6318 std::pair<unsigned, const TargetRegisterClass*>
6319 X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
6320                                                 MVT::ValueType VT) const {
6321   // First, see if this is a constraint that directly corresponds to an LLVM
6322   // register class.
6323   if (Constraint.size() == 1) {
6324     // GCC Constraint Letters
6325     switch (Constraint[0]) {
6326     default: break;
6327     case 'r':   // GENERAL_REGS
6328     case 'R':   // LEGACY_REGS
6329     case 'l':   // INDEX_REGS
6330       if (VT == MVT::i64 && Subtarget->is64Bit())
6331         return std::make_pair(0U, X86::GR64RegisterClass);
6332       if (VT == MVT::i32)
6333         return std::make_pair(0U, X86::GR32RegisterClass);
6334       else if (VT == MVT::i16)
6335         return std::make_pair(0U, X86::GR16RegisterClass);
6336       else if (VT == MVT::i8)
6337         return std::make_pair(0U, X86::GR8RegisterClass);
6338       break;
6339     case 'y':   // MMX_REGS if MMX allowed.
6340       if (!Subtarget->hasMMX()) break;
6341       return std::make_pair(0U, X86::VR64RegisterClass);
6342       break;
6343     case 'Y':   // SSE_REGS if SSE2 allowed
6344       if (!Subtarget->hasSSE2()) break;
6345       // FALL THROUGH.
6346     case 'x':   // SSE_REGS if SSE1 allowed
6347       if (!Subtarget->hasSSE1()) break;
6348       
6349       switch (VT) {
6350       default: break;
6351       // Scalar SSE types.
6352       case MVT::f32:
6353       case MVT::i32:
6354         return std::make_pair(0U, X86::FR32RegisterClass);
6355       case MVT::f64:
6356       case MVT::i64:
6357         return std::make_pair(0U, X86::FR64RegisterClass);
6358       // Vector types.
6359       case MVT::v16i8:
6360       case MVT::v8i16:
6361       case MVT::v4i32:
6362       case MVT::v2i64:
6363       case MVT::v4f32:
6364       case MVT::v2f64:
6365         return std::make_pair(0U, X86::VR128RegisterClass);
6366       }
6367       break;
6368     }
6369   }
6370   
6371   // Use the default implementation in TargetLowering to convert the register
6372   // constraint into a member of a register class.
6373   std::pair<unsigned, const TargetRegisterClass*> Res;
6374   Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
6375
6376   // Not found as a standard register?
6377   if (Res.second == 0) {
6378     // GCC calls "st(0)" just plain "st".
6379     if (StringsEqualNoCase("{st}", Constraint)) {
6380       Res.first = X86::ST0;
6381       Res.second = X86::RFP80RegisterClass;
6382     }
6383
6384     return Res;
6385   }
6386
6387   // Otherwise, check to see if this is a register class of the wrong value
6388   // type.  For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6389   // turn into {ax},{dx}.
6390   if (Res.second->hasType(VT))
6391     return Res;   // Correct type already, nothing to do.
6392
6393   // All of the single-register GCC register classes map their values onto
6394   // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp".  If we
6395   // really want an 8-bit or 32-bit register, map to the appropriate register
6396   // class and return the appropriate register.
6397   if (Res.second != X86::GR16RegisterClass)
6398     return Res;
6399
6400   if (VT == MVT::i8) {
6401     unsigned DestReg = 0;
6402     switch (Res.first) {
6403     default: break;
6404     case X86::AX: DestReg = X86::AL; break;
6405     case X86::DX: DestReg = X86::DL; break;
6406     case X86::CX: DestReg = X86::CL; break;
6407     case X86::BX: DestReg = X86::BL; break;
6408     }
6409     if (DestReg) {
6410       Res.first = DestReg;
6411       Res.second = Res.second = X86::GR8RegisterClass;
6412     }
6413   } else if (VT == MVT::i32) {
6414     unsigned DestReg = 0;
6415     switch (Res.first) {
6416     default: break;
6417     case X86::AX: DestReg = X86::EAX; break;
6418     case X86::DX: DestReg = X86::EDX; break;
6419     case X86::CX: DestReg = X86::ECX; break;
6420     case X86::BX: DestReg = X86::EBX; break;
6421     case X86::SI: DestReg = X86::ESI; break;
6422     case X86::DI: DestReg = X86::EDI; break;
6423     case X86::BP: DestReg = X86::EBP; break;
6424     case X86::SP: DestReg = X86::ESP; break;
6425     }
6426     if (DestReg) {
6427       Res.first = DestReg;
6428       Res.second = Res.second = X86::GR32RegisterClass;
6429     }
6430   } else if (VT == MVT::i64) {
6431     unsigned DestReg = 0;
6432     switch (Res.first) {
6433     default: break;
6434     case X86::AX: DestReg = X86::RAX; break;
6435     case X86::DX: DestReg = X86::RDX; break;
6436     case X86::CX: DestReg = X86::RCX; break;
6437     case X86::BX: DestReg = X86::RBX; break;
6438     case X86::SI: DestReg = X86::RSI; break;
6439     case X86::DI: DestReg = X86::RDI; break;
6440     case X86::BP: DestReg = X86::RBP; break;
6441     case X86::SP: DestReg = X86::RSP; break;
6442     }
6443     if (DestReg) {
6444       Res.first = DestReg;
6445       Res.second = Res.second = X86::GR64RegisterClass;
6446     }
6447   }
6448
6449   return Res;
6450 }