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